package com.xhkjedu.slive.service.gradeclass;

import com.xhkjedu.slive.mapper.gradeclass.ClassMapper;
import com.xhkjedu.slive.mapper.gradeclass.ClassStudentMapper;
import com.xhkjedu.slive.mapper.gradeclass.GroupStudentMapper;
import com.xhkjedu.slive.mapper.gradeclass.SchoolMapper;
import com.xhkjedu.slive.model.gradeclass.TClass;
import com.xhkjedu.slive.model.gradeclass.TClassStudent;
import com.xhkjedu.slive.model.gradeclass.TGroup;
import com.xhkjedu.slive.model.gradeclass.TGroupStudent;
import com.xhkjedu.slive.vo.gradeclass.ClassVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ywx
 * @classname TClassService
 * @description 
 * @date 2020/6/2 14:57
 **/
@Service
public class ClassService {
    @Resource
    private ClassMapper classMapper;
    @Resource
    private GroupService groupService;
    @Resource
    private ClassStudentMapper classStudentMapper;
    @Resource
    private GroupStudentMapper groupStudentMapper;
    @Resource
    private SchoolMapper schoolMapper;

    //添加
    public Integer save(TClass model) {
        model.setCreatetime(N_Utils.getSecondTimestamp());
        model.setClassnum(0);
        model.setClassstate(1);
        Integer year = schoolMapper.getYearById(model.getSchoolid());
        model.setYear(year);//当前年份必须和学校年份一致
        classMapper.insertUseGeneratedKeys(model);
        //创建全体分组
        TGroup group = new TGroup();
        group.setGroupname("全班学生");
        group.setClassid(model.getId());
        group.setGrouptype(1);
        group.setCreateid(model.getCreateid());
        group.setSchoolid(model.getSchoolid());
        groupService.save(group);
        return model.getId();
    }

    //删除
    public ResultVo deleteById(Integer classid) {
        //2021-06-07删除时执行逻辑删除，不再判断是否有学生
        /*ImportStudentVo student = classStudentMapper.getNumById(classid,0);
        if (student.getClassnum() !=0){
            return new ResultVo(1,"班级已关联学生，暂无法删除");
        }*/
        classMapper.updateClassstateByClassid(classid,10);//删除班级逻辑删除
        return new ResultVo(0,"删除成功");
    }

    //修改
    public Integer update(TClass model) {
        model.setYear(null);//当前年份不能修改
        return classMapper.updateByPrimaryKeySelective(model);
    }

    //更新学生数量
    public void updateNum(Integer classid) {
        classMapper.updateNum(classid);
    }

    //更新班级、分组学生数量
    public void updateNum2(Integer classid) {
        classMapper.updateNum2(classid);
    }

    //基本信息
    public ClassVo getById(Integer classid) {
        return classMapper.getById(classid);
    }

    //学生选课
    public void saveSelection(Integer classid, Integer studentid, Integer teacherid, Integer schoolid) {
        Integer student = classStudentMapper.getByClassIdStudentId(classid, studentid);
        if (student == null) {
            //班级学生
            TClassStudent cs = new TClassStudent();
            cs.setClassid(classid);
            cs.setStudentid(studentid);
            cs.setCreateid(teacherid);
            cs.setSchoolid(schoolid);
            cs.setCreatetime(N_Utils.getSecondTimestamp());
            cs.setXzed(0);//是否是直播行政班学生0是1否（加入的非直播班级学生）
            classStudentMapper.save(cs);

            //分组学生
            ClassVo classVo = getById(classid);
            Integer groupid = classVo.getGroupid();
            TGroupStudent gs = new TGroupStudent();
            gs.setClassid(classid);
            gs.setGroupid(groupid);
            gs.setStudentid(studentid);
            gs.setCreateid(teacherid);
            gs.setSchoolid(schoolid);
            gs.setCreatetime(N_Utils.getSecondTimestamp());
            groupStudentMapper.save(gs);
        }

        //更新班级、分组学生数量
        updateNum2(classid);
    }

    //学生取消选课
    public void cancelSelection(Integer classid, Integer studentid) {
        classStudentMapper.deleteById(classid, studentid);
        //更新班级、分组学生数量
        updateNum2(classid);
    }

    //批量添加报名学生
    public void batchSaveStu(Integer createid, Integer schoolid, Integer classid, List<Integer> studentids) {
        List<TClassStudent> classStudents = new ArrayList<>();
        List<TGroupStudent> groupStudents = new ArrayList<>();
        //班级学生
        TClassStudent cs = new TClassStudent();
        cs.setClassid(classid);
        cs.setCreateid(createid);
        cs.setSchoolid(schoolid);
        cs.setXzed(0);
        cs.setCreatetime(N_Utils.getSecondTimestamp());
        //分组学生
        ClassVo classVo = getById(classid);
        Integer groupid = classVo.getGroupid();
        TGroupStudent gs = new TGroupStudent();
        gs.setClassid(classid);
        gs.setGroupid(groupid);
        gs.setCreateid(createid);
        gs.setSchoolid(schoolid);
        gs.setCreatetime(N_Utils.getSecondTimestamp());
        for (Integer studentid : studentids) {
            TClassStudent classStudent = new TClassStudent();
            BeanUtils.copyProperties(cs, classStudent);
            classStudent.setStudentid(studentid);
            classStudents.add(classStudent);

            TGroupStudent groupStudent = new TGroupStudent();
            BeanUtils.copyProperties(gs, groupStudent);
            groupStudent.setStudentid(studentid);
            groupStudents.add(groupStudent);
        }
        classStudentMapper.insertList(classStudents);
        groupStudentMapper.insertList(groupStudents);
        updateNum2(classid);
    }

    public List<Integer> listClassidsByGradeid(Integer gradeid,Integer schoolid){
        return classMapper.listClassidsByGradeid(gradeid,schoolid);
    }

    //修改班级类型
    public Integer updateClasstype(Integer classid, Integer classtype){
        return classMapper.updateClasstype(classid, classtype);
    }

    public Integer updateClassstateByClassid(Integer classid,Integer classstate){
        return classMapper.updateClassstateByClassid(classid, classstate);
    }
}
