package weaver.interfaces.htsc.util;

import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.TimeUtil;
import weaver.general.Util;

import java.util.*;

/**
 * 建模工具类
 */
public class ModelUtil {


    /**
     * 根据建模ID查询表单名称
     * @param modeid 建模id
     * @return 建模表名
     */
    public static String getTableNameByModeID(int modeid){
        String tableName = "";
        if(modeid>0){
            RecordSet rs = new RecordSet();
            rs.executeQuery(" SELECT TABLENAME FROM MODEINFO T1,WORKFLOW_BILL T2 " +
                    "WHERE T1.FORMID=T2.ID AND T1.ID=? ",modeid);
            if(rs.next()){
                tableName = Util.null2String(rs.getString("TABLENAME"));
            }
        }
        return tableName;
    }


    /**
     * 根据建模名称获取建模id,建模权限同步的使用
     * @param modeName 建模名称
     * @return int 建模id
     */
    public static int getModeIdByName(String modeName){
        RecordSet rs = new RecordSet();
        String sql = " SELECT ID FROM MODEINFO WHERE MODENAME = ? ";
        rs.executeQuery(sql, modeName);
        if (rs.next()) {
            return rs.getInt("id");
        }
        return -1;
    }


    /**
     * 创建建模主表中的一行数据，带有权限重构
     * @param modeId     建模id
     * @param creater    数据创建者
     * @param data       插入数据集
     * @return 建模主表数据行id，方法执行失败返回-1
     */
    public static int addModeData(final int modeId, final int creater, final Map<String, Object> data) {
        int id = -1;
        String modeTableName = getTableNameByModeID(modeId);
        SqlUtil sqlUtil = new SqlUtil(modeTableName);
        String uuid = UUID.randomUUID().toString();
        data.put("modeuuid",uuid);
        data.put("modedatacreater",creater);
        data.put("modedatacreatedate",TimeUtil.getCurrentDateString());
        data.put("modedatacreatetime",TimeUtil.getOnlyCurrentTimeString());
        data.put("formmodeid",modeId);

        for (Map.Entry<String, Object> entry : data.entrySet()) {
            sqlUtil.addInsertKV(entry.getKey(), entry.getValue());
        }
        RecordSet rs = new RecordSet();
        String insertSql = sqlUtil.getInsertSql(true);
        rs.writeLog("addModeData insertSql=====>"+insertSql);
        rs.writeLog("addModeData insertSql values=====>"+sqlUtil.getInsertColumnValueList());
        rs.executeQuery(insertSql,sqlUtil.getInsertColumnValueList().toArray());

        id = Util.getIntValue(SqlUtil.querySingleField("SELECT ID FROM " + modeTableName + " WHERE MODEUUID=?",uuid));
        if(id>0){
            // 权限重构
            ModeRightInfo right = new ModeRightInfo();
            right.editModeDataShare(creater, modeId, id);
        }
        return id;
    }


    public static void main(String[] args) {
        Map<String,Object> data = new HashMap<String,Object>();
        data.put("szbm",728);
        data.put("ngr",28302);
        data.put("bz","测试数据");
        data.put("je","100.00");
        data.put("fklx",3);
        addModeData(801,1,data);
    }


}
