package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SalesCommissionRecordMapper;
import com.crm.manager.service.SalesCommissionRecordService;
import com.crm.rely.backend.core.constant.Constants;
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.SalesCommissionRecordDto;
import com.crm.rely.backend.core.entity.sales.SalesCommissionRecordListEntity;
import com.crm.rely.backend.core.pojo.table.SalesCommissionManualTable;
import com.crm.rely.backend.core.pojo.table.SalesCommissionRecordTable;
import com.crm.rely.backend.core.pojo.view.SalesCommissionRecordView;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.FormatPage;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SalesCommissionRecordServiceImpl implements SalesCommissionRecordService {

    @Autowired
    private SalesCommissionRecordMapper salesCommissionRecordMapper;

    @Override
    public BaseResultWithPagerDto<List<SalesCommissionRecordDto>> commissionRecords(SalesCommissionRecordListEntity entity) throws ServiceException {

        Integer count = salesCommissionRecordMapper.countSearch(entity);

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

        List<SalesCommissionRecordView> views = salesCommissionRecordMapper.listSearch(entity);
        List<SalesCommissionRecordDto> dtos = Lists.newArrayList();
        for (SalesCommissionRecordView view : views) {
            dtos.add(this.convertViewToDto(view));
        }

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

    @Override
    public List<SalesCommissionRecordDto> commissionExportRecords(SalesCommissionRecordListEntity entity) throws ServiceException {

        entity.setPage(null);
        List<SalesCommissionRecordView> views = salesCommissionRecordMapper.listSearch(entity);
        List<SalesCommissionRecordDto> dtos = Lists.newArrayList();
        for (SalesCommissionRecordView view : views) {
            dtos.add(this.convertViewToDto(view));
        }

        return dtos;
    }

    @Override
    public void processAfterManualCommissionRecord(SalesCommissionManualTable table) throws ServiceException {

    }

    public void processAfterManualCommissionRecord(SalesCommissionRecordTable table) throws ServiceException {
        salesCommissionRecordMapper.updateAmountBySalesIdAndYm(table);
    }

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