package com.red.circle.console.adapter.app.sys.pay;

import com.red.circle.common.business.core.amount.PayAmountArithmeticUtils;
import com.red.circle.console.app.service.app.sys.pay.SysPayChannelFactoryService;
import com.red.circle.console.infra.annotations.OpsOperationReqLog;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.order.inner.model.cmd.DelAssociatedChannelsCmd;
import com.red.circle.order.inner.model.cmd.SysPayChannelFactoryCmd;
import com.red.circle.order.inner.model.dto.PayCommodityPreviewDTO;
import com.red.circle.order.inner.model.dto.PayCountryAssociatedChannelsDTO;
import com.red.circle.order.inner.model.dto.SysPayChannelFactoryDTO;
import com.red.circle.order.inner.model.dto.SysPayFactoryAssociatedChannelsDTO;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 系统渠道管理关联 前端控制器.
 *
 * @author pengliang
 * @since 2021-11-29
 */
@RequiredArgsConstructor
@RestController
@RequestMapping(value = "/sys-pay-channel-factory", produces = MediaType.APPLICATION_JSON_VALUE)
public class SysPayChannelFactoryController extends BaseController {

  private final SysPayChannelFactoryService sysPayChannelFactoryService;

  /**
   * 渠道关联厂商列表.
   */
  @GetMapping("/associate")
  public List<SysPayChannelFactoryDTO> listChannelFactory() {
    return sysPayChannelFactoryService.listChannelFactory();
  }

  /**
   * 保存关联渠道.
   */
  @OpsOperationReqLog("保存-厂商关联渠道")
  @PostMapping("/add-batch")
  public void addAssociatedChannels(
      @RequestBody @Validated List<SysPayChannelFactoryCmd> params) {
    sysPayChannelFactoryService.addAssociatedChannels(params);
  }

  /**
   * 修改第三方关联信息.
   */
  @Deprecated
  @OpsOperationReqLog("保存-修改第三方关联信息")
  @PostMapping("/update")
  public void updateFactoryCode(
      @RequestBody @Validated SysPayChannelFactoryCmd param) {
    sysPayChannelFactoryService.updateAssociatedChannels(param);
  }

  /**
   * 删除关联渠道.
   */
  @OpsOperationReqLog("保存-删除关联渠道")
  @PostMapping("/del-batch")
  public void delAssociatedChannels(
      @RequestBody @Validated DelAssociatedChannelsCmd param) {
    sysPayChannelFactoryService.delAssociatedChannels(param);
  }

  /**
   * 国家已关联渠道-列表.
   */
  @GetMapping("/country")
  public List<PayCountryAssociatedChannelsDTO> list(Long payCountryId) {
    return sysPayChannelFactoryService.listByPayCountry(payCountryId);
  }

  /**
   * 根据限额获取支付的付款渠道-列表.
   */
  @GetMapping("/support-amount")
  public PayCommodityPreviewDTO list(@RequestParam("payCountryId") Long payCountryId,
      @RequestParam(value = "amountUsd", required = false) BigDecimal amountUsd) {
    return sysPayChannelFactoryService.listSupportAmountUsd(payCountryId,
        Objects.isNull(amountUsd) ? BigDecimal.ZERO : amountUsd);
  }

  /**
   * 根据限额获取支付的付款渠道-列表.
   */
  @GetMapping("/support-amount-rate")
  public PayCommodityPreviewDTO list(@RequestParam Long payCountryId,
      @RequestParam("amount") BigDecimal amount,
      @RequestParam("exchangeRate") BigDecimal exchangeRate) {
    return sysPayChannelFactoryService.listSupportAmountUsd(payCountryId,
        PayAmountArithmeticUtils.divideUpTwoPoint(
            amount, exchangeRate
        )
    );
  }

  /**
   * 已关联渠道code-列表.
   */
  @GetMapping("/channels")
  public List<SysPayFactoryAssociatedChannelsDTO> listChannels(String factoryCode) {
    return sysPayChannelFactoryService.listChannelByFactoryCode(factoryCode);
  }
}
