package com.ruoyi.sim.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
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.common.utils.StringUtils;
import com.ruoyi.sim.constant.Consts;
import com.ruoyi.sim.domain.Teacher;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysRoleService;
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 java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class TeacherService {

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

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysRoleMapper roleMapper;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private ISysPostService postService;

    // @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 = userService.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 = userService.selectAllocatedListV2(suQ);
        List<Teacher> listT = new ArrayList<>(listSu.size());
        listSu.forEach(su -> {
            Teacher t = new Teacher();
            BeanUtils.copyProperties(su, t);
            listT.add(t);
        });
        return listT;
    }

    public AjaxResult selectTeacherByUserId(Long userId) {
        // 不校验数据权限
        // userService.checkUserDataScope(userId);

        AjaxResult ajax = AjaxResult.success();
        List<SysRole> roles = roleService.selectRoleAll();
        ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        ajax.put("posts", postService.selectPostAll());
        if (StringUtils.isNotNull(userId)) {
            SysUser sysUser = userService.selectUserById(userId);
            ajax.put(AjaxResult.DATA_TAG, sysUser);
            ajax.put("postIds", postService.selectPostListByUserId(userId));
            ajax.put("roleIds", sysUser.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList()));
        }
        return ajax;
//        Teacher t = new Teacher();
//        // todo:用户角色判断。
//        // 屏蔽密码
//        if (Objects.nonNull(sysUser)) {
//            BeanUtils.copyProperties(sysUser, t);
//        }
//        return t;
    }
}
