package com.yifu.cloud.plus.v1.yifu.social.service.impl;/*
 *    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)
 */

import cn.hutool.core.collection.CollUtil;
import com.alibaba.excel.read.listener.ReadListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
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.admin.api.vo.AreaVo;
import com.yifu.cloud.plus.v1.yifu.archives.vo.TSettleDomainListVo;
import com.yifu.cloud.plus.v1.yifu.archives.vo.TSettleDomainSelectVo;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.ExcelAttributeConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.exception.ErrorCodes;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.dapr.util.ArchivesDaprUtil;
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.salary.vo.TPaymentBySalaryVo;
import com.yifu.cloud.plus.v1.yifu.salary.vo.TPaymentVo;
import com.yifu.cloud.plus.v1.yifu.social.concurrent.threadpool.YFSocialImportThreadPoolExecutor;
import com.yifu.cloud.plus.v1.yifu.social.constants.PaymentConstants;
import com.yifu.cloud.plus.v1.yifu.social.constants.SocialConstants;
import com.yifu.cloud.plus.v1.yifu.social.entity.*;
import com.yifu.cloud.plus.v1.yifu.social.mapper.*;
import com.yifu.cloud.plus.v1.yifu.social.service.TPaymentInfoImportLogService;
import com.yifu.cloud.plus.v1.yifu.social.service.TPaymentInfoService;
import com.yifu.cloud.plus.v1.yifu.social.vo.TPaymentHeFeiVo;
import com.yifu.cloud.plus.v1.yifu.social.vo.TPaymentInfoSearchVo;
import com.yifu.cloud.plus.v1.yifu.social.vo.TPaymentInfoVo;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yifu.cloud.plus.v1.yifu.common.core.util.ErrorMessage;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.*;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.metadata.holder.ReadRowHolder;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.*;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * 缴费库
 *
 * @author huyc
 * @date 2022-07-22 17:01:22
 */
@Log4j2
@Service
public class TPaymentInfoServiceImpl extends ServiceImpl<TPaymentInfoMapper, TPaymentInfo> implements TPaymentInfoService {

	@Autowired
	private RedisUtil redisUtil;

	@Autowired
	private YFSocialImportThreadPoolExecutor yfSocialImportThreadPoolExecutor;

	@Autowired
	private UpmsDaprUtils upmsDaprUtils;

	@Autowired
	private TPaymentInfoImportLogService tPaymentInfoImportLogService;

	@Autowired
	private TSocialFundInfoMapper socialFundInfoMapper;

	@Autowired
	private TSocialInfoMapper socialInfoMapper;

	@Autowired
	private TProvidentFundMapper providentFundMapper;

	private final int partSize = 30;

	@Autowired
	private ArchivesDaprUtil archivesDaprUtil;

	@Autowired
	private SysHouseHoldInfoMapper sysHouseHoldInfoMapper;

	/**
	 * 缴费库简单分页查询
	 *
	 * @param tPaymentInfo 缴费库
	 * @return
	 */
	@Override
	public IPage<TPaymentInfo> getTPaymentInfoPage(Page<TPaymentInfo> page, TPaymentInfo tPaymentInfo) {
		return baseMapper.getTPaymentInfoPage(page, tPaymentInfo);
	}

	/**
	 * 缴费库批量导出
	 *
	 * @param searchVo 缴费库
	 * @return
	 */
	@Override
	public void listExport(HttpServletResponse response, TPaymentInfoSearchVo searchVo) {
		String fileName = "缴费库批量导出" + DateUtil.getThisTime() + ".xlsx";
		//获取要导出的列表
		List<TPaymentInfo> list = new ArrayList<>();
		Integer count = baseMapper.selectCountTPaymentInfo(searchVo);
		ServletOutputStream out = null;
		try {
			out = response.getOutputStream();
			response.setContentType(CommonConstants.MULTIPART_FORM_DATA);
			response.setCharacterEncoding("utf-8");
			response.setHeader(CommonConstants.CONTENT_DISPOSITION, CommonConstants.ATTACHMENT_FILENAME + URLEncoder.encode(fileName, "UTF-8"));
			// 这里 需要指定写用哪个class去写，然后写到第一个sheet，然后文件流会自动关闭
			ExcelWriter excelWriter = EasyExcel.write(out, TPaymentInfo.class).includeColumnFiledNames(searchVo.getExportFields()).build();
			int index = 0;
			if (count > CommonConstants.ZERO_INT) {
				for (int i = 0; i <= count; ) {
					// 获取实际记录
					searchVo.setLimitStart(i);
					searchVo.setLimitEnd(CommonConstants.EXCEL_EXPORT_LIMIT);
					list = baseMapper.getTPaymentInfoNoPage(searchVo);
					if (Common.isNotNull(list)) {
						ExcelUtil<TPaymentInfo> util = new ExcelUtil<>(TPaymentInfo.class);
						for (TPaymentInfo vo : list) {
							util.convertEntity(vo, null, null, null);
						}
					}
					if (Common.isNotNull(list)) {
						WriteSheet writeSheet = EasyExcel.writerSheet("缴费库" + index).build();
						excelWriter.write(list, writeSheet);
						index++;
					}
					i = i + CommonConstants.EXCEL_EXPORT_LIMIT;
					if (Common.isNotNull(list)) {
						list.clear();
					}
				}
			} else {
				WriteSheet writeSheet = EasyExcel.writerSheet("缴费库" + index).build();
				excelWriter.write(list, writeSheet);
			}
			if (Common.isNotNull(list)) {
				list.clear();
			}
			out.flush();
			excelWriter.finish();
		} catch (Exception e) {
			log.error("执行异常", e);
		} finally {
			try {
				if (null != out) {
					out.close();
				}
			} catch (IOException e) {
				log.error("执行异常", e);
			}
		}
	}

	@Override
	public R removeInFoById(String id) {
		YifuUser user = SecurityUtils.getUser();

		TPaymentInfo paymentInfo = baseMapper.selectById(id);
		if (!user.getId().equals(paymentInfo.getCreateBy()) && !CommonConstants.ONE_STRING.equals(user.getId())) {
			return R.failed("非本人创建的数据禁止删除！");
		}

		if (CommonConstants.ZERO_STRING.equals(paymentInfo.getPushStatus())) {
			return R.failed("已推送至结算平台禁止删除！");
		}
		baseMapper.deleteById(id);
		return R.ok();
	}

	@Override
	public R<Boolean> removeAllInfoById(String ids) {
		YifuUser user = SecurityUtils.getUser();
		if (Common.isEmpty(ids)) {
			return R.failed(CommonConstants.NO_DATA_TO_HANDLE);
		}
		List<String> idList = Common.initStrToList(ids, CommonConstants.COMMA_STRING);
		TPaymentInfo paymentInfo = null;

		for (String id : idList) {
			paymentInfo = baseMapper.selectById(id);
			if (null != paymentInfo && Common.isNotNull(paymentInfo.getId())) {
				if ((!user.getId().equals(paymentInfo.getCreateBy()) &&
						!CommonConstants.ONE_STRING.equals(user.getId())) ||
						CommonConstants.ZERO_STRING.equals(paymentInfo.getPushStatus())) {
					return R.failed("存在不符合条件的数据(已推送至结算平台或非本人创建),禁止删除！");
				}
				baseMapper.deleteById(id);
			}
		}
		return R.ok();
	}
	/**
	 * 通过ID获取缴费库 及社保、公积金明细
	 *
	 * @param id
	 * @Author huyc
	 * @Date 2022-07-24
	 **/
	@Override
	public R getAllInfoById(String id) {
		TPaymentInfoVo vo = baseMapper.getAllInfoById(id);
		return R.ok(vo, "查询成功！");
	}

	@Override
	public R removeBatchByInfo(String empIdCard, String socialHouseId, String fundHouseId,
							   String socialCreateMonth, String socialPayMonth) {
		YifuUser user = SecurityUtils.getUser();

		LambdaQueryWrapper<TPaymentInfo> wrapper = buildQueryWrapper(empIdCard,socialHouseId,fundHouseId,
				socialCreateMonth,socialPayMonth);

		List<TPaymentInfo> list = baseMapper.selectList(wrapper);
		List<String> idList = new ArrayList<>();
		Boolean delFlag = false;
		if (Common.isNotNull(list)) {
			for (TPaymentInfo paymentInfo : list) {
				if ((!user.getId().equals(paymentInfo.getCreateBy()) &&
						!CommonConstants.ONE_STRING.equals(user.getId())) ||
						CommonConstants.ZERO_STRING.equals(paymentInfo.getPushStatus())) {
					delFlag = true;
				} else {
					idList.add(paymentInfo.getId());
				}
			}
		} else {
			return R.failed(CommonConstants.NO_DATA_TO_HANDLE);
		}
		if (Common.isNotNull(idList)) {
			baseMapper.deleteBatchIds(idList);
		}
		if (delFlag) {
			return R.ok(null,"存在不符合条件的数据(已推送至结算平台或非本人创建),禁止删除！");
		}
		return R.ok(null,"批量删除成功");
	}

	private LambdaQueryWrapper buildQueryWrapper(String empIdCard, String socialHouseId, String fundHouseId,
												 String socialCreateMonth, String socialPayMonth){
		LambdaQueryWrapper<TPaymentInfo> wrapper = Wrappers.lambdaQuery();
		if (Common.isNotNull(empIdCard)) {
			wrapper.eq(TPaymentInfo::getEmpIdcard, empIdCard);
		}
		if (Common.isNotNull(socialHouseId)) {
			SysHouseHoldInfo info = sysHouseHoldInfoMapper.selectById(socialHouseId);
			if (Common.isNotNull(info)) {
				wrapper.eq(TPaymentInfo::getSocialHousehold, info.getName());
			}
		}
		if (Common.isNotNull(fundHouseId)) {
			SysHouseHoldInfo infoFund = sysHouseHoldInfoMapper.selectById(fundHouseId);
			if (Common.isNotNull(infoFund)) {
				wrapper.eq(TPaymentInfo::getProvidentHousehold, infoFund.getName());
			}
		}
		if (Common.isNotNull(socialCreateMonth)) {
			wrapper.eq(TPaymentInfo::getSocialCreateMonth, socialCreateMonth).or()
					.eq(TPaymentInfo::getProvidentCreateMonth, socialCreateMonth);
		}
		if (Common.isNotNull(socialPayMonth)) {
			wrapper.eq(TPaymentInfo::getSocialPayMonth, socialPayMonth).or()
					.eq(TPaymentInfo::getProvidentPayMonth, socialPayMonth);;
		}
		return wrapper;
	}

	@Override
	public R<List<ErrorMessage>> importSocialDiy(InputStream inputStream, String random) {
		YifuUser user = SecurityUtils.getUser();
		List<ErrorMessage> errorMessageList = new ArrayList<>();
		ExcelUtil<TPaymentInfoVo> util1 = new ExcelUtil<>(TPaymentInfoVo.class);
		;
		// 写法2：
		// 匿名内部类 不用额外写一个DemoDataListener
		// 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
		try {
			EasyExcel.read(inputStream, TPaymentInfoVo.class, new ReadListener<TPaymentInfoVo>() {
				/**
				 * 单次缓存的数据量
				 */
				public static final int BATCH_COUNT = CommonConstants.BATCH_COUNT;
				//所有行数
				Integer rowNumber;
				/**
				 *临时存储
				 */
				private List<TPaymentInfoVo> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

				@Override
				public void invoke(TPaymentInfoVo data, AnalysisContext context) {
					ReadRowHolder readRowHolder = context.readRowHolder();
					// 获取总行数（含表头）
					rowNumber = context.readSheetHolder().getApproximateTotalRowNumber();
					Integer rowIndex = readRowHolder.getRowIndex();
					data.setRowIndex(rowIndex + 1);
					ErrorMessage errorMessage = util1.checkEntity(data, data.getRowIndex());
					if (Common.isNotNull(errorMessage)) {
						errorMessageList.add(errorMessage);
					} else {
						cachedDataList.add(data);
					}
					if (cachedDataList.size() >= BATCH_COUNT) {
						saveData();
						// 存储完成清理 list
						cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
					}
				}

				@Override
				public void doAfterAllAnalysed(AnalysisContext context) {
					saveData();
				}

				/**
				 * 加上存储数据库
				 */
				private void saveData() {
					log.info("{}条数据，开始存储数据库！", cachedDataList.size());
					importTPaymentSocialInfo(cachedDataList, errorMessageList, random, user, rowNumber);
					log.info("存储数据库成功！");
				}
			}).sheet().doRead();
		} catch (Exception e) {
			log.error(CommonConstants.IMPORT_DATA_ANALYSIS_ERROR, e);
			return R.failed(CommonConstants.IMPORT_DATA_ANALYSIS_ERROR);
		}
		return R.ok(errorMessageList);
	}

	private void importTPaymentSocialInfo(List<TPaymentInfoVo> list, List<ErrorMessage> errorMessageList,
										  String random, YifuUser user, Integer rowNumber) {
		long start = System.currentTimeMillis();

		HashMap<String, String> areaMap = new HashMap<String, String>();
		HashMap<String, String> areaMap2 = new HashMap<String, String>();
		R<AreaVo> areaListR = upmsDaprUtils.getAreaListR();
		if (Common.isNotNull(areaListR)) {
			AreaVo areaList = areaListR.getData();
			if (null != areaList && !areaList.getSysAreaList().isEmpty()) {
				for (SysArea area : areaList.getSysAreaList()) {
					areaMap.put(Integer.toString(area.getId()), area.getAreaName());
					areaMap2.put(area.getAreaName() + CommonConstants.DOWN_LINE_STRING + (
							null == area.getParentId() ? "null" : (0 == area.getParentId().intValue() ? "null" : Integer.toString(
									area.getParentId()))), Integer.toString(area.getId()));
				}
			}
		}

		// 判断是否有相同的用户在导入社保费用 存在提示稍后重试
		String key = user.getId() + CommonConstants.DOWN_LINE_STRING + CommonConstants.PAYMENT_SOCIAL_IMPORT;
		if (redisUtil.exists(key)) {
			errorMessageList.add(new ErrorMessage(-1, "当前用户数据导入中，请稍后重试！"));
			redisUtil.remove(key);
		} else {
			redisUtil.set(key, user.getId(), 360L);

			// 导入前做队列空闲判断，防止队列不足出现数据丢失的场景
			if (CollUtil.isNotEmpty(list)) {
				int taskSize = list.size() / partSize + CommonConstants.ONE_INT;
				int residualCapacity = yfSocialImportThreadPoolExecutor.getResidualCapacity();
				if (taskSize > residualCapacity) {
					errorMessageList.add(new ErrorMessage(-1, MsgUtils.getMessage(ErrorCodes.SOCIALINFO_LIST_NUM_LARGE)));
				}else {
					// -----------------------------生成paymentInfoMap本段开始--------------------------------
					//已存在的档案数据
					List<String> monthList = Common.listObjectToStrList(list, ExcelAttributeConstants.SOCIAL_PAY_MONTH).stream().distinct().collect(Collectors.toList());
					//已存在社保缴费库数据  非删除状态
					List<TPaymentInfo> paymentInfos = baseMapper.selectListForPaymentImport(monthList, Common.listObjectToStrList(list, ExcelAttributeConstants.EMPIDCARD));
					//养老
					ConcurrentHashMap<String, TPaymentInfo> paymentInfoPensionMap = new ConcurrentHashMap<>();
					//大病救助金
					ConcurrentHashMap<String, TPaymentInfo> paymentInfoBigMap = new ConcurrentHashMap<>();
					//失业
					ConcurrentHashMap<String, TPaymentInfo> paymentInfoUnEmpMap = new ConcurrentHashMap<>();
					//生育
					ConcurrentHashMap<String, TPaymentInfo> paymentInfoBirMap = new ConcurrentHashMap<>();
					//工伤
					ConcurrentHashMap<String, TPaymentInfo> paymentInfoInjuryMap = new ConcurrentHashMap<>();
					//医保
					ConcurrentHashMap<String, TPaymentInfo> paymentInfoMedicalMap = new ConcurrentHashMap<>();
					if (Common.isNotNull(paymentInfos)) {
						for (TPaymentInfo info : paymentInfos) {
							if (BigDecimalUtils.safeAdd(BigDecimalUtils.isNullToZero(info.getUnitPensionMoney()),
									BigDecimalUtils.isNullToZero(info.getPersonalPensionMoney())).compareTo(BigDecimal.ZERO) != 0) {
								paymentInfoPensionMap.put(info.getSocialPayAddr()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getEmpIdcard()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getSocialPayMonth()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getSocialCreateMonth()
												+ CommonConstants.DOWN_LINE_STRING
												+ SocialConstants.YL
										, info);
							}
							if (BigDecimalUtils.safeAdd(BigDecimalUtils.isNullToZero(info.getPersonalBigmailmentMoney()),
									BigDecimalUtils.isNullToZero(info.getUnitBigmailmentMoney())).compareTo(BigDecimal.ZERO) != 0) {
								paymentInfoBigMap.put(info.getSocialPayAddr()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getEmpIdcard()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getSocialPayMonth()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getSocialCreateMonth()
												+ CommonConstants.DOWN_LINE_STRING
												+ SocialConstants.DB
										, info);
							}
							if (BigDecimalUtils.safeAdd(BigDecimalUtils.isNullToZero(info.getUnitUnemploymentMoney()),
									BigDecimalUtils.isNullToZero(info.getPersonalUnemploymentMoney())).compareTo(BigDecimal.ZERO) != 0) {
								paymentInfoUnEmpMap.put(info.getSocialPayAddr()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getEmpIdcard()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getSocialPayMonth()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getSocialCreateMonth()
												+ CommonConstants.DOWN_LINE_STRING
												+ SocialConstants.SY
										, info);
							}
							if (BigDecimalUtils.isNullToZero(info.getUnitBirthMoney()).compareTo(BigDecimal.ZERO) != 0) {
								paymentInfoBirMap.put(info.getSocialPayAddr()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getEmpIdcard()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getSocialPayMonth()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getSocialCreateMonth()
												+ CommonConstants.DOWN_LINE_STRING
												+ SocialConstants.BIR
										, info);
							}
							if (BigDecimalUtils.isNullToZero(info.getUnitInjuryMoney()).compareTo(BigDecimal.ZERO) != 0) {
								paymentInfoInjuryMap.put(info.getSocialPayAddr()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getEmpIdcard()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getSocialPayMonth()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getSocialCreateMonth()
												+ CommonConstants.DOWN_LINE_STRING
												+ SocialConstants.GS
										, info);
							}
							if (BigDecimalUtils.safeAdd(BigDecimalUtils.isNullToZero(info.getUnitMedicalMoney()),
									BigDecimalUtils.isNullToZero(info.getPersonalMedicalMoney())).compareTo(BigDecimal.ZERO) != 0) {
								paymentInfoMedicalMap.put(info.getSocialPayAddr()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getEmpIdcard()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getSocialPayMonth()
												+ CommonConstants.DOWN_LINE_STRING
												+ info.getSocialCreateMonth()
												+ CommonConstants.DOWN_LINE_STRING
												+ SocialConstants.YB
										, info);
							}
						}
					}
					// -----------------------------生成paymentInfoMap本段结束--------------------------------
					/**
					 *  修改批量导入社保为线程池的方式。逻辑分析，根据前端传入的list，将list分成每份partSize个。
					 *  有以下3种情况。1.list数量不足partSize，直接进行处理;2.list数量正好可以根据partSize等分，循环处理;3.不能等分有剩余的情况
					 */

					// -----------------------------线程池处理list，批量保存社保信息开始--------------------------------
					List<TPaymentInfoVo> tempList = new ArrayList<>();

					String importSuccessKey = user.getId() + CommonConstants.DOWN_LINE_STRING + CommonConstants.PAYMENT_SOCIAL_WAIT_EXPORT;

					AtomicInteger atomicLine = new AtomicInteger(CommonConstants.ZERO_INT);
					List<CompletableFuture<List<ErrorMessage>>> completableFutureList = new ArrayList<>();

					// 创建一个数值格式化对象
					NumberFormat numberFormat = NumberFormat.getInstance();
					// 设置精确到小数点后2位
					numberFormat.setMaximumFractionDigits(2);
					String maerialRatio = "";
					String sumNumKey = user.getId() + CommonConstants.DOWN_LINE_STRING + "sum_key";
					Integer bfInt = (Integer) redisUtil.get(sumNumKey);
					if (Common.isNotNull(bfInt)) {
						if (bfInt <= rowNumber.intValue()) {
							maerialRatio = numberFormat.format((float) bfInt.intValue() / (float) rowNumber.intValue() * 100);
							bfInt = bfInt + 100;
						} else {
							maerialRatio = "100";
						}
					}else {
						if (100 <= rowNumber.intValue()) {
							maerialRatio = numberFormat.format((float) 100 / (float) rowNumber.intValue() * 100);
							bfInt = 100;
						} else {
							maerialRatio = "100";
						}
					}
					redisUtil.set(sumNumKey, bfInt, 360L);
					redisUtil.set(importSuccessKey, maerialRatio, 360L);

					try {
						// 1.list.size()不足partSize，直接执行
						if (list.size() < partSize) {
							CompletableFuture<List<ErrorMessage>> listCompletableFuture = CompletableFuture.supplyAsync(() ->
									executeImportSocialList(user, atomicLine, list, areaMap, areaMap2,
											paymentInfoPensionMap, paymentInfoBigMap, paymentInfoBirMap,
											paymentInfoMedicalMap, paymentInfoUnEmpMap, paymentInfoInjuryMap,
											CommonConstants.ONE_INT, errorMessageList), yfSocialImportThreadPoolExecutor);
							completableFutureList.add(listCompletableFuture);
						}

						// 2.list.size()大于partSize，循环分批执行
						int lastIdx = 0;
						if (partSize <= list.size()) {
							// 处理第一个0位元素
							final List<TPaymentInfoVo> finalList = list.subList(0, 1);
							CompletableFuture<List<ErrorMessage>> listCompletableFuture = CompletableFuture.supplyAsync(() ->
									executeImportSocialList(user, atomicLine, finalList, areaMap, areaMap2,
											paymentInfoPensionMap, paymentInfoBigMap, paymentInfoBirMap,
											paymentInfoMedicalMap, paymentInfoUnEmpMap, paymentInfoInjuryMap,
											CommonConstants.ONE_INT, errorMessageList), yfSocialImportThreadPoolExecutor);
							completableFutureList.add(listCompletableFuture);

							lastIdx = 1;
							for (int i = 1; i < list.size(); i++) {
								// partSize数量的list为一个执行单元
								tempList.add(list.get(i));
								if (i % partSize == 0) {
									lastIdx = i;
									List<TPaymentInfoVo> finalTempList = tempList;
									final int idx = i - partSize + 2;
									CompletableFuture.supplyAsync(() ->
											executeImportSocialList(user, atomicLine, finalTempList, areaMap, areaMap2,
													paymentInfoPensionMap, paymentInfoBigMap, paymentInfoBirMap,
													paymentInfoMedicalMap, paymentInfoUnEmpMap, paymentInfoInjuryMap,
													idx, errorMessageList), yfSocialImportThreadPoolExecutor);
									tempList = new ArrayList<>();
								}
							}
						}

						// 3.第2种方式执行完后，有余数的情况下，执行3
						if (lastIdx != 0) {
							tempList = new ArrayList<>();
							if (lastIdx == 1) {
								for (int i = lastIdx; i < list.size(); i++) {
									tempList.add(list.get(i));
								}
							} else {
								for (int i = lastIdx + 1; i < list.size(); i++) {
									tempList.add(list.get(i));
								}
								lastIdx += 1;
							}

							List<TPaymentInfoVo> finalTempList = tempList;
							int finalLastIdx = lastIdx + 1;
							CompletableFuture<List<ErrorMessage>> listCompletableFuture = CompletableFuture.supplyAsync(() ->
									executeImportSocialList(user, atomicLine, finalTempList, areaMap, areaMap2,
											paymentInfoPensionMap, paymentInfoBigMap, paymentInfoBirMap,
											paymentInfoMedicalMap, paymentInfoUnEmpMap, paymentInfoInjuryMap,
											finalLastIdx, errorMessageList), yfSocialImportThreadPoolExecutor);
							completableFutureList.add(listCompletableFuture);
						}

						// 阻塞当前线程，等待所有的线程执行完毕
						boolean computeFlag;
						do {
							computeFlag = false;
							for (CompletableFuture<List<ErrorMessage>> listCompletableFuture : completableFutureList) {
								if (!listCompletableFuture.isDone()) {
									computeFlag = true;
								}
							}
						} while (computeFlag);

					} catch (Exception e) {
			 			log.error("社保缴费库数据批量导入异常：" + e);
						redisUtil.remove(key);
					} finally {
						paymentInfoPensionMap.clear();
						paymentInfoBigMap.clear();
						paymentInfoBirMap.clear();
						paymentInfoMedicalMap.clear();
						paymentInfoUnEmpMap.clear();
						paymentInfoInjuryMap.clear();
					}
					// -----------------------------线程池处理list，批量保存社保信息结束--------------------------------
					//最后一个推送
					log.info("社保批量导入耗时:{}", (System.currentTimeMillis() - start) + "");
					redisUtil.remove(key);
				}
			}
		}
	}

	/**
	 * 转换缴纳地址为省市县ID
	 **/
	private TPaymentInfoVo initAddress(String[] areaArray, HashMap<String, String> areaMap2, TPaymentInfoVo s) {
		if (null == areaArray || null == areaMap2 || null == s) {
			return s;
		}
		String temp = null;
		if (areaArray.length >= CommonConstants.TWO_INT) {
			temp = areaMap2.get(areaArray[0] + CommonConstants.DOWN_LINE_STRING + "null");
			if (Common.isNotNull(temp)) {
				s.setSocialProvince(temp);
			}
			temp = areaMap2.get(areaArray[1] + CommonConstants.DOWN_LINE_STRING + (null == s.getSocialProvince()
					? "null" : s.getSocialProvince()));
			if (Common.isNotNull(temp)) {
				s.setSocialCity(temp);
			}
			if (areaArray.length >= CommonConstants.THREE_INT) {
				temp = areaMap2.get(areaArray[2] + CommonConstants.DOWN_LINE_STRING + (null == s.getSocialCity()
						? "null" : s.getSocialCity()));
				if (Common.isNotNull(temp)) {
					s.setSocialTown(temp);
				}
			}
		}
		return s;
	}

	/**
	 * @param user
	 * @param atomicLine
	 * @param list
	 * @param areaMap
	 * @param areaMap2
	 * @param paymentInfoPensionMap
	 * @param idx
	 * @description:
	 * @author: huyc
	 * @date: 2022/7/25
	 * @return:
	 */
	private List<ErrorMessage> executeImportSocialList(YifuUser user,
										 AtomicInteger atomicLine,
										 List<TPaymentInfoVo> list,
										 HashMap<String, String> areaMap,
										 HashMap<String, String> areaMap2,
										 ConcurrentHashMap<String, TPaymentInfo> paymentInfoPensionMap,
										 ConcurrentHashMap<String, TPaymentInfo> paymentInfoBigMap,
										 ConcurrentHashMap<String, TPaymentInfo> paymentInfoBirMap,
										 ConcurrentHashMap<String, TPaymentInfo> paymentInfoMedicalMap,
										 ConcurrentHashMap<String, TPaymentInfo> paymentInfoUnEmpMap,
										 ConcurrentHashMap<String, TPaymentInfo> paymentInfoInjuryMap,
										 int idx, List<ErrorMessage> errorMessageList) {
		int i = idx;
		if (Common.isNotNull(list)) {
			TPaymentInfo paymentInfo = null;
			TSocialFundInfo socialInfo = null;
			String temp = null;
			int res = -1;

			List<TSocialFundInfo> socialInfoList = socialFundInfoMapper.selectList(Wrappers.<TSocialFundInfo>query().lambda()
					.in(TSocialFundInfo::getEmpIdcard, Common.listObjectToStrList(list, ExcelAttributeConstants.EMPIDCARD)));

			//已存在的社保数据
			HashMap<String, TSocialFundInfo> socialMap = new HashMap<>();
			if (Common.isNotNull(socialInfoList)) {
				for (TSocialFundInfo tSocialInfo : socialInfoList) {
					socialMap.put(tSocialInfo.getEmpIdcard()
							+ CommonConstants.DOWN_LINE_STRING + tSocialInfo.getSocialProvince()
							+ CommonConstants.DOWN_LINE_STRING + tSocialInfo.getSocialCity()
							+ CommonConstants.DOWN_LINE_STRING + (null == tSocialInfo.getSocialTown()
							? "null" : tSocialInfo.getSocialTown()), tSocialInfo);
				}
			}

			String[] areaArray;
			String checkRes;

			for (TPaymentInfoVo infoVo : list) {
				atomicLine.incrementAndGet();
				++i;
				if (Common.isNotNull(infoVo.getEmpIdcard())) {
					infoVo.setEmpIdcard(infoVo.getEmpIdcard().replace("x", "X"));
				}
				//导入校验
				if (!Common.isNotNull(infoVo.getSocialPayMonth())) {
					errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "社保缴纳月份不可为空！"));
					continue;
				}
				if (!Common.isNotNull(infoVo.getSocialCreateMonth())) {
					errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "社保生成月份不可为空！"));
					continue;
				}
				if (!Common.isNotNull(infoVo.getSocialPayAddr())) {
					errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "社保缴纳地不可为空！"));
					continue;
				}
				//无对应员工的社保数据
				if (null != socialMap) {
					//采用客服导入的地址
					areaArray = infoVo.getSocialPayAddr().split(CommonConstants.CENTER_SPLIT_LINE_STRING);
					infoVo = initAddress(areaArray, areaMap2, infoVo);
					socialInfo = socialMap.get(infoVo.getEmpIdcard()
							+ CommonConstants.DOWN_LINE_STRING + infoVo.getSocialProvince()
							+ CommonConstants.DOWN_LINE_STRING + infoVo.getSocialCity()
							+ CommonConstants.DOWN_LINE_STRING + (null == infoVo.getSocialTown()
							? "null" : infoVo.getSocialTown()));
					//对身份证与人员姓名的对应关系进行校验
					if (socialInfo != null && !socialInfo.getEmpName().equals(infoVo.getEmpName())) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "姓名与身份证信息不一致，请核实后再次尝试！"));
						continue;
					}
					if (socialInfo != null && socialInfo.getSocialStartDate() == null) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), infoVo.getEmpIdcard() + "的社保起缴日期为空！"));
						continue;
					}
				}
				if (null == socialInfo) {
					errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "无对应员工" +
							infoVo.getEmpIdcard() + "的社保数据(请查验社保缴纳地)"));
					continue;
				}
				//如果导入项目数大于办理成功项目数给提示，少于给进，有问题自行删除后导入
				checkRes = checkRepeatInfo(socialInfo, infoVo);
				if (null != checkRes) {
					errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), checkRes));
					continue;
				}

				//查看缴纳月是否为空
				if (!Common.isNotNull(infoVo.getSocialPayMonth())) {
					errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "必填项校验失败：社保缴纳月份不可为空！"));
					continue;
				}

				//判断是否存在养老缴费数据
				if (BigDecimalUtils.safeAdd(BigDecimalUtils.isNullToZero(infoVo.getUnitPensionMoney()),
						BigDecimalUtils.isNullToZero(infoVo.getPersonalPensionMoney())).compareTo(BigDecimal.ZERO) != 0) {
					paymentInfo = paymentInfoPensionMap.get(infoVo.getSocialPayAddr()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getEmpIdcard().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialPayMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialCreateMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ SocialConstants.YL);
				}
				//存在社保缴费数据
				if (null != paymentInfo && BigDecimalUtils.safeMultiply(BigDecimalUtils.safeAdd(
						BigDecimalUtils.isNullToZero(infoVo.getUnitPensionMoney()),
						BigDecimalUtils.isNullToZero(infoVo.getPersonalPensionMoney())), BigDecimalUtils.safeAdd(
						BigDecimalUtils.isNullToZero(paymentInfo.getUnitPensionMoney()),
						BigDecimalUtils.isNullToZero(paymentInfo.getPersonalPensionMoney()))).compareTo(BigDecimal.ZERO) > 0) {
					errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "已存在对应员工身份证" + infoVo.getEmpIdcard()
							+ "的养老缴费数据,请勿重复导入！"));
					continue;
				} else {
					paymentInfo = null;
				}

				//判断是否存在失业缴费数据
				if (BigDecimalUtils.safeAdd(BigDecimalUtils.isNullToZero(infoVo.getUnitUnemploymentMoney()),
						BigDecimalUtils.isNullToZero(infoVo.getPersonalUnemploymentMoney())).compareTo(BigDecimal.ZERO) != 0) {
					paymentInfo = paymentInfoUnEmpMap.get(infoVo.getSocialPayAddr()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getEmpIdcard().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialPayMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialCreateMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ SocialConstants.SY);
				}
				//存在社保缴费数据
				if (null != paymentInfo && BigDecimalUtils.safeMultiply(BigDecimalUtils.safeAdd(
						BigDecimalUtils.isNullToZero(infoVo.getUnitUnemploymentMoney()),
						BigDecimalUtils.isNullToZero(infoVo.getPersonalUnemploymentMoney())), BigDecimalUtils.safeAdd(
						BigDecimalUtils.isNullToZero(paymentInfo.getUnitUnemploymentMoney()),
						BigDecimalUtils.isNullToZero(paymentInfo.getPersonalUnemploymentMoney()))).compareTo(BigDecimal.ZERO) > 0) {
					errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "已存在对应员工身份证" + infoVo.getEmpIdcard()
							+ "的失业缴费数据,请勿重复导入！"));
					continue;
				} else {
					paymentInfo = null;
				}

				//判断是否存在医保缴费数据
				if (BigDecimalUtils.safeAdd(BigDecimalUtils.isNullToZero(infoVo.getUnitMedicalMoney()),
						BigDecimalUtils.isNullToZero(infoVo.getPersonalMedicalMoney())).compareTo(BigDecimal.ZERO) != 0) {
					paymentInfo = paymentInfoMedicalMap.get(infoVo.getSocialPayAddr()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getEmpIdcard().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialPayMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialCreateMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ SocialConstants.YB);
				}
				//存在社保缴费数据
				if (null != paymentInfo && BigDecimalUtils.safeMultiply(BigDecimalUtils.safeAdd(
						BigDecimalUtils.isNullToZero(infoVo.getUnitMedicalMoney()),
						BigDecimalUtils.isNullToZero(infoVo.getPersonalMedicalMoney())), BigDecimalUtils.safeAdd(
						BigDecimalUtils.isNullToZero(paymentInfo.getUnitMedicalMoney()),
						BigDecimalUtils.isNullToZero(paymentInfo.getPersonalMedicalMoney()))).compareTo(BigDecimal.ZERO) > 0) {
					errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "已存在对应员工身份证" + infoVo.getEmpIdcard()
							+ "的医保缴费数据,请勿重复导入！"));
					continue;
				} else {
					paymentInfo = null;
				}

				//判断是否存在医疗救助金缴费数据
				if (BigDecimalUtils.safeAdd(BigDecimalUtils.isNullToZero(infoVo.getPersonalBigmailmentMoney()),
						BigDecimalUtils.isNullToZero(infoVo.getUnitBigmailmentMoney())).compareTo(BigDecimal.ZERO) != 0) {
					paymentInfo = paymentInfoBigMap.get(infoVo.getSocialPayAddr()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getEmpIdcard().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialPayMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialCreateMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ SocialConstants.DB);
				}
				//存在社保缴费数据
				if (null != paymentInfo && BigDecimalUtils.safeMultiply(BigDecimalUtils.safeAdd(
						BigDecimalUtils.isNullToZero(infoVo.getUnitBigmailmentMoney()),
						BigDecimalUtils.isNullToZero(infoVo.getPersonalBigmailmentMoney())), BigDecimalUtils.safeAdd(
						BigDecimalUtils.isNullToZero(paymentInfo.getUnitBigmailmentMoney()),
						BigDecimalUtils.isNullToZero(paymentInfo.getPersonalBigmailmentMoney()))).compareTo(BigDecimal.ZERO) > 0) {
					errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "已存在对应员工身份证" + infoVo.getEmpIdcard()
							+ "的医疗救助金缴费数据,请勿重复导入！"));
					continue;
				} else {
					paymentInfo = null;
				}

				//判断是否存在工伤缴费数据
				if (BigDecimalUtils.isNullToZero(infoVo.getUnitInjuryMoney()).compareTo(BigDecimal.ZERO) != 0) {
					paymentInfo = paymentInfoInjuryMap.get(infoVo.getSocialPayAddr()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getEmpIdcard().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialPayMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialCreateMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ SocialConstants.GS);
				}
				//存在社保缴费数据
				if (null != paymentInfo && BigDecimalUtils.safeMultiply(BigDecimalUtils.isNullToZero(infoVo.getUnitInjuryMoney()),
						BigDecimalUtils.isNullToZero(paymentInfo.getUnitInjuryMoney())).compareTo(BigDecimal.ZERO) > 0) {
					errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "已存在对应员工身份证" + infoVo.getEmpIdcard()
							+ "的单位工伤缴费数据,请勿重复导入！"));
					continue;
				} else {
					paymentInfo = null;
				}

				//判断是否存在生育缴费数据
				if (BigDecimalUtils.isNullToZero(infoVo.getUnitBirthMoney()).compareTo(BigDecimal.ZERO) != 0) {
					paymentInfo = paymentInfoBirMap.get(infoVo.getSocialPayAddr()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getEmpIdcard().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialPayMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialCreateMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ SocialConstants.BIR);
				}
				//存在社保缴费数据
				if (null != paymentInfo && BigDecimalUtils.safeMultiply(BigDecimalUtils.isNullToZero(infoVo.getUnitBirthMoney()),
						BigDecimalUtils.isNullToZero(paymentInfo.getUnitBirthMoney())).compareTo(BigDecimal.ZERO) > 0) {
					errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "已存在对应员工身份证" + infoVo.getEmpIdcard()
							+ "的单位生育缴费数据,请勿重复导入！"));
					continue;
				} else {
					paymentInfo = null;
				}

				TSocialInfo tSocialInfo = socialInfoMapper.selectOne(Wrappers.<TSocialInfo>query().lambda()
						.eq(TSocialInfo::getEmpIdcard, infoVo.getEmpIdcard()).groupBy(TSocialInfo::getCreateTime)
						.last(CommonConstants.LAST_ONE_SQL));

				if (null == paymentInfo) {
					paymentInfo = new TPaymentInfo();
					paymentInfo.setLockStatus(CommonConstants.ZERO_STRING);
					paymentInfo.setEmpId(socialInfo.getEmpId());
					paymentInfo.setEmpIdcard(socialInfo.getEmpIdcard());
					paymentInfo.setEmpName(socialInfo.getEmpName());
					paymentInfo.setSettleDomainId(socialInfo.getSettleDomainName());
					paymentInfo.setUnitId(socialInfo.getUnitName());
					if (Common.isNotNull(tSocialInfo)) {
						paymentInfo.setInauguralTeam(tSocialInfo.getInauguralTeam());
						paymentInfo.setEmpNo(tSocialInfo.getEmpNo());
					}
				}
				paymentInfo.setSocialProvince(socialInfo.getSocialProvince());
				paymentInfo.setSocialCity(socialInfo.getSocialCity());
				paymentInfo.setSocialTown(socialInfo.getSocialTown());


				paymentInfo.setSocialHousehold(socialInfo.getSocialHouseholdName());
				paymentInfo.setSocialPayMonth(infoVo.getSocialPayMonth());

				if (null != paymentInfo.getSocialProvince()) {
					temp = areaMap.get(paymentInfo.getSocialProvince());
					if (Common.isNotNull(temp)) {
						paymentInfo.setSocialPayAddr(temp);
					}
				}
				if (null != paymentInfo.getSocialCity()) {
					temp = areaMap.get(paymentInfo.getSocialCity());
					if (Common.isNotNull(temp)) {
						paymentInfo.setSocialPayAddr(paymentInfo.getSocialPayAddr() + CommonConstants.CENTER_SPLIT_LINE_STRING + temp);
					}
				}
				if (null != paymentInfo.getSocialTown()) {
					temp = areaMap.get(paymentInfo.getSocialTown());
					if (Common.isNotNull(temp)) {
						paymentInfo.setSocialPayAddr(paymentInfo.getSocialPayAddr() + CommonConstants.CENTER_SPLIT_LINE_STRING + temp);
					}
				}
				paymentInfo.setSocialSecurityNo(infoVo.getSocialSecurityNo());
				paymentInfo.setSocialCreateMonth(infoVo.getSocialCreateMonth());
				paymentInfo.setUnitPensionMoney(infoVo.getUnitPensionMoney());
				paymentInfo.setUnitMedicalMoney(infoVo.getUnitMedicalMoney());
				paymentInfo.setUnitBirthMoney(null == infoVo.getUnitBirthMoney() ? BigDecimal.ZERO : infoVo.getUnitBirthMoney());
				paymentInfo.setUnitBigmailmentMoney(infoVo.getUnitBigmailmentMoney());
				paymentInfo.setUnitInjuryMoney(infoVo.getUnitInjuryMoney());
				paymentInfo.setUnitUnemploymentMoney(infoVo.getUnitUnemploymentMoney());
				paymentInfo.setPersonalBigmailmentMoney(infoVo.getPersonalBigmailmentMoney());
				paymentInfo.setPersonalUnemploymentMoney(infoVo.getPersonalUnemploymentMoney());
				paymentInfo.setPersonalPensionMoney(infoVo.getPersonalPensionMoney());
				paymentInfo.setPersonalMedicalMoney(null == infoVo.getPersonalMedicalMoney()
						? BigDecimal.ZERO : infoVo.getPersonalMedicalMoney());
				paymentInfo.setUnitPensionSet(infoVo.getUnitPensionSet());
				paymentInfo.setUnitPensionPer(infoVo.getUnitPensionPer());
				paymentInfo.setUnitMedicalSet(infoVo.getUnitMedicalSet());
				paymentInfo.setUnitMedicalPer(infoVo.getUnitMedicalPer());
				paymentInfo.setUnitBirthSet(null == infoVo.getUnitBirthSet() ? BigDecimal.ZERO : infoVo.getUnitBirthSet());
				paymentInfo.setUnitBirthPer(infoVo.getUnitBirthPer());
				paymentInfo.setUnitInjurySet(infoVo.getUnitInjurySet());
				paymentInfo.setUnitInjuryPer(infoVo.getUnitInjuryPer());
				paymentInfo.setUnitUnemploymentSet(infoVo.getUnitUnemploymentSet());
				paymentInfo.setUnitUnemploymentPer(infoVo.getUnitUnemploymentPer());
				paymentInfo.setUnitBigailmentPer(infoVo.getUnitBigailmentPer());
				paymentInfo.setPersonalPensionPer(infoVo.getPersonalPensionPer());
				paymentInfo.setPersonalMedicalPer(infoVo.getPersonalMedicalPer());
				paymentInfo.setPersonalUnemploymentPer(infoVo.getPersonalUnemploymentPer());
				paymentInfo.setPersonalBigailmentPer(infoVo.getPersonalBigailmentPer());
				paymentInfo.setCompanyAccrual(infoVo.getCompanyAccrual());
				paymentInfo.setPersonalAccrual(infoVo.getPersonalAccrual());
				paymentInfo.setUnitSocialSum(BigDecimalUtils.safeAdd(paymentInfo.getUnitPensionMoney()
						, paymentInfo.getUnitMedicalMoney(), paymentInfo.getUnitBirthMoney()
						, paymentInfo.getUnitInjuryMoney()
						, paymentInfo.getUnitUnemploymentMoney()
						, paymentInfo.getUnitBigmailmentMoney()
						, paymentInfo.getCompanyAccrual()));
				paymentInfo.setSocialSecurityPersonalSum(BigDecimalUtils.safeAdd(paymentInfo.getPersonalBigmailmentMoney()
						, paymentInfo.getPersonalPensionMoney()
						, paymentInfo.getPersonalMedicalMoney()
						, paymentInfo.getPersonalUnemploymentMoney()
						, paymentInfo.getPersonalAccrual()));
				paymentInfo.setSocialSum(BigDecimalUtils.safeAdd(
						paymentInfo.getUnitSocialSum(), paymentInfo.getSocialSecurityPersonalSum()));
				paymentInfo.setSumAll(paymentInfo.getSocialSum());
				paymentInfo.setCreateBy(user.getId());
				paymentInfo.setCreateName(user.getNickname());
				if (null != paymentInfo && Common.isEmpty(paymentInfo.getSocialId())) {
					paymentInfo.setSocialId(UUID.randomUUID().toString());
				}
				if (null != paymentInfo && Common.isNotNull(paymentInfo.getId())) {
					res = baseMapper.updateById(paymentInfo);
					if (res < 0) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), infoVo.getEmpName() + "_缴费库更新失败！"));
						continue;
					}
				} else {
					res = insertAndSTimestamp(paymentInfo);
					if (res < 0) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), infoVo.getEmpName() + "_缴费库保存失败！"));
						continue;
					}
				}
			}
		}
		return errorMessageList;
	}

	private String concatStr(String socialType) {
		return "存在未办理成功的险种：" + socialType + ",请办理成功后，再行导入！";
	}

	/**
	 * 验证部分办理失败的导入是否有超出部分的数据导入
	 *
	 * @param socialInfo
	 * @param infoVo
	 * @return
	 * @Author fxj
	 * @Date 2021-06-21
	 **/
	private String checkRepeatInfo(TSocialFundInfo socialInfo, TPaymentInfoVo infoVo) {
		if (checkHandleMoney(socialInfo.getPensionHandle(),
				infoVo.getUnitPensionMoney(), infoVo.getPersonalPensionMoney())) {
			return concatStr("养老");
		}
		if (checkHandleMoney(socialInfo.getMedicalHandle(),
				infoVo.getUnitMedicalMoney(), infoVo.getPersonalMedicalMoney())) {
			return concatStr("医疗");
		}
		if (checkHandleMoney(socialInfo.getUnemployHandle(),
				infoVo.getUnitUnemploymentMoney(), infoVo.getPersonalUnemploymentMoney())) {
			return concatStr("失业");
		}
		if (checkHandleMoney(socialInfo.getWorkInjuryHandle(),
				infoVo.getUnitInjuryMoney(), BigDecimal.ZERO)) {
			return concatStr("工伤");
		}
		if (checkHandleMoney(socialInfo.getBirthHandle(),
				infoVo.getUnitBirthMoney(), BigDecimal.ZERO)) {
			return concatStr("生育");
		}
		if (CommonConstants.ONE_STRING.equals(socialInfo.getIsIllness()) &&
				(Common.isBlankToBigDecimalZero(infoVo.getUnitBigmailmentMoney()).compareTo(
						BigDecimal.ZERO) > CommonConstants.ZERO_INT ||
						Common.isBlankToBigDecimalZero(infoVo.getPersonalBigmailmentMoney()).compareTo(
								BigDecimal.ZERO) > CommonConstants.ZERO_INT)) {
			return "无大病，无需导入大病！";
		}
		if (checkHandleMoney(socialInfo.getBigailmentHandle(),
				infoVo.getUnitBigmailmentMoney(), infoVo.getPersonalBigmailmentMoney())) {
			return concatStr("大病");
		}
		return null;
	}

	private boolean checkHandleMoney(String handleStatus, BigDecimal unitMoney, BigDecimal personalMoney) {
		return CommonConstants.TWO_STRING.equals(handleStatus) &&
				(Common.isBlankToBigDecimalZero(unitMoney).compareTo(BigDecimal.ZERO) > CommonConstants.ZERO_INT ||
						Common.isBlankToBigDecimalZero(personalMoney).compareTo(BigDecimal.ZERO) > CommonConstants.ZERO_INT);
	}

	/**
	 * 拼接排序字段并插入
	 *
	 * @param entity
	 * @return
	 * @Author huyc
	 * @Date 2022-07-25
	 **/
	private int insertAndSTimestamp(TPaymentInfo entity) {
		if (Common.isNotNull(entity.getSocialCreateMonth()) && Common.isNotNull(entity.getSocialPayMonth())) {
			entity.setSortTime(entity.getSocialCreateMonth().concat(entity.getSocialPayMonth()).concat(
					String.valueOf(System.currentTimeMillis())));
		}
		return baseMapper.insert(entity);
	}

	@Override
	public R<List<ErrorMessage>> importSocialHeFeiDiy(InputStream inputStream, String random, String type) {
		YifuUser user = SecurityUtils.getUser();
		List<ErrorMessage> errorMessageList = new ArrayList<>();
		ExcelUtil<TPaymentHeFeiVo> util1 = new ExcelUtil<>(TPaymentHeFeiVo.class);
		// 写法2：
		// 匿名内部类 不用额外写一个DemoDataListener
		// 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
		try {
			EasyExcel.read(inputStream, TPaymentHeFeiVo.class, new ReadListener<TPaymentHeFeiVo>() {
				/**
				 * 单次缓存的数据量
				 */
				public static final int BATCH_COUNT = CommonConstants.BATCH_COUNT;
				Integer rowNumber;
				/**
				 *临时存储
				 */
				private List<TPaymentHeFeiVo> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

				@Override
				public void invoke(TPaymentHeFeiVo data, AnalysisContext context) {
					// 获取总行数（含表头）
					rowNumber = context.readSheetHolder().getApproximateTotalRowNumber();

					ReadRowHolder readRowHolder = context.readRowHolder();
					Integer rowIndex = readRowHolder.getRowIndex();
					data.setRowIndex(rowIndex + 1);
					ErrorMessage errorMessage = util1.checkEntity(data, data.getRowIndex());
					if (Common.isNotNull(errorMessage)) {
						errorMessageList.add(errorMessage);
					} else {
						cachedDataList.add(data);
					}
					if (cachedDataList.size() >= BATCH_COUNT) {
						saveData();
						// 存储完成清理 list
						cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
					}
				}

				@Override
				public void doAfterAllAnalysed(AnalysisContext context) {
					saveData();
				}

				/**
				 * 加上存储数据库
				 */
				private void saveData() {
					log.info("{}条数据，开始存储数据库！", cachedDataList.size());
					importTPaymentSocialHeFei(cachedDataList, errorMessageList, random, user, type,rowNumber);
					log.info("存储数据库成功！");
				}
			}).sheet().doRead();
		} catch (Exception e) {
			log.error(CommonConstants.IMPORT_DATA_ANALYSIS_ERROR, e);
			return R.failed(CommonConstants.IMPORT_DATA_ANALYSIS_ERROR);
		}
		return R.ok(errorMessageList);
	}

	@Override
	public R<List<ErrorMessage>> batchImportPaymentFundInfo(InputStream inputStream) {
		YifuUser user = SecurityUtils.getUser();
		List<ErrorMessage> errorMessageList = new ArrayList<>();
		ExcelUtil<TPaymentInfoVo> util1 = new ExcelUtil<>(TPaymentInfoVo.class);
		// 写法2：
		// 匿名内部类 不用额外写一个DemoDataListener
		// 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
		try {
			EasyExcel.read(inputStream, TPaymentInfoVo.class, new ReadListener<TPaymentInfoVo>() {
				/**
				 * 单次缓存的数据量
				 */
				public static final int BATCH_COUNT = CommonConstants.BATCH_COUNT;
				/**
				 *临时存储
				 */
				private List<TPaymentInfoVo> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

				@Override
				public void invoke(TPaymentInfoVo data, AnalysisContext context) {
					ReadRowHolder readRowHolder = context.readRowHolder();
					Integer rowIndex = readRowHolder.getRowIndex();
					data.setRowIndex(rowIndex + 1);
					ErrorMessage errorMessage = util1.checkEntity(data, data.getRowIndex());
					if (Common.isNotNull(errorMessage)) {
						errorMessageList.add(errorMessage);
					} else {
						cachedDataList.add(data);
					}
					if (cachedDataList.size() >= BATCH_COUNT) {
						saveData();
						// 存储完成清理 list
						cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
					}
				}

				@Override
				public void doAfterAllAnalysed(AnalysisContext context) {
					saveData();
				}

				/**
				 * 加上存储数据库
				 */
				private void saveData() {
					log.info("{}条数据，开始存储数据库！", cachedDataList.size());
					batchSavePaymentFundInfo(cachedDataList, errorMessageList, user);
					log.info("存储数据库成功！");
				}
			}).sheet().doRead();
		} catch (Exception e) {
			log.error(CommonConstants.IMPORT_DATA_ANALYSIS_ERROR, e);
			return R.failed(CommonConstants.IMPORT_DATA_ANALYSIS_ERROR);
		}
		return R.ok(errorMessageList);
	}

	private void batchSavePaymentFundInfo(List<TPaymentInfoVo> list, List<ErrorMessage> errorMessageList, YifuUser user) {

		HashMap<String, String> areaMap = new HashMap<String, String>();
		R<AreaVo> areaListR = upmsDaprUtils.getAreaListR();
		if (Common.isNotNull(areaListR)) {
			AreaVo areaList = areaListR.getData();
			if (null != areaList && !areaList.getSysAreaList().isEmpty()) {
				for (SysArea area : areaList.getSysAreaList()) {
					areaMap.put(Integer.toString(area.getId()), area.getAreaName());
				}
			}
		}
		try {
			if (Common.isNotNull(list)) {
				TPaymentInfo paymentInfo = null;
				TSocialFundInfo fund = null;
				String temp = null;
				int res = -1;

				//获取身份证列表
				List<String> idcards = list.stream().map(TPaymentInfoVo::getEmpIdcard).collect(Collectors.toList());
				//已存在档案数据
				//已存在派增公积金数据
				List<TSocialFundInfo> fundList = socialFundInfoMapper.selectList(Wrappers.<TSocialFundInfo>query().lambda()
						.in(TSocialFundInfo::getEmpIdcard, Common.listObjectToStrList(list, ExcelAttributeConstants.EMPIDCARD)));
				HashMap<String, Object> fundMap = fundList.stream().collect(HashMap::new, (m, v) ->
						m.put(v.getEmpIdcard(), v), HashMap::putAll);

				List<String> monthList = list.stream().map(TPaymentInfoVo::getProvidentPayMonth).collect(Collectors.toList());

				//已存在社保缴费库数据  非删除状态
				List<TPaymentInfo> paymentInfos = baseMapper.selectListForPaymentImport(monthList, idcards);
				//公积金唯一性检验条件：身份证编码+公积金缴纳月份
				HashMap<String, Object> paymentInfoMap = new HashMap<>();
				if (Common.isNotNull(paymentInfos)) {
					for (TPaymentInfo info : paymentInfos) {
						paymentInfoMap.put(info.getProvidentPayAddr()
										+ CommonConstants.DOWN_LINE_STRING
										+ info.getEmpIdcard()
										+ CommonConstants.DOWN_LINE_STRING
										+ info.getProvidentPayMonth()
										+ CommonConstants.DOWN_LINE_STRING
										+ info.getProvidentCreateMonth()
										+ CommonConstants.DOWN_LINE_STRING
										+ BigDecimalUtils.safeAdd(info.getProvidentSum()).doubleValue()
								, info);
					}
				}
				String[] areaArray = null;

				for (TPaymentInfoVo infoVo : list) {
					if (Common.isNotNull(infoVo.getEmpIdcard())) {
						infoVo.setEmpIdcard(infoVo.getEmpIdcard().replace("x", "X"));
					}
					//检查数据规范性
					if (!Common.isNotNull(infoVo.getEmpIdcard())) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), infoVo.getEmpName()
								+ CommonConstants.DOWN_LINE_STRING + "员工身份证不可为空！"));
						continue;
					}
					if (!Common.isNotNull(infoVo.getProvidentPayMonth())) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), infoVo.getEmpName()
								+ CommonConstants.DOWN_LINE_STRING + "公积金缴纳月份不可为空！"));
						continue;
					}
					if (!Common.isNotNull(infoVo.getProvidentCreateMonth())) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), infoVo.getEmpName()
								+ CommonConstants.DOWN_LINE_STRING + "公积金生成月份不可为空！"));
						continue;
					}
					if (!Common.isNotNull(infoVo.getProvidentPercent())) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), infoVo.getEmpName()
								+ CommonConstants.DOWN_LINE_STRING + "公积金单边比例不可为空！"));
						continue;
					}
					if (!Common.isNotNull(infoVo.getUnitProvidentSet())) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), infoVo.getEmpName()
								+ CommonConstants.DOWN_LINE_STRING + "公积金单边基数不可为空！"));
						continue;
					}
					if (!Common.isNotNull(infoVo.getProvidentPayAddr())) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), infoVo.getEmpName()
								+ CommonConstants.DOWN_LINE_STRING + "公积金缴纳地不可为空！"));
						continue;
					}
					if (fundMap.size() >CommonConstants.ZERO_INT) {
						fund = (TSocialFundInfo) fundMap.get(infoVo.getEmpIdcard());
						//对身份证与人员姓名的对应关系进行校验
						if (null != fund && !fund.getEmpName().equals(infoVo.getEmpName())) {
							errorMessageList.add
									(new ErrorMessage(infoVo.getRowIndex(), "姓名与身份证信息不一致，请核实后再次尝试！"));
							continue;
						}
						areaArray = infoVo.getProvidentPayAddr().split(CommonConstants.CENTER_SPLIT_LINE_STRING);
						fund = checkAddress(areaArray, areaMap, fund);
						//无对应员工公积金数据
						if (null == fund) {
							errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "无对应员工" + infoVo.getEmpName() +
									CommonConstants.DOWN_LINE_STRING + infoVo.getEmpIdcard()
									+ "的公积金数据(请查验公积金缴纳地)！"));
							continue;
						}
						//查看是否有操作对应主体的权限
						if (!Common.isNotNull(infoVo.getProvidentPayMonth())) {
							errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), infoVo.getEmpName() +
									CommonConstants.DOWN_LINE_STRING + "必填项校验失败：公积金缴纳月份不可为空！"));
							continue;
						}
						//计算公积金合计，个人金额和单位金额一致
						infoVo.setProvidentSum(BigDecimalUtils.safeAdd(infoVo.getUnitProvidentSum(), infoVo.getUnitProvidentSum()));
						// 判重
						if (paymentInfoMap.size() >CommonConstants.ZERO_INT) {
							paymentInfo = (TPaymentInfo) paymentInfoMap.get(infoVo.getProvidentPayAddr()
									+ CommonConstants.DOWN_LINE_STRING
									+ infoVo.getEmpIdcard().trim()
									+ CommonConstants.DOWN_LINE_STRING
									+ infoVo.getProvidentPayMonth().trim()
									+ CommonConstants.DOWN_LINE_STRING
									+ infoVo.getProvidentCreateMonth().trim()
									+ CommonConstants.DOWN_LINE_STRING
									+ Math.round(BigDecimalUtils.safeAdd(infoVo.getProvidentSum()).doubleValue() * 100) / 100.0);
						}
						//存在公积金缴费数据
						if (null != paymentInfo && ((BigDecimalUtils.isNullToZero(paymentInfo.getProvidentSum()).compareTo(BigDecimal.ZERO) != 0)
								|| ((BigDecimalUtils.isNullToZero(paymentInfo.getSocialSum()).compareTo(BigDecimal.ZERO) == 0)
								&& BigDecimalUtils.isNullToZero(paymentInfo.getProvidentSum()).compareTo(BigDecimal.ZERO) == 0))) {

							//允许覆盖，但已核准表或工资结算或已锁定不允许覆盖
							errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "已存在对应员工身份证" +
									infoVo.getEmpName() + CommonConstants.DOWN_LINE_STRING + infoVo.getEmpIdcard()
									+ "公积金缴费数据，禁止覆盖！"));
							continue;

						} else {
							paymentInfo = null;
						}

						TProvidentFund tProvidentFund = providentFundMapper.selectOne(Wrappers.<TProvidentFund>query().lambda()
								.eq(TProvidentFund::getEmpIdcard, infoVo.getEmpIdcard()).groupBy(TProvidentFund::getCreateTime)
								.last(CommonConstants.LAST_ONE_SQL));
						if (null == paymentInfo) {
							paymentInfo = new TPaymentInfo();
							paymentInfo.setLockStatus(CommonConstants.ZERO_STRING);
							paymentInfo.setEmpId(fund.getEmpId());
							paymentInfo.setEmpIdcard(fund.getEmpIdcard());
							paymentInfo.setEmpName(fund.getEmpName());
							paymentInfo.setSettleDomainId(fund.getSettleDomainNameFund());
							paymentInfo.setUnitId(fund.getUnitNameFund());
							if (Common.isNotNull(tProvidentFund)) {
								paymentInfo.setInauguralTeam(tProvidentFund.getInauguralTeam());
								paymentInfo.setEmpNo(tProvidentFund.getEmpNo());
							}
						}
						paymentInfo.setProvidentPayMonth(infoVo.getProvidentPayMonth());
						paymentInfo.setFundProvince(fund.getFundProvince());
						paymentInfo.setFundCity(fund.getFundCity());
						paymentInfo.setFundTown(fund.getFundTown());
						paymentInfo.setProvidentHousehold(fund.getProvidentHouseholdName());
						if (null != paymentInfo.getFundProvince()) {
							temp = areaMap.get(paymentInfo.getFundProvince());
							if (Common.isNotNull(temp)) {
								paymentInfo.setProvidentPayAddr(temp);
							}
						}
						if (null != paymentInfo.getFundCity()) {
							temp = areaMap.get(paymentInfo.getFundCity());
							if (Common.isNotNull(temp)) {
								paymentInfo.setProvidentPayAddr(paymentInfo.getProvidentPayAddr()
										+ CommonConstants.CENTER_SPLIT_LINE_STRING + temp);
							}
						}
						if (null != paymentInfo.getFundTown()) {
							temp = areaMap.get(paymentInfo.getFundTown());
							if (Common.isNotNull(temp)) {
								paymentInfo.setProvidentPayAddr(paymentInfo.getProvidentPayAddr()
										+ CommonConstants.CENTER_SPLIT_LINE_STRING + temp);
							}
						}

						paymentInfo.setProvidentCreateMonth(infoVo.getProvidentCreateMonth());
						paymentInfo.setUnitProvidentSet(null == infoVo.getUnitProvidentSet() ? BigDecimal.ZERO : infoVo.getUnitProvidentSet());
						paymentInfo.setPersonalProidentSet(null == infoVo.getUnitProvidentSet() ? BigDecimal.ZERO : infoVo.getUnitProvidentSet());
						paymentInfo.setProvidentPercent(null == infoVo.getProvidentPercent() ? BigDecimal.ZERO : infoVo.getProvidentPercent());
						paymentInfo.setProvidentNo(infoVo.getProvidentNo());
						paymentInfo.setPersonalProvidentSum(null == infoVo.getUnitProvidentSum() ? BigDecimal.ZERO : infoVo.getUnitProvidentSum());
						paymentInfo.setUnitProvidentSum(null == infoVo.getUnitProvidentSum() ? BigDecimal.ZERO : infoVo.getUnitProvidentSum());
						paymentInfo.setProvidentSum(BigDecimalUtils.safeAdd(paymentInfo.getUnitProvidentSum(), paymentInfo.getPersonalProvidentSum()));
						paymentInfo.setSumAll(paymentInfo.getProvidentSum());
						if (null != paymentInfo && Common.isEmpty(paymentInfo.getFundId())) {
							paymentInfo.setFundId(UUID.randomUUID().toString());
						}
						if (null != paymentInfo && Common.isNotNull(paymentInfo.getId())) {
							res = baseMapper.updateById(paymentInfo);
							if (res < 0) {
								errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), infoVo.getEmpName() +
										infoVo.getEmpName() + "_缴费库覆盖更新失败！"));
								continue;
							}
						} else {
							res = insertAndSTimestamp(paymentInfo);
							if (res < 0) {
								errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), infoVo.getEmpName()
										+ "_缴费库保存失败！"));
								continue;
							}
						}
						if (null != paymentInfo && Common.isNotNull(paymentInfo.getId()) && Common.isNotNull(paymentInfo.getFundId())) {
							paymentInfoMap.put(paymentInfo.getProvidentPayAddr()
									+ CommonConstants.DOWN_LINE_STRING
									+ paymentInfo.getEmpIdcard().trim()
									+ CommonConstants.DOWN_LINE_STRING
									+ paymentInfo.getProvidentPayMonth().trim()
									+ CommonConstants.DOWN_LINE_STRING
									+ paymentInfo.getProvidentCreateMonth().trim()
									+ CommonConstants.DOWN_LINE_STRING
									+ BigDecimalUtils.safeAdd(paymentInfo.getProvidentSum()).doubleValue(), paymentInfo);
						}
					}
				}
			} else {
				errorMessageList.add(new ErrorMessage(-1, "无数据可导入！"));
			}
		} catch (Exception e) {
			log.error("数据批量导入异常：" + e);
		}
	}

	private void importTPaymentSocialHeFei(List<TPaymentHeFeiVo> list, List<ErrorMessage> errorMessageList,
										   String random, YifuUser user, String type, Integer rowNumber) {

		HashMap<String, String> areaMap = new HashMap<String, String>();
		HashMap<String, String> areaMap2 = new HashMap<String, String>();
		R<AreaVo> areaListR = upmsDaprUtils.getAreaListR();
		if (Common.isNotNull(areaListR)) {
			AreaVo areaList = areaListR.getData();
			if (null != areaList && !areaList.getSysAreaList().isEmpty()) {
				for (SysArea area : areaList.getSysAreaList()) {
					areaMap.put(Integer.toString(area.getId()), area.getAreaName());
					areaMap2.put(area.getAreaName() + CommonConstants.DOWN_LINE_STRING + (null == area.getParentId() ?
							"null" : (0 == area.getParentId().intValue() ? "null" : Integer.toString(
							area.getParentId()))), Integer.toString(area.getId()));
				}
			}
		}

		// 将以下代码提取出来，使用paymentInfoMap提升性能，避免list多次循环查询数据库浪费性能
		// 导入前做队列空闲判断，防止队列不足出现数据丢失的场景
		if (CollUtil.isNotEmpty(list)) {
			int taskSize = list.size() / partSize + CommonConstants.ONE_INT;
			int residualCapacity = yfSocialImportThreadPoolExecutor.getResidualCapacity();
			if (taskSize > residualCapacity) {
				errorMessageList.add(new ErrorMessage(-1, MsgUtils.getMessage(ErrorCodes.SOCIALINFO_LIST_NUM_LARGE)));
			} else {
				// -----------------------------生成paymentInfoMap本段开始--------------------------------
				//已存在的档案数据
				List<String> monthList = Common.listObjectToStrList(list, ExcelAttributeConstants.SOCIAL_PAY_MONTH).stream().distinct().collect(Collectors.toList());
				//已存在社保缴费库数据  非删除状态
				List<TPaymentInfo> paymentInfos = baseMapper.selectListForPaymentImport(monthList,
						Common.listObjectToStrList(list, ExcelAttributeConstants.EMPIDCARD));
				//养老
				ConcurrentHashMap<String, TPaymentInfo> paymentInfoPensionMap = new ConcurrentHashMap<>();
				//大病救助金
				ConcurrentHashMap<String, TPaymentInfo> paymentInfoBigMap = new ConcurrentHashMap<>();
				//失业
				ConcurrentHashMap<String, TPaymentInfo> paymentInfoUnEmpMap = new ConcurrentHashMap<>();
				//工伤
				ConcurrentHashMap<String, TPaymentInfo> paymentInfoInjuryMap = new ConcurrentHashMap<>();
				//医保
				ConcurrentHashMap<String, TPaymentInfo> paymentInfoMedicalMap = new ConcurrentHashMap<>();
				if (Common.isNotNull(paymentInfos)) {
					for (TPaymentInfo info : paymentInfos) {

						if (BigDecimalUtils.safeAdd(BigDecimalUtils.isNullToZero(info.getUnitPensionMoney()),
								BigDecimalUtils.isNullToZero(info.getPersonalPensionMoney())).compareTo(BigDecimal.ZERO) != 0) {
							paymentInfoPensionMap.put(info.getSocialPayAddr()
											+ CommonConstants.DOWN_LINE_STRING
											+ info.getEmpIdcard()
											+ CommonConstants.DOWN_LINE_STRING
											+ info.getSocialPayMonth()
											+ CommonConstants.DOWN_LINE_STRING
											+ info.getSocialCreateMonth()
											+ CommonConstants.DOWN_LINE_STRING
											+ PaymentConstants.PENSION_RISK
									, info);
						}
						if (BigDecimalUtils.safeAdd(BigDecimalUtils.isNullToZero(info.getUnitUnemploymentMoney()),
								BigDecimalUtils.isNullToZero(info.getPersonalUnemploymentMoney())).compareTo(BigDecimal.ZERO) != 0) {
							paymentInfoUnEmpMap.put(info.getSocialPayAddr()
											+ CommonConstants.DOWN_LINE_STRING
											+ info.getEmpIdcard()
											+ CommonConstants.DOWN_LINE_STRING
											+ info.getSocialPayMonth()
											+ CommonConstants.DOWN_LINE_STRING
											+ info.getSocialCreateMonth()
											+ CommonConstants.DOWN_LINE_STRING
											+ PaymentConstants.UNEMPLOYEEMENT_RISK
									, info);
						}
						if (BigDecimalUtils.isNullToZero(info.getUnitInjuryMoney()).compareTo(BigDecimal.ZERO) != 0) {
							paymentInfoInjuryMap.put(info.getSocialPayAddr()
											+ CommonConstants.DOWN_LINE_STRING
											+ info.getEmpIdcard()
											+ CommonConstants.DOWN_LINE_STRING
											+ info.getSocialPayMonth()
											+ CommonConstants.DOWN_LINE_STRING
											+ info.getSocialCreateMonth()
											+ CommonConstants.DOWN_LINE_STRING
											+ PaymentConstants.INJURY_RISK
									, info);
						}
						if (BigDecimalUtils.safeAdd(BigDecimalUtils.isNullToZero(info.getUnitMedicalMoney()),
								BigDecimalUtils.isNullToZero(info.getPersonalMedicalMoney())).compareTo(BigDecimal.ZERO) != 0) {
							paymentInfoMedicalMap.put(info.getSocialPayAddr()
											+ CommonConstants.DOWN_LINE_STRING
											+ info.getEmpIdcard()
											+ CommonConstants.DOWN_LINE_STRING
											+ info.getSocialPayMonth()
											+ CommonConstants.DOWN_LINE_STRING
											+ info.getSocialCreateMonth()
											+ CommonConstants.DOWN_LINE_STRING
											+ PaymentConstants.MEDICAL
									, info);
						}
						if (BigDecimalUtils.safeAdd(BigDecimalUtils.isNullToZero(info.getPersonalBigmailmentMoney()),
								BigDecimalUtils.isNullToZero(info.getUnitBigmailmentMoney())).compareTo(BigDecimal.ZERO) != 0) {
							paymentInfoBigMap.put(info.getSocialPayAddr()
											+ CommonConstants.DOWN_LINE_STRING
											+ info.getEmpIdcard()
											+ CommonConstants.DOWN_LINE_STRING
											+ info.getSocialPayMonth()
											+ CommonConstants.DOWN_LINE_STRING
											+ info.getSocialCreateMonth()
											+ CommonConstants.DOWN_LINE_STRING
											+ SocialConstants.DB
									, info);
						}
					}
				}
				// -----------------------------生成paymentInfoMap本段结束--------------------------------

				/**
				 *  修改批量导入社保为线程池的方式。逻辑分析，根据前端传入的list，将list分成每份partSize个。
				 *  有以下3种情况。1.list数量不足partSize，直接进行处理;2.list数量正好可以根据partSize等分，循环处理;3.不能等分有剩余的情况
				 */

				// -----------------------------线程池处理list，批量保存社保信息开始--------------------------------
				List<TPaymentHeFeiVo> tempList = new ArrayList<>();
				AtomicInteger atomicLine = new AtomicInteger(CommonConstants.ZERO_INT);
				List<CompletableFuture<List<ErrorMessage>>> completableFutureList = new ArrayList<>();

				String importSuccessKey = user.getId() + CommonConstants.DOWN_LINE_STRING + CommonConstants.PAYMENT_SOCIAL_WAIT_EXPORT;

				// 创建一个数值格式化对象
				NumberFormat numberFormat = NumberFormat.getInstance();
				// 设置精确到小数点后2位
				numberFormat.setMaximumFractionDigits(2);
				String maerialRatio = "";
				String sumNumKey = user.getId() + CommonConstants.DOWN_LINE_STRING + "sum_key";
				Integer bfInt = (Integer) redisUtil.get(sumNumKey);
				if (Common.isNotNull(bfInt)) {
					if (bfInt <= rowNumber.intValue()) {
						maerialRatio = numberFormat.format((float) bfInt.intValue() / (float) rowNumber.intValue() * 100);
						bfInt = bfInt + 100;
					} else {
						maerialRatio = "100";
					}
				}else {
					if (100 <= rowNumber.intValue()) {
						maerialRatio = numberFormat.format((float) 100 / (float) rowNumber.intValue() * 100);
						bfInt = 100;
					} else {
						maerialRatio = "100";
					}
				}
				redisUtil.set(sumNumKey, bfInt, 360L);
				redisUtil.set(importSuccessKey, maerialRatio, 360L);

				try {
					// 1.list.size()不足partSize，直接执行
					if (list.size() < partSize) {
						CompletableFuture<List<ErrorMessage>> listCompletableFuture = CompletableFuture.supplyAsync(()
								-> executeImportSocialListThree(user, atomicLine, random, list, areaMap, areaMap2,
								paymentInfoPensionMap, paymentInfoMedicalMap, paymentInfoUnEmpMap, paymentInfoInjuryMap,
								paymentInfoBigMap,CommonConstants.ONE_INT, type, errorMessageList),
								yfSocialImportThreadPoolExecutor);
						completableFutureList.add(listCompletableFuture);
					}

					// 2.list.size()大于partSize，循环分批执行
					int lastIdx = 0;
					if (partSize <= list.size()) {
						// 处理第一个0位元素
						final List<TPaymentHeFeiVo> finalList = list.subList(0, 1);
						CompletableFuture<List<ErrorMessage>> oneCompletableFuture = CompletableFuture.supplyAsync(()
										-> executeImportSocialListThree(user, atomicLine, random, finalList, areaMap,
										areaMap2, paymentInfoPensionMap, paymentInfoMedicalMap, paymentInfoUnEmpMap,
										paymentInfoInjuryMap, paymentInfoBigMap,CommonConstants.ONE_INT, type,
										errorMessageList), yfSocialImportThreadPoolExecutor);
						completableFutureList.add(oneCompletableFuture);
						lastIdx = 1;
						for (int i = 1; i < list.size(); i++) {
							// partSize数量的list为一个执行单元
							tempList.add(list.get(i));
							if (i % partSize == 0) {
								lastIdx = i;
								final int idx = i - partSize + 2;
								List<TPaymentHeFeiVo> finalTempList1 = tempList;
								CompletableFuture<List<ErrorMessage>> listCompletableFuture = CompletableFuture.supplyAsync(()
												-> executeImportSocialListThree(user, atomicLine, random, finalTempList1
												, areaMap, areaMap2, paymentInfoPensionMap, paymentInfoMedicalMap,
												paymentInfoUnEmpMap, paymentInfoInjuryMap, paymentInfoBigMap,idx, type,
												errorMessageList), yfSocialImportThreadPoolExecutor);
								completableFutureList.add(listCompletableFuture);
								tempList = new ArrayList<>();
							}
						}
					}

					// 3.第2种方式执行完后，有余数的情况下，执行3
					if (lastIdx != 0) {
						tempList = new ArrayList<>();
						if (lastIdx == 1) {
							for (int i = lastIdx; i < list.size(); i++) {
								tempList.add(list.get(i));
							}
						} else {
							for (int i = lastIdx + 1; i < list.size(); i++) {
								tempList.add(list.get(i));
							}
							lastIdx += 1;
						}

						List<TPaymentHeFeiVo> finalTempList = tempList;
						int finalLastIdx = lastIdx + 1;
						CompletableFuture<List<ErrorMessage>> listCompletableFuture = CompletableFuture.supplyAsync(()
										-> executeImportSocialListThree(user, atomicLine, random, finalTempList, areaMap,
											areaMap2, paymentInfoPensionMap, paymentInfoMedicalMap, paymentInfoUnEmpMap,
											paymentInfoInjuryMap, paymentInfoBigMap,finalLastIdx, type, errorMessageList)
											, yfSocialImportThreadPoolExecutor);
						completableFutureList.add(listCompletableFuture);
					}

					// 阻塞当前线程，等待所有的线程执行完毕
					boolean computeFlag;
					do {
						computeFlag = false;
						for (CompletableFuture<List<ErrorMessage>> listCompletableFuture : completableFutureList) {
							if (!listCompletableFuture.isDone()) {
								computeFlag = true;
							}
						}
					} while (computeFlag);

				} catch (Exception e) {
					log.error("社保缴费库数据批量导入异常：" + e);
				} finally {
					paymentInfoPensionMap.clear();
					paymentInfoBigMap.clear();
					paymentInfoMedicalMap.clear();
					paymentInfoUnEmpMap.clear();
					paymentInfoInjuryMap.clear();
				}
			}
		}
	}

	private List<ErrorMessage> executeImportSocialListThree(YifuUser user, AtomicInteger atomicLine,
											  String random,
											  List<TPaymentHeFeiVo> list,
											  HashMap<String, String> areaMap,
											  HashMap<String, String> areaMap2,
											  ConcurrentHashMap<String, TPaymentInfo> paymentInfoPensionMap,
											  ConcurrentHashMap<String, TPaymentInfo> paymentInfoMedicalMap,
											  ConcurrentHashMap<String, TPaymentInfo> paymentInfoUnEmpMap,
											  ConcurrentHashMap<String, TPaymentInfo> paymentInfoInjuryMap,
											  ConcurrentHashMap<String, TPaymentInfo> paymentInfoBigMap,
											  int idx, String type,List<ErrorMessage> errorMessageList) {
		int i = idx;
		if (Common.isNotNull(list)) {
			TPaymentInfo payExists = null;
			TSocialFundInfo socialInfo = null;
			String temp;
			int res = -1;
			List<String> idcards = Common.listObjectToStrList(list, ExcelAttributeConstants.EMPIDCARD);

			List<TSocialFundInfo> socialInfoList = socialFundInfoMapper.selectList(Wrappers.<TSocialFundInfo>query().lambda()
					.in(TSocialFundInfo::getEmpIdcard, Common.listObjectToStrList(list, ExcelAttributeConstants.EMPIDCARD)));

			//已存在的社保数据
			HashMap<String, TSocialFundInfo> socialMap = new HashMap<>();
			if (Common.isNotNull(socialInfoList)) {
				for (TSocialFundInfo tSocialInfo : socialInfoList) {
					socialMap.put(tSocialInfo.getEmpIdcard()
							+ CommonConstants.DOWN_LINE_STRING + tSocialInfo.getSocialProvince()
							+ CommonConstants.DOWN_LINE_STRING + tSocialInfo.getSocialCity()
							+ CommonConstants.DOWN_LINE_STRING + (null == tSocialInfo.getSocialTown()
							? "null" : tSocialInfo.getSocialTown()), tSocialInfo);
				}
			}

			String[] areaArray;
			String checkRes;

			for (TPaymentHeFeiVo infoVo : list) {
				atomicLine.incrementAndGet();
				++i;
				if (Common.isNotNull(infoVo.getEmpIdcard())) {
					infoVo.setEmpIdcard(infoVo.getEmpIdcard().replace("x", "X"));
				}
				//导入校验
				if (socialThreeCheckBase(random, i, errorMessageList, infoVo, type)) {
					continue;
				}
				//无对应员工的社保数据
				if (socialMap.size() >CommonConstants.ZERO_INT) {
					//采用客服导入的地址
					areaArray = infoVo.getSocialPayAddr().split(CommonConstants.CENTER_SPLIT_LINE_STRING);
					infoVo = initAddressThree(areaArray, areaMap2, infoVo);
					socialInfo = socialMap.get(infoVo.getEmpIdcard()
							+ CommonConstants.DOWN_LINE_STRING + infoVo.getSocialProvince()
							+ CommonConstants.DOWN_LINE_STRING + infoVo.getSocialCity()
							+ CommonConstants.DOWN_LINE_STRING + (null == infoVo.getSocialTown() ? "null" : infoVo.getSocialTown()));

					//对身份证与人员姓名的对应关系进行校验
					if (socialInfo != null && !socialInfo.getEmpName().equals(infoVo.getEmpName())) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "姓名与身份证信息不一致，请核实后再次尝试！"));
						continue;
					}
					if (socialInfo != null && socialInfo.getSocialStartDate() == null) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), infoVo.getEmpIdcard() + "的社保起缴日期为空！"));
						continue;
					}
				}
				if (null == socialInfo) {
					errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "无对应员工" + infoVo.getEmpIdcard() +
							"的社保数据(请查验社保缴纳地)"));
					continue;
				}
				//如果导入项目数大于办理成功项目数给提示，少于给进，有问题自行删除后导入
				checkRes = checkRepeatInfoThree(socialInfo, infoVo, type);
				if (null != checkRes) {
					errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), checkRes));
					continue;
				}

				//查看缴纳月是否为空
				if (!Common.isNotNull(infoVo.getSocialPayMonth())) {
					errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "必填项校验失败：社保缴纳月份不可为空！"));
					continue;
				}

				//判断是否存在养老缴费数据
				if (PaymentConstants.PENSION_RISK.equals(infoVo.getRiskType())) {
					payExists = paymentInfoPensionMap.get(infoVo.getSocialPayAddr()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getEmpIdcard().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialPayMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialCreateMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ PaymentConstants.PENSION_RISK);
					//存在社保缴费数据
					if (null != payExists && BigDecimalUtils.safeMultiply(BigDecimalUtils.safeAdd(
							BigDecimalUtils.isNullToZero(infoVo.getPersonalMoney()),
							BigDecimalUtils.isNullToZero(infoVo.getUnitMoney())), BigDecimalUtils.safeAdd(
							BigDecimalUtils.isNullToZero(payExists.getUnitPensionMoney()),
							BigDecimalUtils.isNullToZero(payExists.getPersonalPensionMoney()))).compareTo(BigDecimal.ZERO) > 0) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "已存在对应员工身份证" + infoVo.getEmpIdcard()
								+ "的养老缴费数据,请勿重复导入！"));
						continue;
					} else {
						payExists = null;
					}
				}

				//判断是否存在失业缴费数据
				if (PaymentConstants.UNEMPLOYEEMENT_RISK.equals(infoVo.getRiskType())) {
					payExists = paymentInfoUnEmpMap.get(infoVo.getSocialPayAddr()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getEmpIdcard().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialPayMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialCreateMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ PaymentConstants.UNEMPLOYEEMENT_RISK);
					//存在社保缴费数据
					if (null != payExists && BigDecimalUtils.safeMultiply(BigDecimalUtils.safeAdd(
							BigDecimalUtils.isNullToZero(infoVo.getPersonalMoney()),
							BigDecimalUtils.isNullToZero(infoVo.getUnitMoney())), BigDecimalUtils.safeAdd(
							BigDecimalUtils.isNullToZero(payExists.getUnitUnemploymentMoney()),
							BigDecimalUtils.isNullToZero(payExists.getPersonalUnemploymentMoney()))).compareTo(BigDecimal.ZERO) > 0) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "已存在对应员工身份证" + infoVo.getEmpIdcard()
								+ "的失业缴费数据,请勿重复导入！"));
						continue;
					} else {
						payExists = null;
					}
				}

				//判断是否存在医保缴费数据
				if (CommonConstants.ONE_STRING.equals(type)) {
					payExists = paymentInfoMedicalMap.get(infoVo.getSocialPayAddr()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getEmpIdcard().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialPayMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialCreateMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ PaymentConstants.MEDICAL);

					//存在社保缴费数据
					if (null != payExists && BigDecimalUtils.safeMultiply(BigDecimalUtils.safeAdd(
							BigDecimalUtils.isNullToZero(infoVo.getUnitMedicalMoney()),
							BigDecimalUtils.isNullToZero(infoVo.getPersonalMedicalMoney())), BigDecimalUtils.safeAdd(
							BigDecimalUtils.isNullToZero(payExists.getUnitMedicalMoney()),
							BigDecimalUtils.isNullToZero(payExists.getPersonalMedicalMoney()))).compareTo(BigDecimal.ZERO) > 0) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "已存在对应员工身份证" + infoVo.getEmpIdcard()
								+ "的医保缴费数据,请勿重复导入！"));
						continue;
					} else {
						payExists = null;
					}
				}

				//判断是否存在工伤缴费数据
				if (PaymentConstants.INJURY_RISK.equals(infoVo.getRiskType())) {
					payExists = paymentInfoInjuryMap.get(infoVo.getSocialPayAddr()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getEmpIdcard().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialPayMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialCreateMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ PaymentConstants.INJURY_RISK);

					//存在社保缴费数据
					if (null != payExists && BigDecimalUtils.safeMultiply(BigDecimalUtils.safeAdd(
									BigDecimalUtils.isNullToZero(infoVo.getUnitMedicalMoney()),
									BigDecimalUtils.isNullToZero(infoVo.getPersonalMedicalMoney())),
							BigDecimalUtils.isNullToZero(payExists.getUnitInjuryMoney())).compareTo(BigDecimal.ZERO) > 0) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "已存在对应员工身份证" + infoVo.getEmpIdcard()
								+ "的单位工伤缴费数据,请勿重复导入！"));
						continue;
					} else {
						payExists = null;
					}
				}

				//判断是否存在医疗救助金缴费数据
				if (CommonConstants.ONE_STRING.equals(type)) {
					payExists = paymentInfoBigMap.get(infoVo.getSocialPayAddr()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getEmpIdcard().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialPayMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ infoVo.getSocialCreateMonth().trim()
							+ CommonConstants.DOWN_LINE_STRING
							+ SocialConstants.DB);

					//存在社保缴费数据
					if (null != payExists && BigDecimalUtils.safeMultiply(BigDecimalUtils.safeAdd(
							BigDecimalUtils.isNullToZero(infoVo.getUnitBigailmentMoney()),
							BigDecimalUtils.isNullToZero(infoVo.getPersonalBigailmentMoney())), BigDecimalUtils.safeAdd(
							BigDecimalUtils.isNullToZero(payExists.getUnitBigmailmentMoney()),
							BigDecimalUtils.isNullToZero(payExists.getPersonalBigmailmentMoney()))).compareTo(BigDecimal.ZERO) > 0) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "已存在对应员工身份证" + infoVo.getEmpIdcard()
								+ "的医疗救助金缴费数据,请勿重复导入！"));
						continue;
					} else {
						payExists = null;
					}
				}

				TSocialInfo tSocialInfo = socialInfoMapper.selectOne(Wrappers.<TSocialInfo>query().lambda()
						.eq(TSocialInfo::getEmpIdcard, infoVo.getEmpIdcard()).groupBy(TSocialInfo::getCreateTime)
						.last(CommonConstants.LAST_ONE_SQL));
				if (null == payExists) {
					payExists = new TPaymentInfo();
					payExists.setLockStatus(CommonConstants.ZERO_STRING);
					payExists.setEmpId(socialInfo.getEmpId());
					payExists.setEmpIdcard(socialInfo.getEmpIdcard());
					payExists.setEmpName(socialInfo.getEmpName());
					payExists.setSettleDomainId(socialInfo.getSettleDomainName());
					payExists.setUnitId(socialInfo.getUnitName());
					if (Common.isNotNull(tSocialInfo)) {
						payExists.setInauguralTeam(tSocialInfo.getInauguralTeam());
						payExists.setEmpNo(tSocialInfo.getEmpNo());
					}
				}
				payExists.setSocialProvince(socialInfo.getSocialProvince());
				payExists.setSocialCity(socialInfo.getSocialCity());
				payExists.setSocialTown(socialInfo.getSocialTown());

				payExists.setSocialHousehold(socialInfo.getSocialHouseholdName());
				payExists.setSocialPayMonth(infoVo.getSocialPayMonth());

				if (null != payExists.getSocialProvince()) {
					temp = areaMap.get(payExists.getSocialProvince());
					if (Common.isNotNull(temp)) {
						payExists.setSocialPayAddr(temp);
					}
				}
				if (null != payExists.getSocialCity()) {
					temp = areaMap.get(payExists.getSocialCity());
					if (Common.isNotNull(temp)) {
						payExists.setSocialPayAddr(payExists.getSocialPayAddr() + CommonConstants.CENTER_SPLIT_LINE_STRING + temp);
					}
				}
				if (null != payExists.getSocialTown()) {
					temp = areaMap.get(payExists.getSocialTown());
					if (Common.isNotNull(temp)) {
						payExists.setSocialPayAddr(payExists.getSocialPayAddr() + CommonConstants.CENTER_SPLIT_LINE_STRING + temp);
					}
				}
				payExists.setSocialSecurityNo(infoVo.getSocialSecurityNo());
				payExists.setSocialCreateMonth(infoVo.getSocialCreateMonth());
				if (PaymentConstants.PENSION_RISK.equals(infoVo.getRiskType())) {
					payExists.setUnitPensionMoney(infoVo.getUnitMoney());
					payExists.setUnitPensionSet(infoVo.getUnitSet());
					payExists.setPersonalPensionMoney(infoVo.getPersonalMoney());
					payExists.setUnitSocialSum(BigDecimalUtils.safeAdd(payExists.getUnitPensionMoney()
							, payExists.getUnitSocialSum()));
					payExists.setSocialSecurityPersonalSum(BigDecimalUtils.safeAdd(payExists.getPersonalPensionMoney()
							, payExists.getSocialSecurityPersonalSum()));
				}
				if (PaymentConstants.UNEMPLOYEEMENT_RISK.equals(infoVo.getRiskType())) {
					payExists.setUnitUnemploymentMoney(infoVo.getUnitMoney());
					payExists.setPersonalUnemploymentMoney(infoVo.getPersonalMoney());
					payExists.setUnitUnemploymentSet(infoVo.getUnitSet());
					payExists.setUnitSocialSum(BigDecimalUtils.safeAdd(payExists.getUnitUnemploymentMoney()
							, payExists.getUnitSocialSum()));
					payExists.setSocialSecurityPersonalSum(BigDecimalUtils.safeAdd(payExists.getPersonalUnemploymentMoney()
							, payExists.getSocialSecurityPersonalSum()));
				}
				if (PaymentConstants.INJURY_RISK.equals(infoVo.getRiskType())) {
					payExists.setUnitInjuryMoney(infoVo.getUnitMoney());
					payExists.setUnitInjurySet(infoVo.getUnitSet());
					payExists.setUnitSocialSum(BigDecimalUtils.safeAdd(payExists.getUnitInjuryMoney()
							, payExists.getUnitSocialSum()));
				}
				if (CommonConstants.ONE_STRING.equals(type)) {
					payExists.setUnitMedicalMoney(infoVo.getUnitMedicalMoney());
					payExists.setPersonalMedicalMoney(infoVo.getPersonalMedicalMoney());
					payExists.setUnitBigmailmentMoney(infoVo.getUnitBigailmentMoney());
					payExists.setUnitMedicalSet(infoVo.getMedicalSet());
					payExists.setPersonalBigmailmentMoney(infoVo.getPersonalBigailmentMoney());
					payExists.setSocialSecurityNo(infoVo.getSocialSecurityNo());

					payExists.setUnitSocialSum(BigDecimalUtils.safeAdd(payExists.getUnitMedicalMoney()
							, payExists.getUnitBigmailmentMoney()
							, payExists.getUnitSocialSum()));
					payExists.setSocialSecurityPersonalSum(BigDecimalUtils.safeAdd(payExists.getPersonalMedicalMoney()
							, payExists.getPersonalBigmailmentMoney()
							, payExists.getSocialSecurityPersonalSum()));
				}
				payExists.setSocialSum(BigDecimalUtils.safeAdd(payExists.getUnitSocialSum(), payExists.getSocialSecurityPersonalSum()));
				payExists.setSumAll(payExists.getSocialSum());
				payExists.setCreateBy(user.getId());
				payExists.setCreateName(user.getNickname());
				if (null != payExists && Common.isEmpty(payExists.getSocialId())) {
					payExists.setSocialId(UUID.randomUUID().toString());
				}
				if (null != payExists && Common.isNotNull(payExists.getId())) {
					res = baseMapper.updateById(payExists);
					if (res < 0) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), infoVo.getEmpIdcard() + "_缴费库更新失败！"));
						continue;
					}
				} else {
					res = insertAndSTimestamp(payExists);
					if (res < 0) {
						errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), infoVo.getEmpIdcard() + "_缴费库保存失败！"));
						continue;
					}
				}
			}
		}
		return errorMessageList;
	}

	/**
	 * 导入校验
	 **/
	private boolean socialThreeCheckBase(String random, int i, List<ErrorMessage> errorMessageList, TPaymentHeFeiVo
			infoVo, String type) {
		if (!Common.isNotNull(infoVo.getSocialPayMonth())) {
			errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "社保缴纳月份不可为空！"));
			return true;
		}
		if (!Common.isNotNull(infoVo.getSocialCreateMonth())) {
			errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "社保生成月份不可为空！"));
			return true;
		}
		if (!Common.isNotNull(infoVo.getSocialPayAddr())) {
			errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "社保缴纳地不可为空！"));
			return true;
		}
		if (CommonConstants.ZERO_STRING.equals(type) && !PaymentConstants.PENSION_RISK.equals(infoVo.getRiskType())
				&& !PaymentConstants.UNEMPLOYEEMENT_RISK.equals(infoVo.getRiskType())
				&& !PaymentConstants.INJURY_RISK.equals(infoVo.getRiskType())) {
			errorMessageList.add(new ErrorMessage(infoVo.getRowIndex(), "无相关险种！"));
			return true;
		}
		return false;
	}

	/**
	 * 转换缴纳地址为省市县ID
	 **/
	private TPaymentHeFeiVo initAddressThree(String[]
													 areaArray, HashMap<String, String> areaMap2, TPaymentHeFeiVo s) {
		if (null == areaArray || null == areaMap2 || null == s) {
			return s;
		}
		String temp = null;
		if (areaArray.length >= CommonConstants.TWO_INT) {
			temp = areaMap2.get(areaArray[0] + CommonConstants.DOWN_LINE_STRING + "null");
			if (Common.isNotNull(temp)) {
				s.setSocialProvince(Integer.valueOf(temp));
			}
			temp = areaMap2.get(areaArray[1] + CommonConstants.DOWN_LINE_STRING + (null == s.getSocialProvince()
					? "null" : Integer.toString(s.getSocialProvince())));
			if (Common.isNotNull(temp)) {
				s.setSocialCity(Integer.valueOf(temp));
			}
			if (areaArray.length >= CommonConstants.THREE_INT) {
				temp = areaMap2.get(areaArray[2] + CommonConstants.DOWN_LINE_STRING + (null == s.getSocialCity()
						? "null" : Integer.toString(s.getSocialCity())));
				if (Common.isNotNull(temp)) {
					s.setSocialTown(Integer.valueOf(temp));
				}
			}
		}
		return s;
	}

	/**
	 * 验证部分办理失败的导入是否有超出部分的数据导入
	 *
	 * @param socialInfo
	 * @param infoVo
	 * @return
	 * @Author huyc
	 * @Date 2022-07-27
	 **/
	private String checkRepeatInfoThree(TSocialFundInfo socialInfo, TPaymentHeFeiVo infoVo, String type) {
		if (CommonConstants.ZERO_STRING.equals(type)) {
			if (PaymentConstants.PENSION_RISK.equals(infoVo.getRiskType())
					&& checkHandleMoney(socialInfo.getPensionHandle(),
					infoVo.getUnitMoney(), infoVo.getPersonalMoney())) {
				return concatStr(PaymentConstants.PENSION);
			}
			if (PaymentConstants.UNEMPLOYEEMENT_RISK.equals(infoVo.getRiskType())
					&& checkHandleMoney(socialInfo.getUnemployHandle(),
					infoVo.getUnitMoney(), infoVo.getPersonalMoney())) {
				return concatStr(PaymentConstants.UNEMPLOYEEMENT);
			}
			if (PaymentConstants.INJURY_RISK.equals(infoVo.getRiskType())
					&& checkHandleMoney(socialInfo.getWorkInjuryHandle(),
					infoVo.getUnitMoney(), infoVo.getPersonalMoney())) {
				return concatStr(PaymentConstants.INJURY);
			}
		} else if (CommonConstants.ONE_STRING.equals(type)) {
			if (checkHandleMoney(socialInfo.getMedicalHandle(),
					infoVo.getUnitMedicalMoney(), infoVo.getPersonalMedicalMoney())) {
				return concatStr(PaymentConstants.MEDICAL);
			}
			if (CommonConstants.ONE_STRING.equals(socialInfo.getIsIllness()) &&
					(BigDecimalUtils.isNullToZero(infoVo.getUnitBigailmentMoney()).compareTo(
							BigDecimal.ZERO) != CommonConstants.ZERO_INT)) {
				return "无大病，无需导入大病！";
			}
		}
		return null;
	}

	//比对缴纳地是否一致
	private TSocialFundInfo checkAddress(String[] areaArray, HashMap<String, String> areaMap, TSocialFundInfo s) {
		if (null == areaArray || null == areaMap || null == s) {
			return null;
		}
		if (areaArray.length >= CommonConstants.TWO_INT) {
			if (null != s.getFundProvince()) {
				if (areaArray[0].equals(areaMap.get(s.getFundProvince()))) {
					if (null != s.getFundCity()) {
						if (areaArray[1].equals(areaMap.get(s.getFundCity()))) {
							if (areaArray.length >= CommonConstants.THREE_INT) {
								if (null != s.getFundTown() && areaArray[CommonConstants.TWO_INT].equals(
										areaMap.get(s.getFundTown()))) {
									return s;
								}
							} else {
								if (null == s.getFundTown()) {
									return s;
								}
							}
						}
					} else {
						return null;
					}
				}

			} else {
				return null;
			}

		} else if (areaArray.length == 1) {
			if (null != s.getFundProvince() && null == s.getFundCity()) {
				if (areaArray[0].equals(areaMap.get(s.getFundProvince()))) {
					return s;
				}
			} else {
				return null;
			}
		}
		return null;
	}

	/**
	 * 薪资获取缴费库
	 * @Author hgw
	 * @Date 2022-8-10 18:06:13
	 **/
	@Override
	public TPaymentVo selectTPaymentBySalaryVo(TPaymentBySalaryVo tPaymentBySalaryVo) {
		List<TPaymentBySalaryVo> list = baseMapper.selectTPaymentBySalaryVo(tPaymentBySalaryVo);
		TPaymentVo vo = new TPaymentVo();
		vo.setPaymentList(list);
		return vo;
	}

	@Override
	public int updatePaymentSocialStatusToSettle(List<String> idList) {
		if (idList != null && !idList.isEmpty()) {
			return baseMapper.updatePaymentSocialStatusByIdList(idList, CommonConstants.ONE_STRING);
		}
		return 0;
	}

	@Override
	public int updatePaymentSocialStatusToNoSettle(List<String> idList) {
		if (idList != null && !idList.isEmpty()) {
			return baseMapper.updatePaymentSocialStatusByIdList(idList, CommonConstants.ZERO_STRING);
		}
		return 0;
	}

	@Override
	public int updatePaymentFundStatusToSettle(List<String> idList) {
		if (idList != null && !idList.isEmpty()) {
			return baseMapper.updatePaymentFundStatusByIdList(idList, CommonConstants.ONE_STRING);
		}
		return 0;
	}

	@Override
	public int updatePaymentFundStatusToNoSettle(List<String> idList) {
		if (idList != null && !idList.isEmpty()) {
			return baseMapper.updatePaymentFundStatusByIdList(idList, CommonConstants.ZERO_STRING);
		}
		return 0;
	}
}
