MarketingPlanSocketTimer.java 3.9 KB
package com.espeed.time;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import com.espeed.dao.YxyMarketingPlanDao;
import com.espeed.dao.YxyMarketingWeekDao;
import com.espeed.pojo.YxyMarketingPlan;
import com.espeed.pojo.YxyMarketingWeek;
import com.espeed.socket.WebSocketMessageInboundPool;
import com.espeed.tool.DateFormat;

/***
 * 
 * @author 营销计划提醒任务
 *
 */
public class MarketingPlanSocketTimer{

	//提醒计划任务
	public void planwebsocket(){
		try {
			System.out.println("提醒计划在运行中....");
        	//获取在线用户,只对在线用户进行推送
        	Set<String> onlineuser=WebSocketMessageInboundPool.getOnlineUser();
        	SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        	String nowday=df.format(new Date());
        	String nowhour=nowday.split(" ")[1];
        	//在线用户
        	//for(String onlineid:onlineuser){
        	for(String account:onlineuser){
        		if(account!=null&&account.indexOf("@")!=-1){
        			//查询当前用户的提醒计划
                	//String hql="from YxyMarketingPlan where plan_uid="+onlineid+" and plan_status=0 and plan_type=0 and plan_executetime<='"+nowhour+"'";
        			String hql="from YxyMarketingPlan where loginid='"+account.split("@")[0]+"' and domain='"+account.split("@")[1]+"' and plan_status=0 and plan_type=0 and plan_executetime<='"+nowhour+"'";
                	List<YxyMarketingPlan> planlist=marketingplandao.findByHql(hql);
                	//有提醒计划
                	if(planlist.size()>0){
                		for(int i=0;i<planlist.size();i++){
                			//计划ID
                			int planid=planlist.get(i).getPlan_id();
                			//提醒内容
            				String sendbody=planlist.get(i).getPlan_title();
                			//计划提醒周期
                    		List<YxyMarketingWeek> weeklist=planlist.get(i).getWeeklist();
                    		//根据当前时间判断今日周几
        					int nowweek=DateFormat.dayOfWeekNum(nowday);
        					//是否都执行完了
        					int comeover=0;
        					//是否有当前执行周期
        					for(int j=0;j<weeklist.size();j++){
        						//未执行的
        						if(weeklist.get(j).getIsexecution()==0){
        							if(nowweek==weeklist.get(j).getWeek_value()){
            							int weekid=weeklist.get(j).getWeek_id();//周期ID
            							//推送信息到用户
            	            			//WebSocketMessageInboundPool.sendMessageToUser(onlineid,sendbody);
            							WebSocketMessageInboundPool.sendMessageToUser(account,sendbody);
            	            			//修改此提醒为已提醒
            	            			String updatehql="update YxyMarketingWeek set isexecution=1 where week_id="+weekid;
            	            			marketingweekdao.updateByHql(updatehql);
            						}
        						}else{
        							comeover++;
        						}
        						
        					}
        					//如果都执行完了则更新执行计划
        					if(comeover!=0&&comeover==weeklist.size()){
        						String updatehql="update YxyMarketingPlan set plan_status=1 where plan_id="+planid;
        						marketingplandao.updateByHql(updatehql);
        					}
                		}
                	}
        		}
        	}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private YxyMarketingPlanDao marketingplandao;//营销计划dao
	private YxyMarketingWeekDao marketingweekdao;//营销计划周期dao
	public YxyMarketingPlanDao getMarketingplandao() {
		return marketingplandao;
	}
	public void setMarketingplandao(YxyMarketingPlanDao marketingplandao) {
		this.marketingplandao = marketingplandao;
	}
	public YxyMarketingWeekDao getMarketingweekdao() {
		return marketingweekdao;
	}
	public void setMarketingweekdao(YxyMarketingWeekDao marketingweekdao) {
		this.marketingweekdao = marketingweekdao;
	}
	
}