package com.ruoyi.system.service.impl;

import java.lang.reflect.Method;
import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.SnowFlakeUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.UrValidShowData;
import com.ruoyi.system.domain.UrYearValidData;
import com.ruoyi.system.domain.UrYearValidDataDis;
import com.ruoyi.system.domain.vo.UrPatentValidStaticVo;
import com.ruoyi.system.domain.vo.UrPatentValidVo;
import com.ruoyi.system.domain.vo.UrYearValidDataDisVo;
import com.ruoyi.system.domain.vo.UrYearValidDataVo;
import com.ruoyi.system.service.IUrValidShowDataService;
import com.ruoyi.system.service.IUrYearValidDataDisService;
import com.ruoyi.system.service.IUrYearValidDataService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.UrPatentValidMapper;
import com.ruoyi.system.domain.UrPatentValid;
import com.ruoyi.system.service.IUrPatentValidService;

/**
 * 有效专利Service业务层处理
 * 
 * @author dm
 * @date 2023-02-10
 */
@Service
@Transactional
public class UrPatentValidServiceImpl implements IUrPatentValidService 
{
    @Autowired
    private UrPatentValidMapper urPatentValidMapper;
    @Autowired
    private IUrYearValidDataService iUrYearValidDataService;
    @Autowired
    private IUrYearValidDataDisService iUrYearValidDataDisService;
    @Autowired
    private IUrValidShowDataService iUrValidShowDataService;

    /**
     * 查询有效专利列表(总入口)
     *
     * @param urPatentValid 有效专利
     * @return 有效专利集合
     */
    public List<UrPatentValidStaticVo> selectList(UrPatentValidVo urPatentValid) {
        if(null != urPatentValid && StringUtils.isNotBlank(urPatentValid.getCityName())) {
            String cityName = urPatentValid.getCityName();
            if("53".equals(cityName)) {
                return selectUrPatentValidList(urPatentValid);
            }else {
                return selectUrPatentValidDisList(urPatentValid);
            }
        }else {
            return null;
        }
    }

    /**
     * 查询有效专利列表
     * 
     * @param urPatentValid 有效专利
     * @return 有效专利
     */
    @Override
    public List<UrPatentValidStaticVo> selectUrPatentValidList(UrPatentValidVo urPatentValid)
    {
        String year = null;
        List<UrPatentValidStaticVo> resultList = new ArrayList<>();
        try{
            if(StringUtils.isNotBlank(urPatentValid.getYear())) {
                year = urPatentValid.getYear();

                // 获取查询左侧表头列表
                List<UrYearValidData> leftList = iUrYearValidDataService.selectUrLeftList();
                for(int i = 0; i < leftList.size(); i++) {
                    UrYearValidDataVo vo = BeanUtil.toBean(leftList.get(i), UrYearValidDataVo.class);
                    int startYear = Integer.parseInt(year) - 9;
                    vo.setStartDate(String.valueOf(startYear));
                    vo.setEndDate(year);
                    // 判断是云南省还是各市数据
                    if(leftList.get(i).getCityShortName().equals("云南省")) {
                        /** 插入结果表 */
                        UrValidShowData data = new UrValidShowData();
                        data.setCode(urPatentValid.getCityName());
                        data.setYear(year);

                        vo.setCityShortName("");
                        List<UrYearValidData> lineList = iUrYearValidDataService.selectUrYearValidDataProvinceList(vo);
                        // 拼装结果集
                        UrPatentValidStaticVo staticVo = new UrPatentValidStaticVo();
                        List<String> staticList = new ArrayList<>();
                        staticList.add(i % 4 == 0 ? leftList.get(i).getCityShortName() : "");
                        data.setRegionName(i % 4 == 0 ? leftList.get(i).getCityShortName() : "");
                        staticList.add(leftList.get(i).getSubject().equals("all") ? "总计" : leftList.get(i).getSubject());
                        data.setType(leftList.get(i).getSubject().equals("all") ? "总计" : leftList.get(i).getSubject());

                        Class clazz = UrValidShowData.class;
                        for(int j = 0; j < lineList.size(); j++) {
                            staticList.add(lineList.get(j).getCount().toString());

                            Method method = clazz.getMethod("setV" + (j+1), String.class);
                            Object invoke = method.invoke(data, lineList.get(j).getCount().toString());
                        }
                        staticVo.setDatas(staticList);
                        resultList.add(staticVo);
                        //iUrValidShowDataService.insertUrValidShowData(data);
                    }else {
                        /** 插入结果表 */
                        UrValidShowData data = new UrValidShowData();
                        data.setCode(urPatentValid.getCityName());
                        data.setYear(year);

                        List<UrYearValidData> lineList = iUrYearValidDataService.selectUrYearValidDataList(vo);
                        // 拼装结果集
                        UrPatentValidStaticVo staticVo = new UrPatentValidStaticVo();
                        List<String> staticList = new ArrayList<>();
                        staticList.add(i % 4 == 0 ? leftList.get(i).getCityShortName() : "");
                        data.setRegionName(i % 4 == 0 ? leftList.get(i).getCityShortName() : "");
                        staticList.add(leftList.get(i).getSubject().equals("all") ? "总计" : leftList.get(i).getSubject());
                        data.setType(leftList.get(i).getSubject().equals("all") ? "总计" : leftList.get(i).getSubject());

                        Class clazz = UrValidShowData.class;
                        for(int j = 0; j < lineList.size(); j++) {
                            staticList.add(lineList.get(j).getCount().toString());

                            Method method = clazz.getMethod("setV" + (j+1), String.class);
                            Object invoke = method.invoke(data, lineList.get(j).getCount().toString());
                        }
                        staticVo.setDatas(staticList);
                        resultList.add(staticVo);
                        //iUrValidShowDataService.insertUrValidShowData(data);
                    }
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return resultList;
    }

    /**
     * 查询有效专利(区县)列表
     *
     * @param urPatentValid 有效专利
     * @return 有效专利集合
     */
    public List<UrPatentValidStaticVo> selectUrPatentValidDisList(UrPatentValidVo urPatentValid) {
        String year = null;
        List<UrPatentValidStaticVo> resultList = new ArrayList<>();
        try{
            if(StringUtils.isNotBlank(urPatentValid.getYear())) {
                year = urPatentValid.getYear();
                int startYear = Integer.parseInt(year) - 9;

                // 拼接市数据
                String cityName = urPatentValid.getCityName();
                String[] subjectArray = {"all", "发明", "实用新型", "外观设计"};
                for(String subject : subjectArray) {
                    /** 插入结果表 */
                    UrValidShowData data = new UrValidShowData();
                    data.setCode(urPatentValid.getCityName());
                    data.setYear(year);

                    UrYearValidDataDisVo vo = new UrYearValidDataDisVo();
                    vo.setStartDate(String.valueOf(startYear));
                    vo.setEndDate(year);
                    vo.setSubject(subject);
                    vo.setCode(cityName);

                    List<UrYearValidDataDis> lineList = iUrYearValidDataDisService.selectUrYearValidDataDisCityList(vo);
                    // 拼装结果集
                    UrPatentValidStaticVo staticVo = new UrPatentValidStaticVo();
                    List<String> staticList = new ArrayList<>();
                    staticList.add(subject.equals("all") ? this.getCityName(cityName) : "");
                    staticList.add(subject.equals("all") ? "总计" : subject);
                    data.setRegionName(subject.equals("all") ? this.getCityName(cityName) : "");
                    data.setType(subject.equals("all") ? "总计" : subject);

                    Class clazz = UrValidShowData.class;
                    for(int j = 0; j < lineList.size(); j++) {
                        staticList.add(lineList.get(j).getCount().toString());

                        Method method = clazz.getMethod("setV" + (j+1), String.class);
                        Object invoke = method.invoke(data, lineList.get(j).getCount().toString());
                    }
                    staticVo.setDatas(staticList);
                    resultList.add(staticVo);
                    //iUrValidShowDataService.insertUrValidShowData(data);
                }

                // 获取查询左侧表头列表,拼接区县数据
                List<UrYearValidDataDis> leftList = iUrYearValidDataDisService.selectUrLeftList(urPatentValid.getCityName());
                for(int i = 0; i < leftList.size(); i++) {
                    /** 插入结果表 */
                    UrValidShowData data = new UrValidShowData();
                    data.setCode(urPatentValid.getCityName());
                    data.setYear(year);

                    UrYearValidDataDisVo vo = BeanUtil.toBean(leftList.get(i), UrYearValidDataDisVo.class);
                    vo.setStartDate(String.valueOf(startYear));
                    vo.setEndDate(year);

                    List<UrYearValidDataDis> lineList = iUrYearValidDataDisService.selectUrYearValidDataDisList(vo);
                    // 拼装结果集
                    UrPatentValidStaticVo staticVo = new UrPatentValidStaticVo();
                    List<String> staticList = new ArrayList<>();
                    staticList.add(i % 4 == 0 ? leftList.get(i).getDistrictShortName() : "");
                    staticList.add(leftList.get(i).getSubject().equals("all") ? "总计" : leftList.get(i).getSubject());
                    data.setRegionName(i % 4 == 0 ? leftList.get(i).getDistrictShortName() : "");
                    data.setType(leftList.get(i).getSubject().equals("all") ? "总计" : leftList.get(i).getSubject());

                    Class clazz = UrValidShowData.class;
                    for(int j = 0; j < lineList.size(); j++) {
                        staticList.add(lineList.get(j).getCount().toString());

                        Method method = clazz.getMethod("setV" + (j+1), String.class);
                        Object invoke = method.invoke(data, lineList.get(j).getCount().toString());
                    }
                    staticVo.setDatas(staticList);
                    resultList.add(staticVo);
                    //iUrValidShowDataService.insertUrValidShowData(data);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return resultList;
    }


    /**
     * 获取专利有效-概览统计  动态表头
     * @param   vo  有效专利视图对象
     * @return
     */
    public List<UrPatentValid> selectHeaderList(UrPatentValidVo vo) {
        List<UrPatentValid> resultList = new ArrayList<>();
        String endYear = vo.getEndDate();
        Integer year = Integer.parseInt(endYear);
        for(int i = 9; i >= 0; i--) {
            UrPatentValid entity = new UrPatentValid();
            entity.setPatentName(String.valueOf(year - i));
            resultList.add(entity);
            UrPatentValid entity1 = new UrPatentValid();
            entity1.setPatentName("大专院校");//大专院校,科研机构,企业,机关团体,个人,
            resultList.add(entity1);
            UrPatentValid entity2 = new UrPatentValid();
            entity2.setPatentName("科研机构");
            resultList.add(entity2);
            UrPatentValid entity3 = new UrPatentValid();
            entity3.setPatentName("企业");
            resultList.add(entity3);
            UrPatentValid entity4 = new UrPatentValid();
            entity4.setPatentName("机关团体");
            resultList.add(entity4);
            UrPatentValid entity5 = new UrPatentValid();
            entity5.setPatentName("个人");
            resultList.add(entity5);
        }
        return resultList;
//        return urPatentValidMapper.selectHeaderList(vo);
    }

    @Override
    public List<UrPatentValid> selectGroupDistrictName() {
        return urPatentValidMapper.selectGroupDistrictName();
    }

    @Override
    public void setDistrictName(String districtName) {
        urPatentValidMapper.setDistrictName(districtName);
    }

    /**
     * 根据编码获取城市名称
     * @param code
     * @return
     */
    private String getCityName(String code) {
        Map<String, String> cityMap = new HashMap<>();
        cityMap.put("5301", "昆明市");
        cityMap.put("5303", "曲靖市");
        cityMap.put("5304", "玉溪市");
        cityMap.put("5305", "保山市");
        cityMap.put("5306", "昭通市");
        cityMap.put("5307", "丽江市");
        cityMap.put("5308", "普洱市");
        cityMap.put("5309", "临沧市");
        cityMap.put("5323", "楚雄州");
        cityMap.put("5325", "红河州");
        cityMap.put("5326", "文山州");
        cityMap.put("5328", "西双版纳州");
        cityMap.put("5329", "大理州");
        cityMap.put("5331", "德宏州");
        cityMap.put("5333", "怒江州");
        cityMap.put("5334", "迪庆州");
        return cityMap.get(code);
    }


}
