package com.crm.manager.controller;

import com.crm.manager.dto.export.*;
import com.crm.manager.entity.excel.DailyVolumeImportEntity;
import com.crm.manager.entity.excel.TradeDwImportEntity;
import com.crm.manager.service.SelesPerformanceService;
import com.crm.manager.service.SelesRewardWorthStatisticsService;
import com.crm.manager.service.TradeService;
import com.crm.manager.service.ib.TradeFeignService;
import com.crm.manager.service.ib.TradeImportFeignService;
import com.crm.manager.service.ib.TradeShardingFeignService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.constant.FeignClientAnnotation;
import com.crm.rely.backend.core.constant.IbReportAnnotation;
import com.crm.rely.backend.core.constant.PrefixEnum;
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.ib.trade.*;
import com.crm.rely.backend.core.entity.base.BaseIbReportListEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.ib.trade.*;
import com.crm.rely.backend.core.entity.sales.SalesPerformanceListEntity;
import com.crm.rely.backend.service.MqSendService;
import com.crm.rely.backend.util.DateUtil;
import com.crm.rely.backend.util.EasyExcelUti;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.FileProcessUtil;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by max on 2020/7/14.
 */
@RestController
@RequestMapping("/trade")
public class TradeReportController {

    private final Logger logger = LoggerFactory.getLogger(TradeReportController.class);

    @Autowired
    private TradeFeignService tradeFeignService;

    @Autowired
    private TradeService tradeService;

    @Autowired
    private TradeShardingFeignService tradeShardingFeignService;

    @Autowired
    private TradeImportFeignService tradeImportFeignService;

    @Autowired
    private SelesRewardWorthStatisticsService selesRewardWorthStatisticsService;

    @Autowired
    private SelesPerformanceService selesPerformanceService;

    @Autowired
    private MqSendService mqSendService;

    @Value("${web.export-path}")
    private String exportPath;


    /**
     * 交易历史
     *
     * @param entity
     * @param infoEntity
     * @throws Exception
     */
    @PostMapping("/history")
    public BaseReportResultDto<List<TradeHistoryDto>, TradeHistorySumDto> tradeHistory(@RequestBody TradeHistoryListEntity entity, InfoEntity infoEntity) throws Exception {
        return tradeFeignService.tradeHistory(entity);
    }

    @GetMapping("/history/export")
    public void tradeHistoryExport(@RequestParam(name = "login") Long login, @RequestParam(name = "ibNo") String ibNo, @RequestParam(name = "cId") Long cId, @RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate, @RequestParam(name = "platform") String platform, @RequestParam(name = "groupType") Long groupType, InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        TradeHistoryListEntity entity = new TradeHistoryListEntity();
        entity.setLogin(login);
        entity.setIbNo(ibNo);
        entity.setCId(cId);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);
        entity.setGroupType(groupType);

        BaseResultDto<List<TradeHistoryDto>> resultDto = tradeFeignService.tradeHistoryExport(entity);
        if (resultDto.getCode() == 200) {
            List<TradeHistoryExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                TradeHistoryExportDto dto = new TradeHistoryExportDto();
                BeanUtils.copyProperties(item, dto);
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(genFileName("HISTORY"), dtos, TradeHistoryExportDto.class, response);
        }
    }

    /**
     * 持仓查询
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/position")
    public BaseReportResultDto<List<TradePositionDto>, TradePositionSumDto> tradePosition(@RequestBody TradePositionListEntity entity, InfoEntity infoEntity) throws Exception {

        return tradeFeignService.tradePosition(entity);
    }


    @GetMapping("/position/export")
    public void tradePositionExport(@RequestParam(name = "login") Long login, @RequestParam(name = "ibNo") String ibNo, @RequestParam(name = "cId") Long cId, @RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate, @RequestParam(name = "platform") String platform, @RequestParam(name = "agentId") Long agentId, InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        TradePositionListEntity entity = new TradePositionListEntity();
        entity.setLogin(login);
        entity.setIbNo(ibNo);
        entity.setCId(cId);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);
        entity.setAgentId(agentId);

        BaseResultDto<List<TradePositionDto>> resultDto = tradeFeignService.tradePositionExport(entity);
        if (resultDto.getCode() == 200) {
            List<TradePositionExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                TradePositionExportDto dto = new TradePositionExportDto();
                BeanUtils.copyProperties(item, dto);
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(genFileName("POSITION"), dtos, TradePositionExportDto.class, response);
        }
    }

    /**
     * 挂单查询
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/pending")
    public BaseReportResultDto<List<TradePendingDto>, TradePendingSumDto> tradePending(@RequestBody TradePendingListEntity entity, InfoEntity infoEntity) throws Exception {

        return tradeFeignService.tradePending(entity);
    }

    @GetMapping("/pending/export")
    public void tradePendingExport(@RequestParam(name = "login") Long login, @RequestParam(name = "ibNo") String ibNo, @RequestParam(name = "cId") Long cId, @RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate, @RequestParam(name = "platform") String platform, @RequestParam(name = "agentId") Long agentId, InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        TradePendingListEntity entity = new TradePendingListEntity();
        entity.setLogin(login);
        entity.setIbNo(ibNo);
        entity.setCId(cId);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);
        entity.setAgentId(agentId);

        BaseResultDto<List<TradePendingDto>> resultDto = tradeFeignService.tradePendingExport(entity);
        if (resultDto.getCode() == 200) {
            List<TradePendingExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                TradePendingExportDto dto = new TradePendingExportDto();
                BeanUtils.copyProperties(item, dto);
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(genFileName("PENDING"), dtos, TradePendingExportDto.class, response);
        }
    }

    /**
     * 挂单历史
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/pending/history")
    public BaseReportResultDto<List<TradePendingHistoryDto>, TradePendingHistorySumDto> tradePendingHistory(@RequestBody TradePendingHistoryListEntity entity, InfoEntity infoEntity) throws Exception {

        return tradeFeignService.tradePendingHistory(entity);
    }

    @GetMapping("/pending/history/export")
    public void tradePendingHistoryExport(@RequestParam(name = "login") Long login, @RequestParam(name = "ibNo") String ibNo, @RequestParam(name = "cId") Long cId, @RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate, @RequestParam(name = "platform") String platform, @RequestParam(name = "agentId") Long agentId, InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        TradePendingHistoryListEntity entity = new TradePendingHistoryListEntity();
        entity.setLogin(login);
        entity.setIbNo(ibNo);
        entity.setCId(cId);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);
        entity.setAgentId(agentId);

        BaseResultDto<List<TradePendingHistoryDto>> resultDto = tradeFeignService.tradePendingHistoryExport(entity);
        if (resultDto.getCode() == 200) {
            List<TradePendingHistoryExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                TradePendingHistoryExportDto dto = new TradePendingHistoryExportDto();
                BeanUtils.copyProperties(item, dto);
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(genFileName("PENDING_HISTORY"), dtos, TradePendingHistoryExportDto.class, response);
        }
    }

    /**
     * 品种报表
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/symbols")
    public BaseReportResultDto<List<TradeSymbolDto>, TradeSymbolSumDto> tradeSymbol(@RequestBody TradeSymbolListEntity entity, InfoEntity infoEntity) throws Exception {
        return tradeFeignService.tradeSymbol(entity);
    }


    @GetMapping("/symbols/export")
    public void tradeSymbolExport(@RequestParam(name = "login") Long login, @RequestParam(name = "ibNo") String ibNo, @RequestParam(name = "cId") Long cId, @RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate, @RequestParam(name = "platform") String platform, @RequestParam(name = "lang") String lang, @RequestParam(name = "agentId") Long agentId, InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        TradeSymbolListEntity entity = new TradeSymbolListEntity();
        entity.setLogin(login);
        entity.setIbNo(ibNo);
        entity.setCId(cId);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);
        entity.setAgentId(agentId);

        BaseResultDto<List<TradeSymbolDto>> resultDto = tradeFeignService.tradeSymbolExport(entity);
        if (resultDto.getCode() == 200) {
            if (StringUtils.isEmpty(lang) || "cn".equalsIgnoreCase(lang)) {
                List<TradeSymbolExportDto> dtos = Lists.newArrayList();
                resultDto.getData().forEach((item) -> {
                    TradeSymbolExportDto dto = new TradeSymbolExportDto();
                    BeanUtils.copyProperties(item, dto);
                    dtos.add(dto);
                });
                ExportUtil.transferToResponse(genFileName("SYMBOLS"), dtos, TradeSymbolExportDto.class, response);
            } else {
                List<TradeSymbolExportEnDto> dtos = Lists.newArrayList();
                resultDto.getData().forEach((item) -> {
                    TradeSymbolExportEnDto dto = new TradeSymbolExportEnDto();
                    BeanUtils.copyProperties(item, dto);
                    dtos.add(dto);
                });
                ExportUtil.transferToResponse(genFileName("SYMBOLS"), dtos, TradeSymbolExportEnDto.class, response);
            }
        }
    }

    /**
     * 佣金报表
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/agent/commission")
    public BaseReportResultDto<List<TradeCommissionDto>, TradeCommissionSumDto> tradeCommission(@RequestBody BaseIbReportListEntity entity, InfoEntity infoEntity) throws Exception {
        return tradeFeignService.tradeCommission(entity);
    }

    @GetMapping("/agent/commission/export")
    public void tradeCommissionExport(@RequestParam(name = "login") Long login, @RequestParam(name = "ibNo") String ibNo, @RequestParam(name = "cId") Long cId, @RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate, @RequestParam(name = "platform") String platform, @RequestParam(name = "agentId") Long agentId, @RequestParam(name = "lang") String lang, InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        BaseIbReportListEntity entity = new BaseIbReportListEntity();
        entity.setLogin(login);
        entity.setIbNo(ibNo);
        entity.setCId(cId);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);
        entity.setAgentId(agentId);

        BaseResultDto<List<TradeCommissionDto>> resultDto = tradeFeignService.tradeCommissionExport(entity);
        if (resultDto.getCode() == 200) {
            if (StringUtils.isEmpty(lang) || "cn".equalsIgnoreCase(lang)) {
                List<TradeCommissionExportDto> dtos = Lists.newArrayList();
                resultDto.getData().forEach((item) -> {
                    TradeCommissionExportDto dto = new TradeCommissionExportDto();
                    BeanUtils.copyProperties(item, dto);
                    dto.setCID(item.getCId());
                    dtos.add(dto);
                });
                ExportUtil.transferToResponse(genFileName("AGENT_COMMISSION"), dtos, TradeCommissionExportDto.class, response);
            } else {
                List<TradeCommissionExportEnDto> dtos = Lists.newArrayList();
                resultDto.getData().forEach((item) -> {
                    TradeCommissionExportEnDto dto = new TradeCommissionExportEnDto();
                    BeanUtils.copyProperties(item, dto);
                    dto.setCID(item.getCId());
                    dtos.add(dto);
                });
                ExportUtil.transferToResponse(genFileName("AGENT_COMMISSION"), dtos, TradeCommissionExportEnDto.class, response);
            }
        }
    }


    @PostMapping("/agent/commission/import")
    public BaseResultDto<Object> tradeCommissionImport(@RequestParam(value = "file", required = false) MultipartFile file,
                                                       InfoEntity infoEntity, HttpServletRequest req) throws Exception {

        List<DailyVolumeImportEntity> entities = EasyExcelUti.readExcel(file, new DailyVolumeImportEntity(), 1, DailyVolumeImportEntity.class);
        List<DailyVolumeImportSaveEntity> saveEntities = Lists.newArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (DailyVolumeImportEntity entity : entities) {
            DailyVolumeImportSaveEntity saveEntity = new DailyVolumeImportSaveEntity();
            BeanUtils.copyProperties(entity, saveEntity);
            saveEntity.setLogin(Long.parseLong(entity.getLogin()));
            saveEntity.setRebateAmount(Float.parseFloat(entity.getRebateAmount()));
            saveEntity.setCommissionAmount(Float.parseFloat(entity.getCommissionAmount()));
            saveEntity.setOpenTime(sdf.parse(entity.getCloseTime()));
            saveEntity.setCloseTime(sdf.parse(entity.getCloseTime()));
            saveEntities.add(saveEntity);
        }
        tradeImportFeignService.dailyImport(saveEntities);

        return BaseResultDto.success(Constants.UPLOAD_SUCCESS);
    }

    /**
     * 交易账户
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/account")
    public BaseReportResultDto<List<TradeAccountDto>, TradeAccountSumDto> tradeAccount(@RequestBody BaseIbReportListEntity entity, InfoEntity infoEntity) throws Exception {
        return tradeFeignService.tradeAccount(entity);
    }

    @GetMapping("/account/export")
    public void tradeAccountExport(@RequestParam(name = "login") Long login, @RequestParam(name = "ibNo") String ibNo, @RequestParam(name = "cId") Long cId, @RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate, @RequestParam(name = "platform") String platform, @RequestParam(name = "lang") String lang, @RequestParam(name = "agentId") Long agentId, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        BaseIbReportListEntity entity = new BaseIbReportListEntity();
        entity.setLogin(login);
        entity.setIbNo(ibNo);
        entity.setCId(cId);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);
        entity.setAgentId(agentId);

        BaseResultDto<List<TradeAccountDto>> resultDto = tradeFeignService.tradeAccountExport(entity);
        if (resultDto.getCode() == 200) {
            if (StringUtils.isEmpty(lang) || "cn".equalsIgnoreCase(lang)) {
                List<TradeAccountExportDto> dtos = Lists.newArrayList();
                resultDto.getData().forEach((item) -> {
                    TradeAccountExportDto dto = new TradeAccountExportDto();
                    BeanUtils.copyProperties(item, dto);
                    dto.setCID(item.getCId());
                    dtos.add(dto);
                });
                ExportUtil.transferToResponse(genFileName("ACCOUNT"), dtos, TradeAccountExportDto.class, response);
            } else {
                List<TradeAccountExportEnDto> dtos = Lists.newArrayList();
                resultDto.getData().forEach((item) -> {
                    TradeAccountExportEnDto dto = new TradeAccountExportEnDto();
                    BeanUtils.copyProperties(item, dto);
                    dto.setCID(item.getCId());
                    dtos.add(dto);
                });
                ExportUtil.transferToResponse(genFileName("ACCOUNT"), dtos, TradeAccountExportEnDto.class, response);
            }

        }
    }

    @PostMapping("/account/export/file")
    public BaseResultDto<String> tradeAccountExportFile(@RequestBody BaseIbReportListEntity entity) throws Exception {
        entity.setPage(null);
        BaseResultDto<List<TradeAccountDto>> resultDto = tradeFeignService.tradeAccountExport(entity);
        if (resultDto.getCode() == 200) {
            List<TradeAccountExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                TradeAccountExportDto dto = new TradeAccountExportDto();
                BeanUtils.copyProperties(item, dto);
                dto.setCID(item.getCId());
                dtos.add(dto);
            });
            //转换文件
            try {
                String fileName = genFileName("ACCOUNT");
                ExportUtil.transferToFile(exportPath, fileName, dtos, TradeAccountExportDto.class);
                return BaseResultDto.success(fileName);
            } catch (Exception e) {
                logger.error(e.getMessage());
                return BaseResultDto.success();
            }

        } else {
            return BaseResultDto.success();
        }

    }


    /**
     * 存取款
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/dw")
    public BaseReportResultDto<List<TradeDwDto>, TradeDwSumDto> tradeDw(@RequestBody BaseIbReportListEntity entity, InfoEntity infoEntity) throws Exception {
        return tradeFeignService.tradeDw(entity);
    }

    @GetMapping("/dw/export")
    public void tradeDwExport(@RequestParam(name = "login") Long login, @RequestParam(name = "ibNo") String ibNo, @RequestParam(name = "cId") Long cId, @RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate, @RequestParam(name = "platform") String platform, @RequestParam(name = "groupType") Long groupType, @RequestParam(name = "agentId") Long agentId,
                              @RequestParam(name = "dwType") Integer dwType, @RequestParam(name = "lang") String lang, @RequestParam(name = "loginTypes") List<Integer> loginTypes, InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        BaseIbReportListEntity entity = new BaseIbReportListEntity();
        entity.setLogin(login);
        entity.setIbNo(ibNo);
        entity.setCId(cId);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);
        entity.setGroupType(groupType);
        entity.setAgentId(agentId);
        entity.setDwType((dwType == null) ? 1 : dwType);
        entity.setLoginTypes(loginTypes);
        if (PrefixEnum.PREFIX_FINANCE.equals(infoEntity.getPrefix())) {
            entity.setIsFinance(1);
        }
        BaseResultDto<List<TradeDwDto>> resultDto = tradeFeignService.tradeDwExport(entity);
        if (resultDto.getCode() == 200) {
            if (StringUtils.isEmpty(lang) || "cn".equalsIgnoreCase(lang)) {
                List<TradeDwExportDto> dtos = Lists.newArrayList();
                resultDto.getData().forEach((item) -> {
                    TradeDwExportDto dto = new TradeDwExportDto();
                    BeanUtils.copyProperties(item, dto);
                    dto.setCID(item.getCId());
                    dtos.add(dto);
                });
                ExportUtil.transferToResponse(genFileName("DEPOSIT_WITHDRAWAL"), dtos, TradeDwExportDto.class, response);
            } else {
                List<TradeDwExportEnDto> dtos = Lists.newArrayList();
                resultDto.getData().forEach((item) -> {
                    TradeDwExportEnDto dto = new TradeDwExportEnDto();
                    BeanUtils.copyProperties(item, dto);
                    dto.setCID(item.getCId());
                    dtos.add(dto);
                });
                ExportUtil.transferToResponse(genFileName("DEPOSIT_WITHDRAWAL"), dtos, TradeDwExportEnDto.class, response);
            }
        }
    }

    @PostMapping("/dw/import")
    public BaseResultDto<Object> dwImport(@RequestParam(value = "file", required = false) MultipartFile file,
                                          InfoEntity infoEntity, HttpServletRequest req) throws Exception {

        List<TradeDwImportEntity> entities = EasyExcelUti.readExcel(file, new TradeDwImportEntity(), 1, TradeDwImportEntity.class);
        List<TradeDwImportSaveEntity> saveEntities = Lists.newArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Set<String> closeDate = new HashSet();
        for (TradeDwImportEntity entity : entities) {
            TradeDwImportSaveEntity saveEntity = new TradeDwImportSaveEntity();
            BeanUtils.copyProperties(entity, saveEntity);
            saveEntity.setTicket(Long.parseLong(entity.getTicket()));
            saveEntity.setLogin(Long.parseLong(entity.getLogin()));
            saveEntity.setDeposit(Float.parseFloat(entity.getDeposit()));
            saveEntity.setWithdrawal(Float.parseFloat(entity.getWithdrawal()));
            saveEntity.setCredit(Float.parseFloat(entity.getCredit()));
            saveEntity.setCloseTime(sdf.parse(entity.getCloseTime()));
            saveEntities.add(saveEntity);

            closeDate.add(DateUtil.formatDate(saveEntity.getCloseTime()));
        }
        tradeImportFeignService.dwImport(saveEntities);

        mqSendService.send(Constants.TRADE_DW_IMPORT_PERFORMANCE, closeDate);

        return BaseResultDto.success(Constants.UPLOAD_SUCCESS);
    }

    /**
     * 佣金报表
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/agent/commission/conclude")
    public BaseReportResultDto<List<TradeCommissionDto>, TradeCommissionSumDto> tradeCommissionConclude(@RequestBody BaseIbReportListEntity entity, InfoEntity infoEntity) throws Exception {
        return tradeFeignService.tradeCommissionConclude(entity);
    }

    @GetMapping("/agent/commission/conclude/export")
    public void tradeCommissionConcludeExport(@RequestParam(name = "login") Long login,
                                              @RequestParam(name = "ibNo") String ibNo,
                                              @RequestParam(name = "cId") Long cId,
                                              @RequestParam(name = "startDate") String startDate,
                                              @RequestParam(name = "endDate") String endDate,
                                              @RequestParam(name = "platform") String platform,
                                              @RequestParam(name = "lang") String lang,
                                              @RequestParam(name = "agentId") Long agentId,
                                              InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        BaseIbReportListEntity entity = new BaseIbReportListEntity();
        entity.setLogin(login);
        entity.setIbNo(ibNo);
        entity.setCId(cId);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);
        entity.setAgentId(agentId);

        BaseResultDto<List<TradeCommissionDto>> resultDto = tradeFeignService.tradeCommissionExport(entity);
        if (resultDto.getCode() == 200) {
            if (StringUtils.isEmpty(lang) || "cn".equalsIgnoreCase(lang)) {
                List<TradeCommissionConcludeExportDto> dtos = Lists.newArrayList();
                resultDto.getData().forEach((item) -> {
                    TradeCommissionConcludeExportDto dto = new TradeCommissionConcludeExportDto();
                    BeanUtils.copyProperties(item, dto);
                    dto.setCID(item.getCId());
                    dto.setPIBNO(item.getPIbNo());
                    dtos.add(dto);
                });
                ExportUtil.transferToResponse(genFileName("AGENT_COMMISSION_CONCLUDE"), dtos, TradeCommissionConcludeExportDto.class, response);
            } else {
                List<TradeCommissionConcludeExportEnDto> dtos = Lists.newArrayList();
                resultDto.getData().forEach((item) -> {
                    TradeCommissionConcludeExportEnDto dto = new TradeCommissionConcludeExportEnDto();
                    BeanUtils.copyProperties(item, dto);
                    dto.setCID(item.getCId());
                    dto.setPIBNO(item.getPIbNo());
                    dtos.add(dto);
                });
                ExportUtil.transferToResponse(genFileName("AGENT_COMMISSION_CONCLUDE"), dtos, TradeCommissionConcludeExportEnDto.class, response);
            }

        }
    }

    /**
     * 交易量佣金报表
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/volume")
    public BaseReportResultDto<List<TradeVolumeDto>, TradeVolumeSumDto> tradeVolume(@RequestBody BaseIbReportListEntity entity, InfoEntity infoEntity) throws Exception {
        return tradeFeignService.tradeVolume(entity);
    }

    /**
     * 交易量佣金报表导出
     *
     * @param login
     * @param ibNo
     * @param cId
     * @param startDate
     * @param endDate
     * @param platform
     * @param infoEntity
     * @param response
     * @throws Exception
     */
    @GetMapping("/volume/export")
    public void tradeVolumeExport(@RequestParam(name = "login") Long login, @RequestParam(name = "ibNo") String ibNo, @RequestParam(name = "cId") Long cId, @RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate, @RequestParam(name = "platform") String platform, @RequestParam(name = "agentId") Long agentId, @RequestParam(name = "lang") String lang, InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        BaseIbReportListEntity entity = new BaseIbReportListEntity();
        entity.setLogin(login);
        entity.setIbNo(ibNo);
        entity.setCId(cId);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);
        entity.setAgentId(agentId);

        BaseResultDto<List<TradeVolumeDto>> resultDto = tradeFeignService.tradeVolumeExport(entity);
        if (resultDto.getCode() == 200) {
            if (StringUtils.isEmpty(lang) || "cn".equalsIgnoreCase(lang)) {
                List<TradeVolumeExportDto> dtos = Lists.newArrayList();
                if (!CollectionUtils.isEmpty(resultDto.getData())) {
                    resultDto.getData().forEach((item) -> {
                        TradeVolumeExportDto dto = new TradeVolumeExportDto();
                        BeanUtils.copyProperties(item, dto);
                        dto.setCID(item.getCId());
                        dtos.add(dto);
                    });
                    ExportUtil.transferToResponse(genFileName("VOLUME_COMMISSION"), dtos, TradeVolumeExportDto.class, response);
                }
            } else {
                List<TradeVolumeExportEnDto> dtos = Lists.newArrayList();
                if (!CollectionUtils.isEmpty(resultDto.getData())) {
                    resultDto.getData().forEach((item) -> {
                        TradeVolumeExportEnDto dto = new TradeVolumeExportEnDto();
                        BeanUtils.copyProperties(item, dto);
                        dto.setCID(item.getCId());
                        dtos.add(dto);
                    });
                    ExportUtil.transferToResponse(genFileName("VOLUME_COMMISSION"), dtos, TradeVolumeExportEnDto.class, response);
                }
            }

        }
    }


    /**
     * 销售佣金报表
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/sales/commission")
    public BaseReportResultDto<List<TradeSalesCommissionDto>, TradeSalesCommissionSumDto> tradeSalesCommission(@RequestBody BaseIbReportListEntity entity, InfoEntity infoEntity) throws Exception {
        return tradeFeignService.tradeSalesCommission(entity);
    }


    @GetMapping("/sales/commission/export")
    public void tradeSalesCommissionExport(@RequestParam(name = "login") Long login,
                                           @RequestParam(name = "salesNo") String salesNo,
                                           @RequestParam(name = "salesId") Long salesId,
                                           @RequestParam(name = "startDate") String startDate,
                                           @RequestParam(name = "endDate") String endDate,
                                           @RequestParam(name = "platform") String platform,
                                           @RequestParam(name = "lang") String lang,
                                           @RequestParam(name = "customType") Integer customType,
                                           InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        BaseIbReportListEntity entity = new BaseIbReportListEntity();
        entity.setLogin(login);
        entity.setSalesNo(salesNo);
        entity.setSalesId(salesId);
        entity.setCustomType(customType);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);

        BaseResultDto<List<TradeSalesCommissionDto>> resultDto = tradeFeignService.tradeSalesCommissionExport(entity);
        if (resultDto.getCode() == 200) {
            if (StringUtils.isEmpty(lang) || "cn".equalsIgnoreCase(lang)) {
                List<TradeSalesCommissionExportDto> dtos = Lists.newArrayList();
                resultDto.getData().forEach((item) -> {
                    TradeSalesCommissionExportDto dto = new TradeSalesCommissionExportDto();
                    BeanUtils.copyProperties(item, dto);
                    dtos.add(dto);
                });
                ExportUtil.transferToResponse(genFileName("SALES_COMMISSION"), dtos, TradeSalesCommissionExportDto.class, response);
            } else {
                List<TradeSalesCommissionExportEnDto> dtos = Lists.newArrayList();
                resultDto.getData().forEach((item) -> {
                    TradeSalesCommissionExportEnDto dto = new TradeSalesCommissionExportEnDto();
                    BeanUtils.copyProperties(item, dto);
                    dtos.add(dto);
                });
                ExportUtil.transferToResponse(genFileName("SALES_COMMISSION"), dtos, TradeSalesCommissionExportEnDto.class, response);
            }

        }
    }


    /**
     * 盈亏报表
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/profit/ranking")
    public BaseReportResultDto<List<TradeProfitRankingDto>, TradeProfitRankingSumDto> tradeProfitRanking(@RequestBody TradeProfitRankingEntity entity, InfoEntity infoEntity) throws Exception {
        return tradeFeignService.tradeProfitRanking(entity);
    }

    @GetMapping("/profit/ranking/export")
    public void tradeProfitRankingExport(@RequestParam(name = "login") Long login, @RequestParam(name = "ibNo") String ibNo, @RequestParam(name = "cId") Long cId, @RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate, @RequestParam(name = "platform") String platform, @RequestParam(name = "rankingType") Integer rankingType, @RequestParam(name = "groupType") Long groupType, InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        TradeProfitRankingEntity entity = new TradeProfitRankingEntity();
        entity.setLogin(login);
        entity.setIbNo(ibNo);
        entity.setCId(cId);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);
        entity.setRankingType(rankingType);
        entity.setGroupType(groupType);

        BaseResultDto<List<TradeProfitRankingDto>> resultDto = tradeFeignService.tradeProfitRankingExport(entity);
        if (resultDto.getCode() == 200) {
            List<TradeProfitRankingExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                TradeProfitRankingExportDto dto = new TradeProfitRankingExportDto();
                BeanUtils.copyProperties(item, dto);
                dto.setCID(item.getCId());
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(genFileName("RANKING"), dtos, TradeProfitRankingExportDto.class, response);
        }
    }

    /**
     * 销售股票佣金报表
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/sales/stock/commission")
    public BaseReportResultDto<List<TradeSalesStockCommissionDto>, TradeSalesStockCommissionSumDto> tradeStockCommission(@RequestBody TradeSalesStockCommissionEntity entity, InfoEntity infoEntity) throws Exception {
        return tradeFeignService.tradeSalesStockCommission(entity);
    }

    @GetMapping("/sales/stock/commission/export")
    public void tradeSalesStockCommissionExport(@RequestParam(name = "login") Long login, @RequestParam(name = "salesNo") String salesNo, @RequestParam(name = "symbolType") Integer symbolType, @RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate, @RequestParam(name = "platform") String platform, @RequestParam(name = "rate") Float rate, InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        TradeSalesStockCommissionEntity entity = new TradeSalesStockCommissionEntity();
        entity.setLogin(login);
        entity.setSalesNo(salesNo);
        entity.setSymbolType(symbolType);
        entity.setStartDate(StringUtils.isEmpty(startDate) ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(StringUtils.isEmpty(endDate) ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);
        entity.setRate(rate);

        BaseResultDto<List<TradeSalesStockCommissionDto>> resultDto = tradeFeignService.tradeSalesStockCommissionExport(entity);
        if (resultDto.getCode() == 200) {
            List<TradeSalesStockCommissionExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                TradeSalesStockCommissionExportDto dto = new TradeSalesStockCommissionExportDto();
                BeanUtils.copyProperties(item, dto);
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(genFileName("SALES_STOCK_COMMISSION"), dtos, TradeSalesStockCommissionExportDto.class, response);
        }
    }


    /**
     * 对冲报表
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/hedging")
    public BaseReportResultDto<List<TradeHedgingDto>, Object> tradeHedging(@RequestBody BaseIbReportListEntity entity, InfoEntity infoEntity) throws Exception {
        return tradeFeignService.tradeHedging(entity);
    }

    /**
     * PM出入金列表
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/pm/dw")
    public BaseReportResultDto<List<TradePmDwDto>, TradePmDwSumDto> tradePmDw(@RequestBody BaseIbReportListEntity entity, InfoEntity infoEntity) throws Exception {
        return tradeFeignService.tradePmDw(entity);
    }

    @GetMapping("/pm/dw/export")
    public void tradePmDwExport(@RequestParam(name = "login") Long login, @RequestParam(name = "pmLogin") Long pmLogin, @RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate, @RequestParam(name = "platform") String platform, @RequestParam(name = "cId") Long cId, @RequestParam(name = "agentId") Long agentId, InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        BaseIbReportListEntity entity = new BaseIbReportListEntity();
        entity.setLogin(login);
        entity.setStartDate(StringUtils.isEmpty(startDate) ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(StringUtils.isEmpty(endDate) ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);
        entity.setAgentId(agentId);
        entity.setPmLogin(pmLogin);
        entity.setCId(cId);

        BaseResultDto<List<TradePmDwDto>> resultDto = tradeFeignService.tradePmDwExport(entity);
        if (resultDto.getCode() == 200) {
            List<TradePmDwExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                TradePmDwExportDto dto = new TradePmDwExportDto();
                BeanUtils.copyProperties(item, dto);
                dto.setCID(item.getCId());
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(genFileName("PM_DW_REPORT"), dtos, TradePmDwExportDto.class, response);
        }
    }

    @PostMapping("/swaps")
    public BaseReportResultDto<List<TradeSwapsDto>, TradeSwapsSumDto> tradeSwaps(@RequestBody BaseIbReportListEntity entity, InfoEntity infoEntity) throws Exception {

        if (entity.getEndDate() == null) {
            entity.setEndDate(DateUtil.getMonthEnd(entity.getEndDate()));
        } else {
            entity.setEndDate(DateUtil.addDate(entity.getEndDate()));
        }

        return tradeService.tradeSwaps(entity, infoEntity);
    }


    @GetMapping("/swaps/export")
    public void tradeSwapsExport(@RequestParam(name = "startDate") String startDate, @RequestParam(name = "endDate") String endDate, @RequestParam(name = "platform") String platform, @RequestParam(name = "ticket") Long ticket, InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        BaseIbReportListEntity entity = new BaseIbReportListEntity();
        entity.setStartDate(StringUtils.isEmpty(startDate) ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(StringUtils.isEmpty(endDate) ? new Date() : simpleDateFormat.parse(endDate));
        entity.setTicket(ticket);
        entity.setPlatform(StringUtils.isEmpty(platform) ? null : platform.toUpperCase());

        if (entity.getEndDate() == null) {
            entity.setEndDate(DateUtil.getMonthEnd(entity.getEndDate()));
        } else {
            entity.setEndDate(DateUtil.addDate(entity.getEndDate()));
        }

        List<TradeSwapsExportDto> dtos = tradeService.tradeSwapsExport(entity, infoEntity);
        ExportUtil.transferToResponse(genFileName("TRADE_SWAPS_REPORT"), dtos, TradeSwapsExportDto.class, response);
    }

    @PostMapping("/standard/rebate")
    public BaseReportResultDto<List<TradeStandardRebateManagerDto>, TradeStandardRebateSumDto> tradeStandardRebate(@RequestBody TradeStandardRebateListEntity entity, InfoEntity infoEntity) throws Exception {

        if (entity.getEndDate() == null) {
            entity.setEndDate(DateUtil.getMonthEnd(entity.getEndDate()));
        } else {
            entity.setEndDate(DateUtil.addDate(entity.getEndDate()));
        }
        entity.setIsFromCustom(false);

        return tradeShardingFeignService.standardRebate(entity);
    }


    @GetMapping("/standard/rebate/export")
    public void tradeStandardRebateExport(@RequestParam(name = "login") Long login,
                                          @RequestParam(name = "ibNo") String ibNo,
                                          @RequestParam(name = "cId") Long cId,
                                          @RequestParam(name = "startDate") String startDate,
                                          @RequestParam(name = "endDate") String endDate,
                                          @RequestParam(name = "agentId") Long agentId,
                                          InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        TradeStandardRebateListEntity entity = new TradeStandardRebateListEntity();
        entity.setLogin(login);
        entity.setIbNo(ibNo);
        entity.setCId(cId);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        entity.setAgentId(agentId);

        BaseResultDto<List<TradeStandardRebateManagerDto>> resultDto = tradeShardingFeignService.standardRebateExport(entity);
        if (resultDto.getCode() == 200) {
            List<TradeStandardRebateExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                TradeStandardRebateExportDto dto = new TradeStandardRebateExportDto();
                BeanUtils.copyProperties(item, dto);
                dto.setCID(item.getCId());
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("TRADE_STANDARD_REBATE"), dtos, TradeStandardRebateExportDto.class, response);

        }
    }

    @PostMapping("/hidden/rebate")
    public BaseReportResultDto<List<TradeHiddenRebateManagerDto>, TradeHiddenRebateSumDto> tradeHiddenRebate(@RequestBody TradeHiddenRebateListEntity entity, InfoEntity infoEntity) throws Exception {

        if (entity.getEndDate() == null) {
            entity.setEndDate(DateUtil.getMonthEnd(entity.getEndDate()));
        }
        entity.setFromIb(false);

        return tradeShardingFeignService.hiddenRebate(entity);
    }


    @GetMapping("/hidden/rebate/export")
    public void tradeHiddenRebateExport(@RequestParam(name = "login") Long login,
                                        @RequestParam(name = "ibNo") String ibNo,
                                        @RequestParam(name = "salesNo") String salesNo,
                                        @RequestParam(name = "startDate") String startDate,
                                        @RequestParam(name = "endDate") String endDate,
                                        @RequestParam(name = "platform") String platform,
                                        InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        TradeHiddenRebateListEntity entity = new TradeHiddenRebateListEntity();
        entity.setLogin(login);
        entity.setIbNo(ibNo);
        entity.setSalesNo(salesNo);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(platform);

        BaseResultDto<List<TradeHiddenRebateManagerDto>> resultDto = tradeShardingFeignService.hiddenRebateExport(entity);
        if (resultDto.getCode() == 200) {
            List<TradeHiddenRebateExportDto> dtos = Lists.newArrayList();
            resultDto.getData().forEach((item) -> {
                TradeHiddenRebateExportDto dto = new TradeHiddenRebateExportDto();
                BeanUtils.copyProperties(item, dto);
                dtos.add(dto);
            });
            ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("TRADE_HIDDEN_REBATE"), dtos, TradeHiddenRebateExportDto.class, response);

        }
    }

    @PostMapping("/sales/reward")
    public BaseReportResultDto<List<TradeSalesRewardDto>, TradeSalesRewardSumDto> tradeSalesReward(@RequestBody BaseIbReportListEntity entity, InfoEntity infoEntity) throws Exception {

        if (entity.getEndDate() == null) {
            entity.setEndDate(DateUtil.getMonthEnd(entity.getEndDate()));
        }

        return selesRewardWorthStatisticsService.tradeSalesReward(entity, infoEntity);
    }

    @GetMapping("/sales/reward/export")
    public void tradeSalesRewardExport(
            @RequestParam(name = "ibNo") String ibNo,
            @RequestParam(name = "cId") Long cId,
            @RequestParam(name = "startDate") String startDate,
            @RequestParam(name = "endDate") String endDate,
            InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        TradeHiddenRebateListEntity entity = new TradeHiddenRebateListEntity();
        entity.setIbNo(ibNo);
        entity.setCId(cId);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));

        List<TradeSalesRewardExportDto> dtos = selesRewardWorthStatisticsService.tradeSalesRewardExport(entity, infoEntity);
        ExportUtil.transferToResponse(genFileName("TRADE_REWARD_REPORT"), dtos, TradeSalesRewardExportDto.class, response);

    }

    @PostMapping("/sales/performance")
    public BaseReportResultDto<List<TradeSalesPerformanceDto>, TradeSalesPerformanceSumDto> tradePerformanceReward(@RequestBody SalesPerformanceListEntity entity) throws Exception {

        if (entity.getEndDate() == null) {
            entity.setEndDate(DateUtil.getMonthEnd(entity.getEndDate()));
        }

        return selesPerformanceService.tradePerformance(entity);
    }

    @PostMapping("/sales/performance/list")
    public BaseResultDto tradePerformanceSales(@RequestBody SalesPerformanceListEntity entity) throws Exception {

        if (entity.getEndDate() == null) {
            entity.setEndDate(DateUtil.getMonthEnd(entity.getEndDate()));
        }

        return BaseResultDto.success(selesPerformanceService.tradePerformanceList(entity));
    }

    @PostMapping("/sales/performance/send")
    public BaseResultDto tradePerformanceSendReport(@RequestBody SalesPerformanceListEntity entity) throws Exception {

        if (entity.getEndDate() == null) {
            entity.setEndDate(DateUtil.getMonthEnd(entity.getEndDate()));
        }

        selesPerformanceService.tradePerformanceSendReport(entity);

        return BaseResultDto.success();
    }

    @PostMapping("/sales/performance/manager/send")
    public BaseResultDto tradePerformanceSendManagerReport(@RequestBody SalesPerformanceListEntity entity) throws Exception {

        if (entity.getEndDate() == null) {
            entity.setEndDate(DateUtil.getMonthEnd(entity.getEndDate()));
        }

        selesPerformanceService.tradePerformanceSendManagerReport(entity);

        return BaseResultDto.success();
    }

    @PostMapping("/sales/performance/statistics")
    public BaseResultDto salesPerformanceStatistics(@RequestBody SalesPerformanceListEntity entity) throws Exception {
        selesPerformanceService.salesPerformanceStatistics(entity);
        return BaseResultDto.success();
    }

    @GetMapping("/sales/performance/export")
    public void tradeSalesPerformanceExport(
            @RequestParam(name = "startDate") String startDate,
            @RequestParam(name = "endDate") String endDate,
            @RequestParam(name = "salesNo") String salesNo,
            InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        SalesPerformanceListEntity entity = new SalesPerformanceListEntity();
        entity.setSalesNo(salesNo);
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));

        List<String> sheetName = new ArrayList<>();
        List<List<TradeSalesPerformanceExportDto>> dtos = selesPerformanceService.tradeSalesPerformanceExport(entity, sheetName);
        ExportUtil.transferListToResponse(genFileName("SALES_PERFORMANCE_REPORT"), dtos, sheetName, TradeSalesPerformanceExportDto.class, response);

    }

    @PostMapping("/account/summary")
    public BaseReportResultDto<List<AccountDailySummaryDto>, AccountDailySummarySumDto> accountSummary(@RequestBody SalesPerformanceListEntity entity) throws Exception {
        return tradeFeignService.accountSummary(entity);
    }


    private String genFileName(String fileNamePrefix) {
        StringBuffer stringBuffer = new StringBuffer();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        stringBuffer.append(fileNamePrefix);
        stringBuffer.append("_");
        stringBuffer.append(sdf.format(new Date()));
        stringBuffer.append(".xlsx");
        String fileName = stringBuffer.toString();
        return fileName;
    }
}
