package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SalesDwCommissionMapper;
import com.crm.manager.service.SalesDwCommissionService;
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.SalesCommissionRuleDto;
import com.crm.rely.backend.core.dto.sales.SalesDwCommissionDto;
import com.crm.rely.backend.core.dto.sales.SalesDwCommissionSumDto;
import com.crm.rely.backend.core.entity.sales.SalesDwCommissionListEntity;
import com.crm.rely.backend.core.pojo.table.SalesDwCommissionTable;
import com.crm.rely.backend.core.pojo.view.SalesDwCommissionView;
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 SalesDwCommissionServiceImpl implements SalesDwCommissionService {

    @Autowired
    private SalesDwCommissionMapper salesDwCommissionMapper;

    @Override
    public BaseReportResultDto<List<SalesDwCommissionDto>, SalesDwCommissionSumDto> dwCommissionList(SalesDwCommissionListEntity entity) throws ServiceException {

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

        SalesDwCommissionView sumView = salesDwCommissionMapper.sumSearch(entity);
        SalesDwCommissionSumDto sumDto = new SalesDwCommissionSumDto();
        sumDto.setAmount(sumView.getAmount());

        List<SalesDwCommissionView> views = salesDwCommissionMapper.listSearch(entity);
        List<SalesDwCommissionDto> dtos = Lists.newArrayList();
        for (SalesDwCommissionView view : views) {
            dtos.add(convertViewToDto(view));
        }

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

    @Override
    public List<SalesDwCommissionDto> dwCommissionExportList(SalesDwCommissionListEntity entity) throws ServiceException {

        entity.setPage(null);
        List<SalesDwCommissionView> views = salesDwCommissionMapper.listSearch(entity);
        List<SalesDwCommissionDto> dtos = Lists.newArrayList();
        for (SalesDwCommissionView view : views) {
            dtos.add(convertViewToDto(view));
        }
        return dtos;
    }

    private SalesDwCommissionDto convertViewToDto(SalesDwCommissionView view) {
        SalesDwCommissionDto dto = new SalesDwCommissionDto();
        BeanUtils.copyProperties(view, dto);
        if (view.getDwType() == 0) {
            dto.setDwTypeName("TRADE");
        } else {
            dto.setDwTypeName("DW");
        }
        return dto;
    }
}
