package com.crm.manager.controller;

import com.crm.manager.dto.export.GlobalOrderListExportDto;
import com.crm.manager.service.ucard.UcardGlobalFeignService;
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.core.enums.CardApproveStatusEnum;
import com.crm.rely.backend.core.enums.GlobalOrderDeductionStatusEnum;
import com.crm.rely.backend.core.enums.GlobalOrderStatusEnum;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.FileProcessUtil;
import com.crm.rely.backend.util.NameUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
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 javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 速汇服务接口
 */
@Slf4j
@RestController
@RequestMapping("/ucard/global")
public class UcardGlobalController {

    @Autowired
    private UcardGlobalFeignService globalFeignService;

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

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

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

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

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

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

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

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

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

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

    /**
     * 查询最新汇率
     */
    @PostMapping("/query/latest/exchange/rate")
    public BaseResultDto getExchangeRateByCurrency(@RequestBody GlobalQueryExchangeRateEntity entity) throws ServiceException {
        return globalFeignService.getExchangeRateByCurrency(entity);
    }

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

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

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

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

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

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

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

    /**
     * 速汇订单导出
     */
    @PostMapping("/order/list/export")
    public void globalOrderListExport(@RequestBody GlobalOrderPageEntity entity, HttpServletResponse response) throws Exception {
        BaseResultDto<List<GlobalOrderDto>> resultDto = globalFeignService.globalOrderListExport(entity);
        if (resultDto != null && resultDto.getCode() == 200) {
            List<GlobalOrderListExportDto> dtos = Lists.newArrayList();
            if (CollectionUtils.isNotEmpty(resultDto.getData())) {
                resultDto.getData().forEach((item) -> {
                    GlobalOrderListExportDto dto = new GlobalOrderListExportDto();
                    BeanUtils.copyProperties(item, dto);
                    dto.setCID(item.getCId());
                    dto.setName(NameUtil.getName(item.getCountry(), item.getLastName(), null, item.getFirstName()));
                    item.setAreaCode(item.getAreaCode() == null ? "" : item.getAreaCode());
                    item.setMobile(item.getMobile() == null ? "" : item.getMobile());
                    dto.setMobile(item.getAreaCode() + " " + item.getMobile());
                    if (item.getStatus() != null){
                        dto.setStatus(GlobalOrderStatusEnum.matchStatus(item.getStatus()));
                    } else {
                        dto.setStatus("待处理");
                    }
                    dto.setApproveStatus(CardApproveStatusEnum.matchCode(item.getApproveStatus()));
                    dto.setDeductionAccountStatus(GlobalOrderDeductionStatusEnum.matchStatus(item.getDeductionAccountStatus()));
                    dtos.add(dto);
                });
            }
            ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("GLOBAL_ORDER_LIST"), dtos, GlobalOrderListExportDto.class, response);
        }
    }

}
