package com.engine.htsc.subpayment.web;

import com.alibaba.fastjson.JSONObject;
import com.engine.common.util.ParamUtil;
import com.engine.common.util.ServiceUtil;
import com.engine.htsc.subpayment.service.SubPaymentService;
import com.engine.htsc.subpayment.service.impl.SubPaymentServiceImpl;
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.HashMap;
import java.util.Map;

public class SubGeneralPaymentAction extends BaseBean {

  private SubPaymentService getSubPaymentService() {
    return ServiceUtil.getService(SubPaymentServiceImpl.class);
  }

  @GET
  @Path("/queryOverBudget")
  @Produces(MediaType.APPLICATION_JSON)
  public String queryOverBudget(@Context HttpServletRequest request) {
    Map<String, Object> map = ParamUtil.request2Map(request);
    Map<String, Object> apidatas = new HashMap<>();
    try {
      //实例化Service 并调用业务类处理
      apidatas = getSubPaymentService().queryOverBudget(map);
    } catch (Exception e) {
      //异常处理
      e.printStackTrace();
      apidatas.put("api_status", false);
      apidatas.put("api_errormsg", "catch exception : " + e.getMessage());
    }
    return JSONObject.toJSONString(apidatas);
  }

  @GET
  @Path("/queryReiPlace")
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<String> queryReiPlace(@QueryParam("ngr") String ngr) {
    try {
      return AjaxResult.ok(getSubPaymentService().queryReiPlace(ngr));
    } catch (Exception e) {
      writeLog(e.getMessage());
      return AjaxResult.error(e.getMessage());

    }
  }

  @GET
  @Path("/queryBankInfo")
  @Produces(MediaType.APPLICATION_JSON)
  public AjaxResult<Map<String, String>> queryBankInfo(@QueryParam("khr") String uid) {
    try {
      return AjaxResult.ok(getSubPaymentService().queryBankInfoByUid(uid));
    } catch (Exception e) {
      writeLog(e.getMessage());
      return AjaxResult.error(e.getMessage());

    }
  }

  /**
   * 预算实占、废弃
   *
   * @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(getSubPaymentService().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(getSubPaymentService().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(getSubPaymentService().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(getSubPaymentService().updateManualPaymentStatus(id));
    } catch (Exception e) {
      writeLog(e.getMessage());
      return AjaxResult.error(e.getMessage());

    }
  }


}
