package com.xhkjedu.suser.service.gradeclass;

import com.xhkjedu.suser.mapper.gradeclass.GroupMapper;
import com.xhkjedu.suser.mapper.gradeclass.SchoolMapper;
import com.xhkjedu.suser.model.gradeclass.TGroup;
import com.xhkjedu.suser.model.gradeclass.TSchool;
import com.xhkjedu.suser.vo.gradeclass.ClassGroupVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

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

/**
 * @author ywx
 * @classname GroupService
 * @description
 * @date 2020/6/2 17:26
 **/
@Service
public class GroupService {
    @Resource
    private GroupMapper groupMapper;
    @Resource
    private GroupStudentService groupStudentService;
    @Resource
    private SchoolMapper schoolMapper;

    //添加
    public Integer save(TGroup model) {
        model.setGroupnum(0);
        model.setCreatetime(N_Utils.getSecondTimestamp());
        groupMapper.insertUseGeneratedKeys(model);
        if (N_Utils.isListNotEmpty(model.getStudents())) {
            model.setGroupid(model.getId());
            groupStudentService.batchSave(model);//批量添加分组学生
        }
        return model.getId();
    }

    //删除
    public Integer deleteById(Integer groupid) {
        return groupMapper.deleteByPrimaryKey(groupid);
    }

    //修改
    public Integer update(TGroup model) {
        if (N_Utils.isListEmpty(model.getStudents())){//如果分组学生为空直接删除分组
            return deleteById(model.getGroupid());
        }
        groupStudentService.deleteByGroupId(model.getGroupid());//先删除分组学生
        if (N_Utils.isListNotEmpty(model.getStudents())){
            groupStudentService.batchSave(model);//在批量添加分组学生
        }
        updateNum(model.getGroupid());//更新分组人数
        model.setGroupnum(null);
        return groupMapper.updateByPrimaryKeySelective(model);
    }

    //获取列表
    public List <TGroup> findAll(Integer classid, Integer createid) {
        return groupMapper.findAll(classid, createid);
    }

    //更新分组人数
    public void updateNum(Integer groupid) {
        groupMapper.updateNum(groupid);
    }

    //教师分组列表
    public List<ClassGroupVo> teacherList(Integer teacherid) {
        TSchool school = schoolMapper.getSchoolInfoByUserid(teacherid);
        Integer year = school.getYear();
        return groupMapper.teacherList(teacherid, year, school.getSchoolid());
    }

    //伙伴学校班级分组列表
    public List<Map> teacherListJtClass(Integer schoolid, String subjectid, Integer teacherid) {
        return groupMapper.teacherListJtClass(schoolid, subjectid, teacherid);
    }
}
