package com.xhkjedu.slive.service.liveplay;

import com.xhkjedu.slive.mapper.gradeclass.ClassTeacherMapper;
import com.xhkjedu.slive.mapper.gradeclass.SchoolMapper;
import com.xhkjedu.slive.mapper.liveplay.CourseClassMapper;
import com.xhkjedu.slive.mapper.liveplay.CourseMapper;
import com.xhkjedu.slive.mapper.liveplay.CsMapper;
import com.xhkjedu.slive.model.gradeclass.TClass;
import com.xhkjedu.slive.model.gradeclass.TClassTeacher;
import com.xhkjedu.slive.model.liveplay.LCourse;
import com.xhkjedu.slive.model.liveplay.LCourseClass;
import com.xhkjedu.slive.service.gradeclass.ClassService;
import com.xhkjedu.slive.vo.liveplay.CourseParams;
import com.xhkjedu.slive.vo.liveplay.CourseVo;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

/**
 * @author ywx
 * @classname CourseService
 * @description
 * @date 2021/1/15 12:33
 **/
@Service
@Slf4j
public class CsService {
    @Resource
    private CourseMapper courseMapper;
    @Resource
    private ClassService classService;
    @Resource
    private ClassTeacherMapper classTeacherMapper;
    @Resource
    private SchoolMapper schoolMapper;
    @Resource
    private CourseClassMapper courseClassMapper;
    @Resource
    private CsMapper csMapper;

    //添加
    @Transactional(rollbackFor = Exception.class)
    public ResultVo save(LCourse model) {
        try {
            List<Map> users = new ArrayList<>();
            if (model.getCoursetype() == 1) {
                if (N_Utils.isListEmpty(model.getClassids())) {
                    return new ResultVo(1, "班级id集合不能为空");
                }
                users = courseMapper.listUid(model.getClassids());//获取课程学生id集合
                if (N_Utils.isListEmpty(users)) {
                    return new ResultVo(1, "直播班级学生为空");
                }
            }
            model.setStunum(0);
            model.setCoursestate(0);
            model.setCreatetime(N_Utils.getSecondTimestamp());
            String coursename = model.getCoursename();
            Integer createid = model.getCreateid();
            Integer schoolid = model.getSchoolid();
            Integer teacherid = model.getTeacherid();
            //班级信息
            TClass c = new TClass();
            c.setClassname(coursename + "班");
            c.setCreateid(createid);
            c.setSchoolid(schoolid);
            Integer year = schoolMapper.getYearById(schoolid);
            c.setSchoolyear(year);
            c.setYear(year);
            c.setClassorder(999);
            c.setGradeid(model.getGradeid());

            if (model.getCoursetype() == 1) {
                c.setClasstype(99);//已直播班级
            } else {
                c.setClasstype(3);//直播班(学生选班)
            }

            Integer classid = classService.save(c);
            if (!N_Utils.isEmptyInteger(teacherid)) {
                //班级教师
                TClassTeacher teacher = new TClassTeacher();
                teacher.setClassid(classid);
                teacher.setTeacherid(teacherid);
                teacher.setCreateid(createid);
                teacher.setCreatetime(N_Utils.getSecondTimestamp());
                teacher.setSchoolid(schoolid);

                classTeacherMapper.insertSelective(teacher);
            }
            //课程信息
            model.setClassid(classid);
            model.setDeleted(1);
            model.setCourseclass(0);
            model.setCourseschool(1);
            //学校数量
            Integer schoolnum = csMapper.getSchoolNumByCids(model.getClassids());
            model.setSchoolnum(schoolnum);
            courseMapper.insertUseGeneratedKeys(model);
            if (model.getCoursetype() == 1) {
                Integer courseid = model.getId();
                List<Integer> studentids = new ArrayList<>();
                users.forEach(userVo -> {
                    studentids.add((Integer) userVo.get("userid"));
                });

                classService.batchSaveStu(createid, schoolid, classid, studentids);//批量添加报名学生
                courseMapper.updateStuNum(courseid);//更新已报学生数量

                //保存直播关联班级
                List<LCourseClass> cclist = new ArrayList<>();
                for(Integer cid : model.getClassids()){
                    LCourseClass cc = new LCourseClass();
                    cc.setCourseid(courseid);
                    cc.setClassid(cid);
                    cc.setCreateid(model.getCreateid());
                    cc.setCreatetime(N_Utils.getSecondTimestamp());
                    cclist.add(cc);
                }
                courseClassMapper.insertList(cclist);//保存原班级和直播的关联关系
            }

            return new ResultVo(0, "保存成功", model.getId());
        } catch (BeansException e) {
            log.error("添加课程失败", e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResultVo(1, "保存失败");
        }
    }

    //获取列表
    public List<CourseVo> listAll(LCourse course) {
        return csMapper.listAll(course);
    }

    //课程班级
    public List<Map> listClass(Integer courseid) {
        return csMapper.listClass(courseid);
    }

    //我的月份下日期课节数量
    public List<Map> listMySectionDate(CourseParams params) {
        return csMapper.listMySectionDate(params);
    }

    //我的课程列表
    public List<Map> listMySection(CourseParams params) {
        int timestamp = N_Utils.getSecondTimestamp();
        String strtime = N_Utils.getStrtimeTimestamp(timestamp, "yyyy-MM-dd HH:mm");
        params.setStrtime(strtime);
        return csMapper.listMySection(params);
    }
}
