package weaver.interfaces.htsc.workflow.odoc;

import com.alibaba.fastjson.JSONObject;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

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

public class ReceiptUnitAction extends BaseBean implements Action {

    @Override
    public String execute(RequestInfo requestInfo) {
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();
        String requestid = requestInfo.getRequestid();
        writeLog("----ReceiptUnitAction-------" + requestid);
        try {
            String tablename = requestInfo.getRequestManager().getBillTableName();
            String lw = "";
            String sql = "select lw from " + tablename + " where requestid='" + requestid + "'";
            writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                lw = Util.null2String(rs.getString("lw"));
            }

            sql = "select id,lwsl from uf_lwdw where jgdw='" + lw + "'";
            writeLog(sql);
            rs.executeQuery(sql);
            if(rs.next()){
                String id = Util.null2String(rs.getString("id"));
                int lwsl = Util.getIntValue(rs.getInt("lwsl"));

                lwsl++;

                String update_sql = "update uf_lwdw set lwsl = '"+lwsl+"' where id = '"+id+"'";
                writeLog(update_sql);
                rs.executeUpdate(update_sql);
            }else{
                createUnit(lw);
            }
        }catch (Exception e) {
            e.printStackTrace();
            writeLog("ReceiptUnitAction error:" + e);
            requestInfo.getRequestManager().setMessageid("222222");
            requestInfo.getRequestManager().setMessagecontent("流程提交异常,请联系管理员!");
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }

    private void createUnit(String jgdw){
        PropBean pb = new PropBean();
        RecordSet rs = new RecordSet();

        String uuid = UUID.randomUUID().toString();
        String modedatacreater = "1";
        String modedatacreatertype = "0";
        String modedatacreatedate = TimeUtil.getCurrentDateString();
        String modedatacreatetime = TimeUtil.getOnlyCurrentTimeString();

        String lwdwmodeid = pb.getPropName("UF_LWDW_CUBEUUID");
        JSONObject jsonObject_lwdw = pb.getModeInfoByUUID(lwdwmodeid);
        Integer formmodeid = Util.getIntValue(jsonObject_lwdw.getString("modeid"));

        Map<String,String> map = new HashMap<String,String>();
        map.put("formmodeid",formmodeid+"");
        map.put("modedatacreater",modedatacreater+"");
        map.put("modedatacreatertype",modedatacreatertype+"");
        map.put("modedatacreatedate",modedatacreatedate+"");
        map.put("modedatacreatetime",modedatacreatetime+"");
        map.put("modeuuid",uuid+"");
        map.put("jgdw",jgdw);
        map.put("lwsl","1");
        map.put("yxj","1");

        String dttablename = "";
        String dttablenvalue = "";
        for (Map.Entry<String, String> entry : map.entrySet())
        {
            String fieldname = Util.null2String(entry.getKey());
            String fieldvalue = Util.null2String(entry.getValue());
            dttablename += dttablename == "" ? fieldname : "," + fieldname;
            dttablenvalue += dttablenvalue == "" ? "'" + fieldvalue + "'" : ",'" + fieldvalue + "'";
        }

        String sql = "insert into uf_lwdw("+dttablename+") values("+dttablenvalue+")";
        writeLog("sql:"+sql);
        boolean bool = rs.executeUpdate(sql);
        if(bool){
            rs.executeQuery("select id from uf_lwdw where modeuuid='" + uuid + "'");
            if (rs.next()) {
                int id = Util.getIntValue(rs.getString("id"));
                writeLog("id:"+id);
                ModeRightInfo moderight = new ModeRightInfo();
                moderight.editModeDataShare(1, formmodeid, id);
            }
        }
    }
}
