package weaver.interfaces.htsc.aqrwfblc;

import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.*;

import java.text.SimpleDateFormat;
import java.util.*;

public class DtOneToMore extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {

        String back = "";
        RecordSet rs = new RecordSet();
//        获取前端主表数据
        Map map = mainData(requestInfo);
        String rwdw = Util.null2String(map.get("rwdw"));
//        权限重构所需字段
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss");
        String currentdate = formatter1.format(new Date());
        String currenttime = formatter2.format(new Date());
        String modedatacreater = "1";
        String modedatacreatertype = "0";
        String formmodeid = "";
        String sql = " select k.id from modeinfo k  \n" +
                " inner join workflow_bill l on formid = l.id\n" +
                " where l.tablename = 'uf_aqscglbm' ";
        rs.execute(sql);
        if (rs.next()) {
            formmodeid = Util.null2String(rs.getString("id"));
        }
//        主表字段
        String rwksrq = Util.null2String(map.get("rwksrq"));
        String rwjsrq = Util.null2String(map.get("rwjsrq"));
        String rwmc = Util.null2String(map.get("rwmc"));
        String wcsj = Util.null2String(map.get("wcsj"));
        String clrq = Util.null2String(map.get("clrq"));
        String yq = Util.null2String(map.get("yq"));
        String rwlx = Util.null2String(map.get("rwlx"));

        try {
            writeLog("共拆分了"+rwdw.split(",").length+"条数据");
            for (String everyRwdw : rwdw.split(",")) {
                Map<String, Object> datas = new HashMap<>();
                //权限重构
                datas.put("formmodeid", formmodeid);
                datas.put("modedatacreater", modedatacreater);
                datas.put("modedatacreatertype", modedatacreatertype);
                datas.put("modedatacreatedate", currentdate);
                datas.put("modedatacreatetime", currenttime);
                String modeuuid = UUID.randomUUID().toString();
                datas.put("modeuuid", modeuuid);
                //主表字段
                datas.put("rwdwbm", everyRwdw);
                datas.put("rwdw",rwdw);
                datas.put("rwkssj", rwksrq);
                datas.put("rwjssj", rwjsrq);
                datas.put("rwmc", rwmc);
                datas.put("wcsj", wcsj);
                datas.put("clrq", clrq);
                datas.put("yq", yq);
                datas.put("rwlx", rwlx);
                datas.put("rwlc",requestInfo.getRequestid());
                datas.put("sffk","0");
                ModeRightInfo mode = new ModeRightInfo();
                String tablename = "";
                String tablenvalue = "";
                for (Map.Entry<String, Object> entry : datas.entrySet()) {
                    String fieldname = Util.null2String(entry.getKey()).trim();
                    String fieldvalue = Util.null2String(entry.getValue()).replace("'", "''").trim();
                    tablename += tablename == "" ? fieldname : "," + fieldname;
                    tablenvalue += tablenvalue == "" ? "'" + fieldvalue + "'" : ",'" + fieldvalue + "'";
                }
                if (tablename != "" && tablenvalue != "") {
                    int count = 0;
                    sql = " insert into uf_aqscglbm(" + tablename + ") values (" + tablenvalue + ")";
                    writeLog("sql:" + sql);
                    boolean boo = rs.executeUpdate(sql);
                    if (boo) {
                        count++;
                    }
                    if (count > 0) {
                        String dataid = "";
                        sql = " select * from uf_aqscglbm where modeuuid='" + modeuuid + "' ";
                        rs.execute(sql);
                        if (rs.next()) {
                            dataid = Util.null2String(rs.getString("id"));
                        }
                        if (!"".equals(dataid)) {
                            mode.editModeDataShare(Integer.valueOf(modedatacreater), Integer.parseInt(formmodeid), Integer.parseInt(dataid));
                        }
                        back = SUCCESS;
                    } else {
                        requestInfo.getRequestManager().setMessageid("104");
                        requestInfo.getRequestManager().setMessagecontent("find error when Insertting.A data can't insert success");
                        back = FAILURE_AND_CONTINUE;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid("104");
            requestInfo.getRequestManager().setMessagecontent("find error when Insertting");
            back = FAILURE_AND_CONTINUE;
        }
        writeLog("此时的back是："+back);
        return back;
    }

    /*
     * 主表数据
     * */
    private static Map mainData(RequestInfo requestInfo) {
        Map mainTableDataMap = new HashMap();
        Property[] property = requestInfo.getMainTableInfo().getProperty();
        for (int i = 0; i < property.length; i++) {
            mainTableDataMap.put(property[i].getName().toLowerCase(), Util.null2String(property[i].getValue()));
        }
        return mainTableDataMap;
    }
}
