quartzPro/.svn/pristine/56/56b1e44d04f049747f2792ee307...

172 lines
5.0 KiB
Plaintext

package com.ifish.job;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import com.ifish.enums.PhoneTypeEnum;
import com.ifish.jpush.JPushNotification;
import com.ifish.netease.NeteaseIM;
import com.ifish.util.IfishUtil;
public class job{
@Autowired
private NeteaseIM neteaseIM;
@Autowired
private JPushNotification jPushNotification;
@Autowired
private JPushNotification jPushNotification2;
private Connection connection = null;
private Statement stmt = null;
private PreparedStatement prest = null;
/**
* 查询需要推送的用户并且推送完修改为下一次提醒日期
*/
public void pushRemind(){
System.out.println(new Date()+"开始任务");
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myfishdb?characterEncoding=UTF-8", "ifish", "ifish7pwd");
stmt = connection.createStatement();
prest = connection.prepareStatement("update tbl_tmp_push_remind set is_push=? where user_id=? and device_id=?");
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//结果集
ResultSet result = null;
try {
//推送提醒数
int rowCount = 0;
//按分页来获取数据
int pageNo = 0;
//云信限制每次最多500条
int pageSize = 500;
//查询总共需要推送的用户数
String countSql = "select count(1) as countRow from tbl_tmp_push_remind where is_push='0'";
result = stmt.executeQuery(countSql);
if(result.next()){
rowCount=result.getInt("countRow");
pageNo = (rowCount+pageSize-1)/pageSize;
}
System.out.println(pageNo);
for (int i = 0; i < pageNo; i++) {
result = stmt.executeQuery("select device_id,user_id,show_name,login_type from tbl_tmp_push_remind where is_push='0' limit 0,"+pageSize);
//推送的用户
List<String> androidUser = new ArrayList<String>();
List<String> iosUser = new ArrayList<String>();
List<String> ids = new ArrayList<String>();
//更新提醒过的用户
while(result.next()){
Integer deviceId= result.getInt("device_id");
Integer userId = result.getInt("user_id");
String loginType = result.getString("login_type");
prest.setString(1, "1");
prest.setInt(2, userId);
prest.setInt(3, deviceId);
prest.addBatch();
//云信
ids.add(userId.toString());
//极光
if(loginType.toLowerCase().equals(PhoneTypeEnum.android.getKey())){
androidUser.add(userId.toString());
}
else if(loginType.toLowerCase().equals(PhoneTypeEnum.ios.getKey())){
iosUser.add(userId.toString());
}
}
//发送云信消息
if(ids.size()>0){
neteaseIM.sendBatchMsg("ifish", JSONArray.fromObject(ids).toString(), "【换水提醒】您的水族箱需要换水啦~您可以在水箱设置中更改提醒设置");
}
//极光推送
Integer iosSize = iosUser.size();
Integer androidSize = androidUser.size();
if(iosSize>0){
push(PhoneTypeEnum.ios,iosUser.toArray(new String[iosSize]));
}
if(androidSize>0){
push(PhoneTypeEnum.android,androidUser.toArray(new String[androidSize]));
}
//批量提交
prest.executeBatch();
//云信一分钟访问不超过120次
Thread.sleep(600);
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
if(result!=null){
try {
result.close();
result = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt!=null){
try {
stmt.close();
stmt = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if(prest!=null){
try {
prest.close();
prest = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
connection = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
/**
* 推送提醒
* @param type
* @param ids
*/
public void push(PhoneTypeEnum type,String[] ids){
try {
Map<String,String> map = new HashMap<String, String>();
map.put("timestamp", IfishUtil.format2(new Date()));
//推送android
if(type.equals(PhoneTypeEnum.android)){
System.out.println(new Date()+"android推送"+ids.length+"个");
jPushNotification.sendAndroidNotification(ids, "换水提醒", "您的水族箱需要换水啦~您可以在水箱设置中更改提醒设置", map);
}
//推送IOS
else if(type.equals(PhoneTypeEnum.ios)){
System.out.println(new Date()+"ios推送"+ids.length+"个");
jPushNotification2.sendIosNotification(ids, "换水提醒", "您的水族箱需要换水啦~您可以在水箱设置中更改提醒设置", map);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}