package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SalesDpsSalaryRecordMapper;
import com.crm.manager.service.SalesDpsSalaryRecordService;
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.SalesDpsSalaryRecordDto;
import com.crm.rely.backend.core.dto.sales.SalesDpsSalaryRecordSumDto;
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.sales.SalesDpsSalaryRecordListEntity;
import com.crm.rely.backend.core.pojo.table.SalesDpsSalaryRecordTable;
import com.crm.rely.backend.core.pojo.view.SalesDwCommissionView;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.FloatFormat;
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 SalesDpsSalaryRecordServiceImpl implements SalesDpsSalaryRecordService {

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

    @Autowired
    private SalesDpsSalaryRecordMapper salesDpsSalaryRecordMapper;

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

            SalesDpsSalaryRecordSumDto sumDto = convertSumTableToDto(salesDpsSalaryRecordMapper.sumSearch(entity));
            List<SalesDpsSalaryRecordDto> dtos = convertTableToDtoList(salesDpsSalaryRecordMapper.listSearch(entity));

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

    private SalesDpsSalaryRecordSumDto convertSumTableToDto(SalesDpsSalaryRecordTable table) {
        if (table == null) {
            return null;
        }
        SalesDpsSalaryRecordSumDto dto = new SalesDpsSalaryRecordSumDto();
        BeanUtils.copyProperties(table, dto);
        dto.setBasicSalary(FloatFormat.formatFloat(table.getBasicSalary()));
        dto.setCumulativeGrossDeposit(FloatFormat.formatFloat(dto.getCumulativeGrossDeposit()));
        dto.setDpsSalary(FloatFormat.formatFloat(dto.getDpsSalary()));
        dto.setGrossDepositAmount(FloatFormat.formatFloat(dto.getGrossDepositAmount()));
        dto.setNetDepositAmount(FloatFormat.formatFloat(dto.getNetDepositAmount()));
        dto.setNetSalary(FloatFormat.formatFloat(dto.getNetSalary()));
        dto.setOtherPerformanceSalary(FloatFormat.formatFloat(dto.getOtherPerformanceSalary()));
        dto.setVolumeRebate(FloatFormat.formatFloat(dto.getVolumeRebate()));
        return dto;
    }

    private SalesDpsSalaryRecordDto convertTableToDto(SalesDpsSalaryRecordTable table) {
        if (table == null) {
            return null;
        }
        SalesDpsSalaryRecordDto dto = new SalesDpsSalaryRecordDto();
        BeanUtils.copyProperties(table, dto);
        dto.setBasicSalary(FloatFormat.formatFloat(table.getBasicSalary()));
        dto.setCumulativeGrossDeposit(FloatFormat.formatFloat(dto.getCumulativeGrossDeposit()));
        dto.setDpsSalary(FloatFormat.formatFloat(dto.getDpsSalary()));
        dto.setGrossDepositAmount(FloatFormat.formatFloat(dto.getGrossDepositAmount()));
        dto.setGrossDepositKpi(FloatFormat.formatFloat(dto.getGrossDepositKpi()));
        dto.setGrossDepositScore(FloatFormat.formatFloat(dto.getGrossDepositScore()));
        dto.setNetDepositAmount(FloatFormat.formatFloat(dto.getNetDepositAmount()));
        dto.setNetDepositKpi(FloatFormat.formatFloat(dto.getNetDepositKpi()));
        dto.setNetDepositScore(FloatFormat.formatFloat(dto.getNetDepositScore()));
        dto.setOtherPerformanceSalary(FloatFormat.formatFloat(dto.getOtherPerformanceSalary()));
        dto.setTotalScore(FloatFormat.formatFloat(dto.getTotalScore()));
        dto.setVolumeRebate(FloatFormat.formatFloat(dto.getVolumeRebate()));
        return dto;
    }

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