CourseNotificationJob.java 3.24 KB
package com.subsidy.jobs;

import com.subsidy.common.constant.CourseNotification;
import com.subsidy.common.constant.SmsCode;
import com.subsidy.mapper.ClassDictMapper;
import com.subsidy.mapper.ClassNoticeMapper;
import com.subsidy.mapper.MemberMapper;
import com.subsidy.model.ClassNoticeDO;
import com.subsidy.model.MemberDO;
import com.subsidy.util.SMSUtils;
import com.subsidy.vo.classdict.ClassAndCompanyInfoVO;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程通知
 * </p>
 *
 * @author DengMin
 * @since 2022/2/14
 */
@Component
public class CourseNotificationJob implements Job {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private ClassNoticeMapper classNoticeMapper;

    @Autowired
    private ClassDictMapper classDictMapper;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        Map<String, Object> map = jobExecutionContext.getJobDetail().getJobDataMap();
        Map<String, Object> params = (Map<String, Object>) map.get("params");
        if(params != null) {
            ClassNoticeDO classNoticeDO = classNoticeMapper.selectById((Long) params.get("id"));
            if(classNoticeDO != null) {
                ClassAndCompanyInfoVO cmInfo = classDictMapper.getClassAndCompanyInfoVO((Long) params.get("classId"));
                List<MemberDO> list = memberMapper.getMemberList((Long) params.get("classId"));
                if(list != null && list.size() > 0) {
                    for (MemberDO memberDO : list) {
                        if(memberDO != null) {
                            String sms = "";
                            if(classNoticeDO.getNoticeType().equals(SmsCode.CLASS_START.getType())) {
                                sms = "{\"company\":\""+ cmInfo.getCompany()  +"\", \"course\":\""+ cmInfo.getCourseName() +"\", \"startDate\": \""+ cmInfo.getStartDate()+"\", \"endDate\": \""+ cmInfo.getEndDate()+"\"}";
                            } else if(classNoticeDO.getNoticeType().equals(SmsCode.SIGN_IN.getType())) {
                                sms = "{ \"name\": \""+ cmInfo.getName()  +"\", \"course\":\""+ cmInfo.getCourseName()+"\"}";
                            } else if(classNoticeDO.getNoticeType().equals(SmsCode.TEST.getType())) {
                                sms = "{ \"name\": \""+ cmInfo.getName()  +"\", \"course\":\""+ cmInfo.getCourseName()+"\"}";
                            }
                            Map<String, String> data = Arrays.stream(SmsCode.values()).collect(Collectors.toMap(SmsCode::getType, SmsCode::getCode));
                            SMSUtils.sendNoticeSMS(data.get(classNoticeDO.getNoticeType()), memberDO.getTelephone(), sms);
                        }
                    }
                }
                ClassNoticeDO classNotice = new ClassNoticeDO();
                classNotice.setId(classNoticeDO.getId());
                classNotice.setStatus(CourseNotification.SENT);
                classNoticeMapper.updateById(classNotice);
            }
        }
    }
}