package com.xhkjedu.sstudy.service.subjectbook;

import com.xhkjedu.exception.MissingParametersException;
import com.xhkjedu.sstudy.mapper.subjectbook.TeacherSubjectbookMapper;
import com.xhkjedu.sstudy.mapper.system.SchoolMapper;
import com.xhkjedu.sstudy.model.subjectbook.TDirector;
import com.xhkjedu.sstudy.model.subjectbook.TTeacherSubjectbook;
import com.xhkjedu.sstudy.vo.subjectbook.TeacherSClassVo;
import com.xhkjedu.sstudy.vo.subjectbook.TeacherSubjectbookVo;
import com.xhkjedu.utils.N_Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

/**
 * @author ywx
 * @classname TeacherSubjectbookService
 * @description 
 * @date 2020/6/5 15:41
 **/
@Service
@Slf4j
public class TeacherSubjectbookService {
    @Resource
    private TeacherSubjectbookMapper teacherSubjectbookMapper;
    @Autowired
    private DirectorService directorService;
    @Resource
    private SchoolMapper schoolMapper;

    //添加
    public Integer save(TTeacherSubjectbook model) {
        TTeacherSubjectbook sb = getObjByLsbIdTeacherId(model.getLsbid(), model.getTeacherid());
        Integer tbid = sb == null ? null : sb.getTbid();
        if (sb == null) {
            //根据教材id获取科目id
            String subjectid = teacherSubjectbookMapper.getSubjectidByLsbid(model.getLsbid());
            model.setSubjectid(subjectid);
            model.setSelected(1);
            model.setCreatetime(N_Utils.getSecondTimestamp());
            teacherSubjectbookMapper.insertUseGeneratedKeys(model);
            tbid = model.getId();
            updateSelected(tbid);//更改教师教材关系是否选中

            //教师收藏教材下的章节数量
            Integer directorNum = directorService.countByTeacherIdLsbId(model.getTeacherid(), model.getLsbid());
            if (directorNum == 0) {
                //初始化章节
                TDirector director = new TDirector();
                director.setLsbid(model.getLsbid());
                director.setBelong(3);
                director.setSchoolid(model.getSchoolid());
                List<TDirector> directors = directorService.lisTDirectors(director);
                Integer schoolid = model.getSchoolid();
                Integer teacherid = model.getTeacherid();
                for (TDirector d : directors) {
                    d.setBelong(4);
                    d.setCreateid(teacherid);
                    d.setSchoolid(schoolid);
                    d.setCreatetime(N_Utils.getSecondTimestamp());
                    String directorid = directorService.addAndReturnKey(d);
                    d.setDirectorid(directorid);
                    saveDirectorChild(d);//保存子章节信息
                }
            }
        } else if(sb.getSelected() == 0) {
            updateSelected(tbid);//更改教师教材关系是否选中
        }
        return tbid;
    }

    /**
     * 保存子章节信息
     * @Param [d]
     * @Author ywx
     * @Date 2020/7/6 15:14
     * @return void
     **/
    private void saveDirectorChild(TDirector d) {
        String directorid;
        List<TDirector> childDirectors = d.getChildren();
        if (N_Utils.isListNotEmpty(childDirectors)) {
            for (TDirector d2 : childDirectors) {
                d2.setDirectorpid(d.getDirectorid());
                d2.setBelong(4);
                d2.setCreateid(d.getCreateid());
                d2.setSchoolid(d.getSchoolid());
                d2.setCreatetime(N_Utils.getSecondTimestamp());
                directorid = directorService.addAndReturnKey(d2);
                d2.setDirectorid(directorid);
                if (N_Utils.isListNotEmpty(d2.getChildren())) {
                    saveDirectorChild(d2);
                }
            }
        }
    }

    /**
     * 根据教材id教师id获取教师教材关系信息
     * @Param [lsbid, teacherid]
     * @Author ywx
     * @Date 2020/7/1 11:31
     * @return com.xhkjedu.model.subjectbook.TTeacherSubjectbook
     **/
    private TTeacherSubjectbook getObjByLsbIdTeacherId(String lsbid, Integer teacherid) {
        return teacherSubjectbookMapper.getObjByLsbIdTeacherId(lsbid, teacherid);
    }

    //删除
    public Integer deleteById(Integer tbid) {
        return teacherSubjectbookMapper.deleteByPrimaryKey(tbid);
    }

    //获取列表
    public List <TeacherSubjectbookVo> findAll(TTeacherSubjectbook book) {
        return teacherSubjectbookMapper.findAll(book);
    }

    //更改教师教材关系是否选中
    @Transactional(rollbackFor = Exception.class)
    public void updateSelected(Integer tbid) {
        try {
            //获取教材对应的教师
            TTeacherSubjectbook sb= teacherSubjectbookMapper.selectByPrimaryKey(tbid);
            //设置教师所有教材为非选中状态，
            teacherSubjectbookMapper.updateNoSelected(sb.getTeacherid());
            //现更改选中的，然后把选中的
            teacherSubjectbookMapper.updateSelected(tbid);
        }catch (Exception e){
            log.info("修改教师教材关系是否选中出错：" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            throw new MissingParametersException(e.getMessage());
        }

    }

    //学生教材列表
    public List<TeacherSubjectbookVo> listStuBook(String subjectid, Integer studentid) {
        Integer year = schoolMapper.getYearByUserId(studentid);
        return teacherSubjectbookMapper.listStuBook(subjectid,year,studentid);
    }

    //获取教师年级下班级列表
    public TeacherSClassVo listBookClass(Integer teacherid, Integer classtype) {
        Integer year = schoolMapper.getYearByUserId(teacherid);
        TeacherSClassVo list = teacherSubjectbookMapper.listBookClass(teacherid, year, classtype);
        return list;
    }

    //获取教师权限教材科目下所有教材列表
    public List<TeacherSubjectbookVo> listSubjectLsb(Integer teacherid) {
        return teacherSubjectbookMapper.listSubjectLsb(teacherid);
    }
}
