package com.crm.manager.service.impl;

import com.crm.manager.dao.repository.UserGroupRepository;
import com.crm.manager.service.UserGroupService;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.user.group.UserGroupDto;
import com.crm.rely.backend.core.pojo.table.UserGroupTable;
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/9/11 16:13
 * @Description:
 */
@Service
public class UserGroupServiceImpl implements UserGroupService {

    @Autowired
    private UserGroupRepository userGroupRepository;

    @Override
    public BaseResultDto<List<UserGroupDto>> getAllGroup() {
        List<UserGroupTable> userGroupTables = userGroupRepository.getAllByIdNotNull();
        if (userGroupTables == null || userGroupTables.size() <= 0) {
            return BaseResultDto.success();
        }
        List<UserGroupDto> userGroupDtos = new ArrayList<>(userGroupTables.size());
        userGroupTables.forEach(value -> {
            UserGroupDto userGroupDto = new UserGroupDto();

            BeanUtils.copyProperties(value, userGroupDto);
            userGroupDtos.add(userGroupDto);
        });
        return BaseResultDto.success(userGroupDtos);
    }
}
