package weaver.formmode.recruit.modeexpand.interview;

import com.engine.recruit.conn.ApplicantCommonInfo;
import com.engine.recruit.conn.RecruitDataMap;
import com.engine.recruit.conn.RecruitRecordSet;
import com.engine.recruit.exception.CustomizeRunTimeException;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;

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

/**
 * @author:dxfeng
 * @createTime: 2024/09/23
 * @version: 1.0
 */
public class InterviewEvaluate {
    private static final String TABLE_NAME = "uf_jcl_mspjfk";

    /**
     * 创建面试，针对面试官创建面试反馈记录，同步批次ID，面试ID
     *
     * @param mainDataMap
     * @param msId
     * @param user
     */
    public static void createEvaluate(Map<String, Object> mainDataMap, int msId, User user) {
        int formModeId = ApplicantCommonInfo.getModeIdByTableName(TABLE_NAME);
        if (formModeId < 0) {
            new BaseBean().writeLog("uf_jcl_mspjfk,未查询到对应模板");
        }
        String msg = Util.null2String(mainDataMap.get("msg"));
        if (StringUtils.isBlank(msg)) {
            throw new CustomizeRunTimeException("面试官为空，请检查面试官设置");
        }
        String[] split = msg.split(",");
        for (String msgId : split) {
            insertData(mainDataMap, msId, user, TABLE_NAME, formModeId, msgId);
        }
    }


    /**
     * 调整面试，同步调整面试反馈记录，若删除面试官则删除对应的面试反馈记录，若增加面试官，则创建增加面试的面试反馈记录
     *
     * @param mainDataMap
     * @param msId
     * @param user
     */
    public static void updateEvaluate(Map<String, Object> mainDataMap, int msId, User user) {
        RecordSet rs = new RecordSet();
        int formModeId = ApplicantCommonInfo.getModeIdByTableName(TABLE_NAME);
        if (formModeId < 0) {
            rs.writeLog("uf_jcl_mspjfk,未查询到对应模板");
        }
        String msg = Util.null2String(mainDataMap.get("msg"));
        if (StringUtils.isBlank(msg)) {
            throw new CustomizeRunTimeException("面试官为空，请检查面试官设置");
        }

        // 删除非面试官数据
        rs.executeUpdate("delete from " + TABLE_NAME + " where msid=" + msId + " and msg not in (" + msg + ")");

        String[] split = msg.split(",");
        String selectSql = "select id from " + TABLE_NAME + " where msid = ? and msg = ?";
        for (String msgId : split) {
            String billId = "";
            rs.executeQuery(selectSql, msId, msgId);
            if (rs.next()) {
                billId = rs.getString("id");
            }

            if (StringUtils.isBlank(billId)) {
                // 新增数据
                insertData(mainDataMap, msId, user, TABLE_NAME, formModeId, msgId);
            } else {
                // 更新数据
                RecruitDataMap<Object> dataMap = new RecruitDataMap<>();
                // id
                dataMap.put("id", billId);
                // 应聘者
                dataMap.put("ypz", mainDataMap.get("ypz"));
                // 应聘职位
                dataMap.put("ypzw", mainDataMap.get("ypzw"));
                // 面试方式
                dataMap.put("msfs", mainDataMap.get("msfs"));
                // 面试环节
                String mshj = Util.null2String(mainDataMap.get("mshj"));
                if (StringUtils.isNotBlank(mshj)) {
                    dataMap.put("mshj", mshj);
                }
                // 面试轮次
                dataMap.put("msc", mainDataMap.get("msc"));
                // 面试日期
                dataMap.put("msrq", mainDataMap.get("msrq"));
                // 面试地址
                dataMap.put("msdd", mainDataMap.get("msdd"));

                // 更新数据
                RecruitRecordSet.updateDataById(dataMap, TABLE_NAME);
            }
        }
    }


    /**
     * 取消面试，同步取消对应的面试反馈记录的面试状态为已取消
     *
     * @param msId
     */
    public static void cancelEvaluate(int msId) {
        RecordSet rs = new RecordSet();
        rs.executeUpdate("update " + TABLE_NAME + " set zt = 4 where msid=" + msId);
    }


    /**
     * 插入面试评价数据
     *
     * @param mainDataMap
     * @param msId
     * @param user
     * @param tableName
     * @param formModeId
     * @param msgId
     */
    private static void insertData(Map<String, Object> mainDataMap, int msId, User user, String tableName, int formModeId, String msgId) {
        RecruitDataMap<Object> dataMap = new RecruitDataMap<>();
        String uuid = UUID.randomUUID().toString();
        dataMap.put("modeuuid", uuid);
        dataMap.put("formmodeid", formModeId);
        RecruitRecordSet.buildModeInsertFields(dataMap, user.getUID());
        // 面试状态（设置为待面试）
        dataMap.put("zt", "0");
        // 批次ID
        dataMap.put("pcid", mainDataMap.get("pcid"));
        // 面试ID
        dataMap.put("msid", msId);
        // 应聘者
        dataMap.put("ypz", mainDataMap.get("ypz"));
        // 应聘职位
        dataMap.put("ypzw", mainDataMap.get("ypzw"));
        // 面试方式
        dataMap.put("msfs", mainDataMap.get("msfs"));
        // 面试环节
        dataMap.put("mshj", mainDataMap.get("mshj"));
        // 面试轮次
        dataMap.put("msc", mainDataMap.get("msc"));
        // 面试日期
        dataMap.put("msrq", mainDataMap.get("msrq"));
        // 面试地址
        dataMap.put("msdd", mainDataMap.get("msdd"));
        // 面试官
        dataMap.put("msg", msgId);

        // 创建数据
        RecruitRecordSet.insertData(dataMap, tableName);
        // 刷新权限
        RecruitRecordSet.refreshRight(uuid, tableName, formModeId, user.getUID());
    }

}
