package com.yifu.cloud.plus.v1.csp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
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.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yifu.cloud.plus.v1.csp.config.WxConfig;
import com.yifu.cloud.plus.v1.csp.constants.RegistConstants;
import com.yifu.cloud.plus.v1.csp.entity.EmployeeRegistration;
import com.yifu.cloud.plus.v1.csp.entity.TOperationLog;
import com.yifu.cloud.plus.v1.csp.mapper.EmployeeRegistrationMapper;
import com.yifu.cloud.plus.v1.csp.service.EmployeeRegistrationService;
import com.yifu.cloud.plus.v1.csp.service.TOperationLogService;
import com.yifu.cloud.plus.v1.csp.vo.*;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysUser;
import com.yifu.cloud.plus.v1.yifu.admin.api.vo.SysCspDeptVo;
import com.yifu.cloud.plus.v1.yifu.archives.entity.SysAutoDictItem;
import com.yifu.cloud.plus.v1.yifu.archives.entity.TEmployeeProjectBelongDept;
import com.yifu.cloud.plus.v1.yifu.archives.vo.*;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.ClientNameConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.SecurityConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.*;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.UserPermissionVo;
import com.yifu.cloud.plus.v1.yifu.common.core.vo.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.dapr.config.DaprArchivesProperties;
import com.yifu.cloud.plus.v1.yifu.common.dapr.util.*;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import com.yifu.cloud.plus.v1.yifu.insurances.util.ValidityUtil;
import com.yifu.cloud.plus.v1.yifu.insurances.vo.TEmployeeInsurancePreVo;
import com.yifu.cloud.plus.v1.yifu.social.vo.TEmployeeInsuranceWorkDayVo;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

/**
 * 入离职登记表
 *
 * @author huych
 * @date 2025-02-25 14:48:11
 */
@Log4j2
@Service
@AllArgsConstructor
@EnableConfigurationProperties(DaprArchivesProperties.class)
public class EmployeeRegistrationServiceImpl extends ServiceImpl<EmployeeRegistrationMapper, EmployeeRegistration> implements EmployeeRegistrationService {

	private final DaprArchivesProperties archivesProperties;

	@Autowired
	private ArchivesDaprUtil archivesDaprUtil;

	@Autowired
	private SocialDaprUtils socialDaprUtils;

	@Autowired
	private WxConfig wxConfig;

	@Autowired
	private UpmsDaprUtils upmsDaprUtils;

	@Autowired
	private InsuranceDaprUtil insuranceDaprUtil;

	private final TOperationLogService logService;

	/**
	 * 入离职登记表简单分页查询
	 *
	 * @param employeeRegistration 入离职登记表
	 * @return
	 */
	@Override
	public IPage<EmployeeRegistration> getEmployeeRegistrationPage(Page<EmployeeRegistration> page, EmployeeRegistrationSearchVo employeeRegistration) {
		getDtpeAuth(employeeRegistration);
		return baseMapper.getEmployeeRegistrationPage(page, employeeRegistration);
	}

	/**
	 * hr入离职登记表代办列表简单分页查询
	 *
	 * @param employeeRegistration 入离职登记表
	 * @return
	 */
	@Override
	public IPage<EmployeeRegistration> getHrEmployeeRegistrationPage(Page<EmployeeRegistration> page, EmployeeRegistrationSearchVo employeeRegistration) {
		YifuUser user = SecurityUtils.getUser();
		// 全服管理者
		long roleId = 1839501715787390978L;
		boolean isSsc = this.haveRole(user, roleId);
		if (isSsc) {
			employeeRegistration.setCustomerUserLoginname(null);
		}
		return baseMapper.getEmployeeRegistrationPage(page, employeeRegistration);
	}

	/**
	 * 作业自动化-入离职登记列表简单分页查询
	 *
	 * @param employeeRegistration 入离职登记表
	 * @return
	 */
	@Override
	public IPage<EmployeeRegistration> getHrAutoEmployeeRegistrationPage(Page<EmployeeRegistration> page, EmployeeRegistrationSearchVo employeeRegistration) {
		employeeRegistration.setCustomerUserLoginname(null);
		// 项目权限赋值
		employeeRegistration.setType(CommonConstants.TWO_STRING);
		initSearchVo(employeeRegistration);
		return baseMapper.getEmployeeRegistrationPage(page, employeeRegistration);
	}

	/**
	 * 入职待确认简单分页查询表简单分页查询
	 *
	 * @param employeeRegistration 入离职登记表
	 * @return
	 */
	@Override
	public IPage<EmployeeRegistration> getHrConfirmEmployeeRegistrationPage(Page<EmployeeRegistration> page, EmployeeRegistrationSearchVo employeeRegistration) {
		//权限和条件赋值
		initSearchVo(employeeRegistration);
		if (Common.isEmpty(employeeRegistration.getStatusList())) {
			employeeRegistration.setStatusList(CommonConstants.processPreStatus);
		}
		employeeRegistration.setTypeList(CommonConstants.processPreStatus);
		return baseMapper.getEmployeeRegistrationPage(page, employeeRegistration);
	}

	@Override
	public List<String> getHrConfirmIdcardList(EmployeeRegistrationSearchVo searchVo) {
		//权限和条件赋值
		initSearchVo(searchVo);
		if (Common.isEmpty(searchVo.getStatusList())) {
			searchVo.setStatusList(CommonConstants.processPreStatus);
		}
		searchVo.setTypeList(CommonConstants.processPreStatus);
		return baseMapper.getHrConfirmIdcardList(searchVo);
	}

	/**
	 * 入离职登记表批量导出
	 *
	 * @param searchVo 入离职登记表
	 * @return
	 */
	@Override
	public void listExport(HttpServletResponse response, EmployeeRegistrationSearchVo searchVo) {
		String fileName = "入离职登记表批量导出" + DateUtil.getThisTime() + ".xlsx";
		//获取要导出的列表
		List<EmployeeRegistrationExportVo> list = new ArrayList<>();
		getDtpeAuth(searchVo);
		long count = baseMapper.selectExportCount(searchVo);
		try (ServletOutputStream out = response.getOutputStream()) {
			response.setContentType(CommonConstants.MULTIPART_FORM_DATA);
			response.setCharacterEncoding("utf-8");
			response.setHeader(CommonConstants.CONTENT_DISPOSITION, CommonConstants.ATTACHMENT_FILENAME + URLEncoder.encode(fileName, CommonConstants.UTF8));
			// 这里 需要指定写用哪个class去写，然后写到第一个sheet，然后文件流会自动关闭
			ExcelWriter excelWriter = EasyExcel.write(out, EmployeeRegistrationExportVo.class).build();
			int index = 0;
			if (count > CommonConstants.ZERO_INT) {
				for (int i = 0; i <= count; i += CommonConstants.EXCEL_EXPORT_LIMIT) {
					// 获取实际记录
					searchVo.setLimitStart(i);
					searchVo.setLimitEnd(CommonConstants.EXCEL_EXPORT_LIMIT);
					list = baseMapper.selectExportList(searchVo);
					if (Common.isNotNull(list)) {
						WriteSheet writeSheet = EasyExcel.writerSheet("入离职登记表" + index).build();
						excelWriter.write(list, writeSheet);
						index++;
					}
					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);
		}
	}

	/**
	 * 入离职代办表批量导出
	 *
	 * @param searchVo 入离职登记表
	 */
	@Override
	public void listHrExport(HttpServletResponse response, EmployeeRegistrationSearchVo searchVo) {
		// 1.9.8: 前端客服去掉指定人查询
		YifuUser user = SecurityUtils.getUser();
		long roleId = 1839501715787390978L;
		boolean isSsc = this.haveRole(user, roleId);
		if (isSsc) {
			searchVo.setCustomerUserLoginname(null);
		}

		String fileName = "入离职待办任务表批量导出" + DateUtil.getThisTime() + ".xlsx";

		//获取要导出的列表
		List<EmployeeRegistrationHrExportVo> list = new ArrayList<>();
		long count = baseMapper.selectExportCount(searchVo);
		try (ServletOutputStream out = response.getOutputStream()) {
			response.setContentType(CommonConstants.MULTIPART_FORM_DATA);
			response.setCharacterEncoding("utf-8");
			response.setHeader(CommonConstants.CONTENT_DISPOSITION, CommonConstants.ATTACHMENT_FILENAME + URLEncoder.encode(fileName, CommonConstants.UTF8));
			// 这里 需要指定写用哪个class去写，然后写到第一个sheet，然后文件流会自动关闭
			ExcelWriter excelWriter = EasyExcel.write(out, EmployeeRegistrationHrExportVo.class).build();
			int index = 0;
			if (count > CommonConstants.ZERO_INT) {
				for (int i = 0; i <= count; i += CommonConstants.EXCEL_EXPORT_LIMIT) {
					// 获取实际记录
					searchVo.setLimitStart(i);
					searchVo.setLimitEnd(CommonConstants.EXCEL_EXPORT_LIMIT);
					list = baseMapper.selectHrExportList(searchVo);
					if (Common.isNotNull(list)) {
						WriteSheet writeSheet = EasyExcel.writerSheet("入离职待办任务表" + index).build();
						excelWriter.write(list, writeSheet);
						index++;
					}
					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);
		}
	}

	/**
	 * 作业自动化-入离职登记导出
	 *
	 * @param searchVo 入离职登记表
	 * @since 1.9.8
	 * @author chenyuxi
	 */
	@Override
	public void hrRegistrationExport(HttpServletResponse response, EmployeeRegistrationSearchVo searchVo) {
		// 1.9.8: 前端客服去掉指定人查询
		searchVo.setCustomerUserLoginname(null);
		// 1.9.8: 项目权限赋值
		searchVo.setType(CommonConstants.TWO_STRING);
		initSearchVo(searchVo);

		String fileName = "入离职登记批量导出" + DateUtil.getThisTime() + ".xlsx";
		//获取要导出的列表
		List<EmployeeRegistrationHrAutoExportVo> list = new ArrayList<>();
		long count = baseMapper.selectExportCount(searchVo);
		try (ServletOutputStream out = response.getOutputStream()) {
			response.setContentType(CommonConstants.MULTIPART_FORM_DATA);
			response.setCharacterEncoding("utf-8");
			response.setHeader(CommonConstants.CONTENT_DISPOSITION, CommonConstants.ATTACHMENT_FILENAME + URLEncoder.encode(fileName, CommonConstants.UTF8));
			// 这里 需要指定写用哪个class去写，然后写到第一个sheet，然后文件流会自动关闭
			ExcelWriter excelWriter = EasyExcel.write(out, EmployeeRegistrationHrAutoExportVo.class).build();
			int index = 0;
			if (count > CommonConstants.ZERO_INT) {
				for (int i = 0; i <= count; i += CommonConstants.EXCEL_EXPORT_LIMIT) {
					// 获取实际记录
					searchVo.setLimitStart(i);
					searchVo.setLimitEnd(CommonConstants.EXCEL_EXPORT_LIMIT);
					list = baseMapper.selectHrAutoExportList(searchVo);
					if (Common.isNotNull(list)) {
						WriteSheet writeSheet = EasyExcel.writerSheet("入离职登记" + index).build();
						excelWriter.write(list, writeSheet);
						index++;
					}
					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);
		}

	}

	/**
	 * 入职待确认批量导出
	 * @param searchVo 入职待确认
	 * @return
	 */
	@Override
	public void hrConfirmExport(HttpServletResponse response, EmployeeRegistrationSearchVo searchVo) {
		String fileName = "入职待确认批量导出" + DateUtil.getThisTime() + ".xlsx";
		//获取要导出的列表
		List<EmployeeRegistrationHrPreExportVo> list = new ArrayList<>();
		//前端客服的所有项目权限赋值
		initSearchVo(searchVo);
		//待办列表只查看未处理的数据
		if (Common.isEmpty(searchVo.getStatusList())) {
			searchVo.setStatusList(CommonConstants.processPreStatus);
		}
		searchVo.setTypeList(CommonConstants.processPreStatus);
		long count = baseMapper.selectExportCount(searchVo);
		try (ServletOutputStream out = response.getOutputStream()) {
			response.setContentType(CommonConstants.MULTIPART_FORM_DATA);
			response.setCharacterEncoding("utf-8");
			response.setHeader(CommonConstants.CONTENT_DISPOSITION, CommonConstants.ATTACHMENT_FILENAME + URLEncoder.encode(fileName, CommonConstants.UTF8));
			// 这里 需要指定写用哪个class去写，然后写到第一个sheet，然后文件流会自动关闭
			ExcelWriter excelWriter = EasyExcel.write(out, EmployeeRegistrationHrPreExportVo.class).build();
			int index = 0;
			if (count > CommonConstants.ZERO_INT) {
				for (int i = 0; i <= count; i += CommonConstants.EXCEL_EXPORT_LIMIT) {
					// 获取实际记录
					searchVo.setLimitStart(i);
					searchVo.setLimitEnd(CommonConstants.EXCEL_EXPORT_LIMIT);
					list = baseMapper.selectHrPreExportList(searchVo);
					if (Common.isNotNull(list)) {
						WriteSheet writeSheet = EasyExcel.writerSheet("入职待确认表" + index).build();
						excelWriter.write(list, writeSheet);
						index++;
					}
					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);
		}
	}

	@Override
	public R<List<ErrorMessage>> importDiy(InputStream inputStream, String deptId, String dataSource) {

		List<ErrorMessage> errorMessageList = new ArrayList<>();
		ExcelUtil<EmployeeRegistrationVo> util1 = new ExcelUtil<>(EmployeeRegistrationVo.class);
		YifuUser user = SecurityUtils.getUser();
		//获取项目信息
		R<TSettleDomainSelectVo> domainR = archivesDaprUtil.getSettleDomainSelectVoById(deptId);

		if (null == domainR || null == domainR.getData()) {
			errorMessageList.add(new ErrorMessage(-1, "访问员工基础服务异常："));
			return R.ok(errorMessageList);
		}
		TSettleDomainSelectVo selectVo = domainR.getData();

		// 1.9.8：获取项目配置的字典值
		List<String> postConfigList = new ArrayList<>();
		R<SysAutoDictItemVo> domainDictR = archivesDaprUtil.getDictListByDeptNo(deptId);
		if (Common.isNotNull(domainDictR) && Common.isNotNull(domainDictR.getData()) && Common.isNotNull(domainDictR.getData().getSysAutoDictItemList())) {
			List<SysAutoDictItem> dictItemList = domainDictR.getData().getSysAutoDictItemList();
			for(SysAutoDictItem sysAutoDictItem : dictItemList){
				if("post_type".equals(sysAutoDictItem.getType())){
					postConfigList.add(sysAutoDictItem.getLabel());
				}
			}
		}

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

				@Override
				public void invoke(EmployeeRegistrationVo 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)) {
						errorMessage.setData(data);
						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());
					importEmployeeRegistration(cachedDataList, errorMessageList, user, selectVo, postConfigList, dataSource);
					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 importEmployeeRegistration(List<EmployeeRegistrationVo> excelVOList,
											List<ErrorMessage> errorMessageList, YifuUser user, TSettleDomainSelectVo selectVo,
											List<String> postConfigList, String dataSource) {
		// 1.9.8新增来源区分
		// 没有来源默认是：1客户端
		if(Common.isEmpty(dataSource)){
			dataSource = CommonConstants.ONE_STRING;
		}

		Map<String, String> exitMap = new HashMap<>();
		EmployeeRegistrationVo excel;
		int inNum = 0;
		int outNum = 0;
		List<EmployeeRegistration> insertList = new ArrayList<>();
		for (int i = 0; i < excelVOList.size(); i++) {
			excel = excelVOList.get(i);
			// 插入
			EmployeeRegistration insert = new EmployeeRegistration();
			BeanUtil.copyProperties(excel, insert);
			//表数据验重
			//表内数据重复 员工姓名、员工身份证号码、反馈类型
			StringBuilder errorTemp = new StringBuilder();
			errorTemp.append(excel.getEmployeeName());
			errorTemp.append("_");
			errorTemp.append(excel.getEmpIdcard());
			errorTemp.append("_");
			errorTemp.append(excel.getFeedbackType());
			if (null == exitMap.get(errorTemp.toString())) {
				exitMap.put(errorTemp.toString(), "1");
			} else {
				errorMessageList.add(new ErrorMessage(excel.getRowIndex(), RegistConstants.IMPORT_EXIT_CHECK, excel));
				continue;
			}
			insert.setDeptNo(selectVo.getDepartNo());
			// 1.9.8：新增校验
			if(CommonConstants.TWO_STRING.equals(dataSource) && Common.isNotNull(postConfigList) && !postConfigList.contains(insert.getPosition())){
				errorMessageList.add(new ErrorMessage(excel.getRowIndex(), "岗位不在项目配置的岗位配置项里", excel));
				continue;
			}

			String error = registCheck(insert);
			if (null != error) {
				errorMessageList.add(new ErrorMessage(excel.getRowIndex(), error, excel));
				continue;
			}
			// 1.9.8：新增赋值
			insert.setDataSource(dataSource);
			initRegistInfo(insert, user, selectVo);
			insertList.add(insert);
			//新增记录,新增操作记录
			baseMapper.insert(insert);
			if (CommonConstants.ONE_STRING.equals(insert.getFeedbackType())) {
				inNum++;
			} else {
				outNum++;
			}
			logService.saveLog(insert.getId(), CommonConstants.ZERO_STRING, RegistConstants.MESSAGE_REGIST, LocalDateTime.now(),
					insert.getRegistorUsername(), null);
		}
		if (!Common.isEmpty(insertList)) {
			insertList.clear();
			//发送企业微信待办
			EmployeeRegistration msgRegistration = new EmployeeRegistration();
			msgRegistration.setInNum(inNum);
			msgRegistration.setOutNum(outNum);
			msgRegistration.setCustomerUserLoginname(selectVo.getCsLoginName());
			msgRegistration.setDeptName(selectVo.getDepartName());
			msgRegistration.setDeptNo(selectVo.getDepartNo());
			//企业微信消息提醒
			sendMessageToWx(msgRegistration, CommonConstants.THREE_STRING);
		}

	}

	/**
	 * 入职待确认表确认接收
	 *
	 * @param preVo 确认接受vo
	 * @return
	 */
	@Override
	public R confirmReceive(EmployeeRegistrationPreVo preVo) {
		if (preVo.getServerItem().contains("商险") && !preVo.getEmployeeInsurancePreVos().isEmpty()) {
			StringBuilder errorTemp = new StringBuilder();
			for (TEmployeeInsurancePreVo insurancePreVo : preVo.getEmployeeInsurancePreVos()) {
				String error = checkInsuranceBuy(insurancePreVo,CommonConstants.ONE_STRING);
				if (null != error) {
					errorTemp.append(error);
				}
			}
			if (errorTemp.length() > 0) {
				return R.other(CommonConstants.TWO_INT,null,errorTemp.toString());
			}
		}

		YifuUser user = SecurityUtils.getUser();
		if (Common.isEmpty(preVo)) {
			return R.failed(CommonConstants.PARAM_IS_NOT_ERROR);
		}
		EmployeeRegistration registration = baseMapper.selectById(preVo.getId());
		if (Common.isEmpty(registration) || (Common.isNotNull(registration) &&
				!CommonConstants.ONE_STRING.equals(registration.getProcessStatus()))) {
			return R.failed(CommonConstants.NO_DATA_TO_HANDLE);
		}
		//商险是否存在判断


		//点击“接收确认”的时候，监测到该人员该项目下已有在项的档案时，再次提醒：该人员已在项，
		//状态将更新成“已处理”；处理记录中，针对此情况，记录真实的进项时间，备注：先于登记入项
		EmpProjectStatusVo vo = new EmpProjectStatusVo();
		vo.setEmpIdcard(registration.getEmpIdcard());
		vo.setDeptNo(registration.getDeptNo());
		EmpProjectStatusVo exit = getEmpProjectStatus(vo);
		if (Common.isNotNull(exit)) {
			return R.ok(exit);
		}
		registration.setProcessStatus(CommonConstants.ZERO_STRING);
		baseMapper.updateById(registration);
		logService.saveLog(registration.getId(), CommonConstants.ZERO_STRING, RegistConstants.CONFIRM_SUBMIT, LocalDateTime.now(),
				user.getNickname(), null);
		try {
			//生成档案待建档数据
			initRegistPreInfo(registration, preVo, user, CommonConstants.ZERO_STRING);
			R<String> domainR = archivesDaprUtil.saveRegistPreInfo(preVo);
			if (Common.isNotNull(domainR) && Common.isNotNull(domainR.getData()) &&
					preVo.getServerItem().contains("商险") && !preVo.getEmployeeInsurancePreVos().isEmpty()) {
				//生成商险待购买的数据
				for (TEmployeeInsurancePreVo insurancePreVo : preVo.getEmployeeInsurancePreVos()) {
					initInsruancePreInfo(registration, insurancePreVo, user, CommonConstants.ZERO_STRING,domainR.getData());
				}
				insuranceDaprUtil.saveInsurancePreInfo(preVo);
			}
		} catch (Exception e) {
			log.error("执行异常", e);
		}
		return R.ok();
	}

	/**
	 * 入职待确认表确认接收
	 *
	 * @param preVo 确认接受vo
	 * @return
	 */
	@Override
	public R batchConfirmReceive(EmployeeRegistrationPreVo preVo) {
		if (Common.isEmpty(preVo)) {
			return R.failed(CommonConstants.PARAM_IS_NOT_ERROR);
		}
		if (preVo.getServerItem().contains("商险") && !preVo.getEmployeeInsurancePreVos().isEmpty()) {
			StringBuilder errorTemp = new StringBuilder();
			for (TEmployeeInsurancePreVo insurancePreVo : preVo.getEmployeeInsurancePreVos()) {
				String error = checkInsuranceBuy(insurancePreVo,CommonConstants.TWO_STRING);
				if (null != error) {
					errorTemp.append(error);
				}
			}
			if (errorTemp.length() > 0) {
				return R.other(CommonConstants.TWO_INT,null,errorTemp.toString());
			}
		}
		//查询该项目该前端客服下所有待接收的入职登记
		//前端客服的所有项目权限赋值
		EmployeeRegistrationSearchVo searchVo = new EmployeeRegistrationSearchVo();
		initSearchVo(searchVo);
		searchVo.setStatusList(CommonConstants.processPreStatus);
		searchVo.setTypeList(CommonConstants.processPreStatus);
		searchVo.setDeptNo(preVo.getDeptNo());
		if (Common.isNotNull(preVo.getIdcardList())) {
			searchVo.setIdcardList(preVo.getIdcardList());
		}
		List<EmployeeRegistration> registrationList = baseMapper.getEmployeeRegistrationList(searchVo);
		YifuUser user = SecurityUtils.getUser();
		if (Common.isEmpty(registrationList)) {
			return R.failed(CommonConstants.NO_DATA_TO_HANDLE);
		}
		//先进行商险是否存在校验


		List<EmployeeRegistrationReceiveVo> receiveVoList = new ArrayList<>();
		for (EmployeeRegistration registration:registrationList) {
			//点击“接收确认”的时候，监测到该人员该项目下已有在项的档案时，再次提醒：该人员已在项，
			//状态将更新成“已处理”；处理记录中，针对此情况，记录真实的进项时间，备注：先于登记入项
			EmpProjectStatusVo vo = new EmpProjectStatusVo();
			vo.setEmpIdcard(registration.getEmpIdcard());
			vo.setDeptNo(registration.getDeptNo());
			EmpProjectStatusVo exit = getEmpProjectStatus(vo);
			if (Common.isNotNull(exit)) {
				EmployeeRegistrationReceiveVo receiveVo = new EmployeeRegistrationReceiveVo();
				receiveVo.setId(registration.getId());
				receiveVo.setEmployeeName(registration.getEmployeeName());
				receiveVo.setEmpIdcard(registration.getEmpIdcard());
				receiveVo.setReason(RegistConstants.BEFORE_IN_PROJECT);
				receiveVo.setEmpProjectId(exit.getEmpProjectId());
				receiveVo.setProjectCreateTime(exit.getProjectTime());
				receiveVoList.add(receiveVo);
				continue;
			}
			registration.setProcessStatus(CommonConstants.ZERO_STRING);
			baseMapper.updateById(registration);
			logService.saveLog(registration.getId(), CommonConstants.ZERO_STRING, RegistConstants.CONFIRM_SUBMIT, LocalDateTime.now(),
					user.getNickname(), null);
			try {
				//生成档案待建档数据
				initRegistPreInfo(registration, preVo, user, CommonConstants.ZERO_STRING);
				R<String> domainR = archivesDaprUtil.saveRegistPreInfo(preVo);
				if (Common.isNotNull(domainR) && Common.isNotNull(domainR.getData()) &&
						preVo.getServerItem().contains("商险") && !preVo.getEmployeeInsurancePreVos().isEmpty()) {
					//生成商险待购买的数据
					for (TEmployeeInsurancePreVo insurancePreVo : preVo.getEmployeeInsurancePreVos()) {
						initInsruancePreInfo(registration, insurancePreVo, user, CommonConstants.ZERO_STRING,domainR.getData());
					}
					insuranceDaprUtil.saveInsurancePreInfo(preVo);
				}
			}catch (Exception e) {
				log.error("执行异常", e);
			}
		}
		if (!receiveVoList.isEmpty() && Common.isNotNull(receiveVoList)) {
			return R.ok(receiveVoList);
		}
		return R.ok();
	}

	@Override
	public R registAdd(EmployeeRegistration registration) {
		//提交校验内容：
		//1、身份证号位数、手机号码位数及格式，就职岗位下拉选择“内勤、揽投员、其他”
		//2、按照类型、按照身份证号、按照项目进行去重校验
		String errorInfo = registCheck(registration);
		if (null != errorInfo) {
			return R.failed(errorInfo);
		} else {
			// 1.9.8: 没有来源默认是：1客户端
			if(Common.isEmpty(registration.getDataSource())){
				registration.setDataSource(CommonConstants.ONE_STRING);
			}
			// 1.9.8: 赋值【最新的客服名称】
			registration.setCustomerUsernameNew(registration.getCustomerUsername());
			baseMapper.insert(registration);
		}
		//企业微信消息提醒
		sendMessageToWx(registration, registration.getFeedbackType());
		//操作记录
		logService.saveLog(registration.getId(), CommonConstants.ZERO_STRING, RegistConstants.MESSAGE_REGIST, LocalDateTime.now(),
				registration.getRegistorUsername(), null);
		return R.ok();
	}

	@Override
	public R confirmSubmit(EmployeeRegistration registration) {
		YifuUser user = SecurityUtils.getUser();
		EmployeeRegistration registrationNow = baseMapper.selectById(registration.getId());
		if (Common.isEmpty(registrationNow) || !CommonConstants.ONE_STRING.equals(registrationNow.getProcessStatus())) {
			return R.failed(RegistConstants.NO_DATA_TO_HANDLE);
		}
		//点击“接收确认”的时候，监测到该人员该项目下已有在项的档案时，再次提醒：该人员已在项，
		//状态将更新成“已处理”；处理记录中，针对此情况，记录真实的进项时间，备注：先于登记入项
		EmpProjectStatusVo vo = new EmpProjectStatusVo();
		vo.setEmpIdcard(registrationNow.getEmpIdcard());
		vo.setDeptNo(registrationNow.getDeptNo());
		EmpProjectStatusVo exit;
		if (CommonConstants.ONE_STRING.equals(registrationNow.getFeedbackType())) {
			exit = getEmpProjectStatus(vo);
		} else {
			exit = getOutEmpProjectStatus(vo);
		}
		if (Common.isNotNull(exit)) {
			return R.ok(exit);
		}
		registrationNow.setProcessStatus(CommonConstants.ZERO_STRING);
		baseMapper.updateById(registrationNow);
		logService.saveLog(registration.getId(), CommonConstants.ZERO_STRING, RegistConstants.CONFIRM_SUBMIT, LocalDateTime.now(),
				user.getNickname(), null);
		return R.ok();
	}

	@Override
	public R updateRegist(EmployeeRegistration registration) {
		YifuUser user = SecurityUtils.getUser();
		EmployeeRegistration registrationNow = baseMapper.selectById(registration.getId());
		if (Common.isEmpty(registrationNow) || !CommonConstants.ONE_STRING.equals(registrationNow.getProcessStatus())) {
			return R.failed(RegistConstants.NO_DATA_TO_HANDLE);
		}
		registrationNow.setProcessStatus(CommonConstants.TWO_STRING);
		logService.saveLog(registration.getId(), CommonConstants.ZERO_STRING, RegistConstants.CONFIRM_SUBMIT, LocalDateTime.now(),
				user.getNickname(), null);
		logService.saveLog(registration.getId(), CommonConstants.ZERO_STRING, RegistConstants.MESSAGE_FINISH, registration.getProjectCreateTime(),
				user.getNickname(), CommonConstants.ONE_STRING.equals(registrationNow.getFeedbackType())
						? RegistConstants.BEFORE_IN_PROJECT : RegistConstants.BEFORE_OUT_PROJECT);
		SysCspDeptVo vo = new SysCspDeptVo();
		if (CommonConstants.ONE_STRING.equals(registrationNow.getFeedbackType())) {
			if (Common.isNotNull(registrationNow.getEmpDeptid())) {
				vo.setDepartName(registrationNow.getEmpDeptname());
				vo.setDepartId(Long.valueOf(registrationNow.getEmpDeptid()));
				vo.setProjectId(registrationNow.getEmpProjectId());
				archivesDaprUtil.saveOrDeleteDeptRelation(vo);
			}
		} else {
			vo.setProjectId(registration.getEmpProjectId());
			archivesDaprUtil.saveOrDeleteDeptRelation(vo);
		}
		baseMapper.updateById(registrationNow);
		return R.ok();
	}

	@Override
	public R batchUpdatePreById(List<EmployeeRegistrationReceiveVo> employeeRegistList) {
		YifuUser user = SecurityUtils.getUser();
		EmployeeRegistration registrationNow;
		for (EmployeeRegistrationReceiveVo vo : employeeRegistList) {
			registrationNow = baseMapper.selectById(vo.getId());
			if (Common.isEmpty(registrationNow) || !CommonConstants.ONE_STRING.equals(registrationNow.getProcessStatus())) {
				return R.failed(RegistConstants.NO_DATA_TO_HANDLE);
			}
			registrationNow.setProcessStatus(CommonConstants.TWO_STRING);
			logService.saveLog(registrationNow.getId(), CommonConstants.ZERO_STRING, RegistConstants.CONFIRM_SUBMIT, LocalDateTime.now(),
					user.getNickname(), null);
			logService.saveLog(registrationNow.getId(), CommonConstants.ZERO_STRING, RegistConstants.MESSAGE_FINISH, vo.getProjectCreateTime(),
					user.getNickname(), CommonConstants.ONE_STRING.equals(registrationNow.getFeedbackType())
							? RegistConstants.BEFORE_IN_PROJECT : RegistConstants.BEFORE_OUT_PROJECT);
			SysCspDeptVo deptVo = new SysCspDeptVo();
			if (CommonConstants.ONE_STRING.equals(registrationNow.getFeedbackType())) {
				if (Common.isNotNull(registrationNow.getEmpDeptid())) {
					deptVo.setDepartName(registrationNow.getEmpDeptname());
					deptVo.setDepartId(Long.valueOf(registrationNow.getEmpDeptid()));
					deptVo.setProjectId(vo.getEmpProjectId());
					archivesDaprUtil.saveOrDeleteDeptRelation(deptVo);
				}
			} else {
				deptVo.setProjectId(vo.getEmpProjectId());
				archivesDaprUtil.saveOrDeleteDeptRelation(deptVo);
			}
			baseMapper.updateById(registrationNow);
		}
		return R.ok();
	}

	@Override
	public Boolean updateRegistByPreInfo(EmployeeRegistrationUpdateVo updateVo) {
		EmployeeRegistration registration = baseMapper.selectOne(Wrappers.<EmployeeRegistration>query().lambda()
				.eq(EmployeeRegistration::getDeptNo, updateVo.getDeptNo())
				.eq(EmployeeRegistration::getEmpIdcard, updateVo.getEmpIdcard())
				.in(EmployeeRegistration::getProcessStatus, CommonConstants.processStatus)
				.last(CommonConstants.LAST_ONE_SQL));
		if (Common.isNotNull(registration)) {
			registration.setProcessStatus(CommonConstants.THREE_STRING);
			baseMapper.updateById(registration);
			//新增拒绝入职操作记录
			logService.saveInnerLog(registration.getId(), CommonConstants.ZERO_STRING, RegistConstants.BACK_RECEIVE, LocalDateTime.now(),
					updateVo.getEmployeeName(), updateVo.getReason(),updateVo.getEmployeeId());
		}
		return true;
	}

	@Override
	public Boolean updateRegistCustomerUserInfo(EmployeeRegistrationCustomerUserUpdateVo updateVo) {
		LambdaUpdateWrapper<EmployeeRegistration> updateWrapper = new LambdaUpdateWrapper<>();
		updateWrapper.eq(EmployeeRegistration::getDeptNo,updateVo.getDeptNo())
				.eq(EmployeeRegistration::getFeedbackType, CommonConstants.ONE_STRING)
				.set(EmployeeRegistration::getCustomerUsernameNew, updateVo.getCustomerUserName());
		this.update(updateWrapper);
		return true;
	}

	@Override
	public long getRegisterPreCount(EmployeeRegistrationSearchVo employeeRegistration) {
		//权限和条件赋值
		initSearchVo(employeeRegistration);
		employeeRegistration.setStatusList(CommonConstants.processPreStatus);
		employeeRegistration.setTypeList(CommonConstants.processPreStatus);
		return baseMapper.selectExportCount(employeeRegistration);
	}

	//单个新增、导入校验
	private String registCheck(EmployeeRegistration registration) {
		//定义判别用户身份证号的正则表达式（要么是15位，要么是18位，最后一位可以为字母）
		Pattern idNumPattern = compile("(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])");
		//通过Pattern获得Matcher
		Matcher idNumMatcher = idNumPattern.matcher(registration.getEmpIdcard());
		if (!idNumMatcher.matches()) {
			return RegistConstants.IDCARD_FORMAT_ERROR;
		}
		if (!ValidityUtil.validateEmpPhone(registration.getEmpPhone())) {
			return RegistConstants.PHONE_FORMAT_ERROR;
		}
		EmployeeRegistration exit = baseMapper.selectOne(Wrappers.<EmployeeRegistration>query().lambda()
				.eq(EmployeeRegistration::getEmpIdcard, registration.getEmpIdcard())
				.eq(EmployeeRegistration::getDeptNo, registration.getDeptNo())
				.eq(EmployeeRegistration::getFeedbackType, registration.getFeedbackType())
				.in(EmployeeRegistration::getProcessStatus, CommonConstants.processStatus)
				.last(CommonConstants.LAST_ONE_SQL));
		if (Common.isNotNull(exit)) {
			return RegistConstants.REGIST_SAME_CHECK;
		}
		//针对离职的判断人员项目档案状态是否正常
		if (CommonConstants.TWO_STRING.equals(registration.getFeedbackType())) {
			EmpProjectStatusVo vo = new EmpProjectStatusVo();
			vo.setEmpIdcard(registration.getEmpIdcard());
			vo.setDeptNo(registration.getDeptNo());
			EmpProjectStatusVo exitVo = getEmpProjectStatus(vo);
			if (Common.isEmpty(exitVo)) {
				return RegistConstants.EMP_PROJECT_EXIT_CHECK;
			}
		}
		return null;
	}

	//获取人员档案信息
	private EmpProjectStatusVo getEmpProjectStatus(EmpProjectStatusVo vo) {
		//找到不在项人员的身份证信息
		R<EmpProjectStatusVo> sdr = HttpDaprUtil.invokeMethodPost(archivesProperties.getAppUrl(), archivesProperties.getAppId()
				, "/temployeeproject/inner/getEmpProjectStatus", vo, EmpProjectStatusVo.class, SecurityConstants.FROM_IN);
		if (sdr != null && sdr.getData() != null) {
			vo = sdr.getData();
			return vo;
		} else {
			return null;
		}
	}

	//获取减项人员档案信息
	private EmpProjectStatusVo getOutEmpProjectStatus(EmpProjectStatusVo vo) {
		//找到不在项人员的身份证信息
		R<EmpProjectStatusVo> sdr = HttpDaprUtil.invokeMethodPost(archivesProperties.getAppUrl(), archivesProperties.getAppId()
				, "/temployeeproject/inner/getOutEmpProjectStatus", vo, EmpProjectStatusVo.class, SecurityConstants.FROM_IN);
		if (sdr != null && sdr.getData() != null) {
			vo = sdr.getData();
			return vo;
		} else {
			return null;
		}
	}

	//数据初始化
	private void initRegistInfo(EmployeeRegistration insert, YifuUser user, TSettleDomainSelectVo selectVo) {
		SysCspDeptVo vo = new SysCspDeptVo();
		vo.setUserId(user.getId());
		vo.setProjectId(selectVo.getId());
		// 1.9.8: 从客服端导入，入职部门默认为空
		if(CommonConstants.ONE_STRING.equals(insert.getDataSource())){
			R<SysCspDeptVo> booleanR = upmsDaprUtils.selectCspDeptByUserDeptId(vo);
			if (Common.isNotNull(booleanR) && Common.isNotNull(booleanR.getData())
					&& Common.isNotNull(booleanR.getData().getDepartId()) && Common.isNotNull(booleanR.getData().getDepartName())) {
				vo = booleanR.getData();
				insert.setRegistorDeptname(vo.getDepartName());
				insert.setRegistorDeptid(vo.getDepartId().toString());
				insert.setEmpDeptid(vo.getDepartId().toString());
				insert.setEmpDeptname(vo.getDepartName());
			} else {
				insert.setRegistorDeptname(user.getDeptName());
				insert.setRegistorDeptid(user.getDeptId().toString());
				insert.setEmpDeptid(user.getDeptId().toString());
				insert.setEmpDeptname(user.getDeptName());
			}
		}

		insert.setRegistorUsername(user.getNickname());
		insert.setRegistorPhone(user.getPhone());
		insert.setCustomerPhone(selectVo.getCsPhone());
		insert.setCustomerUsername(selectVo.getCsName());
		insert.setCustomerUserLoginname(selectVo.getCsLoginName());
		insert.setDeptName(selectVo.getDepartName());
		insert.setDeptId(selectVo.getId());

		// 1.9.8: 新增字段
		insert.setCustomerUsernameNew(selectVo.getCsName());
	}

	//发送企业微信待办
	private void sendMessageToWx(EmployeeRegistration registration, String type) {
		//获取前端客服
		SysUser user;
		if (Common.isEmpty(registration.getCustomerUserLoginname())) {
			return;
		}
		R<SysUser> res = upmsDaprUtils.getSimpleUserByLoginName(registration.getCustomerUserLoginname());
		if (Common.isNotNull(res) && Common.isNotNull(res.getData())) {
			user = res.getData();
		} else {
			return;
		}
		StringBuilder sendUser = null;
		if (Common.isNotKong(user.getWxMessage())) {
			sendUser = new StringBuilder(user.getWxMessage());
		}
		if (sendUser != null) {
			RestTemplate restTemplate = new RestTemplate();
			Map<String, Object> requestMap = new HashMap<>();
			Map<String, Object> textcard = new HashMap<>();
			String authUrl = String.format(SecurityConstants.WX_GET_MESSAGE_AUTH_URL, wxConfig.getCorpid(), wxConfig.getDomainName() + "/auth/oauth/wxLogin", "66" + registration.getId());
			StringBuilder description = new StringBuilder();
			String title = "";
			if (CommonConstants.ONE_STRING.equals(type) || CommonConstants.TWO_STRING.equals(type)) {
				title = CommonConstants.ONE_STRING.equals(type) ? "人员入职提醒：" : "人员离职提醒：";
				description.append(CommonConstants.ONE_STRING.equals(type) ? "入职日期：" : "离职日期：")
						.append(DateUtil.dateToString(registration.getJoinLeaveDate(), DateUtil.ISO_EXPANDED_DATE_FORMAT)).append("<br>");
				description.append("项目名称：").append(registration.getDeptName()).append("<br>");
				description.append("项目编码：").append(registration.getDeptNo()).append("<br>");
				description.append("员工姓名：").append(registration.getEmployeeName()).append("<br>");
				description.append("身份证号：").append(registration.getEmpIdcard()).append("<br>");
				description.append("手机号码：").append(registration.getEmpPhone()).append("<br>");
			} else {
				title = "人员批量入离职提醒";
				description.append("项目名称：").append(registration.getDeptName()).append("<br>");
				description.append("项目编码：").append(registration.getDeptNo()).append("<br>");
				description.append("入职人数：").append(registration.getInNum()).append("<br>");
				description.append("离职人数：").append(registration.getOutNum()).append("<br>");
			}
			textcard.put("title", title);
			textcard.put("url", authUrl);
			textcard.put("description", description.toString());
			requestMap.put("touser", sendUser);
			requestMap.put("agentid", wxConfig.getAgentid());
			requestMap.put("msgtype", "textcard");
			requestMap.put("textcard", textcard);
			// 必须加上header说明
			if (!wxConfig.sendTextCard(restTemplate, requestMap)) {
				wxConfig.sendTextCard(restTemplate, requestMap);
			}
			//企业微信发送记录
			logService.saveLog(user.getUserId(), CommonConstants.ZERO_STRING, "企业微信消息提醒", LocalDateTime.now(),
					registration.getRegistorUsername(), "提醒人：" + user.getNickname());
		}
	}

	/**
	 * @param searchCspVo 身份证与类型
	 * @Description: 获取已接收的入离职申请，同步所属部门到项目档案里
	 * @Author: hgw
	 * @Date: 2025/3/3 16:57
	 * @return: com.yifu.cloud.plus.v1.yifu.archives.entity.TEmployeeProjectBelongDept
	 **/
	@Override
	public TEmployeeProjectBelongDept getZeroRegistrationAndUpdateTwo(TEmployeeProjectBelongDeptSearchCspVo searchCspVo) {
//		EmployeeRegistration registration = baseMapper.selectOne(Wrappers.<EmployeeRegistration>query().lambda()
//				.eq(EmployeeRegistration::getDeptId, searchCspVo.getDeptId())
//				.eq(EmployeeRegistration::getEmpIdcard, searchCspVo.getEmpIdCard())
//				.eq(EmployeeRegistration::getProcessStatus, CommonConstants.ZERO_STRING)
//				.eq(EmployeeRegistration::getDataSource,CommonConstants.TWO_STRING)
//				.last(CommonConstants.LAST_ONE_SQL));
//		if (Common.isNotNull(registration)) {
//			registration.setProcessStatus(CommonConstants.TWO_STRING);
//			baseMapper.updateById(registration);
//			logService.saveInnerLog(registration.getId(), CommonConstants.ZERO_STRING, RegistConstants.MESSAGE_FINISH, searchCspVo.getEmpCreateAndLeaveTime(),
//					searchCspVo.getEmpCreateAndLeaveUser(),null,searchCspVo.getUserId() );
//			return null;
//		} else {
		List<TEmployeeProjectBelongDept> belongDeptList = baseMapper.getZeroRegistrationByIdCard(searchCspVo);
		if (belongDeptList != null && !belongDeptList.isEmpty()) {
			// 1：加日志
			if (doSaveLogBatch(searchCspVo, belongDeptList)) return null;
			// 更新为已处理
			baseMapper.updateTwoRegistrationByIdCard(searchCspVo);
			return belongDeptList.get(0);
		} else {
			return null;
		}
//		}
	}

	/**
	 * @Description: 批量处理日志
	 * @Author: hgw
	 * @Date: 2025/3/4 10:41
	 * @return: boolean
	 **/
	private boolean doSaveLogBatch(TEmployeeProjectBelongDeptSearchCspVo searchCspVo, List<TEmployeeProjectBelongDept> belongDeptList) {
		TOperationLog log;
		List<TOperationLog> logList = new ArrayList<>();
		String title = "已处理";
		for (TEmployeeProjectBelongDept belongDept : belongDeptList) {
			log = new TOperationLog();
			log.setMainId(belongDept.getId());
			log.setType(CommonConstants.ZERO_STRING);
			log.setTitle(title);
			log.setOperationName(searchCspVo.getEmpCreateAndLeaveUser());
			log.setOperationTime(searchCspVo.getEmpCreateAndLeaveTime());
			if (searchCspVo.getEmpCreateAndLeaveTime() != null && belongDept.getLogCreateTime() != null
					&& searchCspVo.getEmpCreateAndLeaveTime().isBefore(belongDept.getLogCreateTime())) {
				if (CommonConstants.ONE_STRING.equals(searchCspVo.getFeedbackType())) {
					log.setRemark("入职登记前已增员");
				} else {
					log.setRemark("离职登记前已减员");
				}
			}
			log.setCreateBy(searchCspVo.getUserId());
			log.setCreateName(searchCspVo.getUserName());
			log.setCreateTime(LocalDateTime.now());
			logList.add(log);
		}
		if (!logList.isEmpty()) {
			logService.saveBatch(logList);
		}
		return false;
	}

	/**
	 * @Description: 获取部门权限
	 * @Author: huych
	 * @Date: 2025/3/4 10:41
	 * @return:
	 **/
	private void getDtpeAuth(EmployeeRegistrationSearchVo searchVo) {
		YifuUser user = SecurityUtils.getUser();
		if (null != user && Common.isNotNull(user.getId())) {
			List<UserPermissionVo> cspPermList = user.getCspPermMap();
			if (Common.isNotNull(cspPermList)) {
				// 获取用户在当前项目下的权限
				UserPermissionVo authority = null;
				for (UserPermissionVo userPermissionVo : cspPermList) {
					if (searchVo.getDeptNo().equals(userPermissionVo.getProjectNo())) {
						authority = userPermissionVo;
					}
				}
				if (Common.isNotNull(authority) && !authority.isHaveAll()) {
					if (Common.isNotNull(authority.getDepartIdSet())) {
						searchVo.setDepartIdSet(authority.getDepartIdSet());
					} else {
						searchVo.setId(CommonConstants.ONE_STRING_NEGATE);
					}
				}
			}
		}
	}

	/**
	 * @Description: 获取前端客服的所有项目权限
	 * @Author: huych
	 * @Date: 2025/3/13 17:02
	 * @return:
	 **/
	public void initSearchVo(EmployeeRegistrationSearchVo searchVo) {
		YifuUser user = SecurityUtils.getUser();
		long roleId = 1839501715787390978L;
		boolean isSsc = this.haveRole(user, roleId);
		if (isSsc || CommonConstants.ZERO_STRING.equals(user.getSystemFlag())) {
			searchVo.setAuthSql(null);
			return;
		}
		if (Common.isNotNull(searchVo.getType()) && CommonConstants.ONE_STRING.equals(searchVo.getType())) {
			searchVo.setAuthSql(null);
			//获取项目信息
			R<TSettleDomainRegistListVo> domainR = archivesDaprUtil.getAllDeptByCustomerLoginName(user.getUsername());
			if (null != domainR && null != domainR.getData() && null != domainR.getData().getDeptNos() && domainR.getData().getDeptNos().size() > 0) {
				searchVo.setDeptNoList(domainR.getData().getDeptNos());
			} else {
				searchVo.setId(CommonConstants.ONE_STRING_NEGATE);
			}
		}
	}

	public boolean haveRole(YifuUser user, long roleId) {
		List<Long> roleList = user.getClientRoleMap().get(ClientNameConstants.CLIENT_MVP);
		for (Long role : roleList) {
			if (role == roleId) {
				return true;
			}
		}
		return false;
	}

	//入职待建档数据初始化
	private void initRegistPreInfo(EmployeeRegistration registration,EmployeeRegistrationPreVo preVo,
								   YifuUser user,String status) throws ParseException {
		preVo.setId(registration.getId());
		preVo.setCustomerUsername(registration.getCustomerUsername());
		preVo.setCustomerUserLoginname(registration.getCustomerUserLoginname());
		preVo.setDeptName(registration.getDeptName());
		preVo.setDeptId(registration.getDeptId());
		preVo.setDataSource(registration.getDataSource());
		preVo.setPosition(registration.getPosition());
		preVo.setJoinLeaveDate(registration.getJoinLeaveDate());
		preVo.setConfirmTime(DateUtil.getCurrentDateTime());
		preVo.setDeptNo(registration.getDeptNo());
		preVo.setConfirmUser(user.getNickname());
		preVo.setCustomerUsernameNew(registration.getCustomerUsername());
		preVo.setEmpPhone(registration.getEmpPhone());
		preVo.setEmployeeName(registration.getEmployeeName());
		preVo.setEmpIdcard(registration.getEmpIdcard());
		preVo.setCreateBy(user.getId());
		preVo.setCreateName(user.getNickname());
		preVo.setUpdateBy(user.getId());
		preVo.setEmpIdcard(registration.getEmpIdcard());
		preVo.setProcessStatus(status);
		if (Common.isNotNull(preVo.getPushDate())) {
			preVo.setExpectedCollectionTime(DateUtil.parseDate(DateUtil.dateToString(
					registration.getJoinLeaveDate(), DateUtil.ISO_EXPANDED_DATE_FORMAT) + " " + preVo.getPushDate(), DateUtil.DATETIME_PATTERN_MINUTE));
		}
	}

	//商险待购买数据初始化
	private void initInsruancePreInfo(EmployeeRegistration registration,TEmployeeInsurancePreVo preVo,
									  YifuUser user,String status,String id) throws ParseException {
		preVo.setRegisterId(id);
		preVo.setCustomerUsername(registration.getCustomerUsername());
		preVo.setCustomerUserLoginname(registration.getCustomerUserLoginname());
		preVo.setDeptName(registration.getDeptName());
		preVo.setDeptId(registration.getDeptId());
		preVo.setDataSource(registration.getDataSource());
		preVo.setPosition(registration.getPosition());
		preVo.setJoinLeaveDate(registration.getJoinLeaveDate());
		preVo.setDeptNo(registration.getDeptNo());
		preVo.setEmpPhone(registration.getEmpPhone());
		preVo.setEmployeeName(registration.getEmployeeName());
		preVo.setEmpIdcard(registration.getEmpIdcard());
		preVo.setCreateBy(user.getId());
		preVo.setCreateName(user.getNickname());
		preVo.setUpdateBy(user.getId());
		preVo.setEmpIdcard(registration.getEmpIdcard());
		preVo.setProcessStatus(status);
		if (Common.isNotNull(preVo.getPolicyStartType()) && CommonConstants.ONE_STRING.equals(preVo.getPolicyStartType())) {
			preVo.setPolicyStart(preVo.getJoinLeaveDate());
		}
		//预计派单日期
		if (Common.isEmpty(preVo.getExpectedCollectionTime())) {
			TEmployeeInsuranceWorkDayVo dayVo = new TEmployeeInsuranceWorkDayVo();
			dayVo.setType(CommonConstants.ONE_STRING);
			if (preVo.getBuyType() ==1) {
				dayVo.setRegistDate(preVo.getPolicyStart());
			} else {
				dayVo.setRegistDate(preVo.getJoinLeaveDate());
			}
			R<TEmployeeInsuranceWorkDayVo> dataR = socialDaprUtils.getBeforeOrAfterWorkDay(dayVo);
			if (Common.isNotNull(dataR) && Common.isNotNull(dataR.getData())
					&& Common.isNotNull(dataR.getData().getRegistDate())) {
				preVo.setExpectedCollectionTime(dataR.getData().getRegistDate());
			} else {
				preVo.setExpectedCollectionTime(preVo.getPolicyStart());
			}
		}
		preVo.setExpectedCollectionTime(DateUtil.parseDate(DateUtil.dateToString(
				preVo.getExpectedCollectionTime(), DateUtil.ISO_EXPANDED_DATE_FORMAT) + " 14:00", DateUtil.DATETIME_PATTERN_MINUTE));
	}

	//商险的工作日判断 type 1 单个接收 2 批量接收
	private String checkInsuranceBuy(TEmployeeInsurancePreVo preVo,String type) {
		//新增判断保单开始日期
		TEmployeeInsuranceWorkDayVo vo = new TEmployeeInsuranceWorkDayVo();
		R<Boolean> dataR;
		if (preVo.getBuyType() == 1) {
			vo.setRegistDate(preVo.getPolicyStart());
			dataR = socialDaprUtils.checkIsWorkDay(vo);
			if (Common.isNotNull(dataR) && Boolean.TRUE.equals(dataR.getData())) {
				return "新增类型的保单开始日期不能为工作日;";
			}
		}
		if (preVo.getBuyType() == 3  && CommonConstants.ONE_STRING.equals(type)){
			vo.setRegistDate(preVo.getExpectedCollectionTime());
			dataR = socialDaprUtils.checkIsWorkDay(vo);
			if (Common.isNotNull(dataR) && Boolean.TRUE.equals(dataR.getData())) {
				return "批增类型的预计派单日期不能为工作日;";
			}
		}
		if (preVo.getBuyType() == 4 && CommonConstants.ONE_STRING.equals(type)){
			vo.setRegistDate(preVo.getExpectedCollectionTime());
			dataR = socialDaprUtils.checkIsWorkDay(vo);
			if (Common.isNotNull(dataR) && Boolean.TRUE.equals(dataR.getData())) {
				return "替换类型的预计派单日期不能为工作日;";
			}
		}
		return null;
	}

	//商险是否存在判断 type 1 单个接收 2 批量接收
	private String checkInsuranceExit(TEmployeeInsurancePreVo preVo,String type) {

		return null;
	}
}
