package com.ruoyi.test.seriver.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.test.domain.User;
import com.ruoyi.test.domain.Vo.UserVo;
import com.ruoyi.test.mapper.UserMapper;
import com.ruoyi.test.seriver.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Resource
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public IPage<User> selectStudentList(int pagenum) {
        Page<User> page = new Page<>(pagenum,10);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(User::getUserId,User::getName,User::getTname,User::getAge,User::getSex,User::getBirthday
            ,User::getTelephone,User::getGrade,User::getState);
        queryWrapper.eq(User::getRoleKey,2L);
        IPage<User> iPage = userMapper.selectPage(page,queryWrapper);
        return iPage;
    }

    @Override
    public IPage<User> selectStudent(int pagenum,String name) {
        Page<User> page = new Page<>(pagenum,10);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(User::getUserId,User::getName,User::getTname,User::getAge,User::getSex,User::getBirthday
                ,User::getTelephone,User::getGrade,User::getState);
        queryWrapper.eq(User::getName,name);
        queryWrapper.eq(User::getRoleKey,2L);
        IPage<User> iPage = userMapper.selectPage(page,queryWrapper);
        return iPage;
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insertUser(user);
    }

    @Override
    public List<User> selectStudentByZhid(Long zhid) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getZhId,zhid);
        return userMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<User> selectTeacherList(int pagenum) {
        Page<User> page = new Page<>(pagenum,10);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(User::getUserId,User::getName,User::getTname,User::getAge,User::getSex,User::getBirthday
                ,User::getTelephone,User::getGrade,User::getState);
        queryWrapper.eq(User::getRoleKey,3L);
        IPage<User> iPage = userMapper.selectPage(page,queryWrapper);
        return iPage;
    }

    @Override
    public IPage<User> selectTeacher(int pagenum,String name) {
        Page<User> page = new Page<>(pagenum,10);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(User::getUserId,User::getName,User::getTname,User::getAge,User::getSex,User::getBirthday
                ,User::getTelephone,User::getGrade,User::getState);
        queryWrapper.eq(User::getName,name);
        queryWrapper.eq(User::getRoleKey,3L);
        IPage<User> iPage = userMapper.selectPage(page,queryWrapper);
        return iPage;
    }

    @Override
    public IPage<User> selectGlList(int pagenum) {
        Page<User> page = new Page<>(pagenum,10);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(User::getUserId,User::getName,User::getTname,User::getAge,User::getSex,User::getBirthday
                ,User::getTelephone,User::getGrade,User::getState);
        queryWrapper.eq(User::getRoleKey,4L);
        IPage<User> iPage = userMapper.selectPage(page,queryWrapper);
        return iPage;
    }

    @Override
    public IPage<User> selectGl(int pagenum,String name) {
        Page<User> page = new Page<>(pagenum,10);
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(User::getUserId,User::getName,User::getTname,User::getAge,User::getSex,User::getBirthday
                ,User::getTelephone,User::getGrade,User::getState);
        queryWrapper.eq(User::getName,name);
        queryWrapper.eq(User::getRoleKey,4L);
        IPage<User> iPage = userMapper.selectPage(page,queryWrapper);
        return iPage;
    }

    @Override
    public Long selectByzhid(Long zhid) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getZhId,zhid);
        return userMapper.selectOne(queryWrapper).getUserId();
    }

    @Override
    public User user(Long id) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUserId,id);
        return userMapper.selectOne(queryWrapper);
    }

    public List<UserVo> up(List<User> list){
        List<UserVo> list1 = new ArrayList<>();
        for(User user:list){
            UserVo userVo = new UserVo();
            userVo.setUserId(user.getUserId());
            userVo.setName(user.getName());
            userVo.setTname(user.getTname());
            userVo.setAge(user.getAge());
            userVo.setGrade(user.getGrade());
            userVo.setSex(user.getSex());
            userVo.setBirthday(user.getBirthday());
            userVo.setTelephone(user.getTelephone());
            list1.add(userVo);
        }
        return list1;
    }
}
