SystemNoticeThread.java 3.99 KB
/**  
* @Title: SystemNoticeThread.java
* @Package com.bjivt.web.thread
* @Description: TODO(用一句话描述该文件做什么)
* @author Yh.T
* @date 2016年9月27日 下午3:41:35
* @version  
*/
package com.bjivt.web.thread;

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

import org.codehaus.jackson.map.ObjectMapper;

import com.alibaba.fastjson.JSONObject;
import com.bjivt.entity.showtime.SystemNotice;
import com.bjivt.service.showtime.SystemNoticeService;
import com.bjivt.service.showtime.UserService;
import com.bjivt.util.JPushUtil;
import com.bjivt.util.Pager;
import com.bjivt.util.PropertiesUtil;

import cn.jpush.api.push.PushResult;

/**
 * 项目名称:admin
 *
 * 类描述: 类名称:com.bjivt.web.thread.SystemNoticeThread Yh.T 创建时间:2016年9月27日
 * 下午3:41:35 修改备注:
 * 
 * @version
 */

public class SystemNoticeThread extends Thread {
	private boolean isRun = true;
	private String appkey = PropertiesUtil.config.get("jpush.appkey");
	private String jpushSecret = PropertiesUtil.config.get("jpush.masterSecret");
	private JPushUtil client = null;
	protected UserService userService;
	private int type;
	private SystemNotice notice;

	public SystemNoticeThread() {
		client = new JPushUtil(appkey, jpushSecret, true);
	}

	@Override
	public void run() {
			sendMsg();
	}

	public void sendMsg() {
		Map<String, String> extras = new HashMap<String, String>();
		ObjectMapper mapper = new ObjectMapper();
		try {
			List<String> idList = null;
			Pager page = new Pager();
			page.setSize(500);
			Map<String, Object> content = new HashMap<String, Object>();
			content.put("content", notice.getContent());
			content.put("title", notice.getTitle());
			String strCont = mapper.writeValueAsString(content);
			if (type == 1) {
				page.setCount(userService.loadAnchorUserCount());
				page.setPages((int) Math.ceil((float) page.getCount() / page.getSize()));
				idList = userService.loadAnchorUserList(page);
				for (int i = 1; i <= page.getPages(); i++) {
					// PushResult info =
					// client.sendToAliasAndroidAndiOS(notice.getTitle(),
					// strCont, extras, idList.toArray(new
					// String[idList.size()]));
					client.sendMsgToAliasAndroidAndiOS(notice.getTitle(), strCont,
							idList.toArray(new String[idList.size()]));
					page.setPage(i + 1);
					page.setFrom((page.getPage() - 1) * page.getSize());
					idList = userService.loadAnchorUserList(page);
				}
			} else if (type == 2) {
				page.setCount(userService.loadAllUserCount());
				page.setPages((int) Math.ceil((float) page.getCount() / page.getSize()));
				idList = userService.loadAllUserList(page);
				for (int i = 1; i <= page.getPages(); i++) {
					client.sendMsgToAliasAndroidAndiOS(notice.getTitle(), strCont,
							idList.toArray(new String[idList.size()]));
					// System.out.println(strCont+"============");
					// PushResult info = client.sendMsgToAll(strCont);
					page.setPage(i + 1);
					page.setFrom((page.getPage() - 1) * page.getSize());
					idList = userService.loadAllUserList(page);
				}
			} else if (type == 3) {
				page.setCount(userService.loadCommonUserCount());
				idList = userService.loadCommonUserList(page);
				for (int i = 1; i <= page.getPages(); i++) {
					client.sendMsgToAliasAndroidAndiOS(notice.getTitle(), strCont,
							idList.toArray(new String[idList.size()]));
					page.setPage(i + 1);
					page.setFrom((page.getPage() - 1) * page.getSize());
					idList = userService.loadCommonUserList(page);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public boolean isRun() {
		return isRun;
	}

	public void setRun(boolean isRun) {
		this.isRun = isRun;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public SystemNotice getNotice() {
		return notice;
	}

	public void setNotice(SystemNotice notice) {
		this.notice = notice;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

}