package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.SalesDpsBasicSalaryMapper;
import com.crm.manager.service.SalesDpsBasicSalaryService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
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.SalesDpsBasicSalaryDto;
import com.crm.rely.backend.core.entity.sales.SalesDpsBasicSalaryListEntity;
import com.crm.rely.backend.core.pojo.table.SalesDpsBasicSalaryTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.rely.backend.util.FormatPage;
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.ArrayList;
import java.util.List;

@Service
public class SalesDpsBasicSalaryServiceImpl implements SalesDpsBasicSalaryService {

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

    @Autowired
    private SalesDpsBasicSalaryMapper salesDpsBasicSalaryMapper;

    @Override
    public BaseResultDto<SalesDpsBasicSalaryDto> searchSingleById(Long id) throws ServiceException {
        try {
            return BaseResultDto.success(convertTableToDto(salesDpsBasicSalaryMapper.findById(id)));
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public BaseResultWithPagerDto<List<SalesDpsBasicSalaryDto>> searchList(SalesDpsBasicSalaryListEntity entity) throws ServiceException {
        try {
            Integer count = salesDpsBasicSalaryMapper.countList(entity);
            PageDto pageDto = FormatPage.format(entity.getPage(), count);
            if (count == null || count == 0) {
                return new BaseResultWithPagerDto<>(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, pageDto);
            }

            List<SalesDpsBasicSalaryDto> dtos = convertTableToDtoList(salesDpsBasicSalaryMapper.findList(entity));
            return new BaseResultWithPagerDto(Constants.SUCCESS_CODE, Constants.SEARCH_SUCCESS, pageDto, dtos);
        } catch (Exception e) {
            logger.error(e.getMessage());
            throw new ServiceException(e.getMessage());
        }
    }


    private SalesDpsBasicSalaryDto convertTableToDto(SalesDpsBasicSalaryTable table) {
        if (table == null) {
            return null;
        }
        SalesDpsBasicSalaryDto result = new SalesDpsBasicSalaryDto();
        BeanUtils.copyProperties(table, result);
        return result;
    }

    private List<SalesDpsBasicSalaryDto> convertTableToDtoList(List<SalesDpsBasicSalaryTable> tables) {
        List<SalesDpsBasicSalaryDto> result = new ArrayList<>();
        for (SalesDpsBasicSalaryTable table : tables) {
            SalesDpsBasicSalaryDto dto = convertTableToDto(table);
            result.add(dto);
        }
        return result;
    }

}
