ClassNoticeServiceImpl.java 4.63 KB
package com.subsidy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.subsidy.common.constant.CourseNotification;
import com.subsidy.common.exception.HttpException;
import com.subsidy.dto.classNotice.SendNotificationDTO;
import com.subsidy.jobs.CourseNotificationJob;
import com.subsidy.mapper.MemberMapper;
import com.subsidy.model.ClassDictDO;
import com.subsidy.model.ClassNoticeDO;
import com.subsidy.mapper.ClassNoticeMapper;
import com.subsidy.model.MemberDO;
import com.subsidy.service.ClassDictService;
import com.subsidy.service.ClassNoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.util.ConstantUtils;
import com.subsidy.util.DateFormatUtil;
import com.subsidy.util.QuartzUtil;
import com.subsidy.util.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author DengMin
 * @since 2022-01-21
 */
@Service
public class ClassNoticeServiceImpl extends ServiceImpl<ClassNoticeMapper, ClassNoticeDO> implements ClassNoticeService {

    @Autowired
    private QuartzUtil quartzUtil;

    @Autowired
    private ClassDictService classDictService;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private SMSUtils smsUtils;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public String addNotice(ClassNoticeDO classNoticeDO) {
        if(DateFormatUtil.parse(classNoticeDO.getNoticeTime(),"yyyy-MM-dd").before(new Date())) {
            throw new HttpException(70001);
        }

        classNoticeDO.setStatus(CourseNotification.UNSENT);
        this.baseMapper.insert(classNoticeDO);

        ClassDictDO classDictDO = classDictService.getById(classNoticeDO.getClassId());
        Map<String, Object> params = new HashMap<>();
        params.put("classId", classNoticeDO.getClassId());
        params.put("id", classNoticeDO.getId());
        String name = classDictDO.getClassName()+"-"+classNoticeDO.getNoticeType()+"-"+classNoticeDO.getNoticeTime();
        quartzUtil.addSimpleJob(CourseNotificationJob.class,DateFormatUtil.parse(classNoticeDO.getNoticeTime(), "yyyy-MM-dd") , params, name, "CourseNotificationJob");
        return ConstantUtils.ADD_SUCCESS;
    }

    public String updateNotice(ClassNoticeDO classNoticeDO) {
        this.baseMapper.updateById(classNoticeDO);
        return ConstantUtils.SET_SUCCESS;
    }

    @Transactional(rollbackFor = Exception.class)
    public String deleteNotice(ClassNoticeDO classNoticeDO) {
        ClassNoticeDO noticeDO = this.baseMapper.selectById(classNoticeDO.getId());

        this.baseMapper.deleteById(classNoticeDO.getId());

        ClassDictDO classDictDO = classDictService.getById(noticeDO.getClassId());
        String name = classDictDO.getClassName()+"-"+noticeDO.getNoticeType()+"-"+noticeDO.getNoticeTime();
        quartzUtil.deleteJob(name, "CourseNotificationJob");
        return ConstantUtils.DELETE_SUCCESS;
    }

    public List<ClassNoticeDO> queryClassNotices(ClassNoticeDO classNoticeDO){
        return this.baseMapper.selectList(new QueryWrapper<ClassNoticeDO>()
        .lambda()
        .eq(ClassNoticeDO::getStatus, classNoticeDO.getStatus())
        .eq(ClassNoticeDO::getClassId,classNoticeDO.getClassId()));
    }

    @Override
    public void sendNotification(SendNotificationDTO sendNotificationDTO) {
        if(sendNotificationDTO.getSendType().equals(CourseNotification.ALL)) {
            List<MemberDO> list = memberMapper.getMemberList(sendNotificationDTO.getClassId());
            if(list != null) {
                for (MemberDO memberDO : list) {
                    smsUtils.send(memberDO.getTelephone(), "");
                }
            }
        } else if(sendNotificationDTO.getSendType().equals(CourseNotification.NOT_SIGNED_IN)) {
            List<MemberDO> list = memberMapper.getMemberListBySignInRecord(sendNotificationDTO.getClassId());
            if(list != null) {
                for (MemberDO memberDO : list) {
                    smsUtils.send(memberDO.getTelephone(), "");
                }
            }
        } else if(sendNotificationDTO.getSendType().equals(CourseNotification.UNFINISHED)) {
            List<MemberDO> list = memberMapper.getUnfinishedMemberList(sendNotificationDTO.getClassId());
            if(list != null) {
                for (MemberDO memberDO : list) {
                    smsUtils.send(memberDO.getTelephone(), "");
                }
            }
        }
    }
}