package com.yaoyuan.jiscuss.service.impl;

import com.yaoyuan.jiscuss.entity.User;
import com.yaoyuan.jiscuss.repository.UsersRepository;
import com.yaoyuan.jiscuss.service.IUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class UsersServiceImpl implements IUsersService {
    
    @Autowired
    private UsersRepository usersRepository;

    /**
     * 获取全部用户
     *
     * @return
     */
    @Transactional(readOnly = true)
    @Cacheable(value = "userList")
    @Override
    public List<User> getAllList() {
        return usersRepository.findAll();
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public Page<User> queryAllUsersList(int pageNum, int pageSize) {
        Pageable pageable = PageRequest.of(pageNum, pageSize, Sort.by(Sort.Direction.DESC, "id"));
        return usersRepository.findAll(pageable);
    }

    /**
     * 根据名称模糊查询
     *
     * @param name
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public List<User> getByUsernameIsLike(String name) {
        return usersRepository.getByUsernameIsLike(name);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Transactional(readOnly = true)
    @Cacheable(value = "user", key = "#id")
    @Override
    public User findOne(Integer id) {
        return usersRepository.findById(id).orElse(null);
    }

    /**
     * 新增
     *
     * @param user
     * @return
     */
    @Transactional
    @CachePut(value = "user", key = "#user.id")
    @CacheEvict(value = "userList", allEntries = true)
    @Override
    public User insert(User user) {
        return usersRepository.save(user);
    }

    /**
     * 更新修改
     *
     * @param user
     * @param id
     * @return
     */
    @Transactional
    @CachePut(value = "user", key = "#user.id")
    @CacheEvict(value = "userList", allEntries = true)
    @Override
    public User update(User user, Integer id) {
        return usersRepository.saveAndFlush(user);
    }

    /**
     * 移除
     *
     * @param id
     */
    @Transactional
    @Caching(evict = {
        @CacheEvict(value = "user", key = "#id"),
        @CacheEvict(value = "userList", allEntries = true)
    })
    @Override
    public void remove(Integer id) {
        usersRepository.deleteById(id);
    }

    /**
     * 删除所有
     *
     */
    @Transactional
    @Override
    public void deleteAll() {
        usersRepository.deleteAll();
    }

    /**
     * 根据名称查询
     *
     * @param username
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public User getByUsername(String username) {
        return usersRepository.getByUsername(username);
    }

    @Transactional(readOnly = true)
    @Override
    public List<User> findAllById(Iterable<Integer> ids) {
        return usersRepository.findAllById(ids);
    }

    @Transactional(readOnly = true)
    @Override
    public List<User> getTopActiveUsers(int limit) {
        return usersRepository.findTop10ActiveUsers(
                org.springframework.data.domain.PageRequest.of(0, limit));
    }

}


