package com.crm.ucard.controller;

import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.dto.base.*;
import com.crm.rely.backend.core.dto.ucard.*;
import com.crm.rely.backend.core.dto.wasabi.*;
import com.crm.rely.backend.core.entity.base.*;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.custom.kyc.WebsdkLinkAliEntity;
import com.crm.rely.backend.core.entity.ucard.*;
import com.crm.rely.backend.core.entity.wasabi.*;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.GetIpAndMac;
import com.crm.ucard.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 银行卡服务接口
 */
@Slf4j
@RestController
@RequestMapping("/wasabi/custom")
public class CustomWasabiCardController {

    @Autowired
    private UcardService ucardService;
    @Autowired
    private WasabiCardService wasabiCardService;
    @Autowired
    private UcardApiService ucardApiService;

    /**
     * 商户用户注册
     */
    @PostMapping("/merchant/user/register")
    @FeignClientAnnotation
    public BaseResultDto registerMerchantUser(@RequestBody CardMerchantUserEntity request, InfoEntity entity, HttpServletRequest servletRequest) throws ServiceException {
        String ip = GetIpAndMac.getIp(servletRequest);
        request.setIpAddress(ip);
        request.setCustomId(entity.getCustomInfo().getId());
        request.setCId(entity.getCustomInfo().getCId());
        request.setOperateUser(entity.getCustomInfo().getCId());
        return wasabiCardService.registerMerchantUser(request);
    }

    /**
     * 更新商户用户信息
     */
    @PostMapping("/merchant/user/update")
    @FeignClientAnnotation
    public BaseResultDto updateMerchantUser(@RequestBody CardMerchantUserEntity request, InfoEntity entity) throws ServiceException {
        request.setCustomId(entity.getCustomInfo().getId());
        request.setCId(entity.getCustomInfo().getCId());
        request.setOperateUser(entity.getCustomInfo().getCId());
        return wasabiCardService.updateMerchantUser(request);
    }

    /**
     * 更新用户邮寄地址
     */
    @PostMapping("/merchant/user/address/update")
    @FeignClientAnnotation
    public BaseResultDto updateUserMailingAddress(@RequestBody WasabiSaveUserMailingAddressEntity request) throws ServiceException {
        return wasabiCardService.updateUserMailingAddress(request);
    }

    /**
     * 查看用户详情
     */
    @PostMapping("/merchant/user/single")
    @FeignClientAnnotation
    public BaseResultDto merchantUserSingle(InfoEntity infoEntity) throws Exception {
        return wasabiCardService.merchantUserSingle(infoEntity.getCustomInfo().getId());
    }

    /**
     * 用户获取卡片类型
     */
    @PostMapping("/card/types/list")
    @FeignClientAnnotation
    public BaseResultDto getCardTypeList(@RequestBody CardTypeEntity entity, InfoEntity infoEntity) throws ServiceException {
        entity.setCId(infoEntity.getCustomInfo().getCId());
        return BaseResultDto.success(wasabiCardService.getCardTypeList(entity));
    }

    /**
     * 申请开卡
     */
    @PostMapping("/card/apply")
    @FeignClientAnnotation
    public BaseResultDto applyCard(@RequestBody CardApplyEntity request, InfoEntity infoEntity) throws Exception {
        request.setCId(infoEntity.getCustomInfo().getCId());
        return wasabiCardService.applyCard(request);
    }

    /**
     * 获取卡片申请列表
     */
    @PostMapping("/card/apply/page")
    @FeignClientAnnotation
    public ResultWithPagerDto<CardApplyDto> getCardApplyPageList(@RequestBody CardApplyPageEntity entity) throws ServiceException {
        return ucardService.getCardApplyPageList(entity);
    }

    /**
     * 获取卡片申请详情
     */
    @PostMapping("/card/apply/details")
    @FeignClientAnnotation
    public BaseResultDto cardApplyDetails(@RequestBody BaseEntity entity) throws ServiceException {
        return wasabiCardService.cardApplyDetails(entity);
    }

    /**
     * 查询开卡进度
     */
    @PostMapping("/card/apply/progress")
    @FeignClientAnnotation
    public BaseResultDto queryApplyProgress(@RequestBody CardApplyProgressEntity request) throws Exception {
        return wasabiCardService.queryApplyProgress(request);
    }

    /**
     * 获取我的卡片列表
     */
    @PostMapping("/card/list")
    @FeignClientAnnotation
    public BaseResultDto getCardList(@RequestBody CardPageEntity entity, InfoEntity infoEntity) throws ServiceException {
        entity.setCId(infoEntity.getCustomInfo().getCId());
        return BaseResultDto.success(ucardApiService.getCardList(entity));
    }

    /**
     * 获取卡片详情
     */
    @PostMapping("/card/single")
    @FeignClientAnnotation
    public BaseResultDto getCardSingle(@RequestBody SingleLongEntity entity) throws ServiceException {
        return BaseResultDto.success(ucardApiService.getCardSingle(entity));
    }

    /**
     * 银行卡激活
     */
    @PostMapping("/card/activate")
    @FeignClientAnnotation
    public BaseResultDto<WasabiActiveCardDto> activateCard(@RequestBody WasabiActiveCardEntity request,
                                                           InfoEntity infoEntity) throws ServiceException {
        request.setCId(infoEntity.getCustomInfo().getCId());
        return wasabiCardService.activeCard(request);
    }

    /**
     * 银行卡充值
     */
    @PostMapping("/card/recharge")
    @FeignClientAnnotation
    public BaseResultDto rechargeCard(@RequestBody WasabiBalanceEntity request, InfoEntity infoEntity) throws ServiceException {
        request.setCId(infoEntity.getCustomInfo().getCId());
        return wasabiCardService.cardDeposit(request);
    }

    /**
     * 查询用户钱包余额
     */
    @PostMapping("/card/wallet/balance")
    @FeignClientAnnotation
    public BaseResultDto<WalletBalanceDto> walletBalance(@RequestBody WalletBalanceEntity request) throws ServiceException {
        return wasabiCardService.walletBalance(request);
    }

    /**
     * 获取用户钱包记录列表
     */
    @PostMapping("/card/wallet/record/page")
    @FeignClientAnnotation
    public ResultWithPagerDto<CardWalletRecordDto> getCardWalletRecordPageList(@RequestBody CardWalletRecordPageEntity entity) throws ServiceException {
        return wasabiCardService.getCardWalletRecordPageList(entity);
    }

    /**
     * 充值记录分页查询
     */
    @PostMapping("/card/recharge/page")
    @FeignClientAnnotation
    public BaseReportResultDto<List<CardRechargeOrderDto>, CardRechargeOrderSumDto> queryRechargePageList(@RequestBody CardRechargePageEntity entity) throws ServiceException {
        return ucardService.queryRechargePageList(entity);
    }

    /**
     * 查询卡片余额
     */
    @PostMapping("/card/balance")
    @FeignClientAnnotation
    public BaseResultDto<WasabiBalanceInfoDto> queryCardBalance(@RequestBody WasabiBalanceInfoEntity request) throws ServiceException {
        return wasabiCardService.getBalanceInfo(request);
    }

    /**
     * 查询充值订单
     */
    @PostMapping("/card/recharge/order")
    @FeignClientAnnotation
    public BaseResultDto queryRechargeOrder(@RequestBody CardRechargeOrderQueryEntity request) throws Exception {
        return wasabiCardService.queryDepositOrder(request);
    }


    /**
     * 冻结卡片
     */
    @PostMapping("/card/freeze")
    @FeignClientAnnotation
    public BaseResultDto freezeCard(@RequestBody WasabiFreezeAndUnfreezeEntity request, InfoEntity infoEntity) throws Exception {
        request.setCId(infoEntity.getCustomInfo().getCId());
        return wasabiCardService.cardFreeze(request);
    }

    /**
     * 解冻卡片
     */
    @PostMapping("/card/unfreeze")
    @FeignClientAnnotation
    public BaseResultDto unfreezeCard(@RequestBody WasabiFreezeAndUnfreezeEntity request, InfoEntity infoEntity) throws Exception {
        request.setCId(infoEntity.getCustomInfo().getCId());
        return wasabiCardService.cardUnfreeze(request);
    }

    /**
     * 找回密码
     */
    @PostMapping("/card/password/reset")
    @FeignClientAnnotation
    public BaseResultDto<WasabiUpdatePinDto> resetPassword(@RequestBody WasabiUpdatePinEntity entity,
                                                           InfoEntity infoEntity) throws ServiceException {
        entity.setCId(infoEntity.getCustomInfo().getCId());
        return wasabiCardService.updatePin(entity);
    }

    /**
     * 校验文件
     */
    @PostMapping("/validated/file")
    @FeignClientAnnotation
    public void validatedFile(@RequestParam(value = "file") MultipartFile file) throws ServiceException {
        wasabiCardService.validatedFile(file);
    }

    /**
     * 查询国家和城市
     */
    @PostMapping("/card/country")
    @FeignClientAnnotation
    public BaseResultDto getCardCountry(@RequestBody CardCountryEntity request) throws ServiceException {
        request.setCardCategory(2);
        return BaseResultDto.success(ucardService.getCardCountryList(request));
    }

    /**
     * 查询职业信息
     */
    @PostMapping("/card/occupation/list")
    @FeignClientAnnotation
    public BaseResultDto<List<WasabiOccupationsDto>> getOccupationList() throws ServiceException {
        return wasabiCardService.getOccupationList();
    }

    /**
     * 查询交易记录分页列表
     */
    @PostMapping("/card/transac/page")
    @FeignClientAnnotation
    public ResultWithPagerDto<CardTransacOrderDto> getTransactionPageList(@RequestBody CardTransacOrderPageEntity request) throws ServiceException {
        return ucardService.getTransacOrderPageList(request);
    }

    /**
     * 权限验证
     */
    @PostMapping("/card/permission/verify")
    @FeignClientAnnotation
    public BaseResultDto permissionVerify(InfoEntity entity) throws ServiceException {
        return wasabiCardService.permissionVerify(entity.getCustomInfo());
    }

    /**
     * 身份认证获取web链接
     * @param entity
     * @return
     */
    @PostMapping("/getWebsdkLink")
    public BaseResultDto getWebsdkLink(@RequestBody WebsdkLinkAliEntity entity, InfoEntity infoEntity) throws Exception {
        return wasabiCardService.getWebsdkLink(entity, infoEntity.getCustomInfo().getId());
    }

    /**
     * 扫码上传文件
     */
    @PostMapping("/scan/upload/file")
    @FeignClientAnnotation
    public BaseResultDto scanUpload(@RequestBody CardWebSocketEntity entity) throws ServiceException {
        return wasabiCardService.scanUpload(entity);
    }

    /**
     * 证件类型配置列表
     *
     * @param entity
     * @return
     */
    @PostMapping("/card/id/type/config/list")
    @FeignClientAnnotation
    public ResultWithPagerDto<CardIdTypeConfigDto> getCardIdTypeConfigList(@RequestBody CardIdTypeConfigPageEntity entity) throws ServiceException {
        return wasabiCardService.getCardIdTypeConfigList(entity);
    }

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

    /**
     * 查询CVV 发送短信验证码
     */
    @PostMapping("/query/cvv/send/code")
    @FeignClientAnnotation
    public void queryCvvSendCode(@RequestBody @Validated CardQueryCvvSendEmailCodeEntity entity) throws Exception {
        wasabiCardService.queryCvvSendCode(entity);
    }

    /**
     * 验证邮箱验证码查询cvv
     */
    @PostMapping("/query/cvv/verify/code")
    @FeignClientAnnotation
    public BaseResultDto queryCvvVerifyCode(@RequestBody @Validated CardQueryCvvVerifyEmailCodeEntity entity) throws Exception {
        return wasabiCardService.queryCvvVerifyCode(entity);
    }

    /**
     * 查询汇率
     */
    @PostMapping("/global/query/exchange/rate")
    @FeignClientAnnotation
    public BaseResultDto queryExchangeRate(@RequestBody GlobalQueryExchangeRateEntity entity) throws ServiceException {
        return BaseResultDto.success(wasabiCardService.queryExchangeRate(entity));
    }

    /**
     * 查询账户信息
     */
    @PostMapping("/card/account/dropdown")
    @FeignClientAnnotation
    public BaseResultDto cardAccountDropdown(@RequestBody CardAccountDropdownEntity entity) throws ServiceException {
        return wasabiCardService.cardAccountDropdown(entity);
    }

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

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

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

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

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

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

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

    /**
     * 速汇查询用户详情
     */
    @PostMapping("/global/user/details")
    @FeignClientAnnotation
    public BaseResultDto findGlobalUserDetails(InfoEntity entity) throws ServiceException {
        return wasabiCardService.findGlobalUserDetails(entity);
    }

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

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

    /**
     * 区块链配置下拉列表
     */
    @PostMapping("/card/blockchain/config/dropdown")
    @FeignClientAnnotation
    BaseResultDto getCardBlockchainConfigDropdown(@RequestBody CardBlockchainConfigDropdownEntity entity) throws ServiceException {
        return wasabiCardService.getCardBlockchainConfigDropdown(entity);
    }

    /**
     * 加密货币交易分页列表
     */
    @PostMapping("/encrypted/wallet/transaction/page")
    @FeignClientAnnotation
    ResultWithPagerDto<EncryptedWalletTransactionDto> getEncryptedWalletTransactionPage(@RequestBody EncryptedWalletTransactionPageEntity entity) throws ServiceException{
        return wasabiCardService.getEncryptedWalletTransactionPage(entity);
    }

    /**
     * 卡扣款分页列表
     */
    @PostMapping("/card/withdraw/page")
    @FeignClientAnnotation
    ResultWithPagerDto<CardWithdrawDto> queryCardWithdrawPage(@RequestBody CardWithdrawPageEntity entity) throws ServiceException{
        return wasabiCardService.queryCardWithdrawPage(entity);
    }

}
