package weaver.interfaces.htsc.fundtrans;

import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

public class DeletePayDetailReturn extends BaseBean implements Action {

  @Override
  public String execute(RequestInfo requestInfo) {
    RequestManager rm = requestInfo.getRequestManager();
    String rid = requestInfo.getRequestid();
    writeLog(String.format("退回删除建模数据 paydetail, requestid: %s", rid));

    RecordSetTrans rst = new RecordSetTrans();
    try {
      rst.setAutoCommit(false);
      int lastNodeId = rm.getLastNodeid(); //上一个节点
      int currentNodeId = rm.getNodeid(); //当前节点,退回节点前指的就是cnfk
      String lastNode = "SELECT wf.NODEID,wn.UNNUMBER FROM workflow_flownode wf\n" +
              "LEFT JOIN workflow_nodebase wn\n" +
              "ON wf.NODEID = wn.ID \n" +
              "WHERE wf.NODEID = ?";
      rst.executeQuery(lastNode, currentNodeId);
      String lastUnNumber = "";
      if (rst.next()) {
        lastUnNumber = Util.null2String(rst.getString("UNNUMBER"));
      }
      writeLog(String.format("当前流程reqeustId: %s; 上一节点: %s; 上一节点UNNUMBER: %s", rid, lastNodeId, lastUnNumber));
      if ("zbzjnbdb_cnfk".equalsIgnoreCase(lastUnNumber) || "zjywfk_cnfk".equalsIgnoreCase(lastUnNumber)) {
        // 当前节点执行删除
        String sql = "DELETE FROM uf_paydetal WHERE REQID = ?";
        rst.executeUpdate(sql, rid);
        rst.commit();
      }
      return SUCCESS;
    } catch (Exception e) {
      rst.rollback();
      writeLog(e.getMessage());
      return FAILURE_AND_CONTINUE;
    }

  }
}
