package com.crm.manager.controller;

import com.crm.manager.dto.export.SalesCommissionRecordExportDto;
import com.crm.manager.dto.export.SalesDwCommissionExportDto;
import com.crm.manager.dto.export.SalesVolumeCommissionExportDto;
import com.crm.manager.dto.export.TradeHistoryExportDto;
import com.crm.manager.service.SalesCommissionRecordService;
import com.crm.manager.service.SalesDwCommissionService;
import com.crm.manager.service.SalesVolumeCommissionService;
import com.crm.manager.service.UserInfoService;
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.base.BaseResultWithPagerDto;
import com.crm.rely.backend.core.dto.ib.trade.TradeHistoryDto;
import com.crm.rely.backend.core.dto.sales.*;
import com.crm.rely.backend.core.entity.custom.info.IbInfoEntity;
import com.crm.rely.backend.core.entity.custom.info.InfoEntity;
import com.crm.rely.backend.core.entity.ib.trade.TradeHistoryListEntity;
import com.crm.rely.backend.core.entity.sales.SalesCommissionRecordListEntity;
import com.crm.rely.backend.core.entity.sales.SalesDwCommissionListEntity;
import com.crm.rely.backend.core.entity.sales.SalesVolumeCommissionListEntity;
import com.crm.rely.backend.core.enums.CustomGroupType;
import com.crm.rely.backend.core.enums.SalesKpiType;
import com.crm.rely.backend.core.enums.SalesType;
import com.crm.rely.backend.core.pojo.table.UserInfoTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.DwTypeUtil;
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.util.StringUtils;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/sales/commission")
public class SalesCommissionController {

    @Autowired
    private SalesCommissionRecordService salesCommissionRecordService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private SalesDwCommissionService salesDwCommissionService;
    @Autowired
    private SalesVolumeCommissionService salesVolumeCommissionService;

    @PostMapping("/records")
    public BaseResultWithPagerDto<List<SalesCommissionRecordDto>> commissionRecords(@RequestBody SalesCommissionRecordListEntity entity, InfoEntity infoEntity) throws ServiceException {
        if (StringUtils.isEmpty(entity.getYm())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            entity.setYm(sdf.format(new Date()));
        }

        IbInfoEntity userInfo = infoEntity.getIbInfo();

        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setSalesId(userInfo.getSalesId());
            entity.setSalesType(userInfo.getSalesType());
        } else {
            entity.setSalesId(0L);
            entity.setSalesType(0);
            entity.setSalesIdStamp(userInfo.getIdStamp());
        }
        return salesCommissionRecordService.commissionRecords(entity);
    }


    @GetMapping("/records/export")
    public void recordsExport(@RequestParam(name = "ym") String ym,
                              @RequestParam(name = "salesNo") String salesNo,
                              InfoEntity infoEntity,
                              HttpServletResponse response) throws Exception {
        SalesCommissionRecordListEntity entity = new SalesCommissionRecordListEntity();
        entity.setYm(ym);
        entity.setSalesNo(salesNo);
        if (StringUtils.isEmpty(entity.getYm())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            entity.setYm(sdf.format(new Date()));
        }

        IbInfoEntity userInfo = infoEntity.getIbInfo();

        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setSalesId(userInfo.getSalesId());
            entity.setSalesType(userInfo.getSalesType());
        } else {
            entity.setSalesId(0L);
            entity.setSalesType(0);
            entity.setSalesIdStamp(userInfo.getIdStamp());
        }

        List<SalesCommissionRecordDto> recordDtos = salesCommissionRecordService.commissionExportRecords(entity);
        List<SalesCommissionRecordExportDto> dtos = Lists.newArrayList();
        recordDtos.forEach((item) -> {
            SalesCommissionRecordExportDto dto = new SalesCommissionRecordExportDto();
            BeanUtils.copyProperties(item, dto);
            dto.setSalesTypeName(SalesType.matchCode(item.getSalesType()).getDesc());
            dtos.add(dto);
        });
        ExportUtil.transferToExcelResponse("SALES_COMMISSION_RECORD", dtos, SalesCommissionRecordExportDto.class, response);
    }

    @PostMapping("/dw")
    public BaseReportResultDto<List<SalesDwCommissionDto>, SalesDwCommissionSumDto> dw(@RequestBody SalesDwCommissionListEntity entity, InfoEntity infoEntity) throws ServiceException {

        IbInfoEntity userInfo = infoEntity.getIbInfo();

        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setSalesId(userInfo.getSalesId());
            entity.setSalesType(userInfo.getSalesType());
        } else {
            entity.setSalesId(0L);
            entity.setSalesType(0);
        }

        entity.setSalesIdStamp(userInfo.getIdStamp());
        return salesDwCommissionService.dwCommissionList(entity);
    }

    @GetMapping("/dw/export")
    public void dwExport(@RequestParam(name = "ym") String ym,
                         @RequestParam(name = "salesNo") String salesNo,
                         @RequestParam(name = "reportType") Integer reportType,
                         InfoEntity infoEntity,
                         HttpServletResponse response) throws Exception {
        SalesDwCommissionListEntity entity = new SalesDwCommissionListEntity();
        entity.setYm(ym);
        entity.setSalesNo(salesNo);
        entity.setReportType(reportType);
        if (StringUtils.isEmpty(entity.getYm())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            entity.setYm(sdf.format(new Date()));
        }

        IbInfoEntity userInfo = infoEntity.getIbInfo();

        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setSalesId(userInfo.getSalesId());
            entity.setSalesType(userInfo.getSalesType());
        } else {
            entity.setSalesId(0L);
            entity.setSalesType(0);
            entity.setSalesIdStamp(userInfo.getIdStamp());
        }

        List<SalesDwCommissionDto> recordDtos = salesDwCommissionService.dwCommissionExportList(entity);
        List<SalesDwCommissionExportDto> dtos = Lists.newArrayList();
        recordDtos.forEach((item) -> {
            SalesDwCommissionExportDto dto = new SalesDwCommissionExportDto();
            BeanUtils.copyProperties(item, dto);
            dtos.add(dto);
        });
        ExportUtil.transferToExcelResponse("SALES_COMMISSION_RECORD", dtos, SalesDwCommissionExportDto.class, response);
    }

    @PostMapping("/volume")
    public BaseReportResultDto<List<SalesVolumeCommissionDto>, SalesVolumeCommissionSumDto> volume(@RequestBody SalesVolumeCommissionListEntity entity, InfoEntity infoEntity) throws ServiceException {

        IbInfoEntity userInfo = infoEntity.getIbInfo();

        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setSalesId(userInfo.getSalesId());
            entity.setSalesType(userInfo.getSalesType());
        } else {
            entity.setSalesId(0L);
            entity.setSalesType(0);

        }
        entity.setSalesIdStamp(userInfo.getIdStamp());

        return salesVolumeCommissionService.findSalesVolumeCommission(entity);
    }

    @GetMapping("/volume/export")
    public void volumeExport(@RequestParam(name = "ym") String ym,
                             @RequestParam(name = "salesNo") String salesNo,
                             @RequestParam(name = "reportType") Integer reportType,
                             InfoEntity infoEntity,
                             HttpServletResponse response) throws Exception {
        SalesVolumeCommissionListEntity entity = new SalesVolumeCommissionListEntity();
        entity.setYm(ym);
        entity.setSalesNo(salesNo);
        entity.setReportType(reportType);
        if (StringUtils.isEmpty(entity.getYm())) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            entity.setYm(sdf.format(new Date()));
        }

        IbInfoEntity userInfo = infoEntity.getIbInfo();

        if (PrefixEnum.PREFIX_SALE.equals(infoEntity.getPrefix())) {
            entity.setSalesId(userInfo.getSalesId());
            entity.setSalesType(userInfo.getSalesType());
        } else {
            entity.setSalesId(0L);
            entity.setSalesType(0);
            entity.setSalesIdStamp(userInfo.getIdStamp());
        }

        List<SalesVolumeCommissionDto> recordDtos = salesVolumeCommissionService.findSalesVolumeExportCommission(entity);
        List<SalesVolumeCommissionExportDto> dtos = Lists.newArrayList();
        recordDtos.forEach((item) -> {
            SalesVolumeCommissionExportDto dto = new SalesVolumeCommissionExportDto();
            BeanUtils.copyProperties(item, dto);
            dto.setGroupTypeName(CustomGroupType.matchCode(item.getGroupType()).getDesc());
            dtos.add(dto);
        });
        ExportUtil.transferToExcelResponse("SALES_COMMISSION_RECORD", dtos, SalesVolumeCommissionExportDto.class, response);
    }
}
