package com.crm.manager.service.impl;

import com.crm.manager.dao.mapper.CustomLoginGroupHistoryMapper;
import com.crm.manager.service.CustomLoginGroupHistoryService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.custom.login.group.CustomLoginGroupHistoryDto;
import com.crm.rely.backend.core.dto.custom.remark.CustomRemarkSearchListDto;
import com.crm.rely.backend.core.entity.custom.login.group.CustomLoginGroupHistorySearchEntity;
import com.crm.rely.backend.core.pojo.table.CustomLoginGroupHistoryTable;
import com.crm.rely.backend.core.pojo.table.CustomRemarkTable;
import com.crm.rely.backend.exception.ServiceException;
import lombok.Data;
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;

/**
 * @Author: houn
 * @Date: 2020/11/17 12:32
 * @Description:
 */
@Service
public class CustomLoginGroupHistoryServiceImpl implements CustomLoginGroupHistoryService {

    @Autowired
    private CustomLoginGroupHistoryMapper customLoginGroupHistoryMapper;

    @Override
    public ResultWithPagerDto<CustomLoginGroupHistoryDto> searchPageList(CustomLoginGroupHistorySearchEntity entity) throws ServiceException {
        Integer count = customLoginGroupHistoryMapper.countList(entity);
        PageDto pageDto = PageDto.format(entity, count);
        if (count == null || count <= 0) {
            return ResultWithPagerDto.success(pageDto);
        }
        List<CustomLoginGroupHistoryTable> customLoginGroupHistoryTables = customLoginGroupHistoryMapper.pageList(entity);

        if (customLoginGroupHistoryTables == null || customLoginGroupHistoryTables.size() <= 0) {

            throw new ServiceException(Constants.SYSTEM_ERROR);
        }
        List<CustomLoginGroupHistoryDto> customLoginGroupHistoryDtos = new ArrayList<>(customLoginGroupHistoryTables.size());

        for (CustomLoginGroupHistoryTable customLoginGroupHistoryTable : customLoginGroupHistoryTables) {
            CustomLoginGroupHistoryDto rateSearchDto = transform(customLoginGroupHistoryTable);
            customLoginGroupHistoryDtos.add(rateSearchDto);
        }

        return ResultWithPagerDto.success(pageDto, customLoginGroupHistoryDtos);
    }


    private CustomLoginGroupHistoryDto transform(CustomLoginGroupHistoryTable customLoginGroupHistoryTable) {
        CustomLoginGroupHistoryDto customLoginGroupHistoryDto = new CustomLoginGroupHistoryDto();
        BeanUtils.copyProperties(customLoginGroupHistoryTable, customLoginGroupHistoryDto);
        return customLoginGroupHistoryDto;
    }
}
