package com.xhkjedu.slive.service.gradeclass;

import com.xhkjedu.slive.mapper.gradeclass.GroupMapper;
import com.xhkjedu.slive.mapper.gradeclass.GroupStudentMapper;
import com.xhkjedu.slive.model.gradeclass.TGroup;
import com.xhkjedu.slive.model.gradeclass.TGroupStudent;
import com.xhkjedu.slive.vo.gradeclass.GroupStudentVo;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

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

/**
 * @author ywx
 * @classname TGroupStudentService
 * @description 
 * @date 2020/6/2 16:57
 **/
@Service
public class GroupStudentService {
    @Resource
    private GroupStudentMapper groupStudentMapper;
    @Resource
    private GroupMapper groupMapper;

    //添加
    public Integer save(TGroupStudent model) {
        model.setCreatetime(N_Utils.getSecondTimestamp());
        groupStudentMapper.insertUseGeneratedKeys(model);
        groupMapper.updateNum(model.getGroupid());//更新分组人数
        return model.getId();
    }

    //批量添加
    public void batchSave(TGroup group) {
        Integer classid = group.getClassid();
        Integer groupid = group.getGroupid();
        Integer createid = group.getCreateid();
        Integer schoolid = group.getSchoolid();
        List<GroupStudentVo> students = group.getStudents();
        List<TGroupStudent> groupStudents = new ArrayList<>();
        for (GroupStudentVo student : students) {
            TGroupStudent gs = new TGroupStudent();
            gs.setClassid(classid);
            gs.setGroupid(groupid);
            gs.setStudentid(student.getStudentid());
            gs.setCreateid(createid);
            gs.setSchoolid(schoolid);
            gs.setCreatetime(N_Utils.getSecondTimestamp());
            groupStudents.add(gs);
        }
        groupStudentMapper.insertList(groupStudents);
        groupMapper.updateNum(groupid);//更新分组人数
    }

    //删除
    public void deleteById(TGroupStudent group) {
        groupStudentMapper.deleteByPrimaryKey(group.getGsid());
        groupMapper.updateNum(group.getGroupid());//更新分组人数
    }
}
