package weaver.formmode.recruit.modeexpand.process;

import cn.hutool.core.convert.Convert;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.formmode.recruit.modeexpand.util.RecruitModeUtil;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.Util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @author:dxfeng
 * @createTime: 2023/10/18
 * @version: 1.0
 */
public class StageDataInsert {
    /**
     * 生成关联的招聘阶段数据
     *
     * @param creator    创建者ID
     * @param billId     当前数据ID
     * @param processIds 路程表单所选阶段ID
     */
    public static void relatedStageData(int creator, String billId, String relateField, String... processIds) {
        String dateTime = DateUtil.getFullDate();
        String[] dateSplit = dateTime.split(" ");

        List<Object> paramsList = new ArrayList<>();
        // modedatacreater
        paramsList.add(creator);
        // modedatacreatedate
        paramsList.add(dateSplit[0]);
        // modedatacreatetime
        paramsList.add(dateSplit[1]);
        // modedatamodifier
        //paramsList.add(null);
        // modedatamodifydatetime
        //paramsList.add(null);
        // modedatacreatertype
        paramsList.add("0");

        String processIdsStr = StringUtils.join(Arrays.asList(processIds), ",");
        String insertSql = "insert into uf_jcl_zpjdsz (modeuuid, modedatacreater, modedatacreatedate, modedatacreatetime, modedatacreatertype, formmodeid, jdmc, jdms, jdlx, hj, sfqy, zssx, zpjd, " + relateField + ") values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
        if (StringUtils.isNotBlank(processIdsStr)) {
            insertMainTableData(creator, billId, processIdsStr, insertSql, paramsList);
            // 更新流程表单的阶段ID
            updateFlowStageId(Integer.parseInt(billId));

        }
    }

    /**
     * 更新流程表单的阶段ID
     *
     * @param flowId
     */
    public static void updateFlowStageId(Integer flowId) {
        RecordSet rs = new RecordSet();
        Integer ksjd = null;
        String gcjd = null;
        Integer jsjd = null;
        rs.executeQuery("select id from uf_jcl_zpjdsz where hj = 0 and zplc = ?", flowId);
        if (rs.next()) {
            ksjd = Convert.toInt(rs.getString("id"), null);
        }
        rs.executeQuery("select id from uf_jcl_zpjdsz where hj = 1 and zplc = ? order by hj,zssx", flowId);
        List<Integer> gcjdList = new ArrayList<>();
        while (rs.next()) {
            Integer gcjdId = Convert.toInt(rs.getString("id"), null);
            if (null != gcjdId) {
                gcjdList.add(gcjdId);
            }
        }
        if (CollectionUtils.isNotEmpty(gcjdList)) {
            gcjd = StringUtils.join(gcjdList, ",");
        }
        rs.executeQuery("select id from uf_jcl_zpjdsz where hj = 2 and zplc = ?", flowId);
        if (rs.next()) {
            jsjd = Convert.toInt(rs.getString("id"), null);
        }
        // 插入之后，更新流程表的阶段浏览按钮ID
        rs.executeUpdate("update uf_jcl_zplc set ksjd = ?, gcjd = ?, jsjd = ? where id = ?", ksjd, gcjd, jsjd, flowId);
    }

    /**
     * 删除流程表单的阶段ID
     *
     * @param flowId
     * @param stageId
     * @param hjId
     */
    public static void deleteFlowStageId(Integer flowId, Integer stageId, Integer hjId) {
        RecordSet rs = new RecordSet();
        if (hjId == 0) {
            rs.executeUpdate("update uf_jcl_zplc set ksjd = null where id = ?", flowId);
        } else if (hjId == 2) {
            rs.executeUpdate("update uf_jcl_zplc set jsjd = null where id = ?", flowId);
        } else if (hjId == 1) {
            String gcjd = null;
            rs.executeQuery("select id from uf_jcl_zpjdsz where hj = 1 and zplc = ? order by hj,zssx", flowId);
            List<Integer> gcjdList = new ArrayList<>();
            while (rs.next()) {
                Integer gcjdId = Convert.toInt(rs.getString("id"), null);
                if (null != gcjdId && !gcjdId.equals(stageId)) {
                    gcjdList.add(gcjdId);
                }
            }

            if (CollectionUtils.isNotEmpty(gcjdList)) {
                gcjd = StringUtils.join(gcjdList, ",");
            }

            rs.executeUpdate("update uf_jcl_zplc set gcjd = ? where id = ?", gcjd, flowId);
        }
    }

    /**
     * 插入主表数据
     *
     * @param billId        招聘流程表单ID
     * @param processIdsStr 招聘阶段ID
     * @param insertSql     插入SQL语句
     * @param paramsList    待插入参数集合
     */
    private static void insertMainTableData(int userId, String billId, String processIdsStr, String insertSql, List<Object> paramsList) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select * from uf_jcl_zpjdsz where id in (" + processIdsStr + ") order by hj,zssx");
        while (rs.next()) {
            String uuid = UUID.randomUUID().toString();
            ArrayList<Object> insertList = new ArrayList<>(paramsList);
            insertList.add(0, uuid);
            String formModeId = rs.getString("formmodeid");
            insertList.add(formModeId);
            insertList.add(RecruitModeUtil.parseBlankToNull(rs.getString("jdmc")));
            insertList.add(RecruitModeUtil.parseBlankToNull(rs.getString("jdms")));
            insertList.add(RecruitModeUtil.parseBlankToNull(rs.getString("jdlx")));
            insertList.add(RecruitModeUtil.parseBlankToNull(rs.getString("hj")));
            insertList.add(RecruitModeUtil.parseBlankToNull(rs.getString("sfqy")));
            insertList.add(RecruitModeUtil.parseBlankToNull(rs.getString("zssx")));
            // zpjd
            insertList.add(RecruitModeUtil.parseBlankToNull(rs.getString("id")));
            // zplc
            insertList.add(RecruitModeUtil.parseBlankToNull(billId));
            // 插入主表数据
            rs.executeUpdate(insertSql, insertList);
            refreshRight(userId, uuid, formModeId, rs.getString("id"));
        }
    }

    /**
     * 权限重构，插入明细表数据
     *
     * @param uuid       UUID
     * @param formModeId formModeId
     */
    private static void refreshRight(int userId, String uuid, String formModeId, String processId) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from uf_jcl_zpjdsz where modeuuid='" + uuid + "'");
        if (rs.next()) {
            //建模数据的id
            String bid = rs.getString("id");
            ModeRightInfo modeRightInfo = new ModeRightInfo();
            modeRightInfo.setNewRight(true);
            //新建的时候添加共享
            modeRightInfo.editModeDataShare(userId, Util.getIntValue(formModeId), Util.getIntValue(bid));

            // 插入明细表数据
            rs.executeQuery("select * from uf_jcl_zpjdsz_dt1 where mainid = ? ", processId);
            List<List<Object>> insertList = new ArrayList<>();
            while (rs.next()) {
                List<Object> paramList = new ArrayList<>();
                // mainid
                paramList.add(bid);
                paramList.add(RecruitModeUtil.parseBlankToNull(rs.getString("czan")));
                paramList.add(RecruitModeUtil.parseBlankToNull(rs.getString("zdyxsmc")));
                paramList.add(RecruitModeUtil.parseBlankToNull(rs.getString("sfqy")));
                paramList.add(RecruitModeUtil.parseBlankToNull(rs.getString("tzymbt")));
                paramList.add(RecruitModeUtil.parseBlankToNull(rs.getString("tzymdz")));
                paramList.add(RecruitModeUtil.parseBlankToNull(rs.getString("zssx")));
                insertList.add(paramList);
            }
            for (List<Object> list : insertList) {
                rs.executeUpdate("insert into uf_jcl_zpjdsz_dt1 (mainid, czan, zdyxsmc, sfqy, tzymbt, tzymdz, zssx) values (?, ?, ?, ?, ?, ?, ?)", list);
            }

            insertList = new ArrayList<>();
            rs.executeQuery("select * from uf_jcl_zpjdsz_dt2 where mainid = ? ", processId);
            while (rs.next()) {
                List<Object> paramList = new ArrayList<>();
                // mainid
                paramList.add(bid);
                paramList.add(RecruitModeUtil.parseBlankToNull(rs.getString("ymbt")));
                paramList.add(RecruitModeUtil.parseBlankToNull(rs.getString("ymdz")));
                paramList.add(RecruitModeUtil.parseBlankToNull(rs.getString("zssx")));
                insertList.add(paramList);
            }
            for (List<Object> list : insertList) {
                rs.executeUpdate("insert into uf_jcl_zpjdsz_dt2 (mainid, ymbt, ymdz, zssx) values (?, ?, ?, ?)", list);
            }
        }
    }
}
