package weaver.interfaces.htsc.fundbusinesspay;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSetTrans;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.hrm.search.HrmSearchComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

/**
 * k1810012
 * 会签节点后,根据明细表2
 * 人员名称更新会签状态
 */
public class FbpUpdateDt2Action implements Action {

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

  @Override
  public String execute(RequestInfo requestInfo) {
    RequestManager rm = requestInfo.getRequestManager();
    String rid = requestInfo.getRequestid();
    int userId = rm.getUserId();
    log.info(String.format("fbpupdate2-dt2分送到达节点处理人:%s", userId));
    String tableName = rm.getBillTableName();
    RecordSetTrans rst = new RecordSetTrans();

    try {
      ResourceComInfo rc = new ResourceComInfo();
      String departmentID = rc.getDepartmentID(String.valueOf(userId));
      rst.setAutoCommit(false);

      String sql = "select t2.REQUESTID,t1.MAINID,t1.HQBM from " + tableName + "_DT2 t1\n" +
              "left join " + tableName + " t2\n" +
              "on t1.MAINID=t2.ID\n" +
              "where t2.REQUESTID=? and t1.HQBM=?";

      rst.executeQuery(sql, rid, departmentID);
      log.info(String.format("fbpupdate2-查询明细表2: %s;%s;%s", sql, rid, departmentID));
      String mainId = "";
      String countersignDept = "";
      if (rst.next()) {
        mainId = Util.null2String(rst.getString("MAINID"));
        countersignDept = Util.null2String(rst.getString("HQBM"));
      }
      log.info(String.format("countersignDept:%s;mainId=%s", countersignDept, mainId));

      if (StringUtils.isNotBlank(mainId)) {
        rst.executeUpdate("update " + tableName + "_DT2 set YHQ=1 where MAINID=? and HQBM=?", mainId, countersignDept);
      }

      rst.commit();
      return SUCCESS;
    } catch (Exception e) {
      rst.rollback();
      rm.setMessageid("500");
      rm.setMessagecontent("流程提交失败,请联系管理员!原因:" + e.getMessage());
      return Action.FAILURE_AND_CONTINUE;
    }

  }
}
