package com.ruoyi.reportpub.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
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.reportpub.domain.RzgApplicantRankingInfo;
import com.ruoyi.reportpub.mapper.RzgApplicantRankingInfoMapper;
import com.ruoyi.reportpub.service.IRzgApplicantRankingInfoService;

/**
 * 专利公开-申请人排名Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-27
 */
@Service
public class RzgApplicantRankingInfoServiceImpl extends ServiceImpl<RzgApplicantRankingInfoMapper, RzgApplicantRankingInfo>
		implements IRzgApplicantRankingInfoService {

	@Autowired
	private RzgApplicantRankingInfoMapper rzgApplicantRankingInfoMapper;

	@Override
	public List<RzgApplicantRankingInfo> listByReport(RzgApplicantRankingInfo rzgApplicantRankingInfo) {
		
		if("53".equals(rzgApplicantRankingInfo.getCityCode())) {
			rzgApplicantRankingInfo.setCityCode(null);
		}
		boolean retEmpty = false;
		if (StringUtils.isNotBlank(rzgApplicantRankingInfo.getCityCode()) && !"53".equals(rzgApplicantRankingInfo.getCityCode())) {
			String cityCode = rzgApplicantRankingInfo.getCityCode().trim();
			if (SecurityUtils.isNeedFilterByCityCode()) {
				String cc = SecurityUtils.needFilterByCityCode();
				if (!cc.equals(cityCode)) {
					retEmpty = true;
				}
			}
		} else {
			if (SecurityUtils.isNeedFilterByCityCode()) {
				String cityCode = SecurityUtils.needFilterByCityCode();
				rzgApplicantRankingInfo.setCityCode(cityCode);
			}
		}
		
		List<RzgApplicantRankingInfo> dataList =new ArrayList<RzgApplicantRankingInfo>();
		if(!retEmpty) {
			dataList = rzgApplicantRankingInfoMapper.selectForRank(rzgApplicantRankingInfo);
		}
		
//		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<RzgApplicantRankingInfo>() {
			@Override
			public int compare(RzgApplicantRankingInfo o1, RzgApplicantRankingInfo 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<RzgApplicantRankingInfo> listByReport(RzgApplicantRankingInfo rzgApplicantRankingInfo) {
////		List<RzlApplicantRankingInfo>dataList=queryList(rzlApplicantRankingInfo);
//		
//		List<RzgApplicantRankingInfo>dataList=queryList(new RzgApplicantRankingInfo());
//		
//		for(RzgApplicantRankingInfo 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<RzgApplicantRankingInfo>() {
//			@Override
//			public int compare(RzgApplicantRankingInfo o1, RzgApplicantRankingInfo o2) {
//				return o2.getPatentsTotalNumber()-o1.getPatentsTotalNumber();
//			}
//		});
//		
//		for(int i=1;i<=dataList.size();i++) {
//			dataList.get(i-1).setRanking(i);
//		}
//		
//		return dataList;
//		
//	}

	/**
	 * 查询专利公开-申请人排名列表
	 *
	 * @param rzgApplicantRankingInfo 专利公开-申请人排名
	 * @return 专利公开-申请人排名集合
	 * @date 2023-03-27
	 */
	@Override
	public List<RzgApplicantRankingInfo> queryList(RzgApplicantRankingInfo rzgApplicantRankingInfo) {
		QueryWrapper<RzgApplicantRankingInfo> queryWrapper = Wrappers.query();
		if (StringUtils.isNotNull(rzgApplicantRankingInfo.getCityCode())) {
			queryWrapper.eq("city_code", rzgApplicantRankingInfo.getCityCode());
		}
		if (StringUtils.isNotEmpty(rzgApplicantRankingInfo.getCityName())) {
			queryWrapper.like("city_name", rzgApplicantRankingInfo.getCityName());
		}
		if (StringUtils.isNotEmpty(rzgApplicantRankingInfo.getYear())) {
			queryWrapper.eq("year", rzgApplicantRankingInfo.getYear());
		}
		if (StringUtils.isNotEmpty(rzgApplicantRankingInfo.getMonth())) {
			queryWrapper.eq("month", rzgApplicantRankingInfo.getMonth());
		}
		if (StringUtils.isNotEmpty(rzgApplicantRankingInfo.getPatenteeName())) {
			queryWrapper.like("patentee_name", rzgApplicantRankingInfo.getPatenteeName());
		}
//                if ( StringUtils.isNotEmpty(rzgApplicantRankingInfo.getPatentsTotalNumber())  ){
//                    queryWrapper.eq("patents_total_number", rzgApplicantRankingInfo.getPatentsTotalNumber());
//                }
		if (StringUtils.isNotEmpty(rzgApplicantRankingInfo.getApplicantType())) {
			queryWrapper.eq("applicant_type", rzgApplicantRankingInfo.getApplicantType());
		}
//                if ( StringUtils.isNotEmpty(rzgApplicantRankingInfo.getInvent())  ){
//                    queryWrapper.eq("invent", rzgApplicantRankingInfo.getInvent());
//                }
//                if ( StringUtils.isNotEmpty(rzgApplicantRankingInfo.getUtilityModel())  ){
//                    queryWrapper.eq("utility_model", rzgApplicantRankingInfo.getUtilityModel());
//                }
//                if ( StringUtils.isNotEmpty(rzgApplicantRankingInfo.getAppearanceDesign())  ){
//                    queryWrapper.eq("appearance_design", rzgApplicantRankingInfo.getAppearanceDesign());
//                }
		if (StringUtils.isNotNull(rzgApplicantRankingInfo.getStatus())) {
			queryWrapper.eq("status", rzgApplicantRankingInfo.getStatus());
		}
		queryWrapper.orderByDesc("id");
		return this.list(queryWrapper);
	}
}
