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.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.sim.constant.Consts;
import com.ruoyi.sim.domain.AddOnDept;
import com.ruoyi.sim.domain.Major;
import com.ruoyi.sim.domain.Student;
import com.ruoyi.system.mapper.*;
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;
import java.util.Objects;

@Service
public class StudentService {

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

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private AddOnDeptService addOnDeptService;

    @Autowired
    private MajorService majorService;

    @Autowired
    protected Validator validator;

    @Autowired
    private ISysUserService sysUserService;

    // @DataScope(deptAlias = "d", userAlias = "u")
    public List<Student> selectStudentListOld() {
        SysUser su = new SysUser();
        List<SysUser> list = userMapper.selectUserList(su);
        List<Student> list2 = new ArrayList<>();
        // todo:分页
        for (SysUser sysUser : list) {
            List<SysRole> listR = roleMapper.selectRolesByUserName(sysUser.getUserName());
            for (SysRole sysRole : listR) {
                if (Objects.equals(sysRole.getRoleId(), Consts.ROLE_ID_STUDENT)) {
                    Student target = new Student();
                    BeanUtils.copyProperties(sysUser, target);
                    Major m = getMajorByDeptId(sysUser.getDeptId());
                    if (Objects.nonNull(m)) {
                        target.setMajor(m);
                    }
                    // final add.
                    list2.add(target);
                    break;
                }
            }
        }
        return list2;
    }

    /**
     * [分页]查询学生列表。
     *
     * @param suQ
     * @return
     */
    public List<Student> list(SysUser suQ) {
        // 分页
        PageUtils.startPage();
        suQ.setRoleId(Consts.ROLE_ID_STUDENT);
        List<SysUser> listSu = sysUserService.selectAllocatedList(suQ);
        List<Student> listSt = new ArrayList<>(listSu.size());
        listSu.forEach(su -> {
            Student st = new Student();
            su.setPassword("~~~~");
            BeanUtils.copyProperties(su, st);
            listSt.add(st);
        });
        return listSt;
    }

    public void letTry(int code) {
        List<SysUser> list = null;
        switch (code) {
            case 1: {
                SysUser su = new SysUser();
                list = userMapper.selectUserList(su);
            }
            break;
            case 2: {
                PageUtils.startPage();
                SysUser su = new SysUser();
                list = userMapper.selectUserList(su);
            }
            break;
            case 3: {
                list = sysUserService.selectUserList(new SysUser());
            }
            break;
            case 4: {
                PageUtils.startPage();
                list = sysUserService.selectUserList(new SysUser());
            }
            break;
            case 5: {
                list = sysUserService.selectAllocatedList(new SysUser());
            }
            break;
            case 6: {
                // ok
                PageUtils.startPage();
                SysUser su = new SysUser();
                su.setRoleId(Consts.ROLE_ID_STUDENT);
                list = sysUserService.selectAllocatedList(su);
            }
            break;
            case 7: {
                list = sysUserService.selectUnallocatedList(new SysUser());
            }
            break;
            case 8: {
                PageUtils.startPage();
                list = sysUserService.selectUnallocatedList(new SysUser());
            }
            break;
        }
        for (SysUser o : list) {
            l.info(o.toString());
        }
    }


    public Student selectStudentByUserId(Long userId) {
        SysUser sysUser = userMapper.selectUserById(userId);
        Student student = new Student();
        // todo:用户角色判断。
        // 屏蔽密码
        if (Objects.nonNull(sysUser)) {
            sysUser.setPassword("");
            BeanUtils.copyProperties(sysUser, student);
            Major m = getMajorByDeptId(sysUser.getDeptId());
            if (Objects.nonNull(m)) {
                student.setMajor(m);
            }
            // logger.info(Objects.requireNonNull(student.toString()));
        }
        return student;
    }

    public Major getMajorByDeptId(Long deptId) {
        AddOnDept a = addOnDeptService.selectAddOnDeptByDeptId(deptId);
        Major m = null;
        if (Objects.nonNull(a)) {
            m = majorService.selectMajorByMajorId(a.getMajorId());
        }
        return m;
    }
}
