package weaver.interfaces.htsc.workflow;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.htsc.payment.enums.HsType;
import com.engine.htsc.payment.pojo.CostAccountDto;
import com.engine.htsc.payment.service.impl.HeadPaymentBudgetCommand;
import com.engine.htsc.payment.util.BizUtils;
import weaver.general.BaseBean;
import weaver.interfaces.htsc.cus.HTUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;
import java.util.Map;

/**
 * Created by K1810006 on 2021/08/06.
 */

public class CostShareBudgetAndAccountAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RequestManager requestManager = requestInfo.getRequestManager();
        String requestId = requestInfo.getRequestid();
        String tableName = requestManager.getBillTableName();
        writeLog("费用分摊款流程,办结前对接预算核算接口");
        try {
            HeadPaymentBudgetCommand command = new HeadPaymentBudgetCommand();
            writeLog(requestId + "@预算开始");
            String submitResp = command.submit(requestId, "3");
            writeLog("执行预算预占,返回值@" + submitResp);
            String finishResp = command.finish(requestId);
            writeLog("执行预算实占,返回值@" + finishResp);

            writeLog(requestId + "@核算开始");
            String lb = HsType.ZBFK.getType();
            String hsResp = doCost(requestId, lb);
            writeLog("执行核算,返回值@" + hsResp);
        } catch (Exception e) {
            e.printStackTrace();
            requestManager.setMessageid("5001");
            requestManager.setMessagecontent("预算核算接口调用失败!");
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }



    /**
     * 预付款核算
     *
     * @param requestid 流程id
     * @param lb    流程类型
     * @return 处理核算的返回值
     */
    public String doCost(String requestid, String lb) throws Exception {
        BizUtils bizUtils = new BizUtils();
        CostAccountDto costAccountDto = bizUtils.generateCommonPayData(requestid,lb);
        costAccountDto.setBz2("1");
        String params = JSON.toJSONString(costAccountDto);
        Map<String, Object> paramsMap = JSONObject.parseObject(params);
        HTUtil ht = new HTUtil();
        writeLog("开始执行核算接口");
        writeLog("CostShareBudgetAndAccountAction核算入参@request=\n" + paramsMap + "@");
        String response = ht.saveFydj(paramsMap);
        writeLog("CostShareBudgetAndAccountAction核算返回@response=\n" + response + "@");
        return response;
    }

}
