package weaver.interfaces.htsc.itcglc.util;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.RequestService;
import weaver.workflow.request.RequestComInfo;
import weaver.workflow.workflow.WorkflowComInfo;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class WorkflowUtil extends BaseBean {

    public String WorkflowCreateByRequestMap(String creatorId ,String workflowId ,String requestName ,String IsNextFlow,Map<String,String> requestMainMap)
    {
        writeLog("-----创建流程传递的参数个数--------");
        writeLog("creatorId:"+creatorId);
        writeLog("workflowId:"+workflowId);
        writeLog("requestName:"+requestName);
        writeLog("IsNextFlow:"+IsNextFlow);

        String requestid = "";
        RequestInfo requestInfo = new RequestInfo();
        requestInfo.setCreatorid(creatorId);//创建人Id
        requestInfo.setWorkflowid(workflowId);//工作流Id
        requestInfo.setDescription(requestName);//请求标题
        if(!"".equals(IsNextFlow))
        {
            requestInfo.setIsNextFlow(IsNextFlow);
        }

        //主表字段
        MainTableInfo mainTableInfo = new MainTableInfo();
        Property[] propertyArray = new Property[requestMainMap.size()];
        int p = 0;
        for (Entry<String, String> entry : requestMainMap.entrySet())
        {
            propertyArray[p] = new Property();
            propertyArray[p].setName(Util.null2String(entry.getKey()));
            propertyArray[p].setValue(Util.null2String(entry.getValue()));
            writeLog("\r\n---p="+p+"---Key="+entry.getKey()+"---Value="+entry.getValue());
            p++;
        }
        writeLog("-----创建流程传递的参数个数p="+p);
        mainTableInfo.setProperty(propertyArray);
        requestInfo.setMainTableInfo(mainTableInfo);

        RequestService service = new RequestService();
        try {
            //流程
            try {
                requestid = service.createRequest(requestInfo);//创建请求id
            } catch (Exception e) {
                writeLog("======="+e.getMessage());
            }
            //String userId = requestInfo.getLastoperator();//请求最后的操作者
            //记录日志
            StringBuffer sbf = new StringBuffer("\r\n-----xwd创建工作流记录日志开始");
            WorkflowComInfo wfcif =new WorkflowComInfo();

            RequestComInfo rcif = new RequestComInfo();
            ResourceComInfo rscif = new ResourceComInfo();
            sbf.append("\r\n-----姓名:"+rscif.getLastname(rcif.getRequestCreater(requestid)));
            sbf.append("\r\n-----时间:"+rcif.getRequestCreateTime(requestid));
            sbf.append("\r\n-----创建流程:"+wfcif.getWorkflowname(workflowId));
            sbf.append("\r\n-----请求:"+rcif.getRequestname(requestid));
            sbf.append("\r\n-----请求:"+requestid);
            sbf.append("\r\n-----创建工作流记录日志结束");
            writeLog(sbf.toString());
            System.out.println(""+sbf.toString());
        } catch (Exception e) {
            writeLog("错误：" + e);
            System.out.println("错误：" + e);
        }
        return requestid;
    }


    /***
     *
     * @param requestid
     * @param detailList
     * @return
     */
    public String insertTableDt5(String requestid,String workflowid, List<Map<String,String>> detailList)
    {
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String message = "0" ;
        if(!"".equals(requestid) && requestid !=null)
        {
            String formid = "";
            String sql = " select abs(formid) as formid from workflow_base where id="+workflowid ;
            rs.execute(sql);
            if(rs.next()){
                formid = Util.null2String(rs.getString("formid"));
            }
            String tablename = "formtable_main_"+formid;
            bb.writeLog("tablename:"+tablename);
            String mainid = "" ;
            sql = " select id from "+tablename+" where requestid = "+requestid;
            writeLog("sql："+sql);
            rs.execute(sql);
            if(rs.next())
            {
                mainid = Util.null2String(rs.getString("id"));
            }
            bb.writeLog("mainid:"+mainid);
            if(!"".equals(mainid))
            {
                //插入明细表
                String tablename_dt1 = tablename+"_dt5";		//明细表名
                for(Map<String,String> detailMap : detailList)
                {
                    String dttablename = "" ;
                    String dttablenvalue = "" ;
                    for (Map.Entry<String, String> entry : detailMap.entrySet())
                    {
                        String fieldname = Util.null2String(entry.getKey());
                        String fieldvalue = Util.null2String(entry.getValue());
                        dttablename += dttablename==""? fieldname :","+fieldname ;
                        dttablenvalue += dttablenvalue=="" ? "'"+fieldvalue+"'" :",'"+fieldvalue+"'" ;
                    }
                    writeLog("dttablename:"+dttablename);
                    writeLog("dttablenvalue:"+dttablenvalue);
                    if(dttablename !="" && dttablenvalue !="")
                    {
                        sql = "insert into "+tablename_dt1+" (mainid,"+dttablename+") values ("+mainid+","+dttablenvalue+")";
                        writeLog(sql);
                        boolean boo = rs.execute(sql);
                        if(!boo)
                        {
                            message = "-1" ;
                        }
                    }else{
                        message = "-2" ;
                    }
                }
            }
        }else{
            message = "-3" ;
        }
        return message;
    }
}
