package com.ruoyi.report.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.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.report.domain.RzlViewStatisticsInfo;
import com.ruoyi.report.mapper.RzlViewStatisticsInfoMapper;
import com.ruoyi.report.service.IRzlViewStatisticsInfoService;
import com.ruoyi.report.vo.PatentLastSumDTO;
import com.ruoyi.report.vo.PatentSumItem;
import com.ruoyi.report.vo.ViewStatisticsInfoRep;
import com.ruoyi.report.vo.ViewStatisticsInfoVo;
import com.ruoyi.report.vo.ViewStatisticsInfoVo.Item;
import com.ruoyi.reportrzs.domain.RzsViewStatisticsInfo;
import com.ruoyi.reportrzs.mapper.RzsViewStatisticsInfoMapper;
import com.ruoyi.reportrzs.service.IRzsViewStatisticsInfoService;
import com.ruoyi.system.domain.BladeRegion;
import com.ruoyi.system.service.IBladeRegionService;
import com.ruoyi.utils.BladeRegionUtils;

/**
 * 专利有效-概况统计Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-26
 */
@Service
public class RzlViewStatisticsInfoServiceImpl extends ServiceImpl<RzlViewStatisticsInfoMapper, RzlViewStatisticsInfo> implements IRzlViewStatisticsInfoService {

	@Autowired
	private RzlViewStatisticsInfoMapper rzlViewStatisticsInfoMapper;
	@Autowired
	private IBladeRegionService bladeRegionService;
	@Autowired
	private IRzsViewStatisticsInfoService rzsViewStatisticsInfoService;
	@Autowired
	private RzsViewStatisticsInfoMapper rzsViewStatisticsInfoMapper;

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

	public RzlViewStatisticsInfo getLastOne() {
		PageHelper.startPage(1, 1);
		QueryWrapper<RzlViewStatisticsInfo> queryWrapper = Wrappers.query();
		queryWrapper.orderByDesc("year", "month");
		List<RzlViewStatisticsInfo> list = this.list(queryWrapper);
		if (list != null && list.size() > 0) {
			return list.get(0);
		}
		return null;
	}

	/**
	 * 授权量
	 * 
	 * @return
	 */
	public PatentLastSumDTO authListForLast() {
		PatentLastSumDTO dto = new PatentLastSumDTO();
		RzsViewStatisticsInfo rzsViewStatisticsInfo = rzsViewStatisticsInfoService.getLastOne();
		if (rzsViewStatisticsInfo == null) {
			dto.setList(new ArrayList());
			return dto;
		}

		String year = rzsViewStatisticsInfo.getYear();
		String month = rzsViewStatisticsInfo.getMonth();
		dto.setLastTime(year + "-" + month);

		List<PatentSumItem> itemList = new ArrayList<PatentSumItem>();
		List<Map> mapList = rzsViewStatisticsInfoMapper.listForLast(rzsViewStatisticsInfo);

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

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

		for (Map map : mapList) {
			PatentSumItem item = new PatentSumItem();
			itemList.add(item);
			item.setAuthCnt(getLong(map, "total_cnt"));
			item.setAreaName(getStr(map, "city_name"));
			item.setAreaCode(getStr(map, "city_code"));
			totalItem.setAuthCnt(totalItem.getAuthCnt() + item.getAuthCnt());

			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())) {
				PatentSumItem item = new PatentSumItem();
				item.setAuthCnt(0L);
				item.setAreaName(br.getCityName());
				item.setAreaCode(br.getCityCode());
				item.setAreaShortName(br.getCityShortName());
				itemList.add(item);
			}
		}

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

		itemList.add(0, totalItem);

		dto.setList(itemList);

		return dto;

	}

	public PatentLastSumDTO listForLast() {
		PatentLastSumDTO actDto = actListForLast();
		PatentLastSumDTO authDto = authListForLast();

		for (PatentSumItem item : actDto.getList()) {
			item.setAuthCnt(0L);

			for (PatentSumItem auth : authDto.getList()) {
				if (auth.getAreaCode().equals(item.getAreaCode())) {
					item.setAuthCnt(auth.getAuthCnt());
					continue;
				}
			}
		}

		return actDto;
	}

	/**
	 * 有效量
	 * 
	 * @return
	 */
	public PatentLastSumDTO actListForLast() {
		PatentLastSumDTO dto = new PatentLastSumDTO();
		RzlViewStatisticsInfo rzlViewStatisticsInfo = getLastOne();
		if (rzlViewStatisticsInfo == null) {
			return dto;
		}

		String year = rzlViewStatisticsInfo.getYear();
		String month = rzlViewStatisticsInfo.getMonth();
		dto.setLastTime(year + "-" + month);

		List<PatentSumItem> itemList = new ArrayList<PatentSumItem>();
		List<Map> mapList = rzlViewStatisticsInfoMapper.listForLast(rzlViewStatisticsInfo);

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

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

		for (Map map : mapList) {
			PatentSumItem item = new PatentSumItem();
			itemList.add(item);
			item.setActCnt(getLong(map, "total_cnt"));
			item.setAreaName(getStr(map, "city_name"));
			item.setAreaCode(getStr(map, "city_code"));
			totalItem.setActCnt(totalItem.getActCnt() + item.getActCnt());

			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())) {
				PatentSumItem item = new PatentSumItem();
				item.setActCnt(0L);
				item.setAreaName(br.getCityName());
				item.setAreaCode(br.getCityCode());
				item.setAreaShortName(br.getCityShortName());
				itemList.add(item);
			}
		}

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

		itemList.add(0, totalItem);

		dto.setList(itemList);

		return dto;
	}

	private void margeNotThisArea(List<Map> notThisAreaList, Map dataMap, Map<String, Map> alreadyNotThisArea, String areaCodeKey, String areaNameKey,
			String[] keys, String[] dataKeys) {
		StringBuffer keysb = new StringBuffer();

//		String areaCode = "000000";
		String areaCode = "非本区划";
		String areaName = "非本区划";
		keysb.append(areaCode).append("_").append(areaName);
		for (String key : keys) {
			String val = getStr(dataMap, key);
			if (StringUtils.isBlank(val)) {
				val = "null";
			}
			keysb.append("_").append(val);
		}
		if (!alreadyNotThisArea.containsKey(keysb.toString())) {
			Map newMap = new HashMap();
			newMap.putAll(dataMap);
			newMap.put(areaCodeKey, areaCode);
			newMap.put(areaNameKey, areaName);
			notThisAreaList.add(newMap);
			alreadyNotThisArea.put(keysb.toString(), newMap);
		} else {
			Map oldMap = alreadyNotThisArea.get(keysb.toString());
			for (String key : dataKeys) {
				long oldVal = getLong(oldMap, key);
				long newVal = getLong(dataMap, key);
				oldMap.put(key, oldVal + newVal);
			}
		}

	}

	private boolean isNotThisArea(Map map) {
		Object obj = map.get("not_this_area");
		if (obj != null) {
			return Boolean.parseBoolean(obj + "");
		}
		return false;
	}

	@Override
	public ViewStatisticsInfoRep listByReport(RzlViewStatisticsInfo rzlViewStatisticsInfo) {
		// TODO Auto-generated method stub
		
		boolean retEmpty=false;
		
		if (StringUtils.isNotBlank(rzlViewStatisticsInfo.getCityCode()) && !"53".equals(rzlViewStatisticsInfo.getCityCode())) {
			String cityCode = rzlViewStatisticsInfo.getCityCode().trim();
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cc=SecurityUtils.needFilterByCityCode();
				if(!cc.equals(cityCode) ) {
					retEmpty=true;
				}
			}
		}else {
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cityCode=SecurityUtils.needFilterByCityCode();
				rzlViewStatisticsInfo.setCityCode(cityCode);
			}
		}
		
		

//		List<RzlViewStatisticsInfoVo> typeTotalList = new ArrayList<RzlViewStatisticsInfoVo>();
		String cityCode=null;
		ViewStatisticsInfoRep rep = new ViewStatisticsInfoRep();
		if (StringUtils.isNotBlank(rzlViewStatisticsInfo.getCityCode()) && !"53".equals(rzlViewStatisticsInfo.getCityCode())) {
			cityCode = rzlViewStatisticsInfo.getCityCode().trim();
		}
		rzlViewStatisticsInfo.setCityCode(cityCode);
		String qyear = rzlViewStatisticsInfo.getYear();
		if (StringUtils.isBlank(qyear)) {
			qyear = new SimpleDateFormat("yyyy").format(new Date());
		}
		if (StringUtils.isNotBlank(qyear)) {
			String minYear = (Integer.parseInt(qyear) - 9) + "";
			rzlViewStatisticsInfo.setMinYear(minYear);
			rzlViewStatisticsInfo.setMaxYear(qyear);
		}

		List<Map> mapList =null;
		if(retEmpty) {
			 mapList=new ArrayList<Map>();
		}else {
			mapList = rzlViewStatisticsInfoMapper.listByReport(rzlViewStatisticsInfo);
		}

		boolean needSumNotThisArea = false;
		if (StringUtils.isNotBlank(cityCode)) {
			needSumNotThisArea = true;
			BladeRegionUtils.setNotThisArea(mapList, cityCode);
		}
		List<Map> notThisAreaList = new ArrayList<Map>();
		Map<String, Map> alreadyNotThisArea = new HashMap<String, Map>();
		if (needSumNotThisArea && mapList != null && !mapList.isEmpty()) {
			List<Map> newList = new ArrayList<Map>();

//			List<String> allCodes=bladeRegionService.getAllAreaCode();
//			Map<String,Boolean>alreadyMap=new HashMap<String,Boolean>();
			int i = 0;
			for (Map map : mapList) {
				String area_code = getStr(map, "area_code");
				i++;
//				logger.info("准备调用service判断非本区划,area_code:"+area_code+",第:"+i+"个,一共:"+mapList.size()+"个 ");
//				if(StringUtils.isBlank(area_code) || !area_code.startsWith(cityCode)) {
				if (isNotThisArea(map)) {
					// 合并到本区化

					margeNotThisArea(notThisAreaList, map, alreadyNotThisArea, "area_code", "area_name", new String[] { "patent_type", "year" },
							new String[] { "total_cnt", "higher_school", "research_institute", "enterprise", "institutions", "individual" });
				} else {
					newList.add(map);// 不是非本区划
				}
			}
			mapList = newList;
			mapList.addAll(notThisAreaList);
		}

		// key: areaName 20231022 key改成 areaCode
		Map<String, List<ViewStatisticsInfoVo>> areaMap = new LinkedHashMap<String, List<ViewStatisticsInfoVo>>();

		// key:areaName_type
		Map<String, ViewStatisticsInfoVo> areaTypeMap = new HashMap<String, ViewStatisticsInfoVo>();

		// key: type
		Map<String, ViewStatisticsInfoVo> typeTotalMap = new LinkedHashMap<String, ViewStatisticsInfoVo>();

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

		for (Map itemMap : mapList) {

			String areaName = null;

			StringBuffer sb = new StringBuffer();

//			if (itemMap.containsKey("area_name")) {
//				areaName = itemMap.get("area_name").toString();
//			} else {
//				areaName = "null";
//			}
//			20231011 改为 area_code，为了按指定顺序排序
			if (itemMap.containsKey("area_code")) {
				areaName = itemMap.get("area_code").toString();
			} else {
				areaName = "null";
			}
			
		

			sb.append(areaName);
			sb.append("_");

			String patentType = null;
			if (itemMap.containsKey("patent_type")) {
				patentType = itemMap.get("patent_type").toString();
			} else {
				patentType = "null";
			}
			sb.append(patentType);

			String alreadyKey = sb.toString();

			ViewStatisticsInfoVo vo = null;
			if (areaTypeMap.containsKey(alreadyKey)) {
				vo = areaTypeMap.get(alreadyKey);
			} else {
				vo = new ViewStatisticsInfoVo();
				// list.add(vo);
				vo.setAreaCode(getStr(itemMap, "area_code"));
				vo.setPatentType(patentType);
				vo.setYearData(new LinkedHashMap<String, ViewStatisticsInfoVo.Item>());
				areaTypeMap.put(alreadyKey, vo);

				if (!areaMap.containsKey(areaName)) {
					List<ViewStatisticsInfoVo> areaList = new ArrayList<ViewStatisticsInfoVo>();
					areaMap.put(areaName, areaList);
					areaList.add(vo);
				} else {
					areaMap.get(areaName).add(vo);
				}

			}

			String year = getStr(itemMap, "year");
			yearSet.add(year);
			ViewStatisticsInfoVo.Item item = new ViewStatisticsInfoVo.Item();
			item.setYear(year);
			item.setAreaCode(getStr(itemMap, "area_code"));
			item.setTotalCnt(getLong(itemMap, "total_cnt"));
			item.setHigherSchool(getLong(itemMap, "higher_school"));
			item.setResearchInstitute(getLong(itemMap, "research_institute"));
			item.setEnterprise(getLong(itemMap, "enterprise"));
			item.setInstitutions(getLong(itemMap, "institutions"));
			item.setIndividual(getLong(itemMap, "individual"));

			vo.getYearData().put(year, item);
			// 总计
			ViewStatisticsInfoVo typeTotal = null;
			if (typeTotalMap.containsKey(patentType)) {
				typeTotal = typeTotalMap.get(patentType);
			} else {
				typeTotal = new ViewStatisticsInfoVo();
				typeTotal.setAreaCode(getStr(itemMap, "area_code"));
//				if(areaName==null || "null".equals(areaName.trim())  || "".equals(areaName.trim()) ) {
				typeTotal.setPatentType(patentType);
				typeTotal.setYearData(new LinkedHashMap<String, ViewStatisticsInfoVo.Item>());
//				}

				typeTotalMap.put(patentType, typeTotal);
			}
			Item totItem = null;
			Item curr = item;
			if (typeTotal.getYearData().containsKey(year)) {
				totItem = typeTotal.getYearData().get(year);

				totItem.setTotalCnt(totItem.getTotalCnt() + curr.getTotalCnt());
				totItem.setHigherSchool(totItem.getHigherSchool() + curr.getHigherSchool());
				totItem.setResearchInstitute(totItem.getResearchInstitute() + curr.getResearchInstitute());
				totItem.setEnterprise(totItem.getEnterprise() + curr.getEnterprise());
				totItem.setInstitutions(totItem.getInstitutions() + curr.getInstitutions());
				totItem.setIndividual(totItem.getIndividual() + curr.getIndividual());

			} else {
				totItem = new Item();
				totItem.setYear(year);
				typeTotal.getYearData().put(year, totItem);
				totItem.setAreaCode(curr.getAreaCode());
				totItem.setTotalCnt(curr.getTotalCnt());
				totItem.setHigherSchool(curr.getHigherSchool());
				totItem.setResearchInstitute(curr.getResearchInstitute());
				totItem.setEnterprise(curr.getEnterprise());
				totItem.setInstitutions(curr.getInstitutions());
				totItem.setIndividual(curr.getIndividual());
			}

		}

		// 处理小计
		for (Map.Entry<String, List<ViewStatisticsInfoVo>> ent : areaMap.entrySet()) {
			List<ViewStatisticsInfoVo> areaList = ent.getValue();

			ViewStatisticsInfoVo total = new ViewStatisticsInfoVo();

			String areaName = ent.getKey();
			BladeRegion br = BladeRegionUtils.selectByCode(ent.getKey());
			total.setAreaCode(ent.getKey());
			if (br != null) {
				areaName = br.getDistrictShortName();
			}
			total.setAreaName(areaName);
			total.setPatentType("总计");

			total.setYearData(new LinkedHashMap<String, Item>());
			for (ViewStatisticsInfoVo vo : areaList) {
				Map<String, Item> yearData = vo.getYearData();

				for (Map.Entry<String, Item> itemEnt : yearData.entrySet()) {
					String year = itemEnt.getKey();
					if (total.getYearData().containsKey(itemEnt.getKey())) {
						// 累加
						Item item = total.getYearData().get(itemEnt.getKey());
						Item curr = itemEnt.getValue();

						item.setTotalCnt(item.getTotalCnt() + curr.getTotalCnt());
						item.setHigherSchool(item.getHigherSchool() + curr.getHigherSchool());
						item.setResearchInstitute(item.getResearchInstitute() + curr.getResearchInstitute());
						item.setEnterprise(item.getEnterprise() + curr.getEnterprise());
						item.setInstitutions(item.getInstitutions() + curr.getInstitutions());
						item.setIndividual(item.getIndividual() + curr.getIndividual());

						total.getYearData().put(year, item);

					} else {

						Item item = new Item();

						item.setYear(year);
						Item curr = itemEnt.getValue();
						item.setAreaCode(curr.getAreaCode());
						item.setTotalCnt(curr.getTotalCnt());
						item.setHigherSchool(curr.getHigherSchool());
						item.setResearchInstitute(curr.getResearchInstitute());
						item.setEnterprise(curr.getEnterprise());
						item.setInstitutions(curr.getInstitutions());
						item.setIndividual(curr.getIndividual());

						total.getYearData().put(year, item);

					}
				}

			}

			areaList.add(0, total);
		}

		// 结果列表
		List<ViewStatisticsInfoVo> list = new ArrayList<ViewStatisticsInfoVo>();

		// 总的统计
		ViewStatisticsInfoVo allTypeList = new ViewStatisticsInfoVo();

		allTypeList.setYearData(new LinkedHashMap<String, ViewStatisticsInfoVo.Item>());
		if (rzlViewStatisticsInfo.getCityCode() == null || "null".equals(rzlViewStatisticsInfo.getCityCode().trim())
				|| "".equals(rzlViewStatisticsInfo.getCityCode().trim())) {
			allTypeList.setAreaName("云南省");
		} else {
//			allTypeList.setAreaName(rzlViewStatisticsInfo.getCityName());
			BladeRegion bladeRegion = BladeRegionUtils.selectByCode(rzlViewStatisticsInfo.getCityCode());
			if (bladeRegion != null) {
//				allTypeList.setAreaName(bladeRegion.getName());
				allTypeList.setAreaName(bladeRegion.getDistrictShortName());
			}

		}
		allTypeList.setPatentType("总计");

		// 处理统计
		for (Map.Entry<String, ViewStatisticsInfoVo> ent : typeTotalMap.entrySet()) {
			ViewStatisticsInfoVo vo = ent.getValue();

			list.add(vo);

			Map<String, Item> yearData = vo.getYearData();

			for (Map.Entry<String, Item> itemEnt : yearData.entrySet()) {
				String year = itemEnt.getKey();
				if (allTypeList.getYearData().containsKey(itemEnt.getKey())) {
					// 累加
					Item item = allTypeList.getYearData().get(itemEnt.getKey());
					Item curr = itemEnt.getValue();

					item.setTotalCnt(item.getTotalCnt() + curr.getTotalCnt());
					item.setHigherSchool(item.getHigherSchool() + curr.getHigherSchool());
					item.setResearchInstitute(item.getResearchInstitute() + curr.getResearchInstitute());
					item.setEnterprise(item.getEnterprise() + curr.getEnterprise());
					item.setInstitutions(item.getInstitutions() + curr.getInstitutions());
					item.setIndividual(item.getIndividual() + curr.getIndividual());

					allTypeList.getYearData().put(year, item);

				} else {

					Item item = new Item();

					item.setYear(year);
					Item curr = itemEnt.getValue();
					item.setAreaCode(curr.getAreaCode());
					item.setTotalCnt(curr.getTotalCnt());
					item.setHigherSchool(curr.getHigherSchool());
					item.setResearchInstitute(curr.getResearchInstitute());
					item.setEnterprise(curr.getEnterprise());
					item.setInstitutions(curr.getInstitutions());
					item.setIndividual(curr.getIndividual());

					allTypeList.getYearData().put(year, item);

				}
			}

		}

		// 按专利类型排序
		Collections.sort(list, new ViewStatisticsInfoVo());
		
		

		// 20231011 需要按指定顺序排序
		List<SortedData>sdList=new ArrayList<SortedData>();
		// 处理非本区划
		List<ViewStatisticsInfoVo> notThis = null;
		for (Map.Entry<String, List<ViewStatisticsInfoVo>> ent : areaMap.entrySet()) {
			String areaCode = ent.getKey();
			List<ViewStatisticsInfoVo> valList = ent.getValue();
			if ("非本区划".equals(areaCode)) {
				notThis = valList;
			} else {
				
				int sort = Integer.MAX_VALUE;// 默认排最后
				String areaName=areaCode;
				BladeRegion br = BladeRegionUtils.selectByCode(areaCode);
				if (br != null) {
					sort = br.getSort();
					areaName=br.getDistrictShortName();
				}
				SortedData sd = new SortedData(sort, areaName, valList);
				sdList.add(sd);
//				list1.add(valList);
			}
		}
		
		//排 序
		Collections.sort(sdList);

		
		List<List<ViewStatisticsInfoVo>> list1 = new ArrayList<List<ViewStatisticsInfoVo>>();
//		Collections.sort(list1, new Comparator<List<ViewStatisticsInfoVo>>() {
//			@Override
//			public int compare(List<ViewStatisticsInfoVo> o1, List<ViewStatisticsInfoVo> o2) {
//				long t1 = getTotal(o1);
//				long t2 = getTotal(o2);
//				return (int) (t2 - t1);
//			}
//		});
		
		for(SortedData sd:sdList) {
			list1.add(sd.dataList);
		}

		if (notThis != null) {
			list1.add(notThis);
		}

		Collections.sort(list, new ViewStatisticsInfoVo());
		for (List<ViewStatisticsInfoVo> ent : list1) {
			List<ViewStatisticsInfoVo> list2 = ent;
			Collections.sort(list2, new ViewStatisticsInfoVo());
			list.addAll(list2);
		}
		if (!list.isEmpty()) {
			list.add(0, allTypeList);
		}

		

		List<String> yearList = new ArrayList<String>();
		// 确保返回列表中所有年份都有数据
		for (String year : yearSet) {
			for (ViewStatisticsInfoVo vo : list) {
				if (!vo.getYearData().containsKey(year)) {
					Item item = new Item();
					item.setYear(year);
					vo.getYearData().put(year, item);
				}
			}
			yearList.add(year);
		}
		Collections.sort(yearList, new Comparator<String>() {
			@Override
			public int compare(String o1, String o2) {
				// TODO Auto-generated method stub
				if (o1 == null && o2 == null) {
					return 0;
				} else {
					if (o1 == null) {
						return -1;
					} else if (o2 == null) {
						return 1;
					} else {
						return o1.compareTo(o2);
					}
				}
			}
		});
		rep.setDataList(list);
		rep.setYearList(yearList);

		return rep;
	}

	private static class SortedData implements Comparable<SortedData> {
		private String areaName;
		private List<ViewStatisticsInfoVo> dataList;
		private int sort;

		private SortedData(int sort, String areaName, List<ViewStatisticsInfoVo> dataList) {
			this.areaName = areaName;
			this.dataList = dataList;
			this.sort=sort;
		}

		@Override
		public int compareTo(SortedData o) {
			return sort - o.sort;
		}
	}

	private long getTotal(List<ViewStatisticsInfoVo> list) {
		long total = 0;
		if (list == null || list.isEmpty()) {
			total = 0;
		} else {
			for (ViewStatisticsInfoVo vo : list) {
				if (vo.getYearData() == null || vo.getYearData().isEmpty()) {
					continue;
				}
				for (Item item : vo.getYearData().values()) {
					if (item.getTotalCnt() == null) {
						continue;
					}
					total = total + item.getTotalCnt();
				}
			}
		}
		return total;
	}

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

	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 0L;
		}
		return Long.parseLong(map.get(key).toString());
	}

	@Override
	public List<String> selectYearList(int year) {
		List<Map> mapList = rzlViewStatisticsInfoMapper.selectYearList(year);

		Set<String> yearSet = new HashSet<String>();
		int lastyear = Integer.parseInt(new SimpleDateFormat("yyyy").format(new Date()));
		for (int i = 0; i < 3; i++) {
			yearSet.add(lastyear + "");
			lastyear--;
		}

		List<String> list = new ArrayList<String>();
		if (mapList != null && !mapList.isEmpty()) {

			for (Map map : mapList) {
				if (map != null && map.get("year") != null) {
//					list.add(map.get("year").toString());
					yearSet.add(map.get("year").toString());
				}
			}
		} else {
//			list.add(year + "");
		}

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

		return list;
	}

	/**
	 * 查询专利有效-概况统计列表
	 *
	 * @param rzlViewStatisticsInfo 专利有效-概况统计
	 * @return 专利有效-概况统计集合
	 * @date 2023-03-26
	 */
	@Override
	public List<RzlViewStatisticsInfo> queryList(RzlViewStatisticsInfo rzlViewStatisticsInfo) {
		QueryWrapper<RzlViewStatisticsInfo> queryWrapper = Wrappers.query();
		if (StringUtils.isNotEmpty(rzlViewStatisticsInfo.getCityCode())) {
			queryWrapper.eq("city_code", rzlViewStatisticsInfo.getCityCode());
		}
		if (StringUtils.isNotEmpty(rzlViewStatisticsInfo.getCityName())) {
			queryWrapper.like("city_name", rzlViewStatisticsInfo.getCityName());
		}
		if (StringUtils.isNotEmpty(rzlViewStatisticsInfo.getYear())) {
			queryWrapper.eq("year", rzlViewStatisticsInfo.getYear());
		}
		if (StringUtils.isNotEmpty(rzlViewStatisticsInfo.getMonth())) {
			queryWrapper.eq("month", rzlViewStatisticsInfo.getMonth());
		}
		if (StringUtils.isNotEmpty(rzlViewStatisticsInfo.getPatentType())) {
			queryWrapper.eq("patent_type", rzlViewStatisticsInfo.getPatentType());
		}
		queryWrapper.orderByDesc("id");
		return this.list(queryWrapper);
	}

}
