package com.ruoyi.trade.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.report.vo.IpcStatisticsInfoVo;
import com.ruoyi.trade.domain.CTrademark;
import com.ruoyi.trade.service.ICTrademarkExcel;
import com.ruoyi.trade.service.ICTrademarkService;
import com.ruoyi.trade.vo.TrademarkDTO;
import com.ruoyi.trade.vo.TrademarkVO;

/**
 * 中国商标Controller
 *
 * @author ruoyi
 * @date 2023-04-28
 */
@RestController
@RequestMapping("/trade/cTrademark")
public class CTrademarkController extends BaseController {
	private final ICTrademarkService cTrademarkService;

	@Autowired
	private ICTrademarkExcel cTrademarkExcel;

	public CTrademarkController(ICTrademarkService cTrademarkService) {
		this.cTrademarkService = cTrademarkService;
	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@GetMapping("/listReportNcGrowth")
	public AjaxResult listReportNcGrowth(CTrademark cTrademark) {
		if (StringUtils.isBlank(cTrademark.getBeginDate1()) || StringUtils.isBlank(cTrademark.getEndDate1())
				|| StringUtils.isBlank(cTrademark.getBeginDate2()) || StringUtils.isBlank(cTrademark.getEndDate2())) {
			return AjaxResult.error(500, "请指定对比周期");
		}
		cTrademark.setBeginDate1(cTrademark.getBeginDate1() + "16");
		cTrademark.setEndDate1(cTrademark.getEndDate1() + "15");

		cTrademark.setBeginDate2(cTrademark.getBeginDate2() + "16");
		cTrademark.setEndDate2(cTrademark.getEndDate2() + "15");
		String sumType = "province";
		if (StringUtils.isBlank(cTrademark.getProvince()) || StringUtils.isBlank(cTrademark.getAppliCity())
				|| "null".equals(cTrademark.getAppliCity())) {
			sumType = "province";
		} else if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && "all".equals(cTrademark.getAppliCity())) {
			sumType = "city";
//			cTrademark.setAppliCity(null);
		} else if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"all".equals(cTrademark.getAppliCity())
				&& !"null".equals(cTrademark.getAppliCity())) {
			sumType = "city";

			if (!"53".equals(cTrademark.getAppliCity().trim())) {
				cTrademark.setCityCode(cTrademark.getAppliCity().trim());
			}
			if (StringUtils.isNotBlank(cTrademark.getAppliCounty()) && "null".equals(cTrademark.getAppliCounty())) {
				sumType = "city";

			} else if (StringUtils.isNotBlank(cTrademark.getAppliCounty())
					&& "all".equals(cTrademark.getAppliCounty())) {
//				cTrademark.setAppliCounty(null);
				sumType = "area";
			}
		}
		cTrademark.setSumType(sumType);
//		 isPressHightValueList: [
//		                         { id: "2", name: "有效" },
//		                         { id: "1", name: "申请" },
//		                         { id: "0", name: "注册" },
//		isDeptHightValues

		if (StringUtils.isNotBlank(cTrademark.getIsDeptHightValues())) {
			if ("0".equals(cTrademark.getIsDeptHightValues())) {
				cTrademark.setQueryType("reg");
			} else if ("1".equals(cTrademark.getIsDeptHightValues())) {
				cTrademark.setQueryType("apl");
			} else if ("2".equals(cTrademark.getIsDeptHightValues())) {
				cTrademark.setQueryType("act");
			}
		}
//		对比数据
		cTrademark.setCurrDate(new SimpleDateFormat("yyyyMMdd").format(new Date()));
		Map<String, Object> map = cTrademarkService.listReportNcGrowth(cTrademark);

//		OutputStream out = null;
//		try {
//			out = new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//			cTrademarkExcel.exportReportNcGrowth(out, cTrademark, map);
//
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally {
//			try {
//				out.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
		return AjaxResult.success(map);
	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@PostMapping("/exportReportNcGrowth")
	public void exportReportNcGrowth(HttpServletResponse response, CTrademark cTrademark) {
		if (StringUtils.isBlank(cTrademark.getBeginDate1()) || StringUtils.isBlank(cTrademark.getEndDate1())
				|| StringUtils.isBlank(cTrademark.getBeginDate2()) || StringUtils.isBlank(cTrademark.getEndDate2())) {
//			return AjaxResult.error(500, "请指定对比周期");
			throw new RuntimeException("请指定对比周期");
		}
		cTrademark.setBeginDate1(cTrademark.getBeginDate1() + "16");
		cTrademark.setEndDate1(cTrademark.getEndDate1() + "15");

		cTrademark.setBeginDate2(cTrademark.getBeginDate2() + "16");
		cTrademark.setEndDate2(cTrademark.getEndDate2() + "15");
		String sumType = "province";
		if (StringUtils.isBlank(cTrademark.getProvince()) || StringUtils.isBlank(cTrademark.getAppliCity())
				|| "null".equals(cTrademark.getAppliCity())) {
			sumType = "province";
		} else if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && "all".equals(cTrademark.getAppliCity())) {
			sumType = "city";
//			cTrademark.setAppliCity(null);
		} else if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"all".equals(cTrademark.getAppliCity())
				&& !"null".equals(cTrademark.getAppliCity())) {
			sumType = "city";

			if (!"53".equals(cTrademark.getAppliCity().trim())) {
				cTrademark.setCityCode(cTrademark.getAppliCity().trim());
			}
			if (StringUtils.isNotBlank(cTrademark.getAppliCounty()) && "null".equals(cTrademark.getAppliCounty())) {
				sumType = "city";

			} else if (StringUtils.isNotBlank(cTrademark.getAppliCounty())
					&& "all".equals(cTrademark.getAppliCounty())) {
//				cTrademark.setAppliCounty(null);
				sumType = "area";
			}
		}
		cTrademark.setSumType(sumType);
//		 isPressHightValueList: [
//		                         { id: "2", name: "有效" },
//		                         { id: "1", name: "申请" },
//		                         { id: "0", name: "注册" },
//		isDeptHightValues

		if (StringUtils.isNotBlank(cTrademark.getIsDeptHightValues())) {
			if ("0".equals(cTrademark.getIsDeptHightValues())) {
				cTrademark.setQueryType("reg");
			} else if ("1".equals(cTrademark.getIsDeptHightValues())) {
				cTrademark.setQueryType("apl");
			} else if ("2".equals(cTrademark.getIsDeptHightValues())) {
				cTrademark.setQueryType("act");
			}
		}
//		对比数据
		cTrademark.setCurrDate(new SimpleDateFormat("yyyyMMdd").format(new Date()));
		Map<String, Object> map = cTrademarkService.listReportNcGrowth(cTrademark);

		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("utf-8");

		try {

			cTrademarkExcel.exportReportNcGrowth(response.getOutputStream(), cTrademark, map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("导出失败," + e.getMessage(), e);
			throw new RuntimeException("导出失败，请联系管理员", e);
		}

	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@GetMapping("/listReportGrowth")
	public AjaxResult listReportGrowth(CTrademark cTrademark) {
		if (StringUtils.isBlank(cTrademark.getBeginDate1()) || StringUtils.isBlank(cTrademark.getEndDate1())
				|| StringUtils.isBlank(cTrademark.getBeginDate2()) || StringUtils.isBlank(cTrademark.getEndDate2())) {
			return AjaxResult.error(500, "请指定对比周期");
		}

		cTrademark.setBeginDate1(cTrademark.getBeginDate1() + "16");
		cTrademark.setEndDate1(cTrademark.getEndDate1() + "15");

		cTrademark.setBeginDate2(cTrademark.getBeginDate2() + "16");
		cTrademark.setEndDate2(cTrademark.getEndDate2() + "15");

		cTrademark.setCurrDate(new SimpleDateFormat("yyyyMMdd").format(new Date()));

		if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"53".equals(cTrademark.getAppliCity().trim())) {
			cTrademark.setCityCode(cTrademark.getAppliCity().trim());
		}
		String sumType = "province";
		if (StringUtils.isBlank(cTrademark.getProvince()) || StringUtils.isBlank(cTrademark.getAppliCity())
				|| "null".equals(cTrademark.getAppliCity())) {
			sumType = "province";
		} else if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && "all".equals(cTrademark.getAppliCity())) {
			sumType = "city";
		} else if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"all".equals(cTrademark.getAppliCity())
				&& !"null".equals(cTrademark.getAppliCity())) {
			sumType = "city";
			if (!"53".equals(cTrademark.getAppliCity().trim())) {
				cTrademark.setCityCode(cTrademark.getAppliCity().trim());
			}
			if (StringUtils.isNotBlank(cTrademark.getAppliCounty()) && "null".equals(cTrademark.getAppliCounty())) {
				sumType = "city";
			} else if (StringUtils.isNotBlank(cTrademark.getAppliCounty())
					&& "all".equals(cTrademark.getAppliCounty())) {
				sumType = "area";
			}
		}
		cTrademark.setSumType(sumType);

		List<TrademarkDTO> list = cTrademarkService.listReportGrowth(cTrademark);

//		OutputStream out = null;
//		try {
//			out = new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//			cTrademarkExcel.exportReportGrowth(out, cTrademark, list);
//
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally {
//			try {
//				out.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}

		return AjaxResult.success(list);

	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@PostMapping("/exportReportGrowth")
	public void exportReportGrowth(HttpServletResponse response, CTrademark cTrademark) {
		if (StringUtils.isBlank(cTrademark.getBeginDate1()) || StringUtils.isBlank(cTrademark.getEndDate1())
				|| StringUtils.isBlank(cTrademark.getBeginDate2()) || StringUtils.isBlank(cTrademark.getEndDate2())) {
//			return AjaxResult.error(500, "请指定对比周期");
			throw new RuntimeException("请指定对比周期");
		}

		cTrademark.setBeginDate1(cTrademark.getBeginDate1() + "16");
		cTrademark.setEndDate1(cTrademark.getEndDate1() + "15");

		cTrademark.setBeginDate2(cTrademark.getBeginDate2() + "16");
		cTrademark.setEndDate2(cTrademark.getEndDate2() + "15");

		cTrademark.setCurrDate(new SimpleDateFormat("yyyyMMdd").format(new Date()));

		if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"53".equals(cTrademark.getAppliCity().trim())) {
			cTrademark.setCityCode(cTrademark.getAppliCity().trim());
		}

		String sumType = "province";
		if (StringUtils.isBlank(cTrademark.getProvince()) || StringUtils.isBlank(cTrademark.getAppliCity())
				|| "null".equals(cTrademark.getAppliCity())) {
			sumType = "province";
		} else if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && "all".equals(cTrademark.getAppliCity())) {
			sumType = "city";
		} else if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"all".equals(cTrademark.getAppliCity())
				&& !"null".equals(cTrademark.getAppliCity())) {
			sumType = "city";
			if (!"53".equals(cTrademark.getAppliCity().trim())) {
				cTrademark.setCityCode(cTrademark.getAppliCity().trim());
			}
			if (StringUtils.isNotBlank(cTrademark.getAppliCounty()) && "null".equals(cTrademark.getAppliCounty())) {
				sumType = "city";
			} else if (StringUtils.isNotBlank(cTrademark.getAppliCounty())
					&& "all".equals(cTrademark.getAppliCounty())) {
				sumType = "area";
			}
		}
		cTrademark.setSumType(sumType);

		List<TrademarkDTO> list = cTrademarkService.listReportGrowth(cTrademark);
//		return AjaxResult.success(list);
		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("utf-8");

		try {

			cTrademarkExcel.exportReportGrowth(response.getOutputStream(), cTrademark, list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("导出失败," + e.getMessage(), e);
			throw new RuntimeException("导出失败，请联系管理员", e);
		}

	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@GetMapping("/listReportApply")
	public AjaxResult listReportApply(CTrademark cTrademark) {
		String countTimeStr = "统计时期";
		if (StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeBegin())
				&& StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeEnd())) {
			countTimeStr = countTimeStr + "" + cTrademark.getApplyAdjustmentTimeBegin() + "至"
					+ cTrademark.getApplyAdjustmentTimeEnd();
		}
		countTimeStr = countTimeStr + "(每年范围为上年12月16日到本年12月15日)";

		cTrademark.setCountTimeStr(countTimeStr);
		if (StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeBegin())
				&& StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeEnd())) {
			cTrademark.setBeginDate(cTrademark.getApplyAdjustmentTimeBegin().replace("-", "") + "16");
			cTrademark.setEndDate(cTrademark.getApplyAdjustmentTimeEnd().replace("-", "") + "15");
		}
		if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"53".equals(cTrademark.getAppliCity().trim())) {
			cTrademark.setCityCode(cTrademark.getAppliCity().trim());
		}

		TrademarkDTO dto = cTrademarkService.listReportApply(cTrademark);

//		OutputStream out = null;
//		try {
//			out = new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//			cTrademarkExcel.exportReportApply(out, cTrademark, dto);
//
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally {
//			try {
//				out.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}

		return AjaxResult.success(dto);
	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@PostMapping("/exportReportApply")
	public void exportReportApply(HttpServletResponse response, CTrademark cTrademark) {
		String countTimeStr = "统计时期";
		if (StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeBegin())
				&& StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeEnd())) {
			countTimeStr = countTimeStr + "" + cTrademark.getApplyAdjustmentTimeBegin() + "至"
					+ cTrademark.getApplyAdjustmentTimeEnd();
			cTrademark.setBeginDate(cTrademark.getApplyAdjustmentTimeBegin().replace("-", "") + "16");
			cTrademark.setEndDate(cTrademark.getApplyAdjustmentTimeEnd().replace("-", "") + "15");
		}
		countTimeStr = countTimeStr + "(每年范围为上年12月16日到本年12月15日)";
		cTrademark.setCountTimeStr(countTimeStr);
		if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"53".equals(cTrademark.getAppliCity().trim())) {
			cTrademark.setCityCode(cTrademark.getAppliCity().trim());
		}

		TrademarkDTO dto = cTrademarkService.listReportApply(cTrademark);
//		return AjaxResult.success(dto);

		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("utf-8");

		try {

			cTrademarkExcel.exportReportApply(response.getOutputStream(), cTrademark, dto);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("导出失败," + e.getMessage(), e);
			throw new RuntimeException("导出失败，请联系管理员", e);
		}

	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@GetMapping("/listReportReg")
	public AjaxResult listReportReg(CTrademark cTrademark) {

		String countTimeStr = "统计时期";
		if (StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeBegin())
				&& StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeEnd())) {
			countTimeStr = countTimeStr + "" + cTrademark.getApplyAdjustmentTimeBegin() + "至"
					+ cTrademark.getApplyAdjustmentTimeEnd();
			cTrademark.setBeginDate(cTrademark.getApplyAdjustmentTimeBegin().replace("-", "") + "16");
			cTrademark.setEndDate(cTrademark.getApplyAdjustmentTimeEnd().replace("-", "") + "15");
		}
		countTimeStr = countTimeStr + "(每年范围为上年12月16日到本年12月15日)";
		cTrademark.setCountTimeStr(countTimeStr);

		if (StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeBegin())
				&& StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeEnd())) {
			cTrademark.setBeginDate(cTrademark.getApplyAdjustmentTimeBegin().replace("-", "") + "16");
			cTrademark.setEndDate(cTrademark.getApplyAdjustmentTimeEnd().replace("-", "") + "15");
		}
		if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"53".equals(cTrademark.getAppliCity().trim())) {
			cTrademark.setCityCode(cTrademark.getAppliCity().trim());
		}

		TrademarkDTO dto = cTrademarkService.listReportReg(cTrademark);

//		OutputStream out = null;
//		try {
//			out = new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//			cTrademarkExcel.exportReportReg(out, cTrademark, dto);
//
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally {
//			try {
//				out.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}

		return AjaxResult.success(dto);
	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@PostMapping("/exportReportReg")
	public void exportReportReg(HttpServletResponse response, CTrademark cTrademark) {

		String countTimeStr = "统计时期";
		if (StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeBegin())
				&& StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeEnd())) {
			countTimeStr = countTimeStr + "" + cTrademark.getApplyAdjustmentTimeBegin() + "至"
					+ cTrademark.getApplyAdjustmentTimeEnd();
			cTrademark.setBeginDate(cTrademark.getApplyAdjustmentTimeBegin().replace("-", "") + "16");
			cTrademark.setEndDate(cTrademark.getApplyAdjustmentTimeEnd().replace("-", "") + "15");
		}
		countTimeStr = countTimeStr + "(每年范围为上年12月16日到本年12月15日)";
		cTrademark.setCountTimeStr(countTimeStr);

		if (StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeBegin())
				&& StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeEnd())) {
			cTrademark.setBeginDate(cTrademark.getApplyAdjustmentTimeBegin().replace("-", "") + "16");
			cTrademark.setEndDate(cTrademark.getApplyAdjustmentTimeEnd().replace("-", "") + "15");
		}
		if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"53".equals(cTrademark.getAppliCity().trim())) {
			cTrademark.setCityCode(cTrademark.getAppliCity().trim());
		}

		TrademarkDTO dto = cTrademarkService.listReportReg(cTrademark);
//		return AjaxResult.success(dto);

		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("utf-8");

		try {

			cTrademarkExcel.exportReportReg(response.getOutputStream(), cTrademark, dto);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("导出失败," + e.getMessage(), e);
			throw new RuntimeException("导出失败，请联系管理员", e);
		}

	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@GetMapping("/listReportAct2")
	public AjaxResult listReportAct2(CTrademark cTrademark) {

		cTrademark.setCurrDate(new SimpleDateFormat("yyyyMMdd").format(new Date()));

		if (StringUtils.isBlank(cTrademark.getEndDate()) && StringUtils.isNotBlank(cTrademark.getYear())
				&& StringUtils.isNotBlank(cTrademark.getMonth())) {
			cTrademark.setEndDate(cTrademark.getYear() + cTrademark.getMonth());
		}
		if (StringUtils.isBlank(cTrademark.getEndDate())) {
			cTrademark.setEndDate(new SimpleDateFormat("yyyy").format(new Date()) + "12");
		}

		String endDate = cTrademark.getEndDate().replace("-", "");
		String month = endDate.substring(4, 6);
		int endYear = Integer.parseInt(cTrademark.getEndDate().substring(0, 4));
		int beginYear = endYear - 6;
		String beginDate = beginYear + month + "16";
		endDate = endYear + month + "15";

		cTrademark.setBeginDate(beginDate);
		cTrademark.setEndDate(endDate);

		if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"53".equals(cTrademark.getAppliCity().trim())) {
			cTrademark.setCityCode(cTrademark.getAppliCity().trim());
		}

		TrademarkDTO dto = cTrademarkService.listReportAct2(cTrademark);
		return AjaxResult.success(dto);

	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@GetMapping("/listReportAct")
	public AjaxResult listReportAct(CTrademark cTrademark) {

		cTrademark.setCurrDate(new SimpleDateFormat("yyyyMMdd").format(new Date()));
		
		String countTimeStr = "统计时期";
		if (StringUtils.isNotBlank(cTrademark.getYear()) && StringUtils.isNotBlank(cTrademark.getMonth())) {
			countTimeStr = countTimeStr + "" + cTrademark.getYear() + "-" + cTrademark.getMonth();
		}

		countTimeStr = countTimeStr + "(范围近6年，含本年，每年范围为12月16日到本月15日 )";
		cTrademark.setCountTimeStr(countTimeStr);

		if (StringUtils.isBlank(cTrademark.getEndDate()) && StringUtils.isNotBlank(cTrademark.getYear())
				&& StringUtils.isNotBlank(cTrademark.getMonth())) {
			cTrademark.setEndDate(cTrademark.getYear() + cTrademark.getMonth());
		}
		if (StringUtils.isBlank(cTrademark.getEndDate())) {
			cTrademark.setEndDate(new SimpleDateFormat("yyyy").format(new Date()) + "12");
		}
		if (StringUtils.isNotBlank(cTrademark.getYear())) {
			String year = cTrademark.getYear();
			String month = "12";
			if (StringUtils.isNotBlank(cTrademark.getMonth())) {
				month = cTrademark.getMonth();
			}
			cTrademark.setEndDate(year + month + "15");
		}
		String endDate = cTrademark.getEndDate().replace("-", "");
		String month = endDate.substring(4, 6);
		int endYear = Integer.parseInt(cTrademark.getEndDate().substring(0, 4));
		int beginYear = endYear - 6;
		String beginDate = beginYear + "1216";
		endDate = endYear + month + "15";

		cTrademark.setBeginDate(beginDate);
		cTrademark.setEndDate(endDate);

		if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"53".equals(cTrademark.getAppliCity().trim())) {
			cTrademark.setCityCode(cTrademark.getAppliCity().trim());
		}

		TrademarkDTO dto = cTrademarkService.listReportAct(cTrademark);

//		OutputStream out = null;
//		try {
//			out = new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//			cTrademarkExcel.exportReportAct(out, cTrademark, dto);
//
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally {
//			try {
//				out.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}

		return AjaxResult.success(dto);
	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@PostMapping("/exportReportAct")
	public void exportReportAct(HttpServletResponse response, CTrademark cTrademark) {

		cTrademark.setCurrDate(new SimpleDateFormat("yyyyMMdd").format(new Date()));

		String countTimeStr = "统计时期";
		if (StringUtils.isNotBlank(cTrademark.getYear()) && StringUtils.isNotBlank(cTrademark.getMonth())) {
			countTimeStr = countTimeStr + "" + cTrademark.getYear() + "-" + cTrademark.getMonth();
		}

		countTimeStr = countTimeStr + "(范围近6年，含本年，每年范围为12月16日到本月15日 )";
		cTrademark.setCountTimeStr(countTimeStr);

		if (StringUtils.isBlank(cTrademark.getEndDate()) && StringUtils.isNotBlank(cTrademark.getYear())
				&& StringUtils.isNotBlank(cTrademark.getMonth())) {
			cTrademark.setEndDate(cTrademark.getYear() + cTrademark.getMonth());
		}
		if (StringUtils.isBlank(cTrademark.getEndDate())) {
			cTrademark.setEndDate(new SimpleDateFormat("yyyy").format(new Date()) + "12");
		}
		if (StringUtils.isNotBlank(cTrademark.getYear())) {
			String year = cTrademark.getYear();
			String month = "12";
			if (StringUtils.isNotBlank(cTrademark.getMonth())) {
				month = cTrademark.getMonth();
			}
			cTrademark.setEndDate(year + month + "15");
		}
		String endDate = cTrademark.getEndDate().replace("-", "");
		String month = endDate.substring(4, 6);
		int endYear = Integer.parseInt(cTrademark.getEndDate().substring(0, 4));
		int beginYear = endYear - 6;
		String beginDate = beginYear + "1216";
		endDate = endYear + month + "15";

		cTrademark.setBeginDate(beginDate);
		cTrademark.setEndDate(endDate);

		if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"53".equals(cTrademark.getAppliCity().trim())) {
			cTrademark.setCityCode(cTrademark.getAppliCity().trim());
		}

		TrademarkDTO dto = cTrademarkService.listReportAct(cTrademark);
//		return AjaxResult.success(dto);

		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("utf-8");

		try {

			cTrademarkExcel.exportReportAct(response.getOutputStream(), cTrademark, dto);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("导出失败," + e.getMessage(), e);
			throw new RuntimeException("导出失败，请联系管理员", e);
		}

	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@GetMapping("/listReportNc")
	public AjaxResult listReportNc(CTrademark cTrademark) {

		if (StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeBegin())
				&& StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeEnd())) {
			cTrademark.setCountTimeStr(
					"统计时期" + cTrademark.getApplyAdjustmentTimeBegin() + "至" + cTrademark.getApplyAdjustmentTimeEnd());
		}
		if (StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeBegin())
				&& StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeEnd())) {
			cTrademark.setBeginDate(cTrademark.getApplyAdjustmentTimeBegin().replace("-", "") + "16");
			cTrademark.setEndDate(cTrademark.getApplyAdjustmentTimeEnd().replace("-", "") + "15");
		}
		if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"53".equals(cTrademark.getAppliCity().trim())) {
			cTrademark.setCityCode(cTrademark.getAppliCity().trim());
		}

		List<TrademarkVO> list = cTrademarkService.listReportNc(cTrademark);

//		OutputStream out = null;
//		try {
//			out = new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//			cTrademarkExcel.exportReportNc(out, cTrademark, list);
//
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally {
//			try {
//				out.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}

		return AjaxResult.success(list);
	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@PostMapping("/exportReportNc")
	public void exportReportNc(HttpServletResponse response, CTrademark cTrademark) {
		if (StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeBegin())
				&& StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeEnd())) {
			cTrademark.setCountTimeStr(
					"统计时期" + cTrademark.getApplyAdjustmentTimeBegin() + "至" + cTrademark.getApplyAdjustmentTimeEnd());
		}
		if (StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeBegin())
				&& StringUtils.isNotBlank(cTrademark.getApplyAdjustmentTimeEnd())) {
			cTrademark.setBeginDate(cTrademark.getApplyAdjustmentTimeBegin().replace("-", "") + "16");
			cTrademark.setEndDate(cTrademark.getApplyAdjustmentTimeEnd().replace("-", "") + "15");
		}
		if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"53".equals(cTrademark.getAppliCity().trim())) {
			cTrademark.setCityCode(cTrademark.getAppliCity().trim());
		}

		List<TrademarkVO> list = cTrademarkService.listReportNc(cTrademark);
//		return AjaxResult.success(list);

		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("utf-8");

		try {

			cTrademarkExcel.exportReportNc(response.getOutputStream(), cTrademark, list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("导出失败," + e.getMessage(), e);
			throw new RuntimeException("导出失败，请联系管理员", e);
		}

	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@GetMapping("/listReportRank")
	public TableDataInfo listReportRank(CTrademark cTrademark) {
		if (StringUtils.isNotBlank(cTrademark.getYear())) {
			if(StringUtils.isNotBlank(cTrademark.getMonth())) {
				String beginDate = (Integer.parseInt(cTrademark.getYear()) - 1) + cTrademark.getMonth()+"16";
				String endDate = cTrademark.getYear() + cTrademark.getMonth()+"15";
				cTrademark.setBeginDate(beginDate);
				cTrademark.setEndDate(endDate);
			}else {
				String beginDate = (Integer.parseInt(cTrademark.getYear()) - 1) + "1216";
				String endDate = cTrademark.getYear() + "1215";
				cTrademark.setBeginDate(beginDate);
				cTrademark.setEndDate(endDate);
			}
			
		}
		List<TrademarkVO> list = cTrademarkService.listReportRank(cTrademark);

//		OutputStream out = null;
//		try {
//			out = new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//			cTrademarkExcel.exportReportRank(out, cTrademark, list);
//
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally {
//			try {
//				out.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}

		return getDataTable(list);

	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@PostMapping("/exportReportRank")
	public void exportReportRank(HttpServletResponse response, CTrademark cTrademark) {
		if (StringUtils.isNotBlank(cTrademark.getYear())) {
			String beginDate = (Integer.parseInt(cTrademark.getYear()) - 1) + "1216";
			String endDate = cTrademark.getYear() + "1215";
			cTrademark.setBeginDate(beginDate);
			cTrademark.setEndDate(endDate);
		}
		List<TrademarkVO> list = cTrademarkService.listReportRank(cTrademark);
//		return getDataTable(list);

		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("utf-8");

		try {

			cTrademarkExcel.exportReportRank(response.getOutputStream(), cTrademark, list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("导出失败," + e.getMessage(), e);
			throw new RuntimeException("导出失败，请联系管理员", e);
		}

	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@GetMapping("/listReportHouse")
	public TableDataInfo listReportHouse(CTrademark cTrademark) {
		if (StringUtils.isNotBlank(cTrademark.getYear())) {
			String beginDate = (Integer.parseInt(cTrademark.getYear()) - 1) + "1216";
			String endDate = cTrademark.getYear() + "1215";
			cTrademark.setBeginDate(beginDate);
			cTrademark.setEndDate(endDate);
		}
		if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"53".equals(cTrademark.getAppliCity().trim())) {
			cTrademark.setCityCode(cTrademark.getAppliCity().trim());
		}
		List<TrademarkVO> list = cTrademarkService.listReportHouse(cTrademark);

//		OutputStream out = null;
//		try {
//			out = new FileOutputStream(new File("D:\\temp\\exp.xlsx"));
//			cTrademarkExcel.exportReportHouse(out, cTrademark, list);
//
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} finally {
//			try {
//				out.close();
//			} catch (IOException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}

		return getDataTable(list);

	}

	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@PostMapping("/exportReportHouse")
	public void exportReportHouse(HttpServletResponse response, CTrademark cTrademark) {
		if (StringUtils.isNotBlank(cTrademark.getYear())) {
			String beginDate = (Integer.parseInt(cTrademark.getYear()) - 1) + "1216";
			String endDate = cTrademark.getYear() + "1215";
			cTrademark.setBeginDate(beginDate);
			cTrademark.setEndDate(endDate);
		}
		if (StringUtils.isNotBlank(cTrademark.getAppliCity()) && !"53".equals(cTrademark.getAppliCity().trim())) {
			cTrademark.setCityCode(cTrademark.getAppliCity().trim());
		}
		List<TrademarkVO> list = cTrademarkService.listReportHouse(cTrademark);
//		return getDataTable(list);

		response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
		response.setCharacterEncoding("utf-8");

		try {

			cTrademarkExcel.exportReportHouse(response.getOutputStream(), cTrademark, list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("导出失败," + e.getMessage(), e);
			throw new RuntimeException("导出失败，请联系管理员", e);
		}

	}

	/**
	 * 查询中国商标列表
	 *
	 * @param cTrademark 中国商标
	 * @return 中国商标集合
	 * @date 2023-04-28
	 */
	@PreAuthorize("@ss.hasPermi('trade:cTrademark:list')")
	@GetMapping("/list")
	public TableDataInfo list(CTrademark cTrademark) {
		startPage();
		List<CTrademark> list = cTrademarkService.queryList(cTrademark);
		return getDataTable(list);
	}

	/**
	 * 导出中国商标列表
	 *
	 * @param cTrademark 中国商标
	 * @date 2023-04-28
	 */
	@PreAuthorize("@ss.hasPermi('trade:cTrademark:export')")
	@Log(title = "中国商标", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, CTrademark cTrademark) {
		List<CTrademark> list = cTrademarkService.list(new QueryWrapper<>());
		ExcelUtil<CTrademark> util = new ExcelUtil<>(CTrademark.class);
		util.exportExcel(response, list, "中国商标数据");
	}

	/**
	 * 获取中国商标详细信息
	 *
	 * @param id 主键id
	 * @return 结果
	 * @date 2023-04-28
	 */
	@PreAuthorize("@ss.hasPermi('trade:cTrademark:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(cTrademarkService.getById(id));
	}

	/**
	 * 新增中国商标信息
	 *
	 * @param cTrademark 中国商标
	 * @return 结果
	 * @date 2023-04-28
	 */
	@PreAuthorize("@ss.hasPermi('trade:cTrademark:add')")
	@Log(title = "中国商标", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody CTrademark cTrademark) {
		return toAjax(cTrademarkService.save(cTrademark));
	}

	/**
	 * 修改中国商标信息
	 *
	 * @param cTrademark 中国商标
	 * @return 结果
	 * @date 2023-04-28
	 */
	@PreAuthorize("@ss.hasPermi('trade:cTrademark:edit')")
	@Log(title = "中国商标", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody CTrademark cTrademark) {
		return toAjax(cTrademarkService.updateById(cTrademark));
	}

	/**
	 * 删除中国商标信息
	 *
	 * @param ids 主键ids
	 * @return 结果
	 * @date 2023-04-28
	 */
	@PreAuthorize("@ss.hasPermi('trade:cTrademark:remove')")
	@Log(title = "中国商标", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(cTrademarkService.removeByIds(Arrays.asList(ids)));
	}

}
