package com.engine.htsc.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.engine.htsc.payment.enums.BudgetWF;
import com.engine.htsc.payment.pojo.BudgetDTO;
import com.engine.htsc.payment.pojo.WbLog;
import com.engine.htsc.payment.service.BudgetCommand;
import com.engine.htsc.payment.service.BudgetStrategy;
import com.engine.htsc.payment.util.*;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.joda.time.DateTime;
import weaver.interfaces.htsc.comInfo.PropBean;
import weaver.interfaces.htsc.cus.HTUtil;

import java.util.Map;

public class ITHeadPaymentBudgetCommand implements BudgetCommand {

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

  private final BudgetStrategy<BudgetDTO> bg = BudgetStrategyFactory.getBudgetStrategyForPayment(
          BudgetWF.PAYMENT.getCode());

  @Override
  public String submit(String requestNo, String billStatus) throws Exception {
    assert bg != null;
    String requestUrl = new PropBean().getPropName("ITBudgetUpdateUrl");
    BizUtils bizUtils = new BizUtils();
    BudgetDTO budgetDTO = bg.buildNewBudgetParamFromWorkflowDB(requestNo, billStatus);
    log.info("\n 执行总部通用付款预算 >>> 当前状态submit");
    log.info("\n 调用预算参数(状态:" + billStatus + ") >>> \n " + JSON.toJSONString(budgetDTO));
    //记录日志信息
    WbLog wbLog = new WbLog();
    DateTime today = new DateTime();
    wbLog.setRequestNo(requestNo);
    wbLog.setTime(today.toString("yyyy-MM-dd HH:mm:ss"));
    wbLog.setUrl(requestUrl);
    wbLog.setMethod("配置文件ITBudgetUpdateUrl");
    wbLog.setIp(IpUtils.getHostIp());
    try {
      HTUtil htUtil = new HTUtil();
      Map<String, Object> stringObjectMap = bizUtils.convertDtoToMap(budgetDTO);
      String normalResp = htUtil.budgetCommit(stringObjectMap);

      Map<String, String> appHeader = htUtil.getAppHeader();
      log.info("getAppHeader@" + appHeader + "@");
      //新增token校验
      String resp = HttpUtil.postApplicationJson(requestUrl, JSON.toJSONString(budgetDTO), appHeader);
      bizUtils.refreshBudgetRecord(requestNo, billStatus, JSON.toJSONString(budgetDTO), "1");
      log.info("\n 调用预算返回(状态:" + billStatus + ") >>> \n " + resp);
      boolean b = bizUtils.updateBudgetMidTable(requestNo, billStatus);
      log.info("\n 调用预算更新中间表返回 >>> \n " + b);
      //记录日志
      wbLog.setResponse(normalResp);
      if ("F1001".equals(normalResp)) {
        wbLog.setResult("预算成功");
      } else {
        wbLog.setResult("预算失败,返回值:" + normalResp);
      }
      wbLog.setRequest(JSON.toJSONString(budgetDTO));
      return normalResp; //F1001
    } catch (Exception e) {
      //记录日志
      wbLog.setResponse("");
      wbLog.setResult("预算失败");
      wbLog.setRequest(JSON.toJSONString(budgetDTO));
      throw new Exception("预算失败");
    } finally {
      BizUtils.saveLog(wbLog);
    }
  }

  @Override
  public String reSubmit(String requestNo) throws Exception {
    String reject = reject(requestNo);
    if ("F1001".equalsIgnoreCase(reject)) {
      return submit(requestNo, "3"); //重新预占
    } else {
      return reject;
    }
  }

  @Override
  public String reject(String requestNo) throws Exception {
    assert bg != null;
    String requestUrl = new PropBean().getPropName("ITBudgetUpdateUrl");
    BizUtils bizUtils = new BizUtils();
    BudgetDTO lastBudgetDTO = bg.getLastBudgetParamFromModeDB(requestNo, "1");
    if (AssertUtil.isEmpty(lastBudgetDTO)) {
      // 为不影响后面的逻辑,默认返回成功F1001
      log.info(String.format("UF_YSJLB无requestno=%s的预算数据", requestNo));
      return "F1001";
    }
    lastBudgetDTO.setBillStatus("2"); //回退
    log.info("\n 执行总部IT付款预算 >>> 当前状态reject");
    log.info("\n 调用预算参数(回退) >>> \n " + JSON.toJSONString(lastBudgetDTO));
    //记录日志信息
    WbLog wbLog = new WbLog();
    DateTime today = new DateTime();
    wbLog.setRequestNo(requestNo);
    wbLog.setTime(today.toString("yyyy-MM-dd HH:mm:ss"));
    wbLog.setUrl(requestUrl);
    wbLog.setIp(IpUtils.getHostIp());

    try {
      //退回时调用budgetCommit
      HTUtil htUtil = new HTUtil();
      Map<String, Object> stringObjectMap = bizUtils.convertDtoToMap(lastBudgetDTO);
      String normalResp = htUtil.budgetCommit(stringObjectMap);

      Map<String, String> appHeader = htUtil.getAppHeader();
      log.info("getAppHeader@" + appHeader + "@");
      String resp = HttpUtil.postApplicationJson(requestUrl, JSON.toJSONString(lastBudgetDTO), appHeader);
      log.info("ITBudgetInterfaceInvoke>>预算接口调用返回 resp : @" + resp + "@");
      bizUtils.refreshBudgetRecord(requestNo, "2", JSON.toJSONString(lastBudgetDTO), "1");
      log.info("\n 调用预算返回(回退) >>> \n " + normalResp);
      boolean b = bizUtils.updateBudgetMidTable(requestNo, "2");
      log.info("\n 调用预算更新中间表返回(回退) >>> \n " + b);
      //记录日志
      wbLog.setResponse(resp);
      wbLog.setResult("预算成功");
      wbLog.setRequest(JSON.toJSONString(lastBudgetDTO));
      return resp;
    } catch (Exception e) {
      //记录日志
      wbLog.setResponse("");
      wbLog.setResult("预算失败");
      wbLog.setRequest(JSON.toJSONString(lastBudgetDTO));
      throw new Exception("预算失败");
    } finally {
      BizUtils.saveLog(wbLog);
    }

  }

  @Override
  public String finish(String requestNo) throws Exception {
    return submit(requestNo, "1");
  }
}
