package com.ruoyi.reportrzs.service.impl;

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

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.reportrzs.domain.RzsApplicantRankingInfo;
import com.ruoyi.reportrzs.mapper.RzsApplicantRankingInfoMapper;
import com.ruoyi.reportrzs.service.IRzsApplicantRankingInfoService;

/**
 * 专利授权-申请人排名Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-27
 */
@Service
public class RzsApplicantRankingInfoServiceImpl extends ServiceImpl<RzsApplicantRankingInfoMapper, RzsApplicantRankingInfo> implements IRzsApplicantRankingInfoService {

	@Autowired
	private RzsApplicantRankingInfoMapper rzsApplicantRankingInfoMapper;
	
	@Override
	public List<RzsApplicantRankingInfo> listByReport(RzsApplicantRankingInfo rzsApplicantRankingInfo) {
		
		boolean retEmpty=false;
		if (StringUtils.isNotBlank(rzsApplicantRankingInfo.getCityCode()) && !"53".equals(rzsApplicantRankingInfo.getCityCode())) {
			String cityCode = rzsApplicantRankingInfo.getCityCode().trim();
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cc=SecurityUtils.needFilterByCityCode();
				if(!cc.equals(cityCode) ) {
					retEmpty=true;
				}
			}
		}else {
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cityCode=SecurityUtils.needFilterByCityCode();
				rzsApplicantRankingInfo.setCityCode(cityCode);
			}
		}
		
		List<RzsApplicantRankingInfo> dataList =new ArrayList<RzsApplicantRankingInfo>();
		if(!retEmpty) {
			dataList = rzsApplicantRankingInfoMapper.selectForRank(rzsApplicantRankingInfo);
		}
		 
//		patentTypes

//		List<RzlApplicantRankingInfo> dataList =queryList(new RzlApplicantRankingInfo());

//		for(RzlApplicantRankingInfo data:dataList) {
//			data.setInvent(data.getInvent()==null?0:data.getInvent());
//			data.setUtilityModel(data.getUtilityModel()==null?0:data.getUtilityModel());
//			data.setAppearanceDesign(data.getAppearanceDesign()==null?0:data.getAppearanceDesign());
//			
//			data.setPatentsTotalNumber(data.getInvent()+data.getUtilityModel()+data.getAppearanceDesign());
//		}

//		Collections.sort(dataList, new Comparator<RzsApplicantRankingInfo>() {
//			@Override
//			public int compare(RzsApplicantRankingInfo o1, RzsApplicantRankingInfo o2) {
//				int n1 = (o1.getPatentsTotalNumber() == null ? 0 : o1.getPatentsTotalNumber().intValue());
//				int n2 = (o2.getPatentsTotalNumber() == null ? 0 : o2.getPatentsTotalNumber().intValue());
//
//				int r = n2 - n1;
//				if (r <= 0) {
//					r = -1;
//				} else if (r > 0) {
//					r = 1;
//				}
//
//				return r;
//			}
//		});

		for (int i = 1; i <= dataList.size(); i++) {
			dataList.get(i - 1).setRanking(i);
		}

		return dataList;

	}
//	@Override
//	public List<RzsApplicantRankingInfo> listByReport(RzsApplicantRankingInfo rzsApplicantRankingInfo) {
//
////		List<RzlApplicantRankingInfo>dataList=queryList(rzlApplicantRankingInfo);
//		
//		List<RzsApplicantRankingInfo>dataList=queryList(new RzsApplicantRankingInfo());
//		
//		for(RzsApplicantRankingInfo data:dataList) {
//			data.setInvent(data.getInvent()==null?0:data.getInvent());
//			data.setUtilityModel(data.getUtilityModel()==null?0:data.getUtilityModel());
//			data.setAppearanceDesign(data.getAppearanceDesign()==null?0:data.getAppearanceDesign());
//			
//			data.setPatentsTotalNumber(data.getInvent()+data.getUtilityModel()+data.getAppearanceDesign());
//		}
//		
//		Collections.sort(dataList, new Comparator<RzsApplicantRankingInfo>() {
//			@Override
//			public int compare(RzsApplicantRankingInfo o1, RzsApplicantRankingInfo o2) {
//				return o2.getPatentsTotalNumber()-o1.getPatentsTotalNumber();
//			}
//		});
//		
//		for(int i=1;i<=dataList.size();i++) {
//			dataList.get(i-1).setRanking(i);
//		}
//		
//		return dataList;
//		
//	
//	}
    /**
     * 查询专利授权-申请人排名列表
     *
     * @param rzsApplicantRankingInfo 专利授权-申请人排名
     * @return 专利授权-申请人排名集合
     * @date 2023-03-27
     */
    @Override
    public List<RzsApplicantRankingInfo> queryList(RzsApplicantRankingInfo rzsApplicantRankingInfo) {
        QueryWrapper<RzsApplicantRankingInfo> queryWrapper = Wrappers.query();
                if (  StringUtils.isNotNull(rzsApplicantRankingInfo.getCityCode())){
                    queryWrapper.eq("city_code", rzsApplicantRankingInfo.getCityCode());
                }
                if ( StringUtils.isNotEmpty(rzsApplicantRankingInfo.getCityName())  ){
                    queryWrapper.like("city_name", rzsApplicantRankingInfo.getCityName());
                }
                if ( StringUtils.isNotEmpty(rzsApplicantRankingInfo.getYear())  ){
                    queryWrapper.eq("year", rzsApplicantRankingInfo.getYear());
                }
                if ( StringUtils.isNotEmpty(rzsApplicantRankingInfo.getMonth())  ){
                    queryWrapper.eq("month", rzsApplicantRankingInfo.getMonth());
                }
                if ( StringUtils.isNotEmpty(rzsApplicantRankingInfo.getPatenteeName())  ){
                    queryWrapper.like("patentee_name", rzsApplicantRankingInfo.getPatenteeName());
                }
//                if ( StringUtils.isNotEmpty(rzsApplicantRankingInfo.getInvent())  ){
//                    queryWrapper.eq("invent", rzsApplicantRankingInfo.getInvent());
//                }
//                if ( StringUtils.isNotEmpty(rzsApplicantRankingInfo.getUtilityModel())  ){
//                    queryWrapper.eq("utility_model", rzsApplicantRankingInfo.getUtilityModel());
//                }
//                if ( StringUtils.isNotEmpty(rzsApplicantRankingInfo.getAppearanceDesign())  ){
//                    queryWrapper.eq("appearance_design", rzsApplicantRankingInfo.getAppearanceDesign());
//                }
                if ( StringUtils.isNotEmpty(rzsApplicantRankingInfo.getApplicantType())  ){
                    queryWrapper.eq("applicant_type", rzsApplicantRankingInfo.getApplicantType());
                }
//                if ( StringUtils.isNotEmpty(rzsApplicantRankingInfo.getPatentsTotalNumber())  ){
//                    queryWrapper.eq("patents_total_number", rzsApplicantRankingInfo.getPatentsTotalNumber());
//                }
                if (  StringUtils.isNotNull(rzsApplicantRankingInfo.getStatus())){
                    queryWrapper.eq("status", rzsApplicantRankingInfo.getStatus());
                }
        queryWrapper.orderByDesc("id");
        return this.list(queryWrapper);
    }

}
