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

import com.alibaba.fastjson.JSONObject;
import com.cnlive.shenhe.bean.ErrorEnum;
import com.cnlive.shenhe.bean.ResponseBean;
import com.cnlive.shenhe.entity.ShyChannel;
import com.cnlive.shenhe.entity.ShyComments;
import com.cnlive.shenhe.entity.ShyEmail;
import com.cnlive.shenhe.entity.ShyLiveapply;
import com.cnlive.shenhe.entity.ShySites;
import com.cnlive.shenhe.entity.ShyVideos;
import com.cnlive.shenhe.mapper.ShyLiveapplyMapper;
import com.cnlive.shenhe.serviceImpl.ChannelServiceImpl;
import com.cnlive.shenhe.serviceImpl.CommentsServiceImpl;
import com.cnlive.shenhe.serviceImpl.EmailServiceImpl;
import com.cnlive.shenhe.serviceImpl.LiveApplyServiceImpl;
import com.cnlive.shenhe.serviceImpl.SitesServiceImpl;
import com.cnlive.shenhe.serviceImpl.UsersServiceImpl;
import com.cnlive.shenhe.utils.AuditPass;
import com.cnlive.shenhe.utils.CommonConst;
import com.cnlive.shenhe.utils.CommonUtils;
import com.cnlive.shenhe.utils.HttpUtil;
import com.cnlive.shenhe.utils.InfoUtil;
import com.cnlive.shenhe.utils.OpenUtil;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

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.Scheduled;
import org.springframework.stereotype.Component;

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

@Component
public class LiveApplyJob {
    private static Logger logger = LoggerFactory.getLogger(LiveApplyJob.class);
    @Value("${spring.localhost.url}")
	private String localhost_url;//本机地址
    @Value("${email_app_id}")
    private String email_app_id;//开发者对应的id,与app_key相对应
    @Value("${email_app_key}")
    private String email_app_key;//开发者帐号对应的key. 请开发者谨慎保管此值。
    @Value("${email_send_URL}")
    private String email_send_URL;//调用邮件发送服务器的地址
	
    @Autowired
    SitesServiceImpl sitesService;
    @Autowired
    AuditPass Pass;
    @Autowired
    InfoUtil infoUtil;
    @Autowired
    LiveApplyServiceImpl liveApplyServiceImpl;
    @Autowired
    ShyLiveapplyMapper shyLiveApplyMapper;

    @Autowired
    UsersServiceImpl usersService;
    @Autowired
    EmailServiceImpl emailServiceImpl;

    @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 = liveApplyServiceImpl.findshyLiveApply(shyLiveApply);
            if (liveApplyList.size() > 0) {
                for (ShyLiveapply liveApply : liveApplyList) {
                	liveApplyServiceImpl.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) {
        	String app_id = email_app_id; //开发者对应的id,与app_key相对应
        	String app_key = email_app_key; //开发者帐号对应的key. 请开发者谨慎保管此值。
        	String URL = email_send_URL;//调用邮件发送服务器的地址
        	List<ShyEmail> shyEmailList=emailServiceImpl.getEmailSendList(CommonConst.EMAIL_LIVEAPPLY);
        	logger.info("直播申请邮件通知开始,共"+shyLiveApplyList.size()+"个申请,每个申请通知"+shyEmailList.size()+"个邮箱");
        	for(ShyLiveapply liveApply : shyLiveApplyList){
        		logger.info("当前发送邮件的直播申请id为:"+liveApply.getId());
        		Integer notice_state = liveApply.getEmail_notice_state();//初始化通知状态
        		String notice_msg = 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> params = new HashMap<>();
        			params.put("appId", app_id);
        			params.put("emailAddr", emailAddr);
        			params.put("title",title );
        			params.put("content", content);
        			params.put("templateId", templateId);
        			params.put("sign", OpenUtil.sign(params,app_key));
        			HttpUtil httpUtil = HttpUtil.init();
        			httpUtil.setParamMap(params);
        			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) {
        					notice_state=CommonConst.NOTICE_FAIL;
        					notice_msg=notice_msg+"\n"+dateTime+" 邮件服务器调用失败,result为空";
        					break;
        				}
        				Integer code = result.getInteger("errorCode");
        				if (code != 0) {
        					notice_state=CommonConst.NOTICE_FAIL;
        					notice_msg=notice_msg+"\n"+dateTime+" 邮件服务器调用失败,result返回消息为:"+result.getString("errorMessage");
        					break;
        				}else{
        					notice_state=CommonConst.NOTICE_SUCCESS;
        					notice_msg=notice_msg+"\n"+dateTime+" 邮件"+emailAddr+"("+shyEmail.getRemark()+")发送成功";
        				}
        			} catch (Exception e) {
        				notice_state=CommonConst.NOTICE_FAIL;
        				notice_msg=notice_msg+"\n"+dateTime+" 邮件服务器调用失败,服务器报错了";
        				break;
        			}
        		}
                
                liveApply.setEmail_notice_state(notice_state);
                liveApply.setEmail_notice_msg(notice_msg);
                liveApplyServiceImpl.updateshyLiveApply(liveApply);
        	}
         }
    	
    }
    /**
     * 自动退领
     */
    @Scheduled(cron="0 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 = liveApplyServiceImpl.findshyLiveApply(liveApply);
    	if(shyLiveapplyList.size()>0){
    		for (ShyLiveapply liveapply : shyLiveapplyList) {
        		logger.info("自动退领的liveApply_id:"+liveapply.getId());
        		liveapply.setReceive(CommonConst.RECEIVE_BEFORE);//未领取
        		liveapply.setAuditor(null);
        		liveApplyServiceImpl.updateshyLiveApply(liveApply);
    		}
    	}
    }
   
}