package com.ruoyi.adjust.service.impl;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.adjust.domain.DPatentDataCollationInfo;
import com.ruoyi.adjust.domain.DPatentDataCollationLog;
import com.ruoyi.adjust.mapper.DPatentDataCollationInfoMapper;
import com.ruoyi.adjust.model.batchUpdatePatentDataQO;
import com.ruoyi.adjust.service.IDPatentDataCollationInfoService;
import com.ruoyi.adjust.service.IDPatentDataCollationLogService;
import com.ruoyi.common.constant.ApplicantType;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.dimp.domain.DPatentAuthInfo;
import com.ruoyi.dimp.domain.DPatentEffectiveInfo;
import com.ruoyi.dimp.service.IDPatentAuthInfoService;
import com.ruoyi.dimp.service.IDPatentEffectiveInfoService;
import com.ruoyi.reportpub.domain.DPatentPubInfo;
import com.ruoyi.reportpub.service.IDPatentPubInfoService;
import com.ruoyi.system.domain.BladeRegion;
import com.ruoyi.system.domain.DFixAddressArea;
import com.ruoyi.system.service.IBladeRegionService;
import com.ruoyi.system.service.IDFixAddressAreaService;
import com.ruoyi.utils.BladeRegionUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 专利数据整理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-26
 */
@Service
@Slf4j
public class DPatentDataCollationInfoServiceImpl
		extends ServiceImpl<DPatentDataCollationInfoMapper, DPatentDataCollationInfo>
		implements IDPatentDataCollationInfoService {
	
	private static final Logger logger=LoggerFactory.getLogger(DPatentDataCollationInfoServiceImpl.class);

	@Autowired
	private DPatentDataCollationInfoMapper dPatentDataCollationInfoMapper;
	@Autowired
	private IDPatentAuthInfoService dPatentAuthInfoService;
	@Autowired
	private IDPatentEffectiveInfoService dPatentEffectiveInfoService;
	@Autowired
	private IDPatentPubInfoService dPatentPubInfoService;
	@Autowired
	private IDPatentDataCollationLogService dPatentDataCollationLogService;
	@Autowired
	private IDFixAddressAreaService dFixAddressAreaService;
	@Autowired
	private IBladeRegionService bladeRegionService;
	@Override
	public List<DPatentDataCollationInfo> listForAdj(DPatentDataCollationInfo dPatentDataCollationInfo) {
		return dPatentDataCollationInfoMapper.listForAdj(dPatentDataCollationInfo);
	}
 
	@Override
	@Transactional
	public void doSync(String type) {
		// TODO Auto-generated method stub
		logger.debug("同步到应用系统-开始同步,type:"+type);

		if ("act".equals(type)) {
			doSyncAct();
		} else if ("auth".equals(type)) {
			doSyncAuth();
		} else if ("pub".equals(type)) {
			doSyncPub();
		}

	}

//	TODO	同步某种类型的报表
	/**
	 * 调整公开报表
	 */
	public void doSyncPub() {
		logger.debug("同步到应用系统-同步公开数据...");
		doSyncPubView();// 概览
		// 分类统计正动更新
		doSyncPubType();
		// 更新ipc类型表
		doSyncPubIpc();
		// 申请人排名
		doSyncPubRank();
	}

	/**
	 * 调整授权报表
	 */
	public void doSyncAuth() {
		logger.debug("同步到应用系统-同步授权数据...");
		doSyncAuthView();// 概览
		// 分类统计正动更新
		doSyncAuthType();
		// 更新ipc类型表
		doSyncAuthIpc();
		// 申请人排名
		doSyncAuthRank();
	}

	/**
	 * 调整有效报表
	 */
	public void doSyncAct() {
		logger.debug("同步到应用系统-同步有效数据...");
		doSyncActView();// 概览

		// 分类统计正动更新
		doSyncActType();
		// 万人发明自动更新

		// 更新ipc类型表
		doSyncActIpc();

		// 申请人排名
		doSyncActRank();
		
	}
	
	

	
	
	
	
	
	public void doSyncHvDetail(String yearBatch,String monthBatch ) {
		logger.debug("同步到应用系统-同步高价明细数据");
		dPatentDataCollationInfoMapper.deleteForSyncHvDetail(yearBatch);
		dPatentDataCollationInfoMapper.insertForSyncHvDetail(yearBatch,monthBatch);
	}
	
	private void doSyncPubView() {
		logger.debug("同步到应用系统-同步公开数据概览数据");
		dPatentDataCollationInfoMapper.deleteForSyncPubView();
		dPatentDataCollationInfoMapper.insertForSyncPubView();
	}
	
	private void doSyncPubType() {
		logger.debug("同步到应用系统-同步公开数据分类统计");
		dPatentDataCollationInfoMapper.deleteForSyncPubType();
		dPatentDataCollationInfoMapper.insertForSyncPubType();
	}

	private void doSyncAuthView() {
		logger.debug("同步到应用系统-同步授权数据概览数据");
		dPatentDataCollationInfoMapper.deleteForSyncAuthView();
		dPatentDataCollationInfoMapper.insertForSyncAuthView();
	}

	private void doSyncPubRank() {
		logger.debug("同步到应用系统-同步公开数据排名数据");
		dPatentDataCollationInfoMapper.deleteForSyncPubRank();
		dPatentDataCollationInfoMapper.insertForSyncPubRank();
	}

	private void doSyncAuthRank() {
		logger.debug("同步到应用系统-同步授权数据排名数据");
		dPatentDataCollationInfoMapper.deleteForSyncAuthRank();
		dPatentDataCollationInfoMapper.insertForSyncAuthRank();
	}

	private void doSyncActRank() {
		logger.debug("同步到应用系统-同步有效数据排名数据");
		dPatentDataCollationInfoMapper.deleteForSyncActRank();
		dPatentDataCollationInfoMapper.insertForSyncActRank();
	}

	private void doSyncActType() {
		logger.debug("同步到应用系统-同步有效数据分类统计");
		dPatentDataCollationInfoMapper.deleteForSyncActType();
		dPatentDataCollationInfoMapper.insertForSyncActType();
	}
	
	private void doSyncActIpc() {
		logger.debug("同步到应用系统-同步有效数据ipc数据");
		dPatentDataCollationInfoMapper.deleteForSyncActIpc();
		dPatentDataCollationInfoMapper.insertForSyncActIpc();
	}

	private void doSyncPubIpc() {
		logger.debug("同步到应用系统-同步公开数据ipc数据");
		dPatentDataCollationInfoMapper.deleteForSyncPubIpc();
		dPatentDataCollationInfoMapper.insertForSyncPubIpc();
	}
	private void doSyncAuthType() {
		logger.debug("同步到应用系统-同步授权数据分类统计");
		dPatentDataCollationInfoMapper.deleteForSyncAuthType();
		dPatentDataCollationInfoMapper.insertForSyncAuthType();
	}
	

	private void doSyncAuthIpc() {
		logger.debug("同步到应用系统-同步授权数据ipc数据");
		dPatentDataCollationInfoMapper.deleteForSyncAuthIpc();
		dPatentDataCollationInfoMapper.insertForSyncAuthIpc();
	}

	/**
	 * 调整有效-概览
	 */
	private void doSyncActView() {
		logger.debug("同步到应用系统-同步有效数据概览。。。");
		dPatentDataCollationInfoMapper.deleteForSyncActView();
		dPatentDataCollationInfoMapper.insertForSyncActView();
	}

	/**
	 * 修改专利数据整理信息
	 *
	 * @param dPatentDataCollationInfo 专利数据整理
	 * @return 结果
	 * @date 2023-03-26
	 */
	@Override
	@Transactional
	public AjaxResult edit(DPatentDataCollationInfo dPatentDataCollationInfo) {
		log.info("修改专利数据整理信息,请求参数为：dPatentDataCollationInfo：" + JSONObject.toJSONString(dPatentDataCollationInfo));
		
		if(StringUtils.isNotBlank(dPatentDataCollationInfo.getModifyAppliProvinceCode()) && dPatentDataCollationInfo.getModifyAppliProvinceCode().trim().length()>2) {
			String provinceCode=dPatentDataCollationInfo.getModifyAppliProvinceCode().trim().substring(0,2);
			dPatentDataCollationInfo.setModifyAppliProvinceCode(provinceCode);
		}
		if(StringUtils.isNotBlank(dPatentDataCollationInfo.getModifyAppliCityCode()) && dPatentDataCollationInfo.getModifyAppliCityCode().trim().length()>4) {
			String provinceCode=dPatentDataCollationInfo.getModifyAppliCityCode().trim().substring(0,4);
			dPatentDataCollationInfo.setModifyAppliCityCode(provinceCode);
		}
		
		//处理一下省市名称
		if(StringUtils.isNotBlank(dPatentDataCollationInfo.getModifyAppliProvinceCode())) {
			BladeRegion bladeRegion = BladeRegionUtils.selectByCode(dPatentDataCollationInfo.getModifyAppliProvinceCode());
			if(bladeRegion!=null) {
				dPatentDataCollationInfo.setModifyAppliProvince(bladeRegion.getName());
			}
		}
		if(StringUtils.isNotBlank(dPatentDataCollationInfo.getModifyAppliCityCode())) {
			BladeRegion bladeRegion = BladeRegionUtils.selectByCode(dPatentDataCollationInfo.getModifyAppliCityCode());
			if(bladeRegion!=null) {
				dPatentDataCollationInfo.setModifyAppliCity(bladeRegion.getName());
			}
		}
		if(StringUtils.isNotBlank(dPatentDataCollationInfo.getModifyAppliCountyCode())) {
			BladeRegion bladeRegion = BladeRegionUtils.selectByCode(dPatentDataCollationInfo.getModifyAppliCountyCode());
			if(bladeRegion!=null) {
				dPatentDataCollationInfo.setModifyAppliCounty(bladeRegion.getName());
			}
		}
		
		DPatentDataCollationLog dPatentDataCollationLog = new DPatentDataCollationLog();
		BeanUtils.copyBeanProp(dPatentDataCollationLog, dPatentDataCollationInfo);
		dPatentDataCollationLog.setAdjustDataId(dPatentDataCollationInfo.getId());
		Date nowDate = DateUtils.getNowDate();
		dPatentDataCollationLog.setSyncStatus("0");
		
		
		
		String sameFlag = "0";

		int resCount = 0;

		// 地址调整
		if ("addr".equals(dPatentDataCollationInfo.getAdjtype())) {
			if (StringUtils.isNull(dPatentDataCollationInfo.getAppliAddress())) {
//				return AjaxResult.error("申请人地址不能为空！");//20230713
				dPatentDataCollationInfo.setSameFalg(false);//申请人地址不能为空时自动忽略同步到所有
			}
			
//			存固定字段
			DFixAddressArea fixArea=new DFixAddressArea();
			fixArea.setMatchMode("1");//精确匹配
			fixArea.setProvinceCode(dPatentDataCollationInfo.getModifyAppliProvinceCode());
			fixArea.setProvinceName(dPatentDataCollationInfo.getModifyAppliProvince());
			fixArea.setCityCode(dPatentDataCollationInfo.getModifyAppliCityCode());
			fixArea.setCityName(dPatentDataCollationInfo.getModifyAppliCity());
			fixArea.setAreaCode(dPatentDataCollationInfo.getModifyAppliCountyCode());
			fixArea.setAreaName(dPatentDataCollationInfo.getModifyAppliCounty());
			if (StringUtils.isNotBlank(dPatentDataCollationInfo.getAppliAddress())) {
				fixArea.setAddress(dPatentDataCollationInfo.getAppliAddress());
			}
			
			dFixAddressAreaService.save(fixArea);
			
			
//			updateEffective.set("modify_province_code", dPatentDataCollationInfo.getModifyAppliProvinceCode()); // 省份代码
//			updateEffective.set("modify_province_name", dPatentDataCollationInfo.getModifyAppliProvince()); // 省份
//			updateEffective.set("modify_city_code", dPatentDataCollationInfo.getModifyAppliCityCode()); // 州市代码
//			updateEffective.set("modify_city_name", dPatentDataCollationInfo.getModifyAppliCity()); // 州市
//			updateEffective.set("modify_area_code", dPatentDataCollationInfo.getModifyAppliCountyCode()); // 区县代码
//			updateEffective.set("modify_area_name", dPatentDataCollationInfo.getModifyAppliCounty()); // 区县
			
			
			// 是否同步更新相同数据
			if (null != dPatentDataCollationInfo.getSameFalg() && dPatentDataCollationInfo.getSameFalg()) {
				sameFlag = "1";
				
				if (StringUtils.isNull(dPatentDataCollationInfo.getAppliAddress())) {
					return AjaxResult.error("申请人地址不能为空！");
				}
				
				// 专利公开-批量更新
				UpdateWrapper<DPatentPubInfo> updatePatent = new UpdateWrapper<>();
//				updatePatent.eq("modify_appli_address", dPatentDataCollationInfo.getAppliAddress());
				updatePatent.eq("modify_cowner_address", dPatentDataCollationInfo.getAppliAddress());
				// 设置要更新的字段及值
				
				updatePatent.set("modify_appli_province_code", dPatentDataCollationInfo.getModifyAppliProvinceCode()); // 省份代码
				updatePatent.set("modify_appli_province", dPatentDataCollationInfo.getModifyAppliProvince()); // 省份
				
				
				updatePatent.set("modify_appli_city_code", dPatentDataCollationInfo.getModifyAppliCityCode()); // 州市代码
				updatePatent.set("modify_appli_city", dPatentDataCollationInfo.getModifyAppliCity()); // 州市
				updatePatent.set("modify_appli_county_code", dPatentDataCollationInfo.getModifyAppliCountyCode()); // 区县代码
				updatePatent.set("modify_appli_county", dPatentDataCollationInfo.getModifyAppliCounty()); // 区县
				updatePatent.set("address_adjustment_time", nowDate);
				updatePatent.set("update_time", nowDate);
				
				//去掉非本区划标识 20230713
				updatePatent.set("not_this_area", "0");
				
				
				// 执行更新操作
//				dPatentPubInfoService.update(null, updatePatent);
				resCount = resCount + dPatentPubInfoService.getBaseMapper().update(null,   updatePatent);
				// 专利有效-批量更新
				UpdateWrapper<DPatentEffectiveInfo> updateEffective = new UpdateWrapper<>();
				// 设置条件，在申请人地址集合中
				updateEffective.eq("appli_address", dPatentDataCollationInfo.getAppliAddress());

				// 设置要更新的字段及值
//                原始值不更新
//                updateEffective.set("province_code", dPatentDataCollationInfo.getModifyAppliProvinceCode());
//                updateEffective.set("province_name", dPatentDataCollationInfo.getModifyAppliProvince());
//                updateEffective.set("city_code", dPatentDataCollationInfo.getModifyAppliCityCode());
//                updateEffective.set("city_name", dPatentDataCollationInfo.getModifyAppliCity());
//                updateEffective.set("area_code", dPatentDataCollationInfo.getModifyAppliCountyCode());
//                updateEffective.set("area_name", dPatentDataCollationInfo.getModifyAppliCounty());

				updateEffective.set("modify_province_code", dPatentDataCollationInfo.getModifyAppliProvinceCode()); // 省份代码
				updateEffective.set("modify_province_name", dPatentDataCollationInfo.getModifyAppliProvince()); // 省份
				updateEffective.set("modify_city_code", dPatentDataCollationInfo.getModifyAppliCityCode()); // 州市代码
				updateEffective.set("modify_city_name", dPatentDataCollationInfo.getModifyAppliCity()); // 州市
				updateEffective.set("modify_area_code", dPatentDataCollationInfo.getModifyAppliCountyCode()); // 区县代码
				updateEffective.set("modify_area_name", dPatentDataCollationInfo.getModifyAppliCounty()); // 区县

				updateEffective.set("address_adjustment_time", nowDate);
				updateEffective.set("update_time", nowDate);
				
				//去掉非本区划标识 20230713
				updateEffective.set("not_this_area", "0");
				
				// 执行更新操作
//				dPatentEffectiveInfoService.update(null, updateEffective);
				resCount = resCount + dPatentEffectiveInfoService.getBaseMapper().update(null, updateEffective);

				// 专利授权-批量更新
				UpdateWrapper<DPatentAuthInfo> updateWrapper = new UpdateWrapper<>();
				// 设置条件，在申请人地址集合中
				updateWrapper.eq("patentee_address", dPatentDataCollationInfo.getAppliAddress());
				// 设置要更新的字段及值
				
				if(StringUtils.isNotBlank(dPatentDataCollationInfo.getModifyAppliProvinceCode()) && dPatentDataCollationInfo.getModifyAppliProvinceCode().trim().length()>2) {
					String provinceCode=dPatentDataCollationInfo.getModifyAppliProvinceCode().trim().substring(0,2);
					dPatentDataCollationInfo.setModifyAppliProvinceCode(provinceCode);
				}
				updateWrapper.set("modify_province_code", dPatentDataCollationInfo.getModifyAppliProvinceCode()); // 省份代码
				updateWrapper.set("modify_province_name", dPatentDataCollationInfo.getModifyAppliProvince()); // 省份
				
				if(StringUtils.isNotBlank(dPatentDataCollationInfo.getModifyAppliCityCode()) && dPatentDataCollationInfo.getModifyAppliCityCode().trim().length()>4) {
					String provinceCode=dPatentDataCollationInfo.getModifyAppliCityCode().trim().substring(0,4);
					dPatentDataCollationInfo.setModifyAppliCityCode(provinceCode);
				}
				updateWrapper.set("modify_city_code", dPatentDataCollationInfo.getModifyAppliCityCode()); // 州市代码
				updateWrapper.set("modify_city_name", dPatentDataCollationInfo.getModifyAppliCity()); // 州市
				updateWrapper.set("modify_area_code", dPatentDataCollationInfo.getModifyAppliCountyCode()); // 区县代码
				updateWrapper.set("modify_area_name", dPatentDataCollationInfo.getModifyAppliCounty());// 区县
				updateWrapper.set("address_adjustment_time", nowDate);
				updateWrapper.set("update_time", nowDate);
				
				//去掉非本区划标识 20230713
				updateWrapper.set("not_this_area", "0");
				
				// 执行更新操作
//				dPatentAuthInfoService.update(null, updateWrapper);
				resCount = resCount + dPatentAuthInfoService.getBaseMapper().update(null, updateWrapper);
				
			} else {
				if (StringUtils.isNull(dPatentDataCollationInfo.getId())) {
					return AjaxResult.error("需要调整的记录的ID不能为空！");
				}
				if ("pub".equals(dPatentDataCollationInfo.getDataType())) { // 数据类型：公开
					// 专利公开数据对象
					DPatentPubInfo patentPubInfo = dPatentPubInfoService.getById(dPatentDataCollationInfo.getId());
					if (null == patentPubInfo) {
						return AjaxResult.error("数据不存在，无法修改！");
					}
					if(StringUtils.isNotBlank(dPatentDataCollationInfo.getModifyAppliProvinceCode()) && dPatentDataCollationInfo.getModifyAppliProvinceCode().trim().length()>2) {
						String provinceCode=dPatentDataCollationInfo.getModifyAppliProvinceCode().trim().substring(0,2);
						patentPubInfo.setModifyAppliProvinceCode(provinceCode);
					}
					patentPubInfo.setModifyAppliProvinceCode(dPatentDataCollationInfo.getModifyAppliProvinceCode()); // 省份代码
					patentPubInfo.setModifyAppliProvince(dPatentDataCollationInfo.getModifyAppliProvince()); // 省份
					
					if(StringUtils.isNotBlank(dPatentDataCollationInfo.getModifyAppliCityCode()) && dPatentDataCollationInfo.getModifyAppliCityCode().trim().length()>4) {
						String provinceCode=dPatentDataCollationInfo.getModifyAppliCityCode().trim().substring(0,4);
						dPatentDataCollationInfo.setModifyAppliCityCode(provinceCode);
					}
					patentPubInfo.setModifyAppliCityCode(dPatentDataCollationInfo.getModifyAppliCityCode()); // 州市代码
					patentPubInfo.setModifyAppliCity(dPatentDataCollationInfo.getModifyAppliCity()); // 州市
					patentPubInfo.setModifyAppliCountyCode(dPatentDataCollationInfo.getModifyAppliCountyCode());// 区县代码
					patentPubInfo.setModifyAppliCounty(dPatentDataCollationInfo.getModifyAppliCounty()); // 区县
					patentPubInfo.setAddressAdjustmentTime(nowDate);
					patentPubInfo.setUpdateTime(nowDate);
					
					patentPubInfo.setNotThisArea("0");//非本区化标志去掉
//					dPatentPubInfoService.updateById(patentPubInfo);
					resCount=resCount+dPatentPubInfoService.getBaseMapper().updateById(patentPubInfo);
					
				} else if ("act".equals(dPatentDataCollationInfo.getDataType())) { // 数据类型：有效
					// 专利有效导入对象
					DPatentEffectiveInfo patentEffectiveInfo = dPatentEffectiveInfoService
							.getById(dPatentDataCollationInfo.getId());
					if (null == patentEffectiveInfo) {
						return AjaxResult.error("数据不存在，无法修改！");
					}
//                    patentEffectiveInfo.setProvinceCode(dPatentDataCollationInfo.getModifyAppliProvinceCode()); //省份
//                    patentEffectiveInfo.setProvinceName(dPatentDataCollationInfo.getModifyAppliProvince()); //州市
//                    patentEffectiveInfo.setCityCode(dPatentDataCollationInfo.getModifyAppliCityCode());  //区县
//                    patentEffectiveInfo.setCityName(dPatentDataCollationInfo.getModifyAppliCity()); //省份
//                    patentEffectiveInfo.setAreaCode(dPatentDataCollationInfo.getModifyAppliCountyCode()); //州市
//                    patentEffectiveInfo.setAreaName(dPatentDataCollationInfo.getModifyAppliCounty());  //区县

					patentEffectiveInfo.setModifyProvinceCode(dPatentDataCollationInfo.getModifyAppliProvinceCode()); // 省份
					patentEffectiveInfo.setModifyProvinceName(dPatentDataCollationInfo.getModifyAppliProvince()); // 州市
					patentEffectiveInfo.setModifyCityCode(dPatentDataCollationInfo.getModifyAppliCityCode()); // 区县
					patentEffectiveInfo.setModifyCityName(dPatentDataCollationInfo.getModifyAppliCity()); // 省份
					patentEffectiveInfo.setModifyAreaCode(dPatentDataCollationInfo.getModifyAppliCountyCode()); // 州市
					patentEffectiveInfo.setModifyAreaName(dPatentDataCollationInfo.getModifyAppliCounty()); // 区县
					patentEffectiveInfo.setNotThisArea("0");
					patentEffectiveInfo.setAddressAdjustmentTime(nowDate);
					patentEffectiveInfo.setUpdateTime(nowDate);
//					dPatentEffectiveInfoService.updateById(patentEffectiveInfo);
					resCount=resCount+dPatentEffectiveInfoService.getBaseMapper().updateById(patentEffectiveInfo);
				} else if ("auth".equals(dPatentDataCollationInfo.getDataType())) { // 数据类型：授权
					DPatentAuthInfo authInfo = dPatentAuthInfoService.getById(dPatentDataCollationInfo.getId());
					if (null == authInfo) {
						return AjaxResult.error("数据不存在，无法修改！");
					}
					authInfo.setModifyProvinceCode(dPatentDataCollationInfo.getModifyAppliProvinceCode()); // 省份代码
					authInfo.setModifyProvinceName(dPatentDataCollationInfo.getModifyAppliProvince()); // 省份
					authInfo.setModifyCityCode(dPatentDataCollationInfo.getModifyAppliCityCode()); // 州市代码
					authInfo.setModifyCityName(dPatentDataCollationInfo.getModifyAppliCity()); // 州市
					authInfo.setModifyAreaCode(dPatentDataCollationInfo.getModifyAppliCountyCode()); // 区县代码
					authInfo.setModifyAreaName(dPatentDataCollationInfo.getModifyAppliCounty()); // 区县
					authInfo.setAddressAdjustmentTime(nowDate);
					authInfo.setNotThisArea("0");
					authInfo.setUpdateTime(nowDate);
//					dPatentAuthInfoService.updateById(authInfo);
					resCount=resCount+dPatentAuthInfoService.getBaseMapper().updateById(authInfo);
				}
			}
			dPatentDataCollationLog.setAdjustType(dPatentDataCollationInfo.getAdjtype()); // 调整类型
			dPatentDataCollationLog.setAddressAdjustmentTime(nowDate); // 地址调整时间
			dPatentDataCollationLog.setApplyAdjustmentTime(null);
			dPatentDataCollationLog.setSameFalg(sameFlag);
			dPatentDataCollationLog.setDataType(dPatentDataCollationInfo.getDataType());
			dPatentDataCollationLogService.save(dPatentDataCollationLog);
			
			//至少更新1条
			if(resCount<=0) {
				resCount=1;
			}
			return AjaxResult.success("调整成功！",resCount);
		} else if ("type".equals(dPatentDataCollationInfo.getAdjtype())) { // 申请人类型调整
			if (StringUtils.isBlank(dPatentDataCollationInfo.getAppliType())) {
				return AjaxResult.error("请输入申请人类型！");
			}
			// 是否同步更新相同数据
			if (null != dPatentDataCollationInfo.getSameFalg() && dPatentDataCollationInfo.getSameFalg()) {
				sameFlag = "1";
				

				
				// 专利公开-批量更新
				UpdateWrapper<DPatentPubInfo> updatePatent = new UpdateWrapper<>();
				updatePatent.eq("appli", dPatentDataCollationInfo.getAppli());
				// 设置要更新的字段及值
				updatePatent.set("modify_appli_type", dPatentDataCollationInfo.getAppliType());
				updatePatent.set("apply_adjustment_time", nowDate);
				updatePatent.set("update_time", nowDate);
				// 执行更新操作
//				dPatentPubInfoService.update(null, updatePatent);
				resCount=resCount+dPatentPubInfoService.getBaseMapper().update(null, updatePatent);

				// 专利有效-批量更新
				UpdateWrapper<DPatentEffectiveInfo> updateEffective = new UpdateWrapper<>();
				// 设置条件，在申请人地址集合中
				updateEffective.eq("patentee_name", dPatentDataCollationInfo.getAppli());
				// 设置要更新的字段及值
				String appliTypeName=ApplicantType.getByCode(dPatentDataCollationInfo.getAppliType()).getTypeName();
				updateEffective.set("modify_patentee_type", appliTypeName);
				updateEffective.set("apply_adjustment_time", nowDate);
				updateEffective.set("update_time", nowDate);
				// 执行更新操作
//				dPatentEffectiveInfoService.update(null, updateEffective);
				resCount=resCount+dPatentEffectiveInfoService.getBaseMapper().update(null, updateEffective);

				// 专利授权-批量更新
				UpdateWrapper<DPatentAuthInfo> updateAuth = new UpdateWrapper<>();
				// 设置条件，在申请人地址集合中
				updateAuth.eq("patentee_name", dPatentDataCollationInfo.getAppli());
				// 设置要更新的字段及值
				updateAuth.set("modify_patentee_type", appliTypeName);
				updateAuth.set("apply_adjustment_time", nowDate);
				updateAuth.set("update_time", nowDate);
				// 执行更新操作
//				dPatentAuthInfoService.update(null, updateAuth);
				resCount=resCount+dPatentAuthInfoService.getBaseMapper().update(null, updateAuth);
			} else {
				if (StringUtils.isNull(dPatentDataCollationInfo.getId())) {
					return AjaxResult.error("需要调整的记录的ID不能为空！");
				}
				if ("pub".equals(dPatentDataCollationInfo.getDataType())) { // 数据类型：公开
					// 专利公开数据对象
					DPatentPubInfo patentPubInfo = dPatentPubInfoService.getById(dPatentDataCollationInfo.getId());
					if (null == patentPubInfo) {
						return AjaxResult.error("数据不存在，无法修改！");
					}
					patentPubInfo.setModifyAppliType(dPatentDataCollationInfo.getAppliType()); // 申请人类型
					patentPubInfo.setApplyAdjustmentTime(nowDate);
					patentPubInfo.setUpdateTime(nowDate);
//					dPatentPubInfoService.updateById(patentPubInfo);
					resCount=resCount+dPatentPubInfoService.getBaseMapper().updateById(patentPubInfo);
				} else if ("act".equals(dPatentDataCollationInfo.getDataType())) { // 数据类型：有效
					// 专利有效导入对象
					DPatentEffectiveInfo patentEffectiveInfo = dPatentEffectiveInfoService
							.getById(dPatentDataCollationInfo.getId());
					if (null == patentEffectiveInfo) {
						return AjaxResult.error("数据不存在，无法修改！");
					}
					String appliTypeName=ApplicantType.getByCode(dPatentDataCollationInfo.getAppliType()).getTypeName();
					patentEffectiveInfo.setModifyPatenteeType(appliTypeName); // 申请人类型
					patentEffectiveInfo.setApplyAdjustmentTime(nowDate);
					patentEffectiveInfo.setUpdateTime(nowDate);
//					dPatentEffectiveInfoService.updateById(patentEffectiveInfo);
					resCount=resCount+dPatentEffectiveInfoService.getBaseMapper().updateById(patentEffectiveInfo);
				} else if ("auth".equals(dPatentDataCollationInfo.getDataType())) { // 数据类型：授权
					DPatentAuthInfo authInfo = dPatentAuthInfoService.getById(dPatentDataCollationInfo.getId());
					if (null == authInfo) {
						return AjaxResult.error("数据不存在，无法修改！");
					}
					String appliTypeName=ApplicantType.getByCode(dPatentDataCollationInfo.getAppliType()).getTypeName();
					authInfo.setModifyPatenteeType(appliTypeName); // 申请人类型
					authInfo.setApplyAdjustmentTime(nowDate);
					authInfo.setUpdateTime(nowDate);
//					dPatentAuthInfoService.updateById(authInfo);
					resCount=resCount+dPatentAuthInfoService.getBaseMapper().updateById(authInfo);
				}
			}
			dPatentDataCollationLog.setAdjustType(dPatentDataCollationInfo.getAdjtype()); // 调整类型
			dPatentDataCollationLog.setApplyAdjustmentTime(nowDate); // 申请人类型调整时间
			dPatentDataCollationLog.setAddressAdjustmentTime(null);
			dPatentDataCollationLog.setSameFalg(sameFlag);
			dPatentDataCollationLogService.save(dPatentDataCollationLog);
			return AjaxResult.success("调整成功！",resCount);
		} else {
			return AjaxResult.error("数据类型错误,无法修改！");
		}
	}

	/**
	 * 批量更新专利数据整理列表
	 *
	 * @param patentDataQO
	 * @return
	 */
	@Override
	@Transactional
	public AjaxResult batchUpdateList(batchUpdatePatentDataQO patentDataQO) {
		if (null == patentDataQO.getPatentDataList() || patentDataQO.getPatentDataList().size() == 0) {
			return AjaxResult.error("请选择要修改的记录！");
		}
		
		if(StringUtils.isNotBlank(patentDataQO.getModifyAppliProvinceCode()) && patentDataQO.getModifyAppliProvinceCode().trim().length()>2) {
			String provinceCode=patentDataQO.getModifyAppliProvinceCode().trim().substring(0,2);
			patentDataQO.setModifyAppliProvinceCode(provinceCode);
		}
		if(StringUtils.isNotBlank(patentDataQO.getModifyAppliCityCode()) && patentDataQO.getModifyAppliCityCode().trim().length()>4) {
			String provinceCode=patentDataQO.getModifyAppliCityCode().trim().substring(0,4);
			patentDataQO.setModifyAppliCityCode(provinceCode);
		}
		
		List<Long> dPatentPubIdList = new ArrayList<>(); // 专利公开数据ID列表
		List<Long> effectiveIdList = new ArrayList<>(); // 专利有效数据ID列表
		List<Long> authIdList = new ArrayList<>(); // 专利授权数据ID列表
		for (DPatentDataCollationInfo patent : patentDataQO.getPatentDataList()) {
			if (StringUtils.isNull(patent.getId())) {
				return AjaxResult.error("选择的记录中存在id为空的记录，无法修改！");
			}
			if (StringUtils.isEmpty(patent.getDataType())) {
				return AjaxResult.error("id为：“" + patent.getId() + "”的记录数据类型为空，无法修改！");
			}
			if ("act".equals(patent.getDataType())) {
				effectiveIdList.add(patent.getId());
			} else if ("pub".equals(patent.getDataType())) {
				dPatentPubIdList.add(patent.getId());
			} else if ("auth".equals(patent.getDataType())) {
				authIdList.add(patent.getId());
			}
		}
		
		int resCount = 0;
		
		Date nowDate = DateUtils.getNowDate();
		if (dPatentPubIdList.size() > 0) {
			UpdateWrapper<DPatentPubInfo> updateWrapper = new UpdateWrapper<>();
			updateWrapper.in("id", dPatentPubIdList);
			// 设置要更新的字段及值
			if (StringUtils.isNotEmpty(patentDataQO.getAppliType())) {
				
				updateWrapper.set("modify_appli_type", patentDataQO.getAppliType()); // 申请人类型
				updateWrapper.set("update_time", nowDate);
			}
			if (StringUtils.isNotEmpty(patentDataQO.getModifyAppliProvince())
					|| StringUtils.isNotEmpty(patentDataQO.getModifyAppliCity())
					|| StringUtils.isNotEmpty(patentDataQO.getModifyAppliCounty())) {
				updateWrapper.set("modify_appli_province_code", patentDataQO.getModifyAppliProvinceCode());
				updateWrapper.set("modify_appli_province", patentDataQO.getModifyAppliProvince());
				updateWrapper.set("modify_appli_city_code", patentDataQO.getModifyAppliCityCode());
				updateWrapper.set("modify_appli_city", patentDataQO.getModifyAppliCity());
				updateWrapper.set("modify_appli_county_code", patentDataQO.getModifyAppliCountyCode());
				updateWrapper.set("modify_appli_county", patentDataQO.getModifyAppliCounty());
				updateWrapper.set("update_time", nowDate);
			}
			// 执行更新操作-专利公开数据
			resCount = resCount+dPatentPubInfoService.getBaseMapper().update(null, updateWrapper);
//			boolean update = dPatentPubInfoService.update(null, updateWrapper);
//			if (!update) {
//				throw new ServiceException("专利公开记录更新失败!", HttpStatus.ERROR);
//			}
		}
		if (effectiveIdList.size() > 0) {
			UpdateWrapper<DPatentEffectiveInfo> updateWrapper = new UpdateWrapper<>();
			updateWrapper.in("id", effectiveIdList);
			// 设置要更新的字段及值
			if (StringUtils.isNotEmpty(patentDataQO.getAppliType())) {
				String appliTypeName=ApplicantType.getByCode(patentDataQO.getAppliType()).getTypeName();
				updateWrapper.set("modify_patentee_type", appliTypeName); // 申请人类型
				updateWrapper.set("update_time", nowDate);
			}
			if (StringUtils.isNotEmpty(patentDataQO.getModifyAppliProvince())
					|| StringUtils.isNotEmpty(patentDataQO.getModifyAppliCity())
					|| StringUtils.isNotEmpty(patentDataQO.getModifyAppliCounty())) {
				updateWrapper.set("province_code", patentDataQO.getModifyAppliProvinceCode());
				updateWrapper.set("province_name", patentDataQO.getModifyAppliProvince());
				updateWrapper.set("city_code", patentDataQO.getModifyAppliCityCode());
				updateWrapper.set("city_name", patentDataQO.getModifyAppliCity());
				updateWrapper.set("area_code", patentDataQO.getModifyAppliCountyCode());
				updateWrapper.set("area_name", patentDataQO.getModifyAppliCounty());
				updateWrapper.set("update_time", nowDate);
			}
			// 执行更新操作-专利有效数据
			resCount = resCount+dPatentEffectiveInfoService.getBaseMapper().update(null, updateWrapper);
//			boolean update = dPatentEffectiveInfoService.update(null, updateWrapper);
//			if (!update) {
//				throw new ServiceException("专利有效记录更新失败!", HttpStatus.ERROR);
//			}
		}
		if (authIdList.size() > 0) {
			UpdateWrapper<DPatentAuthInfo> updateWrapper = new UpdateWrapper<>();
			updateWrapper.in("id", authIdList);
			// 设置要更新的字段及值
			if (StringUtils.isNotEmpty(patentDataQO.getAppliType())) {
				String appliTypeName=ApplicantType.getByCode(patentDataQO.getAppliType()).getTypeName();
				updateWrapper.set("modify_patentee_type", appliTypeName); // 申请人类型
			}
			if (StringUtils.isNotEmpty(patentDataQO.getModifyAppliProvince())
					|| StringUtils.isNotEmpty(patentDataQO.getModifyAppliCity())
					|| StringUtils.isNotEmpty(patentDataQO.getModifyAppliCounty())) {
				updateWrapper.set("modify_province_code", patentDataQO.getModifyAppliProvinceCode());
				updateWrapper.set("modify_province_name", patentDataQO.getModifyAppliProvince());
				updateWrapper.set("modify_city_code", patentDataQO.getModifyAppliCityCode());
				updateWrapper.set("modify_city_name", patentDataQO.getModifyAppliCity());
				updateWrapper.set("modify_area_code", patentDataQO.getModifyAppliCountyCode());
				updateWrapper.set("modify_area_name", patentDataQO.getModifyAppliCounty());
				updateWrapper.set("update_time", nowDate);
			}
			// 执行更新操作-专利授权数据
			resCount = resCount+dPatentAuthInfoService.getBaseMapper().update(null, updateWrapper);
//			boolean update = dPatentAuthInfoService.update(null, updateWrapper);
//			if (!update) {
//				throw new ServiceException("专利授权记录更新失败!", HttpStatus.ERROR);
//			}
		}
		
		DPatentDataCollationLog log=new DPatentDataCollationLog();
		log.setAdjustType("batch");
		log.setSameFalg("1");
		log.setSyncStatus("0");
		dPatentDataCollationLogService.save(log);		
		return AjaxResult.success("批量修改成功！",resCount);
	}

	/**
	 * 查询专利数据整理列表
	 *
	 * @param dPatentDataCollationInfo 专利数据整理
	 * @return 专利数据整理集合
	 * @date 2023-03-26
	 */
	@Override
	public List<DPatentDataCollationInfo> queryList(DPatentDataCollationInfo dPatentDataCollationInfo) {
		QueryWrapper<DPatentDataCollationInfo> queryWrapper = Wrappers.query();
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getDataType())) {
			queryWrapper.eq("data_type", dPatentDataCollationInfo.getDataType());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getAppNumber())) {
			queryWrapper.eq("app_number", dPatentDataCollationInfo.getAppNumber());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getAppDate())) {
			queryWrapper.eq("app_date", dPatentDataCollationInfo.getAppDate());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getAppli())) {
			queryWrapper.eq("appli", dPatentDataCollationInfo.getAppli());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getAppliTypeSrc())) {
			queryWrapper.eq("appli_type_src", dPatentDataCollationInfo.getAppliTypeSrc());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getAppliType())) {
			queryWrapper.eq("appli_type", dPatentDataCollationInfo.getAppliType());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getAppliAddress())) {
			queryWrapper.eq("appli_address", dPatentDataCollationInfo.getAppliAddress());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getAppliProvinceCode())) {
			queryWrapper.eq("appli_province_code", dPatentDataCollationInfo.getAppliProvinceCode());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getAppliProvince())) {
			queryWrapper.eq("appli_province", dPatentDataCollationInfo.getAppliProvince());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getAppliCityCode())) {
			queryWrapper.eq("appli_city_code", dPatentDataCollationInfo.getAppliCityCode());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getAppliCity())) {
			queryWrapper.eq("appli_city", dPatentDataCollationInfo.getAppliCity());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getAppliCountyCode())) {
			queryWrapper.eq("appli_county_code", dPatentDataCollationInfo.getAppliCountyCode());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getAppliCounty())) {
			queryWrapper.eq("appli_county", dPatentDataCollationInfo.getAppliCounty());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getModifyAppliProvinceCode())) {
			queryWrapper.eq("modify_appli_province_code", dPatentDataCollationInfo.getModifyAppliProvinceCode());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getModifyAppliProvince())) {
			queryWrapper.eq("modify_appli_province", dPatentDataCollationInfo.getModifyAppliProvince());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getModifyAppliCityCode())) {
			queryWrapper.eq("modify_appli_city_code", dPatentDataCollationInfo.getModifyAppliCityCode());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getModifyAppliCity())) {
			queryWrapper.eq("modify_appli_city", dPatentDataCollationInfo.getModifyAppliCity());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getModifyAppliCountyCode())) {
			queryWrapper.eq("modify_appli_county_code", dPatentDataCollationInfo.getModifyAppliCountyCode());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getModifyAppliCounty())) {
			queryWrapper.eq("modify_appli_county", dPatentDataCollationInfo.getModifyAppliCounty());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getHighValue())) {
			queryWrapper.eq("high_value", dPatentDataCollationInfo.getHighValue());
		}
		if (StringUtils.isNotNull(dPatentDataCollationInfo.getInboundDate())) {
			queryWrapper.eq("inbound_date", dPatentDataCollationInfo.getInboundDate());
		}
		if (StringUtils.isNotNull(dPatentDataCollationInfo.getAddressAdjustmentTime())) {
			queryWrapper.eq("address_adjustment_time", dPatentDataCollationInfo.getAddressAdjustmentTime());
		}
		if (StringUtils.isNotNull(dPatentDataCollationInfo.getApplyAdjustmentTime())) {
			queryWrapper.eq("apply_adjustment_time", dPatentDataCollationInfo.getApplyAdjustmentTime());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getTitle())) {
			queryWrapper.eq("title", dPatentDataCollationInfo.getTitle());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getPubNumber())) {
			queryWrapper.eq("pub_number", dPatentDataCollationInfo.getPubNumber());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getCurrPatentStatus())) {
			queryWrapper.eq("curr_patent_status", dPatentDataCollationInfo.getCurrPatentStatus());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getCurrPatentHolder())) {
			queryWrapper.eq("curr_patent_holder", dPatentDataCollationInfo.getCurrPatentHolder());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getPatentType())) {
			queryWrapper.eq("patent_type", dPatentDataCollationInfo.getPatentType());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getIpcMain())) {
			queryWrapper.eq("ipc_main", dPatentDataCollationInfo.getIpcMain());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getLocStr())) {
			queryWrapper.eq("loc_str", dPatentDataCollationInfo.getLocStr());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getNationalEcoStr())) {
			queryWrapper.eq("national_eco_str", dPatentDataCollationInfo.getNationalEcoStr());
		}
		if (StringUtils.isNotNull(dPatentDataCollationInfo.getPatTranCounts())) {
			queryWrapper.eq("pat_tran_counts", dPatentDataCollationInfo.getPatTranCounts());
		}
		if (StringUtils.isNotNull(dPatentDataCollationInfo.getAppTranCounts())) {
			queryWrapper.eq("app_tran_counts", dPatentDataCollationInfo.getAppTranCounts());
		}
		if (StringUtils.isNotNull(dPatentDataCollationInfo.getLicCounts())) {
			queryWrapper.eq("lic_counts", dPatentDataCollationInfo.getLicCounts());
		}
		if (StringUtils.isNotNull(dPatentDataCollationInfo.getPleCounts())) {
			queryWrapper.eq("ple_counts", dPatentDataCollationInfo.getPleCounts());
		}
		if (StringUtils.isNotNull(dPatentDataCollationInfo.getPreCounts())) {
			queryWrapper.eq("pre_counts", dPatentDataCollationInfo.getPreCounts());
		}
		if (StringUtils.isNotNull(dPatentDataCollationInfo.getCurtCounts())) {
			queryWrapper.eq("curt_counts", dPatentDataCollationInfo.getCurtCounts());
		}
		if (StringUtils.isNotEmpty(dPatentDataCollationInfo.getAgencyName())) {
			queryWrapper.like("agency_name", dPatentDataCollationInfo.getAgencyName());
		}
		if (StringUtils.isNotNull(dPatentDataCollationInfo.getStatus())) {
			queryWrapper.eq("status", dPatentDataCollationInfo.getStatus());
		}
		queryWrapper.orderByDesc("id");
		return this.list(queryWrapper);
	}

}
