package com.red.circle.console.adapter.app.order;


import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.console.app.dto.clienobject.order.OrderPurchaseHistoryCO;
import com.red.circle.console.app.dto.cmd.app.order.OrderCompensateCmd;
import com.red.circle.console.app.service.app.order.IOrderPurchaseHistoryService;
import com.red.circle.console.infra.annotations.OpsOperationReqLog;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.order.inner.model.cmd.inapp.OrderPurchaseHistoryExportQryCmd;
import com.red.circle.order.inner.model.cmd.inapp.OrderPurchaseHistoryQryCmd;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 购买记录 前端控制器
 *
 * @author pengliang
 * @since 2020-03-24
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/order/purchase/history")
public class OrderPurchaseHistoryRestController extends BaseController {

  private final IOrderPurchaseHistoryService orderPurchaseHistoryService;

  /**
   * 补偿订单.
   */
  @PostMapping("/compensate")
  public void compensateOrder(@RequestBody OrderCompensateCmd cmd) {
    orderPurchaseHistoryService.compensateOrder(cmd);
  }

  /**
   * 补偿糖果.
   */
  @PostMapping("/compensate/candy")
  public void compensateCandy(Long userId, BigDecimal candy) {
    orderPurchaseHistoryService.compensateCandy(userId, candy);
  }

  /**
   * 今日内购总额.
   */
  @GetMapping("/purchase/today/total")
  public BigDecimal purchaseTodayTotal() {
    return orderPurchaseHistoryService.purchaseTodayTotal();
  }

  /**
   * 系统今日内购总额.
   */
  @GetMapping("/{sysOrigin}/purchase/today/total")
  public BigDecimal purchaseTodayTotal(
      @PathVariable("sysOrigin") SysOriginPlatformEnum sysOrigin) {
    return orderPurchaseHistoryService.purchaseTodayTotalBySysOrigin(sysOrigin);
  }

  /**
   * 购买记录.
   */
  @GetMapping("/page")
  public PageResult<OrderPurchaseHistoryCO> purchaseHistoryPage(OrderPurchaseHistoryQryCmd qryCmd) {
    return orderPurchaseHistoryService.purchaseHistoryPage(qryCmd);
  }

  /**
   * 购买记录.
   */
  @OpsOperationReqLog(value = "导出-购买记录", ignoreRequestParam = true)
  @GetMapping("/consume/export")
  public void exportCandy(HttpServletResponse response,
      @Validated OrderPurchaseHistoryExportQryCmd qryCmd) throws IOException {
    orderPurchaseHistoryService.exportPurchaseHistory(response, qryCmd);
  }

  /**
   * 用户购买总额.
   */
  @GetMapping("/buy/total/{userId}")
  public BigDecimal getBuyTotalByUserId(@PathVariable("userId") Long userId) {
    return orderPurchaseHistoryService.getPurchaseTotalByUserId(userId);
  }

}
