package com.crm.ucard.controller;

import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.dto.base.BaseReportResultDto;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.ucard.GlobalOrderDto;
import com.crm.rely.backend.core.dto.ucard.GlobalOrderSumDto;
import com.crm.rely.backend.core.dto.wasabi.WasabiAvailableCurrenciesListDto;
import com.crm.rely.backend.core.entity.base.BaseEntity;
import com.crm.rely.backend.core.entity.ucard.*;
import com.crm.rely.backend.core.entity.wasabi.WasabiTransactionSubmitRfiEntity;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.ucard.service.GlobalService;
import com.crm.ucard.service.UcardExportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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;

import java.util.List;

/**
 * 银行卡服务接口
 */
@Slf4j
@RestController
@RequestMapping("/ucard/global/manager")
public class ManagerUcardGlobalController {

    @Autowired
    private GlobalService globalService;
    @Autowired
    private UcardExportService ucardExportService;

    /**
     * 速汇订单导出
     */
    @PostMapping("/order/list/export")
    @FeignClientAnnotation
    public BaseResultDto<List<GlobalOrderDto>> globalOrderListExport(@RequestBody GlobalOrderPageEntity entity) throws ServiceException {
        return ucardExportService.globalOrderListExport(entity);
    }

    /**
     * 同步币种信息
     */
    @PostMapping("/currencies/save")
    @FeignClientAnnotation
    public BaseResultDto saveAvailableCurrencies() throws Exception {
        return globalService.saveCurrencies();
    }

    /**
     * 币种分页列表
     */
    @PostMapping("/currencies/list")
    @FeignClientAnnotation
    ResultWithPagerDto<WasabiAvailableCurrenciesListDto> getCurrenciesPageList(@RequestBody GlobalCurrenciesPageListEntity entity) throws ServiceException {
        return globalService.getCurrenciesPageList(entity);
    }

    /**
     * 支持的币种下拉列表
     */
    @PostMapping("/currencies/dropdown")
    @FeignClientAnnotation
    public BaseResultDto getCurrenciesDropdown(@RequestBody GlobalCurrenciesDropdownEntity entity) throws ServiceException {
        return globalService.getCurrenciesDropdown(entity);
    }

    /**
     * 币种字段和可选值列表
     */
    @PostMapping("/currencies/field/list")
    @FeignClientAnnotation
    public BaseResultDto getCurrenciesFieldList(@RequestBody @Validated GlobalCurrenciesFieldListEntity entity) throws ServiceException {
        return globalService.getCurrenciesFieldList(entity);
    }

    /**
     * 币种所有字段名称
     */
    @PostMapping("/field/params")
    @FeignClientAnnotation
    public BaseResultDto getGlobalFieldParams() throws ServiceException {
        return globalService.getGlobalFieldParams();
    }

    /**
     * 创建交易订单
     */
    @PostMapping("/create/order")
    @FeignClientAnnotation
    public BaseResultDto globalCreateOrder(@RequestBody GlobalCreateOrderEntity entity) throws Exception {
        return globalService.globalCreateOrder(entity);
    }

    /**
     * 取消交易订单
     */
    @PostMapping("/cancel/order")
    @FeignClientAnnotation
    public BaseResultDto globalCancelOrder(@RequestBody GlobalCancelOrderEntity entity) throws Exception {
        return globalService.globalCancelOrder(entity);
    }

    /**
     * 币种配置更改
     */
    @PostMapping("/currencies/config")
    @FeignClientAnnotation
    public BaseResultDto globalCurrenciesConfig(@RequestBody GlobalCurrenciesConfigEntity entity) throws ServiceException {
        return globalService.globalCurrenciesConfig(entity);
    }

    /**
     * 查询汇率和手续费率
     */
    @PostMapping("/query/exchange/rate")
    @FeignClientAnnotation
    public BaseResultDto queryExchangeRate(@RequestBody GlobalQueryExchangeRateEntity entity) throws Exception {
        return BaseResultDto.success(globalService.queryExchangeRate(entity));
    }

    /**
     * 查询最新汇率
     */
    @PostMapping("/query/latest/exchange/rate")
    @FeignClientAnnotation
    public BaseResultDto getExchangeRateByCurrency(@RequestBody GlobalQueryExchangeRateEntity entity) throws Exception {
        return BaseResultDto.success(globalService.getExchangeRateByCurrency(entity.getPayoutCurrency(), entity.getTransferTypeId(), entity.getPayoutMethodId()));
    }

    /**
     * 查询支持的城市列表
     */
    @PostMapping("/query/bank/cities")
    @FeignClientAnnotation
    public BaseResultDto queryBankCities(@RequestBody CardBankCitiesEntity entity) throws ServiceException {
        return globalService.queryBankCities(entity);
    }

    /**
     * 查询币种全局配置
     */
    @PostMapping("/query/currencies/config")
    @FeignClientAnnotation
    public BaseResultDto getCurrenciesGlobalConfig() throws ServiceException {
        return globalService.getCurrenciesGlobalConfig();
    }

    /**
     * 速汇订单分页列表
     */
    @PostMapping("/order/page/list")
    @FeignClientAnnotation
    public BaseReportResultDto<List<GlobalOrderDto>, GlobalOrderSumDto> globalOrderPageList(@RequestBody GlobalOrderPageEntity entity) throws Exception {
        return globalService.globalOrderPageList(entity);
    }

    /**
     * 补充资料
     */
    @PostMapping("/supplementary/data")
    @FeignClientAnnotation
    public BaseResultDto supplementaryData(@RequestBody WasabiTransactionSubmitRfiEntity entity) throws Exception {
        return globalService.supplementaryData(entity);
    }

    /**
     * 速汇订单审批
     */
    @PostMapping("/order/approve")
    @FeignClientAnnotation
    public BaseResultDto globalOrderApprove(@RequestBody GlobalOrderApproveEntity entity) throws Exception {
        return globalService.globalOrderApprove(entity);
    }

    /**
     * 查询速汇订单详情
     */
    @PostMapping("/order/details")
    @FeignClientAnnotation
    public BaseResultDto queryGlobalOrderDetails(@RequestBody BaseEntity entity) throws ServiceException {
        return globalService.queryGlobalOrderDetails(entity);
    }

    /**
     * 获取收款用户列表
     */
    @PostMapping("/receiver/user/list")
    @FeignClientAnnotation
    BaseResultDto getGlobalReceiverUserList(@RequestBody GlobalReceiverUserEntity entity) throws ServiceException {
        return globalService.getGlobalReceiverUserList(entity);
    }

    /**
     * 删除收款用户
     */
    @PostMapping("/receiver/user/delete")
    @FeignClientAnnotation
    BaseResultDto deleteGlobalReceiverUser(@RequestBody BaseEntity entity) throws ServiceException{
        return globalService.deleteGlobalReceiverUser(entity);
    }

}
