package com.ruoyi.trade.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.support.json.JSONUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.ruoyi.adjust.mapper.DPatentDataCollationInfoMapper;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.dimp.domain.DMarketsubDataInfo;
import com.ruoyi.dimp.service.IDMarketsubDataInfoService;
import com.ruoyi.report.vo.TrademarkLastSumDTO;
import com.ruoyi.report.vo.TrademarkSumItem;
import com.ruoyi.system.domain.BladeRegion;
import com.ruoyi.system.service.IBladeRegionService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.trade.domain.CTrademark;
import com.ruoyi.trade.mapper.CTrademarkMapper;
import com.ruoyi.trade.service.ICTrademarkService;
import com.ruoyi.trade.vo.TrademarkDTO;
import com.ruoyi.trade.vo.TrademarkVO;
import com.ruoyi.utils.BladeRegionUtils;

/**
 * 中国商标Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-28
 */
@Service
public class CTrademarkServiceImpl extends ServiceImpl<CTrademarkMapper, CTrademark> implements ICTrademarkService {

	private static final Logger logger = LoggerFactory.getLogger(CTrademarkServiceImpl.class);

	@Autowired
	private ICTrademarkService cTrademarkService;

	@Autowired
	private IBladeRegionService bladeRegionService;

	@Autowired
	private CTrademarkMapper cTrademarkMapper;

	@Autowired
	private ISysConfigService sysConfigService;

	@Autowired
	private RedisCache redisCache;

	@Autowired
	private RuoYiConfig ruoYiConfig;

	@Autowired
	private IDMarketsubDataInfoService dDMarketsubDataInfoService;

	@Autowired
	private DPatentDataCollationInfoMapper dPatentDataCollationInfoMapper;

	public static Map<String, CTrademark> CTrademark_MAP = new HashMap<String, CTrademark>();

//	private TrademarkSaveQueue trademarkSaveQueue;

//	private static Map<String, String> SUBJECT_MOCK_MAP = new LinkedHashMap<String, String>();
//	static {
//		SUBJECT_MOCK_MAP.put("昆明市", "96.8636");
//		SUBJECT_MOCK_MAP.put("西双版纳州", "14.8309");
//		SUBJECT_MOCK_MAP.put("迪庆州", "5.0961");
//		SUBJECT_MOCK_MAP.put("怒江州", "3.5792");
//		SUBJECT_MOCK_MAP.put("玉溪市", "24.6105");
//		SUBJECT_MOCK_MAP.put("临沧市", "17.156");
//		SUBJECT_MOCK_MAP.put("普洱市", "21.9793");
//		SUBJECT_MOCK_MAP.put("德宏州", "12.9678");
//		SUBJECT_MOCK_MAP.put("丽江市", "12.925");
//		SUBJECT_MOCK_MAP.put("大理州", "26.8798");
//		SUBJECT_MOCK_MAP.put("保山市", "17.8749");
//		SUBJECT_MOCK_MAP.put("文山州", "20.7852");
//		SUBJECT_MOCK_MAP.put("红河州", "31.0437");
//		SUBJECT_MOCK_MAP.put("昭通市", "26.9124");
//		SUBJECT_MOCK_MAP.put("楚雄州", "21.3511");
//		SUBJECT_MOCK_MAP.put("曲靖市", "43.7129");
//	}

	public static final List<String> ALL_PROVINCE_LIST = new ArrayList<String>();
	static {
		ALL_PROVINCE_LIST.add("云南省");
	}

	public void doSyncMark() {
		logger.debug("同步到应用系统-同步商标数据1");
		dPatentDataCollationInfoMapper.deleteForSyncMarkReg();

		logger.debug("同步到应用系统-同步商标数据2");
		dPatentDataCollationInfoMapper.insertForSyncMarkReg();

		logger.debug("同步到应用系统-同步商标数据3");
		dPatentDataCollationInfoMapper.deleteForSyncMarkApl();

		logger.debug("同步到应用系统-同步商标数据4");
		dPatentDataCollationInfoMapper.insertForSyncMarkApl();

		logger.debug("同步到应用系统-同步商标数据5");
		dPatentDataCollationInfoMapper.deleteForSyncMarkAct();

		logger.debug("同步到应用系统-同步商标数据6");
		dPatentDataCollationInfoMapper.insertForSyncMarkAct();

		logger.debug("同步到应用系统-同步商标数据7");
		dPatentDataCollationInfoMapper.deleteForSyncMarkRegNc();

		logger.debug("同步到应用系统-同步商标数据8");
		dPatentDataCollationInfoMapper.insertForSyncMarkRegNc();

		logger.debug("同步到应用系统-同步商标数据9");
		dPatentDataCollationInfoMapper.deleteForSyncMarkAplNc();

		logger.debug("同步到应用系统-同步商标数据10");
		dPatentDataCollationInfoMapper.insertForSyncMarkAplNc();

		logger.debug("同步到应用系统-同步商标数据11");
		dPatentDataCollationInfoMapper.deleteForSyncMarkActNc();

		logger.debug("同步到应用系统-同步商标数据12");
		dPatentDataCollationInfoMapper.insertForSyncMarkActNc();

		logger.debug("同步到应用系统-同步商标数据13");
	}

//	public static final List<String> ALL_AREA_LIST = new ArrayList<String>();
//	static {
//
//		ALL_AREA_LIST.add("昆明市");
//		ALL_AREA_LIST.add("西双版纳州");
//		ALL_AREA_LIST.add("迪庆州");
//		ALL_AREA_LIST.add("怒江州");
//		ALL_AREA_LIST.add("玉溪市");
//		ALL_AREA_LIST.add("临沧市");
//		ALL_AREA_LIST.add("普洱市");
//		ALL_AREA_LIST.add("德宏州");
//		ALL_AREA_LIST.add("丽江市");
//		ALL_AREA_LIST.add("大理州");
//		ALL_AREA_LIST.add("保山市");
//		ALL_AREA_LIST.add("文山州");
//		ALL_AREA_LIST.add("红河州");
//		ALL_AREA_LIST.add("昭通市");
//		ALL_AREA_LIST.add("楚雄州");
//		ALL_AREA_LIST.add("曲靖市");
//
//	}

	private static Map<String, String> NC_MAP = new LinkedHashMap<String, String>();
	static {
		NC_MAP.put("1", "用于工业、科学、摄影、农业、园艺和林业的化学品；未加工人造合成树脂，未加工塑料物质；灭火和防火用合成物；淬火和焊接用制剂；鞣制动物皮毛用物质；工业用粘合剂；油灰及其他膏状填料；堆肥，肥料，化肥；工业和科学用生物制剂。");
		NC_MAP.put("2", "颜料，清漆，漆；防锈剂和木材防腐剂；着色剂，染料；印刷、标记和雕刻用油墨；未加工的天然树脂；绘画、装饰、印刷和艺术用金属箔及金属粉。");
		NC_MAP.put("3", "不含药物的化妆品和梳洗用制剂；不含药物的牙膏：香料，香精油；洗衣用漂白剂及其他物料；清洁、擦亮、去渍及研磨用制剂。");
		NC_MAP.put("4", "工业用油和油脂，蜡；润滑剂；吸收、润湿和粘结灰尘用合成物；燃料和照明材料；照明用蜡烛和灯芯。");
		NC_MAP.put("5", "药品，医用和兽医用制剂；医用卫生制剂；医用或兽医用营养食物和物质，婴儿食品；人用和动物用膳食补充剂；膏药，绷敷材料；填塞牙孔用料，牙科用蜡；消毒剂；消灭有害动物制剂；杀真菌剂，除莠剂。");
		NC_MAP.put("6", "普通金属及其合金，金属矿石；金属建筑材料；可移动金属建筑物；普通金属制非电气用缆线；金属小五金具；存储和运输用金属容器；保险箱。");
		NC_MAP.put("7", "机器，机床，电动工具；马达和引擎（陆地车辆用的除外）；机器联结器和传动机件（陆地车辆用的除外）；除手动手工具以外的农业器具；孵化器：自动售货机。");
		NC_MAP.put("8", "手工具和器具（手动的）；刀、叉和勺餐具；除火器外的随身武器；剃刀。");
		NC_MAP.put("9",
				"科学、航海、测量、摄影、电影、光学、衡具、量具、信号、检验（监督）、救护（营救）和教学用装置及仪器；处理、开关、转换、积累、调节或控制电的装置和仪器；录制、传送、重放声音或影像的装置；磁性数据载体，录音盘；光盘，DVD盘和其他数字存储媒介；投币启动装置的机械结构；收银机，计算机器，数据处理装置，计算机；计算机软件；灭火设备。");
		NC_MAP.put("10", "外科、医疗、牙科和兽医用仪器及器械；假肢，假眼和假牙；矫形用物品；缝合用材料；残疾人专用治疗装置；按摩器械；婴儿护理用器械、器具及用品；性生活用器械、器具及用品。");
		NC_MAP.put("11", "照明、加热、蒸汽发生、烹饪、冷藏、干燥、通风、供水以及卫生用装置。");
		NC_MAP.put("12", "运载工具；陆、空、海用客运或货运装置。");
		NC_MAP.put("13", "火器；军火及弹药；爆炸物；烟火。");
		NC_MAP.put("14", "贵重金属及其合金；首饰，宝石和半宝石；钟表和计时仪器。");
		NC_MAP.put("15", "乐器。");
		NC_MAP.put("16", "纸和纸板；印刷品；书籍装订材料；照片；文具和办公用品（家具除外）；文具用或家庭用粘合剂；绘画材料和艺术家用材料；画笔；教育或教学用品；包装和打包用塑料纸、塑料膜和塑料袋；印刷铅字，印版。");
		NC_MAP.put("17", "未加工和半加工的橡胶、古塔胶、树胶、石棉、云母及这些材料的代用品；生产用成型塑料和树脂制品；包装、填充和绝缘用材料；非金属软管和非金属柔性管。");
		NC_MAP.put("18", "皮革和人造皮革；毛皮；行李箱和背包；雨伞和阳伞；手杖；鞭，马具和鞍具；动物用项圈、皮带和衣服。");
		NC_MAP.put("19", "非金属的建筑材料；建筑用非金属刚性管；柏油，沥青；可移动非金属建筑物；非金属碑。");
		NC_MAP.put("20", "家具，镜子，相框；存储或运输用非金属容器；未加工或半加工的骨、角、鲸骨或珍珠母；贝壳；海泡石；黄琥珀。");
		NC_MAP.put("21", "家用或厨房用器具和容器；烹饪用具和餐具（刀、叉、匙除外）；梳子和海绵；刷子（画笔除外）；制刷材料；清洁用具；未加工或半加工玻璃（建筑用玻璃除外）；玻璃器皿、瓷器和陶器。");
		NC_MAP.put("22", "绳索和细绳；网；帐篷和防水遮布；纺织品或合成纤维材料制遮篷；帆；运输和贮存散装物用麻袋；衬垫和填充材料（纸或纸板、橡胶、塑料制除外）；纺织用纤维原料及其替代品。");
		NC_MAP.put("23", "纺织用纱和线。");
		NC_MAP.put("24", "织物及其替代品；家庭日用纺织品；纺织品制或塑料制帘。");
		NC_MAP.put("25", "服装，鞋，帽。");
		NC_MAP.put("26", "花边和刺绣品，饰带和编带；纽扣，领钩扣，饰针和缝针；假花；发饰；假发。");
		NC_MAP.put("27", "地毯，地席，席类，油毡及其他铺地板材料；非纺织品制墙帷。");
		NC_MAP.put("28", "游戏器具和玩具；视频游戏装置；体育和运动用品；圣诞树用装饰品。");
		NC_MAP.put("29", "肉，鱼，家禽和野味；肉汁；腌渍、冷冻、干制及煮熟的水果和蔬菜；果冻，果酱，蜜饯；蛋；奶和奶制品；食用油和油脂。");
		NC_MAP.put("30", "咖啡，茶，可可和咖啡代用品；米；食用淀粉和西米；面粉和谷类制品；面包、糕点和甜食；食用冰；糖，蜂蜜，糖浆；鲜酵母，发酵粉；食盐；芥末；醋，沙司（调味品）；辛香料；冰（冻结的水）。");
		NC_MAP.put("31", "未加工的农业、水产养殖业、园艺、林业产品；未加工的谷物和种子；新鲜水果和蔬菜，新鲜芳香草本植物；草木和花卉；种植用球茎、幼苗和种子；活动物；动物的饮食；麦芽。");
		NC_MAP.put("32", "啤酒；矿泉水和汽水以及其他不含酒精的饮料；水果饮料及果汁；糖浆及其他制饮料用的制剂。");
		NC_MAP.put("33", "含酒精的饮料（啤酒除外）。");
		NC_MAP.put("34", "烟草；烟具；火柴。");
		NC_MAP.put("35", "广告；商业经营；商业管理；办公事务。");
		NC_MAP.put("36", "保险；金融事务；货币事务；不动产事务。");
		NC_MAP.put("37", "房屋建筑；修理；安装服务。");
		NC_MAP.put("38", "电信。");
		NC_MAP.put("39", "运输；商品包装和贮藏；旅行安排。");
		NC_MAP.put("40", "材料处理。");
		NC_MAP.put("41", "教育；提供培训；娱乐；文体活动。");
		NC_MAP.put("42", "科学技术服务和与之相关的研究与设计服务；工业分析与研究；计算机硬件与软件的设计与开发。");
		NC_MAP.put("43", "提供食物和饮料服务；临时住宿。");
		NC_MAP.put("44", "医疗服务；兽医服务；人或动物的卫生和美容服务；农业、园艺和林业服务。");
		NC_MAP.put("45", "法律服务；为有形财产和个人提供实体保护的安全服务；由他人提供的为满足个人需要的私人和社会服务。");
	}

	public TrademarkLastSumDTO listForLast() {
		TrademarkLastSumDTO dto = new TrademarkLastSumDTO();
		String lastTimeKey = "trademark_last_time";
		String lastTime = sysConfigService.selectConfigByKey(lastTimeKey);
		if (StringUtils.isNotBlank(lastTime)) {
			dto.setLastTime(lastTime);
		}

		TrademarkSumItem totalItem = new TrademarkSumItem();
		totalItem.setAreaCode("53");
		totalItem.setAreaName("云南省");
		totalItem.setAreaShortName("云南省");
		totalItem.setTotalCnt(0L);

		Set<String> citySet = new HashSet<String>();

		List<TrademarkSumItem> itemList = new ArrayList<TrademarkSumItem>();

		CTrademark cTrademark = new CTrademark();
		cTrademark.setEndDate(new SimpleDateFormat("yyyyMM").format(new Date()) + "15");
		List<Map> dataList = cTrademarkMapper.listForLast(cTrademark);

		for (Map map : dataList) {
			TrademarkSumItem item = new TrademarkSumItem();
			itemList.add(item);
			item.setTotalCnt(getLong(map, "total_cnt"));
			item.setAreaName(getStr(map, "modify_city"));
			item.setAreaCode(getStr(map, "modify_city_code"));
			totalItem.setTotalCnt(totalItem.getTotalCnt() + item.getTotalCnt());

			citySet.add(item.getAreaCode());

			BladeRegion bladeRegion = BladeRegionUtils.selectByCode(item.getAreaCode());
			if (bladeRegion != null) {
				if (bladeRegion.getCityName().equals(item.getAreaName())) {
					if (StringUtils.isNotBlank(bladeRegion.getCityShortName())) {
						item.setAreaShortName(bladeRegion.getCityShortName());
					}

				} else {
					logger.warn("数据错误，城市名称与城市代码不匹配,city:" + item.getAreaName() + ",code:" + item.getAreaCode());
				}
			}
		}

		List<BladeRegion> cityList = bladeRegionService.selectAllCityList();
		for (BladeRegion br : cityList) {
			if (!citySet.contains(br.getCityCode())) {
				TrademarkSumItem item = new TrademarkSumItem();
				item.setTotalCnt(0L);
				item.setAreaName(br.getCityName());
				item.setAreaCode(br.getCityCode());
				item.setAreaShortName(br.getCityShortName());
				itemList.add(item);
			}
		}

		Collections.sort(itemList, new Comparator<TrademarkSumItem>() {
			@Override
			public int compare(TrademarkSumItem o1, TrademarkSumItem o2) {
				return (int) (o2.getTotalCnt() - o1.getTotalCnt());
			}
		});

		itemList.add(0, totalItem);

		dto.setList(itemList);
		return dto;
	}

	private Map<String, String> getMarketsub(String year) {
		DMarketsubDataInfo dMarketsubDataInfo = new DMarketsubDataInfo();
		dMarketsubDataInfo.setYear(year);
		List<DMarketsubDataInfo> list = dDMarketsubDataInfoService.queryList(dMarketsubDataInfo);
		Map<String, String> map = new HashMap<String, String>();
		for (DMarketsubDataInfo m : list) {

			String areaName = BladeRegionUtils.getDistrictShortName(m.getCityCode());
			if (StringUtils.isNotBlank(areaName)) {
				m.setCityName(areaName);
			}

			map.put(m.getCityName(), m.getMarketsubTotal() + "");
		}
		return map;
	}

	@Override
	public Map<String, Object> listReportNcGrowth(CTrademark cTrademark) {

		cTrademark.setBeginDate(cTrademark.getBeginDate1());
		cTrademark.setEndDate(cTrademark.getEndDate1());
//		key:nc,areaName
		Map<String, Map<String, String>> map1 = listReportNcGrowthOne(cTrademark);

		cTrademark.setBeginDate(cTrademark.getBeginDate2());
		cTrademark.setEndDate(cTrademark.getEndDate2());
		Map<String, Map<String, String>> map2 = listReportNcGrowthOne(cTrademark);

		String compareRate1 = cTrademark.getBeginDate1() + " 至  " + cTrademark.getEndDate1();
		String compareRate2 = cTrademark.getBeginDate2() + " 至  " + cTrademark.getEndDate2();

		Set<String> ncSet = new HashSet<String>();
		for (Map.Entry<String, Map<String, String>> ent : map2.entrySet()) {
			String key = ent.getKey();
			ncSet.add(key);
			if (!map1.containsKey(key)) {
				Map<String, String> map = new HashMap<String, String>();
				map1.put(key, map);
			}
		}

		for (Map.Entry<String, Map<String, String>> ent : map1.entrySet()) {
			String key = ent.getKey();
			ncSet.add(key);
			if (!map2.containsKey(key)) {
				Map<String, String> map = new HashMap<String, String>();
				map2.put(key, map);
			}
		}
		List<String> areaList = new ArrayList<String>();

		if ("province".equals(cTrademark.getSumType())) {
			areaList = ALL_PROVINCE_LIST;
		} else if ("city".equals(cTrademark.getSumType()) && "all".equals(cTrademark.getAppliCity())) {
//			areaList = ALL_AREA_LIST;
		}

		if (areaList.isEmpty()) {
			Set<String> areaSet = new HashSet<String>();
			for (Map.Entry<String, Map<String, String>> ent : map1.entrySet()) {
				for (String key : ent.getValue().keySet()) {
					areaSet.add(key);
				}
			}
			for (Map.Entry<String, Map<String, String>> ent : map2.entrySet()) {
				for (String key : ent.getValue().keySet()) {
					areaSet.add(key);
				}
			}
			areaList.addAll(areaSet);
		}

		for (Map.Entry<String, Map<String, String>> ent : map1.entrySet()) {
			for (String areaName : areaList) {
				if (!ent.getValue().containsKey(areaName)) {
					ent.getValue().put(areaName, "0");
				}
			}
		}
		for (Map.Entry<String, Map<String, String>> ent : map2.entrySet()) {
			for (String areaName : areaList) {
				if (!ent.getValue().containsKey(areaName)) {
					ent.getValue().put(areaName, "0");
				}
			}
		}

		List<TrademarkDTO> list = new ArrayList<TrademarkDTO>();
		for (String nc : ncSet) {
//			key:areaName
			Map<String, String> ncItem1 = map1.get(nc);
			Map<String, String> ncItem2 = map2.get(nc);

			TrademarkDTO dto = new TrademarkDTO();
			dto.setNc(nc);
			String ncStr = nc;
			if (NC_MAP.containsKey(nc)) {
				ncStr = NC_MAP.get(nc);
			}

			dto.setNcStr(ncStr);
			dto.setNcItem1(ncItem1);
			dto.setNcItem2(ncItem2);

			Map<String, String> ncGrowth = new HashMap<String, String>();

			for (Map.Entry<String, String> ent : ncItem1.entrySet()) {
				String key = ent.getKey();
				String ncGrowthVal = getNcGrowth(Long.parseLong(ncItem1.get(key)), Long.parseLong(ncItem2.get(key)));
				ncGrowth.put(key, ncGrowthVal);
			}

			ncItem1.put("compareRate", compareRate1);
			ncItem2.put("compareRate", compareRate2);
			ncGrowth.put("compareRate", "同比增长");
			dto.setNcGrowth(ncGrowth);

			list.add(dto);
		}

		Collections.sort(list, new Comparator<TrademarkDTO>() {
			@Override
			public int compare(TrademarkDTO o1, TrademarkDTO o2) {
				try {
					return Integer.parseInt(o1.getNc()) - Integer.parseInt(o2.getNc());
				} catch (Exception e) {
					return o1.getNc().compareTo(o2.getNc());
				}

			}
		});

		Map<String, Object> map = new HashMap<String, Object>();

		map.put("areaNames", areaList);
		map.put("dataList", list);
		return map;
	}

	@Override
	public List<TrademarkDTO> listReportGrowth(CTrademark cTrademark) {

		cTrademark.setBeginDate(cTrademark.getBeginDate1());
		cTrademark.setEndDate(cTrademark.getEndDate1());
		Map<String, TrademarkVO> map1 = listReportGrowthOne(cTrademark);

		cTrademark.setBeginDate(cTrademark.getBeginDate2());
		cTrademark.setEndDate(cTrademark.getEndDate2());
		Map<String, TrademarkVO> map2 = listReportGrowthOne(cTrademark);

		String compareRate1 = cTrademark.getBeginDate1() + " 至  " + cTrademark.getEndDate1();
		String compareRate2 = cTrademark.getBeginDate2() + " 至  " + cTrademark.getEndDate2();

		Set<String> areaSet = new HashSet<String>();
		for (Map.Entry<String, TrademarkVO> ent : map2.entrySet()) {
			String key = ent.getKey();
			areaSet.add(key);
			if (!map1.containsKey(key)) {
				TrademarkVO vo = new TrademarkVO();
				vo.setAreaName(key);
				vo.setRegTotalCnt(0L);
				vo.setAplTotalCnt(0L);
				vo.setActTotalCnt(0L);
				map1.put(key, vo);
			}
		}

		for (Map.Entry<String, TrademarkVO> ent : map1.entrySet()) {
			String key = ent.getKey();
			areaSet.add(key);
			if (!map2.containsKey(key)) {
				TrademarkVO vo = new TrademarkVO();
				vo.setAreaName(key);
				vo.setRegTotalCnt(0L);
				vo.setAplTotalCnt(0L);
				vo.setActTotalCnt(0L);
				map2.put(key, vo);
			}
		}

		List<TrademarkDTO> list = new ArrayList<TrademarkDTO>();
		for (String areaName : areaSet) {

			TrademarkVO item1 = map1.get(areaName);
			TrademarkVO item2 = map2.get(areaName);
			item2.setAreaName(null);

			TrademarkDTO dto = new TrademarkDTO();
			dto.setAreaName(areaName);
			dto.setItem1(item1);
			item1.setCompareRate(compareRate1);

			dto.setItem2(item2);
			item2.setCompareRate(compareRate2);

			TrademarkVO growth = new TrademarkVO();
			growth.setCompareRate("同比增长");

			growth.setRegGrowth(getGrowth(item1.getRegTotalCnt(), item2.getRegTotalCnt()));
			growth.setAplGrowth(getGrowth(item1.getAplTotalCnt(), item2.getAplTotalCnt()));
			growth.setActGrowth(getGrowth(item1.getActTotalCnt(), item2.getActTotalCnt()));

			dto.setGrowth(growth);

			list.add(dto);
		}

		return list;
	}

	private String getNcGrowth(Long total1, Long total2) {
		if (total1 == null || total1 == 0L || total2 == null || total2 == 0L) {
			return "--";
		}

		Double growth = (total2 - total1 + 0.0) / total1 * 100;
		String growthStr = String.format("%.2f", growth) + "%";
		return growthStr;
	}

	private String getGrowth(Long total1, Long total2) {
		if (total1 == null || total1 == 0L || total2 == null || total2 == 0L) {
			return "--";
		}

		Double growth = (total2 - total1 + 0.0) / total1 * 100;
		String growthStr = String.format("%.2f", growth) + "%";
		return growthStr;
	}

//	key:nc,areaName
	private Map<String, Map<String, String>> listReportNcGrowthOne(CTrademark cTrademark) {
		boolean retEmpty=false;
		if (StringUtils.isNotBlank(cTrademark.getCityCode()) && !"53".equals(cTrademark.getCityCode())) {
			String cityCode = cTrademark.getCityCode().trim();
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cc=SecurityUtils.needFilterByCityCode();
				if(!cc.equals(cityCode) ) {
					retEmpty=true;
				}
			}
		}else {
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cityCode=SecurityUtils.needFilterByCityCode();
				cTrademark.setCityCode(cityCode);
			}
		}
		
		List<Map> dataList =new ArrayList<Map>();
		if(!retEmpty) {
			dataList = cTrademarkMapper.listReportNcGrowth(cTrademark);
		}

//		nc,modify_city,count(*) total_cnt
//		List<String>areaList=ALL_AREA_LIST;
//		if( "province".equals(cTrademark.getSumType())) {
//			areaList=ALL_PROVINCE_LIST;
//		}
		Map<String, Map<String, String>> ncMap = new HashMap<String, Map<String, String>>();
		for (Map map : dataList) {
			String areaName = getStr(map, "modify_city");

//			if(!areaList.contains(areaName)) {
//				continue;
//			}

			String nc = getStr(map, "nc");
			if (!ncMap.containsKey(nc)) {
				Map<String, String> areaMap = new HashMap<String, String>();
				ncMap.put(nc, areaMap);
			}

			String totalCnt = getStr(map, "total_cnt");
			ncMap.get(nc).put(areaName, totalCnt);

		}

		return ncMap;
	}

	private Map<String, TrademarkVO> listReportGrowthOne(CTrademark cTrademark) {
		
		
		boolean retEmpty=false;
		if (StringUtils.isNotBlank(cTrademark.getCityCode()) && !"53".equals(cTrademark.getCityCode())) {
			String cityCode = cTrademark.getCityCode().trim();
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cc=SecurityUtils.needFilterByCityCode();
				if(!cc.equals(cityCode) ) {
					retEmpty=true;
				}
			}
		}else {
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cityCode=SecurityUtils.needFilterByCityCode();
				cTrademark.setCityCode(cityCode);
			}
		}
		
		
		List<Map> dataList =new ArrayList<Map>();
		if(!retEmpty) {
			dataList = cTrademarkMapper.listReportGrowth(cTrademark);
		}
		
		Map<String, TrademarkVO> voMap = new HashMap<String, TrademarkVO>();
		for (Map map : dataList) {
			TrademarkVO vo = new TrademarkVO();
			String areaName = getStr(map, "modify_city");
			vo.setAreaName(areaName);
			vo.setRegTotalCnt(getLong(map, "reg_total_cnt"));
			vo.setAplTotalCnt(getLong(map, "apl_total_cnt"));
			vo.setActTotalCnt(getLong(map, "act_total_cnt"));
			voMap.put(areaName, vo);

		}
		return voMap;
	}

	@Override
	public TrademarkDTO listReportReg(CTrademark cTrademark) {

		// TODO Auto-generated method stub
//		List<TrademarkVO>list=new ArrayList<TrademarkVO>();

		TrademarkDTO dto = new TrademarkDTO();
		dto.setList(new ArrayList<TrademarkVO>());
		Map<String, TrademarkVO> alreadyMap = new HashMap<String, TrademarkVO>();
		
		
		boolean retEmpty=false;
		if (StringUtils.isNotBlank(cTrademark.getCityCode()) && !"53".equals(cTrademark.getCityCode())) {
			String cityCode = cTrademark.getCityCode().trim();
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cc=SecurityUtils.needFilterByCityCode();
				if(!cc.equals(cityCode) ) {
					retEmpty=true;
				}
			}
		}else {
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cityCode=SecurityUtils.needFilterByCityCode();
				cTrademark.setCityCode(cityCode);
			}
		}
		

//		TODO	汇总	
		TrademarkVO totalVo = new TrademarkVO();
		totalVo.setTotals(new HashMap<String, Long>());
		String cityCode = cTrademark.getCityCode();
		if (StringUtils.isBlank(cityCode)) {
			totalVo.setAreaName("云南省");
		} else {
			String areaName = "---";
			BladeRegion bladeRegion = BladeRegionUtils.selectByCode(cityCode);
//			BladeRegion bladeRegion = bladeRegionService.selectByCityName(cityCode, "53");
			if (bladeRegion != null) {
				String name = bladeRegion.getName();
				if (StringUtils.isNotBlank(bladeRegion.getCityShortName())) {
					name = bladeRegion.getCityShortName().replace("-", "");
				} else if (StringUtils.isNotBlank(bladeRegion.getDistrictShortName())) {
					name = bladeRegion.getDistrictShortName();
				}
				areaName = name;
			}
			totalVo.setAreaName(areaName);
		}
		List<Map> dataList = new ArrayList<Map>();
		if(!retEmpty) {
			dataList = cTrademarkMapper.listReportReg(cTrademark);
		}


		Set<String> yearSet = new HashSet<String>();
		for (Map map : dataList) {
			yearSet.add(getStr(map, "reg_year"));
		}

		List<String> years = new ArrayList<String>();
		years.addAll(yearSet);
		if (years.isEmpty()) {
			if (StringUtils.isNotBlank(cTrademark.getBeginDate()) && StringUtils.isNotBlank(cTrademark.getEndDate())
					&& Integer.parseInt(cTrademark.getBeginDate()) <= Integer.parseInt(cTrademark.getEndDate())) {
				int begin = Integer.parseInt(cTrademark.getBeginDate().substring(0, 4));
				int end = Integer.parseInt(cTrademark.getEndDate().substring(0, 4));
				for (int b = begin; b <= end; b++) {
					years.add(b + "");
				}
			} else {
				int currYear = Integer.parseInt(new SimpleDateFormat("yyyy").format(new Date()));
				for (int i = 0; i < 6; i++) {
					years.add((currYear - 5) + "");
				}
			}
		}

		Collections.sort(years, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				return Integer.parseInt(o1) - Integer.parseInt(o2);
			}

		});
		dto.setYears(years);

		for (String year : years) {
			totalVo.getTotals().put(year, 0L);
		}

		for (Map map : dataList) {

//			String areaName = getStr(map, "modify_city");

			String areaName = getStr(map, "modify_city_code");

			if (!alreadyMap.containsKey(areaName)) {
				TrademarkVO vo = new TrademarkVO();
				vo.setAreaName(areaName);
				alreadyMap.put(areaName, vo);
				vo.setTotals(new HashMap<String, Long>());
				for (String year : yearSet) {
					vo.getTotals().put(year, 0L);
				}
			}

			TrademarkVO vo = alreadyMap.get(areaName);
			String year = getStr(map, "reg_year");
			long oldCnt = 0L;
			if (vo.getTotals().containsKey(year)) {
				oldCnt = vo.getTotals().get(year);
			}
			long newCnt = getLong(map, "total_cnt");
			vo.getTotals().put(year, oldCnt + newCnt);

			if (totalVo.getTotals().containsKey(year)) {
				long oldTotalCnt = totalVo.getTotals().get(year);
				totalVo.getTotals().put(year, oldTotalCnt + newCnt);
			}

		}
		
		if(!alreadyMap.isEmpty()) {
			dto.getList().add(totalVo);
		}
		

		List<String> codes = BladeRegionUtils.sortedKey(alreadyMap.keySet());
		for (String code : codes) {

			TrademarkVO vo = alreadyMap.get(code);

			String areaName = BladeRegionUtils.getDistrictShortName(code);
			if (StringUtils.isNotBlank(areaName)) {
				vo.setAreaName(areaName);
			}

			dto.getList().add(vo);
		}

//		dto.getList().addAll(alreadyMap.values());

		return dto;

	}

	@Override
	public TrademarkDTO listReportAct2(CTrademark cTrademark) {

		// TODO Auto-generated method stub
//		List<TrademarkVO>list=new ArrayList<TrademarkVO>();

		TrademarkDTO dto = new TrademarkDTO();
		dto.setList(new ArrayList<TrademarkVO>());
		Map<String, TrademarkVO> alreadyMap = new HashMap<String, TrademarkVO>();

		List<Map> dataList = cTrademarkMapper.listReportAct2(cTrademark);

		for (Map map : dataList) {

			String areaName = getStr(map, "modify_city");
			if (!alreadyMap.containsKey(areaName)) {
				TrademarkVO vo = new TrademarkVO();
				vo.setAreaName(areaName);
				alreadyMap.put(areaName, vo);
				vo.setTotals(new HashMap<String, Long>());
				vo.setTotalCnt(0L);
			}
			TrademarkVO vo = alreadyMap.get(areaName);
			long newCnt = getLong(map, "total_cnt");
			vo.setTotalCnt(newCnt);
		}

		dto.getList().addAll(alreadyMap.values());

		return dto;

	}

	@Override
	public TrademarkDTO listReportAct(CTrademark cTrademark) {

		// TODO Auto-generated method stub
//		List<TrademarkVO>list=new ArrayList<TrademarkVO>();

		TrademarkDTO dto = new TrademarkDTO();
		dto.setList(new ArrayList<TrademarkVO>());
		Map<String, TrademarkVO> alreadyMap = new HashMap<String, TrademarkVO>();

		List<String> endDateList = new ArrayList<String>();
		cTrademark.setEndDateList(endDateList);

		int maxLen = 6;
		List<String> years = new ArrayList<String>();
		if (StringUtils.isNotBlank(cTrademark.getBeginDate()) && StringUtils.isNotBlank(cTrademark.getEndDate())
				&& Integer.parseInt(cTrademark.getBeginDate()) <= Integer.parseInt(cTrademark.getEndDate())) {
			int begin = Integer.parseInt(cTrademark.getBeginDate().substring(0, 4));
			int end = Integer.parseInt(cTrademark.getEndDate().substring(0, 4));

			String endMonth = cTrademark.getEndDate().substring(4, 6);
			for (int b = begin; b < end; b++) {
				String year = (b + 1) + "";
				years.add(year);
				String m = endMonth + "15";
				if (b + 1 < end) {
					m = "1216";
				}
				String endDay = year + m;
				endDateList.add(endDay);
			}
		} else {
			int currYear = Integer.parseInt(new SimpleDateFormat("yyyy").format(new Date()));
			for (int i = 0; i < maxLen; i++) {
				String year = (currYear - (maxLen + i - 1)) + "";
				years.add(year);
				String m = new SimpleDateFormat("MM").format(new Date());
				if (maxLen + i > 1) {
					m = "1216";
				}
				String endDay = year + m;
				endDateList.add(endDay);

			}
		}
		dto.setYears(years);
		
		
		boolean retEmpty=false;
		if (StringUtils.isNotBlank(cTrademark.getCityCode()) && !"53".equals(cTrademark.getCityCode())) {
			String cityCode = cTrademark.getCityCode().trim();
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cc=SecurityUtils.needFilterByCityCode();
				if(!cc.equals(cityCode) ) {
					retEmpty=true;
				}
			}
		}else {
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cityCode=SecurityUtils.needFilterByCityCode();
				cTrademark.setCityCode(cityCode);
			}
		}
		List<Map> dataList = new ArrayList<Map>();
		if(!retEmpty) {
			dataList = cTrademarkMapper.listReportAct(cTrademark);
		}


//		TODO	汇总	
		TrademarkVO totalVo = new TrademarkVO();
		totalVo.setTotals(new HashMap<String, Long>());
		String cityCode = cTrademark.getCityCode();
		if (StringUtils.isBlank(cityCode)) {
			totalVo.setAreaName("云南省");
		} else {
			String areaName = "---";
			BladeRegion bladeRegion = BladeRegionUtils.selectByCode(cityCode);
//			BladeRegion bladeRegion = bladeRegionService.selectByCityName(cityCode, "53");
			if (bladeRegion != null) {
				String name = bladeRegion.getName();
				if (StringUtils.isNotBlank(bladeRegion.getCityShortName())) {
					name = bladeRegion.getCityShortName().replace("-", "");
				} else if (StringUtils.isNotBlank(bladeRegion.getDistrictShortName())) {
					name = bladeRegion.getDistrictShortName();
				}
				areaName = name;
			}
			totalVo.setAreaName(areaName);
		}

		for (String year : years) {
			totalVo.getTotals().put(year, 0L);
		}

		for (Map map : dataList) {

//			String areaName = getStr(map, "modify_city");
			String areaName = getStr(map, "modify_city_code");
			if (!alreadyMap.containsKey(areaName)) {
				TrademarkVO vo = new TrademarkVO();
				vo.setAreaName(areaName);
				alreadyMap.put(areaName, vo);
				vo.setTotals(new HashMap<String, Long>());
				for (String year : years) {
					vo.getTotals().put(year, 0L);
				}
			}

			TrademarkVO vo = alreadyMap.get(areaName);
			String year = getStr(map, "reg_year");
			if (!years.contains(year)) {
				continue;
			}
			long oldCnt = 0L;
			if (vo.getTotals().containsKey(year)) {
				oldCnt = vo.getTotals().get(year);
			}
			long newCnt = getLong(map, "total_cnt");
			vo.getTotals().put(year, oldCnt + newCnt);

			if (totalVo.getTotals().containsKey(year)) {
				long oldTotalCnt = totalVo.getTotals().get(year);
				totalVo.getTotals().put(year, oldTotalCnt + newCnt);
			}

		}
		if(!alreadyMap.isEmpty()) {
			dto.getList().add(totalVo);
		}
		
//		dto.getList().addAll(alreadyMap.values());

		List<String> codes = BladeRegionUtils.sortedKey(alreadyMap.keySet());
		for (String code : codes) {

			TrademarkVO vo = alreadyMap.get(code);

			String areaName = BladeRegionUtils.getDistrictShortName(code);
			if (StringUtils.isNotBlank(areaName)) {
				vo.setAreaName(areaName);
			}

			dto.getList().add(vo);
		}

		return dto;

	}

	public TrademarkDTO listReportAct_bak(CTrademark cTrademark) {

		// TODO Auto-generated method stub
//		List<TrademarkVO>list=new ArrayList<TrademarkVO>();

		TrademarkDTO dto = new TrademarkDTO();
		dto.setList(new ArrayList<TrademarkVO>());
		Map<String, TrademarkVO> alreadyMap = new HashMap<String, TrademarkVO>();

		List<Map> dataList = cTrademarkMapper.listReportAct(cTrademark);

		int maxLen = 6;

		Set<String> yearSet2 = new HashSet<String>();
		for (Map map : dataList) {
			yearSet2.add(getStr(map, "reg_year"));
		}

		if (StringUtils.isNotBlank(cTrademark.getBeginDate()) && StringUtils.isNotBlank(cTrademark.getEndDate())
				&& Integer.parseInt(cTrademark.getBeginDate()) <= Integer.parseInt(cTrademark.getEndDate())) {
			int begin = Integer.parseInt(cTrademark.getBeginDate().substring(0, 4));
			int end = Integer.parseInt(cTrademark.getEndDate().substring(0, 4));
			for (int b = begin; b < end; b++) {
				yearSet2.add((b + 1) + "");
			}
		} else {
			int currYear = Integer.parseInt(new SimpleDateFormat("yyyy").format(new Date()));
			for (int i = 0; i < maxLen; i++) {
				yearSet2.add((currYear - (maxLen - 1)) + "");
			}
		}
		List<String> years2 = new ArrayList<String>();
		years2.addAll(yearSet2);

		Collections.sort(years2, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				return Integer.parseInt(o1) - Integer.parseInt(o2);
			}

		});

		List<String> years = years2;
		if (years2.size() > maxLen) {
			years = new ArrayList<String>();
			for (int i = maxLen; i > 0; i--) {
				years.add(years2.get(years2.size() - i));
			}
		}
		dto.setYears(years);

//		TODO	汇总	
		TrademarkVO totalVo = new TrademarkVO();
		totalVo.setTotals(new HashMap<String, Long>());
		String cityCode = cTrademark.getCityCode();
		if (StringUtils.isBlank(cityCode)) {
			totalVo.setAreaName("云南省");
		} else {
			String areaName = "---";
			BladeRegion bladeRegion = BladeRegionUtils.selectByCode(cityCode);
//			BladeRegion bladeRegion = bladeRegionService.selectByCityName(cityCode, "53");
			if (bladeRegion != null) {
				String name = bladeRegion.getName();
				if (StringUtils.isNotBlank(bladeRegion.getCityShortName())) {
					name = bladeRegion.getCityShortName().replace("-", "");
				} else if (StringUtils.isNotBlank(bladeRegion.getDistrictShortName())) {
					name = bladeRegion.getDistrictShortName();
				}
				areaName = name;
			}
			totalVo.setAreaName(areaName);
		}

		for (String year : years) {
			totalVo.getTotals().put(year, 0L);
		}

		for (Map map : dataList) {

			String areaName = getStr(map, "modify_city");
			if (!alreadyMap.containsKey(areaName)) {
				TrademarkVO vo = new TrademarkVO();
				vo.setAreaName(areaName);
				alreadyMap.put(areaName, vo);
				vo.setTotals(new HashMap<String, Long>());
				for (String year : years) {
					vo.getTotals().put(year, 0L);
				}
			}

			TrademarkVO vo = alreadyMap.get(areaName);
			String year = getStr(map, "reg_year");
			if (!years.contains(year)) {
				continue;
			}
			long oldCnt = 0L;
			if (vo.getTotals().containsKey(year)) {
				oldCnt = vo.getTotals().get(year);
			}
			long newCnt = getLong(map, "total_cnt");
			vo.getTotals().put(year, oldCnt + newCnt);

			if (totalVo.getTotals().containsKey(year)) {
				long oldTotalCnt = totalVo.getTotals().get(year);
				totalVo.getTotals().put(year, oldTotalCnt + newCnt);
			}

		}
		dto.getList().add(totalVo);
		dto.getList().addAll(alreadyMap.values());

		return dto;

	}

	@Override
	public TrademarkDTO listReportApply(CTrademark cTrademark) {
		// TODO Auto-generated method stub
//		List<TrademarkVO>list=new ArrayList<TrademarkVO>();

		TrademarkDTO dto = new TrademarkDTO();
		dto.setList(new ArrayList<TrademarkVO>());
		Map<String, TrademarkVO> alreadyMap = new HashMap<String, TrademarkVO>();

		
		boolean retEmpty=false;
		if (StringUtils.isNotBlank(cTrademark.getCityCode()) && !"53".equals(cTrademark.getCityCode())) {
			String cityCode = cTrademark.getCityCode().trim();
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cc=SecurityUtils.needFilterByCityCode();
				if(!cc.equals(cityCode) ) {
					retEmpty=true;
				}
			}
		}else {
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cityCode=SecurityUtils.needFilterByCityCode();
				cTrademark.setCityCode(cityCode);
			}
		}
//		TODO	汇总	
		TrademarkVO totalVo = new TrademarkVO();
		totalVo.setTotals(new HashMap<String, Long>());
		String cityCode = cTrademark.getCityCode();
		if (StringUtils.isBlank(cityCode)) {
			totalVo.setAreaName("云南省");
		} else {
			String areaName = "---";
			BladeRegion bladeRegion = BladeRegionUtils.selectByCode(cityCode);
//			BladeRegion bladeRegion = bladeRegionService.selectByCityName(cityCode, "53");
			if (bladeRegion != null) {
				String name = bladeRegion.getName();
				if (StringUtils.isNotBlank(bladeRegion.getCityShortName())) {
					name = bladeRegion.getCityShortName().replace("-", "");
				} else if (StringUtils.isNotBlank(bladeRegion.getDistrictShortName())) {
					name = bladeRegion.getDistrictShortName();
				}
				areaName = name;
			}
			totalVo.setAreaName(areaName);
		}
		List<Map> dataList =new ArrayList<Map>();
		if(!retEmpty) {
			dataList = cTrademarkMapper.listReportApply(cTrademark);
		}

		Set<String> yearSet = new HashSet<String>();
		for (Map map : dataList) {
			yearSet.add(getStr(map, "apl_year"));
		}

		List<String> years = new ArrayList<String>();
		years.addAll(yearSet);
		if (years.isEmpty()) {
			if (StringUtils.isNotBlank(cTrademark.getBeginDate()) && StringUtils.isNotBlank(cTrademark.getEndDate())
					&& Integer.parseInt(cTrademark.getBeginDate()) <= Integer.parseInt(cTrademark.getEndDate())) {
				int begin = Integer.parseInt(cTrademark.getBeginDate().substring(0, 4));
				int end = Integer.parseInt(cTrademark.getEndDate().substring(0, 4));
				for (int b = begin; b <= end; b++) {
					years.add(b + "");
				}
			} else {
				int currYear = Integer.parseInt(new SimpleDateFormat("yyyy").format(new Date()));
				for (int i = 0; i < 6; i++) {
					years.add((currYear - 5) + "");
				}
			}
		}

		Collections.sort(years, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				return Integer.parseInt(o1) - Integer.parseInt(o2);
			}

		});

		dto.setYears(years);

		for (String year : years) {
			totalVo.getTotals().put(year, 0L);
		}

		for (Map map : dataList) {

//			String areaName = getStr(map, "modify_city");
			String areaName = getStr(map, "modify_city_code");
			if (!alreadyMap.containsKey(areaName)) {
				TrademarkVO vo = new TrademarkVO();
				vo.setAreaName(areaName);
				alreadyMap.put(areaName, vo);
				vo.setTotals(new HashMap<String, Long>());
				for (String year : yearSet) {
					vo.getTotals().put(year, 0L);
				}
			}

			TrademarkVO vo = alreadyMap.get(areaName);
			String year = getStr(map, "apl_year");
			long oldCnt = 0L;
			if (vo.getTotals().containsKey(year)) {
				oldCnt = vo.getTotals().get(year);
			}
			long newCnt = getLong(map, "total_cnt");
			vo.getTotals().put(year, oldCnt + newCnt);

			if (totalVo.getTotals().containsKey(year)) {
				long oldTotalCnt = totalVo.getTotals().get(year);
				totalVo.getTotals().put(year, oldTotalCnt + newCnt);
			}

		}
		
		if(!alreadyMap.isEmpty()) {

			dto.getList().add(totalVo);
		}

		List<String> codes = BladeRegionUtils.sortedKey(alreadyMap.keySet());
		for (String code : codes) {

			TrademarkVO vo = alreadyMap.get(code);

			String areaName = BladeRegionUtils.getDistrictShortName(code);
			if (StringUtils.isNotBlank(areaName)) {
				vo.setAreaName(areaName);
			}

			dto.getList().add(vo);
		}

//		dto.getList().addAll(alreadyMap.values());

		return dto;
	}

	public List<TrademarkVO> listReportNc(CTrademark cTrademark, String type) {

		return null;

	}

	@Override
	public List<TrademarkVO> listReportNc(CTrademark cTrademark) {
		// TODO Auto-generated method stub
		List<TrademarkVO> list = new ArrayList<TrademarkVO>();
		cTrademark.setCurrDate(new SimpleDateFormat("yyyyMMdd").format(new Date()));

		Map<String, TrademarkVO> alreadyMap = new HashMap<String, TrademarkVO>();
		cTrademark.setNcType("reg");
		
		boolean retEmpty=false;
		if (StringUtils.isNotBlank(cTrademark.getCityCode()) && !"53".equals(cTrademark.getCityCode())) {
			String cityCode = cTrademark.getCityCode().trim();
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cc=SecurityUtils.needFilterByCityCode();
				if(!cc.equals(cityCode) ) {
					retEmpty=true;
				}
			}
		}else {
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cityCode=SecurityUtils.needFilterByCityCode();
				cTrademark.setCityCode(cityCode);
			}
		}
		
		List<Map> dataList =new ArrayList<Map>(); 
		if(!retEmpty) {
			dataList = cTrademarkMapper.listReportNc(cTrademark);
		}
		for (Map map : dataList) {
			String nc = getStr(map, "nc");
			if (!alreadyMap.containsKey(nc)) {
				TrademarkVO vo = new TrademarkVO();
				vo.setRegTotalCnt(0L);
				vo.setAplTotalCnt(0L);
				vo.setActTotalCnt(0L);
				vo.setNc(nc);
				String ncStr = nc;
				if (NC_MAP.containsKey(nc)) {
					ncStr = NC_MAP.get(nc);
				}
				vo.setNcStr(ncStr);
				alreadyMap.put(nc, vo);
			}
			TrademarkVO vo = alreadyMap.get(nc);
			vo.setRegTotalCnt(vo.getRegTotalCnt() + getLong(map, "total_cnt"));
		}

		cTrademark.setNcType("apl");
		if(!retEmpty) {
			dataList = cTrademarkMapper.listReportNc(cTrademark);
		}
		
		for (Map map : dataList) {
			String nc = getStr(map, "nc");
			if (!alreadyMap.containsKey(nc)) {
				TrademarkVO vo = new TrademarkVO();
				vo.setRegTotalCnt(0L);
				vo.setAplTotalCnt(0L);
				vo.setActTotalCnt(0L);
				vo.setNc(nc);
				String ncStr = nc;
				if (NC_MAP.containsKey(nc)) {
					ncStr = NC_MAP.get(nc);
				}
				vo.setNcStr(ncStr);
				alreadyMap.put(nc, vo);
			}
			TrademarkVO vo = alreadyMap.get(nc);
			vo.setAplTotalCnt(vo.getAplTotalCnt() + getLong(map, "total_cnt"));
		}

		cTrademark.setNcType("act");
		if(!retEmpty) {
			dataList = cTrademarkMapper.listReportNc(cTrademark);
		}
		
		for (Map map : dataList) {
			String nc = getStr(map, "nc");
			if (!alreadyMap.containsKey(nc)) {
				TrademarkVO vo = new TrademarkVO();
				vo.setRegTotalCnt(0L);
				vo.setAplTotalCnt(0L);
				vo.setActTotalCnt(0L);
				vo.setNc(nc);
				String ncStr = nc;
				if (NC_MAP.containsKey(nc)) {
					ncStr = NC_MAP.get(nc);
				}
				vo.setNcStr(ncStr);
				alreadyMap.put(nc, vo);
			}
			TrademarkVO vo = alreadyMap.get(nc);
			vo.setActTotalCnt(vo.getActTotalCnt() + getLong(map, "total_cnt"));
		}

		list.addAll(alreadyMap.values());

		Collections.sort(list, new Comparator<TrademarkVO>() {
			@Override
			public int compare(TrademarkVO o1, TrademarkVO o2) {
				try {
					return Integer.parseInt(o1.getNc()) - Integer.parseInt(o2.getNc());
				} catch (Exception e) {
					return o1.getNc().compareTo(o2.getNc());
				}

			}
		});

		return list;
	}

	@Override
	public List<TrademarkVO> listReportHouse(CTrademark cTrademark) {
		List<TrademarkVO> list = new ArrayList<TrademarkVO>();
		
		boolean retEmpty=false;
		if (StringUtils.isNotBlank(cTrademark.getCityCode()) && !"53".equals(cTrademark.getCityCode())) {
			String cityCode = cTrademark.getCityCode().trim();
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cc=SecurityUtils.needFilterByCityCode();
				if(!cc.equals(cityCode) ) {
					retEmpty=true;
				}
			}
		}else {
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cityCode=SecurityUtils.needFilterByCityCode();
				cTrademark.setCityCode(cityCode);
			}
		}
		List<Map> dataList =new ArrayList<Map>();
		if(!retEmpty) {
			dataList = cTrademarkMapper.listReportHouse(cTrademark);
		}
		int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(new Date()));
		if (StringUtils.isNotBlank(cTrademark.getYear())) {
			year = Integer.parseInt(cTrademark.getYear());
		}

		Map<String, String> markMap = getMarketsub(year + "");
		if (markMap.isEmpty()) {
			markMap = getMarketsub((year - 1) + "");// 用上一年的数据
		}
		if (markMap.isEmpty()) {
			markMap = getMarketsub((2022) + "");// 用上一年的数据
		}

		TrademarkVO totalVo = new TrademarkVO();
		totalVo.setAreaName("云南省");
		totalVo.setTotalCnt(0L);

		double totalSubjectCnt = 0d;

		for (Map map : dataList) {
			TrademarkVO vo = new TrademarkVO();
			list.add(vo);
			vo.setAreaName(getStr(map, "modify_city"));
			vo.setAreaCode(getStr(map, "modify_city_code"));

			String areaName = BladeRegionUtils.getDistrictShortName(vo.getAreaCode());
			if (StringUtils.isNotBlank(areaName)) {
				vo.setAreaName(areaName);
			}

			vo.setTotalCnt(getLong(map, "total_cnt"));// 有效量

			totalVo.setTotalCnt(vo.getTotalCnt() + totalVo.getTotalCnt());

			if (markMap.containsKey(vo.getAreaName())) {
				vo.setSubjectCnt(markMap.get(vo.getAreaName()));
			} else {
				vo.setSubjectCnt("0");
			}

			totalSubjectCnt = totalSubjectCnt + Double.parseDouble(vo.getSubjectCnt());

			Double growThDouble = 0.0;
			if (!"0".equals(vo.getSubjectCnt())) {
				growThDouble = Double.parseDouble(vo.getTotalCnt() + "") / Double.parseDouble(vo.getSubjectCnt());
			}
			String growTh = String.format("%.2f", growThDouble);
			vo.setMillionArg(growTh);
		}

		Double totalThDouble = 0.0;
		if (totalSubjectCnt > 0) {
			totalThDouble = Double.parseDouble(totalVo.getTotalCnt() + "") / totalSubjectCnt;
		}
		totalVo.setSubjectCnt(String.format("%.4f", totalSubjectCnt));
		totalVo.setMillionArg(String.format("%.2f", totalThDouble));

		Collections.sort(list, new Comparator<TrademarkVO>() {
			@Override
			public int compare(TrademarkVO o1, TrademarkVO o2) {
				if (o1 == null && o2 == null) {
					return 0;
				} else if (o1 == null) {
					return -1;
				} else if (o2 == null) {
					return 1;
				} else {
					if (o1.getMillionArg() == null && o2.getMillionArg() == null) {
						return 0;
					} else if (o1.getMillionArg() == null) {
						return -1;
					} else if (o2.getMillionArg() == null) {
						return 1;
					} else {
						if (Double.parseDouble(o2.getMillionArg()) > Double.parseDouble(o1.getMillionArg())) {
							return 1;
						} else if (Double.parseDouble(o2.getMillionArg()) < Double.parseDouble(o1.getMillionArg())) {
							return -1;
						} else {
							return 0;
						}
					}
				}
			}
		});

		for (int i = 0; i < list.size(); i++) {
			list.get(i).setRanking(i + 1);
		}
		list.add(totalVo);
		return list;
	}

	@Override
	public List<TrademarkVO> listReportRank(CTrademark cTrademark) {
//		/tm/tm_total_valid?project_code=530000&ak=259894358%4020230406&end_date=20230112
//		String url = ruoYiConfig.getPressApiServer()+"tm/tm_total?project_code=530000&ak=569896359%4020230406";
		String url = ruoYiConfig.getPressApiServer() + "tm/tm_total_valid?project_code=530000&ak=259894358%4020230406";
		String key = "CTrademark:listReportRank2";
		if (StringUtils.isNotBlank(cTrademark.getEndDate())) {
			url = url + "&end_date=" + cTrademark.getEndDate();
			key = key + ":" + cTrademark.getEndDate();
		}
//		ruoYiConfig

		String res = redisCache.getCacheObject(key);
//		String res =null;
//		if (StringUtils.isBlank(res)) {
		res = HttpUtils.sendGet(url);
		if (StringUtils.isNotBlank(res)) {
			redisCache.setCacheObject(key, res);

		}
//		}
		LinkedHashMap map = (LinkedHashMap) JSONUtils.parse(res);
		List<LinkedHashMap> list = (List<LinkedHashMap>) map.get("results");

		List<TrademarkVO> itemList = getRankingList(list, cTrademark.getEndDate());

		return itemList;
	}

	public static void main(String[] args) {
//		String url = "http://111.198.99.166:20000/tm/iprd_tm?project_code=530000&ak=68f39e394@20230406&time_update="
//				+ "20210101" + "&size=1000";
//		String res = HttpUtils.sendGet(url);
//		TrademarkMod mod = new Gson().fromJson(res, TrademarkMod.class);
//		System.out.println(mod.results.size());

//		String url = "http://111.198.99.166:20000/tm/tm_total?project_code=530000&ak=569896359%4020230406";
//		String res = HttpUtils.sendGet(url);
//		LinkedHashMap map = (LinkedHashMap) JSONUtils.parse(res);
//		List<LinkedHashMap> list = (List<LinkedHashMap>) map.get("results");
//		List<TrademarkVO> itemList = getRankingList(list);
//		System.out.println("ok");
	}

	public List<TrademarkVO> getRankingList(List<LinkedHashMap> list, String endDate) {
		List<TrademarkVO> itemList = new ArrayList<TrademarkVO>();

		Long ynSum = cTrademarkMapper.listReportActSum(endDate);
		for (LinkedHashMap<String, Object> map : list) {
			boolean bro = false;
			for (Map.Entry<String, Object> ent : map.entrySet()) {
				if (ent.getKey().contains("全国")) {
					bro = true;
					continue;
				}
//				TODO	这里要取云南本地数据
//				商标有效量
				if (ent.getKey().contains("商标有效量")) {

					String province = ent.getKey().replace("商标有效量", "");
					if (province.endsWith("省")) {
						province = province.substring(0, province.length() - 1);
					}

					if ("云南".equals(province) && ynSum.longValue() > 0) {
						TrademarkVO item = new TrademarkVO();
						itemList.add(item);
						item.setProvince(province);
						item.setNumber(ynSum);// 云南省的用本地数据
					} else {
						TrademarkVO item = new TrademarkVO();
						itemList.add(item);
						item.setProvince(province);
						item.setNumber(ent.getValue() == null ? 0L : Long.parseLong(ent.getValue() + ""));
					}

					continue;
				}
			}
			if (bro) {
				continue;
			}
		}

		Collections.sort(itemList, new Comparator<TrademarkVO>() {
			@Override
			public int compare(TrademarkVO o1, TrademarkVO o2) {
				if (o1 == null && o2 == null) {
					return 0;
				} else if (o1 == null) {
					return -1;
				} else if (o2 == null) {
					return 1;
				} else {
					if (o1.getNumber() == null && o2.getNumber() == null) {
						return 0;
					} else if (o1.getNumber() == null) {
						return -1;
					} else if (o2.getNumber() == null) {
						return 1;
					} else {
						return (int) (o2.getNumber() - o1.getNumber());
					}
				}
			}
		});

		for (int i = 0; i < itemList.size(); i++) {
			itemList.get(i).setRanking(i + 1);
		}
		return itemList;
	}

//	private String getSubjectCntMock(String areaName) {
//		
//		
//		if (SUBJECT_MOCK_MAP.containsKey(areaName)) {
//			return SUBJECT_MOCK_MAP.get(areaName);
//		} else {
//			return "100";
//		}
//
//	}

	private String getStr(Map map, String key) {
		if (map == null || key == null || !map.containsKey(key)) {
			return "";
		}
		return map.get(key).toString();
	}

	private int getInt(Map map, String key) {
		if (map == null || key == null || !map.containsKey(key)) {
			return 0;
		}
		return Integer.parseInt(map.get(key).toString());
	}

	private long getLong(Map map, String key) {
		if (map == null || key == null || !map.containsKey(key)) {
			return 0;
		}
		return Long.parseLong(map.get(key).toString());
	}

	public void loadToCacheWidthPid() {
		logger.debug("开始加载公开数据缓存,准备查询");
		List<CTrademark> list = queryList(new CTrademark());
		int count = list.size();
		logger.debug("商标数据查询完成，一共" + count + "条");
		for (int i = 0; i < list.size(); i++) {
			logger.debug("准备将第[" + i + "]条商标数据库载缓存，一共[" + count + "]条");
			String key = getKeyByPid(list.get(i).getPid());
//			redisCache.setCacheObject(key, list.get(i));
			CTrademark_MAP.put(key, list.get(i));
		}
	}

	public CTrademark queryByPidFormCache(String pid) {
		String key = getKeyByPid(pid);
		CTrademark t = CTrademark_MAP.get(key);
		return t;
	}

	public CTrademark queryByPid(String pid) {
		String key = getKeyByPid(pid);
		CTrademark t = redisCache.getCacheObject(key);
		if (t != null && StringUtils.isNotBlank(t.getPid())) {
			return t;
		}

		CTrademark qo = new CTrademark();
		qo.setPid(pid);
		List<CTrademark> list = queryList(qo);
		if (list != null && !list.isEmpty() && list.get(0) != null) {
			return list.get(0);
		}
		return null;
	}

	public String getKeyByPid(String pid) {
		String key = "CTrademark:Pid:" + pid.trim();
		return key;
	}

	public void syncTrademarkArea() {
		logger.debug("开始加载商标数据数据缓存...");
		long begin = System.currentTimeMillis();
		loadToCacheWidthPid();

//		if(trademarkSaveQueue==null) {
//			trademarkSaveQueue = new TrademarkSaveQueue(2000000,this);
//		}

		long endTime = System.currentTimeMillis();
		int total = CTrademark_MAP.size();
		logger.debug("商标数据缓存加载完成,用时:" + (endTime - begin) + ",一共[" + total + "]条");

		int i = 0;
		int n = 0;
		for (CTrademark t : CTrademark_MAP.values()) {
			i++;
			boolean hasChange = false;
			logger.debug("当前第[" + i + "]第，一共[" + total + "]条");
			if (StringUtils.isNotBlank(t.getProvince())) {
				BladeRegion bladeRegion = bladeRegionService.selectByProvinceName(t.getProvince());
				if (bladeRegion != null) {
					t.setProvinceCode(bladeRegion.getCode());
				} else {
					hasChange = true;
					t.setProvinceCode("00");// 20231017 非本区划
				}
			}

			if (StringUtils.isNotBlank(t.getCity()) && StringUtils.isNotBlank(t.getProvinceCode())) {
				BladeRegion bladeRegion = bladeRegionService.selectByCityName(t.getCity(), t.getProvinceCode());
				if (bladeRegion != null) {
					t.setCityCode(bladeRegion.getCode());
				} else {
					hasChange = true;
					t.setCityCode("0000");// 20231017 非本区划
				}
			}

			if (StringUtils.isNotBlank(t.getCounty()) && StringUtils.isNotBlank(t.getCityCode())) {
				BladeRegion bladeRegion = bladeRegionService.selectByAreaName(t.getCounty(), t.getCityCode());
				if (bladeRegion != null) {
					t.setCountyCode(bladeRegion.getCode());
				} else {
					hasChange = true;
					t.setCountyCode("000000");// 20231017 非本区划
				}
			}

			if (StringUtils.isNotBlank(t.getModifyProvince())) {
				BladeRegion bladeRegion = bladeRegionService.selectByProvinceName(t.getModifyProvince());
				if (bladeRegion != null) {
					t.setModifyProvinceCode(bladeRegion.getCode());
				} else {
					hasChange = true;
					t.setModifyProvinceCode("00");// 20231017 非本区划
				}
			}

			if (StringUtils.isNotBlank(t.getModifyCity()) && StringUtils.isNotBlank(t.getModifyProvinceCode())) {
				BladeRegion bladeRegion = bladeRegionService.selectByCityName(t.getModifyCity(), t.getModifyProvinceCode());
				if (bladeRegion != null) {
					t.setModifyCityCode(bladeRegion.getCode());
				} else {
					hasChange = true;
					t.setModifyCityCode("0000");// 20231017 非本区划
				}
			}

			if (StringUtils.isNotBlank(t.getModifyCounty()) && StringUtils.isNotBlank(t.getModifyCityCode())) {
				BladeRegion bladeRegion = bladeRegionService.selectByAreaName(t.getModifyCounty(), t.getModifyCityCode());
				if (bladeRegion != null) {
					t.setModifyCountyCode(bladeRegion.getCode());
				} else {
					hasChange = true;
					t.setModifyCountyCode("000000");// 20231017 非本区划
				}
			}
			if (hasChange) {
				logger.debug("查询到第" + (++n) + "条非本区划数据:" + new Gson().toJson(t));
				t.setStatus(1L);// 禁用
				updateById(t);
//				trademarkSaveQueue.putQueue(t);
			}
		}

	}

	private static class SyncThread extends Thread {
		private List<CTrademark> tiList;
		private ICTrademarkService cTrademarkService;

		public SyncThread(ICTrademarkService cTrademarkService, List<CTrademark> tiList) {
			this.tiList = tiList;
			this.cTrademarkService = cTrademarkService;
		}

		public void run() {
			logger.debug("准备异步保存,数量:" + tiList.size());
			for (CTrademark t : tiList) {
				cTrademarkService.save(t);
			}
		}

	}

	@Override
	public void doSyncForIprd(String beginDate, String endDate) {
		// TODO Auto-generated method stub
//		http://111.198.99.166:20000/tm/iprd_tm?project_code=530000&ak=68f39e394@20230406&time_update=20210101&size=100&scroll_id=1
		logger.debug("开始清空商标数据数据...");

//		TrademarkSaveQueue trademarkSaveQueue = new TrademarkSaveQueue(2000000, this);

		try {
			Thread.sleep(2000);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		long begin = System.currentTimeMillis();
//		loadToCacheWidthPid();
		cTrademarkMapper.clearData();
		long endTime = System.currentTimeMillis();
		logger.debug("清空商标数据完成,用时:" + (endTime - begin));

		String baseUrl = ruoYiConfig.getPressApiServer() + "tm/iprd_tm?project_code=530000&ak=68f39e394@20230406&time_update=" + beginDate + "&size=10000";

		String code = "200";
		String scroll_id = null;

		List<SyncThread> threadList = new ArrayList<SyncThread>();

		int i = 0;
		while ("200".equals(code)) {

			String url = baseUrl;
			if (StringUtils.isNotBlank(scroll_id)) {
				url = baseUrl + "&scroll_id=" + scroll_id;
			}
			i++;
			logger.debug("开始查询第" + i + "页商标基本数据,url:" + url);
			long beginTime = System.currentTimeMillis();
			String res = HttpUtils.sendGet(url);
			logger.debug("第" + i + "页商标基本数据查询,用时:" + (System.currentTimeMillis() - beginTime));
			logger.debug("商标基本数据结果" + res);
			TrademarkMod mod = new Gson().fromJson(res, TrademarkMod.class);
			scroll_id = mod.scroll_id;
			code = mod.code;
			if (mod.results == null || mod.results.isEmpty()) {
				break;
			}

			List<CTrademark> tiList = new ArrayList<CTrademark>();

			for (TrademarkIprd ti : mod.results) {

//				CTrademark old = queryByPid(ti._id);
//				CTrademark old = queryByPidFormCache(ti._id);//不再从缓存中加载
				CTrademark old = null;
//				if(old!=null) {
////					TODO	
//					updateForImp(old,ti);
//					continue;
//				}
				CTrademark t = ti.toCTrademark();
				boolean hasChange = false;
				if (StringUtils.isNotBlank(t.getProvince())) {
					BladeRegion bladeRegion = bladeRegionService.selectByProvinceName(t.getProvince());
					if (bladeRegion != null) {
						t.setProvinceCode(bladeRegion.getCode());
					} else {
						hasChange = true;
						t.setProvinceCode("00");// 20231017 非本区划
					}
				}

				if (StringUtils.isNotBlank(t.getCity()) && StringUtils.isNotBlank(t.getProvinceCode())) {
					BladeRegion bladeRegion = bladeRegionService.selectByCityName(t.getCity(), t.getProvinceCode());
					if (bladeRegion != null) {
						t.setCityCode(bladeRegion.getCode());
					} else {
						hasChange = true;
						t.setCityCode("0000");// 20231017 非本区划
					}
				}

				if (StringUtils.isNotBlank(t.getCounty()) && StringUtils.isNotBlank(t.getCityCode())) {
					BladeRegion bladeRegion = bladeRegionService.selectByAreaName(t.getCounty(), t.getCityCode());
					if (bladeRegion != null) {
						t.setCountyCode(bladeRegion.getCode());
					} else {
						hasChange = true;
						t.setCountyCode("000000");// 20231017 非本区划
					}
				}

				if (StringUtils.isNotBlank(t.getModifyProvince())) {
					BladeRegion bladeRegion = bladeRegionService.selectByProvinceName(t.getModifyProvince());
					if (bladeRegion != null) {
						t.setModifyProvinceCode(bladeRegion.getCode());
					} else {
						hasChange = true;
						t.setModifyProvinceCode("00");// 20231017 非本区划
					}
				}

				if (StringUtils.isNotBlank(t.getModifyCity()) && StringUtils.isNotBlank(t.getModifyProvinceCode())) {
					BladeRegion bladeRegion = bladeRegionService.selectByCityName(t.getModifyCity(), t.getModifyProvinceCode());
					if (bladeRegion != null) {
						t.setModifyCityCode(bladeRegion.getCode());
					} else {
						hasChange = true;
						t.setModifyCityCode("0000");// 20231017 非本区划
					}
				}

				if (StringUtils.isNotBlank(t.getModifyCounty()) && StringUtils.isNotBlank(t.getModifyCityCode())) {
					BladeRegion bladeRegion = bladeRegionService.selectByAreaName(t.getModifyCounty(), t.getModifyCityCode());
					if (bladeRegion != null) {
						t.setModifyCountyCode(bladeRegion.getCode());
					} else {
						hasChange = true;
						t.setModifyCountyCode("000000");// 20231017 非本区划
					}
				}

				t.setAplYearSrc(t.getAplYear());
				t.setRegYearSrc(t.getRegYear());
				if (hasChange) {
					t.setStatus(1L);
				} else {
					t.setStatus(0L);
				}

				t.setAplYear(getModifyYear(t.getAplDate(), t.getAplYear()));
				t.setRegYear(getModifyYear(t.getRegDate(), t.getRegYear()));
				if (old != null) {
					t.setUpdateTime(new Date());
					t.setId(old.getId());
					logger.info("准备更新标基本数据," + new Gson().toJson(t));
					if (StringUtils.isNotBlank(t.getRegDate())) {
//						if (20230702 < Integer.parseInt(t.getRegDate())) {
//							logger.info("查询到202307以后的商标数据,id:" + t.getPid());
//						}
					}
//					updateById(t);
//					trademarkSaveQueue.putQueue(t);
				} else {
					logger.info("准备插入商标基本数据," + new Gson().toJson(t));
//					save(t);
//					trademarkSaveQueue.putQueue(t);
					tiList.add(t);
				}

			}

			SyncThread thread = new SyncThread(cTrademarkService, tiList);
			threadList.add(thread);
			thread.start();

		}

		for (Thread t : threadList) {
			try {
				t.join();
			} catch (InterruptedException e) {
				logger.error(e.getMessage(), e);
			}
		}

//		trademarkSaveQueue.setCollEd(true);

//		for(Thread t:trademarkSaveQueue.getThreads()) {
//			try {
//				t.join();
//			} catch (InterruptedException e) {
//				logger.error(e.getMessage(),e);
//			}
//		}

		logger.debug("所有线程都同步完了，准备统计中间表,");
		doSyncMark();
		logger.debug("统计中间表完成");

	}

	private String getModifyYear(String date, String year) {
		if (StringUtils.isBlank(date) || date.length() != 8) {
			return year;
		}
		int yearInt = Integer.parseInt(date.substring(0, 4));
		int dateInt = Integer.parseInt(date.substring(4, 8));
		if (dateInt >= 1216) {
			yearInt++;
		}
		return yearInt + "";

	}

	public static class TrademarkMod {
		public String code;
		public Long total;
		public String scroll_id;
		public List<TrademarkIprd> results;
	}

	public static class TrademarkIprd {
		private String _id;
		private String apl_no;
		private String country;
		private String app_type;
		private String agent_name;
		private String city;
		private String modify_county;
		private String county;
		private String industry;
		private String apl_date;
		private String time_update;
		private String old_city;
		private String modify_province;
		private String province;
		private String apl_year;
		private String reg_month;
		private String reg_year;
		private String agent_code;
		private String park;
		private String similar_code;
		private String property_bgn_date;
		private String app;
		private String time_create;
		private String address;
		private String app_region_code;
		private String reg_date;
		private String property_end_date;
		private String old_province;
		private String nc;
		private String name;
		private String apl_month;
		private String old_county;
		private String modify_city;

		public CTrademark toCTrademark() {
			CTrademark t = new CTrademark();
//			private String _id;
			t.setPid(_id);
//			private String apl_no;
			if (StringUtils.isNotBlank(apl_no)) {
				t.setAplNo(apl_no);
			}

//			private String country;

			if (StringUtils.isNotBlank(country)) {
				t.setCountry(country);
			}

//			private String app_type;

			if (StringUtils.isNotBlank(app_type)) {
				t.setAppType(app_type);
			}

//			private String agent_name;

			if (StringUtils.isNotBlank(agent_name)) {
				t.setAgentName(agent_name);
			}

//			private String city;

			if (StringUtils.isNotBlank(city)) {
				t.setCity(city);
			}

//			private String modify_county;

			if (StringUtils.isNotBlank(modify_county)) {
				t.setModifyCounty(modify_county);
			}

//			private String county;

			if (StringUtils.isNotBlank(county)) {
				t.setCountry(county);
			}

//			private String industry;

			if (StringUtils.isNotBlank(industry)) {
				t.setIndustry(industry);
			}

//			private String apl_date;

			if (StringUtils.isNotBlank(apl_date)) {
				t.setAplDate(apl_date);
			}

//			private String time_update;

			if (StringUtils.isNotBlank(time_update)) {
				t.setTimeUpdate(time_update);
			}

//			private String old_city;

			if (StringUtils.isNotBlank(old_city)) {
				t.setOldCity(old_city);
			}

//			private String modify_province;

			if (StringUtils.isNotBlank(modify_province)) {
				t.setModifyProvince(modify_province);
			}

//			private String province;

			if (StringUtils.isNotBlank(province)) {
				t.setProvince(province);
			}

//			private String apl_year;

			if (StringUtils.isNotBlank(apl_year)) {
				t.setAplYear(apl_year);
			}

//			private String reg_month;

			if (StringUtils.isNotBlank(reg_month)) {
				t.setRegMonth(reg_month);
			}

//			private String reg_year;

			if (StringUtils.isNotBlank(reg_year)) {
				t.setRegYear(reg_year);
			}

//			private String agent_code;

			if (StringUtils.isNotBlank(agent_code)) {
				t.setAgentCode(agent_code);
			}

//			private String park;

			if (StringUtils.isNotBlank(park)) {
				t.setPark(park);
			}

//			private String similar_code;
			if (StringUtils.isNotBlank(similar_code)) {
				t.setSimilarCode(similar_code);
			}

//			private String property_bgn_date;

			if (StringUtils.isNotBlank(property_bgn_date)) {
				t.setPropertyBgnDate(property_bgn_date);
			}

//			private String app;

			if (StringUtils.isNotBlank(app)) {
				t.setApp(app);
			}

//			private String time_create;

			if (StringUtils.isNotBlank(time_create)) {
				t.setTimeCreate(time_create);
			}

//			private String address;

			if (StringUtils.isNotBlank(address)) {
				t.setAddress(address);
			}

//			private String app_region_code;

			if (StringUtils.isNotBlank(app_region_code)) {
				t.setAppRegionCode(app_region_code);
			}

//			private String reg_date;

			if (StringUtils.isNotBlank(reg_date)) {
				t.setRegDate(reg_date);
			}

//			private String property_end_date;
			if (StringUtils.isNotBlank(property_end_date)) {
				t.setPropertyEndDate(property_end_date);
			}

//			private String old_province;

			if (StringUtils.isNotBlank(old_province)) {
				t.setOldProvince(old_province);
			}

//			private String nc;

			if (StringUtils.isNotBlank(nc)) {
				t.setNc(nc);
			}

//			private String name;

			if (StringUtils.isNotBlank(name)) {
				t.setName(name);
			}

//			private String apl_month;

			if (StringUtils.isNotBlank(apl_month)) {
				t.setAplMonth(apl_month);
			}

//			private String old_county;

			if (StringUtils.isNotBlank(old_county)) {

			}
			t.setOldCounty(old_county);
//			private String modify_city;

			if (StringUtils.isNotBlank(modify_city)) {
				t.setModifyCity(modify_city);
			}

			return t;
		}

	}

	/**
	 * 查询中国商标列表
	 *
	 * @param cTrademark 中国商标
	 * @return 中国商标集合
	 * @date 2023-04-28
	 */
	@Override
	public List<CTrademark> queryList(CTrademark cTrademark) {
		QueryWrapper<CTrademark> queryWrapper = Wrappers.query();
		if (StringUtils.isNotNull(cTrademark.getPid())) {
			queryWrapper.eq("pid", cTrademark.getPid());
		}
		if (StringUtils.isNotNull(cTrademark.getAplNo())) {
			queryWrapper.eq("apl_no", cTrademark.getAplNo());
		}
		if (StringUtils.isNotEmpty(cTrademark.getAplDate())) {
			queryWrapper.eq("apl_date", cTrademark.getAplDate());
		}
		if (StringUtils.isNotEmpty(cTrademark.getAplYear())) {
			queryWrapper.eq("apl_year", cTrademark.getAplYear());
		}
		if (StringUtils.isNotEmpty(cTrademark.getRegDate())) {
			queryWrapper.eq("reg_date", cTrademark.getRegDate());
		}
		if (StringUtils.isNotEmpty(cTrademark.getRegYear())) {
			queryWrapper.eq("reg_year", cTrademark.getRegYear());
		}
		if (StringUtils.isNotNull(cTrademark.getNc())) {
			queryWrapper.eq("nc", cTrademark.getNc());
		}
		if (StringUtils.isNotEmpty(cTrademark.getApp())) {
			queryWrapper.eq("app", cTrademark.getApp());
		}
		if (StringUtils.isNotEmpty(cTrademark.getAddress())) {
			queryWrapper.eq("address", cTrademark.getAddress());
		}
		if (StringUtils.isNotEmpty(cTrademark.getAppRegionCode())) {
			queryWrapper.eq("app_region_code", cTrademark.getAppRegionCode());
		}
		if (StringUtils.isNotEmpty(cTrademark.getCountry())) {
			queryWrapper.eq("country", cTrademark.getCountry());
		}
		if (StringUtils.isNotEmpty(cTrademark.getProvince())) {
			queryWrapper.eq("province", cTrademark.getProvince());
		}
		if (StringUtils.isNotEmpty(cTrademark.getCity())) {
			queryWrapper.eq("city", cTrademark.getCity());
		}
		if (StringUtils.isNotEmpty(cTrademark.getCounty())) {
			queryWrapper.eq("county", cTrademark.getCounty());
		}
		if (StringUtils.isNotEmpty(cTrademark.getModifyProvince())) {
			queryWrapper.eq("modify_province", cTrademark.getModifyProvince());
		}
		if (StringUtils.isNotEmpty(cTrademark.getModifyCity())) {
			queryWrapper.eq("modify_city", cTrademark.getModifyCity());
		}
		if (StringUtils.isNotEmpty(cTrademark.getModifyCounty())) {
			queryWrapper.eq("modify_county", cTrademark.getModifyCounty());
		}
		if (StringUtils.isNotEmpty(cTrademark.getOldProvince())) {
			queryWrapper.eq("old_province", cTrademark.getOldProvince());
		}
		if (StringUtils.isNotEmpty(cTrademark.getOldCity())) {
			queryWrapper.eq("old_city", cTrademark.getOldCity());
		}
		if (StringUtils.isNotEmpty(cTrademark.getOldCounty())) {
			queryWrapper.eq("old_county", cTrademark.getOldCounty());
		}
		if (StringUtils.isNotEmpty(cTrademark.getName())) {
			queryWrapper.like("name", cTrademark.getName());
		}
		if (StringUtils.isNotEmpty(cTrademark.getType())) {
			queryWrapper.eq("type", cTrademark.getType());
		}
		if (StringUtils.isNotEmpty(cTrademark.getState())) {
			queryWrapper.eq("state", cTrademark.getState());
		}
		if (StringUtils.isNotEmpty(cTrademark.getAgentName())) {
			queryWrapper.like("agent_name", cTrademark.getAgentName());
		}
		if (StringUtils.isNotEmpty(cTrademark.getPropertyBgnDate())) {
			queryWrapper.eq("property_bgn_date", cTrademark.getPropertyBgnDate());
		}
		if (StringUtils.isNotEmpty(cTrademark.getPropertyEndDate())) {
			queryWrapper.eq("property_end_date", cTrademark.getPropertyEndDate());
		}
		if (StringUtils.isNotEmpty(cTrademark.getSimilarCode())) {
			queryWrapper.eq("similar_code", cTrademark.getSimilarCode());
		}
		if (StringUtils.isNotEmpty(cTrademark.getIndustry())) {
			queryWrapper.eq("industry", cTrademark.getIndustry());
		}
		if (StringUtils.isNotEmpty(cTrademark.getPark())) {
			queryWrapper.eq("park", cTrademark.getPark());
		}
		if (StringUtils.isNotEmpty(cTrademark.getAppType())) {
			queryWrapper.eq("app_type", cTrademark.getAppType());
		}
		if (StringUtils.isNotEmpty(cTrademark.getTimeCreate())) {
			queryWrapper.eq("time_create", cTrademark.getTimeCreate());
		}
		if (StringUtils.isNotEmpty(cTrademark.getTimeUpdate())) {
			queryWrapper.eq("time_update", cTrademark.getTimeUpdate());
		}
		if (StringUtils.isNotNull(cTrademark.getStatus())) {
			queryWrapper.eq("status", cTrademark.getStatus());
		}
		if (StringUtils.isNotNull(cTrademark.getSysFileId())) {
			queryWrapper.eq("sys_file_id", cTrademark.getSysFileId());
		}
		queryWrapper.orderByDesc("id");
		return this.list(queryWrapper);
	}

}
