package com.engine.htxc.contract.util;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.htsc.comInfo.PropBean;

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

public class RecordChangeVersion {
    private static BaseBean bb = new BaseBean();

    /**
     * 变更快照方法
     * @param billid 数据id
     * @param tableName 主表建模表表名
     * @throws Exception
     */
    public static void recordChangeVersion(int billid,String tableName)throws Exception{
        String querySql = "select * from "+tableName+" where id="+billid;
        Map<String,Object> dataMap = new HashMap<>();
        RecordSet rs = new RecordSet();
        rs.executeQuery(querySql);
        if (rs.next()){
            String[] columNames =  rs.getColumnName();
            for (String str : columNames){
                dataMap.put(str,rs.getString(str));
            }
        }
        PropBean pb = new PropBean();
        String counterpartySnapshotmodeid = pb.getPropName("contractCreationSnapshotmodeid");
        dataMap.put("FORMMODEID",counterpartySnapshotmodeid);
        dataMap.put("FID",billid);
        String uuid = UUID.randomUUID().toString();
        dataMap.put("MODEUUID",uuid);
        int dqbb = Integer.valueOf(dataMap.get("DQBB").toString());
        dqbb = dqbb+1;
        String names="";
        String values="";
        for (Map.Entry entry :dataMap.entrySet()){
            names = names + entry.getKey() +",";
            values = values +"'"+entry.getValue()+"'" +",";
        };
        //插入快照版本并修改主记录的当前版本
        names = names.substring(0,names.length()-1);
        values = values.substring(0,values.length()-1);
        String insertSql = "insert into "+tableName+" ("+names+") values ("+values+")";
        bb.writeLog("插入语句 insertSql:"+insertSql);
        rs.executeUpdate("update "+tableName+" set DQBB = ? where id=?", dqbb,billid);
        rs.executeUpdate(insertSql);

        //查询快照版本billid
        rs.executeQuery("select id from "+tableName+" where modeuuid=?",uuid);
        if (rs.next()){
            String id = rs.getString("id");
            //插入明细表1
            insertDetail(tableName+"_dt1",id,billid);
            //插入明细表2
            insertDetail(tableName+"_dt2",id,billid);
            //插入明细表3
            insertDetail(tableName+"_dt3",id,billid);
        }

    }
    public static void insertDetail(String tableName,String id,int billid){
        RecordSet rs = new RecordSet();
        //插入明细表
        String detail1Sql = "select * from "+tableName+" where mainid=?";
        //获取主记录明细表数据
        rs.executeQuery(detail1Sql,billid);
        while (rs.next()){
            Map<String,Object> detail1Map = new HashMap<>();
            String[] columNames =  rs.getColumnName();
            for (String str : columNames){
                detail1Map.put(str,rs.getString(str));
            }
            detail1Map.put("MAINID",id);
            String names="";
            String values="";
            for (Map.Entry entry :detail1Map.entrySet()){
                names = names + entry.getKey() +",";
                values = values +"'"+entry.getValue()+"'" +",";
            };
            names = names.substring(0,names.length()-1);
            values = values.substring(0,values.length()-1);
            //插入快照的明细表
            String insertSql = "insert into "+tableName+" ("+names+") values ("+values+")";
            bb.writeLog("插入明细表"+tableName+":"+insertSql);
            rs.executeUpdate(insertSql);
        }

    }
}
