package com.crm.manager.controller;

import com.crm.manager.service.FinanceTrainFeignService;
import com.crm.manager.service.TaskReportFeignService;
import com.crm.rely.backend.core.constant.Constants;
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.model.entity.user.info.UserInfoEntity;
import com.crm.rely.backend.model.dto.report.*;
import com.crm.rely.backend.model.entity.finance.train.FinanceTrainSearchListEntity;
import com.crm.rely.backend.model.entity.report.VolumeReportEntity;
import com.crm.rely.backend.util.ExportUtil;
import com.crm.rely.backend.util.FileProcessUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 报表服务
 *
 * @author y
 * @create 2022/5/6
 */
@RestController
@RequestMapping("/report")
public class ReportController {

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    private FinanceTrainFeignService financeTrainFeignService;
    @Autowired
    private TaskReportFeignService taskReportFeignService;

    /**
     * 培训费报表
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/train/commission")
    public BaseReportResultDto<List<FinanceTrainReportListDto>, FinanceTrainReportSumDto> trainCommission(@RequestBody @Validated FinanceTrainSearchListEntity entity, UserInfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);
        }
        return financeTrainFeignService.report(entity);
    }

    @GetMapping("/train/commission/export")
    public void
    trainCommissionExport(@RequestParam(name = "startDate") String startDate,
                          @RequestParam(name = "endDate") String endDate,
                          @RequestParam(name = "ibNo") String ibNo,
                          @RequestParam(name = "salesNo") String salesNo,
                          UserInfoEntity infoEntity, HttpServletResponse response) throws Exception {
        FinanceTrainSearchListEntity entity = new FinanceTrainSearchListEntity();
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);

        }
        entity.setStartDate(StringUtils.isEmpty(startDate) ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(StringUtils.isEmpty(endDate) ? new Date() : simpleDateFormat.parse(endDate));
        entity.setIbNo(ibNo);
        entity.setSalesNo(salesNo);

        BaseResultDto<List<FinanceTrainReportListDto>> resultDto = financeTrainFeignService.reportExport(entity);
        if (resultDto.getCode() == 200) {
            List<FinanceTrainReportListDto> dtos = resultDto.getData();
            ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("TRAIN_COMMISSION"), dtos, FinanceTrainReportListDto.class, response);
        }

    }

    /**
     * 交易量报表
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/trade/volume")
    public BaseReportResultDto<List<VolumeReportListDto>, VolumeReportSumDto> volume(@RequestBody VolumeReportEntity entity, UserInfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);
        }
        entity.setPlatform(Constants.MT4);
        return taskReportFeignService.volume(entity);
    }

    /**
     * 交易量报表
     *
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @GetMapping("/trade/volume/export")
    public void
    volumeExport(@RequestParam(name = "login") Long login,
                 @RequestParam(name = "startDate") String startDate,
                 @RequestParam(name = "endDate") String endDate,
                 @RequestParam(name = "ibNo") String ibNo,
                 @RequestParam(name = "salesNo") String salesNo,
                 UserInfoEntity infoEntity, HttpServletResponse response) throws Exception {
        VolumeReportEntity entity = new VolumeReportEntity();
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);

        }
        entity.setStartDate(StringUtils.isEmpty(startDate) ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(StringUtils.isEmpty(endDate) ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(Constants.MT4);
        entity.setIbNo(ibNo);
        entity.setSalesNo(salesNo);
        entity.setLogin(login);

        BaseResultDto<List<VolumeReportListDto>> resultDto = taskReportFeignService.volumeExport(entity);
        if (resultDto.getCode() == 200) {
            List<VolumeReportListDto> dtos = resultDto.getData();

            ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("VOLUME"), dtos, VolumeReportListDto.class, response);
        }

    }

    /**
     * 销售交易佣金报表
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/sales/volume")
    public BaseReportResultDto<List<SalesCommissionReportListDto>, SalesCommissionReportSumDto> salesVolume(@RequestBody VolumeReportEntity entity, UserInfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);
        }
        entity.setPlatform(Constants.MT4);
        return taskReportFeignService.salesVolume(entity);
    }

    /**
     * 销售交易佣金报表导出
     *
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @GetMapping("/sales/volume/export")
    public void
    salesVolumeExport(@RequestParam(name = "login") Long login,
                      @RequestParam(name = "startDate") String startDate,
                      @RequestParam(name = "endDate") String endDate,
                      @RequestParam(name = "ibNo") String ibNo,
                      @RequestParam(name = "salesNo") String salesNo,
                      UserInfoEntity infoEntity, HttpServletResponse response) throws Exception {
        VolumeReportEntity entity = new VolumeReportEntity();
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);

        }
        entity.setStartDate(StringUtils.isEmpty(startDate) ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(StringUtils.isEmpty(endDate) ? new Date() : simpleDateFormat.parse(endDate));
        entity.setPlatform(Constants.MT4);
        entity.setIbNo(ibNo);
        entity.setSalesNo(salesNo);
        entity.setLogin(login);

        BaseResultDto<List<SalesCommissionReportListDto>> resultDto = taskReportFeignService.salesVolumeExport(entity);
        if (resultDto.getCode() == 200) {
            List<SalesCommissionReportListDto> dtos = resultDto.getData();

            ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("SALES_COMMISSION"), dtos, SalesCommissionReportListDto.class, response);
        }

    }


    /**
     * 培训费报表
     *
     * @param entity
     * @param infoEntity
     * @return
     * @throws Exception
     */
    @PostMapping("/train/sales/commission")
    public BaseReportResultDto<List<FinanceTrainSalesReportListDto>, FinanceTrainReportSumDto> salesTrainCommission(@RequestBody @Validated FinanceTrainSearchListEntity entity, UserInfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);
        }
        return financeTrainFeignService.salesReport(entity);
    }

    @GetMapping("/train/sales/commission/export")
    public void
    salesTrainCommissionExport(@RequestParam(name = "startDate") String startDate,
                               @RequestParam(name = "endDate") String endDate,
                               @RequestParam(name = "ibNo") String ibNo,
                               @RequestParam(name = "salesNo") String salesNo,
                               UserInfoEntity infoEntity, HttpServletResponse response) throws Exception {
        FinanceTrainSearchListEntity entity = new FinanceTrainSearchListEntity();
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getStamp());
        } else {
            entity.setStamp(null);

        }
        entity.setStartDate(StringUtils.isEmpty(startDate) ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(StringUtils.isEmpty(endDate) ? new Date() : simpleDateFormat.parse(endDate));
        entity.setIbNo(ibNo);
        entity.setSalesNo(salesNo);

        BaseResultDto<List<FinanceTrainSalesReportListDto>> resultDto = financeTrainFeignService.salesReportExport(entity);
        if (resultDto.getCode() == 200) {
            List<FinanceTrainSalesReportListDto> dtos = resultDto.getData();
            ExportUtil.transferToResponse(FileProcessUtil.genExportFileName("SALES_TRAIN_COMMISSION"), dtos, FinanceTrainSalesReportListDto.class, response);
        }

    }

}
