package com.engine.htsc.payment.model;

import com.engine.htsc.payment.util.AssertUtil;
import com.engine.htsc.payment.util.CommonAPI;
import org.apache.commons.lang3.StringUtils;
import weaver.general.BaseBean;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 流程相关信息
 */
public class WfParams implements Serializable {

  private static final long serialVersionUID = 3311023766057703155L;

  public static final String REQUEST_ID = "requestid";
  public static final String REQUEST_NAME = "requestname";
  public static final String REQUEST_MARK = "requestmark";
  public static final String CREATE = "creater";

  /**
   * map keys:
   * requestid,requestname,requestmark,creater
   */
  private List<Map<String, String>> requestInfos;

  private String tableName;

  /**
   * 流程唯一英文简写
   */
  private String unnumber;

  private int workflowid;
  /**
   * 打印模板
   */
  private String modeid;

  /**
   * 附件
   */
  private List<Map<String, String>> appendices;

  public WfParams(Builder builder) {
    this.requestInfos = builder.requestInfos;
    this.modeid = builder.modeid;
    this.tableName = builder.tableName;
    this.unnumber = builder.unnumber;
    this.workflowid = builder.workflowid;
    this.appendices = builder.appendices;
  }

  public List<Map<String, String>> getRequestInfos() {
    return requestInfos;
  }

  public String getModeid() {
    return modeid;
  }

  public void setModeid(String modeid) {
    this.modeid = modeid;
  }

  public void setRequestInfos(List<Map<String, String>> requestInfos) {
    this.requestInfos = requestInfos;
  }

  public List<Map<String, String>> getAppendices() {
    return appendices;
  }

  public void setAppendices(List<Map<String, String>> appendices) {
    this.appendices = appendices;
  }

  public static class Builder extends BaseBean {
    private List<Map<String, String>> requestInfos;
    private String modeid;
    private String tableName;
    private String unnumber;
    private int workflowid;
    private List<Map<String, String>> appendices;

    private Builder setTableName(String tableName) {
      this.tableName = tableName;
      return this;
    }

    private Builder setWorkflowid(int workflowid) {
      this.workflowid = workflowid;
      return this;
    }

    private Builder setAppendices(List<Map<String, String>> appendices) {
      this.appendices = appendices;
      return this;
    }

    private Builder setRequestInfos(List<Map<String, String>> requestInfos) {
      this.requestInfos = requestInfos;
      return this;
    }

    public Builder setModeid(String modeid) {
      this.modeid = modeid;
      return this;
    }

    private Builder() {

    }

    public Builder(String unnumber) {
      this.unnumber = unnumber;
    }

    public void buildTableInfo() {
      String sql = "select t1.tablename,t2.id from workflow_bill t1\n" +
          "left join workflow_base t2\n" +
          "on t1.id=t2.formid\n" +
          "where t2.isvalid=1 and t2.unnumber=?";
      Map<String, String> info = CommonAPI.querySingleRow(sql, this.unnumber);
      this.setTableName(info.get("tablename"));
      String workflowid = info.get("id");
      this.setWorkflowid(Integer.parseInt(workflowid));
    }

    public WfParams build() {
      try {
        //获取 tablename workflowid
        buildTableInfo();
        return new WfParams(this);
      } catch (Exception e) {
        writeLog("build WfParams error, ", e);
        return null;
      }

    }

  }

  public String getTableName() {
    return tableName;
  }

  public String getUnnumber() {
    return unnumber;
  }

  public int getWorkflowid() {
    return workflowid;
  }

  public static boolean qualifyRequestInfo(Map<String, String> requestInfo) {
    if (requestInfo == null) {
      return false;
    }
    if (StringUtils.isEmpty(requestInfo.get("requestmark"))) {
      return false;
    }
    if (StringUtils.isEmpty(requestInfo.get("requestname"))) {
      return false;
    }
    return true;
  }

  public static boolean qualifyAppendices(Map<String, String> affix) {
    if (affix == null) {
      return false;
    }
    if (StringUtils.isEmpty(affix.get("requestmark"))) {
      return false;
    }
    if (StringUtils.isEmpty(affix.get("appendices"))) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return "WfParams{" +
        " tableName='" + tableName + '\'' +
        ", unnumber='" + unnumber + '\'' +
        ", workflowid=" + workflowid +
        ", modeid='" + modeid + '\'' +
        '}';
  }
}
