package com.xhkjedu.slive.service.notice;

import com.xhkjedu.slive.mapper.notice.NoticeClassMapper;
import com.xhkjedu.slive.mapper.notice.NoticeMapper;
import com.xhkjedu.slive.mapper.notice.NoticeSchoolMapper;
import com.xhkjedu.slive.model.gradeclass.TSchool;
import com.xhkjedu.slive.model.notice.TNotice;
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 NoticeService
 * @description 
 * @date 2020/6/12 17:55
 **/
@Service
public class NoticeService {
    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    private NoticeClassMapper noticeClassMapper;
    @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();
    }

    //添加通知对象
    private void saveNoticeObj(TNotice model) {
        if (model.getNoticetype() == 1) {//通知对象1班级
            noticeClassMapper.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);//通知学校关联信息
    }
}
