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


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.core.impl.Service;
import com.engine.htsc.financialadjustment.service.AdjustFundService;
import com.engine.htsc.financialadjustment.util.BizUtil;
import com.engine.htsc.payment.pojo.BudgetDTO;
import com.engine.htsc.payment.pojo.CostAccountDto;
import com.engine.htsc.payment.util.BizUtils;
import weaver.general.BaseBean;
import weaver.interfaces.htsc.cus.HTUtil;

import java.util.Map;

public class AdjustFundServiceImpl extends Service implements AdjustFundService {

  private final HTUtil htUtil = new HTUtil();

  private final BaseBean log = new BaseBean();

  @Override
  public String doBudget(String rid) throws Exception {
    log.writeLog("财务调整流程预算 requestId:" + rid);
    BudgetDTO budgetDTO = BizUtil.getAdjustBudget(rid, "3");
    if (budgetDTO == null) {
      throw new Exception("构建财务调整流程预算数据报文出错,budgetDTO为空");
    }
    BizUtils bizUtils = new BizUtils();
    Map<String, Object> paras = bizUtils.convertDtoToMap(budgetDTO);
    log.writeLog("财务调整流程预算预占 入参:" + JSON.toJSONString(paras));
    String ys = htUtil.budgetCommit(paras);
    log.writeLog("财务调整流程预算预占 出参:" + ys);
    //预占执行完后实占
    budgetDTO.setBillStatus("1");
    paras = bizUtils.convertDtoToMap(budgetDTO);
    log.writeLog("财务调整流程预算实占 入参:" + JSON.toJSONString(paras));
    String response = htUtil.budgetCommit(paras);
    log.writeLog("财务调整流程预算实占 返回值:" + response);
    return response;
  }

  @Override
  public String doCost(String rid) throws Exception {
    log.writeLog("财务调整流程核算 requestId:" + rid);
    CostAccountDto adjustCost = BizUtil.getAdjustCost(rid, "fnaj");
    @SuppressWarnings("unchecked")
    Map<String, Object> paras = JSONObject.parseObject(JSON.toJSONString(adjustCost));
    log.writeLog("财务调整流程核算 入参:" + JSON.toJSONString(paras));
    String resp = htUtil.saveFydj(paras);
    log.writeLog("财务调整流程核算 返回值:" + resp);
    return resp;
  }
}
