package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SalesVolumeCommissionMapper;
import com.crm.manager.service.SalesVolumeCommissionService;
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.BaseResultWithPagerDto;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.sales.SalesDwCommissionDto;
import com.crm.rely.backend.core.dto.sales.SalesVolumeCommissionDto;
import com.crm.rely.backend.core.dto.sales.SalesVolumeCommissionSumDto;
import com.crm.rely.backend.core.entity.sales.SalesVolumeCommissionListEntity;
import com.crm.rely.backend.core.pojo.view.SalesDwCommissionView;
import com.crm.rely.backend.core.pojo.view.SalesVolumeCommissionView;
import com.crm.rely.backend.exception.ServiceException;
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 java.util.Collections;
import java.util.List;

@Service
public class SalesVolumeCommissionServiceImpl implements SalesVolumeCommissionService {

    @Autowired
    private SalesVolumeCommissionMapper salesVolumeCommissionMapper;

    @Override
    public BaseReportResultDto<List<SalesVolumeCommissionDto>, SalesVolumeCommissionSumDto> findSalesVolumeCommission(SalesVolumeCommissionListEntity entity) throws ServiceException {
        Integer count = salesVolumeCommissionMapper.countSearch(entity);
        PageDto pageDto = FormatPage.format(entity.getPage(), count);
        if (count == null || count == 0) {
            return new BaseReportResultDto<>(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, pageDto);
        }

        SalesVolumeCommissionView sumView = salesVolumeCommissionMapper.sumSearch(entity);
        SalesVolumeCommissionSumDto sumDto = SalesVolumeCommissionSumDto.getInstance();
        BeanUtils.copyProperties(sumView, sumDto);

        List<SalesVolumeCommissionView> views = salesVolumeCommissionMapper.listSearch(entity);
        List<SalesVolumeCommissionDto> dtos = Lists.newArrayList();
        for (SalesVolumeCommissionView view : views) {
            dtos.add(convertViewToDto(view));
        }

        return new BaseReportResultDto<>(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, dtos, sumDto, pageDto);
    }

    @Override
    public List<SalesVolumeCommissionDto> findSalesVolumeExportCommission(SalesVolumeCommissionListEntity entity) throws ServiceException {

        entity.setPage(null);
        List<SalesVolumeCommissionView> views = salesVolumeCommissionMapper.listSearch(entity);
        List<SalesVolumeCommissionDto> dtos = Lists.newArrayList();
        for (SalesVolumeCommissionView view : views) {
            dtos.add(convertViewToDto(view));
        }

        return dtos;
    }

    private SalesVolumeCommissionDto convertViewToDto(SalesVolumeCommissionView view) {
        SalesVolumeCommissionDto dto = new SalesVolumeCommissionDto();
        BeanUtils.copyProperties(view, dto);
        return dto;
    }
}
