package weaver.interfaces.htsc.payment;

import com.engine.htsc.payment.dao.BudgetDao;
import com.engine.htsc.payment.enums.BudgetNodeType;
import com.engine.htsc.payment.service.BudgetService;
import com.engine.htsc.payment.service.BxBudgetService;
import com.engine.htsc.payment.service.PaymentService;
import com.engine.htsc.payment.service.impl.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.time.format.DateTimeFormatter;

public class BxBudgetInterfaceInvoke implements Action {
    //通用sql
    BudgetDao budgetDao = new BudgetDao();

    BudgetService bs = new BudgetServiceImpl();

    //入参日期格式
    static DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    //预算状态
    private String billStatus;

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

    @Override
    public String execute(RequestInfo requestInfo) {
        RequestManager requestManager = requestInfo.getRequestManager();
        try {
            // 主表
            String tableName = requestManager.getBillTableName();
            String requestId = requestInfo.getRequestid();
            String src = requestManager.getSrc(); //提交方式
//            BxBudgetService ps = new BxBudgetServiceImpl();
//            String message = ps.doBudgetAccount(requestId, tableName, billStatus);
            //不再使用BxBudgetServiceImpl
            if ("2".equals(billStatus) && !"reject".equals(src)) {
                log.info("识别当前流程为[" + requestId + "]自动创建,流程状态@" + src);
                return SUCCESS;
            }
            HeadBxBudgetCommand headBxBudgetCommand = new HeadBxBudgetCommand();
            if ("3".equals(billStatus)) {
                //拟稿后提交
                headBxBudgetCommand.submit(requestId, BudgetNodeType.SUBMIT.getValue());
            }
            if ("2,3".equals(billStatus)) {
                //会计节点提交,先取消预占再重新提交
                headBxBudgetCommand.reSubmit(requestId);
            }
            if ("2".equals(billStatus)) {
                //取消预占
                headBxBudgetCommand.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;
    }

}
