package com.crm.ucard.controller;

import com.crm.rely.backend.core.constant.Constants;
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.*;
import com.crm.rely.backend.core.entity.base.SingleLongEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.ucard.*;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.ucard.service.UcardApiService;
import com.crm.ucard.service.UcardCardService;
import com.crm.ucard.service.UcardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/ucard/api")
public class UcardApiController {

    @Autowired
    private UcardApiService ucardApiService;
    @Autowired
    private UcardService ucardService;
    @Autowired
    private UcardCardService ucardCardService;

    /**
     * 获取卡片类型列表
     */
    @PostMapping("/card/types/page")
    public ResultWithPagerDto<CardTypeDto> getCardTypePageList(@RequestBody CardTypePageEntity entity) throws ServiceException {
        entity.setEnableStatus(1);
        return ucardService.getCardTypePageList(entity);
    }

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

    /**
     * 商户用户注册
     */
    @PostMapping("/merchant/user/register")
    public BaseResultDto registerMerchantUser(@RequestBody MerchantUserEntity request) throws ServiceException {
        return ucardService.registerMerchantUser(request);
    }

    /**
     * 更新商户用户信息
     */
    @PostMapping("/merchant/user/update")
    public BaseResultDto updateMerchantUser(@RequestBody MerchantUserEntity request) throws ServiceException {
        return ucardService.updateMerchantUser(request);
    }

    /**
     * 查看商户用户信息
     */
    @PostMapping("/merchant/user/single")
    public BaseResultDto merchantUserSingle(InfoEntity entity) throws ServiceException {
        return BaseResultDto.success(ucardApiService.merchantUserSingle(entity.getCustomInfo().getCId()));
    }

    /**
     * 获取kyc认证分页列表
     */
    @PostMapping("/merchant/kyc/page")
    ResultWithPagerDto<CardKycStatusDto> getKycPageList(@RequestBody CardKycStatusPageEntity entity) throws ServiceException{
        return ucardService.getKycPageList(entity);
    }

    /**
     * 上传KYC附件
     */
    @PostMapping("/merchant/kyc/upload")
    public BaseResultDto uploadKycAttachment(@RequestBody KycAttachmentUploadEntity request) throws ServiceException {
        return ucardService.uploadKycAttachment(request);
    }

    /**
     * 提交KYC认证"
     */
    @PostMapping("/merchant/kyc/submit")
    public BaseResultDto submitKyc(@RequestBody KycEntity request) throws ServiceException {
        return ucardService.submitKyc(request);
    }

    /**
     * 查询KYC认证状态
     */
    @PostMapping("/merchant/kyc/status")
    public BaseResultDto<KycStatusDto> queryKycStatus(@RequestBody KycEntity entity) throws ServiceException {
        return ucardService.queryKycStatus(entity);
    }

    /**
     * 申请开卡
     */
    @PostMapping("/card/apply")
    public BaseResultDto applyCard(@RequestBody CardApplyEntity request) throws ServiceException {
        return ucardService.applyCard(request);
    }

    /**
     * 查询开卡进度
     */
    @PostMapping("/card/apply/progress")
    public BaseResultDto<CardApplyProgressDto> queryApplyProgress(@RequestBody CardApplyProgressEntity request) throws ServiceException {
        return ucardService.queryApplyProgress(request);
    }

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

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

    /**
     * 银行卡激活
     */
    @PostMapping("/card/activate")
    public BaseResultDto<CardActivateDto> activateCard(@RequestBody CardActivateEntity request) throws ServiceException {
        return ucardService.activateCard(request);
    }

    /**
     * 查询充值预估到账金额
     */
    @PostMapping("/card/recharge/estimate")
    public BaseResultDto<CardRechargeEstimateDto> queryRechargeEstimate(@RequestBody CardRechargeEstimateEntity request) throws ServiceException {
        return ucardService.queryRechargeEstimate(request);
    }

    /**
     * 银行卡充值
     */
    @PostMapping("/card/recharge")
    public BaseResultDto<CardRechargeDto> rechargeCard(@RequestBody CardRechargeEntity request) throws ServiceException {
        return ucardService.rechargeCard(request);
    }

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

    /**
     * 查询卡片余额
     */
    @PostMapping("/card/balance")
    public BaseResultDto<List<CardBalanceDto>> queryCardBalance(@RequestBody CardBalanceEntity request) throws ServiceException {
        return ucardService.queryCardBalance(request);
    }

    /**
     * 冻结卡片
     */
    @PostMapping("/card/freeze")
    public BaseResultDto<CardFreezeDto> freezeCard(@RequestBody CardOperateEntity request) throws ServiceException {
        return ucardService.freezeCard(request);
    }

    /**
     * 解冻卡片
     */
    @PostMapping("/card/unfreeze")
    public BaseResultDto<CardFreezeDto> unfreezeCard(@RequestBody CardOperateEntity request) throws ServiceException {
        return ucardService.unfreezeCard(request);
    }

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

    /**
     * 获取交易纪录详情
     */
    @PostMapping("/card/transac/single")
    public BaseResultDto getTransactionSingle(@RequestBody SingleLongEntity entity) throws ServiceException {
        return BaseResultDto.success(ucardApiService.getTransactionSingle(entity));
    }

    /**
     * 查询速汇银行及相关配置
     */
    @PostMapping("/transfer/banks")
    public BaseResultDto<List<GlobalTransferBankDto>> queryBankList() throws ServiceException {
        return ucardService.queryBankList();
    }

    /**
     * 查询法币汇率
     */
    @PostMapping("/transfer/rate")
    public BaseResultDto<GlobalExchangeRateDto> queryExchangeRate(@RequestBody GlobalExchangeRateEntity request) throws ServiceException {
        return ucardService.queryExchangeRate(request);
    }

    /**
     * 代付校验
     */
    @PostMapping("/transfer/validate")
    public BaseResultDto validateTransfer(@RequestBody GlobalPaymentEntity request) throws ServiceException {
        return ucardService.validateTransfer(request);
    }

    /**
     * 代付付款人校验
     */
    @PostMapping("/transfer/validate/payer")
    public BaseResultDto validatePayer(@RequestBody GlobalPayerEntity request) throws ServiceException {
        return ucardService.validatePayer(request);
    }

    /**
     * 代付收款人校验
     */
    @PostMapping("/transfer/validate/payee")
    public BaseResultDto validatePayee(@RequestBody GlobalPayeeEntity request) throws ServiceException {
        return ucardService.validatePayee(request);
    }

    /**
     * 代付
     */
    @PostMapping("/transfer")
    public BaseResultDto<GlobalPaymentDto> transfer(@RequestBody GlobalPaymentEntity request) throws ServiceException {
        return ucardService.transfer(request);
    }

    /**
     * 用户代付订单分页查询
     */
    @PostMapping("/transfer/page")
    public ResultWithPagerDto<GlobalPaymentOrderDto> queryOrderPageList(@RequestBody GlobalPaymentPageEntity request) throws ServiceException {
        return ucardService.queryOrderPageList(request);
    }

    /**
     * 获取用户代付订单详情
     */
    @PostMapping("/transfer/single")
    public BaseResultDto getTransferPaymentSingle(@RequestBody SingleLongEntity entity) throws ServiceException {
        return BaseResultDto.success(ucardApiService.getTransferPaymentSingle(entity));
    }

    /**
     * 提交调单信息或文件
     */
    @PostMapping("/transfer/dispute")
    public BaseResultDto submitDispute(@RequestBody GlobalSubmitDisputeEntity request) throws ServiceException {
        return ucardService.submitDispute(request);
    }

    /**
     * 查询代付订单结果
     */
    @PostMapping("/transfer/order/result")
    public BaseResultDto<GlobalOrderInfoDto> queryOrderResult(@RequestBody GlobalOrderQueryEntity request) throws ServiceException {
        return ucardService.queryOrderResult(request);
    }

    /**
     * 上传文件
     */
    @PostMapping("/upload/file")
    public BaseResultDto<String> uploadFile(@RequestParam(value = "file") MultipartFile file) throws ServiceException {
        return BaseResultDto.success(Constants.SUCCESS,ucardService.uploadFile(file));
    }

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

}
