package weaver.interfaces.htsc.fundbusinesspay;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import weaver.conn.RecordSetTrans;
import weaver.cpcompanyinfo.CompanyInfo;
import weaver.hrm.company.CompanyComInfo;
import weaver.hrm.company.DepartmentComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * k1810012
 * 资金业务付款,跨公司会签
 * 1.拟稿节点后
 * <p>
 * 2.跨公司会签节点后
 */
public class CrossCompanyCountersignAction implements Action {

  //节点状态
  private int status = 0;

  private final Log log = LogFactory.getLog(CrossCompanyCountersignAction.class);

  @Override
  public String execute(RequestInfo requestInfo) {

    RequestManager rm = requestInfo.getRequestManager();

    String rid = requestInfo.getRequestid();
    String tableName = rm.getBillTableName();

    RecordSetTrans rst = new RecordSetTrans();
    try {
      rst.setAutoCommit(false);
      if (status == 0) {
        operateAfterNg(rid, tableName, rst);
      }

      if (status == 1) {
        operateAfterFs(rid, tableName, rst);
      }

      rst.commit();
      return SUCCESS;
    } catch (Exception e) {
      rst.rollback();
      log.info(String.format("流程节点后流转失败,当前流程状态:%d,错误信息:%s", status, e.getMessage()));
      rm.setMessageid("500");
      rm.setMessagecontent("流程提交失败,请联系管理员!原因:" + e.getMessage());
      return Action.FAILURE_AND_CONTINUE;
    }
  }

  /**
   * 分送节点后
   *
   * @param rid       rid
   * @param tableName tb
   * @param rst       数据源
   * @throws Exception 异常
   */
  private void operateAfterFs(String rid, String tableName, RecordSetTrans rst) throws Exception {


  }

  /**
   * 拟稿节点后
   *
   * @param rid       rid
   * @param tableName tb
   * @param rst       数据源
   * @throws Exception 异常
   */
  private void operateAfterNg(String rid, String tableName, RecordSetTrans rst) throws Exception {
    rst.executeQuery("select kgsldsp,ID from " + tableName + " where REQUESTID=?", rid);
    String signers = "";
    String mainId = "";
    if (rst.next()) {
      signers = rst.getString("kgsldsp"); //会签人员
      mainId = rst.getString("ID");
    }

    rst.executeUpdate("delete "+tableName+"_DT3 where MAINID=?", mainId); //先清空明细3
    if ("".equals(signers)) {
      log.info("会签人员为空,不更新明细表3");
    } else {
      CompanyComInfo cci = new CompanyComInfo();
      DepartmentComInfo dci = new DepartmentComInfo();
      String finalMainId = mainId; //final
      //插入明细表3数据
      List<List<Object>> paramsList = new ArrayList<>();
      Arrays.stream(signers.split(",")).forEach(s -> {
        List<Object> valueList = new ArrayList<>();
        valueList.add(finalMainId);
        valueList.add(dci.getDepartmentid(Integer.parseInt(s)));
        valueList.add(s);
        paramsList.add(valueList);
      });

      String isql = "INSERT INTO " +
              tableName+"_DT3 (MAINID,HQBM,ZDRY,SFSHQ,HTDELETE,YHQ) " +
              "VALUES (?,?,?,1,'删除',0)";
      rst.executeUpdate(isql, paramsList);

    }

  }

  public int getStatus() {
    return status;
  }

  public void setStatus(int status) {
    this.status = status;
  }
}
