package com.engine.htxc.commonutil.workflow;

import com.engine.htxc.commonutil.db.DbTools;
import com.engine.htxc.commonutil.log.CustomizeLog;
import com.engine.integration.util.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.htsc.GlobatTools.JAVATools;
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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Consumer;

/**
 * <p>创建流程相关的方式</p>
 * @author K1810015
 * @version 1.0.0
 * @ClassName WorkflowCreate.java
 * @createTime 2022年07月11日 15:07:00
 */
public class WorkflowCreate extends BaseBean {

    public WorkflowCreate() {
        super();
    }

    private final DbTools dbtools=new DbTools();

    public CreateWkType createWkType;

    public String requestid = "";

    private int maxlength=4000;

    /**
     * <p>提供一个创建WorkflowCreate的静态变量</p>
     * @title createWkType
     * @author K1810015
     * @updateTime 2022/8/17 20:00
     */
    public static WorkflowCreate createWorkflow(Consumer<CreateWkType> consumer) {
        WorkflowCreate workflowCreate=new WorkflowCreate();
        workflowCreate.createWkType=new CreateWkType();
        consumer.accept(workflowCreate.createWkType);
        return  workflowCreate;
    }

    /**
     * <p>创建一个主流程</p>
     * @title workflowCreateByRequestMap
     * @author K1810015
     * @updateTime 2022/8/17 20:06
     */
    public WorkflowCreate workflowCreateByRequestMap()
    {
        if(createWkType==null ){
            requestid= "-1";
            return this;
        }
        RequestInfo requestInfo = new RequestInfo();
        //创建人
        requestInfo.setCreatorid(createWkType.creatorId);
        //工作流Id
        requestInfo.setWorkflowid(createWkType.workflowId);
        //请求标题
        requestInfo.setDescription(createWkType.requestName);
        //是否提交到下一节点
        if(StringUtils.isBlank(createWkType.isNextFlow))
        {
            requestInfo.setIsNextFlow("0");
        }else{
            requestInfo.setIsNextFlow(createWkType.isNextFlow);
        }
        //主表字段
        MainTableInfo mainTableInfo = new MainTableInfo();
        Property[] propertyArray = new Property[createWkType.requestMainMap.size()];
        int p = 0;
        for (Entry<String, String> entry : createWkType.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 {
             requestid = service.createRequest(requestInfo);
            StringBuilder sbf = new StringBuilder("\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(createWkType.workflowId));
            sbf.append("\r\n-----请求:"+rcif.getRequestname(requestid));
            sbf.append("\r\n-----请求:"+requestid);
            sbf.append("\r\n-----创建工作流记录日志结束");
            CustomizeLog.printLog(sbf.toString());
        } catch (Exception e) {
            CustomizeLog.printLog("error:"+createWkType.toString());
        }
        return this;
    }

    /**
     * <p>将创建的流程插入明细表</p>
     * @title workflowCreatelist
     * @author K1810015
     * @param detailNo 明细表行号
     * @param detailist 明细表内容(List)
     * @updateTime 2022/8/17 20:08
     */
    public WorkflowCreate workflowCreatelist(String detailNo, List< Map<String,String>> detailist)
    {
        if("".equals(detailNo)||detailist.isEmpty()){
            return this;
        }
       String detailname= JAVATools.getdetailtable(requestid,detailNo);
        String mainid= JAVATools.getMaintableid(requestid);
        for ( Map<String,String> detailmap:detailist) {
            excetesqlvoid(detailmap,detailname,mainid);
        }
        return this;
    }

    /**
     * <p>插入明细表</p>
     * @title excetesqlvoid
     * @author K1810015
     * @updateTime 2022/8/17 20:46
     */
    private void excetesqlvoid(  Map<String,String> mapvalue,String table,String mainid) {
        mapvalue.put("mainid",mainid);
        CustomizeLog.printLog("table："+table+"mapvalue:"+mapvalue.toString());
        dbtools.insert(table,mapvalue);
     }

    /**
     *<p>最大长度校验</p>
     * @title lengthmax
     * @author K1810015
     * @param value 内容
     * @updateTime 2022/8/17 19:41
     */
    public String lengthmax(String value){
        if(StringUtils.isBlank(value))
        {
            return "";
        }
        if(value.length()>=maxlength){
            value=value.substring(0,maxlength-1);
        }
        return  value;
    }







}
