package com.engine.recruit.service.impl;

import com.engine.common.util.ServiceUtil;
import com.engine.core.impl.Service;
import com.engine.recruit.conn.ApplicantCommonInfo;
import com.engine.recruit.constant.RecruitConstant;
import com.engine.recruit.entity.record.ApplicantRecordPo;
import com.engine.recruit.enums.ApplicationStatusEnum;
import com.engine.recruit.enums.RecordOperateEnum;
import com.engine.recruit.service.RecruitButtonService;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.formmode.recruit.modeexpand.util.RecruitModeUtil;
import weaver.general.Util;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * <p>聚才林招聘</p>
 * "加入黑名单"按钮实现类
 *
 * @author:dxfeng
 * @createTime: 2023/09/18
 * @version: 1.0
 */
public class JoinBlacklistServiceImpl extends Service implements RecruitButtonService {
    @Override
    public Map<String, Object> execute(Map<String, Object> params) {
        // 当前应聘者ID
        String billId = Util.null2String(params.get("billId"));
        RecordSet rs = new RecordSet();

        // 推人才表，建模推建模（更新人才表数据），是否加入黑名单字段值变更为“是”
        ServiceUtil.getService(ApplicantResumeServiceImpl.class, user).archiveTalentPool(billId, true,"16");

        // 更新当前应聘者关联的所有简历状态为已淘汰
        String name = "";
        String mobile = "";
        String positionId = "";
        rs.executeQuery("select xm, sjhm, ypzw from uf_jcl_yppc where id = ? ", billId);
        if (rs.next()) {
            name = rs.getString("xm");
            mobile = rs.getString("sjhm");
            positionId = rs.getString("ypzw");
        }
        // 更新当前应聘者关联的所有简历状态为已淘汰
        rs.executeUpdate("update uf_jcl_yppc set zt = ? where xm = ? and sjhm = ?", ApplicationStatusEnum.OBSOLETE.getValue(), name, mobile);

        ApplicantRecordPo recordPo = ApplicantRecordPo.builder()
                .billId(String.valueOf(billId))
                .operateTime(DateUtil.getDateTime())
                .user(user)
                .recordOperateType(RecordOperateEnum.JOIN_BLACKLIST)
                .build();
        recordPo.execute();

        // 消息提醒当前人员简历的跟进者（应聘职位的负责人/协助人）
        String messageType = RecruitConstant.RECRUIT_MESSAGE_TYPE;
        String messageTitle = RecruitConstant.JOIN_BLACKLIST_MESSAGE_TITLE;
        String applicantName = ApplicantCommonInfo.getApplicantName(billId);
        String operatorName = RecruitModeUtil.getResourceNames(String.valueOf(user.getUID()));
        String messageContent = "应聘者：【" + applicantName + "】，已被【" + operatorName + "】加入黑名单，相关投递已自动淘汰，请知悉。";

        // 消息提醒当前人员简历的跟进者（应聘职位的负责人/协助人）
        String zpzwfzr = "";
        String zpxzr = "";
        rs.executeQuery("select zpzwfzr , zpxzr from uf_jcl_zp_zpzw where id = ?", positionId);
        if (rs.next()) {
            zpzwfzr = rs.getString("zpzwfzr");
            zpxzr = rs.getString("zpxzr");
        }
        Set<String> userIdSet = new HashSet<>();
        userIdSet.addAll(Arrays.asList(zpzwfzr.split(",")));
        userIdSet.addAll(Arrays.asList(zpxzr.split(",")));
        RecruitModeUtil.messagePush(messageType, messageTitle, messageContent, userIdSet, user.getUID());

        return null;
    }

}
