diff --git a/src/main/java/com/ifish/job/HeaterJob.java b/src/main/java/com/ifish/job/HeaterJob.java new file mode 100644 index 0000000..ea8f6b1 --- /dev/null +++ b/src/main/java/com/ifish/job/HeaterJob.java @@ -0,0 +1,191 @@ +package com.ifish.job; + +import com.ifish.enums.PhoneTypeEnum; +import com.ifish.jpush.JPushNotification; +import com.ifish.netease.NeteaseIM; +import com.ifish.util.IfishUtil; +import net.sf.json.JSONArray; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.stereotype.Component; + +import java.sql.*; +import java.util.*; +import java.util.Date; +import java.util.logging.Logger; + +/** + * @author: yan.y + * @Description: + * @Date: Created in 19:47 2018/9/18 + * @Modified by: + */ +@Component +@EnableScheduling +@Lazy(false) +public class HeaterJob { + @Autowired + private NeteaseIM neteaseIM; + @Autowired + private JPushNotification jPushNotification2; + + private Connection connection = null; + private Statement stmt = null; + private PreparedStatement prest = null; + private PreparedStatement prestDevice = null; + + private static final Logger log = Logger.getLogger("job"); + + public static void main(String[] args) { + Calendar calendar = Calendar.getInstance(); + int i = calendar.get(Calendar.HOUR_OF_DAY); + System.out.println(i); + + } + + public void heraterChangeWater() { + log.info("智能加热棒换水提醒推送begin....."); + log.info(IfishUtil.format2(new Date())+"开始任务"); + String nowDate = IfishUtil.format1(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_device_heater_detail set herter_remind_date=? where heater_mac_address=?"); + } catch (SQLException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); + //结果集 + ResultSet result = null; + try { + //推送提醒数 + int rowCount = 0; + //按分页来获取数据 + int pageNo = 0; + //云信限制每次最多500条 + int pageSize = 500; + //查询总共需要推送的用户数 + String countSql = "select count(1) as countRow from tbl_device_heater_detail where herter_remind_date = '" + nowDate + "'"; + result = stmt.executeQuery(countSql); + if(result.next()){ + rowCount=result.getInt("countRow"); + pageNo = (rowCount+pageSize-1)/pageSize; + } + + log.info("pageNo : " + pageNo); + for (int i = 0; i < pageNo; i++) { + //查询出推送日期、推送周期 未推送 及当天 + result = stmt.executeQuery("select tdhd.heater_mac_address,tdhd.heater_cycle,tdu.user_id from tbl_device_heater_detail tdhd,tbl_device td,tbl_device_user tdu where tdhd.heater_mac_address = td.mac_address and td.device_id = tdu.device_id and tdhd.herter_remind_date = '"+ nowDate +"' and tdhd.heater_reminder_time = '" + hour + "' limit 0,"+pageSize); + //推送的用户 + List androidUser = new ArrayList(); + List iosUser = new ArrayList(); + List ids = new ArrayList(); + //更新提醒过的用户 + while(result.next()){ + Integer userId = result.getInt("user_id"); + String macAddress = result.getString("heater_mac_address"); + + String remindCycle = result.getString("heater_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.setString(2, macAddress); + prest.addBatch(); + ids.add(userId.toString()); + //极光 + 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(); + } + } + } + log.info("智能加热棒换水提醒推送end....."); + + } + + + /** + * 推送提醒 + * @param type + * @param ids + */ + public void push(PhoneTypeEnum type,String[] ids){ + try { + Map map = new HashMap(); + 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 + if(type.equals(PhoneTypeEnum.ios)){ + System.out.println(new Date()+"ios推送"+ids.length+"个"); + jPushNotification2.sendIosNotification(ids, "换水提醒", "您的水族箱需要换水啦~您可以在水箱设置中更改提醒设置", map); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/src/main/java/com/ifish/job/job.java b/src/main/java/com/ifish/job/job.java index 6aa8fcc..e730fb9 100644 --- a/src/main/java/com/ifish/job/job.java +++ b/src/main/java/com/ifish/job/job.java @@ -8,8 +8,6 @@ import java.sql.SQLException; import java.sql.Statement; import java.util.*; -import com.ifish.enums.NeteaseEnum; -import com.ifish.jpush.utils.StringUtils; import com.ifish.netease.CheckSumBuilder; import net.sf.json.JSONArray; @@ -20,7 +18,6 @@ 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 com.ifish.enums.PhoneTypeEnum; @@ -113,7 +110,6 @@ public class job{ } } - //发送云信消息 if(ids.size()>0){ neteaseIM.sendBatchMsg("ifish", JSONArray.fromObject(ids).toString(), "【换水提醒】您的水族箱需要换水啦~您可以在水箱设置中更改提醒设置"); @@ -226,7 +222,6 @@ public class job{ HttpResponse response = httpClient.execute(httpPost); //执行结果 String responseStr = EntityUtils.toString(response.getEntity(), "utf-8"); - JSONObject json = new JSONObject(responseStr); System.out.println(responseStr); } diff --git a/src/main/resources/quartz.xml b/src/main/resources/quartz.xml index 9f3bda6..e52fb6e 100644 --- a/src/main/resources/quartz.xml +++ b/src/main/resources/quartz.xml @@ -13,6 +13,7 @@ + @@ -34,6 +35,17 @@ 0 0 10 * * ? + + + + + + + 0 0 0/1 * * ? + + + @@ -45,12 +57,22 @@ pushRemind + + + + + + + heraterChangeWater + - + +