LiveApplyJob.java 10.1 KB
package com.cnlive.shenhe.job;

import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.entity.ShyEmail;
import com.cnlive.shenhe.entity.ShyLiveapply;
import com.cnlive.shenhe.entity.ShySites;
import com.cnlive.shenhe.mapper.ShyLiveapplyMapper;
import com.cnlive.shenhe.service.EmailService;
import com.cnlive.shenhe.service.LiveApplyService;
import com.cnlive.shenhe.service.SitesService;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonUtils;
import com.cnlive.shenhe.utils.HttpUtil;
import com.cnlive.shenhe.utils.OpenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Component
@Async
public class LiveApplyJob {

    private static Logger logger = LoggerFactory.getLogger(LiveApplyJob.class);

    /**
     * //开发者对应的id,与app_key相对应
     */
    @Value("${email_app_id}")
    private String email_app_id;
    /**
     * //开发者帐号对应的key. 请开发者谨慎保管此值。
     */
    @Value("${email_app_key}")
    private String email_app_key;
    /**
     * //调用邮件发送服务器的地址
     */
    @Value("${email_send_URL}")
    private String email_send_URL;

    @Value("${mobile_send_URL}")
    private String mobile_send_URL;

    @Autowired
    SitesService sitesService;
    @Autowired
    LiveApplyService liveApplyService;
    @Autowired
    ShyLiveapplyMapper shyLiveApplyMapper;

    @Autowired
    EmailService emailService;

    @Scheduled(fixedRate = 60 * 1000)
    public void updateLiveApply() {

        //遍历免审的spid去更改状态值
        ShyLiveapply shyLiveApply = new ShyLiveapply();
        // 审核类型为免审
        shyLiveApply.setShenhe_type(CommonConst.AUDIT_TYPE_FREE);
        shyLiveApply.setShenhe_state(CommonConst.AUDIT_INTT);
        List<ShyLiveapply> liveApplyList = liveApplyService.findShyLiveApply(shyLiveApply);
        if (liveApplyList.size() > 0) {
            for (ShyLiveapply liveApply : liveApplyList) {
                liveApplyService.updateLiveApply(liveApply.getId(), "", "白名单", CommonConst.AUDIT_SUCCESS, "白名单通过");
            }
        }
    }

    /**
     * 直播申请邮件通知 五分钟一次
     * 检测未通知的邮箱进行通知
     */
    @Scheduled(fixedRate = 5 * 60 * 1000)
    public void autoLiveApplyEmailNotice() {
        Example example = new Example(ShyLiveapply.class);
        Criteria criteria = example.createCriteria();
        //审核通过的
        criteria.andEqualTo("shenhe_state", CommonConst.AUDIT_SUCCESS);
        //状态等于未通知
        criteria.andEqualTo("email_notice_state", CommonConst.NOTICE_INTT);
        List<ShyLiveapply> shyLiveApplyList = shyLiveApplyMapper.selectByExample(example);
        if (shyLiveApplyList.size() > 0) {
            //开发者对应的id,与app_key相对应
            String appId = email_app_id;
            //开发者帐号对应的key. 请开发者谨慎保管此值。
            String appKey = email_app_key;
            //调用邮件发送服务器的地址
            String url = email_send_URL;
            List<ShyEmail> shyEmailList = emailService.getEmailSendList(CommonConst.EMAIL_LIVEAPPLY);
            logger.info("直播申请邮件通知开始,共" + shyLiveApplyList.size() + "个申请,每个申请通知" + shyEmailList.size() + "个邮箱");
            for (ShyLiveapply liveApply : shyLiveApplyList) {
                logger.info("当前发送邮件的直播申请id为:" + liveApply.getId());
                //初始化通知状态
                Integer noticeState = liveApply.getEmail_notice_state();
                //初始化通知消息
                StringBuilder noticeMsg = new StringBuilder(liveApply.getEmail_notice_msg());
                //显示spid简称
                ShySites shySites = sitesService.findspidDescByspid(liveApply.getSp_id());
                if (CommonUtils.isNotNull(shySites)) {
                    liveApply.setSpid_desc(shySites.getName());
                }
                StringBuilder content = new StringBuilder();
                //循环需要发送邮件的邮箱
                for (ShyEmail shyEmail : shyEmailList) {
                    String startTime = CommonUtils.formatDate(liveApply.getPre_start_time(), "yyyy-MM-dd HH:mm:ss");
                    String endTime = CommonUtils.isNotNull(liveApply.getPre_end_time()) ? CommonUtils.formatDate(liveApply.getPre_end_time(), "yyyy-MM-dd HH:mm:ss") : "待定";
                    content.append(liveApply.getSender()).append("__").append(liveApply.getMobile()).append("__").append(liveApply.getOrganization()).append("__").append(liveApply.getTheme()).append("__").append(startTime).append("__").append(endTime).append("__").append(liveApply.getLive_place()).append("__").append(liveApply.getPush_url()).append("__").append(liveApply.getPull_source()).append(",").append(liveApply.getPull_url());
                    //获取一个格式化后的当前时间
                    String dateTime = CommonUtils.getCurrentDate("yyyy-MM-dd HH:mm:ss");
                    String emailAddr = shyEmail.getEmail();
                    String title = liveApply.getSpid_desc() + "新直播开播申请";
                    String templateId = "4017";
                    Map<String, String> emailparams = new HashMap<>(16);
                    emailparams.put("appId", appId);
                    emailparams.put("emailAddr", emailAddr);
                    emailparams.put("title", title);
                    emailparams.put("content", content.toString());
                    emailparams.put("templateId", templateId);
                    emailparams.put("sign", OpenUtil.sign(emailparams, appKey));
                    HttpUtil httpUtil = HttpUtil.init();
                    httpUtil.setParamMap(emailparams);
                    try {
                        Map<String, String> post = httpUtil.post(url);
                        logger.info("当前发送邮件用户为:" + emailAddr + ",发送邮件的直播申请审核云id为:" + liveApply.getId() + "结果为:" + post.get("result"));
                        JSONObject result = JSONObject.parseObject(post.get("result"));
                        if (result == null) {
                            noticeState = CommonConst.NOTICE_FAIL;
                            noticeMsg.append("\n").append(dateTime).append(" 邮件服务器调用失败,result为空");
                            break;
                        }
                        Integer code = result.getInteger("errorCode");
                        if (code != 0) {
                            noticeState = CommonConst.NOTICE_FAIL;
                            noticeMsg.append("\n").append(dateTime).append(" 邮件服务器调用失败,result返回消息为:").append(result.getString("errorMessage"));
                            break;
                        } else {
                            noticeState = CommonConst.NOTICE_SUCCESS;
                            noticeMsg.append("\n").append(dateTime).append(" 邮件").append(emailAddr).append("(").append(shyEmail.getRemark()).append(")发送成功");
                        }
                    } catch (Exception e) {
                        logger.info("异常结果为:{}", e.getMessage());
                        noticeState = CommonConst.NOTICE_FAIL;
                        noticeMsg.append("\n").append(dateTime).append(" 邮件服务器调用失败,服务器报错了");
                        break;
                    }
                }
                liveApply.setEmail_notice_state(noticeState);
                liveApply.setEmail_notice_msg(noticeMsg.toString());
                liveApplyService.updateShyLiveApply(liveApply);
            }
        }
    }

    /**
     * 每个小时执行一次,定时修改直播申请库中的邮箱通知状态
     * 检测邮箱通知失败状态的邮箱状态改为未通知状态
     */
    @Scheduled(fixedRate = 60 * 60 * 1000)
    public void autoLiveApplyEmailNoticeStateModify() {
        Example example = new Example(ShyLiveapply.class);
        Criteria criteria = example.createCriteria();
        //审核通过的
        criteria.andEqualTo("shenhe_state", CommonConst.AUDIT_SUCCESS);
        //状态等于通知失败的
        criteria.andEqualTo("email_notice_state", CommonConst.NOTICE_FAIL);
        List<ShyLiveapply> shyLiveApplyList = shyLiveApplyMapper.selectByExample(example);
        logger.info("邮箱通知失败的修改数量:{}", shyLiveApplyList.size());
        for (ShyLiveapply shyLiveapply : shyLiveApplyList) {
            if (CommonUtils.isNotNull(shyLiveapply)) {
                //修改为未通知 继续发送
                shyLiveapply.setEmail_notice_state(CommonConst.NOTICE_INTT);
                liveApplyService.updateShyLiveApply(shyLiveapply);
            }
        }
    }

    /**
     * 自动退领
     * 每天0点执行
     */
    @Scheduled(cron = "0 0 0 * * ? ")
    public void autoRetReceive() {
        logger.info("直播申请自动退领的进入.....");
        ShyLiveapply liveApply = new ShyLiveapply();
        //审核状态:未审核
        liveApply.setShenhe_state(CommonConst.AUDIT_INTT);
        //已领取
        liveApply.setReceive(CommonConst.RECEIVE_AFTER);
        List<ShyLiveapply> shyLiveapplyList = liveApplyService.findShyLiveApply(liveApply);
        if (shyLiveapplyList.size() > 0) {
            for (ShyLiveapply liveapply : shyLiveapplyList) {
                logger.info("自动退领的liveApply_id:" + liveapply.getId());
                //未领取
                liveapply.setReceive(CommonConst.RECEIVE_BEFORE);
                liveapply.setAuditor(null);
                liveApplyService.updateShyLiveApply(liveApply);
            }
        }
    }
}