LiveApplyJob.java 9.84 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 = 60 * 1000)
    public void liveApplyNotice() {
        Example example = new Example(ShyLiveapply.class);
        Criteria criteria = example.createCriteria();
        criteria.andEqualTo("shenhe_state", CommonConst.AUDIT_SUCCESS);
        //状态不等于已通知
        criteria.andNotEqualTo("email_notice_state", CommonConst.NOTICE_SUCCESS);
        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() + "个邮箱");
            int i = 0;
            for (ShyLiveapply liveApply : shyLiveApplyList) {
                logger.info("当前发送邮件的直播申请id为:" + liveApply.getId());
                //初始化通知状态
                Integer noticeState = liveApply.getEmail_notice_state();
                //初始化通知消息
                String noticeMsg = liveApply.getEmail_notice_msg();
                //显示spid简称
                ShySites shySites = sitesService.findspidDescByspid(liveApply.getSp_id());
                if (CommonUtils.isNotNull(shySites)) {
                    liveApply.setSpid_desc(shySites.getName());
                }
                //循环需要发送邮件的邮箱
                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") : "待定";
                    String content = liveApply.getSender() + "__" + liveApply.getMobile() + "__" + liveApply.getOrganization() + "__" + liveApply.getTheme() + "__" + startTime + "__" + endTime + "__" + liveApply.getLive_place() + "__" + liveApply.getPush_url() + "__" + liveApply.getPull_source() + "," + 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<>();
                    emailparams.put("appId", appId);
                    emailparams.put("emailAddr", emailAddr);
                    emailparams.put("title", title);
                    emailparams.put("content", content);
                    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 = noticeMsg + "\n" + dateTime + " 邮件服务器调用失败,result为空";
                            i += 1;
                            break;
                        }
                        Integer code = result.getInteger("errorCode");
                        if (code != 0) {
                            noticeState = CommonConst.NOTICE_FAIL;
                            noticeMsg = noticeMsg + "\n" + dateTime + " 邮件服务器调用失败,result返回消息为:" + result.getString("errorMessage");
                            i += 1;
                            break;
                        } else {
                            noticeState = CommonConst.NOTICE_SUCCESS;
                            noticeMsg = noticeMsg + "\n" + dateTime + " 邮件" + emailAddr + "(" + shyEmail.getRemark() + ")发送成功";
                        }
                    } catch (Exception e) {
                        logger.info("异常结果为:{}", e.getMessage());
                        noticeState = CommonConst.NOTICE_FAIL;
                        noticeMsg = noticeMsg + "\n" + dateTime + " 邮件服务器调用失败,服务器报错了";
                        i += 1;
                        break;
                    }
                    String taskId = "003_01";
                    String signatureId = "0";
                    Map<String, String> mobileparams = new HashMap<>(16);
                    mobileparams.put("appId", appId);
                    mobileparams.put("mobile", liveApply.getMobile());
                    mobileparams.put("content", content);
                    mobileparams.put("taskId", taskId);
                    mobileparams.put("signatureId", signatureId);
                    httpUtil.setParamMap(mobileparams);
                    if (i > 10) {
                        Map<String, String> post = httpUtil.post(mobile_send_URL);
                        logger.info("当前发送短信用户为:" + liveApply.getMobile() + ",发送短信的直播申请审核云id为:" + liveApply.getId() + "结果为:" + post.get("result"));
                        noticeState = CommonConst.NOTICE_SUCCESS;
                        noticeMsg = noticeMsg + "\n" + dateTime + "短信手机号" + liveApply.getMobile() + "(" + liveApply.getSender() + ")发送成功";
                    }
                }
                liveApply.setEmail_notice_state(noticeState);
                liveApply.setEmail_notice_msg(noticeMsg);
                liveApplyService.updateShyLiveApply(liveApply);
            }
        }
    }

    /**
     * 自动退领
     * 每天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);
            }
        }
    }
}