package com.crm.ucard.controller;

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

import java.util.List;

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

    @Autowired
    private UcardService ucardService;

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

    /**
     * 更新卡片类型列表
     */
    @PostMapping("/card/types")
    @Transactional(rollbackFor = Exception.class)
    //@FeignClientAnnotation
    public BaseResultDto getCardTypes() throws ServiceException {
        //ucardService.getCardTypes();
//        wasabiCardService.getCardTypeList();
        return BaseResultDto.success();
    }

    /**
     * 商户用户分页列表
     */
    @PostMapping("/merchant/user/page")
    @FeignClientAnnotation
    public ResultWithPagerDto<MerchantUserDto> getMerchantUserPageLists(@RequestBody MerchantUserPageEntity entity) throws ServiceException {
        return ucardService.getMerchantUserPageList(entity);
    }

    /**
     * 查询商户账户信息
     */
    @PostMapping("/merchant/account")
    @FeignClientAnnotation
    public BaseResultDto<List<MerchantAccountDto>> queryMerchantAccount() throws ServiceException {
        return ucardService.queryMerchantAccount();
    }

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

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

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

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

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

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

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

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

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

    /**
     * 获取卡片列表
     */
    @PostMapping("/card/page")
    @FeignClientAnnotation
    public ResultWithPagerDto<UcardCardDto> getCardPageList(@RequestBody CardPageEntity entity) throws ServiceException {
        return ucardService.getCardPageList(entity);
    }

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

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

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

    /**
     * 查询充值订单
     */
    @PostMapping("/card/recharge/order")
    @FeignClientAnnotation
    public BaseResultDto<CardRechargeOrderDto> queryRechargeOrder(@RequestBody CardRechargeOrderQueryEntity request) throws ServiceException {
        return ucardService.queryRechargeOrder(request);
    }

    /**
     * 查询充值记录
     */
    @PostMapping("/card/recharge/records")
    @FeignClientAnnotation
    public BaseResultDto<CardRechargeOrderListDto> queryRechargeRecords(@RequestBody CardRechargeRecordQueryEntity request) throws ServiceException {
        return ucardService.queryRechargeRecords(request);
    }

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

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

    /**
     * 找回密码
     */
    @PostMapping("/card/password/reset")
    @FeignClientAnnotation
    public BaseResultDto resetPassword(@RequestBody CardOperateEntity request) throws ServiceException {
        return ucardService.resetPassword(request);
    }

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

    /**
     * 查询交易记录
     */
    @PostMapping("/card/transac")
    @FeignClientAnnotation
    public BaseResultDto<CardTransacOrderListDto> queryTransactions(@RequestBody CardTransacQueryEntity request) throws ServiceException {
        return ucardService.queryTransactions(request);
    }

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

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

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

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

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

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

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

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

    /**
     * 用户订单列表
     */
    @PostMapping("/transfer/orders")
    @FeignClientAnnotation
    public BaseResultDto<GlobalPaymentOrderListDto> queryOrderList(@RequestBody GlobalTransferOrderEntity request) throws ServiceException {
        return ucardService.queryOrderList(request);
    }

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

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

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

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

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


    /**
     * 银行卡交易信息数据定时同步查询
     */
//    @Scheduled(cron = "0 0 1 * * ?")
    public void queryOrderInfo() throws ServiceException {

        log.info("银行卡交易信息数据定时同步开始: {}.............................");
        try {
            ucardService.queryOrderInfo();
        } catch (Exception e) {
            log.error("定时任务执行失败", e);
        }
    }

}
