package weaver.interfaces.htsc.cgrwd.action;

import com.alibaba.fastjson.JSON;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

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

public class InsertWorkFlowContentsAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        //采购单流程的requestid
        String requestid = Util.null2String(requestInfo.getRequestid());
        PropBean pr = new PropBean();
        //跳转用的url
        String url =pr.getPropName("OAHOST") + "spa/workflow/static4form/index.html?#/main/workflow/req?requestid="+requestid;
        RequestManager requestManager = requestInfo.getRequestManager();
        //获取主表名
        String tableName = requestManager.getBillTableName();
        //拼接获取明细表名
        String tableNameDt5 = tableName+ "_dt5";

        //采购任务单主表
        String projectMaster = "";
        String projectMasterPracticebadge = "";
        String projectName = "";
        String projectNumber = "";
        String flc = "";
        //IT采购申请主表
        String budgetTotal = "";
        String itProject_workFlowNumber = "";
        String itProject_workFlowNum = "";
        String itProject_workFlowTitle = "";
        String itProjectUrl = "";

        String purchase_workFlowNumber = "";
        String purchase_workFlowNum = "";
        String purchase_workFlowTitle = "";
        String purchaseUrl = "";

        RecordSet rs = new RecordSet();
        try {
            //先从采购任务单主表里查出所需要的数据
            String sqlMain = "select t1.loginid as loginid,t1.lastname as lastname,t2.xmmczw as xmmczw,t2.xmbh as xmbh,t2.flc as flc from hrmresource t1, " + tableName + " t2 where t1.id=t2.ngr and t2.requestid= " + requestid;
            rs.execute(sqlMain);
            if(rs.next()){
                projectMaster=Util.null2String(rs.getString("lastname"));
                projectMasterPracticebadge=Util.null2String(rs.getString("loginid"));
                projectName=Util.null2String(rs.getString("xmmczw"));
                projectNumber=Util.null2String(rs.getString("xmbh"));
                flc=Util.null2String(rs.getString("flc"));
            }

            //通过flc查出IT采购流程的表名
            String sql = "select t1.tablename as tablename from workflow_bill t1,workflow_requestbase t2,workflow_base t3 where t2.workflowid = t3.id and t3.formid = t1.id and t2.requestid= " + flc;
            String cgTableName = "";
            rs.execute(sql);
            if(rs.next()){
                cgTableName=Util.null2String(rs.getString("tablename"));
            }
            //String cgTableNameDt5 = cgTableName+"_dt5";
            //通过IT采购申请requestid获取所需要的数据
            String sqlCG = "select t.hjxxysje,t.jkzd1,t.jkzd2,t.jkzd3,t.jkzd4,t.lcbh,t.lcbt from " + cgTableName + " t where t.requestid= " + flc;
            rs.execute(sqlCG);
            if(rs.next()){
                //it立项
                budgetTotal=Util.null2String(rs.getString("hjxxysje"));
                itProject_workFlowTitle=Util.null2String(rs.getString("jkzd1"));
                itProject_workFlowNum=Util.null2String(rs.getString("jkzd2"));
                itProject_workFlowNumber=Util.null2String(rs.getString("jkzd3"));
                itProjectUrl=Util.null2String(rs.getString("jkzd4"));
                //it采购
                purchase_workFlowTitle=Util.null2String(rs.getString("lcbt"));
                purchase_workFlowNum=Util.null2String(rs.getString("lcbh"));
                purchase_workFlowNumber=flc;
                purchaseUrl=url;
            }

            //查询采购任务单明细表dt5
            String sqlDetail = "select t3.lastname as lastname,t3.loginid as loginid,t2.departmentname as departmentname,t2.departmentcode as departmentcode,t4.selectname as cgfs,t5.selectname as cglx,t1.ysje as ysje,t1.cgnr as cgnr,t1.gys as gys,t1.cgzd as cgzd,t1.ejcgzd as ejcgzd,t1.bz as bz,t1.sl as sl,t1.appid as appid " +
                    "from " + tableNameDt5 + " t1, hrmdepartment t2, hrmresource t3, workflow_selectitem t4, workflow_selectitem t5, workflow_billfield t6, workflow_billfield t7, " + tableName + " t8 " +
                    "where to_char(t1.cgfzr)=t3.id and instr(','||t1.ysbm||',',','||t2.id||',')>0 and t1.mainid=t8.id and t6.detailtable= '" + tableNameDt5 + "' and t7.detailtable = '" + tableNameDt5 + "' and t6.fieldname='cgfs' and t6.id=t4.fieldid and t7.fieldname='cglx' and t5.fieldid=t7.id and t1.mainid=t8.id and t1.cglx=t5.selectvalue and t1.cgfs=t4.selectvalue " +
                    " and t8.requestid= " + requestid;

            rs.execute(sqlDetail);
            String deptNames = "";
            String deptCodes = "";
            String workFlowContents = "";
            String appid = "";
            while (rs.next()){

                Map<String,Object> map  = new HashMap<String,Object>();
                String deptName = Util.null2String(rs.getString("departmentname"));
                String deptCode = Util.null2String(rs.getString("departmentCODE"));
                if(appid.equals(rs.getString("appid"))){
                    deptNames += deptNames == "" ? deptName : ","+deptName ;
                    deptCodes += deptCodes == "" ? deptCode : ","+deptCode ;
                }else{
                    deptNames = "";
                    deptCodes = "";
                    deptNames += deptNames == "" ? deptName : ","+deptName ;
                    deptCodes += deptCodes == "" ? deptCode : ","+deptCode ;
                }
                appid = Util.null2String(rs.getString("appid"));
                map.put("budget",Util.null2String(rs.getString("ysje"))); //预算金额
                map.put("budgetTotal",budgetTotal); //总采购金额
                map.put("purchaseWay",""); //采购方式编号
                map.put("purchaseWayStr",Util.null2String(rs.getString("cgfs"))); //采购方式中文
                map.put("purchaseType",""); //采购类型编号
                map.put("purchaseTypeStr",Util.null2String(rs.getString("cglx"))); //采购类型中文
                map.put("projectMaster",projectMaster); //项目负责人
                map.put("projectMasterPracticebadge",projectMasterPracticebadge); //项目负责人工号
                map.put("projectName",projectName); //项目名称
                map.put("projectNumber",projectNumber); //项目编号
                map.put("budgetDepartmentName",deptNames); //预算部门名称
                map.put("budgetDepartmentCode",deptCodes); //预算部门编号
                map.put("purchaseContent",Util.null2String(rs.getString("cgnr"))); //采购内容
                map.put("supplier",Util.null2String(rs.getString("gys"))); //供应商
                map.put("purchaseRegulationsStr",Util.null2String(rs.getString("cgzd"))); //采购制度
                map.put("purchaseRegulations2",Util.null2String(rs.getString("ejcgzd"))); //二级采购制度
                map.put("purchaseRemark",Util.null2String(rs.getString("bz")));
                map.put("unit",Util.null2String(rs.getString("sl")));
                map.put("itProject_workFlowNumber",itProject_workFlowNumber);
                map.put("itProject_workFlowNum",itProject_workFlowNum);
                map.put("itProject_workFlowTitle",itProject_workFlowTitle);
                map.put("itProjectUrl",itProjectUrl);
                map.put("purchase_workFlowNumber",purchase_workFlowNumber);
                map.put("purchase_workFlowNum",purchase_workFlowNum);
                map.put("purchase_workFlowTitle",purchase_workFlowTitle);
                map.put("purchaseUrl",purchaseUrl);
                if(!"IT硬件".equals(Util.null2String(rs.getString("cglx")))){
                    map.put("caigoufuzeren",Util.null2String(rs.getString("lastname")));
                    map.put("caigoufuzerenid",Util.null2String(rs.getString("loginid")));
                }
                workFlowContents = JSON.toJSONString(map);//map转json字符串
                if(""!=workFlowContents && ""!=appid){
                    String sqlins = "update " + tableNameDt5 + " t set t.workflowcontents= '" + workFlowContents + "' where t.appid='" + appid + "'";
                    Boolean result = rs.execute(sqlins);
                    JAVATools.SetCmdwritelog("workflowcontents插入情况" + result);
                }else {
                    JAVATools.SetCmdwritelog("workflowcontents和appid不全都有数据");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
