/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the yifu4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.yifu.cloud.plus.v1.yifu.social.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysArea;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.*;
import com.yifu.cloud.plus.v1.yifu.common.core.util.*;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.dapr.util.UpmsDaprUtils;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import com.yifu.cloud.plus.v1.yifu.social.constants.PreDispatchConstants;
import com.yifu.cloud.plus.v1.yifu.social.entity.SysBaseSetInfo;
import com.yifu.cloud.plus.v1.yifu.social.entity.TPreDispatchInfo;
import com.yifu.cloud.plus.v1.yifu.social.mapper.SysBaseSetInfoMapper;
import com.yifu.cloud.plus.v1.yifu.social.mapper.TPreDispatchInfoMapper;
import com.yifu.cloud.plus.v1.yifu.social.service.TPreDispatchInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 预派单记录
 *
 * @author huyc
 * @date 2022-07-14 18:53:42
 */
@Service
@RequiredArgsConstructor
public class TPreDispatchInfoServiceImpl extends ServiceImpl<TPreDispatchInfoMapper, TPreDispatchInfo> implements TPreDispatchInfoService {

	@Autowired
	private SysBaseSetInfoMapper sysBaseSetInfoMapper;

	/**
	 * 预派单记录简单分页查询
	 *
	 * @param tPreDispatchInfo 预派单记录
	 * @return
	 */
	@Override
	public IPage<TPreDispatchInfo> getTPreDispatchInfoPage(Page<TPreDispatchInfo> page, TPreDispatchInfo tPreDispatchInfo) {
		return baseMapper.getTPreDispatchInfoPage(page, tPreDispatchInfo);
	}

	/**
	 * 修改资料是否提交状态  0 是 1 否
	 *
	 * @param ids
	 * @param status
	 * @return
	 * @Author huyc
	 * @Date 2022-07-18
	 **/
	@Override
	public R<Boolean> modifyDataSubmitStatus(String ids, String status) {
		//  验证传参
		if (Common.isEmpty(ids) ||
				Common.isEmpty(status) ||
				(!CommonConstants.ZERO_STRING.equals(status) && !CommonConstants.ONE_STRING.equals(status))) {
			return R.failed(CommonConstants.PARAM_IS_NOT_ERROR);
		}
		List<String> idList = Common.initStrToList(ids, CommonConstants.COMMA_STRING);
		if (null == idList || idList.isEmpty()) {
			return R.ok(null, CommonConstants.DATA_CAN_NOT_EMPTY);
		}
		baseMapper.modifyDataSubmitStatus(idList, status);
		return R.ok();
	}

	/**
	 * 清空预派单默认的派单合同信息
	 *
	 * @param ids
	 * @return
	 * @Author huyc
	 * @Date 2022-07-18
	 **/
	@Override
	public R<Boolean> clearContractInfo(String ids) {
		if (Common.isEmpty(ids)) {
			return R.failed(CommonConstants.PARAM_IS_NOT_ERROR);
		}
		List<String> idList = Common.initStrToList(ids, CommonConstants.COMMA_STRING);
		if (null == idList || idList.isEmpty()) {
			return R.ok(null, CommonConstants.DATA_CAN_NOT_EMPTY);
		}
		baseMapper.clearContractInfo(idList);
		return R.ok();
	}

	/**
	 * 获取社保和公积金基数
	 *
	 * @param baseSocialTimeMap
	 * @param baseFundTimeMap
	 * @param baseSocialSetMap
	 * @param baseFundSetMap
	 * @return
	 * @Author huyc
	 * @Date 2022-07-18
	 **/
	private void initBaseSetMap(Map<String, TPreDispatchInfo> baseSocialTimeMap,
								Map<String, TPreDispatchInfo> baseFundTimeMap,
								Map<String, SysBaseSetInfo> baseSocialSetMap,
								Map<String, SysBaseSetInfo> baseFundSetMap) {
		/************************获取社保公积金基数开始************************/
		SysBaseSetInfo baseSetInfo = new SysBaseSetInfo();
		// 处理社保基数
		handleSocialBase(baseSocialTimeMap, baseSocialSetMap, baseSetInfo);
		// 处理公积金基数
		handleFundBase(baseFundTimeMap, baseFundSetMap, baseSetInfo);
		/************************获取社保公积金基数结束************************/
	}

	/**
	 * 获取社保基数
	 *
	 * @param baseFundTimeMap
	 * @param baseFundSetMap
	 * @param baseSetInfo
	 * @return
	 * @Author huyc
	 * @Date 2022-07-18
	 **/
	private void handleFundBase(Map<String, TPreDispatchInfo> baseFundTimeMap, Map<String, SysBaseSetInfo> baseFundSetMap, SysBaseSetInfo baseSetInfo) {
		LocalDateTime dateTime;
		List<SysBaseSetInfo> baseSetInfoList;
		if (Common.isNotNull(baseFundTimeMap)) {
			HashMap<String, SysBaseSetInfo> baseFundSetMapT = null;
			baseSetInfo.setBaseType(CommonConstants.ONE_STRING);
			for (TPreDispatchInfo vo : baseFundTimeMap.values()) {
				baseFundSetMapT = null;
				if (null != vo.getFundStart()) {
					dateTime = LocalDateTimeUtils.dateOfYearAndMonth(vo.getFundStart(), LocalDateTimeUtils.DATE_SHORT_PATTERN);
					baseSetInfo.setApplyStartDate(LocalDateTimeUtils.convertLDTToDate(dateTime));
					baseSetInfo.setDeleteFlag(CommonConstants.ZERO_STRING);
					baseSetInfo.setStatus(CommonConstants.ZERO_INT);
					baseSetInfoList = sysBaseSetInfoMapper.getAllByEntity(baseSetInfo);
					if (Common.isNotNull(baseSetInfoList)) {
						baseFundSetMapT = Common.listToHashMapByKey(baseSetInfoList,
								LocalDateTimeUtils.formatTime(dateTime, ValidityConstants.DATEFORMAT3_PATTERN),
								ExcelAttributeConstants.DEPARTID_PROVINCE_CITY_TOWN);
					}
				}
				if (Common.isNotNull(baseFundSetMapT)) {
					baseFundSetMap.putAll(baseFundSetMapT);
				}
			}
		}
	}

	/**
	 * 获取公积金基数
	 *
	 * @param baseSocialTimeMap
	 * @param baseSocialSetMap
	 * @param baseSetInfo
	 * @return
	 * @Author huyc
	 * @Date 2022-07-18
	 **/
	private void handleSocialBase(Map<String, TPreDispatchInfo> baseSocialTimeMap, Map<String, SysBaseSetInfo> baseSocialSetMap, SysBaseSetInfo baseSetInfo) {
		LocalDateTime dateTime;
		List<SysBaseSetInfo> baseSetInfoList;
		if (Common.isNotNull(baseSocialTimeMap)) {
			HashMap<String, SysBaseSetInfo> baseSocialSetMapT = null;
			baseSetInfo.setBaseType(CommonConstants.ZERO_STRING);
			for (TPreDispatchInfo vo : baseSocialTimeMap.values()) {
				baseSocialSetMapT = null;
				if (null != vo.getPensionStart()) {
					dateTime = LocalDateTimeUtils.dateOfYearAndMonth(vo.getPensionStart(), LocalDateTimeUtils.DATE_SHORT_PATTERN);
					baseSetInfo.setApplyStartDate(LocalDateTimeUtils.convertLDTToDate(dateTime));
					baseSetInfo.setDeleteFlag(CommonConstants.ZERO_STRING);
					baseSetInfo.setStatus(CommonConstants.ZERO_INT);
					baseSetInfoList = sysBaseSetInfoMapper.getAllByEntity(baseSetInfo);
					if (Common.isNotNull(baseSetInfoList) && null != dateTime) {
						baseSocialSetMapT = Common.listToHashMapByKey(baseSetInfoList,
								LocalDateTimeUtils.formatTime(dateTime, ValidityConstants.DATEFORMAT3_PATTERN),
								ExcelAttributeConstants.DEPARTID_PROVINCE_CITY_TOWN);
					}
				}
				if (Common.isNotNull(baseSocialSetMapT)) {
					baseSocialSetMap.putAll(baseSocialSetMapT);
				}
			}
		}
	}

	/**
	 * 查看公积金起缴时间是否符合政策
	 *
	 * @param newEntity
	 * @return
	 * @Author huyc
	 * @Date 2022-07-18
	 **/
	private String checkFundStartDate(TPreDispatchInfo newEntity, SysBaseSetInfo baseSetInfo) {
		if (null == baseSetInfo) {
			return PreDispatchConstants.NO_FUND_BASE_SET_INFO;
		}
		// 查看是否符合公积金补缴政策
		if (checkDispatchDate(
				LocalDateTimeUtils.dateOfYearAndMonth(newEntity.getFundStart(), DateUtil.ISO_DATE_FORMAT),
				baseSetInfo.getCanOverpay(), baseSetInfo.getOverpayNumber(), baseSetInfo.getHaveThisMonth())) {
			return PreDispatchConstants.FUND_START_DATE_ERROR;
		}
		return null;
	}

	/**
	 * 查看公积金起缴时间是否符合政策
	 *
	 * @param newEntity
	 * @param oldEntity
	 * @return
	 * @Author huyc
	 * @Date 2022-07-18
	 **/
	private R<Boolean> checkFundStartDate(TPreDispatchInfo newEntity, TPreDispatchInfo oldEntity) {
		// 公积金确实要派增且时间有变动要判断是否符合派单政策
		if (Common.isNotNull(newEntity.getFundAddress()) &&
				Common.isNotNull(newEntity.getFundStart()) &&
				!newEntity.getFundStart().equals(oldEntity.getFundStart())) {
			SysBaseSetInfo baseSetFund = null;
			if (null != newEntity.getFundProvince()) {
				baseSetFund = getSysBaseSetInfo(newEntity.getFundProvince(), newEntity.getFundCity(),
						newEntity.getFundTown(), oldEntity.getFundHouseAdd(), CommonConstants.ONE_STRING);
			} else {
				baseSetFund = getSysBaseSetInfo(oldEntity.getFundProvince(), oldEntity.getFundCity(),
						oldEntity.getFundTown(), oldEntity.getFundHouseAdd(), CommonConstants.ONE_STRING);
			}
			if (null == baseSetFund) {
				return R.failed(PreDispatchConstants.NO_FUND_BASE_SET_INFO);
			}
			// 查看是否符合公积金补缴政策
			if (checkDispatchDate(
					LocalDateTimeUtils.dateOfYearAndMonth(newEntity.getFundStart(), DateUtil.ISO_DATE_FORMAT),
					baseSetFund.getCanOverpay(), baseSetFund.getOverpayNumber(), baseSetFund.getHaveThisMonth())) {
				return R.failed(PreDispatchConstants.FUND_START_DATE_ERROR);
			}
		}
		return null;
	}

	/**
	 * 更新预派单是否派单状态
	 *
	 * @param id
	 * @param dispatchFlag
	 * @param cancelRemark
	 * @return
	 * @Author huyc
	 * @Date 2022-07-18
	 **/
	@Override
	public R<Boolean> updateDispatchFlag(String id, String dispatchFlag, String cancelRemark) {
		TPreDispatchInfo dispatchInfo = baseMapper.selectById(id);
		if (null == dispatchFlag) {
			return R.failed("无此实体！");
		}
		if (!CommonConstants.ZERO_STRING.equals(dispatchFlag) && !CommonConstants.ONE_STRING.equals(dispatchFlag)) {
			return R.failed(CommonConstants.PARAM_IS_NOT_ERROR);
		}
		dispatchInfo.setDispatchFlag(dispatchFlag);
		dispatchInfo.setCancelRemark(cancelRemark);
		baseMapper.updateById(dispatchInfo);
		return R.ok();
	}

	@Override
	public R<Boolean> dispatchById(String id) {
		YifuUser user = SecurityUtils.getUser();
		if (null == user || null == user.getId()) {
			return R.failed(CommonConstants.USER_FAIL);
		}
		if (Common.isEmpty(id)) {
			return R.failed(CommonConstants.PARAM_IS_NOT_ERROR);
		}
//		R<List<TSettleDomainSelectVo>> settleDomainR = null;
		//结算主体名称map key:结算主体id value:结算主体名称
		Map<String, String> settleDomainNameMap = new HashMap<>();
		//客户名称map key:客户id value:客户名称
		Map<String, String> customerNameMap = new HashMap<>();
		//结算主体客户关系map key:结算主体id value:客户id
		Map<String, String> settleIdAndCustomerIdMap = new HashMap<>();
//		settleDomainR = remoteTSettleDomainService.selectAllSettleDomainSelectVos(SecurityConstants.FROM_IN);
		//map数据封装
//		initSettleDomainMaps(settleDomainR, settleDomainNameMap, customerNameMap, settleIdAndCustomerIdMap);
		return R.ok();
	}

	@Override
	public R update(TPreDispatchInfo tPreDispatchInfo) {
		YifuUser user = SecurityUtils.getUser();
		if (null == user || null == user.getId()) {
			return R.failed(CommonConstants.USER_FAIL);
		}
		ExcelAttributeValidityUtil<TPreDispatchInfo> validity = new ExcelAttributeValidityUtil<>(TPreDispatchInfo.class);
		validity.ValidityObj(tPreDispatchInfo);
		if (null != validity.getErrorInfo() && !validity.getErrorInfo().isEmpty()) {
			return R.failed(validity.getErrorInfo(), ResultConstants.VALIDITY_FAIL);
		}
		HashMap<String, SysArea> areaHashMap = new HashMap<>(CommonConstants.SIXTEEN_INT);
		HashMap<String, SysArea> areaIdMap = new HashMap<>(CommonConstants.SIXTEEN_INT);
		TPreDispatchInfo preDispatchTemp = baseMapper.selectById(tPreDispatchInfo.getId());
		tPreDispatchInfo.setType(preDispatchTemp.getType());

		// 初步校验  如果合同时间变更 同步更新 派单的时间
		R<Boolean> errorMessage1 = checkUpdate(tPreDispatchInfo, areaHashMap, areaIdMap, preDispatchTemp);
		if (errorMessage1 != null) {
			return errorMessage1;
		}
		// 社保缴纳地变更 同步更新 社保的省市县数据
		R<Boolean> areaErrorMessage = handleSocialArea(areaHashMap, areaIdMap, tPreDispatchInfo, preDispatchTemp);
		if (null != areaErrorMessage) {
			return areaErrorMessage;
		}
		// 公积金缴纳地变更 同步更新 公积金的省市县数据
		areaErrorMessage = handleFundArea(areaHashMap, areaIdMap, tPreDispatchInfo, preDispatchTemp);
		if (null != areaErrorMessage) {
			return areaErrorMessage;
		}
		// 派增要派单时间  派减不需要
		if (CommonConstants.ZERO_STRING.equals(tPreDispatchInfo.getType())) {
			// 社保起缴日期变更 验证是否可派增
			R<Boolean> x = checkSocialStartDate(tPreDispatchInfo, preDispatchTemp);
			if (x != null) {
				return x;
			}
			// 公积金起缴日期变更 验证是否可派增
			x = checkFundStartDate(tPreDispatchInfo, preDispatchTemp);
			if (x != null) {
				return x;
			}
			// 手机号码必填及格式校验
			if (Common.isEmpty(tPreDispatchInfo.getTelOne())) {
				return R.failed(PreDispatchConstants.TEL_NOT_EMPTY);
			}
			//TODO
//			if (ValidityUtil.checkInvalidEmpPhone(tPreDispatchInfo.getTelOne())){
//				return R.failed(PreDispatchConstants.TEL_INVALID);
//			}
		}
		if (CommonConstants.ONE_STRING.equals(tPreDispatchInfo.getType())) {
			initLeaveReason(tPreDispatchInfo);
			tPreDispatchInfo.setPensionAddress(tPreDispatchInfo.getPensionAddressReduce());
			tPreDispatchInfo.setFundAddress(tPreDispatchInfo.getFundAddressReduce());
		}
		// 合同开始截止时间 同步更新派单的合同时间
		tPreDispatchInfo.setContractEndAdd(tPreDispatchInfo.getContractEnd());
		tPreDispatchInfo.setContractStartAdd(tPreDispatchInfo.getContractStart());
		// 没问题重置异常状态，有问题 在前面就返回了
		tPreDispatchInfo.setExceptionContent(CommonConstants.EMPTY_STRING);
		tPreDispatchInfo.setPreStatus(CommonConstants.ZERO_STRING);
		baseMapper.updatePreDispatchInfoById(tPreDispatchInfo);
		return R.ok();
	}

	private static boolean isaBoolean(LocalDateTime temp, LocalDateTime now) {
		return temp.getYear() == now.getYear() && temp.getMonthValue() < now.getMonthValue();
	}

	/**
	 * 预派单更新前校验
	 * @param tPreDispatchInfo
	 * @param areaHashMap
	 * @param areaIdMap
	 * @param preDispatchTemp
	 * @return
	 * @Author huyc
	 * @Date 2022-07-18
	 **/
	private R<Boolean> checkUpdate(TPreDispatchInfo tPreDispatchInfo, Map<String, SysArea> areaHashMap,
								   Map<String, SysArea> areaIdMap, TPreDispatchInfo preDispatchTemp) {
		if (null == tPreDispatchInfo || Common.isEmpty(tPreDispatchInfo.getId())) {
			return R.failed(CommonConstants.PARAM_IS_NOT_ERROR);
		}
		if (null == preDispatchTemp) {
			return R.failed(CommonConstants.ERROR_NO_DOMAIN);
		}
		// 先验证必填项
		String errorMessage = checkField(tPreDispatchInfo);
		if (null != errorMessage) {
			return R.failed(errorMessage);
		}
		R<List<SysArea>> areaListR = UpmsDaprUtils.getAreaListR();
		if (null == areaListR || areaListR.getCode() != CommonConstants.SUCCESS) {
			return R.failed(PreDispatchConstants.GET_AREA_INFO);
		}
		initAreaMap(areaHashMap, areaIdMap, areaListR);
		return null;
	}

	private void initAreaMap(Map<String, SysArea> areaHashMap, Map<String, SysArea> areaIdMap, R<List<SysArea>> areaListR) {
		List<SysArea> areaList = areaListR.getData();
		if (null != areaList && !areaList.isEmpty()) {
			for (SysArea area : areaList) {
				areaHashMap.put(area.getAreaName(), area);
				areaIdMap.put(Integer.toString(area.getId()), area);
			}
		}
	}

	/**
	 * 校验修改派增派减
	 *
	 * @param tPreDispatchInfo
	 * @return
	 * @Author huyc
	 * @Date 2022-07-18
	 **/
	private String checkField(TPreDispatchInfo tPreDispatchInfo) {
		// 姓名  身份证  缴费地 必填验证
		String x2 = updateOrSaveBaseCheck(tPreDispatchInfo);
		if (x2 != null) {
			return x2;
		}
		// 派增验证
		String x = checkAddPreDispatch(tPreDispatchInfo);
		if (x != null) {
			return x;
		}
		// 派减验证
		String x1 = checkReducePreDispatch(tPreDispatchInfo);
		if (x1 != null) {
			return x1;
		}
		return null;
	}

	private String updateOrSaveBaseCheck(TPreDispatchInfo tPreDispatchInfo) {
		if (Common.isEmpty(tPreDispatchInfo.getEmpName())) {
			return "员工姓名不可为空！";
		}
		if (Common.isEmpty(tPreDispatchInfo.getEmpIdcard())) {
			return "身份证号不可为空！";
		}
		if (Common.isEmpty(tPreDispatchInfo.getPayAddress())) {
			return "缴费地不可为空！";
		}
		// 派增或派减的缴纳地不可同时为空
		if (Common.isEmpty(tPreDispatchInfo.getPensionAddress())
				&& Common.isEmpty(tPreDispatchInfo.getFundAddress())
				&& CommonConstants.ZERO_STRING.equals(tPreDispatchInfo.getType())) {
			return "派增的社保缴纳地与公积金缴纳地不可同时为空！";
		}
		if (Common.isEmpty(tPreDispatchInfo.getPensionAddressReduce())
				&& Common.isEmpty(tPreDispatchInfo.getFundAddressReduce())
				&& CommonConstants.ONE_STRING.equals(tPreDispatchInfo.getType())) {
			return "派减的社保缴纳地与公积金缴纳地不可同时为空！";
		}
		return null;
	}

	private String checkAddPreDispatch(TPreDispatchInfo tPreDispatchInfo) {
		if (CommonConstants.ZERO_STRING.equals(tPreDispatchInfo.getType())) {
			// 拍增的合同开始和结束时间必填 2021-08-13
			if (Common.isEmpty(tPreDispatchInfo.getContractStart()) || Common.isEmpty(tPreDispatchInfo.getContractEnd())) {
				return "派增的合同开始日期和合同终止日期不可为空！";
			}
			if (Common.isEmpty(tPreDispatchInfo.getTelOne())) {
				return PreDispatchConstants.TEL_NOT_EMPTY;
			}
			//TODO
//			if (ValidityUtil.checkInvalidEmpPhone(tPreDispatchInfo.getTelOne())){
//				return PreDispatchConstants.TEL_INVALID;
//			}
			if (Common.isEmpty(tPreDispatchInfo.getPensionAddress()) &&
					Common.isEmpty(tPreDispatchInfo.getFundAddress())) {
				return PreDispatchConstants.NO_SOCIAL_FUND_ADDRESS_ADD;
			}
			// 派单按养老的基数缴纳地和起缴月来 其他险种只保存数据 不做数据校验
			if (Common.isNotNull(tPreDispatchInfo.getPensionAddress())) {
				if (Common.isEmpty(tPreDispatchInfo.getPensionStart())) {
					return "养老起缴月不可为空！";
				}
				if (Common.isNullOrZero(tPreDispatchInfo.getPensionBase())) {
					return "养老基数不可为空！";
				}
			}
			String x = checkFundForMod(tPreDispatchInfo);
			if (x != null) {
				return x;
			}
		}
		return null;
	}

	private String checkReducePreDispatch(TPreDispatchInfo tPreDispatchInfo) {
		if (CommonConstants.ONE_STRING.equals(tPreDispatchInfo.getType())) {
			if (Common.isEmpty(tPreDispatchInfo.getPensionAddressReduce()) &&
					Common.isEmpty(tPreDispatchInfo.getFundAddressReduce())) {
				return PreDispatchConstants.NO_SOCIAL_FUND_ADDRESS_REDUCE;
			}
			// 派单按养老的基数缴纳地和起缴月来 其他险种只保存数据 不做数据校验
			if (Common.isNotNull(tPreDispatchInfo.getPensionAddressReduce()) &&
					Common.isEmpty(tPreDispatchInfo.getPensionEndReduce())) {
				return "派减养老时间不可为空！";
			}
			if (Common.isNotNull(tPreDispatchInfo.getFundAddressReduce()) &&
					Common.isEmpty(tPreDispatchInfo.getFundEndReduce())) {
				return "派减公积金时间不可为空！";
			}
			if (Common.isNotNull(tPreDispatchInfo.getLeaveReason()) &&
					Common.isEmpty(tPreDispatchInfo.getLeaveReason())) {
				return "离职原因不可为空！";
			}
		}
		return null;
	}

	private String checkFundForMod(TPreDispatchInfo tPreDispatchInfo) {
		if (Common.isNotNull(tPreDispatchInfo.getFundAddress())) {
			if (Common.isEmpty(tPreDispatchInfo.getFundStart())) {
				return "公积金起缴月不可为空！";
			}
			if (Common.isNullOrZero(tPreDispatchInfo.getFundBase())) {
				return "公积金基数不可为空！";
			}
			if (Common.isNullOrZero(tPreDispatchInfo.getFundCompanyPer())) {
				return "公积金单位比例不可为空！";
			}
			if (Common.isNullOrZero(tPreDispatchInfo.getFundPersonalPer())) {
				return "公积个人比例不可为空！";
			}
		}
		return null;
	}

	/**
	 * 查看社保起缴日期是否符合补缴政策
	 *
	 * @param newEntity
	 * @Author huyc
	 * @Date 2022-07-18
	 * @return
	 **/
	private String checkSocialStartDate(TPreDispatchInfo newEntity, SysBaseSetInfo baseSetInfo) {
		// 社保确实要派增且时间有变动要判断是否符合派单政策
		if (null == baseSetInfo) {
			return PreDispatchConstants.NO_SOCIAL_BASE_SET_INFO;
		}
		// 查看是否符合社保补缴政策
		if (checkDispatchDate(
				LocalDateTimeUtils.dateOfYearAndMonth(newEntity.getPensionStart(), DateUtil.ISO_DATE_FORMAT),
				baseSetInfo.getCanOverpay(), baseSetInfo.getOverpayNumber(), baseSetInfo.getHaveThisMonth())) {
			return PreDispatchConstants.SOCIAL_START_DATE_ERROR;
		}
		return null;
	}

	/**
	 * 查看社保起缴日期是否符合补缴政策
	 * @Author huyc
	 * @Date 2022-07-18
	 * @param newEntity
	 * @return
	 **/
	private R<Boolean> checkSocialStartDate(TPreDispatchInfo newEntity, TPreDispatchInfo oldEntity) {
		// 社保确实要派增且时间有变动要判断是否符合派单政策
		if (Common.isNotNull(newEntity.getPensionAddress()) &&
				Common.isNotNull(newEntity.getPensionStart()) &&
				!newEntity.getPensionStart().equals(oldEntity.getPensionStart())){
			SysBaseSetInfo baseSetSocial = null;
			if (null != newEntity.getSocialProvince()){
				baseSetSocial = getSysBaseSetInfo(newEntity.getSocialProvince(),newEntity.getSocialCity(),
						newEntity.getSocialTown(), oldEntity.getSocialHouseAdd(),CommonConstants.ZERO_STRING);
			}else {
				baseSetSocial = getSysBaseSetInfo(oldEntity.getSocialProvince(),oldEntity.getSocialCity(),
						oldEntity.getSocialTown(), oldEntity.getSocialHouseAdd(),CommonConstants.ZERO_STRING);
			}
			if (null == baseSetSocial){
				return R.failed(PreDispatchConstants.NO_SOCIAL_BASE_SET_INFO);
			}
			// 查看是否符合社保补缴政策
			if (checkDispatchDate(
					LocalDateTimeUtils.dateOfYearAndMonth(newEntity.getPensionStart(), DateUtil.ISO_DATE_FORMAT),
					baseSetSocial.getCanOverpay(),baseSetSocial.getOverpayNumber(),baseSetSocial.getHaveThisMonth())){
				return R.failed(PreDispatchConstants.SOCIAL_START_DATE_ERROR);
			}
		}
		return null;
	}

	/**
	 * 查看社保公积金起缴日期是否符合补缴逻辑
	 * @Author huyc
	 * @Date 2022-07-18
	 * @param socialStartDate 起缴日期
	 * @param canOverpay      是否可补缴 0：是，1：否
	 * @param overpayNumber   补缴期限 整数
	 * @param haveThisMonth   是否含当月 0：是，1：否   如果无符合条件默认不含当月
	 * @return 符合 false  不符合 true
	 **/
	public static boolean checkDispatchDate(LocalDateTime socialStartDate, String canOverpay, Integer overpayNumber, String haveThisMonth) {
		if (null == socialStartDate) {
			return false;
		}
		LocalDateTime temp = null;
		LocalDateTime now = LocalDateTime.now();
		if (CommonConstants.ZERO_STRING.equals(canOverpay)) {
			temp = socialStartDate.plusMonths(CommonConstants.ZERO_STRING.equals(haveThisMonth) ? (long) overpayNumber : (long) (overpayNumber + 1));
		} else if (CommonConstants.ONE_STRING.equals(canOverpay)) {
			temp = socialStartDate;
		}
		return (null != temp && (temp.getYear() < now.getYear() || isaBoolean(temp, now)));
	}

	/**
	 * 0 个人辞职 = 合同主动解除  1单位辞退 = 合同被动解除  15  死亡 = 死亡  19 其他
	 * @param tPreDispatchInfo
	 * @Author fxj
	 * @Date 2021-01-07
	 * @return
	 **/
	private void initLeaveReason(TPreDispatchInfo tPreDispatchInfo) {
		if ("合同主动解除".equals(tPreDispatchInfo.getLeaveReason())) {
			tPreDispatchInfo.setLeaveReasonAdd(CommonConstants.ZERO_STRING);
		} else if ("合同被动解除".equals(tPreDispatchInfo.getLeaveReason())) {
			tPreDispatchInfo.setLeaveReasonAdd(CommonConstants.ONE_STRING);
		} else if ("死亡".equals(tPreDispatchInfo.getLeaveReason())) {
			tPreDispatchInfo.setLeaveReasonAdd(CommonConstants.FIFTEEN);
		} else {
			tPreDispatchInfo.setLeaveReasonAdd(CommonConstants.NINETEEN);
		}
	}

	private SysBaseSetInfo getSysBaseSetInfo(String province,String city,String town,String houseId,String type) {
		SysBaseSetInfo queryEntity = new SysBaseSetInfo();
		queryEntity.setBaseType(type);
		queryEntity.setStatus(CommonConstants.ZERO_INT);
		queryEntity.setDepartId(houseId);
		if (Common.isNotNull(province)){
			queryEntity.setProvince(Integer.valueOf(province));
		}
		if (Common.isNotNull(city)){
			queryEntity.setCity(Integer.valueOf(city));
		}
		if (Common.isNotNull(town)){
			queryEntity.setTown(Integer.valueOf(town));
		}
		queryEntity.setDeleteFlag(CommonConstants.ZERO_STRING);
		QueryWrapper<SysBaseSetInfo> queryWrapper =  new QueryWrapper<>();
		queryWrapper.setEntity(queryEntity);
		if (Common.isEmpty(city)){
			queryWrapper.isNull(PreDispatchConstants.CITY);
		}
		if (Common.isEmpty(town)){
			queryWrapper.isNull(PreDispatchConstants.TOWN);
		}
		queryWrapper.last(CommonConstants.LAST_ONE_SQL);
		return sysBaseSetInfoMapper.selectOne(queryWrapper);
	}

	/**
	 * 重新获取公积金缴纳地数据
	 * @Author huyc
	 * @Date 2022-07-18
	 * @param areaMap
	 * @param areaIdMap
	 * @param newEntity
	 * @param oldEntity
	 * @return
	 **/
	private R<Boolean> handleFundArea(Map<String, SysArea> areaMap,
									  Map<String, SysArea> areaIdMap, TPreDispatchInfo newEntity, TPreDispatchInfo oldEntity) {
		// 为空由更新语句清空缴纳地  不为空且有变化的要重新获取数据
		if (Common.isNotNull(newEntity.getFundAddress()) &&
				(!newEntity.getFundAddress().equals(oldEntity.getFundAddress())
						|| null == oldEntity.getFundProvince())){
			return initFundArea(areaMap, areaIdMap, newEntity,newEntity.getFundAddress());
		}
		if (Common.isNotNull(newEntity.getFundAddressReduce()) &&
				(!newEntity.getFundAddressReduce().equals(oldEntity.getFundAddressReduce())
						|| null == oldEntity.getFundProvince())){
			return initFundArea(areaMap, areaIdMap, newEntity,newEntity.getFundAddressReduce());
		}
		return null;
	}

	private R<Boolean> initFundArea(Map<String, SysArea> areaMap,
									Map<String, SysArea> areaIdMap, TPreDispatchInfo entity,String address) {
		SysArea area = areaMap.get(address);
		SysArea areaTemp;
		if (null != area) {
			// 处理省数据
			if (null == area.getParentId() || CommonConstants.ZERO_INT == area.getParentId().intValue()) {
				entity.setFundProvince(area.getId().toString());
				entity.setFundCity(null);
				entity.setFundTown(null);
				return null;
			}
			areaTemp = areaIdMap.get(Integer.toString(area.getParentId()));
			// 处理市县数据
			if (handleFundAreaSub(entity, area, areaTemp)) {
				return R.failed(getAreaErrorInfo(entity.getType()));
			}

		} else {
			return R.failed(getAreaErrorInfo(entity.getType()));
		}
		return null;
	}

	/**
	 * 获取公积金市县 数据
	 * @Author huyc
	 * @Date 2022-07-18
	 * @param newEntity
	 * @param area
	 * @param areaTemp
	 * @return
	 **/
	private boolean handleFundAreaSub(TPreDispatchInfo newEntity, SysArea area, SysArea areaTemp) {
		if (null != areaTemp){
			if (null == areaTemp.getParentId() || CommonConstants.ZERO_INT == areaTemp.getParentId().intValue()){
				newEntity.setFundProvince(areaTemp.getId().toString());
				newEntity.setFundCity(area.getId().toString());
				newEntity.setFundTown(null);
			}else {
				newEntity.setFundProvince(areaTemp.getParentId().toString());
				newEntity.setFundCity(areaTemp.getId().toString());
				newEntity.setFundTown(area.getId().toString());
			}
		}else {
			return true;
		}
		return false;
	}

	private String getAreaErrorInfo(String type){
		if (CommonConstants.ZERO_STRING.equals(type)){
			return PreDispatchConstants.NO_PENSION_AREA_ADD;
		}
		if (CommonConstants.ONE_STRING.equals(type)){
			return PreDispatchConstants.NO_PENSION_AREA_REDUCE;
		}
		return CommonConstants.EMPTY_STRING;
	}

	/**
	 * 重新获取社保缴纳地
	 * @Author huyc
	 * @Date 2022-07-18
	 * @param areaMap
	 * @param newEntity
	 * @param oldEntity
	 * @return
	 **/
	private R<Boolean> handleSocialArea(Map<String, SysArea> areaMap, Map<String,SysArea> areaIdMap,
										TPreDispatchInfo newEntity, TPreDispatchInfo oldEntity) {
		// 为空由更新语句清空缴纳地  不为空且有变化的要重新获取数据 不为空且不相等 更新缴纳地
		if (Common.isNotNull(newEntity.getPensionAddress()) &&
				(!newEntity.getPensionAddress().equals(oldEntity.getPensionAddress())
						|| null == oldEntity.getSocialProvince())){
			return initSocialArea(areaMap, areaIdMap, newEntity,newEntity.getPensionAddress());
		}
		//  处理派减 不为空且不相等 更新缴纳地
		if (Common.isNotNull(newEntity.getPensionAddressReduce()) &&
				(!newEntity.getPensionAddressReduce().equals(oldEntity.getPensionAddressReduce())
						|| null == oldEntity.getSocialProvince())){
			return initSocialArea(areaMap, areaIdMap, newEntity,newEntity.getPensionAddressReduce());
		}
		return null;
	}

	private R<Boolean> initSocialArea(Map<String, SysArea> areaMap,
									  Map<String, SysArea> areaIdMap, TPreDispatchInfo entity,String address) {
		SysArea area = areaMap.get(address);
		SysArea areaTemp;
		if (null != area){
			// 处理省数据
			if (null == area.getParentId() || CommonConstants.ZERO_INT == area.getParentId().intValue()){
				entity.setSocialProvince(area.getId().toString());
				entity.setSocialCity(null);
				entity.setSocialTown(null);
				return null;
			}
			areaTemp = areaIdMap.get(Integer.toString(area.getParentId()));
			// 处理市县数据
			if (handleSocialAreaSub(entity, area, areaTemp)){
				return R.failed(getAreaErrorInfo(entity.getType()));
			}
		}else {
			return R.failed(getAreaErrorInfo(entity.getType()));
		}
		return null;
	}

	/**
	 * 获取社保市县 数据
	 * @Author huyc
	 * @Date 2022-07-18
	 * @param newEntity
	 * @param area
	 * @param areaTemp
	 * @return
	 **/
	private boolean handleSocialAreaSub(TPreDispatchInfo newEntity, SysArea area, SysArea areaTemp) {
		if (null != areaTemp){
			if (null == areaTemp.getParentId() || CommonConstants.ZERO_INT == areaTemp.getParentId().intValue()){
				newEntity.setSocialProvince(areaTemp.getId().toString());
				newEntity.setSocialCity(area.getId().toString());
				newEntity.setSocialTown(null);
			}else {
				newEntity.setSocialProvince(areaTemp.getParentId().toString());
				newEntity.setSocialCity(areaTemp.getId().toString());
				newEntity.setSocialTown(area.getId().toString());
			}
		}else {
			return true;
		}
		return false;
	}

}
