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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import com.alibaba.excel.EasyExcelFactory;
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.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.yifu.archives.entity.*;
import com.yifu.cloud.plus.v1.yifu.archives.mapper.TPreEmpMainMapper;
import com.yifu.cloud.plus.v1.yifu.archives.service.*;
import com.yifu.cloud.plus.v1.yifu.archives.utils.ExcelStyle;
import com.yifu.cloud.plus.v1.yifu.archives.vo.*;
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.YifuUser;
import com.yifu.cloud.plus.v1.yifu.common.dapr.config.DaprSalaryProperties;
import com.yifu.cloud.plus.v1.yifu.common.dapr.util.HttpDaprUtil;
import com.yifu.cloud.plus.v1.yifu.common.mybatis.base.BaseEntity;
import com.yifu.cloud.plus.v1.yifu.common.security.util.SecurityUtils;
import com.yifu.cloud.plus.v1.yifu.salary.entity.TSalaryEmployee;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.compress.utils.Lists;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.*;
import java.util.regex.Pattern;

/**
 * 预入职-主表
 *
 * @author hgw
 * @date 2024-06-17 11:31:08
 */
@Log4j2
@AllArgsConstructor
@Service
@EnableConfigurationProperties(DaprSalaryProperties.class)
public class TPreEmpMainServiceImpl extends ServiceImpl<TPreEmpMainMapper, TPreEmpMain> implements TPreEmpMainService {
	// 预入职-员工不良记录表
	private final TPreEmpBadRecordService tPreEmpBadRecordService;
	// 预入职-员工工作履历信息表
	private final TPreEmpWorkRecordingService tPreEmpWorkRecordingService;
	// 预入职-紧急联络人
	private final TPreEmpContactInfoService tPreEmpContactInfoService;
	// 预入职-员工申明
	private final TPreEmpDeclarationService tPreEmpDeclarationService;
	// 预入职-员工伤残信息表
	private final TPreEmpDisabilityInfoService tPreEmpDisabilityInfoService;
	// 预入职-员工学历信息表
	private final TPreEmpEducationService tPreEmpEducationService;
	// 预入职-员工家庭信息表
	private final TPreEmpFamilyService tPreEmpFamilyService;
	// 预入职-人员档案表
	private final TPreEmployeeInfoService tPreEmployeeInfoService;
	// 预入职-项目档案表
	private final TPreEmployeeProjectService tPreEmployeeProjectService;
	// 预入职-员工职业资格信息表
	private final TPreEmpProfessionalQualificationService tPreEmpProfessionalService;
	// 预入职-其他附件
	private final TPreEmpOtherFileService tPreEmpOtherFileService;

	// 附件21（预入职申明签名）；22（预入职确认签名）；23（预入职pdf文件)
	private final TAttaInfoService tAttaInfoService;

	// 档案相关服务：
	// 预入职-员工不良记录表
	private final TEmpBadRecordService tEmpBadRecordService;
	// 预入职-员工工作履历信息表
	private final TEmpWorkRecordingService tEmpWorkRecordingService;
	// 预入职-紧急联络人
	private final TEmpContactInfoService tEmpContactInfoService;
	// 预入职-员工伤残信息表
	private final TEmpDisabilityInfoService tEmpDisabilityInfoService;
	// 预入职-员工学历信息表
	private final TEmpEducationService tEmpEducationService;
	// 预入职-员工家庭信息表
	private final TEmpFamilyService tEmpFamilyService;
	// 预入职-人员档案表
	private final TEmployeeInfoService tEmployeeInfoService;
	// 预入职-项目档案表
	private final TEmployeeProjectService tEmployeeProjectService;
	// 预入职-员工职业资格信息表
	private final TEmpProfessionalQualificationService tEmpProfessionalService;
	// 项目
	private final TSettleDomainService tSettleDomainService;
	// 审核日志
	private final TPreEmpMainLogService tPreEmpMainLogService;
	// 其他附件
	private final TEmpOtherFileService tEmpOtherFileService;

	// 薪资服务，获取计税月份
	private final DaprSalaryProperties salaryProperties;

	private final OSSUtil ossUtil;

	private static final String ID = "id";

	public static final String taxMonthRegex = "^2\\d{5}";

	/**
	 * 预入职-主表简单分页查询
	 *
	 * @param tPreEmpMain 预入职-主表
	 * @return
	 */
	@Override
	public IPage<TPreEmpMain> getTPreEmpMainPage(Page<TPreEmpMain> page, TPreEmpMainSearchVo tPreEmpMain) {
		return baseMapper.getTPreEmpMainPage(page, tPreEmpMain);
	}

	/**
	 * @Description: 详情
	 * @Author: hgw
	 * @Date: 2024/6/17 17:19
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<com.yifu.cloud.plus.v1.yifu.archives.vo.PreEmpMainDetailVo>
	 **/
	@Override
	public R<PreEmpMainDetailVo> getByCardAndDeptId(String empIdCard, String deptId) {
		TSettleDomain dept = tSettleDomainService.getById(deptId);
		if (dept == null) {
			return R.failed("未找到项目信息！");
		}
		TPreEmpMain main = baseMapper.getTPreEmpMainByCardAndDept(empIdCard, deptId);
		TEmployeeInfo employee = null;
		String empId = null;
		TEmployeeProject project;
		PreEmpMainDetailVo vo = new PreEmpMainDetailVo();
		YifuUser user = getNewYifuUser();
		if (main != null && CommonConstants.FIVE_STRING.equals(main.getStatus())) {
			// 清空信息
			if (Common.isNotNull(main.getId())) {
				baseMapper.deleteBadByMainId(main.getId());
				baseMapper.deleteContactByMainId(main.getId());
				baseMapper.deleteDisabilityByMainId(main.getId());
				baseMapper.deleteEducationByMainId(main.getId());
				baseMapper.deleteFamilyByMainId(main.getId());
				baseMapper.deleteEmployeeByMainId(main.getId());
				baseMapper.deleteProjectByMainId(main.getId());
				tPreEmpWorkRecordingService.deleteByMainId(main.getId());
				tPreEmpProfessionalService.deleteProfessionalByMainId(main.getId());
				tPreEmpOtherFileService.deleteOtherFileByMainId(main.getId());
				tAttaInfoService.deleteByDomainId(main.getId());
			}
			baseMapper.deleteById(main);
			main = null;
		}
		if (main != null) {
			// 2024-7-3 17:23:50 倩倩要求放开状态拦截，前端加按钮禁用即可
			/* (CommonConstants.ONE_STRING.equals(main.getStatus())
					|| CommonConstants.TWO_STRING.equals(main.getStatus()))
				retur R.failed("信息审核中，请耐心等待！")

			 (CommonConstants.FOUR_STRING.equals(main.getStatus()))
				retur R.failed("信息已填写，请勿重复操作！")
			*/
			TPreEmployeeInfo tPreEmployeeInfo = tPreEmployeeInfoService.getTPreEmployeeInfoList(main.getId());
			if (tPreEmployeeInfo != null && Common.isNotNull(tPreEmployeeInfo.getId())) {
				empId = tPreEmployeeInfo.getId();
				vo.setTpreEmployeeInfo(tPreEmployeeInfo);
			} else {
				tPreEmployeeInfo = new TPreEmployeeInfo();
				tPreEmployeeInfo.setEmpIdcard(main.getEmpIdcard());
				tPreEmployeeInfo.setEmpName(main.getEmpName());
				tPreEmployeeInfo.setPreMainId(main.getId());
				tPreEmployeeInfo.setCreateBy(user.getId());
				tPreEmployeeInfo.setCreateName(user.getNickname());
				tPreEmployeeInfo.setCreateTime(LocalDateTime.now());
				tPreEmployeeInfoService.save(tPreEmployeeInfo);
				vo.setTpreEmployeeInfo(tPreEmployeeInfo);
			}
			TPreEmployeeProject tPreEmployeeProject = tPreEmployeeProjectService.getTPreEmployeeProjectList(main.getId());
			// 预入职-项目档案表
			if (tPreEmployeeProject != null) {
				vo.setTpreEmployeeProject(tPreEmployeeProject);
			} else {
				tPreEmployeeProject = new TPreEmployeeProject();
				tPreEmployeeProject.setEmpIdcard(main.getEmpIdcard());
				tPreEmployeeProject.setEmpName(main.getEmpName());
				tPreEmployeeProject.setUnitId(dept.getCustomerId());
				tPreEmployeeProject.setUnitName(dept.getCustomerName());
				tPreEmployeeProject.setUnitNo(dept.getCustomerNo());
				tPreEmployeeProject.setBusinessPrimaryType(dept.getBusinessPrimaryType());
				tPreEmployeeProject.setBusinessSecondType(dept.getBusinessSecondType());
				tPreEmployeeProject.setBusinessThirdType(dept.getBusinessThirdType());
				tPreEmployeeProject.setDeptId(main.getDeptId());
				tPreEmployeeProject.setDeptName(main.getDeptName());
				tPreEmployeeProject.setDeptNo(main.getDeptNo());
				tPreEmployeeProject.setPreMainId(main.getId());
				tPreEmployeeProject.setCreateBy(user.getId());
				tPreEmployeeProject.setCreateName(user.getNickname());
				tPreEmployeeProject.setCreateTime(LocalDateTime.now());
				tPreEmployeeProjectService.save(tPreEmployeeProject);
				vo.setTpreEmployeeProject(tPreEmployeeProject);
			}
		} else {
			main = new TPreEmpMain();
			main.setEmpIdcard(empIdCard);
			main.setDeptId(dept.getId());
			main.setDeptName(dept.getDepartName());
			main.setDeptNo(dept.getDepartNo());
			main.setUnitId(dept.getCustomerId());
			main.setUnitName(dept.getCustomerName());
			main.setUnitNo(dept.getCustomerNo());
			main.setBusinessPrimaryType(dept.getBusinessPrimaryType());
			main.setBusinessSecondType(dept.getBusinessSecondType());
			main.setBusinessThirdType(dept.getBusinessThirdType());
			main.setStatus(CommonConstants.FIVE_STRING);
			// 预入职-员工申明
			TPreEmpDeclaration tPreEmpDeclaration;
			if (Common.isNotNull(main.getDeclarationId())) {
				tPreEmpDeclaration = tPreEmpDeclarationService.getById(main.getDeclarationId());
			} else {
				tPreEmpDeclaration = tPreEmpDeclarationService.getTPreEmpDeclarationList();
			}
			vo.setTpreEmpDeclaration(tPreEmpDeclaration);
			main.setDeclarationId(tPreEmpDeclaration.getId());
			this.save(main);
			// 查询档案信息
			employee = tEmployeeInfoService.getInfoByIdcard(empIdCard);
			TPreEmployeeInfo tPreEmployeeInfo;
			TPreEmployeeProject tPreEmployeeProject;
			if (employee != null) {
				empId = employee.getId();
				tPreEmployeeInfo = new TPreEmployeeInfo();
				BeanUtil.copyProperties(employee, tPreEmployeeInfo, ID);
				tPreEmployeeInfo.setOldId(employee.getId());
				tPreEmployeeInfo.setPreMainId(main.getId());
				tPreEmployeeInfo.setCreateBy(user.getId());
				tPreEmployeeInfo.setCreateName(user.getNickname());
				tPreEmployeeInfo.setCreateTime(LocalDateTime.now());
				tPreEmployeeInfoService.saveOrUpdate(tPreEmployeeInfo);
				vo.setTpreEmployeeInfo(tPreEmployeeInfo);
				project = tEmployeeProjectService.getByEmpIdCardAndDeptId(empIdCard, deptId);
				if (project != null) {
					tPreEmployeeProject = new TPreEmployeeProject();
					BeanUtil.copyProperties(project, tPreEmployeeProject, ID);
					tPreEmployeeProject.setOldId(project.getId());
					tPreEmployeeProject.setPreMainId(main.getId());
					tPreEmployeeProject.setCreateBy(user.getId());
					tPreEmployeeProject.setCreateName(user.getNickname());
					tPreEmployeeProject.setCreateTime(LocalDateTime.now());
					tPreEmployeeProjectService.saveOrUpdate(tPreEmployeeProject);
					vo.setTpreEmployeeProject(tPreEmployeeProject);
				}
			} else {
				tPreEmployeeInfo = new TPreEmployeeInfo();
				tPreEmployeeInfo.setEmpIdcard(main.getEmpIdcard());
				tPreEmployeeInfo.setPreMainId(main.getId());
				tPreEmployeeInfo.setCreateBy(user.getId());
				tPreEmployeeInfo.setCreateName(user.getNickname());
				tPreEmployeeInfo.setCreateTime(LocalDateTime.now());
				tPreEmployeeInfoService.save(tPreEmployeeInfo);
				vo.setTpreEmployeeInfo(tPreEmployeeInfo);
				tPreEmployeeProject = new TPreEmployeeProject();
				tPreEmployeeProject.setEmpIdcard(main.getEmpIdcard());
				tPreEmployeeProject.setUnitId(dept.getCustomerId());
				tPreEmployeeProject.setUnitName(dept.getCustomerName());
				tPreEmployeeProject.setUnitNo(dept.getCustomerNo());
				tPreEmployeeProject.setBusinessPrimaryType(dept.getBusinessPrimaryType());
				tPreEmployeeProject.setBusinessSecondType(dept.getBusinessSecondType());
				tPreEmployeeProject.setBusinessThirdType(dept.getBusinessThirdType());
				tPreEmployeeProject.setDeptId(main.getDeptId());
				tPreEmployeeProject.setDeptName(main.getDeptName());
				tPreEmployeeProject.setDeptNo(main.getDeptNo());
				tPreEmployeeProject.setPreMainId(main.getId());
				tPreEmployeeProject.setCreateBy(user.getId());
				tPreEmployeeProject.setCreateName(user.getNickname());
				tPreEmployeeProject.setCreateTime(LocalDateTime.now());
				tPreEmployeeProjectService.save(tPreEmployeeProject);
				vo.setTpreEmployeeProject(tPreEmployeeProject);
			}
		}
		String id = main.getId();
		vo.setMain(main);
		if (vo.getTpreEmpDeclaration() == null) {
			// 预入职-员工申明
			TPreEmpDeclaration tPreEmpDeclaration;
			if (Common.isNotNull(main.getDeclarationId())) {
				tPreEmpDeclaration = tPreEmpDeclarationService.getById(main.getDeclarationId());
			} else {
				tPreEmpDeclaration = tPreEmpDeclarationService.getTPreEmpDeclarationList();
			}
			vo.setTpreEmpDeclaration(tPreEmpDeclaration);
		}
		// 预入职-员工不良记录表
		TPreEmpBadRecord tPreEmpBadRecord = tPreEmpBadRecordService.getTPreEmpBadRecordList(id);
		if (tPreEmpBadRecord == null) {
			tPreEmpBadRecord = new TPreEmpBadRecord();
			if (Common.isNotNull(empId)) {
				TEmpBadRecord info = tEmpBadRecordService.getByEmpId(empId);
				if (info != null) {
					BeanUtil.copyProperties(info, tPreEmpBadRecord, ID);
					tPreEmpBadRecord.setEmpId(empId);
					tPreEmpBadRecord.setOldId(info.getId());
				}
			}
			tPreEmpBadRecord.setPreMainId(id);
			tPreEmpBadRecord.setSettleDomain(deptId);
			tPreEmpBadRecord.setProject(dept.getDepartName());
			tPreEmpBadRecord.setProjectCode(dept.getDepartNo());
			tPreEmpBadRecord.setEmpIdcard(empIdCard);
			tPreEmpBadRecord.setCreateBy(user.getId());
			tPreEmpBadRecord.setCreateName(user.getNickname());
			tPreEmpBadRecord.setCreateTime(LocalDateTime.now());
			tPreEmpBadRecordService.save(tPreEmpBadRecord);
		}
		vo.setTpreEmpBadRecord(tPreEmpBadRecord);
		// 预入职-紧急联络人
		TPreEmpContactInfo tPreEmpContactInfo = tPreEmpContactInfoService.getTPreEmpContactInfoList(id);
		if (tPreEmpContactInfo == null) {
			tPreEmpContactInfo = new TPreEmpContactInfo();
			if (Common.isNotNull(empId)) {
				TEmpContactInfo info = tEmpContactInfoService.getByEmpId(empId);
				if (info != null) {
					BeanUtil.copyProperties(info, tPreEmpContactInfo, ID);
					tPreEmpContactInfo.setEmpId(empId);
					tPreEmpContactInfo.setOldId(info.getId());
				}
			}
			tPreEmpContactInfo.setPreMainId(id);
			tPreEmpContactInfo.setCreateBy(user.getId());
			tPreEmpContactInfo.setCreateName(user.getNickname());
			tPreEmpContactInfo.setCreateTime(LocalDateTime.now());
			tPreEmpContactInfoService.save(tPreEmpContactInfo);
		}
		vo.setTpreEmpContactInfo(tPreEmpContactInfo);
		// 预入职-员工伤残信息表
		TPreEmpDisabilityInfo tPreEmpDisabilityInfo = tPreEmpDisabilityInfoService.getTPreEmpDisabilityInfoList(id);
		if (tPreEmpDisabilityInfo == null) {
			tPreEmpDisabilityInfo = new TPreEmpDisabilityInfo();
			if (Common.isNotNull(empId)) {
				TEmpDisabilityInfo info = tEmpDisabilityInfoService.getByEmpId(empId);
				if (info != null) {
					BeanUtil.copyProperties(info, tPreEmpDisabilityInfo, ID);
					tPreEmpDisabilityInfo.setEmpId(empId);
					tPreEmpDisabilityInfo.setOldId(info.getId());
				}
			}
			tPreEmpDisabilityInfo.setPreMainId(id);
			tPreEmpDisabilityInfo.setSettleDomain(deptId);
			if (employee != null) {
				tPreEmpDisabilityInfo.setEmpId(employee.getId());
				tPreEmpDisabilityInfo.setEmpName(employee.getEmpName());
				tPreEmpDisabilityInfo.setEmpCode(employee.getEmpCode());
			}
			tPreEmpDisabilityInfo.setEmpIdcard(empIdCard);
			tPreEmpDisabilityInfo.setCreateBy(user.getId());
			tPreEmpDisabilityInfo.setCreateName(user.getNickname());
			tPreEmpDisabilityInfo.setCreateTime(LocalDateTime.now());
			tPreEmpDisabilityInfoService.save(tPreEmpDisabilityInfo);
		}
		vo.setTpreEmpDisabilityInfo(tPreEmpDisabilityInfo);
		// 预入职-员工学历信息表
		TPreEmpEducation tPreEmpEducation = tPreEmpEducationService.getTPreEmpEducationList(id);
		if (tPreEmpEducation == null) {
			tPreEmpEducation = new TPreEmpEducation();
			if (Common.isNotNull(empId)) {
				TEmpEducation info = tEmpEducationService.getByEmpId(empId);
				if (info != null) {
					BeanUtil.copyProperties(info, tPreEmpEducation, ID);
					tPreEmpEducation.setEmpId(empId);
					tPreEmpEducation.setOldId(info.getId());

					// 附件
					List<TAttaInfo> attaInfoList = tAttaInfoService.getTAttaInfoListByDoMainId(info.getId());
					if (attaInfoList != null) {
						URL url;
						for (TAttaInfo atta : attaInfoList) {
							url = ossUtil.getObjectUrl(null, atta.getAttaSrc());
							atta.setAttaUrl(String.valueOf(url));
						}
					}
					tPreEmpEducation.setAttaList(attaInfoList);
				}
			}
			tPreEmpEducation.setPreMainId(id);
			tPreEmpEducation.setSettleDomain(deptId);
			if (employee != null) {
				tPreEmpEducation.setEmpId(employee.getId());
				tPreEmpEducation.setEmpName(employee.getEmpName());
				tPreEmpEducation.setEmpCode(employee.getEmpCode());
			}
			tPreEmpEducation.setEmpIdcard(empIdCard);
			tPreEmpEducation.setCreateBy(user.getId());
			tPreEmpEducation.setCreateName(user.getNickname());
			tPreEmpEducation.setCreateTime(LocalDateTime.now());
			tPreEmpEducationService.save(tPreEmpEducation);
		} else {
			// 附件
			List<TAttaInfo> attaInfoList = tAttaInfoService.getTAttaInfoListByDoMainId(tPreEmpEducation.getId());
			if (attaInfoList != null) {
				URL url;
				for (TAttaInfo atta : attaInfoList) {
					url = ossUtil.getObjectUrl(null, atta.getAttaSrc());
					atta.setAttaUrl(String.valueOf(url));
				}
			}
			tPreEmpEducation.setAttaList(attaInfoList);
		}
		vo.setTpreEmpEducation(tPreEmpEducation);
		// 预入职-员工家庭信息表
		List<TPreEmpFamily> tPreEmpFamilyList = tPreEmpFamilyService.getTPreEmpFamilyList(id);
		if (tPreEmpFamilyList == null || tPreEmpFamilyList.isEmpty()) {
			tPreEmpFamilyList = new ArrayList<>();
			if (Common.isNotNull(empId)) {
				List<TEmpFamily> infoList = tEmpFamilyService.getListByEmpId(empId);
				if (infoList != null && !infoList.isEmpty()) {
					TPreEmpFamily record;
					for (TEmpFamily info : infoList) {
						record = new TPreEmpFamily();
						BeanUtil.copyProperties(info, record, ID);
						record.setEmpId(empId);
						record.setOldId(info.getId());
						record.setSettleDomain(deptId);
						if (employee != null) {
							record.setEmpId(employee.getId());
							record.setEmpName(employee.getEmpName());
							record.setEmpCode(employee.getEmpCode());
						}
						record.setEmpIdcard(empIdCard);
						record.setPreMainId(id);
						if (Common.isEmpty(record.getCreateBy())) {
							record.setCreateBy(user.getId());
							record.setCreateName(user.getNickname());
							record.setCreateTime(LocalDateTime.now());
						}
						tPreEmpFamilyList.add(record);
					}
					tPreEmpFamilyService.saveBatch(tPreEmpFamilyList);
				}
			}
			if (tPreEmpFamilyList.isEmpty()) {
				TPreEmpFamily record = new TPreEmpFamily();
				record.setSettleDomain(deptId);
				if (employee != null) {
					record.setEmpId(employee.getId());
					record.setEmpName(employee.getEmpName());
					record.setEmpCode(employee.getEmpCode());
				}
				record.setEmpIdcard(empIdCard);
				record.setPreMainId(id);
				record.setCreateBy(user.getId());
				record.setCreateName(user.getNickname());
				record.setCreateTime(LocalDateTime.now());
				tPreEmpFamilyService.save(record);
				tPreEmpFamilyList.add(record);
			}
		}
		vo.setTpreEmpFamilyList(tPreEmpFamilyList);
		// 预入职-人员档案表
		TPreEmployeeInfo tPreEmployeeInfo;
		if (vo.getTpreEmployeeInfo() == null) {
			tPreEmployeeInfo = tPreEmployeeInfoService.getTPreEmployeeInfoList(id);
			if (tPreEmployeeInfo == null) {
				tPreEmployeeInfo = new TPreEmployeeInfo();
				tPreEmployeeInfo.setEmpIdcard(main.getEmpIdcard());
				tPreEmployeeInfo.setPreMainId(main.getId());
				tPreEmployeeInfo.setCreateBy(user.getId());
				tPreEmployeeInfo.setCreateName(user.getNickname());
				tPreEmployeeInfo.setCreateTime(LocalDateTime.now());
				tPreEmployeeInfoService.save(tPreEmployeeInfo);
			}
			vo.setTpreEmployeeInfo(tPreEmployeeInfo);
		}
		tPreEmployeeInfo = vo.getTpreEmployeeInfo();
		if (tPreEmployeeInfo != null && Common.isEmpty(tPreEmployeeInfo.getBankNo()) && Common.isNotNull(tPreEmployeeInfo.getEmpIdcard())) {
			// 去薪资服务获取卡号
			R<TSalaryEmployee> sdr = HttpDaprUtil.invokeMethodPost(salaryProperties.getAppUrl(), salaryProperties.getAppId()
					, "/tsalaryemployee/inner/getSalaryEmployee", tPreEmployeeInfo.getEmpIdcard(), TSalaryEmployee.class, SecurityConstants.FROM_IN);
			if (sdr != null && sdr.getData() != null) {
				TSalaryEmployee tSalaryEmployee = sdr.getData();
				if (tSalaryEmployee != null && Common.isNotNull(tSalaryEmployee.getBankNo())) {
					tPreEmployeeInfo.setBankNo(tSalaryEmployee.getBankNo());
					tPreEmployeeInfo.setBankName(tSalaryEmployee.getBankName());
					tPreEmployeeInfo.setBankSubName(tSalaryEmployee.getBankSubName());
					tPreEmployeeInfo.setBankProvince(tSalaryEmployee.getBankProvince());
					tPreEmployeeInfo.setBankCity(tSalaryEmployee.getBankCity());
				}
			}
		}
		// 预入职-项目档案表
		if (vo.getTpreEmployeeProject() == null) {
			TPreEmployeeProject tPreEmployeeProject = tPreEmployeeProjectService.getTPreEmployeeProjectList(id);
			if (tPreEmployeeProject == null) {
				tPreEmployeeProject = new TPreEmployeeProject();
				tPreEmployeeProject.setEmpIdcard(main.getEmpIdcard());
				tPreEmployeeProject.setUnitId(dept.getCustomerId());
				tPreEmployeeProject.setUnitName(dept.getCustomerName());
				tPreEmployeeProject.setUnitNo(dept.getCustomerNo());
				tPreEmployeeProject.setBusinessPrimaryType(dept.getBusinessPrimaryType());
				tPreEmployeeProject.setBusinessSecondType(dept.getBusinessSecondType());
				tPreEmployeeProject.setBusinessThirdType(dept.getBusinessThirdType());
				tPreEmployeeProject.setDeptId(main.getDeptId());
				tPreEmployeeProject.setDeptName(main.getDeptName());
				tPreEmployeeProject.setDeptNo(main.getDeptNo());
				tPreEmployeeProject.setPreMainId(main.getId());
				tPreEmployeeProject.setCreateBy(user.getId());
				tPreEmployeeProject.setCreateName(user.getNickname());
				tPreEmployeeProject.setCreateTime(LocalDateTime.now());
				tPreEmployeeProjectService.save(tPreEmployeeProject);
			}
			vo.setTpreEmployeeProject(tPreEmployeeProject);
		}
		// 预入职-员工职业资格信息表
		List<TPreEmpProfessionalQualification> tPreEmpProfessionalQualificationList = tPreEmpProfessionalService.getTPreEmpProfessionalQualificationList(id);
		if (tPreEmpProfessionalQualificationList == null || tPreEmpProfessionalQualificationList.isEmpty()) {
			tPreEmpProfessionalQualificationList = new ArrayList<>();
			if (Common.isNotNull(empId)) {
				List<TEmpProfessionalQualification> infoList = tEmpProfessionalService.getListByEmpId(empId);
				TPreEmpProfessionalQualification record;
				if (infoList != null && !infoList.isEmpty()) {
					for (TEmpProfessionalQualification info : infoList) {
						record = new TPreEmpProfessionalQualification();
						BeanUtil.copyProperties(info, record, ID);
						record.setEmpId(empId);
						record.setOldId(info.getId());
						record.setSettleDomain(deptId);
						if (employee != null) {
							record.setEmpId(employee.getId());
							record.setEmpName(employee.getEmpName());
							record.setEmpCode(employee.getEmpCode());
						}

						// 附件
						List<TAttaInfo> attaInfoList = tAttaInfoService.getTAttaInfoListByDoMainId(info.getId());
						if (attaInfoList != null) {
							URL url;
							for (TAttaInfo atta : attaInfoList) {
								url = ossUtil.getObjectUrl(null, atta.getAttaSrc());
								atta.setAttaUrl(String.valueOf(url));
							}
						}
						record.setAttaList(attaInfoList);
						record.setPreMainId(id);
						record.setEmpIdcard(empIdCard);
						if (Common.isEmpty(record.getCreateBy())) {
							record.setCreateBy(user.getId());
							record.setCreateName(user.getNickname());
							record.setCreateTime(LocalDateTime.now());
						}
						tPreEmpProfessionalQualificationList.add(record);
					}
					tPreEmpProfessionalService.saveBatch(tPreEmpProfessionalQualificationList);
				}
			}
			if (tPreEmpProfessionalQualificationList.isEmpty()) {
				TPreEmpProfessionalQualification record = new TPreEmpProfessionalQualification();
				record.setSettleDomain(deptId);
				if (employee != null) {
					record.setEmpId(employee.getId());
					record.setEmpName(employee.getEmpName());
					record.setEmpCode(employee.getEmpCode());
				}
				record.setEmpIdcard(empIdCard);
				record.setPreMainId(id);
				record.setAttaList(new ArrayList<>());
				record.setCreateBy(user.getId());
				record.setCreateName(user.getNickname());
				record.setCreateTime(LocalDateTime.now());
				tPreEmpProfessionalService.save(record);
				tPreEmpProfessionalQualificationList.add(record);
			}
		} else {
			for (TPreEmpProfessionalQualification info : tPreEmpProfessionalQualificationList) {
				// 附件
				List<TAttaInfo> attaInfoList = tAttaInfoService.getTAttaInfoListByDoMainId(info.getId());
				if (attaInfoList != null) {
					URL url;
					for (TAttaInfo atta : attaInfoList) {
						url = ossUtil.getObjectUrl(null, atta.getAttaSrc());
						atta.setAttaUrl(String.valueOf(url));
					}
				}
				info.setAttaList(attaInfoList);
			}
		}
		vo.setTpreEmpProfessionalQualificationList(tPreEmpProfessionalQualificationList);

		// 预入职-其他附件
		List<TPreEmpOtherFile> tPreEmpOtherFileList = tPreEmpOtherFileService.getTPreEmpOtherFileList(id);
		if (tPreEmpOtherFileList == null || tPreEmpOtherFileList.isEmpty()) {
			tPreEmpOtherFileList = new ArrayList<>();
			if (Common.isNotNull(empId)) {
				List<TEmpOtherFile> infoList = tEmpOtherFileService.getListByEmpId(empId);
				TPreEmpOtherFile record;
				if (infoList != null && !infoList.isEmpty()) {
					for (TEmpOtherFile info : infoList) {
						record = new TPreEmpOtherFile();
						BeanUtil.copyProperties(info, record, ID);
						record.setEmpId(empId);
						record.setOldId(info.getId());
						// 附件
						List<TAttaInfo> attaInfoList = tAttaInfoService.getTAttaInfoListByDoMainId(info.getId());
						if (attaInfoList != null) {
							URL url;
							for (TAttaInfo atta : attaInfoList) {
								url = ossUtil.getObjectUrl(null, atta.getAttaSrc());
								atta.setAttaUrl(String.valueOf(url));
							}
						}
						record.setAttaList(attaInfoList);
						record.setPreMainId(id);
						if (Common.isEmpty(record.getCreateTime())) {
							record.setCreateTime(LocalDateTime.now());
						}
						tPreEmpOtherFileList.add(record);
					}
					tPreEmpOtherFileService.saveBatch(tPreEmpOtherFileList);
				}
			}
			if (tPreEmpOtherFileList.isEmpty()) {
				TPreEmpOtherFile record = new TPreEmpOtherFile();
				if (employee != null) {
					record.setEmpId(employee.getId());
				}
				record.setCreateTime(LocalDateTime.now());
				record.setPreMainId(id);
				record.setAttaList(new ArrayList<>());
				tPreEmpOtherFileService.save(record);
				tPreEmpOtherFileList.add(record);
			}
		} else {
			for (TPreEmpOtherFile info : tPreEmpOtherFileList) {
				// 附件
				List<TAttaInfo> attaInfoList = tAttaInfoService.getTAttaInfoListByDoMainId(info.getId());
				if (attaInfoList != null) {
					URL url;
					for (TAttaInfo atta : attaInfoList) {
						url = ossUtil.getObjectUrl(null, atta.getAttaSrc());
						atta.setAttaUrl(String.valueOf(url));
					}
				}
				info.setAttaList(attaInfoList);
			}
		}
		vo.setTpreEmpOtherFileList(tPreEmpOtherFileList);
		// 预入职-员工工作履历信息表
		List<TPreEmpWorkRecording> tPreEmpWorkRecordingList = tPreEmpWorkRecordingService.getTPreEmpWorkRecordingList(id);
		if (tPreEmpWorkRecordingList == null || tPreEmpWorkRecordingList.isEmpty()) {
			tPreEmpWorkRecordingList = new ArrayList<>();
			if (Common.isNotNull(empId)) {
				List<TEmpWorkRecording> infoList = tEmpWorkRecordingService.getListByEmpId(empId);
				TPreEmpWorkRecording record;
				if (infoList != null && !infoList.isEmpty()) {
					for (TEmpWorkRecording info : infoList) {
						record = new TPreEmpWorkRecording();
						BeanUtil.copyProperties(info, record, ID);
						record.setEmpId(empId);
						record.setOldId(info.getId());
						record.setSettleDomain(deptId);
						if (employee != null) {
							record.setEmpId(employee.getId());
							record.setEmpName(employee.getEmpName());
							record.setEmpCode(employee.getEmpCode());
						}
						record.setEmpIdcard(empIdCard);
						record.setPreMainId(id);
						if (Common.isEmpty(record.getCreateBy())) {
							record.setCreateBy(user.getId());
							record.setCreateName(user.getNickname());
							record.setCreateTime(LocalDateTime.now());
						}
						tPreEmpWorkRecordingList.add(record);
					}
					tPreEmpWorkRecordingService.saveBatch(tPreEmpWorkRecordingList);
				}
			}
			if (tPreEmpWorkRecordingList.isEmpty()) {
				TPreEmpWorkRecording record = new TPreEmpWorkRecording();
				record.setSettleDomain(deptId);
				if (employee != null) {
					record.setEmpId(employee.getId());
					record.setEmpName(employee.getEmpName());
					record.setEmpCode(employee.getEmpCode());
				}
				record.setEmpIdcard(empIdCard);
				record.setPreMainId(id);
				record.setCreateBy(user.getId());
				record.setCreateName(user.getNickname());
				record.setCreateTime(LocalDateTime.now());
				tPreEmpWorkRecordingService.save(record);
				tPreEmpWorkRecordingList.add(record);
			}
		}
		vo.setTpreEmpWorkRecordingList(tPreEmpWorkRecordingList);

		// 预入职-附件 9身份证 10 户口本；21（预入职申明签名）；22（预入职确认签名）；23（预入职pdf文件)
		List<TAttaInfo> attaInfoList = tAttaInfoService.getTAttaInfoListByDoMainId(id);
		if ((attaInfoList == null || attaInfoList.isEmpty()) && Common.isNotNull(empId)) {
			attaInfoList = tAttaInfoService.getTAttaInfoListByDoMainId(empId);
		}
		if (attaInfoList != null && !attaInfoList.isEmpty()) {
			URL url;
			for (TAttaInfo atta : attaInfoList) {
				url = ossUtil.getObjectUrl(null, atta.getAttaSrc());
				atta.setAttaUrl(String.valueOf(url));
			}
		}
		vo.setAttaInfoList(attaInfoList);
		return R.ok(vo);

	}

	/**
	 * @Description: 详情
	 * @Author: hgw
	 * @Date: 2024/6/17 17:19
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<com.yifu.cloud.plus.v1.yifu.archives.vo.PreEmpMainDetailVo>
	 **/
	@Override
	public R<PreEmpMainDetailVo> getInfoById(String id) {
		TPreEmpMain main = this.getById(id);
		PreEmpMainDetailVo vo = new PreEmpMainDetailVo();
		if (main != null && main.getId() != null) {
			vo.setMain(main);
			// 预入职-员工不良记录表
			TPreEmpBadRecord tPreEmpBadRecord = tPreEmpBadRecordService.getTPreEmpBadRecordList(id);
			vo.setTpreEmpBadRecord(tPreEmpBadRecord);
			// 预入职-紧急联络人
			TPreEmpContactInfo tPreEmpContactInfo = tPreEmpContactInfoService.getTPreEmpContactInfoList(id);
			vo.setTpreEmpContactInfo(tPreEmpContactInfo);
			// 预入职-员工申明
			TPreEmpDeclaration tPreEmpDeclaration;
			if (Common.isNotNull(main.getDeclarationId())) {
				tPreEmpDeclaration = tPreEmpDeclarationService.getById(main.getDeclarationId());
			} else {
				tPreEmpDeclaration = tPreEmpDeclarationService.getTPreEmpDeclarationList();
			}
			vo.setTpreEmpDeclaration(tPreEmpDeclaration);
			// 预入职-员工伤残信息表
			TPreEmpDisabilityInfo tPreEmpDisabilityInfo = tPreEmpDisabilityInfoService.getTPreEmpDisabilityInfoList(id);
			vo.setTpreEmpDisabilityInfo(tPreEmpDisabilityInfo);
			// 预入职-员工学历信息表
			TPreEmpEducation tPreEmpEducation = tPreEmpEducationService.getTPreEmpEducationList(id);
			if (tPreEmpEducation != null) {
				List<TAttaInfo> attaInfoList = tAttaInfoService.getTAttaInfoListByDoMainId(tPreEmpEducation.getId());
				if (attaInfoList != null) {
					URL url;
					for (TAttaInfo atta : attaInfoList) {
						url = ossUtil.getObjectUrl(null, atta.getAttaSrc());
						atta.setAttaUrl(String.valueOf(url));
					}
				}
				tPreEmpEducation.setAttaList(attaInfoList);
			}
			vo.setTpreEmpEducation(tPreEmpEducation);
			// 预入职-员工家庭信息表
			List<TPreEmpFamily> tPreEmpFamily = tPreEmpFamilyService.getTPreEmpFamilyList(id);
			vo.setTpreEmpFamilyList(tPreEmpFamily);
			// 预入职-人员档案表
			TPreEmployeeInfo tPreEmployeeInfo = tPreEmployeeInfoService.getTPreEmployeeInfoList(id);
			vo.setTpreEmployeeInfo(tPreEmployeeInfo);
			// 预入职-项目档案表
			TPreEmployeeProject tPreEmployeeProject = tPreEmployeeProjectService.getTPreEmployeeProjectList(id);
			vo.setTpreEmployeeProject(tPreEmployeeProject);
			// 预入职-员工职业资格信息表
			List<TPreEmpProfessionalQualification> tPreEmpProfessionalQualificationList = tPreEmpProfessionalService.getTPreEmpProfessionalQualificationList(id);

			if (tPreEmpProfessionalQualificationList != null) {
				List<TAttaInfo> attaInfoList;
				for (TPreEmpProfessionalQualification info : tPreEmpProfessionalQualificationList) {
					attaInfoList = tAttaInfoService.getTAttaInfoListByDoMainId(info.getId());
					if (attaInfoList != null) {
						URL url;
						for (TAttaInfo atta : attaInfoList) {
							url = ossUtil.getObjectUrl(null, atta.getAttaSrc());
							atta.setAttaUrl(String.valueOf(url));
						}
						info.setAttaList(attaInfoList);
					}
				}
			}

			vo.setTpreEmpProfessionalQualificationList(tPreEmpProfessionalQualificationList);

			// 预入职-其他附件
			List<TPreEmpOtherFile> tPreEmpOtherFileList = tPreEmpOtherFileService.getTPreEmpOtherFileList(id);
			if (tPreEmpOtherFileList != null && !tPreEmpOtherFileList.isEmpty()) {
				List<TAttaInfo> attaInfoList;
				URL url;
				for (TPreEmpOtherFile info : tPreEmpOtherFileList) {
					// 附件
					attaInfoList = tAttaInfoService.getTAttaInfoListByDoMainId(info.getId());
					if (attaInfoList != null) {
						for (TAttaInfo atta : attaInfoList) {
							url = ossUtil.getObjectUrl(null, atta.getAttaSrc());
							atta.setAttaUrl(String.valueOf(url));
						}
					}
					info.setAttaList(attaInfoList);
				}
			}
			vo.setTpreEmpOtherFileList(tPreEmpOtherFileList);

			// 预入职-员工工作履历信息表
			List<TPreEmpWorkRecording> tPreEmpWorkRecording = tPreEmpWorkRecordingService.getTPreEmpWorkRecordingList(id);
			vo.setTpreEmpWorkRecordingList(tPreEmpWorkRecording);

			// 预入职-附件 9身份证 10 户口本；21（预入职申明签名）；22（预入职确认签名）；23（预入职pdf文件)
			List<TAttaInfo> attaInfoList = tAttaInfoService.getTAttaInfoListByDoMainId(id);
			if (attaInfoList != null) {
				URL url;
				for (TAttaInfo atta : attaInfoList) {
					url = ossUtil.getObjectUrl(null, atta.getAttaSrc());
					atta.setAttaUrl(String.valueOf(url));
				}
			}
			vo.setAttaInfoList(attaInfoList);
			return R.ok(vo);
		} else {
			return R.failed("根据ID，未找到预入职主表！");
		}
	}

	/**
	 * @Description: 单个完善信息
	 * @Author: hgw
	 * @Date: 2024/6/17 17:42
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.lang.Boolean>
	 **/
	@Override
	public R<String> doImproveInformation(PreEmpMainDetailVo vo) {
		TPreEmpMain preMain = vo.getMain();
		TPreEmployeeInfo tPreEmployee = vo.getTpreEmployeeInfo();
		TPreEmployeeInfo oldPreEmployee = tPreEmployeeInfoService.getTPreEmployeeInfoList(preMain.getId());
		TPreEmployeeProject project = vo.getTpreEmployeeProject();
		if (preMain == null || tPreEmployee == null || project == null ||
				Common.isEmpty(preMain.getId()) || Common.isEmpty(tPreEmployee.getId()) || Common.isEmpty(project.getId())) {
			return R.failed("请传主表ID与人员ID与项目ID");
		}
		TPreEmpMain main = this.getById(preMain.getId());
		if (!CommonConstants.ONE_STRING.equals(main.getStatus())) {
			return R.failed("状态不为待完善，不可编辑！");
		}
		String errorInfo = null;
		if (Common.isEmpty(tPreEmployee.getEmpNatrue())) {
			errorInfo = "【员工类型】";
		}
		if (Common.isEmpty(tPreEmployee.getFileProvince())) {
			errorInfo += "【档案地省】";
		}
		if (Common.isNotNull(errorInfo)) {
			return R.failed(errorInfo + "不可为空！");
		}
		// 计税月份
		if (Common.isNotNull(tPreEmployee.getBankNo()) && Common.isEmpty(tPreEmployee.getTaxMonth())) {
			return R.failed("有银行卡号，计税月份必填！");
		}
		if (Common.isNotNull(tPreEmployee.getTaxMonth())) {
			String taxMonth = tPreEmployee.getTaxMonth().trim();
			if (!taxMonth.matches(taxMonthRegex)) {
				return R.failed("计税月份为6位年月，例如：202407！");
			}
			if (Common.isNotNull(tPreEmployee.getTaxMonthFlag()) && CommonConstants.ZERO_STRING.equals(tPreEmployee.getTaxMonthFlag())
					&& oldPreEmployee != null && !oldPreEmployee.getTaxMonth().equals(tPreEmployee.getTaxMonth()) ) {
				return R.failed("本年度有发薪纪录，计税月份不可编辑！");
			}
		}

		tPreEmployee.setMajor(oldPreEmployee.getMajor());
		tPreEmployee.setSchool(oldPreEmployee.getSchool());
		tPreEmployee.setAdmissionDate(oldPreEmployee.getAdmissionDate());
		tPreEmployee.setGradutionDate(oldPreEmployee.getGradutionDate());

		tPreEmployeeInfoService.updateById(tPreEmployee);
		project.setEmpNatrue(tPreEmployee.getEmpNatrue());
		tPreEmployeeProjectService.updateById(project);

		main.setEmpNatrue(tPreEmployee.getEmpNatrue());
		main.setStatus(CommonConstants.TWO_STRING);
		this.updateById(main);
		return R.ok("已完善！");
	}

	/**
	 * @param vo
	 * @Description: 草稿
	 * @Author: hgw
	 * @Date: 2024/6/17 17:42
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.lang.Boolean>
	 **/
	@Override
	@Transactional
	public R<String> saveDraftPreEmpMain(PreEmpMainDetailVo vo) {
		return saveBase(vo, CommonConstants.ZERO_STRING);
	}

	/**
	 * @param vo
	 * @Description: 保存
	 * @Author: hgw
	 * @Date: 2024/6/17 17:42
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.lang.Boolean>
	 **/
	@Override
	@Transactional
	public R<String> savePreEmpMain(PreEmpMainDetailVo vo) {
		return saveBase(vo, CommonConstants.ONE_STRING);
	}


	/**
	 * @Description: 核心保存
	 * @Author: hgw
	 * @Date: 2024/6/17 17:44
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.lang.String>
	 **/
	private R<String> saveBase(PreEmpMainDetailVo vo, String status) {
		TPreEmpMain main = vo.getMain();
		try {
			YifuUser user = getNewYifuUser();
			String idCard = main.getEmpIdcard();
			if (Common.isNotNull(idCard)) {
				TPreEmployeeProject tPreEmployeeProject = vo.getTpreEmployeeProject();
				if (tPreEmployeeProject != null && Common.isNotNull(tPreEmployeeProject.getOldId())) {
					// 预入职-人员档案表
					TPreEmployeeInfo tPreEmployeeInfo = vo.getTpreEmployeeInfo();
					if (tPreEmployeeInfo != null && Common.isNotNull(tPreEmployeeInfo.getOldId())) {
						// 转到更新档案去
						return updateToEmployeeByPre(vo);
					} else {
						return R.failed("档案原ID不可为空！");
					}
				} else {
					main.setStatus(status);
					main.setCreateBy(user.getId());
					main.setCreateName(user.getNickname());
					main.setCreateTime(LocalDateTime.now());
					this.saveOrUpdate(main);
					String mainId = main.getId();

					// 预入职-员工不良记录表
					TPreEmpBadRecord tPreEmpBadRecord = vo.getTpreEmpBadRecord();
					if (tPreEmpBadRecord != null) {
						tPreEmpBadRecord.setPreMainId(mainId);
						tPreEmpBadRecord.setCreateBy(user.getId());
						tPreEmpBadRecord.setCreateName(user.getNickname());
						tPreEmpBadRecord.setCreateTime(LocalDateTime.now());
						tPreEmpBadRecordService.saveOrUpdate(tPreEmpBadRecord);
					}
					// 预入职-紧急联络人
					TPreEmpContactInfo tPreEmpContactInfo = vo.getTpreEmpContactInfo();
					if (tPreEmpContactInfo != null) {
						tPreEmpContactInfo.setPreMainId(mainId);
						tPreEmpContactInfo.setCreateBy(user.getId());
						tPreEmpContactInfo.setCreateName(user.getNickname());
						tPreEmpContactInfo.setCreateTime(LocalDateTime.now());
						tPreEmpContactInfoService.saveOrUpdate(tPreEmpContactInfo);
					}
					// 预入职-员工伤残信息表
					TPreEmpDisabilityInfo tPreEmpDisabilityInfo = vo.getTpreEmpDisabilityInfo();
					if (tPreEmpDisabilityInfo != null) {
						tPreEmpDisabilityInfo.setPreMainId(mainId);
						tPreEmpDisabilityInfo.setCreateBy(user.getId());
						tPreEmpDisabilityInfo.setCreateName(user.getNickname());
						tPreEmpDisabilityInfo.setCreateTime(LocalDateTime.now());
						tPreEmpDisabilityInfoService.saveOrUpdate(tPreEmpDisabilityInfo);
					}
					// 预入职-员工学历信息表
					TPreEmpEducation tPreEmpEducation = vo.getTpreEmpEducation();
					if (tPreEmpEducation != null) {
						tPreEmpEducation.setPreMainId(mainId);
						tPreEmpEducation.setCreateBy(user.getId());
						tPreEmpEducation.setCreateName(user.getNickname());
						tPreEmpEducation.setCreateTime(LocalDateTime.now());
						tPreEmpEducationService.saveOrUpdate(tPreEmpEducation);
						// 附件
						tAttaInfoService.deleteByDomainIdAndType(tPreEmpEducation.getId(), 0);
						if (tPreEmpEducation.getAttaList() != null && !tPreEmpEducation.getAttaList().isEmpty()) {
							List<TAttaInfo> attaList = tPreEmpEducation.getAttaList();
							if (attaList != null) {
								for (TAttaInfo atta : attaList) {
									atta.setId(null);
									atta.setDomainId(tPreEmpEducation.getId());
									atta.setCreateBy(user.getId());
									atta.setCreateName(user.getNickname());
									atta.setCreateTime(LocalDateTime.now());
								}
								tAttaInfoService.saveBatch(attaList);
							}
						}
					}
					// 预入职-员工家庭信息表
					List<TPreEmpFamily> tPreEmpFamily = vo.getTpreEmpFamilyList();
					if (tPreEmpFamily != null) {
						tPreEmpFamilyService.deleteFamilyByMainId(mainId);
						for (TPreEmpFamily info : tPreEmpFamily) {
							info.setEmpIdcard(idCard);
							info.setPreMainId(mainId);
							info.setCreateBy(user.getId());
							info.setCreateName(user.getNickname());
							info.setCreateTime(LocalDateTime.now());
						}
						tPreEmpFamilyService.saveOrUpdateBatch(tPreEmpFamily);
					}
					// 预入职-人员档案表
					TPreEmployeeInfo tPreEmployeeInfo = vo.getTpreEmployeeInfo();
					if (tPreEmployeeInfo != null) {
						if (tPreEmpEducation != null) {
							if (Common.isNotNull(tPreEmpEducation.getEducationName())) {
								// 5大专 6本科 7硕士 8博士
								if ("5".equals(tPreEmpEducation.getEducationName())
										|| "6".equals(tPreEmpEducation.getEducationName())
										|| "7".equals(tPreEmpEducation.getEducationName())
										|| "8".equals(tPreEmpEducation.getEducationName())) {
									tPreEmployeeInfo.setIsCollege(CommonConstants.ONE_INT);
								} else {
									tPreEmployeeInfo.setIsCollege(CommonConstants.ZERO_INT);
								}
								tPreEmployeeInfo.setHignEducation(tPreEmpEducation.getEducationName());
							}
							tPreEmployeeInfo.setSchool(tPreEmpEducation.getSchool());
							tPreEmployeeInfo.setMajor(tPreEmpEducation.getMajor());
							tPreEmployeeInfo.setAdmissionDate(tPreEmpEducation.getEntryDate());
							tPreEmployeeInfo.setGradutionDate(tPreEmpEducation.getGradutionDate());
						}
						// 生成性别年龄等：
						this.setBaseSexInfo(tPreEmployeeInfo);
						tPreEmployeeInfo.setPreMainId(mainId);
						tPreEmployeeInfo.setCreateBy(user.getId());
						tPreEmployeeInfo.setCreateName(user.getNickname());
						tPreEmployeeInfo.setCreateTime(LocalDateTime.now());

						// 塞计税月份标志
						this.setTaxMonthAndFlag(status, tPreEmployeeInfo);

						tPreEmployeeInfoService.saveOrUpdate(tPreEmployeeInfo);
					}
					// 预入职-项目档案表
					if (tPreEmployeeProject != null) {
						tPreEmployeeProject.setPreMainId(mainId);
						tPreEmployeeProject.setCreateBy(user.getId());
						tPreEmployeeProject.setCreateName(user.getNickname());
						tPreEmployeeProject.setCreateTime(LocalDateTime.now());
						tPreEmployeeProjectService.saveOrUpdate(tPreEmployeeProject);
					}
					// 预入职-员工职业资格信息表
					List<TPreEmpProfessionalQualification> tPreEmpProfessionalQualification = vo.getTpreEmpProfessionalQualificationList();
					if (tPreEmpProfessionalQualification != null) {
						// 先删除后增加
						tPreEmpProfessionalService.deleteProfessionalByMainId(mainId);
						for (TPreEmpProfessionalQualification info : tPreEmpProfessionalQualification) {
							info.setEmpIdcard(idCard);
							info.setPreMainId(mainId);
							info.setCreateBy(user.getId());
							info.setCreateName(user.getNickname());
							info.setCreateTime(LocalDateTime.now());
							tPreEmpProfessionalService.saveOrUpdate(info);
							// 附件
							if (info.getAttaList() != null && !info.getAttaList().isEmpty()) {
								List<TAttaInfo> attaList = info.getAttaList();
								if (attaList != null) {
									tAttaInfoService.deleteByDomainIdAndType(info.getId(), 1);
									for (TAttaInfo atta : attaList) {
										atta.setId(null);
										atta.setDomainId(info.getId());
										atta.setCreateBy(user.getId());
										atta.setCreateName(user.getNickname());
										atta.setCreateTime(LocalDateTime.now());
									}
									tAttaInfoService.saveBatch(attaList);
								}
							}
						}
					}
					// 预入职-其他附件
					List<TPreEmpOtherFile> tPreOtherFileList = vo.getTpreEmpOtherFileList();
					if (tPreOtherFileList != null) {
						// 先删除后增加
						tPreEmpOtherFileService.deleteOtherFileByMainId(mainId);
						for (TPreEmpOtherFile info : tPreOtherFileList) {
							info.setPreMainId(mainId);
							info.setCreateTime(LocalDateTime.now());
							tPreEmpOtherFileService.saveOrUpdate(info);
							// 附件
							if (info.getAttaList() != null && !info.getAttaList().isEmpty()) {
								List<TAttaInfo> attaList = info.getAttaList();
								if (attaList != null) {
									tAttaInfoService.deleteByDomainIdAndType(info.getId(), 2);
									for (TAttaInfo atta : attaList) {
										atta.setId(null);
										atta.setDomainId(info.getId());
										atta.setCreateBy(user.getId());
										atta.setCreateName(user.getNickname());
										atta.setCreateTime(LocalDateTime.now());
									}
									tAttaInfoService.saveBatch(attaList);
								}
							}
						}
					}
					// 预入职-员工工作履历信息表
					List<TPreEmpWorkRecording> tPreEmpWorkRecording = vo.getTpreEmpWorkRecordingList();
					if (tPreEmpWorkRecording != null) {
						// 先删除后增加
						tPreEmpWorkRecordingService.deleteByMainId(mainId);
						for (TPreEmpWorkRecording info : tPreEmpWorkRecording) {
							info.setEmpIdcard(idCard);
							info.setPreMainId(mainId);
							info.setCreateBy(user.getId());
							info.setCreateName(user.getNickname());
							info.setCreateTime(LocalDateTime.now());
						}
						tPreEmpWorkRecordingService.saveOrUpdateBatch(tPreEmpWorkRecording);
					}
					// 预入职-附件
					List<TAttaInfo> attaInfoList = vo.getAttaInfoList();
					if (attaInfoList != null) {
						// 先删除后增加
						tAttaInfoService.deleteByDomainId(mainId);
						for (TAttaInfo info : attaInfoList) {
							info.setId(null);
							info.setDomainId(mainId);
							info.setCreateBy(user.getId());
							info.setCreateName(user.getNickname());
							info.setCreateTime(LocalDateTime.now());
						}
						tAttaInfoService.saveBatch(attaInfoList);
					}
				}
				return R.ok("成功");
			} else {
				return R.failed("身份证不可为空！");
			}
		} catch (Exception e) {
			log.error("档案预入职C端保存异常：", e);
			throw new RuntimeException("保存失败，请联系管理员！！预入职主键【" + main.getId() +"】");
		}
	}

	/**
	 * @param status 1：保存
	 * @Description: 获取计税月份和是否可编辑的标志
	 * @Author: hgw
	 * @Date: 2024/7/16 16:15
	 * @return: java.lang.String
	 **/
	private void setTaxMonthAndFlag(String status, TPreEmployeeInfo employeeInfo) {
		String taxMonth = DateUtil.getThisMonth();
		employeeInfo.setTaxMonthFlag(CommonConstants.ONE_STRING);
		if (CommonConstants.ONE_STRING.equals(status)) {
			// 去薪资服务获取
			R<String> sdr = HttpDaprUtil.invokeMethodPost(salaryProperties.getAppUrl(), salaryProperties.getAppId()
					, "/tsalaryemployee/inner/getEmpTaxMonth", employeeInfo.getEmpIdcard(), String.class, SecurityConstants.FROM_IN);
			if (sdr != null && sdr.getData() != null && !CommonConstants.ZERO_STRING.equals(sdr.getData())) {
				taxMonth = sdr.getData();
				employeeInfo.setTaxMonthFlag(CommonConstants.ZERO_STRING);
			}
		}
		employeeInfo.setTaxMonth(taxMonth);
	}

	private YifuUser getNewYifuUser() {
		Set<String> dbAuthsSet = new HashSet<>();
		Collection<? extends GrantedAuthority> authorities = AuthorityUtils
				.createAuthorityList(dbAuthsSet.toArray(new String[0]));
		return new YifuUser("2", 1L, "", "预入职扫码",
				"预入职扫码", "0", SecurityConstants.BCRYPT + "123456",
				"12345678911", true, true, true,
				true,
				"1", authorities, "1",
				null, null,
				null);
	}

	/**
	 * @Description: 执行复制到档案的操作
	 * @Author: hgw
	 * @Date: 2024/6/18 19:35
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.lang.String>
	 **/
	private R<String> updateToEmployeeByPre(PreEmpMainDetailVo vo) {
		YifuUser user = SecurityUtils.getUser();
		if (user == null) {
			user = getNewYifuUser();
		}
		// 预入职-人员档案表
		TPreEmployeeInfo tPreEmployeeInfo = vo.getTpreEmployeeInfo();
		// 预入职-项目档案表
		TPreEmployeeProject tPreEmployeeProject = vo.getTpreEmployeeProject();
		// 预入职-员工学历信息表
		TPreEmpEducation tPreEmpEducation = vo.getTpreEmpEducation();
		// 如果档案项目id都没有，则新增。newFlag是true：
		String empId = null;
		String empIdCard = null;
		// 1人员档案
		TEmployeeInfo emp = null;
		// 2项目档案
		TEmployeeProject project = null;
		TSettleDomain dept = null;
		if (tPreEmployeeProject != null && Common.isNotNull(tPreEmployeeProject.getDeptId())) {
			dept = tSettleDomainService.getById(tPreEmployeeProject.getDeptId());
		}
		if (dept == null) {
			return R.failed("根据项目id，未找到项目！");
		}
		if (tPreEmployeeInfo != null && tPreEmpEducation != null) {
			if (Common.isNotNull(tPreEmpEducation.getEducationName())) {
				if ("5".equals(tPreEmpEducation.getEducationName())
						|| "6".equals(tPreEmpEducation.getEducationName())
						|| "7".equals(tPreEmpEducation.getEducationName())
						|| "8".equals(tPreEmpEducation.getEducationName())) {
					tPreEmployeeInfo.setIsCollege(CommonConstants.ONE_INT);
				} else {
					tPreEmployeeInfo.setIsCollege(CommonConstants.ZERO_INT);
				}
				tPreEmployeeInfo.setHignEducation(tPreEmpEducation.getEducationName());
			}
			tPreEmployeeInfo.setSchool(tPreEmpEducation.getSchool());
			tPreEmployeeInfo.setMajor(tPreEmpEducation.getMajor());
			tPreEmployeeInfo.setAdmissionDate(tPreEmpEducation.getEntryDate());
			tPreEmployeeInfo.setGradutionDate(tPreEmpEducation.getGradutionDate());
		}
		if (tPreEmployeeInfo != null && Common.isNotNull(tPreEmployeeInfo.getOldId())
				&& Common.isNotNull(tPreEmployeeProject.getOldId())) {
			empId = tPreEmployeeInfo.getOldId();
			emp = tEmployeeInfoService.getById(empId);
			if (emp != null) {
				BeanUtil.copyProperties(tPreEmployeeInfo, emp, ID);
				if (CommonConstants.ONE_INT == emp.getFileStatus()) {
					emp.setCreateTime(LocalDateTime.now());
					emp.setFileStatus(CommonConstants.ZERO_INT);
				}
				tEmployeeInfoService.updateById(emp);
			} else {
				emp = this.saveTEmployeeInfo(user, tPreEmployeeInfo);
				empId = emp.getId();
			}
			empIdCard = emp.getEmpIdcard();
			project = tEmployeeProjectService.getById(tPreEmployeeProject.getOldId());
			if (project != null) {
				if (Common.isEmpty(tPreEmployeeProject.getWorkingHours())) {
					tPreEmployeeProject.setWorkingHours("");
				}
				if (Common.isEmpty(tPreEmployeeProject.getPost())) {
					tPreEmployeeProject.setPost("");
				}
				if (Common.isEmpty(tPreEmployeeProject.getTryPeriod())) {
					tPreEmployeeProject.setTryPeriod("");
				}
				// 2024-7-23 11:52:33 金龙弟预张倩倩认为不同步项目的合同信息
				String[] ignoreProperties = new String[]{"id", "contractType", "workingHours", "post", "enjoinDate"
						, "enjoinDate", "tryPeriod"};
				BeanUtil.copyProperties(tPreEmployeeProject, project, ignoreProperties);
				if (CommonConstants.ONE_INT == project.getProjectStatus()) {
					project.setCreateTime(LocalDateTime.now());
					project.setDeleteFlag(CommonConstants.STATUS_NORMAL);
					project.setProjectStatus(CommonConstants.ZERO_INT);
				}
				tEmployeeProjectService.updateById(project);
			} else {
				this.saveTEmployeeProject(project, user, tPreEmployeeProject, emp, dept);
			}
		} else {
			// 新建档案、项目档案，将档案id赋值，给其他附属信息
			if (tPreEmployeeInfo != null) {
				// 生成性别年龄等：
				this.setBaseSexInfo(tPreEmployeeInfo);

				// 1人员档案
				emp = this.saveTEmployeeInfo(user, tPreEmployeeInfo);
				empId = emp.getId();
				empIdCard = emp.getEmpIdcard();
				// 2项目档案
				this.saveTEmployeeProject(project, user, tPreEmployeeProject, emp, dept);
			}
		}
		if (emp == null) {
			return R.failed("新建员工失败！");
		}
		// 更新或新增薪资人员
		if (Common.isNotNull(tPreEmployeeInfo.getBankNo())) {

			TSalaryEmployee newEmp = new TSalaryEmployee();
			newEmp.setFileStatus(CommonConstants.ZERO_STRING);
			newEmp.setDeptId(dept.getId());
			newEmp.setDeptName(dept.getDepartName());
			newEmp.setDeptNo(dept.getDepartNo());
			newEmp.setUnitId(dept.getCustomerId());
			newEmp.setUnitName(dept.getCustomerName());
			newEmp.setUnitNo(dept.getCustomerNo());
			newEmp.setCreateBy(String.valueOf(user.getId()));
			newEmp.setCreateName(user.getNickname());
			newEmp.setCreateTime(LocalDateTime.now());
			newEmp.setUpdateBy(String.valueOf(user.getId()));
			newEmp.setUpdateTime(LocalDateTime.now());
			newEmp.setInvoiceTitle(dept.getInvoiceTitleSalary());
			newEmp.setEmpName(tPreEmployeeInfo.getEmpName());
			newEmp.setEmpIdcard(tPreEmployeeInfo.getEmpIdcard());
			newEmp.setBankProvince(tPreEmployeeInfo.getBankProvince());
			newEmp.setBankCity(tPreEmployeeInfo.getBankCity());
			newEmp.setBankNo(tPreEmployeeInfo.getBankNo());
			String taxMonth = tPreEmployeeInfo.getTaxMonth();
			if (Common.isEmpty(tPreEmployeeInfo.getTaxMonth())) {
				taxMonth = DateUtil.getThisMonth();
			}
			newEmp.setTaxMonth(taxMonth);
			newEmp.setBankName(tPreEmployeeInfo.getBankName());
			newEmp.setBankSubName(tPreEmployeeInfo.getBankSubName());
			newEmp.setEmpPhone(tPreEmployeeInfo.getEmpPhone());
			// 保存薪资人员（含判断是否有发薪）
			R<String> sdr = HttpDaprUtil.invokeMethodPost(salaryProperties.getAppUrl(), salaryProperties.getAppId()
					, "/tsalaryemployee/inner/savePreNewEmpInfo", newEmp, String.class, SecurityConstants.FROM_IN);
			if (sdr == null || Common.isEmpty(sdr.getData())) {
				return R.failed("同步薪资人员失败！");
			}
		}

		// 3:其他附属信息

		// 预入职-员工不良记录表
		// 预入职-员工工作履历信息表
		// 预入职-紧急联络人
		// 预入职-员工伤残信息表
		// 预入职-员工学历信息表
		// 预入职-员工家庭信息表
		// 预入职-人员档案表
		// 预入职-项目档案表
		// 预入职-员工职业资格信息表

		// 最高学历
		if (tPreEmpEducation != null) {
			TEmpEducation tEmpEducation = null;
			if (Common.isNotNull(tPreEmpEducation.getOldId())) {
				tEmpEducation = tEmpEducationService.getById(tPreEmpEducation.getOldId());
			}
			if (tEmpEducation == null) {
				tEmpEducation = new TEmpEducation();
			} else {
				// 已存在的学历信息（如果不一致，要更新档案的最高学历）
				if (Common.isEmpty(tEmpEducation.getEducationName())
						|| !tEmpEducation.getEducationName().equals(tPreEmpEducation.getEducationName())) {
					emp.setHignEducation(tPreEmpEducation.getEducationName());
					tEmployeeInfoService.updateById(emp);
				}
			}
			BeanUtil.copyProperties(tPreEmpEducation, tEmpEducation, ID);
			if (Common.isEmpty(tEmpEducation.getEmpCode())) {
				tEmpEducation.setEmpCode(emp.getEmpCode());
			}
			if (Common.isEmpty(tEmpEducation.getCreateTime())) {
				tEmpEducation.setCreateTime(LocalDateTime.now());
			}
			tEmpEducation.setDeleteFlag(CommonConstants.STATUS_NORMAL);
			tEmpEducation.setEmpId(empId);
			if (Common.isNotNull(empIdCard)) {
				tEmpEducationService.updateEducationToNoByIdCard(empIdCard);
			}
			if (Common.isEmpty(tEmpEducation.getCreateBy())) {
				tEmpEducation.setCreateBy(user.getId());
				tEmpEducation.setCreateName(user.getNickname());
				tEmpEducation.setCreateTime(LocalDateTime.now());
			}
			tEmpEducationService.saveOrUpdate(tEmpEducation);
			String attaId = tPreEmpEducation.getOldId();
			tAttaInfoService.deleteByDomainIdAndType(attaId, 0);
			if (tPreEmpEducation.getAttaList() != null && !tPreEmpEducation.getAttaList().isEmpty()) {
				List<TAttaInfo> attaList = tPreEmpEducation.getAttaList();
				if (attaList != null) {
					TAttaInfo newEmpAtta;
					for (TAttaInfo atta : attaList) {
						newEmpAtta = new TAttaInfo();
						BeanUtil.copyProperties(atta, newEmpAtta, ID);
						newEmpAtta.setDomainId(tEmpEducation.getId());

						if (Common.isEmpty(tEmpEducation.getCreateBy())) {
							tEmpEducation.setCreateBy(user.getId());
							tEmpEducation.setCreateName(user.getNickname());
							tEmpEducation.setCreateTime(LocalDateTime.now());
						}
						tAttaInfoService.saveOrUpdate(newEmpAtta);
					}
				}
			}
		}
		// 预入职-员工不良记录表
		TPreEmpBadRecord tPreEmpBadRecord = vo.getTpreEmpBadRecord();
		if (tPreEmpBadRecord != null) {
			TEmpBadRecord tEmpBadRecord = null;
			if (Common.isNotNull(tPreEmpBadRecord.getOldId())) {
				tEmpBadRecord = tEmpBadRecordService.getById(tPreEmpBadRecord.getOldId());
			}
			if (tEmpBadRecord == null) {
				tEmpBadRecord = new TEmpBadRecord();
			}
			BeanUtil.copyProperties(tPreEmpBadRecord, tEmpBadRecord, ID);
			tEmpBadRecord.setEmpId(empId);

			if (Common.isEmpty(tEmpBadRecord.getCreateBy())) {
				tEmpBadRecord.setCreateBy(user.getId());
				tEmpBadRecord.setCreateName(user.getNickname());
				tEmpBadRecord.setCreateTime(LocalDateTime.now());
			}
			tEmpBadRecordService.saveOrUpdate(tEmpBadRecord);
		}
		// 预入职-紧急联络人
		TPreEmpContactInfo tPreEmpContactInfo = vo.getTpreEmpContactInfo();
		if (tPreEmpContactInfo != null) {
			TEmpContactInfo tEmpContactInfo = null;
			if (Common.isNotNull(tPreEmpContactInfo.getOldId())) {
				tEmpContactInfo = tEmpContactInfoService.getById(tPreEmpContactInfo.getOldId());
			}
			if (tEmpContactInfo == null) {
				tEmpContactInfo = new TEmpContactInfo();
			}
			BeanUtil.copyProperties(tPreEmpContactInfo, tEmpContactInfo, ID);
			tEmpContactInfo.setEmpId(empId);

			if (Common.isEmpty(tEmpContactInfo.getCreateBy())) {
				tEmpContactInfo.setCreateBy(user.getId());
				tEmpContactInfo.setCreateName(user.getNickname());
				tEmpContactInfo.setCreateTime(LocalDateTime.now());
			}
			if (Common.isEmpty(tEmpContactInfo.getEmpIdcard()) && Common.isNotNull(emp)) {
				tEmpContactInfo.setEmpIdcard(emp.getEmpIdcard());
			}
			tEmpContactInfoService.saveOrUpdate(tEmpContactInfo);
		}
		// 预入职-员工伤残信息表
		TPreEmpDisabilityInfo tPreEmpDisabilityInfo = vo.getTpreEmpDisabilityInfo();
		if (tPreEmpDisabilityInfo != null) {
			TEmpDisabilityInfo tEmpDisabilityInfo = null;
			if (Common.isNotNull(tPreEmpDisabilityInfo.getOldId())) {
				tEmpDisabilityInfo = tEmpDisabilityInfoService.getById(tPreEmpDisabilityInfo.getOldId());
			}
			if (tEmpDisabilityInfo == null) {
				tEmpDisabilityInfo = new TEmpDisabilityInfo();
			}
			BeanUtil.copyProperties(tPreEmpDisabilityInfo, tEmpDisabilityInfo, ID);
			tEmpDisabilityInfo.setDeleteFlag(CommonConstants.STATUS_NORMAL);
			tEmpDisabilityInfo.setEmpId(empId);
			if (Common.isEmpty(tEmpDisabilityInfo.getEmpCode())) {
				tEmpDisabilityInfo.setEmpCode(emp.getEmpCode());
			}

			if (Common.isEmpty(tEmpDisabilityInfo.getCreateBy())) {
				tEmpDisabilityInfo.setCreateBy(user.getId());
				tEmpDisabilityInfo.setCreateName(user.getNickname());
				tEmpDisabilityInfo.setCreateTime(LocalDateTime.now());
			}
			tEmpDisabilityInfoService.saveOrUpdate(tEmpDisabilityInfo);
		}

		// 预入职-员工家庭信息表
		List<TPreEmpFamily> tPreEmpFamilyList = vo.getTpreEmpFamilyList();
		if (tPreEmpFamilyList != null) {
			// 先删除其他信息
			tEmpFamilyService.deleteByEmpId(empId);
			TEmpFamily tEmpFamily;
			for (TPreEmpFamily tPreEmpFamily : tPreEmpFamilyList) {
				tEmpFamily = new TEmpFamily();
				BeanUtil.copyProperties(tPreEmpFamily, tEmpFamily, ID);
				tEmpFamily.setEmpId(empId);
				tEmpFamily.setDeleteFlag(CommonConstants.STATUS_NORMAL);
				tEmpFamily.setEmpCode(emp.getEmpCode());
				tEmpFamily.setEmpName(emp.getEmpName());
				tEmpFamily.setEmpIdcard(emp.getEmpIdcard());
				if (Common.isNotNull(tPreEmpFamily.getOldId())) {
					tEmpFamily.setId(tPreEmpFamily.getOldId());
				}

				if (Common.isEmpty(tEmpFamily.getCreateBy())) {
					tEmpFamily.setCreateBy(user.getId());
					tEmpFamily.setCreateName(user.getNickname());
					tEmpFamily.setCreateTime(LocalDateTime.now());
				}
				tEmpFamilyService.save(tEmpFamily);
			}
		}
		// 预入职-员工职业资格信息表
		List<TPreEmpProfessionalQualification> tPreEmpProfessionalQualificationList = vo.getTpreEmpProfessionalQualificationList();
		// 先删除其他信息
		tEmpProfessionalService.deleteProfessionalByEmpId(empId);
		if (tPreEmpProfessionalQualificationList != null
				&& (Common.isEmpty(emp.getHaveQualification()) || CommonConstants.ZERO_STRING.equals(emp.getHaveQualification()))) {
			TEmpProfessionalQualification info;
			for (TPreEmpProfessionalQualification preInfo : tPreEmpProfessionalQualificationList) {
				info = new TEmpProfessionalQualification();
				BeanUtil.copyProperties(preInfo, info, ID);
				info.setDeleteFlag(CommonConstants.STATUS_NORMAL);
				info.setEmpId(empId);
				info.setEmpCode(emp.getEmpCode());
				info.setEmpName(emp.getEmpName());
				info.setEmpIdcard(emp.getEmpIdcard());
				if (Common.isNotNull(preInfo.getOldId())) {
					info.setId(preInfo.getOldId());
				}

				if (Common.isEmpty(info.getCreateBy())) {
					info.setCreateBy(user.getId());
					info.setCreateName(user.getNickname());
					info.setCreateTime(LocalDateTime.now());
				}
				tEmpProfessionalService.save(info);
				if (preInfo.getAttaList() != null && !preInfo.getAttaList().isEmpty()) {
					List<TAttaInfo> attaList = preInfo.getAttaList();
					if (attaList != null) {
						String attaId = preInfo.getOldId();
						tAttaInfoService.deleteByDomainIdAndType(attaId, 1);
						TAttaInfo newEmpAtta;
						for (TAttaInfo atta : attaList) {
							newEmpAtta = new TAttaInfo();
							BeanUtil.copyProperties(atta, newEmpAtta, ID);
							newEmpAtta.setDomainId(info.getId());

							if (Common.isEmpty(atta.getCreateBy())) {
								atta.setCreateBy(user.getId());
								atta.setCreateName(user.getNickname());
								atta.setCreateTime(LocalDateTime.now());
							}
							tAttaInfoService.saveOrUpdate(newEmpAtta);
						}
					}
				}
			}
		}

		// 预入职-其他附件
		List<TPreEmpOtherFile> preEmpOtherFileList = vo.getTpreEmpOtherFileList();
		// 先删除其他信息
		tEmpOtherFileService.deleteOtherFileByEmpId(empId);
		if (preEmpOtherFileList != null && !preEmpOtherFileList.isEmpty()) {
			TEmpOtherFile info;
			for (TPreEmpOtherFile preInfo : preEmpOtherFileList) {
				info = new TEmpOtherFile();
				BeanUtil.copyProperties(preInfo, info, ID);
				info.setEmpId(empId);
				if (Common.isNotNull(preInfo.getOldId())) {
					info.setId(preInfo.getOldId());
				}
				tEmpOtherFileService.save(info);
				if (preInfo.getAttaList() != null && !preInfo.getAttaList().isEmpty()) {
					List<TAttaInfo> attaList = preInfo.getAttaList();
					if (attaList != null) {
						String attaId = preInfo.getOldId();
						tAttaInfoService.deleteByDomainIdAndType(attaId, 2);
						TAttaInfo newEmpAtta;
						for (TAttaInfo atta : attaList) {
							newEmpAtta = new TAttaInfo();
							BeanUtil.copyProperties(atta, newEmpAtta, ID);
							newEmpAtta.setDomainId(info.getId());
							if (Common.isEmpty(atta.getCreateBy())) {
								atta.setCreateBy(user.getId());
								atta.setCreateName(user.getNickname());
								atta.setCreateTime(LocalDateTime.now());
							}
							tAttaInfoService.saveOrUpdate(newEmpAtta);
						}
					}
				}
			}
		}
		// 预入职-员工工作履历信息表
		List<TPreEmpWorkRecording> tPreEmpWorkRecordingList = vo.getTpreEmpWorkRecordingList();
		// 先删除其他信息
		tEmpWorkRecordingService.deleteWorkRecordByEmpId(empId);
		if (tPreEmpWorkRecordingList != null && (Common.isEmpty(emp.getFirstWorkFlag()) || CommonConstants.ONE_STRING.equals(emp.getFirstWorkFlag()))) {
			TEmpWorkRecording info;
			for (TPreEmpWorkRecording preInfo : tPreEmpWorkRecordingList) {
				info = new TEmpWorkRecording();
				BeanUtil.copyProperties(preInfo, info, ID);
				info.setDeleteFlag(CommonConstants.STATUS_NORMAL);
				info.setEmpId(empId);
				info.setEmpCode(emp.getEmpCode());
				info.setEmpName(emp.getEmpName());
				info.setEmpIdcard(emp.getEmpIdcard());

				if (Common.isEmpty(info.getCreateBy())) {
					info.setCreateBy(user.getId());
					info.setCreateName(user.getNickname());
					info.setCreateTime(LocalDateTime.now());
				}
				if (Common.isNotNull(preInfo.getOldId())) {
					info.setId(preInfo.getOldId());
				}
				tEmpWorkRecordingService.save(info);
			}
		}
		// 预入职-附件
		List<TAttaInfo> attaList = vo.getAttaInfoList();
		if (attaList != null) {
			// 先删除其他信息
			tAttaInfoService.deleteByDomainIdAndOther(empId);
			TAttaInfo attaEmp;
			List<TAttaInfo> attaEmpList = new ArrayList<>();
			for (TAttaInfo atta : attaList) {
				attaEmp = new TAttaInfo();
				BeanUtil.copyProperties(atta, attaEmp, ID);
				attaEmp.setDomainId(empId);
				attaEmp.setCreateBy(user.getId());
				attaEmp.setCreateName(user.getNickname());
				attaEmp.setCreateTime(LocalDateTime.now());
				attaEmpList.add(attaEmp);
			}
			tAttaInfoService.saveBatch(attaEmpList);
		}
		return R.ok("成功！");
	}

	// 2新增或更新项目
	private TEmployeeProject saveTEmployeeProject(TEmployeeProject project, YifuUser user, TPreEmployeeProject tPreEmployeeProject, TEmployeeInfo emp, TSettleDomain dept) {
		boolean isNew;
		if (Common.isNotNull(tPreEmployeeProject.getEmpIdcard()) && Common.isNotNull(tPreEmployeeProject.getDeptId())) {
			project = tEmployeeProjectService.getByEmpIdCardAndDeptId(tPreEmployeeProject.getEmpIdcard(), tPreEmployeeProject.getDeptId());
			if (project == null) {
				project = tEmployeeProjectService.getByEmpIdCardAndDeptIdAndDelete(tPreEmployeeProject.getEmpIdcard(), tPreEmployeeProject.getDeptId());
				if (project != null) {
					project.setCreateTime(LocalDateTime.now());
					project.setProjectStatus(CommonConstants.ZERO_INT);
					project.setProjectSource(CommonConstants.EIGHT_STRING);
					project.setDeleteFlag(CommonConstants.STATUS_NORMAL);
				}
			} else if (CommonConstants.ONE_INT == project.getProjectStatus()) {
				project.setCreateTime(LocalDateTime.now());
				project.setProjectStatus(CommonConstants.ZERO_INT);
			}
			isNew = false;
			if (project == null) {
				isNew = true;
				project = new TEmployeeProject();
			}
			String[] ignoreProperties = new String[]{"id", "status"};
			if (Common.isEmpty(tPreEmployeeProject.getWorkingHours())) {
				tPreEmployeeProject.setWorkingHours("");
			}
			if (Common.isEmpty(tPreEmployeeProject.getPost())) {
				tPreEmployeeProject.setPost("");
			}
			if (Common.isEmpty(tPreEmployeeProject.getTryPeriod())) {
				tPreEmployeeProject.setTryPeriod("");
			}
			// 2024-7-23 11:52:33 金龙弟预张倩倩认为不同步项目的合同信息
			ignoreProperties = new String[]{"id", "contractType", "workingHours", "post", "enjoinDate"
					, "enjoinDate", "tryPeriod"};
			BeanUtil.copyProperties(tPreEmployeeProject, project, ignoreProperties);
			if (Common.isEmpty(project.getStatus())) {
				project.setStatus(CommonConstants.ZERO_INT);
			}
			if (Common.isEmpty(project.getEmpId()) && emp != null) {
				project.setEmpId(emp.getId());
			}
			if (isNew) {
				// 新项目档案塞基本属性
				this.setNewEmpProjectBaseData(project, emp, dept, user);
			}
			tEmployeeProjectService.saveOrUpdate(project);
		}
		return project;
	}

	// 1新增档案
	private TEmployeeInfo saveTEmployeeInfo(YifuUser user, TPreEmployeeInfo tPreEmployeeInfo) {
		TEmployeeInfo emp = tEmployeeInfoService.getInfoByIdcard(tPreEmployeeInfo.getEmpIdcard());
		if (emp == null) {
			emp = tEmployeeInfoService.getInfoByDelete(tPreEmployeeInfo.getEmpIdcard());
			if (emp != null) {
				emp.setCreateTime(LocalDateTime.now());
				emp.setFileStatus(CommonConstants.ZERO_INT);
				emp.setDeleteFlag(CommonConstants.STATUS_NORMAL);
			}
		} else if (CommonConstants.ONE_INT == emp.getFileStatus()) {
			emp.setCreateTime(LocalDateTime.now());
			emp.setFileStatus(CommonConstants.ZERO_INT);
		}
		boolean isNew = false;
		if (emp == null) {
			isNew = true;
			emp = new TEmployeeInfo();
		}
		String empCode = emp.getEmpCode();
		BeanUtil.copyProperties(tPreEmployeeInfo, emp, ID);
		if (Common.isEmpty(emp.getEmpCode()) && Common.isNotNull(empCode)) {
			emp.setEmpCode(empCode);
		}
		if (Common.isEmpty(emp.getEmpCode())) {
			emp.setEmpCode(tEmployeeInfoService.getCode());
		}
		emp.setFileStatus(CommonConstants.ZERO_INT);
		emp.setDeleteFlag(CommonConstants.STATUS_NORMAL);
		if (isNew) {
			// 新员工塞基本属性
			this.setNewEmpBaseData(emp, user);
		}
		emp.setFileStatus(CommonConstants.ZERO_INT);
		tEmployeeInfoService.saveOrUpdate(emp);
		return emp;
	}

	/**
	 * @Description: 新员工塞基本信息
	 * @Author: hgw
	 * @Date: 2024/6/19 14:26
	 * @return: void
	 **/
	private void setNewEmpBaseData(TEmployeeInfo employeeInfo, YifuUser user) {
		// 新增档案
		employeeInfo.setEmpCode(tEmployeeInfoService.getCode());
		employeeInfo.setCreateBy(user.getId());
		employeeInfo.setCreateName(user.getNickname());
		employeeInfo.setCreateTime(LocalDateTime.now());

		employeeInfo.setFileSource(CommonConstants.SEVEN_STRING);
		employeeInfo.setStatus(CommonConstants.ZERO_INT);
		employeeInfo.setFileStatus(CommonConstants.ZERO_INT);
		employeeInfo.setProjectNum(CommonConstants.ONE_INT);
		employeeInfo.setDeleteFlag(CommonConstants.STATUS_NORMAL);

		employeeInfo.setContractStatus(CommonConstants.ZERO_INT);
		employeeInfo.setInsuranceStatus(CommonConstants.ZERO_INT);
		employeeInfo.setSocialStatus(CommonConstants.ZERO_INT);
		employeeInfo.setFundStatus(CommonConstants.ZERO_INT);
		employeeInfo.setSalaryStatus(CommonConstants.ZERO_INT);
	}

	/**
	 * @Description: 新员工塞基本项目档案信息
	 * @Author: hgw
	 * @Date: 2024/6/19 14:26
	 * @return: void
	 **/
	private void setNewEmpProjectBaseData(TEmployeeProject insTEmployeePro, TEmployeeInfo tEmployeeInfo, TSettleDomain tSettleDomain, YifuUser user) {
		// 新增档案
		insTEmployeePro.setId(String.valueOf(UUID.randomUUID()).replace("-", ""));
		insTEmployeePro.setDeleteFlag(CommonConstants.STATUS_NORMAL);
		insTEmployeePro.setStatus(CommonConstants.ZERO_INT);

		//获取项目和单位信息
		if (Common.isNotNull(tSettleDomain)) {
			insTEmployeePro.setDeptId(tSettleDomain.getId());
			insTEmployeePro.setDeptName(tSettleDomain.getDepartName());
			insTEmployeePro.setBusinessPrimaryType(tSettleDomain.getBusinessPrimaryType());
			insTEmployeePro.setBusinessSecondType(tSettleDomain.getBusinessSecondType());
			insTEmployeePro.setBusinessThirdType(tSettleDomain.getBusinessThirdType());
			insTEmployeePro.setUnitId(tSettleDomain.getCustomerId());
			insTEmployeePro.setUnitNo(tSettleDomain.getCustomerNo());
			insTEmployeePro.setUnitName(tSettleDomain.getCustomerName());
			String empNO = tEmployeeProjectService.getEmpNo(insTEmployeePro.getDeptNo());
			insTEmployeePro.setEmpNo(empNO);
		}
		//员工编码生成规则
		if (tEmployeeInfo != null) {
			insTEmployeePro.setEmpId(tEmployeeInfo.getId());
			insTEmployeePro.setEmpName(tEmployeeInfo.getEmpName());
			insTEmployeePro.setEmpCode(tEmployeeInfo.getEmpCode());
		}
		insTEmployeePro.setCreateBy(user.getId());
		insTEmployeePro.setCreateTime(LocalDateTime.now());
		insTEmployeePro.setCreateName(user.getNickname());
		insTEmployeePro.setProjectSource(CommonConstants.EIGHT_STRING);

	}


	/**
	 * @param employeeInfo
	 * @Description: 生成基础性别等信息
	 * @Author: hgw
	 * @Date: 2022/6/24 16:51
	 * @return: void
	 **/
	private void setBaseSexInfo(TPreEmployeeInfo employeeInfo) {
		String idCard = employeeInfo.getEmpIdcard();
		if (Common.isNotNull(idCard) && Common.isEmpty(employeeInfo.getEmpSex()) && idCard.length() > 14) {

			// 出生日期
			String idCardDate = idCard.substring(6, 14);
			Date empBirthday = DateUtil.stringToDateByPatten(idCardDate, "yyyyMMdd");
			employeeInfo.setEmpBirthday(empBirthday);

			// 性别
			if (idCard.length() > 17) {
				if (Integer.parseInt(idCard.substring(16, 17)) % 2 == 1) {
					employeeInfo.setEmpSex(CommonConstants.SEX_MAN);
				} else {
					employeeInfo.setEmpSex(CommonConstants.SEX_WONMAN);
				}
			}

			// 年龄
			int idCardYear = Integer.parseInt(idCard.substring(6, 10));
			employeeInfo.setEmpAge(DateUtil.getAge(idCardYear));
		}
	}

	/**
	 * 预入职-主表批量导出
	 *
	 * @return
	 */
	@Override
	public void listExport(HttpServletResponse response, TPreEmpMainSearchVo searchVo) {
		String fileName = "预入职导出主表" + DateUtil.getThisTime() + ".xlsx";
		//获取要导出的列表
		List<TPreEmpMain> list = new ArrayList<>();
		long count = baseMapper.getTPreEmpMainCount(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, CommonConstants.UTF8));
			// 这里 需要指定写用哪个class去写，然后写到第一个sheet，然后文件流会自动关闭
			ExcelWriter excelWriter = EasyExcelFactory.write(out, TPreEmpMain.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.getTPreEmpMainList(searchVo);
					if (Common.isNotNull(list)) {
						ExcelUtil<TPreEmpMain> util = new ExcelUtil<>(TPreEmpMain.class);
						for (TPreEmpMain vo : list) {
							util.convertEntity(vo, null, null, null);
						}
					}
					if (Common.isNotNull(list)) {
						WriteSheet writeSheet = EasyExcelFactory.writerSheet("预入职-主表" + index).build();
						excelWriter.write(list, writeSheet);
						index++;
					}
					if (Common.isNotNull(list)) {
						list.clear();
					}
				}
			} else {
				WriteSheet writeSheet = EasyExcelFactory.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 List<TPreEmpMain> noPageDiy(TPreEmpMainSearchVo searchVo) {
		LambdaQueryWrapper<TPreEmpMain> wrapper = buildQueryWrapper(searchVo);
		List<String> idList = Common.getList(searchVo.getIds());
		if (Common.isNotNull(idList)) {
			wrapper.in(TPreEmpMain::getId, idList);
		}
		if (searchVo.getLimitStart() >= 0 && searchVo.getLimitEnd() > 0) {
			wrapper.last(" limit " + searchVo.getLimitStart() + "," + searchVo.getLimitEnd());
		}
		wrapper.orderByDesc(BaseEntity::getCreateTime);
		return baseMapper.selectList(wrapper);
	}

	private LambdaQueryWrapper<TPreEmpMain> buildQueryWrapper(TPreEmpMainSearchVo entity) {
		LambdaQueryWrapper<TPreEmpMain> wrapper = Wrappers.lambdaQuery();
		if (ArrayUtil.isNotEmpty(entity.getCreateTimeStart())) {
			wrapper.ge(TPreEmpMain::getCreateTime, entity.getCreateTimeStart());
		}
		if (ArrayUtil.isNotEmpty(entity.getCreateTimeEnd())) {
			wrapper.le(TPreEmpMain::getCreateTime, entity.getCreateTimeEnd());
		}
		if (Common.isNotNull(entity.getStatusArray())) {
			wrapper.in(TPreEmpMain::getStatus, entity.getStatusArray());
		}
		if (Common.isNotNull(entity.getEmpNatureArray())) {
			wrapper.in(TPreEmpMain::getEmpNatrue, entity.getEmpNatureArray());
		}
		if (Common.isNotNull(entity.getCreateName())) {
			wrapper.eq(TPreEmpMain::getCreateName, entity.getCreateName());
		}
		return wrapper;
	}

	/**
	 * @Description: 批量完善信息
	 * @Author: hgw
	 * @Date: 2024/6/21 15:26
	 * @return: com.yifu.cloud.plus.v1.yifu.common.core.util.R<java.util.List < com.yifu.cloud.plus.v1.yifu.common.core.util.ErrorMessage>>
	 **/
	@Override
	public R<List<ErrorMessage<String>>> importDiy(InputStream inputStream) {
		List<ErrorMessage<String>> errorMessageList = new ArrayList<>();
		ExcelUtil<TPreEmpMainVo> util1 = new ExcelUtil<>(TPreEmpMainVo.class);
		// 写法2：
		// 匿名内部类 不用额外写一个DemoDataListener
		// 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
		try {
			EasyExcelFactory.read(inputStream, TPreEmpMainVo.class, new ReadListener<TPreEmpMainVo>() {
				/**
				 * 单次缓存的数据量
				 */
				public static final int BATCH_COUNT = CommonConstants.BATCH_COUNT;
				/**
				 *临时存储
				 */
				private List<TPreEmpMainVo> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

				@Override
				public void invoke(TPreEmpMainVo data, AnalysisContext context) {
					ReadRowHolder readRowHolder = context.readRowHolder();
					Integer rowIndex = readRowHolder.getRowIndex();
					data.setRowIndex(rowIndex + 1);
					ErrorMessage<String> 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());
					importTPreEmpMain(cachedDataList, errorMessageList);
					log.info("存储数据库成功！");
				}
			}).sheet().doRead();
		} catch (Exception e) {
			log.error(CommonConstants.IMPORT_DATA_ANALYSIS_ERROR, e);
			return R.failed(CommonConstants.IMPORT_DATA_ANALYSIS_ERROR);
		}
		if (errorMessageList.isEmpty()) {
			return R.ok();
		} else {
			return R.failed(errorMessageList);
		}
	}

	private void importTPreEmpMain(List<TPreEmpMainVo> excelVOList, List<ErrorMessage<String>> errorMessageList) {
		TPreEmpMain main;
		TPreEmployeeInfo employee;
		TPreEmployeeProject project;
		// 执行数据插入操作 组装
		for (int i = 0; i < excelVOList.size(); i++) {
			TPreEmpMainVo excel = excelVOList.get(i);
			if (Common.isEmpty(excel.getDeptNo()) || Common.isEmpty(excel.getEmpIdcard()) || Common.isEmpty(excel.getEmpNatrue()) || Common.isEmpty(excel.getFileProvince())) {
				errorMessageList.add(new ErrorMessage<>(excel.getRowIndex(), "完善信息不可为空！"));
			} else {
				// 插入
				main = baseMapper.getTPreEmpMainByCardAndDeptNo(excel.getEmpIdcard(), excel.getDeptNo());
				if (main == null || Common.isEmpty(main.getId()) || Common.isEmpty(main.getStatus())) {
					errorMessageList.add(new ErrorMessage<>(excel.getRowIndex(), "未找到预入职主表！"));
				} else {
					if (!CommonConstants.ONE_STRING.equals(main.getStatus())) {
						errorMessageList.add(new ErrorMessage<>(excel.getRowIndex(), "状态不为待完善，不可编辑！"));
					} else {
						employee = tPreEmployeeInfoService.getTPreEmployeeInfoList(main.getId());
						if (employee == null || Common.isEmpty(employee.getId())) {
							errorMessageList.add(new ErrorMessage<>(excel.getRowIndex(), "未找到预入职人员表！"));
						} else {
							project = tPreEmployeeProjectService.getTPreEmployeeProjectList(main.getId());
							if (project == null || Common.isEmpty(project.getId())) {
								errorMessageList.add(new ErrorMessage<>(excel.getRowIndex(), "未找到预入职项目档案表！"));
							} else {
								if (Common.isNotNull(employee.getBankNo()) && Common.isEmpty(excel.getTaxMonth())) {
									errorMessageList.add(new ErrorMessage<>(excel.getRowIndex(), "有银行卡号，计税月份必填！"));
									continue;
								}
								if (Common.isNotNull(excel.getTaxMonth())) {
									String taxMonth = excel.getTaxMonth().trim();
									if (!taxMonth.matches(taxMonthRegex)) {
										errorMessageList.add(new ErrorMessage<>(excel.getRowIndex(), "计税月份为6位年月，例如：202407！"));
										continue;
									}
									if (Common.isEmpty(employee.getTaxMonthFlag()) || CommonConstants.ONE_STRING.equals(employee.getTaxMonthFlag())) {
										employee.setTaxMonth(excel.getTaxMonth());
									}
								}
								employee.setEmpNatrue(excel.getEmpNatrue());
								project.setEmpNatrue(excel.getEmpNatrue());
								employee.setFileProvince(Integer.parseInt(excel.getFileProvince()));
								if (Common.isEmpty(excel.getFileCity())) {
									employee.setFileCity(CommonConstants.ZERO_INT);
								} else {
									try {
										employee.setFileCity(Integer.parseInt(excel.getFileCity()));
									} catch (Exception e) {
										errorMessageList.add(new ErrorMessage<>(excel.getRowIndex(), "未找到市！"));
										continue;
									}
								}
								if (Common.isEmpty(excel.getFileTown())) {
									employee.setFileTown(CommonConstants.ZERO_INT);
								} else {
									try {
										employee.setFileTown(Integer.parseInt(excel.getFileTown()));
									} catch (Exception e) {
										//errorMessageList.add(new ErrorMessage<>(excel.getRowIndex(), "未找到县！")); 2024-09-26 mvp1.6.11 预入职档案地区县选填
										//continue;
										throw new RuntimeException("档案地址-区/县转换异常");
									}
								}
								tPreEmployeeInfoService.updateById(employee);
								tPreEmployeeProjectService.updateById(project);
								main.setEmpNatrue(excel.getEmpNatrue());
								main.setStatus(CommonConstants.TWO_STRING);
								this.updateById(main);
							}
						}
					}
				}
			}
		}
	}

	@Override
	@Transactional
	public R<String> doAudit(String id, String status, String auditRemark) {
		if (Common.isEmpty(id) && Common.isEmpty(status)) {
			return R.failed("id、status不可为空！");
		}
		TPreEmpMain preEmpMain = this.getById(id);
		if (preEmpMain == null || Common.isEmpty(preEmpMain.getStatus()) || !CommonConstants.TWO_STRING.equals(preEmpMain.getStatus())) {
			return R.failed("请确认ID与状态！");
		}
		try {
			if (!CommonConstants.THREE_STRING.equals(status) && !CommonConstants.FOUR_STRING.equals(status)) {
				return R.failed("审核状态不对！");
			}
			if (CommonConstants.THREE_STRING.equals(status) && Common.isEmpty(auditRemark)) {
				return R.failed("审核不通过，原因必填！");
			}
			YifuUser user = SecurityUtils.getUser();
			if (user == null || Common.isEmpty(user.getId())) {
				return R.failed("未找到登录信息！");
			}
			preEmpMain.setStatus(status);
			preEmpMain.setAuditId(user.getId());
			preEmpMain.setAuditRemark(auditRemark);
			preEmpMain.setAuditName(user.getNickname());
			// 2024-9-6 17:32:00 倩倩与 王成沟通，变更为审核时间，且审核通过与不通过的时候显示
			preEmpMain.setAuditTime(new Date());

			// 审核日志
			TPreEmpMainLog auditLog = new TPreEmpMainLog();
			auditLog.setAuditId(user.getId());
			auditLog.setAuditName(user.getNickname());
			auditLog.setAuditTime(new Date());
			auditLog.setAuditRemark(auditRemark);
			auditLog.setMainId(preEmpMain.getId());
			auditLog.setAuditResult(status);

			R<PreEmpMainDetailVo> voR = this.getInfoById(id);
			if (voR != null && voR.getCode() == CommonConstants.SUCCESS) {
				// 转到更新档案去
				PreEmpMainDetailVo vo = voR.getData();
				tPreEmpMainLogService.save(auditLog);
				this.updateById(preEmpMain);
				if (CommonConstants.FOUR_STRING.equals(status)) {
					return updateToEmployeeByPre(vo);
				} else {
					return R.ok("已审核");
				}
			} else {
				return R.failed("未找到信息，未审核！");
			}
		} catch (Exception e) {
			log.error("档案预入职审核异常：", e);
			throw new RuntimeException("保存失败，请联系管理员！！预入职主键【" + preEmpMain.getId() +"】");
		}

	}

	@Override
	public R<List<ErrorVO>> doBatchAudit(List<String> ids, String status, String auditRemark) {
		if (Common.isEmpty(ids) && Common.isEmpty(status)) {
			return R.failed("id、status不可为空！");
		}
		if (!CommonConstants.THREE_STRING.equals(status) && !CommonConstants.FOUR_STRING.equals(status)) {
			return R.failed("审核状态不对！");
		}
		if (CommonConstants.THREE_STRING.equals(status) && Common.isEmpty(auditRemark)) {
			return R.failed("审核不通过，原因必填！");
		}
		YifuUser user = SecurityUtils.getUser();
		if (user == null || Common.isEmpty(user.getId())) {
			return R.failed("未找到登录信息！");
		}
		List<ErrorVO> returnList = new ArrayList<>();
		TPreEmpMain preEmpMain;
		ErrorVO errorVo;
		int i = 1;
		boolean isTrue = true;
		for (String id : ids) {
			errorVo = new ErrorVO();
			errorVo.setResult(CommonConstants.ZERO_INT);
			errorVo.setLineNum(i++);
			preEmpMain = this.getById(id);
			if (preEmpMain == null || Common.isEmpty(preEmpMain.getStatus()) || !CommonConstants.TWO_STRING.equals(preEmpMain.getStatus())) {
				errorVo.setErrorInfo("请确认ID与状态！");
				returnList.add(errorVo);
				isTrue = false;
			} else {
				errorVo.setResult(CommonConstants.ONE_INT);
				errorVo.setMainName(preEmpMain.getEmpIdcard() + CommonConstants.DOWN_LINE_STRING + preEmpMain.getDeptNo());
				returnList.add(errorVo);
				preEmpMain.setStatus(status);
				preEmpMain.setAuditId(user.getId());
				preEmpMain.setAuditRemark(auditRemark);
				preEmpMain.setAuditName(user.getNickname());
				if (CommonConstants.FOUR_STRING.equals(status)) {
					preEmpMain.setAuditTime(new Date());
				}

				// 审核日志
				TPreEmpMainLog auditLog = new TPreEmpMainLog();
				auditLog.setAuditId(user.getId());
				auditLog.setAuditName(user.getNickname());
				auditLog.setAuditTime(new Date());
				auditLog.setAuditRemark(auditRemark);
				auditLog.setMainId(preEmpMain.getId());
				auditLog.setAuditResult(status);

				R<PreEmpMainDetailVo> voR = this.getInfoById(id);
				PreEmpMainDetailVo vo = null;
				if (voR != null && voR.getCode() == CommonConstants.SUCCESS) {
					// 转到更新档案去
					vo = voR.getData();
					this.updateById(preEmpMain);
					tPreEmpMainLogService.save(auditLog);
					if (CommonConstants.FOUR_STRING.equals(status)) {
						updateToEmployeeByPre(vo);
					}
				}
			}
		}
		if (isTrue) {
			return R.ok(returnList);
		} else {
			return R.failed(returnList);
		}
	}

	private List<String> getNewArrayList(String[] strArr) {
		return Arrays.asList(strArr);
	}

	/**
	 * 预入职导出全量
	 *
	 * @return
	 */
	@Override
	public void exportAll(HttpServletResponse response, TPreEmpMainSearchVo searchVo) {
		String name = "预入职导出全量";
		String fileName = name + DateUtil.getThisTime() + ".xlsx";
		//获取要导出的列表
		List<TPreMainExportAllVO> list = Lists.newArrayList();
		long count = baseMapper.getTPreEmpMainAllCount(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, CommonConstants.UTF8));
			ExcelWriter excelWriter = null;
			if (count > CommonConstants.EXCEL_EXPORT_LIMIT) {
				List<List<String>> strList = new ArrayList<>();
				strList.add(this.getNewArrayList(new String[]{"数据超过6万条，请联系管理员后端导出！！"}));
				List<List<String>> headList = Lists.newArrayList();
				headList.add(this.getNewArrayList(new String[]{"提示"}));
				EasyExcelFactory.write(out).head(headList).sheet("数据超限").doWrite(strList);
			} else {
				// 这里 需要指定写用哪个class去写，然后写到第一个sheet，然后文件流会自动关闭
				List<List<String>> headList = Lists.newArrayList();
				headList.add(this.getNewArrayList(new String[]{"提交时间", "提交时间"}));
				headList.add(this.getNewArrayList(new String[]{"状态", "状态"}));
				headList.add(this.getNewArrayList(new String[]{"审核人", "审核人"}));
				headList.add(this.getNewArrayList(new String[]{"审核时间", "审核时间"}));
				String oneTitle = "员工基本信息";
				headList.add(this.getNewArrayList(new String[]{oneTitle, "员工主码"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "员工类型"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "员工姓名"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "身份证号"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "性别"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "身份证期限"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "身份证开始日期"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "身份证截止日期"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "出生日期"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "年龄"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "婚姻状况"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "民族"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "政治面貌"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "手机号码"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "邮箱"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "户籍所在地"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "户口性质"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "档案所在地"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "档案状态"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "通信地址-省"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "通信地址-市"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "通信地址-区/县"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "通信地址-详细地址"}));
				oneTitle = "在项信息";
				headList.add(this.getNewArrayList(new String[]{oneTitle, "客户名称"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "项目名称"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "业务类型"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "合同类型"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "工时制"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "就职岗位"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "入职日期"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "试用期"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "标签"}));
				oneTitle = "紧急联络人";
				headList.add(this.getNewArrayList(new String[]{oneTitle, "姓名"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "与本人关系"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "联系地址"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "联系电话"}));
				oneTitle = "最高教育经历";
				headList.add(this.getNewArrayList(new String[]{oneTitle, "学校"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "专业"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "学历"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "是否大专及以上"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "最高学历"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "入学时间"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "毕业时间"}));
				oneTitle = "主要工作经历";
				headList.add(this.getNewArrayList(new String[]{oneTitle, "是否为首份工作"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "工作单位"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "工作岗位"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "开始工作日期"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "结束工作日期"}));
				oneTitle = "主要家庭成员信息";
				headList.add(this.getNewArrayList(new String[]{oneTitle, "成员姓名"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "与本人关系"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "手机号码"}));
				oneTitle = "职业资格信息";
				headList.add(this.getNewArrayList(new String[]{oneTitle, "是否有职业资格证书"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "申报年度"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "职业资格名称"}));
				oneTitle = "伤残信息";
				headList.add(this.getNewArrayList(new String[]{oneTitle, "是否曾被认定为工伤"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "是否有职业病"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "职业病名称"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "是否有传染性疾病"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "传染病名称"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "是否持有残疾人证明"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "伤残病名称"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "伤残等级"}));
				headList.add(this.getNewArrayList(new String[]{oneTitle, "是否从事过井下、高空、高温、特别繁重体力劳动以及有毒有害工种"}));
				oneTitle = "不良记录";
				headList.add(this.getNewArrayList(new String[]{oneTitle, "不良记录描述"}));

				excelWriter = EasyExcelFactory.write(out).registerWriteHandler(ExcelStyle.getNormalStype()).head(headList).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.getTPreEmpMainAllList(searchVo);
						if (Common.isNotNull(list)) {
							ExcelWriterSheetBuilder writeSheet = EasyExcelFactory.writerSheet(name + index);
							excelWriter.write(list, writeSheet.build());
							index++;
						}
						if (Common.isNotNull(list)) {
							list.clear();
						}
					}

					// 1工作经历
					count = baseMapper.getTEmpMainAllCountOneWork(searchVo);
					for (int i = 0; i <= count; i += CommonConstants.EXCEL_EXPORT_LIMIT) {
						// 获取实际记录
						searchVo.setLimitStart(i);
						searchVo.setLimitEnd(CommonConstants.EXCEL_EXPORT_LIMIT);
						list = baseMapper.getTEmpMainAllListOneWork(searchVo);
						if (Common.isNotNull(list)) {
							ExcelWriterSheetBuilder writeSheet = EasyExcelFactory.writerSheet("工作经历" + index);
							excelWriter.write(list, writeSheet.build());
							index++;
						}
						if (Common.isNotNull(list)) {
							list.clear();
						}
					}
					// 2家庭成员
					count = baseMapper.getTEmpMainAllCountTwoFamily(searchVo);
					for (int i = 0; i <= count; i += CommonConstants.EXCEL_EXPORT_LIMIT) {
						// 获取实际记录
						searchVo.setLimitStart(i);
						searchVo.setLimitEnd(CommonConstants.EXCEL_EXPORT_LIMIT);
						list = baseMapper.getTEmpMainAllListTwoFamily(searchVo);
						if (Common.isNotNull(list)) {
							ExcelWriterSheetBuilder writeSheet = EasyExcelFactory.writerSheet("家庭成员" + index);
							excelWriter.write(list, writeSheet.build());
							index++;
						}
						if (Common.isNotNull(list)) {
							list.clear();
						}
					}
					// 3职业资格
					count = baseMapper.getTEmpMainAllCountThreeProfessional(searchVo);
					for (int i = 0; i <= count; i += CommonConstants.EXCEL_EXPORT_LIMIT) {
						// 获取实际记录
						searchVo.setLimitStart(i);
						searchVo.setLimitEnd(CommonConstants.EXCEL_EXPORT_LIMIT);
						list = baseMapper.getTEmpMainAllListThreeProfessional(searchVo);
						if (Common.isNotNull(list)) {
							ExcelWriterSheetBuilder writeSheet = EasyExcelFactory.writerSheet("职业资格" + index);
							excelWriter.write(list, writeSheet.build());
							index++;
						}
						if (Common.isNotNull(list)) {
							list.clear();
						}
					}

				} else {
					WriteSheet writeSheet = EasyExcelFactory.writerSheet(name + index).build();
					excelWriter.write(list, writeSheet);
				}
				if (Common.isNotNull(list)) {
					list.clear();
				}
			}
			out.flush();
			if (excelWriter != null) {
				excelWriter.finish();
			}
		} catch (Exception e) {
			log.error("执行异常", e);
		} finally {
			try {
				if (null != out) {
					out.close();
				}
			} catch (IOException e) {
				log.error("执行异常", e);
			}
		}
	}

}
