package com.crm.ucard.service.impl;

import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.ucard.*;
import com.crm.rely.backend.core.entity.ucard.*;
import com.crm.rely.backend.core.enums.GlobalPayoutMethodEnum;
import com.crm.rely.backend.core.enums.GlobalTransferTypeEnum;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.DateUtil;
import com.crm.ucard.dao.mapper.*;
import com.crm.ucard.service.UcardExportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UcardExportServiceImpl implements UcardExportService {
    @Autowired
    private CardApplyMapper cardApplyMapper;
    @Autowired
    private CardWalletRecordMapper walletRecordMapper;
    @Autowired
    private CardOperateMapper operateMapper;
    @Autowired
    private CardKycStatusMapper kycStatusMapper;
    @Autowired
    private CardMerchantUserMapper cardMerchantUserMapper;
    @Autowired
    private UcardCardMapper cardMapper;
    @Autowired
    private CardRechargeOrderMapper rechargeOrderMapper;
    @Autowired
    private CardTransacOrderMapper cardTransacOrderMapper;
    @Autowired
    private CardGlobalOrderMapper globalOrderMapper;
    @Autowired
    private CardCryptoCallbackRecordMapper cryptoCallbackRecordMapper;
    @Autowired
    private CardWithdrawMapper withdrawMapper;
    @Autowired
    private Card3dsTransactionMapper card3dsTransactionMapper;

    /**
     * 用户信息导出
     */
    @Override
    public BaseResultDto<List<MerchantUserDto>> merchantUserListExport(MerchantUserPageEntity entity) throws ServiceException {
        entity.setPage(null);
        List<MerchantUserDto> dtos = cardMerchantUserMapper.pageList(entity);
        return BaseResultDto.success(dtos);
    }

    /**
     * kyc 认证列表导出
     */
    @Override
    public BaseResultDto<List<CardKycStatusDto>> merchantKycListExport(CardKycStatusPageEntity entity) throws ServiceException {
        entity.setPage(null);
        List<CardKycStatusDto> dtos = kycStatusMapper.pageList(entity);
        return BaseResultDto.success(dtos);
    }

    /**
     * 卡片申请列表导出
     */
    @Override
    public BaseResultDto<List<CardApplyDto>> cardApplyListExport(CardApplyPageEntity entity) throws ServiceException {
        entity.setPage(null);
        /**
         * 格式化时间
         */
        if (entity.getEndDate() != null) {
            entity.setEndDate(DateUtil.operationDay(entity.getEndDate(), 1));
        }

        List<CardApplyDto> cardlist = cardApplyMapper.pageList(entity);

        return BaseResultDto.success(cardlist);
    }

    /**
     * 卡片列表导出
     */
    @Override
    public BaseResultDto<List<UcardCardDto>> cardListExport(CardPageEntity entity) throws ServiceException {
        entity.setPage(null);
        List<UcardCardDto> cardlist = cardMapper.pageList(entity);
        return BaseResultDto.success(cardlist);
    }

    /**
     * 交易记录列表导出
     */
    @Override
    public BaseResultDto<List<CardTransacOrderDto>> cardTransacRecordExport(CardTransacOrderPageEntity entity) throws ServiceException {
        entity.setPage(null);
        List<CardTransacOrderDto> dtos = cardTransacOrderMapper.pageList(entity);
        return BaseResultDto.success(dtos);
    }

    /**
     * 充值记录导出
     */
    @Override
    public BaseResultDto<List<CardRechargeOrderDto>> cardRechargeListExport(CardRechargePageEntity entity) throws ServiceException {
        entity.setPage(null);
        /**
         * 格式化时间
         */
        if (entity.getEndDate() != null) {
            entity.setEndDate(DateUtil.operationDay(entity.getEndDate(), 1));
        }
        List<CardRechargeOrderDto> dtos = rechargeOrderMapper.pageList(entity);
        return BaseResultDto.success(dtos);
    }

    /**
     * 钱包余额记录列表导出
     */
    @Override
    public BaseResultDto<List<CardWalletRecordDto>> cardWalletRecordExport(CardWalletRecordPageEntity entity) throws ServiceException {
        entity.setPage(null);
        /**
         * 格式化时间
         */
        if (entity.getEndDate() != null) {
            entity.setEndDate(DateUtil.operationDay(entity.getEndDate(), 1));
        }
        List<CardWalletRecordDto> dtos = walletRecordMapper.pageList(entity);
        return BaseResultDto.success(dtos);
    }

    /**
     * 操作记录列表导出
     */
    @Override
    public BaseResultDto<List<CardOperateDto>> cardOperateRecordExport(CardOperatePageEntity entity) throws ServiceException {
        entity.setPage(null);
        /**
         * 格式化时间
         */
        if (entity.getEndDate() != null) {
            entity.setEndDate(DateUtil.operationDay(entity.getEndDate(), 1));
        }
        List<CardOperateDto> dtos = operateMapper.pageList(entity);
        return BaseResultDto.success(dtos);
    }

    /**
     * 速汇订单导出
     */
    @Override
    public BaseResultDto<List<GlobalOrderDto>> globalOrderListExport(GlobalOrderPageEntity entity) throws ServiceException {
        entity.setPage(null);
        /**
         * 格式化时间
         */
        if (entity.getEndDate() != null) {
            entity.setEndDate(DateUtil.operationDay(entity.getEndDate(), 1));
        }
        List<GlobalOrderDto> dtos = globalOrderMapper.pageList(entity);
        if (CollectionUtils.isNotEmpty(dtos)) {
            dtos.forEach(dto -> {
                dto.setPayoutMethod(GlobalPayoutMethodEnum.matchRemark(dto.getPayoutMethod()));
                dto.setTransferType(GlobalTransferTypeEnum.matchType(dto.getTransferType()));
            });
        }
        return BaseResultDto.success(dtos);
    }

    /**
     * 3ds列表导出
     */
    @Override
    public BaseResultDto<List<Card3dsTransactionDto>> card3dsTransactionListExport(Card3dsTransactionPageEntity entity) throws ServiceException {
        entity.setPage(null);
        List<Card3dsTransactionDto> dtos = card3dsTransactionMapper.pageList(entity);
        return BaseResultDto.success(dtos);
    }

    /**
     * 卡扣款列表导出
     */
    @Override
    public BaseResultDto<List<CardWithdrawDto>> cardWithdrawListExport(CardWithdrawPageEntity entity) throws ServiceException {
        entity.setPage(null);
        List<CardWithdrawDto> dtos = withdrawMapper.pageList(entity);
        return BaseResultDto.success(dtos);
    }

    /**
     * 加密货币交易分页列表导出
     */
    @Override
    public BaseResultDto<List<UcardWalletDepositTransactionDto>> encryptedWalletTransactionListExport(UcardWalletDepositTransactionPageEntity entity) throws ServiceException {
        entity.setPage(null);
        /**
         * 格式化时间
         */
        if (entity.getEndDate() != null) {
            entity.setEndDate(DateUtil.operationDay(entity.getEndDate(), 1));
        }
        List<UcardWalletDepositTransactionDto> dtos = cryptoCallbackRecordMapper.pageList(entity);
        return BaseResultDto.success(dtos);

    }

}
