package com.crm.manager.controller;

import com.crm.manager.dto.export.FollowCommissionReportExportDto;
import com.crm.manager.dto.export.FollowPeriodCommissionReportExportDto;
import com.crm.manager.dto.export.TradeHistoryExportDto;
import com.crm.manager.service.follow.FollowReportFeignService;
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.core.dto.follow.report.FollowCommissionReportDto;
import com.crm.rely.backend.core.dto.follow.report.FollowPeriodCommissionReportDto;
import com.crm.rely.backend.core.dto.ib.trade.TradeHistoryDto;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.follow.report.FollowCommissionReportEntity;
import com.crm.rely.backend.core.entity.ib.trade.TradeHistoryListEntity;
import com.crm.rely.backend.util.ExportUtil;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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/19
 */
@RestController
@RequestMapping("/follow/report")
public class FollowReportController {

    @Autowired
    private FollowReportFeignService followReportFeignService;

    @PostMapping("/commission")
    public BaseReportResultDto<List<FollowCommissionReportDto>, FollowCommissionReportDto>
    commissionReport(@RequestBody FollowCommissionReportEntity entity, InfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getIbInfo().getStamp());
        }
        return followReportFeignService.commissionReport(entity);

    }

    @GetMapping("/commission/export")
    public void commissionReportExport(@RequestParam(name = "dealLogin") Long dealLogin,
                                       @RequestParam(name = "followLogin") Long followLogin,
                                       @RequestParam(name = "agentId") Long agentId,
                                       @RequestParam(name = "startDate") String startDate,
                                       @RequestParam(name = "endDate") String endDate,
                                       InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        FollowCommissionReportEntity entity = new FollowCommissionReportEntity();
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getIbInfo().getStamp());
        }
        entity.setDealLogin(dealLogin);
        entity.setFollowLogin(followLogin);
        entity.setAgentId(agentId);
        BaseResultDto<List<FollowCommissionReportDto>> resultDto = followReportFeignService.commissionReportExport(entity);
        if (resultDto.getCode() == 200) {
            List<FollowCommissionReportDto> dtos = resultDto.getData();
            List<FollowCommissionReportExportDto> exportDtos = Lists.newArrayList();
            for (FollowCommissionReportDto dto : dtos) {
                FollowCommissionReportExportDto exportDto = new FollowCommissionReportExportDto();
                exportDtos.add(exportDto);
                BeanUtils.copyProperties(dto, exportDto);
                if (dto.getClear() == 1) {
                    exportDto.setClear("已完成");
                } else {
                    exportDto.setClear("未完成");
                }
            }
            ExportUtil.transferToResponse(genFileName("FOLLOW_COMMISSION"), exportDtos, FollowCommissionReportExportDto.class, response);
        }
    }

    @PostMapping("/period/commission")
    public BaseReportResultDto<List<FollowPeriodCommissionReportDto>, FollowPeriodCommissionReportDto>
    periodCommissionReport(@RequestBody FollowCommissionReportEntity entity, InfoEntity infoEntity) throws Exception {
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getIbInfo().getStamp());
        }
        return followReportFeignService.periodCommissionReport(entity);

    }

    @GetMapping("/period/commission/export")
    public void periodCommissionReportExport(@RequestParam(name = "dealLogin") Long dealLogin,
                                       @RequestParam(name = "followLogin") Long followLogin,
                                       @RequestParam(name = "agentId") Long agentId,
                                       @RequestParam(name = "startDate") String startDate,
                                       @RequestParam(name = "endDate") String endDate,
                                       InfoEntity infoEntity, HttpServletResponse response) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        FollowCommissionReportEntity entity = new FollowCommissionReportEntity();
        entity.setStartDate(startDate == null ? new Date() : simpleDateFormat.parse(startDate));
        entity.setEndDate(endDate == null ? new Date() : simpleDateFormat.parse(endDate));
        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setStamp(infoEntity.getIbInfo().getStamp());
        }
        entity.setDealLogin(dealLogin);
        entity.setFollowLogin(followLogin);
        entity.setAgentId(agentId);
        BaseResultDto<List<FollowPeriodCommissionReportDto>> resultDto = followReportFeignService.periodCommissionReportExport(entity);
        if (resultDto.getCode() == 200) {
            List<FollowPeriodCommissionReportDto> dtos = resultDto.getData();
            List<FollowPeriodCommissionReportExportDto> exportDtos = Lists.newArrayList();
            for (FollowPeriodCommissionReportDto dto : dtos) {
                FollowPeriodCommissionReportExportDto exportDto = new FollowPeriodCommissionReportExportDto();
                BeanUtils.copyProperties(dto, exportDto);
                exportDto.setFollowCId(dto.getFollowCId());
                exportDto.setDealCId(dto.getDealCId());
                exportDtos.add(exportDto);
            }
            ExportUtil.transferToResponse(genFileName("FOLLOW_COMMISSION"), exportDtos, FollowPeriodCommissionReportExportDto.class, response);
        }
    }

    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;
    }

}
