package com.engine.recruit.conn;

import com.engine.recruit.entity.record.ApplicantRecordPo;
import com.engine.recruit.entity.recruitflow.po.RecruitStepPo;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;

import java.util.*;

/**
 * @author:dxfeng
 * @createTime: 2023/09/27
 * @version: 1.0
 */
public class ApplicantCommonInfo {
    /**
     * 获取应聘者姓名
     *
     * @param applicantId 应聘者ID
     * @return 应聘者姓名
     */
    public static String getApplicantName(String applicantId) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select xm from uf_jcl_yppc where id = ?", applicantId);
        if (rs.next()) {
            return rs.getString("xm");
        }
        return "";
    }

    /**
     * 获取应聘职位名称
     *
     * @param positionId 职位ID
     * @return 职位名称
     */
    public static String getApplicantPosition(String positionId) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select zpzwmc from uf_jcl_zp_zpzw where id = ?", positionId);
        if (rs.next()) {
            return rs.getString("zpzwmc");
        }
        return "";
    }


    /**
     * 获取表单下拉框展示文本
     *
     * @param formId    表单ID
     * @param fieldName 字段明湖曾
     * @param value     下拉框值
     * @return
     */
    public static String getSelectName(String formId, String fieldName, String value) {
        String cancelReason = "";
        RecordSet rs = new RecordSet();
        rs.executeQuery("select selectname from workflow_selectitem where fieldid =( select id from workflow_billfield where billid = ? and fieldname = ? ) and selectvalue = ?", formId, fieldName, value);
        if (rs.next()) {
            cancelReason = rs.getString("selectname");
        }
        return cancelReason;
    }

    /**
     * 获取表单下拉框值
     *
     * @param formId     表单ID
     * @param fieldName  字段明湖曾
     * @param selectName 下拉框展示内容
     * @return
     */
    public static String getSelectValue(String formId, String fieldName, String selectName) {
        String selectValue = "";
        RecordSet rs = new RecordSet();
        rs.executeQuery("select selectvalue from workflow_selectitem where fieldid =( select id from workflow_billfield where billid = ? and fieldname = ? ) and selectname = ?", formId, fieldName, selectName);
        if (rs.next()) {
            selectValue = rs.getString("selectvalue");
        }
        return selectValue;
    }

    /**
     * 获取表单下拉框展示文本
     *
     * @param formId    表单ID
     * @param fieldName 字段明湖曾
     * @return
     */
    public static Map<String, String> getSelectMap(String formId, String fieldName) {
        Map<String, String> selectMap = new HashMap<>();
        RecordSet rs = new RecordSet();
        rs.executeQuery("select selectvalue,selectname from workflow_selectitem where fieldid =( select id from workflow_billfield where billid = ? and fieldname = ? ) ", formId, fieldName);
        while (rs.next()) {
            selectMap.put(rs.getString("selectvalue"), rs.getString("selectname"));
        }
        return selectMap;
    }


    /**
     * 获取招聘通用浏览按钮展示内容
     *
     * @param id 浏览按钮ID
     * @return 浏览按钮展示文本
     */
    public static String getRecruitCommonBrowserValue(String id) {
        if (StringUtils.isBlank(id)) {
            return "";
        }
        List<String> value = new ArrayList<>();
        RecordSet rs = new RecordSet();
        String[] split = id.split(",");
        for (String s : split) {
            rs.executeQuery("select xxnr from uf_sjzd where id = ?", s);
            if (rs.next()) {
                value.add(rs.getString("xxnr"));
            }
        }
        return StringUtils.join(value, ",");
    }

    /**
     * 根据建模表名,获取建模ID
     *
     * @param modeTable
     * @return
     */
    public static int getModeIdByTableName(String modeTable) {
        int formModeId = -1;
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from modeinfo where formid =( select id from workflow_bill where tablename = ? ) and isdelete = 0 order by id", modeTable);
        if (rs.next()) {
            formModeId = rs.getInt("id");
        }
        return formModeId;
    }

    /**
     * 获取指定模块下的布局ID
     *
     * @param modeId
     * @param layoutName
     * @return
     */
    public static int getLayoutId(int modeId, String layoutName) {
        int layoutId = -1;
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from modehtmllayout where modeid = ? and layoutname =?", modeId, layoutName);
        if (rs.next()) {
            layoutId = rs.getInt("id");
        }
        return layoutId;
    }

    /**
     * 获取字段ID
     *
     * @param billId
     * @param fieldName
     * @return
     */
    public static int getFieldId(int billId, String fieldName) {
        int fieldId = -1;
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from workflow_billfield where billid =? and fieldname = ?", billId, fieldName);
        if (rs.next()) {
            fieldId = rs.getInt("id");
        }
        return fieldId;
    }

    /**
     * 获取查询ID
     *
     * @param modeId
     * @param customName
     * @return
     */
    public static int getCustomId(int modeId, String customName) {
        int customId = -1;
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from mode_customsearch where modeid = ? and customname = ? ", modeId, customName);
        if (rs.next()) {
            customId = rs.getInt("id");
        }
        return customId;
    }

    /**
     * 根据表名，获取表单ID
     *
     * @param modeTable
     * @return
     */
    public static int getFormIdByTableName(String modeTable) {
        int formId = -1;
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from workflow_bill where tablename = ? ", modeTable);
        if (rs.next()) {
            formId = rs.getInt("id");
        }
        return formId;
    }


    public static int createOperateRecord(ApplicantRecordPo recordPo) {
        try {
            int operateId = recordPo.getUser().getUID();
            String operateTableName = "uf_jcl_ypgc";
            RecruitDataMap<Object> dataMap = new RecruitDataMap<>();
            String uuid = UUID.randomUUID().toString();
            dataMap.put("modeuuid", uuid);
            int formModeId = getModeIdByTableName(operateTableName);
            dataMap.put("formmodeid", formModeId);
            RecruitRecordSet.buildModeInsertFields(dataMap, operateId);
            dataMap.put("czyw", recordPo.getRecordOperateType().getShowName());
            dataMap.put("czsj", recordPo.getOperateTime());
            dataMap.put("jlnr", recordPo.getContent());
            dataMap.put("xqnr", recordPo.getLinkUrl());
            dataMap.put("ypz", recordPo.getPcId());
            RecruitRecordSet.insertData(dataMap, operateTableName);
            return RecruitRecordSet.refreshRight(uuid, operateTableName, formModeId, operateId);
        } catch (Exception e) {
            new BaseBean().writeLog("应聘过程记录数据插入失败", e);
        }
        return -1;
    }

    public static RecruitStepPo getCurrentStep(String billId) {
        RecruitStepPo stepPo = new RecruitStepPo();
        RecordSet rs = new RecordSet();
        // 招聘阶段ID
        String stageId = null;
        // 招聘流程ID
        int recruitFlowId = -1;
        rs.executeQuery("select zplc,zpjd from uf_jcl_yppc where id = ?", billId);
        if (rs.next()) {
            recruitFlowId = rs.getInt("zplc");
            stageId = rs.getString("zpjd");
        }
        if (-1 == recruitFlowId) {
            return stepPo;
        }
        if (StringUtils.isNotBlank(stageId)) {
            rs.executeQuery("select id,jdmc,hj from uf_jcl_zpjdsz where sfqy = 0 and id = ? ", stageId);
        } else {
            rs.executeQuery("select id,jdmc,hj from uf_jcl_zpjdsz where sfqy = 0 and zplc = ? and hj = 0", recruitFlowId);
        }
        if (rs.next()) {
            stepPo = RecruitStepPo.builder().key(rs.getInt("id")).description(rs.getString("jdmc")).type(rs.getString("hj")).build();
        }
        return stepPo;

    }


    /**
     * 获取流程开始阶段，阶段类型
     *
     * @param flowId 流程ID
     * @return
     */
    public static Map<String, String> getInitialStage(String flowId) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id,jdlx from uf_jcl_zpjdsz where sfqy = 0 and zplc = ? and hj = 0", flowId);
        if (rs.next()) {
            Map<String, String> map = new HashMap<>();
            map.put("zpjd", rs.getString("id"));
            map.put("jdlx", rs.getString("jdlx"));
            return map;
        }
        return null;
    }

    /**
     * 获取ImageField
     *
     * @param docId
     * @return
     */
    public static int getImageFieldByDocId(String docId) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select imagefileid from docimagefile where docid = ? ", docId);
        if (rs.next()) {
            return rs.getInt("imagefileid");
        }
        return -1;
    }

    /**
     * 根据customSearchCode获取查询列表的id
     *
     * @param customSearchCode customSearchCode
     * @return ID
     */
    public static String getCustomSearchId(String customSearchCode) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from mode_customsearch where customsearchcode = ? ", customSearchCode);
        if (rs.next()) {
            return rs.getString("id");
        }
        return "";
    }

    /**
     * 根据customSearchCode获取查询列表的地址
     *
     * @param customSearchCode customSearchCode
     * @param linkUrl          自定义的URL
     * @return 查询列表的地址
     */
    public static String getCustomSearchLink(String customSearchCode, String linkUrl) {
        if (StringUtils.isNotBlank(linkUrl)) {
            return linkUrl;
        }
        String customSearchId = getCustomSearchId(customSearchCode);
        if (StringUtils.isNotBlank(customSearchId)) {
            return "/spa/cube/index.html#/main/cube/search?customid=" + customSearchId;
        }
        return "";
    }
}
