ClassesDictServiceImpl.java 17 KB
package com.meishu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meishu.dto.classes.*;
import com.meishu.mapper.*;
import com.meishu.model.*;
import com.meishu.service.ClassesDictService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meishu.util.ConstantUtils;
import com.meishu.vo.classes.GetClassStudentsVO;
import com.meishu.vo.classes.GetClassesVO;
import com.meishu.vo.classes.SubjectDictVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 学校班级字典表 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2021-08-11
 */
@Service
public class ClassesDictServiceImpl extends ServiceImpl<ClassesDictMapper, ClassesDictDO> implements ClassesDictService {

    @Autowired
    private SubjectDictMapper subjectDictMapper;

    @Autowired
    private SubjectSemesterMapper subjectSemesterMapper;

    @Autowired
    private UserSubjectMappingMapper userSubjectMappingMapper;

    @Autowired
    private ClassesUserMappingMapper classesUserMappingMapper;

    @Autowired
    private ClassesDictHistoryMapper classesDictHistoryMapper;

    @Autowired
    private UserSubjectPermissionDictMapper userSubjectPermissionDictMapper;

    @Autowired
    private KnowledgeSubjectDictMapper knowledgeSubjectDictMapper;

    public IPage<GetClassesVO> getClasses(GetClassesDTO getClassesDTO) {

        Page pager = new Page(getClassesDTO.getPageNum(), getClassesDTO.getPageSize());

        IPage<GetClassesVO> getClassesVOIPage = this.baseMapper.getClasses(pager, getClassesDTO.getClassName(), getClassesDTO.getGrade(), getClassesDTO.getSession(), getClassesDTO.getStatus());

        List<GetClassesVO> getClassesVOS = getClassesVOIPage.getRecords();
        //找出班级所有的科目
        for (GetClassesVO getClassesVO : getClassesVOS) {
            List<SubjectDictDO> subjectDictDOS = subjectDictMapper.selectList(new QueryWrapper<SubjectDictDO>()
                    .lambda()
                    .eq(SubjectDictDO::getGrade, getClassesVO.getGrade())
                    .eq(SubjectDictDO::getClasses, getClassesVO.getClasses())
                    .eq(SubjectDictDO::getSession, getClassesVO.getSession()));

            List<SubjectDictVO> subjectDictVOS = new ArrayList<>();
            for (SubjectDictDO subjectDictDO : subjectDictDOS) {
                SubjectDictVO subjectDictVO = new SubjectDictVO();
                BeanUtils.copyProperties(subjectDictDO, subjectDictVO);
                //查询该科目下的授课老师
                List<AdministerDO> administerDOS = userSubjectMappingMapper.querySubjectTeachers(subjectDictDO.getId());
                subjectDictVO.setAdministerDOS(administerDOS);

                KnowledgeSubjectDictDO knowledgeSubjectDict = knowledgeSubjectDictMapper.selectOne(new QueryWrapper<KnowledgeSubjectDictDO>()
                        .lambda()
                        .eq(KnowledgeSubjectDictDO::getSubjectName, subjectDictDO.getSubject()));
                if (null != knowledgeSubjectDict) {
                    subjectDictVO.setId(knowledgeSubjectDict.getId().intValue());
                } else {
                    subjectDictVO.setId(null);
                }
                subjectDictVOS.add(subjectDictVO);
            }
            getClassesVO.setSubjectDictDOS(subjectDictVOS);
        }
        getClassesVOIPage.setRecords(getClassesVOS);
        return getClassesVOIPage;
    }

    @Transactional(rollbackFor = Exception.class)
    public String addClass(AddClassDTO addClassDTO) {

        ClassesDictDO classesDictDO = new ClassesDictDO();
        BeanUtils.copyProperties(addClassDTO, classesDictDO);
        classesDictDO.setClassName(classesDictDO.getGrade() + "(" + classesDictDO.getClasses() + ")班");
        classesDictDO.setSchool(ConstantUtils.SCHOOL);
        classesDictDO.setSubjectType("高中");
        classesDictDO.setStatus("在读");


        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH)+1;//获取月份

        Date date = new Date();
        if (month>=7){
            date.setYear(year-1899);
            date.setMonth(7);
            date.setDate(1);
            date.setHours(0);
            date.setMinutes(0);
            date.setSeconds(0);
        }else {
            date.setYear(year-1900);
            date.setMonth(7);
            date.setDate(1);
            date.setHours(0);
            date.setMinutes(0);
            date.setSeconds(0);
        }
        classesDictDO.setUpgradeDate(date);
        this.baseMapper.insert(classesDictDO);

        ClassesDictHistoryDO classesDictHistoryDO = new ClassesDictHistoryDO();
        BeanUtils.copyProperties(addClassDTO, classesDictHistoryDO);
        classesDictHistoryDO.setClassName(classesDictDO.getClassName());
        classesDictHistoryDO.setSchool(classesDictDO.getSchool());
        classesDictHistoryDO.setSubjectType(classesDictDO.getSubjectType());
        classesDictHistoryDO.setStatus("在读");
        classesDictHistoryDO.setClassDictId(classesDictDO.getId());
        classesDictHistoryMapper.insert(classesDictHistoryDO);

        //找到班级所有学生
        List<ClassesUserMappingDO> classesUserMappingDOS = classesUserMappingMapper.selectList(new QueryWrapper<ClassesUserMappingDO>()
                .lambda()
                .eq(ClassesUserMappingDO::getClassesId,addClassDTO.getId()));


        //添加 班级里的科目信息
        List<String> subjects = addClassDTO.getSubjects();
        for (String str : subjects) {
            SubjectDictDO subjectDictDO = new SubjectDictDO();
            subjectDictDO.setSchool(ConstantUtils.SCHOOL);
            subjectDictDO.setGrade(addClassDTO.getGrade());
            subjectDictDO.setClasses(addClassDTO.getClasses());
            subjectDictDO.setClassName(classesDictDO.getClassName());
            subjectDictDO.setSubject(knowledgeSubjectDictMapper.selectById(str).getSubjectName());
            subjectDictDO.setSubjectType("高中");
            subjectDictDO.setSession(classesDictDO.getSession());
            subjectDictDO.setStatus("在读");
            subjectDictMapper.insert(subjectDictDO);

            SubjectSemesterDO subjectSemesterDO = new SubjectSemesterDO();
            subjectSemesterDO.setSubjectId(subjectDictDO.getId());
            subjectSemesterDO.setSemester("上");
            subjectSemesterMapper.insert(subjectSemesterDO);

//            for (ClassesUserMappingDO classesUserMappingDO : classesUserMappingDOS){
//                PtStudentScoreDO ptStudentScoreDO = new PtStudentScoreDO();
//                ptStudentScoreDO.setScore(30.0);
//                ptStudentScoreDO.setScoreRecord(30.0);
//                ptStudentScoreDO.setSemesterId(subjectSemesterDO.getId());
//                ptStudentScoreDO.setStudentId(classesUserMappingDO.getUserId());
//                ptStudentScoreDO.setSubjectId(subjectDictDO.getId());
//            }

            subjectSemesterDO.setSemester("下");
            subjectSemesterMapper.insert(subjectSemesterDO);

//            for (ClassesUserMappingDO classesUserMappingDO : classesUserMappingDOS){
//                PtStudentScoreDO ptStudentScoreDO = new PtStudentScoreDO();
//                ptStudentScoreDO.setScore(30.0);
//                ptStudentScoreDO.setScoreRecord(30.0);
//                ptStudentScoreDO.setSemesterId(subjectSemesterDO.getId());
//                ptStudentScoreDO.setStudentId(classesUserMappingDO.getUserId());
//                ptStudentScoreDO.setSubjectId(subjectDictDO.getId());
//            }

        }

        return ConstantUtils.ADD_SUCCESS;
    }

    public String updateClass(AddClassDTO addClassDTO) {

        ClassesDictHistoryDO classesDictHistoryDO = classesDictHistoryMapper.selectOne(new QueryWrapper<ClassesDictHistoryDO>()
        .lambda()
        .eq(ClassesDictHistoryDO::getStatus,"在读")
        .eq(ClassesDictHistoryDO::getClassDictId,addClassDTO.getId()));

        classesDictHistoryDO.setGrade(addClassDTO.getGrade());
        classesDictHistoryDO.setClasses(addClassDTO.getClasses());
        classesDictHistoryDO.setSession(addClassDTO.getSession());
        classesDictHistoryDO.setClassName(classesDictHistoryDO.getGrade() + "(" + classesDictHistoryDO.getClasses() + ")班");
        classesDictHistoryMapper.updateById(classesDictHistoryDO);

        ClassesDictDO classesDictDO = new ClassesDictDO();
        BeanUtils.copyProperties(classesDictHistoryDO, classesDictDO);
        classesDictDO.setId(addClassDTO.getId());
        classesDictDO.setClassName(classesDictDO.getGrade() + "(" + classesDictDO.getClasses() + ")班");
        this.baseMapper.updateById(classesDictDO);

        //查找该班级下已经有的科目
        List<String> existSubject = subjectDictMapper.getClassSubjectName(classesDictHistoryDO.getGrade(),classesDictHistoryDO.getClasses(),classesDictHistoryDO.getSession());

        //找到班级所有学生
        List<ClassesUserMappingDO> classesUserMappingDOS = classesUserMappingMapper.selectList(new QueryWrapper<ClassesUserMappingDO>()
        .lambda()
        .eq(ClassesUserMappingDO::getClassesId,addClassDTO.getId()));

        List<String> subjects = addClassDTO.getSubjects();
        for (String str : subjects) {
            KnowledgeSubjectDictDO knowledgeSubjectDictDO = knowledgeSubjectDictMapper.selectById(str);
            if (!existSubject.contains(knowledgeSubjectDictDO.getSubjectName())){
                SubjectDictDO subjectDictDO = new SubjectDictDO();
                subjectDictDO.setSchool(ConstantUtils.SCHOOL);
                subjectDictDO.setGrade(addClassDTO.getGrade());
                subjectDictDO.setClasses(addClassDTO.getClasses());
                subjectDictDO.setClassName(classesDictDO.getClassName());
                subjectDictDO.setSubject(knowledgeSubjectDictMapper.selectById(str).getSubjectName());
                subjectDictDO.setSubjectType("高中");
                subjectDictDO.setSession(classesDictDO.getSession());
                subjectDictDO.setStatus("1");
                subjectDictMapper.insert(subjectDictDO);

                SubjectSemesterDO subjectSemesterDO = new SubjectSemesterDO();
                subjectSemesterDO.setSubjectId(subjectDictDO.getId());
                subjectSemesterDO.setSemester("上");
                subjectSemesterMapper.insert(subjectSemesterDO);

                for (ClassesUserMappingDO classesUserMappingDO : classesUserMappingDOS){
                    PtStudentScoreDO ptStudentScoreDO = new PtStudentScoreDO();
                    ptStudentScoreDO.setScore(30.0);
                    ptStudentScoreDO.setScoreRecord(30.0);
                    ptStudentScoreDO.setSemesterId(subjectSemesterDO.getId());
                    ptStudentScoreDO.setStudentId(classesUserMappingDO.getUserId());
                    ptStudentScoreDO.setSubjectId(subjectDictDO.getId());
                }

                subjectSemesterDO.setSemester("下");
                subjectSemesterMapper.insert(subjectSemesterDO);
                for (ClassesUserMappingDO classesUserMappingDO : classesUserMappingDOS){
                    PtStudentScoreDO ptStudentScoreDO = new PtStudentScoreDO();
                    ptStudentScoreDO.setScore(30.0);
                    ptStudentScoreDO.setScoreRecord(30.0);
                    ptStudentScoreDO.setSemesterId(subjectSemesterDO.getId());
                    ptStudentScoreDO.setStudentId(classesUserMappingDO.getUserId());
                    ptStudentScoreDO.setSubjectId(subjectDictDO.getId());
                }
            }
        }

        return ConstantUtils.SUCCESS_UPDATE;
    }

    @Transactional(rollbackFor = Exception.class)
    public String addSubjectTeacherMapping(AddSubjectTeacherDTO addSubjectTeacherDTO) {

        ClassesDictDO classesDictDO = this.baseMapper.selectById(addSubjectTeacherDTO.getClassId());
        List<AddSubjectTeacherMappingDTO> addSubjectTeacherMappingDTOS =  addSubjectTeacherDTO.getAddSubjectTeacherMappingDTOS();

        for (AddSubjectTeacherMappingDTO addSubjectTeacherMappingDTO :addSubjectTeacherMappingDTOS){
            KnowledgeSubjectDictDO knowledgeSubjectDict = knowledgeSubjectDictMapper.selectById(addSubjectTeacherMappingDTO.getSubjectId());

            List<Integer> integers = addSubjectTeacherMappingDTO.getTeacherIds();

            SubjectDictDO subjectDictDO = subjectDictMapper.selectOne(new QueryWrapper<SubjectDictDO>()
                    .lambda()
                    .eq(SubjectDictDO::getGrade, classesDictDO.getGrade())
                    .eq(SubjectDictDO::getSession, classesDictDO.getSession())
                    .eq(SubjectDictDO::getClasses, classesDictDO.getClasses())
                    .eq(SubjectDictDO::getSubject, knowledgeSubjectDict.getSubjectName()));

            userSubjectMappingMapper.delete(new QueryWrapper<UserSubjectMappingDO>()
            .lambda()
            .eq(UserSubjectMappingDO::getSubjectId,subjectDictDO.getId()));

            userSubjectPermissionDictMapper.delete(new QueryWrapper<UserSubjectPermissionDictDO>()
            .lambda()
            .eq(UserSubjectPermissionDictDO::getSubjectId,subjectDictDO.getId()));

            if (null != subjectDictDO) {

                for (Integer integer : integers) {

                    UserSubjectMappingDO userSubjectMappingDO = new UserSubjectMappingDO();
                    userSubjectMappingDO.setSubjectId(subjectDictDO.getId());
                    userSubjectMappingDO.setUserId(integer);
                    userSubjectMappingDO.setClassType(0);
                    userSubjectMappingMapper.insert(userSubjectMappingDO);

                    UserSubjectPermissionDictDO userSubjectPermissionDictDO = new UserSubjectPermissionDictDO();
                    userSubjectPermissionDictDO.setSubjectId(subjectDictDO.getId());
                    userSubjectPermissionDictDO.setUserId(integer);
                    userSubjectPermissionDictMapper.insert(userSubjectPermissionDictDO);
                }
            }
        }
        return ConstantUtils.ADD_SUCCESS;
    }

    public List<GetClassStudentsVO> getClassStudents(GetClassStudentsDTO getClassStudentsDTO) {

//        Page pager = new Page(getClassStudentsDTO.getPageNum(), getClassStudentsDTO.getPageSize());
        return this.baseMapper.getClassStudents(getClassStudentsDTO.getClassId(), getClassStudentsDTO.getUserName());
    }

    public IPage<UserRoleDO> queryNoClassStudents(QueryNoClassStudentsDTO queryNoClassStudentsDTO) {
        Page pager = new Page(queryNoClassStudentsDTO.getPageNum(), queryNoClassStudentsDTO.getPageSize());
        return this.baseMapper.queryNoClassStudents(pager, queryNoClassStudentsDTO.getUserName());
    }

    @Transactional(rollbackFor = Exception.class)
    public String batchAddStudentToClass(BatchAddStudentToClassDTO batchAddStudentToClassDTO) {

        List<Integer> integers = batchAddStudentToClassDTO.getIds();

        for (Integer integer : integers) {
            classesUserMappingMapper.delete(new QueryWrapper<ClassesUserMappingDO>()
                    .lambda()
                    .eq(ClassesUserMappingDO::getUserId, integer));

            ClassesUserMappingDO classesUserMappingDO = new ClassesUserMappingDO();
            classesUserMappingDO.setClassesId(batchAddStudentToClassDTO.getClassId());
            classesUserMappingDO.setUserId(integer);
            classesUserMappingDO.setStatus(1);
            classesUserMappingMapper.insert(classesUserMappingDO);
        }
        return ConstantUtils.ADD_SUCCESS;
    }

    public String batchDeleteStudent(BatchAddStudentToClassDTO batchAddStudentToClassDTO) {

        List<Integer> integers = batchAddStudentToClassDTO.getIds();

        for (Integer integer : integers) {
            classesUserMappingMapper.delete(new QueryWrapper<ClassesUserMappingDO>()
                    .lambda()
                    .eq(ClassesUserMappingDO::getClassesId, batchAddStudentToClassDTO.getClassId())
                    .eq(ClassesUserMappingDO::getUserId, integer));
        }

        return ConstantUtils.DELETE_SUCCESS;
    }

    public List<ClassesDictDO> getClassesWithPage() {
        return this.baseMapper.selectList(new QueryWrapper<ClassesDictDO>()
                .lambda()
                .eq(ClassesDictDO::getStatus, "在读"));
    }

    @Override
    public ClassesDictDO getClassesById(Long studentId) {
        return this.baseMapper.getClassesById(studentId);
    }

    public  String batchUpGrade(BatchUpGradeDTO batchUpGradeDTO){

        List<Integer> ids = batchUpGradeDTO.getIds();

        for (Integer id : ids){
            ClassesDictDO classesDictDO = new ClassesDictDO();
            classesDictDO.setId(id);
            classesDictDO.setUpgradeDate(batchUpGradeDTO.getUpgradeDate());
            this.baseMapper.updateById(classesDictDO);
        }
        return ConstantUtils.SUCCESS_UPDATE;
    }

}