package weaver.interfaces.htsc.payment;

import com.engine.htsc.payment.enums.BudgetNodeType;
import com.engine.htsc.payment.service.BudgetCommand;
import com.engine.htsc.payment.service.impl.HeadPaymentBudgetCommand;
import com.engine.htsc.payment.service.impl.ITHeadPaymentBudgetCommand;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

/**
 * author: K1810012
 * description: 预算接口调用
 * 会计节点：节点后
 * 预占 3
 * 非会计退回：经过会计节点后的退回
 * 回退 2
 * 办结：节点前
 * 实占 1
 */
public class ITBudgetInterfaceInvoke implements Action {

	//预算状态
	private String billStatus;

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

	@Override
	public String execute(RequestInfo requestInfo) {
		RequestManager requestManager = requestInfo.getRequestManager();
		try {
			// 主表
			String requestId = requestInfo.getRequestid();
			String tableName = requestManager.getBillTableName();
			String src = requestManager.getSrc(); //提交方式
			//it优化,
			String sql = "select sfitfy from " + tableName + " where REQUESTID=" + requestId;
			RecordSet rs = new RecordSet();
			rs.executeQuery(sql);
			String sfitfy = "";
			if (rs.next()) {
				sfitfy = Util.null2String(rs.getString("sfitfy"));
			}
			BudgetCommand headPaymentBudgetCommand;
			if ("0".equals(sfitfy)) {
				log.info("当前it付款流程状态为通用付款");
				headPaymentBudgetCommand = new HeadPaymentBudgetCommand();
			} else {
				headPaymentBudgetCommand = new ITHeadPaymentBudgetCommand();
			}
			if ("3".equals(billStatus)) {
				//拟稿后提交
				new Thread(() -> {
					try {
						Thread.sleep(60 * 1000L);
						headPaymentBudgetCommand.submit(requestId, BudgetNodeType.SUBMIT.getValue());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}).start();
			}
			if ("2,3".equals(billStatus)) {
				//会计节点提交
				headPaymentBudgetCommand.reSubmit(requestId);
			}
			if ("2".equals(billStatus)) {
				headPaymentBudgetCommand.reject(requestId);
			}
			return SUCCESS;
		} catch (Exception e) {
			requestManager.setMessagecontent("流程流转异常!请联系管理员" + e);
			requestManager.setMessageid("5000");
			return FAILURE_AND_CONTINUE;
		}
	}

	public String getBillStatus() {
		return billStatus;
	}

	public void setBillStatus(String billStatus) {
		this.billStatus = billStatus;
	}

}
