package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SalesDpsDwMapper;
import com.crm.manager.service.SalesDpsDwService;
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.SalesDwCommissionDto;
import com.crm.rely.backend.core.dto.sales.SalesDwCommissionSumDto;
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.pojo.table.SalesDpsDwTable;
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.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 SalesDpsDwServiceImpl implements SalesDpsDwService {

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

    @Autowired
    private SalesDpsDwMapper salesDpsDwMapper;

    @Override
    public BaseReportResultDto<List<SalesDpsDwDto>, SalesDpsDwSumDto> searchList(BaseIbReportListEntity entity, InfoEntity infoEntity) throws ServiceException {
        try {
            Integer count = salesDpsDwMapper.countSearch(entity);
            PageDto pageDto = FormatPage.format(entity.getPage(), count);
            if (count == null || count == 0) {
                return new BaseReportResultDto<>(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, pageDto);
            }

            SalesDpsDwTable sumView = salesDpsDwMapper.sumSearch(entity);
            SalesDpsDwSumDto sumDto = new SalesDpsDwSumDto();
            sumDto.setAmount(sumView.getAmount());

            List<SalesDpsDwDto> dtos = convertTableToDto(salesDpsDwMapper.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 SalesDpsDwDto convertTableToDto(SalesDpsDwTable table) {
        SalesDpsDwDto dto = new SalesDpsDwDto();
        BeanUtils.copyProperties(table, dto);
        if (table.getDwType() == 0) {
            dto.setDwTypeName("TRADE");
        } else {
            dto.setDwTypeName("DW");
        }
        return dto;
    }

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