package com.crm.ib.task.service.report.impl;

import com.crm.ib.task.dao.mapper.TradeHistoryDao;
import com.crm.ib.task.service.report.VolumeReportService;
import com.crm.rely.backend.core.constant.Constants;
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.PageDto;
import com.crm.rely.backend.core.enums.SymbolType;
import com.crm.rely.backend.model.dto.report.SalesCommissionReportListDto;
import com.crm.rely.backend.model.dto.report.SalesCommissionReportSumDto;
import com.crm.rely.backend.model.dto.report.VolumeReportListDto;
import com.crm.rely.backend.model.dto.report.VolumeReportSumDto;
import com.crm.rely.backend.model.entity.report.VolumeReportEntity;
import com.crm.rely.backend.model.pojo.view.VolumeReportView;
import com.crm.rely.backend.util.FloatFormat;
import com.crm.rely.backend.util.FormatPage;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author y
 * @create 2022/5/5
 */
@Service
public class VolumeReportServiceImpl implements VolumeReportService {

    @Autowired
    private TradeHistoryDao tradeHistoryDao;

    @Override
    public BaseReportResultDto<List<VolumeReportListDto>, VolumeReportSumDto> pageList(VolumeReportEntity entity) throws Exception {

        List<Integer> counts = tradeHistoryDao.countVolumeReport(entity);
        if (CollectionUtils.isEmpty(counts)) {
            return new BaseReportResultDto<>(Constants.SUCCESS_CODE, "");
        }

        Integer count = counts.size();
        PageDto pageDto = FormatPage.format(entity.getPage(), count);

        List<VolumeReportListDto> dtos = getVolumeList(entity);

        List<VolumeReportView> sumViews = tradeHistoryDao.sumVolumeReport(entity);
        VolumeReportSumDto sumDto = VolumeReportSumDto.getInstance();
        for (VolumeReportView sumView : sumViews) {
            if (null == sumView.getSymbolType()) {
                continue;
            }
            sumDto.setFx(FloatFormat.formatFloat(sumDto.getFx() + (SymbolType.equalCode(sumView.getSymbolType(), SymbolType.FX.getCode()) ? sumView.getVolume() : 0F)));
            sumDto.setCfd(FloatFormat.formatFloat(sumDto.getCfd() + (SymbolType.equalCode(sumView.getSymbolType(), SymbolType.CFD.getCode()) ? sumView.getVolume() : 0F)));
            sumDto.setIndex(FloatFormat.formatFloat(sumDto.getIndex() + (SymbolType.equalCode(sumView.getSymbolType(), SymbolType.INDEX.getCode()) ? sumView.getVolume() : 0F)));
            sumDto.setMetal(FloatFormat.formatFloat(sumDto.getMetal() + (SymbolType.equalCode(sumView.getSymbolType(), SymbolType.METAL.getCode()) ? sumView.getVolume() : 0F)));
        }
        sumDto.setSum(FloatFormat.formatFloat(sumDto.calcSum()));

        return new BaseReportResultDto<>(dtos, sumDto, pageDto);
    }

    @Override
    public BaseResultDto<List<VolumeReportListDto>> exportList(VolumeReportEntity entity) throws Exception {
        return BaseResultDto.success(getVolumeList(entity));
    }

    @Override
    public BaseReportResultDto<List<SalesCommissionReportListDto>, SalesCommissionReportSumDto> pageSalesVolumeList(VolumeReportEntity entity) throws Exception {

        Integer count = tradeHistoryDao.countSalesVolumeReport(entity);
        if (count == null || count <= 0) {
            return new BaseReportResultDto<>(Constants.SUCCESS_CODE, "");
        }
        PageDto pageDto = FormatPage.format(entity.getPage(), count);
        List<SalesCommissionReportListDto> dtos = getSalesVolumeList(entity);

        VolumeReportView sumView = tradeHistoryDao.sumSalesVolumeReport(entity);
        SalesCommissionReportSumDto sumDto = SalesCommissionReportSumDto.getInstance();
        BeanUtils.copyProperties(sumView, sumDto);
        sumDto.setAmount(FloatFormat.formatFloat(sumView.getSalesCommission()));
        sumDto.setVolume(FloatFormat.formatFloat(sumView.getVolume()));

        return new BaseReportResultDto<>(dtos, sumDto, pageDto);
    }

    @Override
    public BaseResultDto<List<SalesCommissionReportListDto>> exportSalesVolumeList(VolumeReportEntity entity) throws Exception {
        return BaseResultDto.success(getSalesVolumeList(entity));
    }

    private List<VolumeReportListDto> getVolumeList(VolumeReportEntity entity) throws Exception {
        List<VolumeReportView> views = tradeHistoryDao.listVolumeReport(entity);
        List<VolumeReportListDto> dtos = Lists.newArrayList();
        for (VolumeReportView view : views) {
            VolumeReportListDto dto = new VolumeReportListDto();
            dtos.add(dto);
            BeanUtils.copyProperties(view, dto);
            dto.setFx(FloatFormat.formatFloat(view.getFxVolume()));
            dto.setCfd(FloatFormat.formatFloat(view.getCfdVolume()));
            dto.setIndex(FloatFormat.formatFloat(view.getIndexVolume()));
            dto.setMetal(FloatFormat.formatFloat(view.getMetalVolume()));
            dto.setSum(FloatFormat.formatFloat(dto.getFx() + dto.getCfd() + dto.getIndex() + dto.getMetal()));
        }
        return dtos;
    }

    private List<SalesCommissionReportListDto> getSalesVolumeList(VolumeReportEntity entity) throws Exception {

        List<SalesCommissionReportListDto> dtos = Lists.newArrayList();
        List<VolumeReportView> views = tradeHistoryDao.listSalesVolumeReport(entity);
        for (VolumeReportView view : views) {
            SalesCommissionReportListDto dto = SalesCommissionReportListDto.getInstance();
            dtos.add(dto);
            BeanUtils.copyProperties(view, dto);
            dto.setAmount(FloatFormat.formatFloat(view.getSalesCommission()));
            dto.setVolume(FloatFormat.formatFloat(view.getVolume()));
        }
        return dtos;
    }
}
