package com.xhkjedu.sapi.service.notice;

import com.xhkjedu.sapi.mapper.notice.NoticeClassMapper;
import com.xhkjedu.sapi.mapper.notice.NoticeMapper;
import com.xhkjedu.sapi.mapper.notice.NoticeSchoolMapper;
import com.xhkjedu.sapi.mapper.notice.NoticeTeacherMapper;
import com.xhkjedu.sapi.model.system.TSchool;
import com.xhkjedu.sapi.model.notice.TNotice;
import com.xhkjedu.utils.N_Utils;
import com.xhkjedu.sapi.vo.gradeclass.ClassVo;
import com.xhkjedu.sapi.vo.notice.NoticeTeacherVo;
import com.xhkjedu.sapi.vo.notice.NoticeVo;
import org.springframework.stereotype.Service;

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

/**
 * @author ywx
 * @classname NoticeService
 * @description 
 * @date 2020/6/12 17:55
 **/
@Service
public class NoticeService {
    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    private NoticeClassMapper noticeClassMapper;
    @Resource
    private NoticeTeacherMapper noticeTeacherMapper;
    @Resource
    private NoticeSchoolMapper noticeSchoolMapper;

    //添加
    public Integer save(TNotice model) {
        if (N_Utils.isEmptyInteger(model.getCreatetime())) {
            model.setCreatetime(N_Utils.getSecondTimestamp());
        }
        noticeMapper.insertUseGeneratedKeys(model);
        model.setNoticeid(model.getId());
        //添加通知对象
        saveNoticeObj(model);

        return model.getId();
    }

    //删除
    public Integer deleteById(Integer noticeid) {
        return noticeMapper.deleteByPrimaryKey(noticeid);
    }

    //修改
    public Integer update(TNotice model) {
        return noticeMapper.updateByPrimaryKeySelective(model);
    }

    //添加通知对象
    private void saveNoticeObj(TNotice model) {
        if (model.getNoticetype() == 1) {//通知对象1班级
            noticeClassMapper.batchSave(model);
        } else if (model.getNoticetype() == 2) {//通知对象2老师
            model.setTeachers(N_Utils.listDistinct(model.getTeachers()));//通知对象去重
            noticeTeacherMapper.batchSave(model);
        }

        if (N_Utils.isListEmpty(model.getSchools())) {//兼容以前添加单个学校模式
            List<TSchool> schools = new ArrayList<>();
            TSchool s = new TSchool();
            s.setSchoolid(model.getSchoolid());
            schools.add(s);
            model.setSchools(schools);
        }
        noticeSchoolMapper.batchSave(model);//通知学校关联信息
    }

    //获取详情
    public TNotice findById(Integer noticeid) {
        TNotice notice = noticeMapper.findById(noticeid);
        Integer noticetype = notice.getNoticetype();
        if (noticetype == 1) {
            List<ClassVo> classes = noticeMapper.listClass(noticeid);
            notice.setClasses(classes);
        } else if (noticetype == 2) {
            List<NoticeTeacherVo> teachers = noticeMapper.listTeacher(noticeid);
            notice.setTeachers(teachers);
        } else {
            List<TSchool> schools = noticeMapper.listSchool(noticeid);
            notice.setSchools(schools);
        }
        return notice;
    }

    //获取列表
    public List <NoticeVo> listAll(TNotice notice) {
        return noticeMapper.listAll(notice);
    }

    /**
     * 教师通知列表
     * @Param [notice]
     * @Author ywx
     * @Date 2020/6/13 9:55
     * @return java.util.List<com.xhkjedu.vo.notice.NoticeVo>
     **/
    public List<NoticeVo> listMy(TNotice notice) {
        return noticeMapper.listMy(notice);
    }

    /**
     * 学生通知列表
     * @Param [notice]
     * @Author ywx
     * @Date 2020/6/22 10:02
     * @return java.util.List<com.xhkjedu.vo.notice.NoticeVo>
     **/
    public List<NoticeVo> listStudent(TNotice notice) {
        return noticeMapper.listStudent(notice);
    }

    /**
     * 教师查看
     * @Param [noticeid, createid]
     * @Author ywx
     * @Date 2020/6/13 11:21
     * @return com.xhkjedu.model.notice.TNotice
     **/
    public TNotice reade(Integer noticeid, Integer createid) {
        TNotice notice = noticeMapper.findById2(noticeid);
        noticeTeacherMapper.reade(noticeid, createid);
        return notice;
    }

    /**
     * 学生查看
     * @Param [noticeid]
     * @Author ywx
     * @Date 2020/6/22 11:00
     * @return com.xhkjedu.model.notice.TNotice
     **/
    public TNotice findById2(Integer noticeid) {
        return noticeMapper.findById2(noticeid);
    }

    /**
     * 教师未看通知数量
     * @Param [teacherid]
     * @Author ywx
     * @Date 2020/7/24 10:37
     * @return java.lang.Integer
     **/
    public Integer noReadNum(Integer teacherid) {
        return noticeMapper.noReadNum(teacherid);
    }

    //根据班级删除通知
    public void deleteByClassId(Integer classid) {
        noticeMapper.deleteByClassId(classid);
    }

    //通知学生--网页--列表
    public List<NoticeVo> listNoticeStuWeb(TNotice notice) {
        return noticeMapper.listNoticeForStuWeb(notice);
    }
}
