package com.ruoyi.reportpub.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.reportpub.domain.DPatentPubInfo;
import com.ruoyi.reportpub.mapper.DPatentPubInfoMapper;
import com.ruoyi.reportpub.service.IDPatentPubInfoService;
import com.ruoyi.system.domain.DFixAddressArea;
import com.ruoyi.system.service.IDFixAddressAreaService;

/**
 * 专利公开数据Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-31
 */
@Service
public class DPatentPubInfoServiceImpl extends ServiceImpl<DPatentPubInfoMapper, DPatentPubInfo>
		implements IDPatentPubInfoService {

	@Autowired
	private RedisCache redisCode;
	@Autowired
	private DPatentPubInfoMapper dPatentPubInfoMapper;
	@Autowired
	private IDFixAddressAreaService dFixAddressAreaService;
	
	private static Map<String,DPatentPubInfo>DPatentPubInfo_MAP=new HashMap<String,DPatentPubInfo>();

	private static final Logger logger = LoggerFactory.getLogger(DPatentPubInfoServiceImpl.class);

	public void clearData() {
		dPatentPubInfoMapper.clearData();
	}


	public String getKeyByAddress(String address) {
		String key = "DPatentPubInfo:queryByAddress:" + address.trim();
		return key;
	}
	public String getKeyByCownerAddress(String address) {
		String key = "DPatentPubInfo:queryByCownerAddress:" + address.trim();
		return key;
	}
	
	public String getKeyByPid(String pid) {
		String key = "DPatentPubInfo:Pid:" + pid.trim();
		return key;
	}
	
	public String getKeyByAppNumber(String appNumber2) {
		String key = "DPatentPubInfo:AppNumber:" + appNumber2.trim();
		return key;
	}

	public List<DPatentPubInfo> queryByAddress(String address) {

		if (StringUtils.isBlank(address)) {
			return null;
		}

		String key = getKeyByAddress(address);

		List<DPatentPubInfo> c = redisCode.getCacheObject(key);
		if (c != null && !c.isEmpty()) {
			return c;
		}

		DPatentPubInfo qo = new DPatentPubInfo();
		qo.setModifyAppliAddress(address);
		List<DPatentPubInfo> list = queryList(qo);
		if (list != null && !list.isEmpty()) {
//			DPatentPubInfo data = list.get(0);
			redisCode.setCacheObject(key, list);
			return list;
		}
		return null;

	}
	
	
	public List<DPatentPubInfo> queryByCownerAddress(String address) {

		if (StringUtils.isBlank(address)) {
			return null;
		}

		String key = getKeyByCownerAddress(address);

		List<DPatentPubInfo> c = redisCode.getCacheObject(key);
		if (c != null && !c.isEmpty()) {
			return c;
		}

		DPatentPubInfo qo = new DPatentPubInfo();
		qo.setModifyCownerAddress(address);
		List<DPatentPubInfo> list = queryList(qo);
		if (list != null && !list.isEmpty()) {
			redisCode.setCacheObject(key, list);
			return list;
		}
		return null;

	}

	public DPatentPubInfo queryByPidFromCache(String pubId) {
		String key = getKeyByPid(pubId);
		return DPatentPubInfo_MAP.get(key);
	}
	
	
	public DPatentPubInfo queryByPid(String pubId,boolean useCache) {
		if (StringUtils.isBlank(pubId)) {
			return null;
		}

		String key = getKeyByPid(pubId);
		
		DPatentPubInfo c = null;
		if(useCache) {
			c = redisCode.getCacheObject(key);
		}
		if (c != null) {
			return c;
		}

		DPatentPubInfo qo = new DPatentPubInfo();
		qo.setPubId(pubId);
		List<DPatentPubInfo> list = queryList(qo);
		if (list != null && !list.isEmpty()) {
			DPatentPubInfo data = list.get(0);
			if(useCache && data!=null) {
				redisCode.setCacheObject(key, data);
			}
			return data;
		}
		return null;

	}
	
	
	public DPatentPubInfo queryByAppNumber(String appNumber2,boolean useCache) {
		if (StringUtils.isBlank(appNumber2)) {
			return null;
		}

		String key = getKeyByAppNumber(appNumber2);
		
		DPatentPubInfo c = null;
		if(useCache) {
			c = redisCode.getCacheObject(key);
		}
		if (c != null) {
			return c;
		}

		DPatentPubInfo qo = new DPatentPubInfo();
		qo.setAppNumber2(appNumber2);
		List<DPatentPubInfo> list = queryList(qo);
		if (list != null && !list.isEmpty()) {
			DPatentPubInfo data = list.get(0);
			if(useCache && data!=null) {
				redisCode.setCacheObject(key, data);
			}
			return data;
		}
		return null;

	}
	
	
	 
	public void loadToCacheWidthPid() {
		logger.debug("开始加载公开数据缓存,准备查询");
		List<DPatentPubInfo> list = queryList(new DPatentPubInfo());
		int count = list.size();
		logger.debug("查询完成，一共" + count + "条");
		for (int i = 0; i < list.size(); i++) {
			logger.debug("准备将第[" + i + "]条数据库载缓存，一共[" + count + "]条");
			String key = getKeyByPid(list.get(i).getPubId());
//			redisCode.setCacheObject(key, list.get(i));
			DPatentPubInfo_MAP.put(key, list.get(i));
		}
	}

//	public void loadToCache() {
//		logger.debug("开始加载公开数据缓存,准备查询");
//		List<DPatentPubInfo> list = queryList(new DPatentPubInfo());
//		int count = list.size();
//		logger.debug("查询完成，一共" + count + "条");
//		for (int i = 0; i < list.size(); i++) {
////            String appNumber2 = list.get(i).getAppNumber2();
//			logger.debug("准备将第[" + i + "]条数据库载缓存，一共[" + count + "]条");
////            if (StringUtils.isBlank(appNumber2)) {
////                appNumber2 = list.get(i).getAppNumber();
////                if (appNumber2 == null) {
////                    continue;
////                }
////                if (appNumber2.startsWith("CN")) {
////                    appNumber2 = appNumber2.replace("CN", "");
////                }
////
////                if (appNumber2.contains(".")) {
////                    appNumber2 = appNumber2.replace(".", "");
////                }
//			DPatentPubInfo pub = new DPatentPubInfo();
//			pub.setId(list.get(i).getId());
////         pub.setAppNumber2(appNumber2);
//
//			String locStr = list.get(i).getLocStr();
//			String locStr2 = toModifyLocStr(locStr);
//			if (StringUtils.isNotBlank(locStr2)) {
//				pub.setModifyLocStr(locStr2);
//				String locStr2Desc = LocStr.ALL.get(locStr2);
//				if (StringUtils.isNotBlank(locStr2Desc)) {
//					pub.setModifyLocStrDesc(locStr2Desc);
//				}
////				dPatentPubInfoMapper.updateById(pub);
//			}
//
//			String appNumber2 = list.get(i).getAppNumber2();
//
////                list.get(i).setAppNumber2(appNumber2);
//
//			String key = getKeyByAppNumber(appNumber2);
//			redisCode.setCacheObject(key, list.get(i));
//		}
//
//	}

//	public static void main(String[] args) {
//		String locStr = "14-0;3;14-04";
//
//		System.out.println(toModifyLocStr(locStr));
//	}

	private static String toModifyLocStr(String locStr) {
		if (StringUtils.isBlank(locStr)) {
			return null;
		}
		String locStr2 = locStr.trim();
		if (locStr.contains(";")) {
//    		14-03;14-04
			locStr2 = locStr2.substring(0, locStr2.indexOf(";"));
		}
		if (locStr2.contains("-")) {
			locStr2 = locStr2.replace("-", "");
		}
		
		String locStr3=null;
		if (locStr2.length() <= 3) {
			locStr3= locStr2.substring(0, 1);
		} else {
			locStr3= locStr2.substring(0, 2);
		}
		
		return Integer.parseInt(locStr3)+"";

	}

	/**
	 * 查询专利公开数据列表
	 *
	 * @param dPatentPubInfo 专利公开数据
	 * @return 专利公开数据集合
	 * @date 2023-03-31
	 */
	@Override
	public List<DPatentPubInfo> queryList(DPatentPubInfo dPatentPubInfo) {
		QueryWrapper<DPatentPubInfo> queryWrapper = Wrappers.query();
		if (StringUtils.isNotEmpty(dPatentPubInfo.getPubId())) {
			queryWrapper.eq("pub_id", dPatentPubInfo.getPubId());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getAppNumber())) {
			queryWrapper.eq("app_number", dPatentPubInfo.getAppNumber());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getAppDate())) {
			queryWrapper.eq("app_date", dPatentPubInfo.getAppDate());
		}
		
		
		if (StringUtils.isNotEmpty(dPatentPubInfo.getModifyAppliAddress())) {
			queryWrapper.eq("modify_appli_address", dPatentPubInfo.getModifyAppliAddress());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getAppli())) {
			queryWrapper.eq("appli", dPatentPubInfo.getAppli());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getAppliType())) {
			queryWrapper.eq("appli_type", dPatentPubInfo.getAppliType());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getModifyAppliType())) {
			queryWrapper.eq("modify_appli_type", dPatentPubInfo.getModifyAppliType());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getAppliAddress())) {
			queryWrapper.eq("appli_address", dPatentPubInfo.getAppliAddress());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getAppliProvinceCode())) {
			queryWrapper.eq("appli_province_code", dPatentPubInfo.getAppliProvinceCode());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getAppliProvince())) {
			queryWrapper.eq("appli_province", dPatentPubInfo.getAppliProvince());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getAppliCityCode())) {
			queryWrapper.eq("appli_city_code", dPatentPubInfo.getAppliCityCode());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getAppliCity())) {
			queryWrapper.eq("appli_city", dPatentPubInfo.getAppliCity());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getAppliCountyCode())) {
			queryWrapper.eq("appli_county_code", dPatentPubInfo.getAppliCountyCode());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getAppliCounty())) {
			queryWrapper.eq("appli_county", dPatentPubInfo.getAppliCounty());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getModifyAppliProvinceCode())) {
			queryWrapper.eq("modify_appli_province_code", dPatentPubInfo.getModifyAppliProvinceCode());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getModifyAppliProvince())) {
			queryWrapper.eq("modify_appli_province", dPatentPubInfo.getModifyAppliProvince());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getModifyAppliCityCode())) {
			queryWrapper.eq("modify_appli_city_code", dPatentPubInfo.getModifyAppliCityCode());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getModifyAppliCity())) {
			queryWrapper.eq("modify_appli_city", dPatentPubInfo.getModifyAppliCity());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getModifyAppliCountyCode())) {
			queryWrapper.eq("modify_appli_county_code", dPatentPubInfo.getModifyAppliCountyCode());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getModifyAppliCounty())) {
			queryWrapper.eq("modify_appli_county", dPatentPubInfo.getModifyAppliCounty());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getHighValue())) {
			queryWrapper.eq("high_value", dPatentPubInfo.getHighValue());
		}
		if (StringUtils.isNotNull(dPatentPubInfo.getAddressAdjustmentTime())) {
			queryWrapper.eq("address_adjustment_time", dPatentPubInfo.getAddressAdjustmentTime());
		}
		if (StringUtils.isNotNull(dPatentPubInfo.getApplyAdjustmentTime())) {
			queryWrapper.eq("apply_adjustment_time", dPatentPubInfo.getApplyAdjustmentTime());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getTitle())) {
			queryWrapper.eq("title", dPatentPubInfo.getTitle());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getPubNumber())) {
			queryWrapper.eq("pub_number", dPatentPubInfo.getPubNumber());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getPatentStatus())) {
			queryWrapper.eq("patent_status", dPatentPubInfo.getPatentStatus());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getPatentHolderPub())) {
			queryWrapper.eq("patent_holder_pub", dPatentPubInfo.getPatentHolderPub());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getPatentType())) {
			queryWrapper.eq("patent_type", dPatentPubInfo.getPatentType());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getIpcMain())) {
			queryWrapper.eq("ipc_main", dPatentPubInfo.getIpcMain());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getLocStr())) {
			queryWrapper.eq("loc_str", dPatentPubInfo.getLocStr());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getNationalEcoStr())) {
			queryWrapper.eq("national_eco_str", dPatentPubInfo.getNationalEcoStr());
		}
		// if (StringUtils.isNotNull(dPatentPubInfo.getPatTranCounts())) {
		// queryWrapper.eq("pat_tran_counts", dPatentPubInfo.getPatTranCounts());
		// }
		// if (StringUtils.isNotNull(dPatentPubInfo.getAppTranCounts())) {
		// queryWrapper.eq("app_tran_counts", dPatentPubInfo.getAppTranCounts());
		// }
		if (StringUtils.isNotNull(dPatentPubInfo.getLicCounts())) {
			queryWrapper.eq("lic_counts", dPatentPubInfo.getLicCounts());
		}
		// if (StringUtils.isNotNull(dPatentPubInfo.getPleCounts())) {
		// queryWrapper.eq("ple_counts", dPatentPubInfo.getPleCounts());
		// }
		if (StringUtils.isNotNull(dPatentPubInfo.getPreCounts())) {
			queryWrapper.eq("pre_counts", dPatentPubInfo.getPreCounts());
		}
		// if (StringUtils.isNotNull(dPatentPubInfo.getCurtCounts())) {
		// queryWrapper.eq("curt_counts", dPatentPubInfo.getCurtCounts());
		// }
		if (StringUtils.isNotEmpty(dPatentPubInfo.getAgencyName())) {
			queryWrapper.like("agency_name", dPatentPubInfo.getAgencyName());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getStatus())) {
			queryWrapper.eq("status", dPatentPubInfo.getStatus());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getAppNumber2())) {
			queryWrapper.eq("app_number2", dPatentPubInfo.getAppNumber2());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getPubDate())) {
			queryWrapper.eq("pub_date", dPatentPubInfo.getPubDate());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getPriNumber())) {
			queryWrapper.eq("pri_number", dPatentPubInfo.getPriNumber());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getPriDate())) {
			queryWrapper.eq("pri_date", dPatentPubInfo.getPriDate());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getYunnanModifyAppliProvince())) {
			queryWrapper.eq("yunnan_modify_appli_province", dPatentPubInfo.getYunnanModifyAppliProvince());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getYunnanModifyAppliCity())) {
			queryWrapper.eq("yunnan_modify_appli_city", dPatentPubInfo.getYunnanModifyAppliCity());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getYunnanModifyAppliCounty())) {
			queryWrapper.eq("yunnan_modify_appli_county", dPatentPubInfo.getYunnanModifyAppliCounty());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getTimeCreate())) {
			queryWrapper.eq("time_create", dPatentPubInfo.getTimeCreate());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getTimeUpdate())) {
			queryWrapper.eq("time_update", dPatentPubInfo.getTimeUpdate());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getCurrentOwners())) {
			queryWrapper.eq("current_owners", dPatentPubInfo.getCurrentOwners());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getCownerAddress())) {
			queryWrapper.eq("cowner_address", dPatentPubInfo.getCownerAddress());
		}
		if (StringUtils.isNotEmpty(dPatentPubInfo.getModifyCownerAddress())) {
			queryWrapper.eq("modify_cowner_address", dPatentPubInfo.getModifyCownerAddress());
		}
		
		if (StringUtils.isNotNull(dPatentPubInfo.getHvSeisDept())) {
			queryWrapper.eq("hv_seis_dept", dPatentPubInfo.getHvSeisDept());
		}
		if (StringUtils.isNotNull(dPatentPubInfo.getHvHaveOverseasDept())) {
			queryWrapper.eq("hv_have_overseas_dept", dPatentPubInfo.getHvHaveOverseasDept());
		}
		if (StringUtils.isNotNull(dPatentPubInfo.getHvKeep10Dept())) {
			queryWrapper.eq("hv_keep10_dept", dPatentPubInfo.getHvKeep10Dept());
		}
		if (StringUtils.isNotNull(dPatentPubInfo.getHvHighPledgeDept())) {
			queryWrapper.eq("hv_high_pledge_dept", dPatentPubInfo.getHvHighPledgeDept());
		}
		if (StringUtils.isNotNull(dPatentPubInfo.getHvTechnologyAwardDept())) {
			queryWrapper.eq("hv_technology_award_dept", dPatentPubInfo.getHvTechnologyAwardDept());
		}
//		queryWrapper.orderByDesc("status","patent_type","pub_date","id");
		queryWrapper.orderByDesc("time_update","pub_date","id");
		
		return this.list(queryWrapper);

	}

	@Override
	public DFixAddressArea matchByAddress(String address, String cityName) {
		return dFixAddressAreaService.matchByAddress(address, cityName);
	}
}
