package com.ruoyi.sim.service.impl;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.sim.constant.Consts;
import com.ruoyi.sim.domain.Teacher;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
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 javax.validation.Validator;
import java.util.ArrayList;
import java.util.List;

@Service
public class TeacherService {

    private static final Logger l = LoggerFactory.getLogger(StudentService.class);

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private SysPostMapper postMapper;

    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Autowired
    private SysUserPostMapper userPostMapper;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    protected Validator validator;
    @Autowired
    private ISysUserService sysUserService;

    // @DataScope(deptAlias = "d", userAlias = "u")
    public List<SysUser> selectTeacherList() {
        SysUser su = new SysUser();
        List<SysUser> list = userMapper.selectUserList(su);
        List<SysUser> list2 = new ArrayList<SysUser>();
        // todo:
        for (SysUser sysUser : list) {
            List<SysRole> listR = roleMapper.selectRolesByUserName(sysUser.getUserName());
            for (SysRole sysRole : listR) {
                if (sysRole.getRoleId() == Consts.ROLE_ID_TEACHER) {
                    list2.add(sysUser);
                    break;
                }
            }
        }
        return list2;
    }

    /**
     * [分页]查询教师列表。
     *
     * @param suQ
     * @return
     */
    public List<Teacher> list(SysUser suQ) {
        l.info("suQ:{}", suQ);
        // 分页
        PageUtils.startPage();
        suQ.setRoleId(Consts.ROLE_ID_TEACHER);
        List<SysUser> listSu = sysUserService.selectAllocatedList(suQ);
        List<Teacher> listSt = new ArrayList<>(listSu.size());
        listSu.forEach(su -> {
            Teacher t = new Teacher();
            su.setPassword("~~~~");
            BeanUtils.copyProperties(su, t);
            listSt.add(t);
        });
        return listSt;
    }

    /**
     * [分页]查询教师列表。
     *
     * @param suQ
     * @return
     */
    public List<Teacher> list2(SysUser suQ) {
        l.info("suQ:{}", suQ);
        // 分页
        PageUtils.startPage();
        suQ.setRoleId(Consts.ROLE_ID_TEACHER);
        List<SysUser> listSu = sysUserService.selectAllocatedListV2(suQ);
        List<Teacher> listSt = new ArrayList<>(listSu.size());
        listSu.forEach(su -> {
            Teacher t = new Teacher();
            su.setPassword("~~~~");
            BeanUtils.copyProperties(su, t);
            listSt.add(t);
        });
        return listSt;
    }
}
