package com.engine.recruit.conn;

import com.engine.recruit.exception.CustomizeRunTimeException;
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.List;
import java.util.Map;

/**
 * @author:dxfeng
 * @createTime: 2023/10/19
 * @version: 1.0
 */
public class RecruitRecordSet {

    /**
     * 获取单个记录映射
     *
     * @param rs RecordSet
     * @return
     */
    public static RecruitDataMap<Object> getSingleRecordMap(RecordSet rs) {
        RecruitDataMap<Object> dataMap = new RecruitDataMap<>();
        if (rs.next()) {
            String[] columnNames = rs.getColumnName();
            for (String columnName : columnNames) {
                dataMap.put(columnName, RecruitModeUtil.parseBlankToNull(rs.getString(columnName)));
            }
        }
        return dataMap;
    }

    public static List<Map<String, Object>> getRecordMapList(RecordSet rs) {
        List<Map<String, Object>> list = new ArrayList<>();
        while (rs.next()) {
            String[] columnNames = rs.getColumnName();
            Map<String, Object> dataMap = new RecruitDataMap<>();
            for (String columnName : columnNames) {
                dataMap.put(columnName.toLowerCase(), RecruitModeUtil.parseBlankToNull(rs.getString(columnName)));
            }
            list.add(dataMap);
        }
        return list;
    }

    /**
     * 插入数据
     *
     * @param dataMap   数据集合
     * @param tableName 表名
     */
    public static void insertData(Map<String, Object> dataMap, String tableName) {
        List<String> fieldList = new ArrayList<>();
        List<String> dataList = new ArrayList<>();
        List<String> paramList = new ArrayList<>();

        dataMap.forEach((key, value) -> {
            if (null != value) {
                String valueStr = String.valueOf(value);
                if(StringUtils.isNotBlank(valueStr)) {
                    fieldList.add(key);
                    dataList.add(valueStr);
                    paramList.add("?");
                }
            }
        });
        String insertSql = " insert into " + tableName + "(" + StringUtils.join(fieldList, ",") + ") values (" + StringUtils.join(paramList, ",") + ")";
        RecordSet rs = new RecordSet();
        rs.executeUpdate(insertSql, dataList);
        if (StringUtils.isNotBlank(rs.getExceptionMsg())) {
            throw new CustomizeRunTimeException(rs.getExceptionMsg());
        }
    }

    /**
     * 根据ID更新数据
     *
     * @param dataMap
     * @param tableName
     */
    public static void updateDataById(Map<String, Object> dataMap, String tableName) {
        List<String> fieldList = new ArrayList<>();
        List<Object> dataList = new ArrayList<>();
        String id = Util.null2String(dataMap.get("id"));
        dataMap.remove("id");

        dataMap.forEach((key, value) -> {
            fieldList.add(key + " = ? ");
            dataList.add(value);
        });
        dataList.add(id);
        String updateSql = "update " + tableName + " set " + StringUtils.join(fieldList, ",") + " where id = ? ";
        RecordSet rs = new RecordSet();
        rs.executeUpdate(updateSql, dataList);
        if (StringUtils.isNotBlank(rs.getExceptionMsg())) {
            throw new CustomizeRunTimeException(rs.getExceptionMsg());
        }
    }

    /**
     * 更新数据
     *
     * @param dataMap
     * @param tableName
     * @param whereSql
     */
    public static void updateData(Map<String, Object> dataMap, String tableName, String whereSql) {
        List<String> fieldList = new ArrayList<>();
        List<Object> dataList = new ArrayList<>();
        dataMap.forEach((key, value) -> {
            fieldList.add(key + " = ? ");
            dataList.add(value);
        });
        String updateSql = "update " + tableName + " set " + StringUtils.join(fieldList, ",") + whereSql;
        RecordSet rs = new RecordSet();
        rs.executeUpdate(updateSql, dataList);
        if (StringUtils.isNotBlank(rs.getExceptionMsg())) {
            throw new CustomizeRunTimeException(rs.getExceptionMsg());
        }
    }

    /**
     * 删除数据
     *
     * @param deleteSql
     */
    public static void deleteData(String deleteSql) {
        RecordSet rs = new RecordSet();
        rs.executeUpdate(deleteSql);
        if (StringUtils.isNotBlank(rs.getExceptionMsg())) {
            throw new CustomizeRunTimeException(rs.getExceptionMsg());
        }
    }


    /**
     * 构建建模表基本数据
     *
     * @param mainDataMap 参数集合
     */
    public static void buildModeInsertFields(Map<String, Object> mainDataMap, int userId) {
        String dateTime = DateUtil.getFullDate();
        String[] dateSplit = dateTime.split(" ");
        mainDataMap.put("modedatacreater", userId);
        mainDataMap.put("modedatacreatedate", dateSplit[0]);
        mainDataMap.put("modedatacreatetime", dateSplit[1]);
        mainDataMap.put("modedatacreatertype", "0");
    }

    /**
     * 构建建模表基本数据
     *
     * @param mainDataMap 参数集合
     */
    public static void buildModeUpdateFields(Map<String, Object> mainDataMap, int userId) {
        String dateTime = DateUtil.getFullDate();
        mainDataMap.put("modedatamodifier", userId);
        mainDataMap.put("modedatamodifydatetime", dateTime);
    }

    /**
     * 建模表数据权限重构
     *
     * @param uuid
     * @param modeTable
     * @param formModeId
     */
    public static int refreshRight(String uuid, String modeTable, int formModeId, int creator) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select id from " + modeTable + " where modeuuid='" + uuid + "'");
        if (rs.next()) {
            //建模数据的id
            int bid = Util.getIntValue(rs.getString("id"));
            ModeRightInfo modeRightInfo = new ModeRightInfo();
            modeRightInfo.setNewRight(true);
            //新建的时候添加共享
            modeRightInfo.editModeDataShare(creator, formModeId, bid);
            return bid;
        }
        return -1;
    }

}
