package com.engine.htsc.branchpayment.web;

import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.branchpayment.service.BranchPaymentService;
import com.engine.htsc.branchpayment.service.impl.BranchPaymentServiceImpl;
import com.engine.htsc.trip.util.AjaxResult;
import weaver.general.BaseBean;
import weaver.general.Util;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.Map;


/**
 * k1810012
 * 分支出纳页面接口
 * <p>
 * 预算实占、核算、付款、废弃
 */
public class BranchGeneralPaymentAction extends BaseBean {

  private BranchPaymentService getBranchPaymentServiceImpl() {
    return ServiceUtil.getService(BranchPaymentServiceImpl.class);
  }

  /**
   * 预算实占、废弃
   *
   * @param request 请求体
   * @return 消息体
   */
  @POST
  @Path("/doBudget")
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<Map<String, String>> doBudget(@Context HttpServletRequest request) {
    try {
      Map<String, Object> requestParam = ParamUtil.request2Map(request);
      String rid = Util.null2String(requestParam.getOrDefault("rid", ""));
      String discard = Util.null2String(requestParam.getOrDefault("discard", ""));
      return AjaxResult.ok(getBranchPaymentServiceImpl().doBudgetByBatchId(rid, discard));
    } catch (Exception e) {
      writeLog(e.getMessage());
      return AjaxResult.error(e.getMessage());

    }
  }

  /**
   * 核算
   *
   * @param request 请求体
   * @return 消息体
   */
  @POST
  @Path("/doAccount")
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<Map<String, String>> doAccount(@Context HttpServletRequest request) {
    try {
      Map<String, Object> requestParam = ParamUtil.request2Map(request);
      String id = Util.null2String(requestParam.getOrDefault("id", ""));
      return AjaxResult.ok(getBranchPaymentServiceImpl().doAccountByBatchId(id));
    } catch (Exception e) {
      writeLog(e.getMessage());
      return AjaxResult.error(e.getMessage());

    }
  }

  /**
   * 付款
   *
   * @param request 请求体
   * @return 消息体
   */
  @POST
  @Path("/doPayment")
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<Map<String, String>> doPayment(@Context HttpServletRequest request) {
    try {
      Map<String, Object> requestParam = ParamUtil.request2Map(request);
      String id = Util.null2String(requestParam.getOrDefault("id", ""));
      return AjaxResult.ok(getBranchPaymentServiceImpl().doPaymentByBatchId(id));
    } catch (Exception e) {
      writeLog(e.getMessage());
      return AjaxResult.error(e.getMessage());

    }
  }

  /**
   * 手动付款
   */
  @POST
  @Path("/doManualPayment")
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<String> doManualPayment(@Context HttpServletRequest request) {
    try {
      Map<String, Object> requestParam = ParamUtil.request2Map(request);
      String id = Util.null2String(requestParam.getOrDefault("id", ""));
      return AjaxResult.ok(getBranchPaymentServiceImpl().updateManualPaymentStatus(id));
    } catch (Exception e) {
      writeLog(e.getMessage());
      return AjaxResult.error(e.getMessage());

    }
  }


}
