package com.jeeplus.human.enrollment.enrollmentRegistration.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.common.SecurityUtils;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.flowable.feign.IFlowableApi;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentKeyCard;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentRegistration;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentSocialSecurityCard;
import com.jeeplus.human.enrollment.enrollmentRegistration.domain.EnrollmentWageCard;
import com.jeeplus.human.enrollment.enrollmentRegistration.mapper.EnrollmentRegistrationMapper;
import com.jeeplus.human.enrollment.enrollmentRegistration.mapper.EnrollmentSocialSecurityCardMapper;
import com.jeeplus.human.enrollment.enrollmentRegistration.mapper.EnrollmentWageCardMapper;
import com.jeeplus.human.enrollment.enrollmentRegistration.service.dto.ExportDTO;
import com.jeeplus.human.enrollment.enrollmentRegistration.utils.SnowFlake;
import com.jeeplus.sys.feign.IOfficeApi;
import com.jeeplus.sys.feign.IPostApi;
import com.jeeplus.sys.feign.IRoleApi;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.*;
import javafx.geometry.Pos;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 王强
 * @version 1.0
 * @date 2023-09-27 14:27
 */
@Service
@Transactional
public class EnrollmentRegistrationService extends ServiceImpl<EnrollmentRegistrationMapper, EnrollmentRegistration> {

    @Resource
    private EnrollmentRegistrationMapper mapper;

    @Resource
    private EnrollmentWageCardService wageCardService;

    @Resource
    private EnrollmentSocialSecurityCardService securityCardService;

    @Resource
    private EnrollmentSocialSecurityCardMapper socialSecurityCardMapper;

    @Resource
    private EnrollmentKeyCardService keyCardService;

    @Resource
    private EnrollmentWageCardMapper wageCardMapper;

    /**
     * 实习人员登记表信息
     * @param page
     * @param projectReportData
     * @return
     * @throws Exception
     */
    public IPage<EnrollmentRegistration> findList(Page<EnrollmentRegistration> page, EnrollmentRegistration projectReportData) throws Exception{
        QueryWrapper<EnrollmentRegistration> queryWrapper = QueryWrapperGenerator.buildQueryCondition (projectReportData,EnrollmentRegistration.class);
        queryWrapper.eq("a.del_flag","0");
        if (StringUtils.isNotBlank(projectReportData.getMobilePhone())) {
            queryWrapper.like("a.mobile_phone",projectReportData.getMobilePhone());
        }
        if (StringUtils.isNotBlank(projectReportData.getName())) {
            queryWrapper.like("a.name",projectReportData.getName());
        }
        if (StringUtils.isNotBlank(projectReportData.getDepartment())) {
            queryWrapper.eq("a.department",projectReportData.getDepartment());
        }
        if (StringUtils.isNotBlank(projectReportData.getCompanyId())) {
            queryWrapper.eq("so.parent_id",projectReportData.getCompanyId());
        }
        IPage<EnrollmentRegistration> list = mapper.findList(page, queryWrapper);
        return list;

    }


    /**
     * 导出实习人员登记表信息
     * @param page
     * @param projectReportData
     * @return
     * @throws Exception
     */
    public IPage<ExportDTO> exportList(Page<ExportDTO> page, ExportDTO projectReportData) throws Exception{
        QueryWrapper<ExportDTO> queryWrapper = QueryWrapperGenerator.buildQueryCondition (projectReportData,ExportDTO.class);
        queryWrapper.eq("a.del_flag","0");
        if (StringUtils.isNotBlank(projectReportData.getMobilePhone())) {
            queryWrapper.like("a.mobile_phone",projectReportData.getMobilePhone());
        }
        if (StringUtils.isNotBlank(projectReportData.getName())) {
            queryWrapper.like("a.name",projectReportData.getName());
        }
        if (StringUtils.isNotBlank(projectReportData.getDepartment())) {
            queryWrapper.eq("a.department",projectReportData.getDepartment());
        }
        if (StringUtils.isNotBlank(projectReportData.getCompanyId())) {
            queryWrapper.eq("so.parent_id",projectReportData.getCompanyId());
        }
        IPage<ExportDTO> list = mapper.exportList(page, queryWrapper);
        for (ExportDTO record : list.getRecords()) {
            //根据用户查询岗位和角色信息
            if (ObjectUtil.isNotEmpty(record)){
                UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getById(record.getUserId());
                if (userDTO != null){
                    if (CollectionUtil.isNotEmpty(userDTO.getRoleDTOList())){
                        StringBuilder roleBuilder = new StringBuilder();
                        for (RoleDTO roleDTO : userDTO.getRoleDTOList()) {
                            RoleDTO dto = SpringUtil.getBean(IRoleApi.class).getRoleDTOByIdForApp(roleDTO.getId());
                            if (ObjectUtil.isNotEmpty(dto)){
                                roleBuilder.append(dto.getName()).append(",");
                            }
                        }

                        // 去掉最后一个多余的逗号
                        if (roleBuilder.length() > 0) {
                            roleBuilder.setLength(roleBuilder.length() - 1);
                        }

                        record.setRole(roleBuilder.toString());
                    }
                    if (CollectionUtil.isNotEmpty(userDTO.getPostDTOList())){
                        StringBuilder postBuilder = new StringBuilder();
                        for (PostDTO postDTO : userDTO.getPostDTOList()) {
                            PostDTO dto = SpringUtil.getBean(IPostApi.class).getPostDTOById(postDTO.getId());
                            if (ObjectUtil.isNotEmpty(dto)){
                                postBuilder.append(dto.getName()).append(",");
                            }
                        }
                        // 去掉最后一个多余的逗号
                        if (postBuilder.length() > 0) {
                            postBuilder.setLength(postBuilder.length() - 1);
                        }
                        record.setPost(postBuilder.toString());
                    }
                }
            }
        }
        return list;

    }


    public EnrollmentRegistration saveRegistration(EnrollmentRegistration registration) throws Exception {
        if (StringUtils.isNotEmpty(registration.getId())){

            EnrollmentRegistration report = mapper.getById(registration.getId());
            if (report != null){
                return update(registration);
            }
        }
        return add(registration);
    }

    public EnrollmentRegistration saveRegistration2(EnrollmentRegistration registration) throws Exception {
        if (StringUtils.isNotEmpty(registration.getId())){

            EnrollmentRegistration report = mapper.getById(registration.getId());
            if (report != null){
                return update2(registration);
            }
        }
        return add(registration);
    }

    /**
     * 新增
     */
    @Transactional(rollbackFor = Exception.class)
    public EnrollmentRegistration add(EnrollmentRegistration reportData) throws Exception{

        String currentToken = TokenProvider.getCurrentToken();

        QueryWrapper<EnrollmentRegistration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_card", reportData.getIdCard());
        queryWrapper.eq("del_flag", 0);

        // 删除基础信息表
        Integer integer = mapper.selectCount(queryWrapper);
        Integer mobilePhoneOnly = SpringUtil.getBean(IUserApi.class).selectCountByMobile(reportData.getMobilePhone());

        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));

        EnrollmentRegistration report = new EnrollmentRegistration();

        if (integer <=0 && mobilePhoneOnly <=0 ) {
            //为空则表示移动端扫码登录
            if (StringUtils.isBlank(reportData.getIsPc())) {
                reportData.setIsPc("2");
            }
            String id = SnowFlake.getId();
            BeanUtils.copyProperties(reportData, report);
            report.setId(id);
            report.setCreateById("1");
            report.setCreateTime(new Date());
            report.setUpdateById("1");
            report.setUpdateTime(new Date());

            if (StringUtils.isNotBlank(report.getSocialSecurityNumber())){
                EnrollmentSocialSecurityCard securityCard = new EnrollmentSocialSecurityCard();
                String amountId = SnowFlake.getId();
                securityCard.setId(amountId);
                securityCard.setCreateById("1");
                securityCard.setCreateTime(new Date());
                securityCard.setUpdateById("1");
                securityCard.setUpdateTime(new Date());
                securityCard.setSocialSecurityNumber(report.getSocialSecurityNumber());
                securityCard.setSocialSecurityBankNumber(report.getSocialSecurityBankNumber());
                securityCard.setSocialSecurityPictureFront(report.getSocialSecurityPictureFront());
                securityCard.setSocialSecurityPictureOpposite(report.getSocialSecurityPictureOpposite());
                securityCard.setEnrollmentRegistrationId(id);

                socialSecurityCardMapper.insert(securityCard);
            }

            //if (null == currentToken) {

                //发起员工入职流程
                Map map = SpringUtil.getBean(IFlowableApi.class).getByNameForFen("员工入职");
                String procDefId = map.get("id") + "";

                report.setProcessDefinitionId(procDefId);
                report.setProcessType("0");
                mapper.insert(report);

                Map<String, Map<String, String>> allMap = new HashMap<>();

                String procDefKey = map.get("key") + "";
                String businessTable = "human_resources_enrollment_registration";

                String businessId = id;
                String title = "管理员发起了- [" + report.getName() + "员工入职]";

                //获取实习信息审核员信息
                RoleDTO roleDTO = SpringUtil.getBean(IRoleApi.class).getRoleDTOByName2("实习信息审核员");
                String assignee = getAssignee(roleDTO.getId());
                String recordType = "";

                Map<String, String> newMap = pingMap(procDefId, procDefKey, businessTable, businessId, title, assignee, recordType);
                allMap.put("入职", newMap);
            if (StringUtils.isBlank(reportData.getIsPc())) {
                SpringUtil.getBean(IFlowableApi.class).startForFenNew(allMap);
            }
            /*} else {
                mapper.insert(report);
            }*/
        }

        return report;
    }

    public String getAssignee(String roleId){
        List<UserDTO> userDTOS = SpringUtil.getBean(IUserApi.class).findListByRoleId(roleId);
        StringJoiner idJoiner = new StringJoiner(",");
        for (UserDTO user : userDTOS) {
            idJoiner.add(String.valueOf(user.getId()));
        }

        return idJoiner.toString();
    }

    public String getAssignee2(String roleId){
        List<UserDTO> userDTOS = SpringUtil.getBean(IUserApi.class).findListFlowAbleByPostId(roleId);
        StringJoiner idJoiner = new StringJoiner(",");
        for (UserDTO user : userDTOS) {
            idJoiner.add(String.valueOf(user.getId()));
        }

        return idJoiner.toString();
    }
    public Map<String,String> pingMap(String procDefId,String procDefKey,String businessTable,
                                      String businessId,String title,String assignee,String recordType){
        Map<String,String> map = new HashMap();
        map.put("procDefId", procDefId);
        map.put("procDefKey", procDefKey);
        map.put("businessTable", businessTable);
        map.put("businessId", businessId);
        map.put("title", title);
        map.put("assignee", assignee);
        map.put("recordType", recordType);
        return map;
    }

    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    public EnrollmentRegistration update(EnrollmentRegistration reportData) throws Exception{
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));

        EnrollmentRegistration report = new EnrollmentRegistration();
        BeanUtils.copyProperties(reportData, report);
        report.setUpdateById(userDTO.getId());
        report.setUpdateTime(new Date());

        //生成一个系统账号
        // 生成id
        String amountId = "";
        if (StringUtils.isBlank(report.getUserId())) {
            amountId = SnowFlake.getId();
            report.setUserId(amountId);
        } else {
            amountId = report.getUserId();
        }


        if (StringUtils.isNotBlank(report.getType()) && !userDTO.isAdmin()){
            if (report.getType().equals("5")){
                //获取当前的用户，审核的人
                UserDTO dto = new UserDTO();
                //设置信息
                dto.setId(amountId);
                dto.setName(report.getName());
                dto.setPassword(SecurityUtils.encryptPassword("123456"));
                dto.setLoginName(report.getName());
                dto.setLoginFlag("1");
                dto.setMobile(report.getMobilePhone());
                dto.setPhoto("");
//                dto.setTenantDTO(userDTO.getTenantDTO());
                //部门
                OfficeDTO officeDTO = new OfficeDTO();
                officeDTO.setId(report.getDepartment());
                dto.setOfficeDTO(officeDTO);
                //设置角色
                OfficeDTO office = SpringUtil.getBean(IOfficeApi.class).getOfficeById(report.getDepartment());
                //公司
                OfficeDTO companyDto = new OfficeDTO();
                companyDto.setId(office.getParent().getId());
                dto.setCompanyDTO(companyDto);

                dto.setNo(report.getMobilePhone());

                String name = "兴光会计员工";
                List <RoleDTO> roleIdList = new ArrayList<>();

//                roleIdList.add(roleInfo);


                String officeName = "";
                String networkAddress = "";
                if (null != office) {
                    Map<String,String> keyMap = new HashMap<>();
                    TenantDTO tenantDTO = new TenantDTO();
                    tenantDTO.setId(office.getTenantId());
                    dto.setTenantDTO(tenantDTO);
                    if (office.getTenantId().equals("10003")){
                        officeName = "会计业务管理公司";
                        networkAddress = "cpapmis.xgccpm.com";
                        keyMap.put("name", "兴光会计员工,共有权限（会计）");
                        keyMap.put("tenantId", office.getTenantId());
                        roleIdList = SpringUtil.getBean(IRoleApi.class).getRoleDTOByNameAndTenantId(keyMap);
                    }else if (office.getTenantId().equals("10004")) {
                        officeName = "中审管理公司";
                        networkAddress = "zs.xgccpm.com";
                        keyMap.put("name", "中审员工,共有权限（中审）");
                        keyMap.put("tenantId", office.getTenantId());
                        roleIdList = SpringUtil.getBean(IRoleApi.class).getRoleDTOByNameAndTenantId(keyMap);
                    }else if (office.getTenantId().equals("10001")) {
                        officeName = "评估管理公司";
                        networkAddress = "http://amc.xgccpm.com";
                        keyMap.put("name", "评估员工,共有权限（评估）");
                        keyMap.put("tenantId", office.getTenantId());
                        roleIdList = SpringUtil.getBean(IRoleApi.class).getRoleDTOByNameAndTenantId(keyMap);
                    }else if (office.getTenantId().equals("10006")) {
                        officeName = "苏州分公司";
                        networkAddress = "sz.xgccpm.com";
                        keyMap.put("name", "审计员工,共有权限（苏州）");
                        keyMap.put("tenantId", office.getTenantId());
                        roleIdList = SpringUtil.getBean(IRoleApi.class).getRoleDTOByNameAndTenantId(keyMap);
                    }else if (office.getTenantId().equals("10005")) {
                        officeName = "兴光项目公司";
                        networkAddress = "xm.xgccpm.com";
                        keyMap.put("name", "项目公司综合管理部员工,共有权限（项目）");
                        keyMap.put("tenantId", office.getTenantId());
                        roleIdList = SpringUtil.getBean(IRoleApi.class).getRoleDTOByNameAndTenantId(keyMap);
                    }
                } else {
                    TenantDTO tenantDTO = new TenantDTO();
                    tenantDTO.setId("10002");
                    dto.setTenantDTO(tenantDTO);
                    officeName = "综合管理公司";
                }
                dto.setRoleDTOList(roleIdList);


                SpringUtil.getBean ( IUserApi.class ).saveOrUpdate(dto);


//                report.setOnJobStatus("正式");
                //发送短信通知 (未实现)

                PostDTO postDTO = SpringUtil.getBean(IPostApi.class).getPostDTOByName("门卡管理员");
                List<UserDTO> listByPostId = SpringUtil.getBean(IUserApi.class).findListFlowAbleByPostId(postDTO.getId());

                String randomCode = report.getName() + "你好!欢迎入职" + officeName + "，请尽快登录公司系统:" + networkAddress
                        + "完善个人信息（包括社保卡号与工资卡号）并修改初始密码，系统初始账号密码为：" + report.getName() +
                        "/123456，人事部门已在为你办理门禁卡，办理成功后将由系统告知，在收到系统通知后，请及时到信息部"+ listByPostId.get(0).getName() +"处领取门禁卡！"+
                        "注：工资卡需同时提供中信银行、招商银行卡号";
                Map<String, Object> stringObjectMap = SpringUtil.getBean(IUserApi.class).sendRandomCodes(report.getMobilePhone(), randomCode);
                String statusCode = (String) stringObjectMap.get("statusCode");
                if (("000000").equals(statusCode)) {
                    System.out.println("短信发送成功!");
                }else if(statusCode.equals("160040")){
                    System.out.println("手机号获取验证码次数已达每日上限!");
                }else{
                    System.out.println("短信发送失败，错误代码：101，请联系管理员!");
                }

                //当流程结束的时候，发起工资卡信息完善流程
                Map map = SpringUtil.getBean(IFlowableApi.class).getByNameForFen("工资卡信息完善");
                String procDefId = map.get("id")+"";

                //当流程结束的时候，发起工资卡信息完善流程
                String uid = SnowFlake.getId();
                EnrollmentWageCard wageCard = new EnrollmentWageCard();
                wageCard.setId(uid);
                wageCard.setCreateById("1");
                wageCard.setUpdateById("1");
                wageCard.setProcessDefinitionId(procDefId);
                wageCard.setEnrollmentRegistrationId(report.getId());
                wageCardService.save(wageCard);

                String procDefKey = map.get("key")+"";
                String businessTable = "human_resources_enrollment_wage_card";
                String businessId = uid;
                String title = "工资卡信息完善";
                String assignee = amountId;
                String recordType = "";
                Map<String,String> newMap = new HashMap();
                newMap.put("procDefId", procDefId);
                newMap.put("procDefKey", procDefKey);
                newMap.put("businessTable", businessTable);
                newMap.put("businessId", businessId);
                newMap.put("title", title);
                newMap.put("assignee", assignee);
                newMap.put("recordType", recordType);
                String procInsId = SpringUtil.getBean(IFlowableApi.class).startForFen(newMap);

                wageCard.setProcInsId(procInsId);
                wageCard.setType("2");
                wageCardService.saveOrUpdate(wageCard);

                //发起社保流程
                if (StringUtils.isBlank(report.getSocialSecurityNumber())){
                    Map cardMap = SpringUtil.getBean(IFlowableApi.class).getByNameForFen("社保登记");
                    String procDefId2 = cardMap.get("id")+"";

                    //当流程结束的时候，发起工资卡信息完善流程
                    String uuid = SnowFlake.getId();
                    EnrollmentSocialSecurityCard securityCard = new EnrollmentSocialSecurityCard();
                    securityCard.setId(uuid);
                    securityCard.setCreateById("1");
                    securityCard.setUpdateById("1");
                    securityCard.setProcessDefinitionId(procDefId2);
                    securityCard.setEnrollmentRegistrationId(report.getId());
                    securityCardService.save(securityCard);

                    String procDefKey2 = cardMap.get("key")+"";
                    String businessTable2 = "human_resources_enrollment_social_security_card";
                    String businessId2 = uuid;
                    String title2 = "社保卡信息完善";
                    String assignee2 = amountId;
                    String recordType2 = "";
                    Map<String,String> newMap2 = new HashMap();
                    newMap2.put("procDefId", procDefId2);
                    newMap2.put("procDefKey", procDefKey2);
                    newMap2.put("businessTable", businessTable2);
                    newMap2.put("businessId", businessId2);
                    newMap2.put("title", title2);
                    newMap2.put("assignee", assignee2);
                    newMap2.put("recordType", recordType2);
                    String procInsId2 = SpringUtil.getBean(IFlowableApi.class).startForFen(newMap2);

                    securityCard.setProcInsId(procInsId2);
                    securityCard.setType("2");
                    securityCardService.saveOrUpdate(securityCard);
                }
                //发起门卡办理通知流程
                Map keyMap = SpringUtil.getBean(IFlowableApi.class).getByNameForFen("门卡办理");
                String keyProcDefId = keyMap.get("id")+"";

                //当流程结束的时候，发起工资卡信息完善流程
                String keyUid = SnowFlake.getId();
                EnrollmentKeyCard keyCard = new EnrollmentKeyCard();
                keyCard.setId(keyUid);
                keyCard.setCreateById("1");
                keyCard.setUpdateById("1");
                keyCard.setProcessDefinitionId(keyProcDefId);
                keyCard.setEnrollmentRegistrationId(report.getId());
                keyCardService.save(keyCard);

                String keyProcDefKey = keyMap.get("key")+"";
                String keyBusinessTable = "human_resources_enrollment_key_card";
                String keyBusinessId = keyUid;
                String keyTitle = report.getName() +  "的门卡办理";

//                UserDTO userInfo = SpringUtil.getBean(IUserApi.class).getByLoginNameNotTenantId("赵俐");
//                String keyAssignee = userInfo.getId();

                PostDTO postInfo = SpringUtil.getBean(IPostApi.class).getPostDTOByName("门卡管理员");
                String keyAssignee = getAssignee2(postInfo.getId());

                String keyRecordType = "";
                Map<String,String> keyMap2 = new HashMap();
                keyMap2.put("procDefId", keyProcDefId);
                keyMap2.put("procDefKey", keyProcDefKey);
                keyMap2.put("businessTable", keyBusinessTable);
                keyMap2.put("businessId", keyBusinessId);
                keyMap2.put("title", keyTitle);
                keyMap2.put("assignee", keyAssignee);
                keyMap2.put("recordType", keyRecordType);
                String procInsId2 = SpringUtil.getBean(IFlowableApi.class).startForFen(keyMap2);
                keyCard.setProcInsId(procInsId2);
                keyCard.setType("2");
                keyCardService.saveOrUpdate(keyCard);
            }
        }

        mapper.updateById(report);
        return report;
    }

    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    public EnrollmentRegistration update2(EnrollmentRegistration reportData) throws Exception{
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));

        EnrollmentRegistration report = new EnrollmentRegistration();
        BeanUtils.copyProperties(reportData, report);
        report.setUpdateById(userDTO.getId());
        report.setUpdateTime(new Date());

        mapper.updateById(report);
        return report;
    }

    public EnrollmentRegistration findById(String id) throws Exception {
        // 查询基础信息表
        EnrollmentRegistration info = mapper.getById(id);
        //根据用户查询岗位和角色信息
        if (ObjectUtil.isNotEmpty(info)){
            UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getById(info.getUserId());
            ArrayList<String> roles = new ArrayList<>();
            ArrayList<String> posts = new ArrayList<>();
            if (userDTO != null){
                if (CollectionUtil.isNotEmpty(userDTO.getRoleDTOList())){
                    for (RoleDTO roleDTO : userDTO.getRoleDTOList()) {
                        roles.add(roleDTO.getId());
                    }
                }
                if (CollectionUtil.isNotEmpty(userDTO.getPostDTOList())){
                    for (PostDTO postDTO : userDTO.getPostDTOList()) {
                        posts.add(postDTO.getId());
                    }
                }
            }
            info.setRoleIdList(roles);
            info.setPostIdList(posts);
        }

        return info;
    }

    public Integer findByUserId(String userId) throws Exception {
        QueryWrapper<EnrollmentRegistration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("del_flag", 0);
        // 删除基础信息表
        Integer integer = mapper.selectCount(queryWrapper);
        return integer;
    }

    public void updateStatusById(EnrollmentRegistration dto) {
        EnrollmentRegistration registration = mapper.getById(dto.getId());
        mapper.updateStatusById(dto.getId(), dto.getType());
        //判断流程的类型  是入职登记流程 还是 个人信息完善流程
        if (StringUtils.isNotBlank(registration.getProcessType()) && registration.getProcessType().equals("0")) {
            //说明是入职登记流程  这时删除入职登记相关信息
            mapper.deleteByIdOnPhysic(dto.getId());

        }



    }

    public String remove(String id) {
        //先删除用户表中的信息
        EnrollmentRegistration registration = mapper.getById(id);
        if (registration != null){
            UserDTO userDTO = SpringUtil.getBean(IUserApi.class).getByMobile(registration.getMobilePhone());
            if (userDTO!=null){
                SpringUtil.getBean(IUserApi.class).deleteById(userDTO.getId());
            }
        }
        // 删除基础信息表
        mapper.removeById(id);
        return "操作成功";
    }

    public Integer findIdCardOnly(String idCard) throws Exception {
        QueryWrapper<EnrollmentRegistration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id_card", idCard);
        queryWrapper.eq("del_flag", 0);
        // 删除基础信息表
        Integer integer = mapper.selectCount(queryWrapper);
        return integer;
    }

    public Integer findMobilePhoneOnly(String mobilePhone) throws Exception {
        // 删除基础信息表
        Integer integer = SpringUtil.getBean(IUserApi.class).selectCountByMobile(mobilePhone);
        return integer;
    }

    /**
     * 获取正式工在职状态为离职的，并且user表中未删除的
     * @return
     */
    public List<EnrollmentRegistration> getFormalUserInfo() {
        return mapper.getFormalUserInfo();
    }

    public void updateRegistration(EnrollmentRegistration reportData) {
        //获取当前登录人信息
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken ( ));

        EnrollmentRegistration report = new EnrollmentRegistration();
        BeanUtils.copyProperties(reportData, report);
        report.setUpdateById(userDTO.getId());
        report.setUpdateTime(new Date());

        mapper.updateById(report);

        if (StringUtils.isNotBlank(report.getSocialSecurityNumber())){
            EnrollmentSocialSecurityCard securityCard = new EnrollmentSocialSecurityCard();
            String amountId = SnowFlake.getId();
            securityCard.setId(amountId);
            securityCard.setCreateById("1");
            securityCard.setCreateTime(new Date());
            securityCard.setUpdateById("1");
            securityCard.setUpdateTime(new Date());
            securityCard.setSocialSecurityNumber(report.getSocialSecurityNumber());
            securityCard.setSocialSecurityBankNumber(report.getSocialSecurityBankNumber());
            securityCard.setSocialSecurityPictureFront(report.getSocialSecurityPictureFront());
            securityCard.setSocialSecurityPictureOpposite(report.getSocialSecurityPictureOpposite());
            securityCard.setEnrollmentRegistrationId(report.getId());

            socialSecurityCardMapper.insert(securityCard);
        }

        /**
         * 修改工资卡信息
         */
        EnrollmentWageCard wageCard = new EnrollmentWageCard();
        wageCard.setEnrollmentRegistrationId(report.getId());
        wageCard.setId(report.getWageCardId());
        wageCard.setAccountHolderFront(report.getAccountHolderFront());
        wageCard.setAccountHolderOpposite(report.getAccountHolderOpposite());
        wageCard.setZxAccountHolder(report.getZxAccountHolder());
        wageCard.setGsAccountHolder(report.getGsAccountHolder());
        wageCard.setGsAccountHolderFront(report.getGsAccountHolderFront());
        wageCard.setGsAccountHolderOpposite(report.getGsAccountHolderOpposite());
        wageCard.setZxBankCardNumber(report.getZxBankCardNumber());
        wageCard.setGsBankCardNumber(report.getGsBankCardNumber());
        wageCardService.saveOrUpdate(wageCard);



        /**
         * 修改用户信息
         */
        EnrollmentRegistration registration = mapper.getById(reportData.getId());
        if (StringUtils.isNotBlank(registration.getUserId())){
            UserDTO dto = SpringUtil.getBean(IUserApi.class).getById(registration.getUserId());
            if (dto != null){
                //往用户表中新增一条数据
                UserDTO dto1 = new UserDTO();
                dto1.setId(dto.getId());
                dto1.setName(report.getName());
                dto1.setLoginName(report.getName());
                dto1.setMobile(report.getMobilePhone());
                dto1.setNo(report.getMobilePhone());
                dto1.setPhoto("");
                //根据实习部门获取部门信息
                OfficeDTO officeDTO = SpringUtil.getBean(IOfficeApi.class).getOfficeById(registration.getDepartment());
                dto1.setOfficeDTO(officeDTO);
                OfficeDTO officeDTO1 = new OfficeDTO();
                officeDTO1.setId(officeDTO.getParent().getId());
                dto1.setCompanyDTO(officeDTO1);
                //角色和岗位信息
                List <RoleDTO> roleIdList = new ArrayList<>();
                List <PostDTO> postDTOList = new ArrayList<>();
                if (CollectionUtil.isNotEmpty(reportData.getPostIdList())){
                    for (String post : reportData.getPostIdList()) {
                        PostDTO postDTO = new PostDTO();
                        postDTO.setId(post);
                        postDTOList.add(postDTO);
                    }
                }
                if (CollectionUtil.isNotEmpty(reportData.getRoleIdList())){
                    for (String role : reportData.getRoleIdList()) {
                        RoleDTO roleDTO = SpringUtil.getBean(IRoleApi.class).getRoleDTOById(role);
                        roleIdList.add(roleDTO);
                    }
                }
                //所属租户
                TenantDTO tenantDTO = new TenantDTO();
                tenantDTO.setId(officeDTO.getTenantId());
                dto1.setTenantDTO(tenantDTO);
//                if (null != officeDTO) {
//                    Map<String,String> keyMap = new HashMap<>();
//                    TenantDTO tenantDTO = new TenantDTO();
//                    tenantDTO.setId(officeDTO.getTenantId());
//                    dto1.setTenantDTO(tenantDTO);
//                    if (officeDTO.getTenantId().equals("10003")){
//                        keyMap.put("name", "兴光会计员工,共有权限（会计）");
//                        keyMap.put("tenantId", officeDTO.getTenantId());
//                        roleIdList = SpringUtil.getBean(IRoleApi.class).getRoleDTOByNameAndTenantId(keyMap);
//                    }else if (officeDTO.getTenantId().equals("10004")) {
//                        keyMap.put("name", "中审员工,共有权限（中审）");
//                        keyMap.put("tenantId", officeDTO.getTenantId());
//                        roleIdList = SpringUtil.getBean(IRoleApi.class).getRoleDTOByNameAndTenantId(keyMap);
//                    }else if (officeDTO.getTenantId().equals("10001")) {
//                        keyMap.put("name", "评估员工,共有权限（评估）");
//                        keyMap.put("tenantId", officeDTO.getTenantId());
//                        roleIdList = SpringUtil.getBean(IRoleApi.class).getRoleDTOByNameAndTenantId(keyMap);
//                    }
//                } else {
//                    TenantDTO tenantDTO = new TenantDTO();
//                    tenantDTO.setId("10002");
//                    dto1.setTenantDTO(tenantDTO);
//                }
                dto1.setRoleDTOList(roleIdList);
                dto1.setPostDTOList(postDTOList);
                SpringUtil.getBean ( IUserApi.class ).saveOrUpdate(dto1);


            }
        }

    }

    public void insertIntoEnrollmentRegistrationInfo(String value) {
        EnrollmentRegistration infos = JSON.parseObject(value, new TypeReference<EnrollmentRegistration>() {});
        mapper.insert(infos);
    }

    public List<String> getAllUserIdInSuccess() {
        return mapper.getAllUserIdInSuccess();
    }

    /**
     * 用户新增时对入职信息进行同步
     * @param registration
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveInfo(EnrollmentRegistration registration) {
        //现根据userId查询入职信息
        if (StringUtils.isNotBlank(registration.getUserId())){
            registration.setType("5");
            registration.setOnJobStatus("2");
            EnrollmentRegistration enrollmentRegistration=mapper.getByUserId(registration.getUserId());
            if (enrollmentRegistration != null){
                registration.setId(enrollmentRegistration.getId());
                mapper.updateById(registration);
            }else {
                registration.setId(SnowFlake.getId());
                mapper.insert(registration);
            }


        }


    }

    /**
     * 根据用户id删除用户信息
     * @param id
     */
    public void delByUserId(String id) {
        mapper.delByUserId(id);
    }

    /**
     * 添加导入的用户信息
     * @param staffUserInfoDTO
     */
    public void updateInfo(EnrollmentRegistration staffUserInfoDTO) {
        if (ObjectUtil.isNotEmpty(staffUserInfoDTO)){
            if (StringUtils.isNotBlank(staffUserInfoDTO.getMobilePhone())){
                //根据手机号查询信息
                EnrollmentRegistration registration = mapper.getByMobile(staffUserInfoDTO.getMobilePhone());
                if (ObjectUtil.isNotEmpty(registration)){
                    if (registration.getName().equals(staffUserInfoDTO.getName())){
                        registration.setSex(staffUserInfoDTO.getSex());//性别
                        registration.setBirthday(staffUserInfoDTO.getBirthday());//生日
                        registration.setAge(staffUserInfoDTO.getAge());//年龄
                        registration.setNativePlace(staffUserInfoDTO.getNativePlace());//籍贯
                        registration.setOnJobStatus(staffUserInfoDTO.getOnJobStatus());//正式
                        registration.setDepartment(staffUserInfoDTO.getDepartment());//所属部门
                        registration.setMobilePhone(staffUserInfoDTO.getMobilePhone());//联系电话
                        if (ObjectUtil.isNotEmpty(staffUserInfoDTO.getInDate())){
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                            String format = simpleDateFormat.format(staffUserInfoDTO.getInDate());
                            registration.setEntryDate(format);//入职时间
                        }
                        registration.setIdCard(staffUserInfoDTO.getIdCard());//身份证
                        mapper.updateById(registration);
                        if (com.jeeplus.utils.StringUtils.isNotBlank(staffUserInfoDTO.getRole()) && staffUserInfoDTO.getRole().contains(",")){
                            if (StringUtils.isNotBlank(registration.getUserId())){
                                UserDTO dto = SpringUtil.getBean(IUserApi.class).getById(registration.getUserId());
                                List <RoleDTO> roleIdList = new ArrayList<>();
                                String[] split = staffUserInfoDTO.getRole().split(",");
                                for (String s : split) {
                                    HashMap<String, String> hashMap = new HashMap<>();
                                    hashMap.put("name", s);
                                    hashMap.put("tenantId", dto.getTenantDTO().getId());
                                    List<RoleDTO> roleDTOS = SpringUtil.getBean(IRoleApi.class).getRoleDTOByNameAndTenantId(hashMap);
                                    roleIdList.addAll(roleDTOS);
                                }
                                dto.setRoleDTOList(roleIdList);
                                //根据实习部门获取部门信息
                                OfficeDTO officeDTO = SpringUtil.getBean(IOfficeApi.class).getOfficeById(staffUserInfoDTO.getDepartment());
                                dto.setOfficeDTO(officeDTO);
                                OfficeDTO officeDTO1 = new OfficeDTO();
                                officeDTO1.setId(officeDTO.getParent().getId());
                                dto.setCompanyDTO(officeDTO1);
                                TenantDTO tenantDTO = new TenantDTO();
                                tenantDTO.setId(officeDTO.getTenantId());
                                dto.setTenantDTO(tenantDTO);
                                SpringUtil.getBean ( IUserApi.class ).saveOrUpdate(dto);
                            }
                        }
                    }
                }else {
                    //入职id
                    String id = SnowFlake.getId();
                    EnrollmentRegistration enrollmentRegistration = new EnrollmentRegistration();
                    enrollmentRegistration.setId(id);
                    enrollmentRegistration.setCreateById("1");
                    enrollmentRegistration.setCreateTime(new Date());
                    enrollmentRegistration.setUpdateById("1");
                    enrollmentRegistration.setUpdateTime(new Date());
                    enrollmentRegistration.setName(staffUserInfoDTO.getName());
                    enrollmentRegistration.setSex(staffUserInfoDTO.getSex());//性别
                    enrollmentRegistration.setBirthday(staffUserInfoDTO.getBirthday());//生日
                    enrollmentRegistration.setAge(staffUserInfoDTO.getAge());//年龄
                    enrollmentRegistration.setNativePlace(staffUserInfoDTO.getNativePlace());//籍贯
                    enrollmentRegistration.setOnJobStatus(staffUserInfoDTO.getOnJobStatus());//正式
                    enrollmentRegistration.setDepartment(staffUserInfoDTO.getDepartment());//所属部门
                    enrollmentRegistration.setMobilePhone(staffUserInfoDTO.getMobilePhone());//联系电话
                    if (ObjectUtil.isNotEmpty(staffUserInfoDTO.getInDate())){
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        String format = simpleDateFormat.format(staffUserInfoDTO.getInDate());
                        enrollmentRegistration.setEntryDate(format);//入职时间
                    }
                    enrollmentRegistration.setIdCard(staffUserInfoDTO.getIdCard());//身份证
                    enrollmentRegistration.setType("5");//状态
                    //根据手机号查询用户信息
                    UserDTO dto = SpringUtil.getBean(IUserApi.class).getByMobile(staffUserInfoDTO.getMobilePhone());

                    //新增用户信息
                    UserDTO dto1 = new UserDTO();
                    if (ObjectUtil.isNotEmpty(dto)){
                        enrollmentRegistration.setUserId(dto.getId());
                        BeanUtils.copyProperties(dto,dto1);
                    }else {
                        //生成一个用户id
                        String userId = SnowFlake.getId();
                        enrollmentRegistration.setUserId(userId);
                        dto1.setId(userId);
                        dto1.setName(staffUserInfoDTO.getName());
                        dto1.setLoginName(staffUserInfoDTO.getName());
                        dto1.setMobile(staffUserInfoDTO.getMobilePhone());
                        dto1.setNo(staffUserInfoDTO.getMobilePhone());
                        dto1.setPhoto("");
                        //根据实习部门获取部门信息
                        OfficeDTO officeDTO = SpringUtil.getBean(IOfficeApi.class).getOfficeById(staffUserInfoDTO.getDepartment());
                        dto1.setOfficeDTO(officeDTO);
                        OfficeDTO officeDTO1 = new OfficeDTO();
                        officeDTO1.setId(officeDTO.getParent().getId());
                        dto1.setCompanyDTO(officeDTO1);
                        TenantDTO tenantDTO = new TenantDTO();
                        tenantDTO.setId(officeDTO.getTenantId());
                        dto1.setTenantDTO(tenantDTO);
                    }
                    if (com.jeeplus.utils.StringUtils.isNotBlank(staffUserInfoDTO.getRole()) && staffUserInfoDTO.getRole().contains(",")){
                        List <RoleDTO> roleIdList = new ArrayList<>();
                        String[] split = staffUserInfoDTO.getRole().split(",");
                        for (String s : split) {
                            HashMap<String, String> hashMap = new HashMap<>();
                            hashMap.put("name", s);
                            hashMap.put("tenantId", dto.getTenantDTO().getId());
                            List<RoleDTO> roleDTOS = SpringUtil.getBean(IRoleApi.class).getRoleDTOByNameAndTenantId(hashMap);
                            roleIdList.addAll(roleDTOS);
                        }
                        dto1.setRoleDTOList(roleIdList);
                    }
                    SpringUtil.getBean ( IUserApi.class ).saveOrUpdate(dto1);
                    mapper.insert(enrollmentRegistration);
                }
            }
        }

    }
}
