package com.ruoyi.report.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.constant.ApplicantType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.report.domain.RzlApplicantRankingInfo;
import com.ruoyi.report.mapper.RzlApplicantRankingInfoMapper;
import com.ruoyi.report.service.IRzlApplicantRankingInfoService;
import com.ruoyi.report.vo.IpcStatisticsInfoVo;

/**
 * 专利有效-申请人排名Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-26
 */
@Service
public class RzlApplicantRankingInfoServiceImpl extends
		ServiceImpl<RzlApplicantRankingInfoMapper, RzlApplicantRankingInfo> implements IRzlApplicantRankingInfoService {

	@Autowired
	private RzlApplicantRankingInfoMapper rzlApplicantRankingInfoMapper;

	@Override
	public List<RzlApplicantRankingInfo> listByReport(RzlApplicantRankingInfo rzlApplicantRankingInfo) {
		
		String cityCode=null;
		if (StringUtils.isNotBlank(rzlApplicantRankingInfo.getCityCode()) && !"53".equals(rzlApplicantRankingInfo.getCityCode())) {
			cityCode = rzlApplicantRankingInfo.getCityCode().trim();
			if(SecurityUtils.isNeedFilterByCityCode()) {
				String cc=SecurityUtils.needFilterByCityCode();
				if(!cc.equals(cityCode)) {
					return new ArrayList<RzlApplicantRankingInfo>();
				}
			}
		}else {
			if(SecurityUtils.isNeedFilterByCityCode()) {
				cityCode=SecurityUtils.needFilterByCityCode();
			}
		}
		rzlApplicantRankingInfo.setCityCode(cityCode);
		
		
		List<RzlApplicantRankingInfo> dataList = rzlApplicantRankingInfoMapper.selectForRank(rzlApplicantRankingInfo);
//		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<RzlApplicantRankingInfo>() {
			@Override
			public int compare(RzlApplicantRankingInfo o1, RzlApplicantRankingInfo 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;

	}

	/**
	 * 查询专利有效-申请人排名列表
	 *
	 * @param rzlApplicantRankingInfo 专利有效-申请人排名
	 * @return 专利有效-申请人排名集合
	 * @date 2023-03-26
	 */
	@Override
	public List<RzlApplicantRankingInfo> queryList(RzlApplicantRankingInfo rzlApplicantRankingInfo) {
		QueryWrapper<RzlApplicantRankingInfo> queryWrapper = Wrappers.query();
		if (StringUtils.isNotEmpty(rzlApplicantRankingInfo.getPatenteeName())) {
			queryWrapper.like("patentee_name", rzlApplicantRankingInfo.getPatenteeName());
		}
		if (StringUtils.isNotBlank(rzlApplicantRankingInfo.getApplicantTypes())) {
			String[] types = rzlApplicantRankingInfo.getApplicantTypes().split(",");
			List<String> typeList = new ArrayList<String>();
			for (int i = 0; i < types.length; i++) {
				if (StringUtils.isNotBlank(types[i])) {
					ApplicantType byCode = ApplicantType.valueOf(types[i]);
					if (byCode != null) {
						typeList.add(byCode.getTypeName());
					}
				}
			}
			if (null != typeList && typeList.size() > 0) {
				queryWrapper.in("applicant_type", typeList);
			}
		}
//                if(StringUtils.isNotBlank(rzlApplicantRankingInfo.getPatentTypes())) {
//                    String[]types=rzlApplicantRankingInfo.getPatentTypes().split(",");
//                    List<String>typeList=new ArrayList<String>();
//                    for(String tp:types) {
//                        if(StringUtils.isNotBlank(tp) ) {
//                            PatentType patentType=PatentType.valueOf(tp.trim());
//                            if(patentType!=null) {
//                                typeList.add(patentType.getTypeName());
//                            }
//
//                        }
//                    }
//                    queryWrapper.in("patent_type", typeList);
//                }

		if (StringUtils.isNotNull(rzlApplicantRankingInfo.getCityCode())) {
			queryWrapper.eq("city_code", rzlApplicantRankingInfo.getCityCode());
		}
		if (StringUtils.isNotEmpty(rzlApplicantRankingInfo.getCityName())) {
			queryWrapper.like("city_name", rzlApplicantRankingInfo.getCityName());
		}
		if (StringUtils.isNotEmpty(rzlApplicantRankingInfo.getYear())) {
			queryWrapper.eq("year", rzlApplicantRankingInfo.getYear());
		}
		if (StringUtils.isNotEmpty(rzlApplicantRankingInfo.getMonth())) {
			queryWrapper.eq("month", rzlApplicantRankingInfo.getMonth());
		}
//                if ( StringUtils.isNotEmpty(rzlApplicantRankingInfo.getApplicantType())  ){
//                    queryWrapper.eq("applicant_type", rzlApplicantRankingInfo.getApplicantType());
//                }
//                if ( StringUtils.isNotEmpty(rzlApplicantRankingInfo.getInvent())  ){
//                    queryWrapper.eq("invent", rzlApplicantRankingInfo.getInvent());
//                }
//                if ( StringUtils.isNotEmpty(rzlApplicantRankingInfo.getUtilityModel())  ){
//                    queryWrapper.eq("utility_model", rzlApplicantRankingInfo.getUtilityModel());
//                }
//                if ( StringUtils.isNotEmpty(rzlApplicantRankingInfo.getAppearanceDesign())  ){
//                    queryWrapper.eq("appearance_design", rzlApplicantRankingInfo.getAppearanceDesign());
//                }
//                if ( StringUtils.isNotEmpty(rzlApplicantRankingInfo.getPatentType())  ){
//                    queryWrapper.eq("patent_type", rzlApplicantRankingInfo.getPatentType());
//                }
//                if ( StringUtils.isNotEmpty(rzlApplicantRankingInfo.getPatentsTotalNumber())  ){
//                    queryWrapper.eq("patents_total_number", rzlApplicantRankingInfo.getPatentsTotalNumber());
//                }
		if (StringUtils.isNotNull(rzlApplicantRankingInfo.getStatus())) {
			queryWrapper.eq("status", rzlApplicantRankingInfo.getStatus());
		}
		if (StringUtils.isNotEmpty(rzlApplicantRankingInfo.getPubDate())) {
			queryWrapper.eq("pub_date", rzlApplicantRankingInfo.getPubDate());
		}
		if (StringUtils.isNotEmpty(rzlApplicantRankingInfo.getCreateTimeBegin())) {
			queryWrapper.ge("pub_date", rzlApplicantRankingInfo.getCreateTimeBegin());
		}
		if (StringUtils.isNotEmpty(rzlApplicantRankingInfo.getCreateTimeEnd())) {
			queryWrapper.le("pub_date", rzlApplicantRankingInfo.getCreateTimeEnd());
		}

		queryWrapper.orderByDesc("patents_total_number");
		return this.list(queryWrapper);
	}
}
