package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SalesDpsVolumeCommissionMapper;
import com.crm.manager.service.SalesDpsVolumeCommissionService;
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.PageDto;
import com.crm.rely.backend.core.dto.sales.SalesDpsDwDto;
import com.crm.rely.backend.core.dto.sales.SalesDpsDwSumDto;
import com.crm.rely.backend.core.dto.sales.SalesDpsVolumeCommissionDto;
import com.crm.rely.backend.core.dto.sales.SalesDpsVolumeCommissionSumDto;
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.enums.CustomGroupType;
import com.crm.rely.backend.core.pojo.table.SalesDpsDwTable;
import com.crm.rely.backend.core.pojo.table.SalesDpsVolumeCommissionTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.FormatPage;
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.stereotype.Service;

import java.util.List;

@Service
public class SalesDpsVolumeCommissionServiceImpl implements SalesDpsVolumeCommissionService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SalesDpsVolumeCommissionMapper salesDpsVolumeCommissionMapper;

    @Override
    public BaseReportResultDto<List<SalesDpsVolumeCommissionDto>, SalesDpsVolumeCommissionSumDto> searchList(BaseIbReportListEntity entity, InfoEntity infoEntity) throws Exception {
        try {

            Integer count = salesDpsVolumeCommissionMapper.countSearch(entity);
            PageDto pageDto = FormatPage.format(entity.getPage(), count);
            if (count == null || count == 0) {
                return new BaseReportResultDto<>(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, pageDto);
            }

            SalesDpsVolumeCommissionTable sumTable = salesDpsVolumeCommissionMapper.sumSearch(entity);
            SalesDpsVolumeCommissionSumDto sumDto = new SalesDpsVolumeCommissionSumDto();
            sumDto.setAmount(sumTable.getAmount());
            sumDto.setVolume(sumTable.getVolume());

            List<SalesDpsVolumeCommissionDto> dtos = convertTableToDto(salesDpsVolumeCommissionMapper.listSearch(entity));

            return new BaseReportResultDto<>(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, dtos, sumDto, pageDto);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new ServiceException(e.getMessage());
        }
    }

    private SalesDpsVolumeCommissionDto convertTableToDto(SalesDpsVolumeCommissionTable table) {
        SalesDpsVolumeCommissionDto dto = new SalesDpsVolumeCommissionDto();
        BeanUtils.copyProperties(table, dto);
        dto.setGroupTypeName(CustomGroupType.matchCode(table.getGroupType()).getDesc());
        return dto;
    }

    private List<SalesDpsVolumeCommissionDto> convertTableToDto(List<SalesDpsVolumeCommissionTable> tables) {
        List<SalesDpsVolumeCommissionDto> dtos = Lists.newArrayList();
        for (SalesDpsVolumeCommissionTable table : tables) {
            dtos.add(convertTableToDto(table));
        }
        return dtos;
    }
}
