package com.ruoyi.reportpub.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
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.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.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.report.vo.ViewStatisticsInfoRep;
import com.ruoyi.report.vo.ViewStatisticsInfoVo;
import com.ruoyi.report.vo.ViewStatisticsInfoVo.Item;
import com.ruoyi.reportpub.domain.RzgViewStatisticsInfo;
import com.ruoyi.reportpub.mapper.RzgViewStatisticsInfoMapper;
import com.ruoyi.reportpub.service.IRzgViewStatisticsInfoService;
import com.ruoyi.system.domain.BladeRegion;
import com.ruoyi.system.service.IBladeRegionService;
import com.ruoyi.utils.BladeRegionUtils;

/**
 * 专利公开-概况统计Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-27
 */
@Service
public class RzgViewStatisticsInfoServiceImpl extends ServiceImpl<RzgViewStatisticsInfoMapper, RzgViewStatisticsInfo> implements IRzgViewStatisticsInfoService {
	
	@Autowired
	private IBladeRegionService bladeRegionService;
	@Autowired
	private RzgViewStatisticsInfoMapper rzgViewStatisticsInfoMapper;
	
	
	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(RzgViewStatisticsInfo rzlViewStatisticsInfo) {
        // TODO Auto-generated method stub

//		List<RzlViewStatisticsInfoVo> typeTotalList = new ArrayList<RzlViewStatisticsInfoVo>();
    	
        ViewStatisticsInfoRep rep = new ViewStatisticsInfoRep();
        String cityCode = null;
        if (StringUtils.isNotBlank(rzlViewStatisticsInfo.getCityCode()) && !"53".equals(rzlViewStatisticsInfo.getCityCode())) {
            cityCode = rzlViewStatisticsInfo.getCityCode().trim();
        }
        rzlViewStatisticsInfo.setCityCode(cityCode);
        
        
        boolean retEmpty=false;
		if (StringUtils.isNotBlank(rzlViewStatisticsInfo.getCityCode()) && !"53".equals(rzlViewStatisticsInfo.getCityCode())) {
			cityCode = rzlViewStatisticsInfo.getCityCode().trim();
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cc=SecurityUtils.needFilterByCityCode();
				if(!cc.equals(cityCode) ) {
					retEmpty=true;
				}
			}
		}else {
			if(SecurityUtils.isNeedFilterByCityCode()) {
				cityCode=SecurityUtils.needFilterByCityCode();
				rzlViewStatisticsInfo.setCityCode(cityCode);
			}
		}
        
        
        String qyear = rzlViewStatisticsInfo.getYear();
        if (StringUtils.isBlank(qyear)) {
//            qyear = new SimpleDateFormat("yyyy").format(new Date());
        	throw new RuntimeException("请指定统计年份");
        }
        
        String month=rzlViewStatisticsInfo.getMonth();
        if (StringUtils.isBlank(month)) {
      	throw new RuntimeException("请指定统计月份");
      }
        
        if (StringUtils.isNotBlank(qyear)) {
            String minYear = (Integer.parseInt(qyear) - 9) + "";
            rzlViewStatisticsInfo.setMinYear(minYear);
            rzlViewStatisticsInfo.setMaxYear(qyear);
        }
        List<Map> mapList = new ArrayList<Map>();
       if(!retEmpty) {
    	   mapList = rzgViewStatisticsInfoMapper.listByReport(rzlViewStatisticsInfo);
       }
        

        boolean needSumNotThisArea=false;
		if(StringUtils.isNotBlank(cityCode)) {
			needSumNotThisArea=true;
		}
		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>();
//			Map<String,Boolean>alreadyMap=new HashMap<String,Boolean>();
//			List<String> allCodes=bladeRegionService.getAllAreaCode();
			
			BladeRegionUtils.setNotThisArea(mapList, cityCode);

			
			for(Map map:mapList) {
				String area_code=getStr(map, "area_code");
//				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, 20231013 改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";
//            }
            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.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.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();

//				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.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();
//            total.setAreaName(ent.getKey());
            
            String areaName= BladeRegionUtils.getDistrictShortName(ent.getKey());
            if(StringUtils.isNotBlank(areaName)) {
            	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.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.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.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());
      		
        List<List<ViewStatisticsInfoVo>> list1 = new ArrayList<List<ViewStatisticsInfoVo>>();
        
        
        List<ViewStatisticsInfoVo> notThis=new ArrayList<ViewStatisticsInfoVo>();
       List<String>codes= BladeRegionUtils.sortedKey(areaMap.keySet());
        
//        for(Map.Entry<String, List<ViewStatisticsInfoVo>>ent:areaMap.entrySet()) {
       for(String code:codes) {
    	   
        	if("非本区划".equals(code)) {
        		notThis.addAll(areaMap.get(code));
        	}else {
        		
        		list1.add(areaMap.get(code));
        	}
        }
        
//        if(areaMap.containsKey("非本区划")) {
//        	for(Map.Entry<String, List<ViewStatisticsInfoVo>>ent:areaMap.entrySet()) {
//        		if("非本区划".equals(ent.getKey())) {
//        			notThis.addAll(ent.getValue());
//        		}else {
//        			list1.add(ent.getValue());
//        		}
//        	}
//        }else {
//        	list1.addAll(areaMap.values());
//        }
        
        
        
//		// 排序
//        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);
//            }
//        });
        
        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 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;
    }
    
//	@Override
//	public List<ViewStatisticsInfoVo> listByReport(RzgViewStatisticsInfo rzlViewStatisticsInfo) {
//
//		// TODO Auto-generated method stub
//
////		List<RzlViewStatisticsInfoVo> typeTotalList = new ArrayList<RzlViewStatisticsInfoVo>();
//
//		
//		String cityCode=null;
//		if(StringUtils.isNotBlank(rzlViewStatisticsInfo.getCityCode()) && ! "53".equals(rzlViewStatisticsInfo.getCityCode())) {
//			cityCode=rzlViewStatisticsInfo.getCityCode().trim();
//		}
//		
//		List<Map> mapList = rzgViewStatisticsInfoMapper.listByReport(cityCode);
//
//		// key: areaName
//		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";
//			}
//
//			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.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.setTotalCnt(getInt(itemMap, "total_cnt"));
//			item.setHigherSchool(getInt(itemMap, "higher_school"));
//			item.setResearchInstitute(getInt(itemMap, "research_institute"));
//			item.setEnterprise(getInt(itemMap, "enterprise"));
//			item.setInstitutions(getInt(itemMap, "institutions"));
//			item.setIndividual(getInt(itemMap, "individual"));
//
//			vo.getYearData().put(year, item);
//			// 总计
//			ViewStatisticsInfoVo typeTotal = null;
//			if (typeTotalMap.containsKey(patentType)) {
//				typeTotal = typeTotalMap.get(patentType);
//			} else {
//				typeTotal = new ViewStatisticsInfoVo();
//				
////				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.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();
//			total.setAreaName(ent.getKey());
//			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.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();
//		list.add(allTypeList);
//		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.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.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);
//
//				}
//			}
//
//		}
//	
//
//		for (Map.Entry<String, List<ViewStatisticsInfoVo>> ent : areaMap.entrySet()) {
//			list.addAll(ent.getValue());
//		}
//		
//		//确保返回列表中所有年份都有数据
//		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);
//				}
//			}
//		}
//
//		return list;
//	
//	}
	
	
	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 0L;
		}
		return Long.parseLong(map.get(key).toString());
	}
	
    /**
     * 查询专利公开-概况统计列表
     *
     * @param rzgViewStatisticsInfo 专利公开-概况统计
     * @return 专利公开-概况统计集合
     * @date 2023-03-27
     */
    @Override
    public List<RzgViewStatisticsInfo> queryList(RzgViewStatisticsInfo rzgViewStatisticsInfo) {
        QueryWrapper<RzgViewStatisticsInfo> queryWrapper = Wrappers.query();
                if (  StringUtils.isNotNull(rzgViewStatisticsInfo.getCityCode())){
                    queryWrapper.eq("city_code", rzgViewStatisticsInfo.getCityCode());
                }
                if ( StringUtils.isNotEmpty(rzgViewStatisticsInfo.getCityName())  ){
                    queryWrapper.like("city_name", rzgViewStatisticsInfo.getCityName());
                }
                if ( StringUtils.isNotEmpty(rzgViewStatisticsInfo.getYear())  ){
                    queryWrapper.eq("year", rzgViewStatisticsInfo.getYear());
                }
                if ( StringUtils.isNotEmpty(rzgViewStatisticsInfo.getMonth())  ){
                    queryWrapper.eq("month", rzgViewStatisticsInfo.getMonth());
                }
                if ( StringUtils.isNotEmpty(rzgViewStatisticsInfo.getPatentType())  ){
                    queryWrapper.eq("patent_type", rzgViewStatisticsInfo.getPatentType());
                }
//                if ( StringUtils.isNotEmpty(rzgViewStatisticsInfo.getHigherSchool())  ){
//                    queryWrapper.eq("higher_school", rzgViewStatisticsInfo.getHigherSchool());
//                }
//                if ( StringUtils.isNotEmpty(rzgViewStatisticsInfo.getResearchInstitute())  ){
//                    queryWrapper.eq("research_institute", rzgViewStatisticsInfo.getResearchInstitute());
//                }
//                if ( StringUtils.isNotEmpty(rzgViewStatisticsInfo.getEnterprise())  ){
//                    queryWrapper.eq("enterprise", rzgViewStatisticsInfo.getEnterprise());
//                }
//                if ( StringUtils.isNotEmpty(rzgViewStatisticsInfo.getInstitutions())  ){
//                    queryWrapper.eq("institutions", rzgViewStatisticsInfo.getInstitutions());
//                }
//                if ( StringUtils.isNotEmpty(rzgViewStatisticsInfo.getIndividual())  ){
//                    queryWrapper.eq("individual", rzgViewStatisticsInfo.getIndividual());
//                }
                if (  StringUtils.isNotNull(rzgViewStatisticsInfo.getStatus())){
                    queryWrapper.eq("status", rzgViewStatisticsInfo.getStatus());
                }
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }


	@Override
	public List<String> selectYearList(int year) {
        List<Map> mapList = rzgViewStatisticsInfoMapper.selectYearList(year);
        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());
                }
            }
        } else {
            list.add(year + "");
        }
        return list;
    }

	
}
