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.sim.domain.AddOnDept;
import com.ruoyi.sim.domain.Major;
import com.ruoyi.sim.domain.Student;
import com.ruoyi.sim.service.IAddOnDeptService;
import com.ruoyi.sim.service.IMajorService;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDeptService;
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 log = 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
    private IAddOnDeptService addOnDeptService;

    @Autowired
    private IMajorService majorService;

    @Autowired
    protected Validator validator;

    // @DataScope(deptAlias = "d", userAlias = "u")
    public List<Student> selectStudentList() {
        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;
    }


    public Student selectStudentByUserId(Long userId) {
        SysUser sysUser = userMapper.selectUserById(userId);
        // 屏蔽密码
        sysUser.setPassword("");
        Student student = new Student();
        BeanUtils.copyProperties(sysUser, student);
        student.setMajor(getMajorByDeptId(sysUser.getDeptId()));
        // logger.info(Objects.requireNonNull(student.toString()));
        return student;
    }

    public Major getMajorByDeptId(long deptId) {
        AddOnDept a = addOnDeptService.selectAddOnDeptByDeptId(deptId);
        Major m = majorService.selectMajorByMajorId(a.getMajorId());
        return m;
    }
}
