package com.crm.manager.controller;

import com.crm.manager.dto.export.EncryptedWalletTransactionListExportDto;
import com.crm.manager.service.ucard.UcardEncryptedFeignService;
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.UcardWalletDepositTransactionDto;
import com.crm.rely.backend.core.entity.ucard.UcardWalletDepositTransactionPageEntity;
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.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/encrypted")
public class UcardEncryptedController {

    @Autowired
    private UcardEncryptedFeignService ucardEncryptedFeignService;

    /**
     * 加密货币交易分页列表
     */
    @PostMapping("/wallet/transaction/page")
    ResultWithPagerDto<UcardWalletDepositTransactionDto> getWalletDepositTransactionPage(@RequestBody UcardWalletDepositTransactionPageEntity entity) throws ServiceException{
        return ucardEncryptedFeignService.getWalletDepositTransactionPage(entity);
    }

    /**
     * 加密货币交易记录导出
     */
    @PostMapping("/wallet/transaction/list/export")
    public void encryptedWalletTransactionListExport(@RequestBody UcardWalletDepositTransactionPageEntity entity, HttpServletResponse response) throws Exception {
        BaseResultDto<List<UcardWalletDepositTransactionDto>> resultDto = ucardEncryptedFeignService.encryptedWalletTransactionListExport(entity);
        if (resultDto != null && resultDto.getCode() == 200) {
            List<EncryptedWalletTransactionListExportDto> dtos = Lists.newArrayList();
            if (CollectionUtils.isNotEmpty(resultDto.getData())) {
                resultDto.getData().forEach((item) -> {
                    EncryptedWalletTransactionListExportDto dto = new EncryptedWalletTransactionListExportDto();
                    BeanUtils.copyProperties(item, dto);
                    dto.setCID(item.getCId());
                    dto.setName(NameUtil.getName(item.getCountry(), item.getLastName(), null, item.getFirstName()));
                    dto.setTokenName(item.getTokenName());
                    item.setAreaCode(item.getAreaCode() == null ? "" : item.getAreaCode());
                    item.setMobile(item.getMobile() == null ? "" : item.getMobile());
                    dto.setMobile(item.getAreaCode() + " " + item.getMobile());
                    dtos.add(dto);
                });
            }
            ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("ENCRYPTED_WALLET_TRANSACTION_LIST_EXPORT"), dtos, EncryptedWalletTransactionListExportDto.class, response);
        }
    }

}
