package com.engine.recruit.service.impl;

import com.engine.core.impl.Service;
import com.engine.recruit.conn.*;
import com.engine.recruit.enums.ApplicationStatusEnum;
import com.engine.recruit.exception.CustomizeRunTimeException;
import com.engine.recruit.service.TalentPoolService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @author:dxfeng
 * @createTime: 2023/10/20
 * @version: 1.0
 */
public class TalentPoolServiceImpl extends Service implements TalentPoolService {
    @Override
    public Map<String, Object> removeBlacklist(Map<String, Object> param) {
        String ids = Util.null2String(param.get("ids"));
        if (StringUtils.isBlank(ids)) {
            throw new CustomizeRunTimeException("请至少选择一条数据");
        }
        RecordSet rs = new RecordSet();
        rs.executeUpdate("update uf_jcl_rck set sfjrhmd = 1 where id in (" + ids + ")");
        return null;
    }

    @Override
    public Map<String, Object> createNewApplicant(Map<String, Object> param) {
        String rcId = Util.null2String(param.get("rcid"));
        String ypzw = Util.null2String(param.get("ypzw"));
        if (StringUtils.isBlank(ypzw)) {
            throw new CustomizeRunTimeException("请选择应聘职位");
        }
        RecordSet rs = new RecordSet();
        rs.executeQuery("select * from uf_jcl_rck where id = ? ", rcId);
        Map<String, Object> mainDataMap = RecruitRecordSet.getSingleRecordMap(rs);
        String xm = Util.null2String(mainDataMap.get("xm"));
        String sjhm = Util.null2String(mainDataMap.get("sjhm"));


        // 职位重复校验
        List<Map<String, Object>> repeatPositionResumeList = CheckRepeatResume.getRepeatPositionResumeList(xm, sjhm, ypzw);
        if (CollectionUtils.isNotEmpty(repeatPositionResumeList)) {
            throw new CustomizeRunTimeException("该人员已有相同的应聘中的职位");
        }

        Map<String, Object> insertDataMap = buildInsertMap(ypzw, mainDataMap);

        String uuid = UUID.randomUUID().toString();
        insertDataMap.put("modeuuid", uuid);
        int formModeId = ApplicantCommonInfo.getModeIdByTableName("uf_jcl_yppc");
        insertDataMap.put("formmodeid", formModeId);
        RecruitRecordSet.buildModeInsertFields(insertDataMap, user.getUID());
        RecruitRecordSet.insertData(insertDataMap, "uf_jcl_yppc");
        int pcId = RecruitRecordSet.refreshRight(uuid, "uf_jcl_yppc", formModeId, user.getUID());

        // 阶段提醒
        StepMsgCommonInfo.sendMsg(String.valueOf(pcId), Util.null2String(ypzw), Util.null2String(insertDataMap.get("zplc")), Util.null2String(insertDataMap.get("zpjd")), user);

        // 创建明细表数据
        rs.executeUpdate("insert into uf_jcl_yppc_dt1 (mainid,xxmc,xl,zy,kssj,jssj) select ?,xxmc,xl,zy,kssj,jssj from uf_jcl_rck_dt1 where mainid = ?", pcId, rcId);
        rs.executeUpdate("insert into uf_jcl_yppc_dt2 (mainid,gsmc,kssj,jssj,gw,sqyxk,lzyy,gzzz) select ?,gsmc,kssj,jssj,gw,sqyxk,lzyy,gzzz from uf_jcl_rck_dt2 where mainid = ?", pcId, rcId);
        rs.executeUpdate("insert into uf_jcl_yppc_dt3 (mainid,xmmc,kssj,jssj,drjs,xmms) select ?,xmmc,kssj,jssj,drjs,xmms from uf_jcl_rck_dt3 where mainid = ?", pcId, rcId);
        rs.executeUpdate("insert into uf_jcl_yppc_dt4 (mainid,yylx,zwcd) select ?,yylx,zwcd from uf_jcl_rck_dt4 where mainid = ?", pcId, rcId);


        return null;
    }

    private Map<String, Object> buildInsertMap(String ypzw, Map<String, Object> mainDataMap) {
        String zt = ApplicationStatusEnum.DISTRIBUTION.getValue();
        Map<String, Object> insertDataMap = new RecruitDataMap<>();
        insertDataMap.put("sfz", mainDataMap.get("sfz"));
        insertDataMap.put("jlzp", mainDataMap.get("jlzp"));
        insertDataMap.put("ysjl", mainDataMap.get("ysjl"));
        insertDataMap.put("jlly", mainDataMap.get("jlly"));
        insertDataMap.put("xm", mainDataMap.get("xm"));
        insertDataMap.put("xb", mainDataMap.get("xb"));
        insertDataMap.put("sjhm", mainDataMap.get("sjhm"));
        insertDataMap.put("dzyx", mainDataMap.get("dzyx"));
        insertDataMap.put("csrq", mainDataMap.get("csrq"));
        insertDataMap.put("nl", mainDataMap.get("nl"));
        insertDataMap.put("zgxl", mainDataMap.get("zgxl"));
        insertDataMap.put("zgxw", mainDataMap.get("zgxw"));
        insertDataMap.put("byyx", mainDataMap.get("byyx"));
        insertDataMap.put("zy", mainDataMap.get("zy"));
        insertDataMap.put("hyzk", mainDataMap.get("hyzk"));
        insertDataMap.put("gzjy", mainDataMap.get("gzjy"));
        insertDataMap.put("zzzt", mainDataMap.get("zzzt"));
        insertDataMap.put("dqszd", mainDataMap.get("dqszd"));
        insertDataMap.put("tdsj", DateUtil.getDateTime());
        insertDataMap.put("cjr", user.getUID());
        insertDataMap.put("zwpj", mainDataMap.get("zwpj"));
        insertDataMap.put("mz", mainDataMap.get("mz"));
        insertDataMap.put("jg", mainDataMap.get("jg"));
        insertDataMap.put("zzmm", mainDataMap.get("zzmm"));
        insertDataMap.put("sgcm", mainDataMap.get("sgcm"));
        insertDataMap.put("tzkg", mainDataMap.get("tzkg"));


        insertDataMap.put("ypzw", ypzw);
        String flowId = PositionCommonInfo.getRecruitFlowId(ypzw);
        Map<String, String> initialStage = ApplicantCommonInfo.getInitialStage(flowId);
        if (null != initialStage) {
            String zpjd = initialStage.get("zpjd");
            String dqypjd = initialStage.get("jdlx");
            if (StringUtils.isNotBlank(zpjd) && StringUtils.isNotBlank(dqypjd)) {
                insertDataMap.put("zplc", flowId);
                insertDataMap.put("zpjd", zpjd);
                insertDataMap.put("dqypjd", dqypjd);
                zt = ApplicationStatusEnum.CANDIDATE.getValue();
            }
        }
        insertDataMap.put("zt", zt);
        return insertDataMap;
    }
}
