package com.ruoyi.hv.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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.hv.domain.DHeightValuePatentCompare;
import com.ruoyi.hv.domain.DHeightValuePatentDetail;
import com.ruoyi.hv.mapper.DHeightValuePatentDetailMapper;
import com.ruoyi.hv.service.IDHeightValuePatentDetailService;

/**
 * 高价值专利详情Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-02
 */
@Service
public class DHeightValuePatentDetailServiceImpl extends ServiceImpl<DHeightValuePatentDetailMapper, DHeightValuePatentDetail> implements IDHeightValuePatentDetailService {

    @Autowired
    private DHeightValuePatentDetailMapper dHeightValuePatentDetailMapper;

    @Override
    public List<DHeightValuePatentCompare> queryCompare(DHeightValuePatentCompare dHeightValuePatentCompare) {
        // TODO Auto-generated method stub
       
        List<Map> maplist = dHeightValuePatentDetailMapper.queryCompare(dHeightValuePatentCompare);

        //查总数
        int count=maplist.size();
        List<Map> totalMapList = dHeightValuePatentDetailMapper.queryCompareCount(dHeightValuePatentCompare);
        if(totalMapList!=null && !totalMapList.isEmpty()) {
        	Map map=totalMapList.get(0);
        	for(Object obj:map.values()) {
        		if(obj!=null) {
        			count=Integer.parseInt(obj.toString());
        		}
      	  	}
        }
        dHeightValuePatentCompare.setTotal(count);
        
        List<DHeightValuePatentCompare> list = new ArrayList<DHeightValuePatentCompare>();

        for (Map map : maplist) {
            DHeightValuePatentCompare d = new DHeightValuePatentCompare();
//			select ff.*,hv_seis_press+hv_keep10_press+hv_have_overseas_press+hv_high_pledge_press+hv_technology_award_press hv_total from (
//					select appli_province,appli_address,appli
//					,sum(case  when hv_seis_press='1' then 1 else 0 end ) hv_seis_press 
//					,sum(case  when hv_keep10_press='1' then 1 else 0 end ) hv_keep10_press 
//					,sum(case  when hv_have_overseas_press='1' then 1 else 0 end ) hv_have_overseas_press 
//					,sum(case  when hv_high_pledge_press='1' then 1 else 0 end ) hv_high_pledge_press 
//					,sum(case  when hv_technology_award_press='1' then 1 else 0 end ) hv_technology_award_press 
//					from d_height_value_patent_detail
//					) ff 
            d.setAppliProvince(getStr(map, "appli_province"));
            d.setAppliAddress(getStr(map, "appli_address"));
            d.setAppli(getStr(map, "appli"));
            d.setHvSeisPress(getStr(map, "hv_seis_press"));
            d.setHvKeep10Press(getStr(map, "hv_keep10_press"));
            d.setHvHaveOverseasPress(getStr(map, "hv_have_overseas_press"));
            d.setHvHighPledgePress(getStr(map, "hv_high_pledge_press"));
            d.setHvTechnologyAwardPress(getStr(map, "hv_technology_award_press"));
            d.setHvHighCnt(getStr(map, "hv_total"));
            list.add(d);

        }

        return list;
    }

    /**
     * 查询高价值专利详情列表
     *
     * @param dHeightValuePatentDetail 高价值专利详情
     * @return 高价值专利详情集合
     * @date 2023-04-02
     */
    @Override
    public List<DHeightValuePatentDetail> selectList(DHeightValuePatentDetail dHeightValuePatentDetail) {
    	
    	if("云南省".equals(dHeightValuePatentDetail.getModifyAppliCity())) {
    		dHeightValuePatentDetail.setModifyAppliCity(null);
    	}
    	boolean retEmpty=false;
		if (StringUtils.isNotBlank(dHeightValuePatentDetail.getModifyAppliCity()) && !"云南省".equals(dHeightValuePatentDetail.getModifyAppliCity())) {
			String cityCode = dHeightValuePatentDetail.getModifyAppliCity().trim();
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cc=SecurityUtils.needFilterByCityCode();
				if(!cc.equals(cityCode) ) {
					retEmpty=true;
				}
			}
		}else {
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cityCode=SecurityUtils.needFilterByCityCode();
				dHeightValuePatentDetail.setModifyAppliCityCode(cityCode);
			}
		}
		
		
//        dHeightValuePatentDetail.setStatus("10");  //10-有效// 20230712 不过滤有效性
		 List<DHeightValuePatentDetail>list=new  ArrayList<DHeightValuePatentDetail>();
		if(!retEmpty) {
			list=this.queryList(dHeightValuePatentDetail);
		}
       
        for(DHeightValuePatentDetail d:list) {
        	 int i=0;
        	StringBuffer sb=new StringBuffer();
        	if("1".equals(d.getHvSeisPress())) {
        		if(i>0) {
        			sb.append(",");
        		}
        		sb.append("战略性新兴产业");
        		i++;
        	}
        	
        	if("1".equals(d.getHvSeisPress())) {
        		if(i>0) {
        			sb.append(",");
        		}
        		sb.append("维持年限超10年");
        		i++;
        	}
        	if("1".equals(d.getHvSeisPress())) {
        		if(i>0) {
        			sb.append(",");
        		}
        		sb.append("在海外有同族专利");
        		i++;
        	}
        	
        	if("1".equals(d.getHvSeisPress())) {
        		if(i>0) {
        			sb.append(",");
        		}
        		sb.append("存在质押记录");
        		i++;
        	}
        	if("1".equals(d.getHvSeisPress())) {
        		if(i>0) {
        			sb.append(",");
        		}
        		sb.append("获得国家科技奖或者中国专利奖");
        		i++;
        	}
        	d.setHighValueStr(sb.toString());
        }
        
        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());
    }

    /**
     * 查询高价值专利详情列表
     *
     * @param dHeightValuePatentDetail 高价值专利详情
     * @return 高价值专利详情集合
     * @date 2023-04-02
     */
    @Override
    public List<DHeightValuePatentDetail> queryList(DHeightValuePatentDetail dHeightValuePatentDetail) {
        QueryWrapper<DHeightValuePatentDetail> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getPubId())) {
            queryWrapper.eq("pub_id", dHeightValuePatentDetail.getPubId());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getDataType())) {
            queryWrapper.eq("data_type", dHeightValuePatentDetail.getDataType());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getAppNumber())) {
            queryWrapper.eq("app_number", dHeightValuePatentDetail.getAppNumber());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getAppDate())) {
            queryWrapper.eq("app_date", dHeightValuePatentDetail.getAppDate());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getAppli())) {
            queryWrapper.eq("appli", dHeightValuePatentDetail.getAppli());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getAppliAddress())) {
            queryWrapper.eq("appli_address", dHeightValuePatentDetail.getAppliAddress());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getPreCounts())) {
            queryWrapper.eq("pre_counts", dHeightValuePatentDetail.getPreCounts());
        }
//        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getHighValue())) {
//            queryWrapper.like("high_value", dHeightValuePatentDetail.getHighValue());
//        }
//        hv_seis_press,hv_have_overseas_press,hv_keep10_press,hv_high_pledge_press,hv_technology_award_press
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getHighValue())) {
        	if("1".equals(dHeightValuePatentDetail.getHighValue())) {
        		queryWrapper.eq("hv_seis_press", "1");
        	}
        	if("3".equals(dHeightValuePatentDetail.getHighValue())) {
        		queryWrapper.eq("hv_keep10_press", "1");
        	}
        	if("2".equals(dHeightValuePatentDetail.getHighValue())) {
        		queryWrapper.eq("hv_have_overseas_press", "1");
        	}
        	if("4".equals(dHeightValuePatentDetail.getHighValue())) {
        		queryWrapper.eq("hv_high_pledge_press", "1");
        	}
        	if("5".equals(dHeightValuePatentDetail.getHighValue())) {
        		queryWrapper.eq("hv_technology_award_press", "1");
        	}
//            queryWrapper.like("high_value", dHeightValuePatentDetail.getHighValue());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getAppliProvince())) {
            queryWrapper.eq("appli_province", dHeightValuePatentDetail.getAppliProvince());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getAppliCity())) {
            queryWrapper.eq("appli_city", dHeightValuePatentDetail.getAppliCity());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getAppliCounty())) {
            queryWrapper.eq("appli_county", dHeightValuePatentDetail.getAppliCounty());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getModifyAppliProvince())) {
            queryWrapper.eq("modify_appli_province", dHeightValuePatentDetail.getModifyAppliProvince());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getModifyAppliCity())) {
            queryWrapper.eq("modify_appli_city", dHeightValuePatentDetail.getModifyAppliCity());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getModifyAppliCityCode())) {
            queryWrapper.eq("modify_appli_city_code", dHeightValuePatentDetail.getModifyAppliCityCode());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getModifyAppliCounty())) {
            queryWrapper.eq("modify_appli_county", dHeightValuePatentDetail.getModifyAppliCounty());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getStatus())) {
            queryWrapper.eq("status", dHeightValuePatentDetail.getStatus());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getHeightValuePress())) {
            queryWrapper.eq("height_value_press", dHeightValuePatentDetail.getHeightValuePress());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getHvSeisPress())) {
            queryWrapper.eq("hv_seis_press", dHeightValuePatentDetail.getHvSeisPress());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getHvHaveOverseasPress())) {
            queryWrapper.eq("hv_have_overseas_press", dHeightValuePatentDetail.getHvHaveOverseasPress());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getHvKeep10Press())) {
            queryWrapper.eq("hv_keep10_press", dHeightValuePatentDetail.getHvKeep10Press());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getHvHighPledgePress())) {
            queryWrapper.eq("hv_high_pledge_press", dHeightValuePatentDetail.getHvHighPledgePress());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getHvTechnologyAwardPress())) {
            queryWrapper.eq("hv_technology_award_press", dHeightValuePatentDetail.getHvTechnologyAwardPress());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getHeightValueDept())) {
            queryWrapper.eq("height_value_dept", dHeightValuePatentDetail.getHeightValueDept());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getHvSeisDept())) {
            queryWrapper.eq("hv_seis_dept", dHeightValuePatentDetail.getHvSeisDept());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getHvHaveOverseasDept())) {
            queryWrapper.eq("hv_have_overseas_dept", dHeightValuePatentDetail.getHvHaveOverseasDept());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getHvKeep10Dept())) {
            queryWrapper.eq("hv_keep10_dept", dHeightValuePatentDetail.getHvKeep10Dept());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getHvHighPledgeDept())) {
            queryWrapper.eq("hv_high_pledge_dept", dHeightValuePatentDetail.getHvHighPledgeDept());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getHvTechnologyAwardDept())) {
            queryWrapper.eq("hv_technology_award_dept", dHeightValuePatentDetail.getHvTechnologyAwardDept());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getLicCounts())) {
            queryWrapper.eq("lic_counts", dHeightValuePatentDetail.getLicCounts());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getTitle())) {
            queryWrapper.eq("title", dHeightValuePatentDetail.getTitle());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getIpcMain())) {
            queryWrapper.eq("ipc_main", dHeightValuePatentDetail.getIpcMain());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getPleCounts())) {
            queryWrapper.eq("ple_counts", dHeightValuePatentDetail.getPleCounts());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getAppTranCounts())) {
            queryWrapper.eq("app_tran_counts", dHeightValuePatentDetail.getAppTranCounts());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getLocStr())) {
            queryWrapper.eq("loc_str", dHeightValuePatentDetail.getLocStr());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getCurtCounts())) {
            queryWrapper.eq("curt_counts", dHeightValuePatentDetail.getCurtCounts());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getNationalEcoStr())) {
            queryWrapper.eq("national_eco_str", dHeightValuePatentDetail.getNationalEcoStr());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getAgencyName())) {
            queryWrapper.like("agency_name", dHeightValuePatentDetail.getAgencyName());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getPubNumber())) {
            queryWrapper.eq("pub_number", dHeightValuePatentDetail.getPubNumber());
        }
//        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getPatTranCounts())) {
//            queryWrapper.eq("pat_tran_counts", dHeightValuePatentDetail.getPatTranCounts());
//        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getPatentType())) {
            queryWrapper.eq("patent_type", dHeightValuePatentDetail.getPatentType());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getAppliType())) {
            queryWrapper.eq("appli_type", dHeightValuePatentDetail.getAppliType());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getYearBatch())) {
            queryWrapper.eq("year_batch",dHeightValuePatentDetail.getYearBatch());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getBeginDate())){
            queryWrapper.ge("app_date",dHeightValuePatentDetail.getBeginDate());
        }
        if (StringUtils.isNotEmpty(dHeightValuePatentDetail.getEndDate())){
            queryWrapper.le("app_date",dHeightValuePatentDetail.getEndDate());
        }
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }


}
