package com.xhkjedu.sstudy.service.system;

import com.xhkjedu.sstudy.mapper.gradeclass.ClassMapper;
import com.xhkjedu.sstudy.mapper.system.NoticeUserMapper;
import com.xhkjedu.sstudy.mapper.system.NoticeMapper;
import com.xhkjedu.sstudy.mapper.system.SchoolMapper;
import com.xhkjedu.sstudy.model.gradeclass.TClass;
import com.xhkjedu.sstudy.model.paperstudent.TPaperStudentBack;
import com.xhkjedu.sstudy.model.system.*;
import com.xhkjedu.utils.N_Utils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

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

/**
 * @Description：通知
 * @Author: WN
 * @Date: 2023/6/13 11:17:40
 **/
@Service
public class NoticeService {

    @Resource
    private NoticeMapper noticeMapper;
    @Resource
    private NoticeUserMapper noticeUserMapper;
    @Resource
    private SchoolMapper schoolMapper;
    @Resource
    private ClassMapper classMapper;

    @Async("asyncPoolTaskExecutor")
    public void save(Map map, TPaperStudentBack psb){
        Integer starttime = Integer.parseInt(map.get("starttime").toString());
        Integer stoptime = Integer.parseInt(map.get("stoptime").toString());
        String starttimestr = N_Utils.getStrtimeTimestamp(starttime,"yyyy-MM-dd HH:mm");
        String stoptimestr = N_Utils.getStrtimeTimestamp(stoptime,"yyyy-MM-dd HH:mm");

        // Integer committime = Integer.parseInt(map.get("committime").toString());
        // String committimestr = N_Utils.getStrtimeTimestamp(committime,"yyyy-MM-dd HH:mm");

        String content = "同学你好,本次退回作业：" +
                map.get("papername").toString() + "(" + map.get("subjectname").toString()  + ")" +
                map.get("username").toString() + "发布,作业时间： " +
                starttimestr+"-"+stoptimestr + "<br/><br/>" +
                "退回原因：" + map.get("comm").toString();

        //获取当前发起退回教师学校id
        Integer schoolid = schoolMapper.getSchoolIdByUserid(psb.getCreateid());

        //给该学生发送通知
        TNotice notice = new TNotice();
        notice.setNoticetitle(map.get("papername").toString() + "-作业退回(" + map.get("subjectname").toString() + ")");
        notice.setNoticecontent(content);
        notice.setNoticetxt(content.replace("<br/>",""));
        notice.setNoticetype(3);
        notice.setCreateid(psb.getCreateid());
        notice.setCreatetime(psb.getCreatetime());
        notice.setSchoolid(schoolid);
        noticeMapper.insertUseGeneratedKeys(notice);
        //保存学生通知内容
        TNoticeUser nu = new TNoticeUser();
        nu.setNoticeid(notice.getId());
        nu.setUserid(Integer.parseInt(map.get("studentid").toString()));
        Integer classid = N_Utils.str2Int(map.get("classid").toString());
        TClass tClass = classMapper.selectByPrimaryKey(classid);
        nu.setGradeid(tClass.getGradeid());
        nu.setClassid(classid);
        nu.setCreatetime(psb.getCreatetime());
        noticeUserMapper.insertUseGeneratedKeys(nu);
    }
}
