优化查询

This commit is contained in:
yiyan 2018-09-15 14:13:14 +08:00
parent 015b12df40
commit 2dd7bc01be
5 changed files with 113 additions and 10 deletions

View File

@ -6,14 +6,21 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ifish.enums.NeteaseEnum;
import com.ifish.jpush.utils.StringUtils;
import com.ifish.netease.CheckSumBuilder;
import net.sf.json.JSONArray; import net.sf.json.JSONArray;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import com.ifish.enums.PhoneTypeEnum; import com.ifish.enums.PhoneTypeEnum;
@ -30,6 +37,7 @@ public class job{
private Connection connection = null; private Connection connection = null;
private Statement stmt = null; private Statement stmt = null;
private PreparedStatement prest = null; private PreparedStatement prest = null;
private PreparedStatement prestDevice = null;
/** /**
* 查询需要推送的用户并且推送完修改为下一次提醒日期 * 查询需要推送的用户并且推送完修改为下一次提醒日期
@ -40,7 +48,8 @@ public class job{
Class.forName("com.mysql.jdbc.Driver"); Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myfishdb?characterEncoding=UTF-8", "ifish", "ifish7pwd"); connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myfishdb?characterEncoding=UTF-8", "ifish", "ifish7pwd");
stmt = connection.createStatement(); stmt = connection.createStatement();
prest = connection.prepareStatement("update tbl_tmp_push_remind set is_push=? where user_id=? and device_id=?"); prest = connection.prepareStatement("update tbl_tmp_push_remind set remind_date=? where user_id=? and device_id=?");
prestDevice = connection.prepareStatement("update tbl_device set remind_date=? where device_id=?");
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
@ -62,10 +71,13 @@ public class job{
rowCount=result.getInt("countRow"); rowCount=result.getInt("countRow");
pageNo = (rowCount+pageSize-1)/pageSize; pageNo = (rowCount+pageSize-1)/pageSize;
} }
System.out.println(pageNo); System.out.println(pageNo);
String nowDate = IfishUtil.format1(new Date());
for (int i = 0; i < pageNo; i++) { 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); //查询出推送日期推送周期 未推送 及当天
//推送的用户 result = stmt.executeQuery("select remind_date,remind_cycle,device_id,user_id,show_name,login_type from tbl_tmp_push_remind where is_push='0' and remind_date = '"+ nowDate +"' limit 0,"+pageSize);
//推送的用户ss
List<String> androidUser = new ArrayList<String>(); List<String> androidUser = new ArrayList<String>();
List<String> iosUser = new ArrayList<String>(); List<String> iosUser = new ArrayList<String>();
List<String> ids = new ArrayList<String>(); List<String> ids = new ArrayList<String>();
@ -74,10 +86,22 @@ public class job{
Integer deviceId= result.getInt("device_id"); Integer deviceId= result.getInt("device_id");
Integer userId = result.getInt("user_id"); Integer userId = result.getInt("user_id");
String loginType = result.getString("login_type"); String loginType = result.getString("login_type");
prest.setString(1, "1");
String remindCycle = result.getString("remind_cycle");
//设置下次提醒日期
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR,calendar.get(Calendar.DAY_OF_YEAR) + Integer.parseInt(remindCycle));
String nextRemindDate = IfishUtil.format1(calendar.getTime());
prest.setString(1, nextRemindDate);
prest.setInt(2, userId); prest.setInt(2, userId);
prest.setInt(3, deviceId); prest.setInt(3, deviceId);
prest.addBatch(); prest.addBatch();
//修改设备表的下次提醒日期
prestDevice.setString(1,nextRemindDate);
prestDevice.setString(2,String.valueOf(deviceId));
prestDevice.addBatch();
//云信 //云信
ids.add(userId.toString()); ids.add(userId.toString());
//极光 //极光
@ -88,6 +112,8 @@ public class job{
iosUser.add(userId.toString()); iosUser.add(userId.toString());
} }
} }
//发送云信消息 //发送云信消息
if(ids.size()>0){ if(ids.size()>0){
neteaseIM.sendBatchMsg("ifish", JSONArray.fromObject(ids).toString(), "【换水提醒】您的水族箱需要换水啦~您可以在水箱设置中更改提醒设置"); neteaseIM.sendBatchMsg("ifish", JSONArray.fromObject(ids).toString(), "【换水提醒】您的水族箱需要换水啦~您可以在水箱设置中更改提醒设置");
@ -103,6 +129,7 @@ public class job{
} }
//批量提交 //批量提交
prest.executeBatch(); prest.executeBatch();
prestDevice.executeBatch();
//云信一分钟访问不超过120次 //云信一分钟访问不超过120次
Thread.sleep(600); Thread.sleep(600);
} }
@ -167,4 +194,40 @@ public class job{
e.printStackTrace(); e.printStackTrace();
} }
} }
public static void main(String[] args) throws Exception{
String msg = "test";
List<String> ids = new ArrayList<String>();
ids.add("30919");
DefaultHttpClient httpClient = new DefaultHttpClient();
String url = "https://api.netease.im/nimserver/msg/sendBatchMsg.action";
HttpPost httpPost = new HttpPost(url);
String nonce = IfishUtil.getCharAndNumr(10);
String curTime = String.valueOf((new Date()).getTime() / 1000L);
//计算CheckSum
String checkSum = CheckSumBuilder.getCheckSum("e62f6c247b46", nonce ,curTime);
//设置请求的header
httpPost.addHeader("AppKey", "87b0e3315dfc2df08060bcb54246da68");
httpPost.addHeader("Nonce", nonce);
httpPost.addHeader("CurTime", curTime);
httpPost.addHeader("CheckSum", checkSum);
httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
//设置请求的参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("fromAccid", "ifish"));
nvps.add(new BasicNameValuePair("toAccids", JSONArray.fromObject(ids).toString()));
nvps.add(new BasicNameValuePair("type", "0"));
nvps.add(new BasicNameValuePair("body", "{\"msg\":\""+msg+"\"}"));
httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
//执行请求
HttpResponse response = httpClient.execute(httpPost);
//执行结果
String responseStr = EntityUtils.toString(response.getEntity(), "utf-8");
JSONObject json = new JSONObject(responseStr);
System.out.println(responseStr);
}
} }

View File

@ -172,6 +172,7 @@ public class NeteaseIM {
String code = json.getString("code"); String code = json.getString("code");
Map<String, String> map = new HashMap<String, String>(); Map<String, String> map = new HashMap<String, String>();
map.put("code", code); map.put("code", code);
log.info("resp" + responseStr);
//200 //200
if(code.equals(NeteaseEnum.status200.getKey())){ if(code.equals(NeteaseEnum.status200.getKey())){
@ -229,6 +230,8 @@ public class NeteaseIM {
else if(code.equals(NeteaseEnum.status414.getKey())){ else if(code.equals(NeteaseEnum.status414.getKey())){
String desc = json.getString("desc"); String desc = json.getString("desc");
} }
log.info("ids : " + toAccids);
log.info("response : " + responseStr);
return map; return map;
} catch (Exception e) { } catch (Exception e) {
log.error("refreshToken error message:{}",e.toString()); log.error("refreshToken error message:{}",e.toString());

View File

@ -0,0 +1,37 @@
log4j.rootLogger=INFO,CONSOLE,FILE,ROLLING_FILE
###################
# Console Appender
###################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=INFO
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[Ifish7Quart][%-5p]%d - (%F:%L) - %m%n
########################
# [INFO]Daily Rolling File
########################
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.Threshold=INFO
log4j.appender.FILE.File=/logs/quartzPro/Ifish7Quart_ALL.log
log4j.appender.FILE.Append=true
log4j.appender.FILE.ImmediateFlush = true
log4j.appender.FILE.DatePattern='.'yyyy-MM-dd
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=[Ifish7Quart][%-5p]%d - %c.%t(%F:%L) - %m%n
########################
# [ERROR]Rolling File
########################
log4j.appender.ROLLING_FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.ROLLING_FILE.Threshold=ERROR
log4j.appender.ROLLING_FILE.File=/logs/quartzPro/Ifish7Quart_ERROR.log
log4j.appender.ROLLING_FILE.Append=true
log4j.appender.ROLLING_FILE.ImmediateFlush = true
log4j.appender.ROLLING_FILE.DatePattern='.'yyyy-MM-dd
log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.ROLLING_FILE.layout.ConversionPattern=[Ifish7Quqrt][%-5p]%d - %c.%t(%F:%L) - %m%n
log4j.logger.org.springframework=INFO
log4j.logger.com.hibernate=INFO
log4j.logger.java.sql=INFO