From ba1966a2737287e456e8e6bd57bf15b7b95d8294 Mon Sep 17 00:00:00 2001 From: landong2015 Date: Fri, 27 May 2016 17:10:57 +0800 Subject: [PATCH] New changelist --- .idea/artifacts/web_api_war_exploded.xml | 3 +- .../Maven__cn_jpush_api_jpush_client_3_2_9.xml | 13 ++ .../Maven__com_google_code_gson_gson_2_3.xml | 13 ++ mainData/pom.xml | 1 + .../yimiao/main/data/service/PushTaskService.java | 9 ++ .../data/service/impl/PushTaskServiceImpl.java | 170 +++++++++++++++++++++ pom.xml | 5 + .../web/controller/v1/NotificationsController.java | 75 +++++++-- .../yimiao/web/controller/v1/UserController.java | 56 +++---- webApi/src/main/resources/config.properties | 7 + .../main/resources/spring/applicationContext.xml | 15 ++ webApi/web-api.iml | 3 +- 12 files changed, 330 insertions(+), 40 deletions(-) create mode 100644 .idea/libraries/Maven__cn_jpush_api_jpush_client_3_2_9.xml create mode 100644 .idea/libraries/Maven__com_google_code_gson_gson_2_3.xml create mode 100644 mainData/src/main/java/com/lymsh/yimiao/main/data/service/PushTaskService.java create mode 100644 mainData/src/main/java/com/lymsh/yimiao/main/data/service/impl/PushTaskServiceImpl.java diff --git a/.idea/artifacts/web_api_war_exploded.xml b/.idea/artifacts/web_api_war_exploded.xml index 01ed77c..b1b9f77 100644 --- a/.idea/artifacts/web_api_war_exploded.xml +++ b/.idea/artifacts/web_api_war_exploded.xml @@ -33,6 +33,8 @@ + + @@ -107,7 +109,6 @@ - diff --git a/.idea/libraries/Maven__cn_jpush_api_jpush_client_3_2_9.xml b/.idea/libraries/Maven__cn_jpush_api_jpush_client_3_2_9.xml new file mode 100644 index 0000000..953cb34 --- /dev/null +++ b/.idea/libraries/Maven__cn_jpush_api_jpush_client_3_2_9.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__com_google_code_gson_gson_2_3.xml b/.idea/libraries/Maven__com_google_code_gson_gson_2_3.xml new file mode 100644 index 0000000..4dcaf15 --- /dev/null +++ b/.idea/libraries/Maven__com_google_code_gson_gson_2_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/mainData/pom.xml b/mainData/pom.xml index 2a4c6e1..55d8434 100644 --- a/mainData/pom.xml +++ b/mainData/pom.xml @@ -17,6 +17,7 @@ base-common ${project.version} + diff --git a/mainData/src/main/java/com/lymsh/yimiao/main/data/service/PushTaskService.java b/mainData/src/main/java/com/lymsh/yimiao/main/data/service/PushTaskService.java new file mode 100644 index 0000000..232f7be --- /dev/null +++ b/mainData/src/main/java/com/lymsh/yimiao/main/data/service/PushTaskService.java @@ -0,0 +1,9 @@ +package com.lymsh.yimiao.main.data.service; + +/** + * Created by Administrator on 2016/5/24 0024. + */ +public interface PushTaskService { + + public void pushTask(); +} diff --git a/mainData/src/main/java/com/lymsh/yimiao/main/data/service/impl/PushTaskServiceImpl.java b/mainData/src/main/java/com/lymsh/yimiao/main/data/service/impl/PushTaskServiceImpl.java new file mode 100644 index 0000000..2e6305b --- /dev/null +++ b/mainData/src/main/java/com/lymsh/yimiao/main/data/service/impl/PushTaskServiceImpl.java @@ -0,0 +1,170 @@ +package com.lymsh.yimiao.main.data.service.impl; + +import cn.jpush.api.JPushClient; +import cn.jpush.api.push.PushResult; +import cn.jpush.api.push.model.Platform; +import cn.jpush.api.push.model.PushPayload; +import cn.jpush.api.push.model.audience.Audience; +import cn.jpush.api.push.model.notification.Notification; +import com.lymsh.mommybaby.basecommon.util.DateUtil; +import com.lymsh.yimiao.main.data.enumdata.YnEnum; +import com.lymsh.yimiao.main.data.model.*; +import com.lymsh.yimiao.main.data.service.*; +import com.lymsh.yimiao.main.data.util.JPushUtil; +import org.apache.commons.collections.CollectionUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.Date; +import java.util.List; + +/** + * + * 定时推送接种消息 + * + * Created by Administrator on 2016/5/24 0024. + */ +@Service +public class PushTaskServiceImpl implements PushTaskService{ + + @Autowired + private JPushClient jPushClient; + @Autowired + private MedInoculateOrderService medInoculateOrderService; + @Autowired + private YmUserKidMapsService ymUserKidMapsService; + @Autowired + private YmUsersService ymUsersService; + @Autowired + private YmPushMessagesService ymPushMessagesService; + @Autowired + private MedKidsService medKidsService; + @Autowired + private MedVaccineNameService medVaccineNameService; + + private final static String isDelete = "1"; + + public static final int THREE_DAY_SECONDS = 86400 * 1000 * 3; + + public static final int ONE_DAY_SECONDS = 86400 * 1000; + + private static final String ALERT = "您有一条新的推送"; + + private static final String ALIAS = "yimiaoer"; + + @Override + public void pushTask() { + //距离三天 + MedInoculateOrderQuery orderQuery = new MedInoculateOrderQuery(); + orderQuery.setIoIsDelete(isDelete); + orderQuery.setIoInoculateDate(DateUtil.getyyyy_MM_dd(new Date(DateUtil.getStartTime() + THREE_DAY_SECONDS)).toString()); + List orderList = medInoculateOrderService.queryMedInoculateOrder(orderQuery); + if (CollectionUtils.isNotEmpty(orderList)){ + //距离当前还有三天就要接种的小孩 + for (MedInoculateOrder data : orderList){ + + //接种的疫苗名称 + MedVaccineName medVaccineName = medVaccineNameService.getMedVaccineName(data.getIoVaccineId()); + + YmUserKidMapsQuery mapsQuery = new YmUserKidMapsQuery(); + mapsQuery.setYn(YnEnum.yes.getId()); + mapsQuery.setKidId(data.getIoKidId()); + List mapsList = ymUserKidMapsService.queryYmUserKidMaps(mapsQuery); + //得到和小孩关联的账号 + if (CollectionUtils.isNotEmpty(mapsList)){ + for (YmUserKidMaps temp : mapsList){ + + //得到小孩的名字 + MedKids medKids = medKidsService.getMedKids(temp.getKidId()); + + YmUsersQuery usersQuery = new YmUsersQuery(); + usersQuery.setYn(YnEnum.yes.getId()); + usersQuery.setId(temp.getUserId()); + //拿到最后所有需要发推送的用户 + List usersList = ymUsersService.queryYmUsers(usersQuery); + if (CollectionUtils.isNotEmpty(usersList)) { + YmUsers users = usersList.get(0); + PushPayload pushPayload = JPushUtil.buildPushObject_all_alias_alert(ALIAS + users.getPhone(), ALERT); + if (pushPayload != null) { + YmPushMessages messages = new YmPushMessages(); + messages.setCreated(new Date()); + messages.setContent((medKids == null ? "" : medKids.getName() == null ? "" : medKids.getName()) + "小朋友按计划3天后将会接种" + + (medVaccineName == null ? "" : medVaccineName.getVnName()) + ",请提前做好准备,准时到接种点接种疫苗。"); + messages.setReceptionId(users.getId()); + messages.setYn(YnEnum.yes.getId()); + ymPushMessagesService.addYmPushMessages(messages); + + //成功后推送提示消息 + try { + jPushClient.sendPush(pushPayload); + } catch (Exception e) { + + } + } + } + } + } + } + } + //距离一天 + MedInoculateOrderQuery orderQuery1 = new MedInoculateOrderQuery(); + orderQuery1.setIoIsDelete(isDelete); + orderQuery1.setIoInoculateDate(DateUtil.getyyyy_MM_dd(new Date(DateUtil.getStartTime() + ONE_DAY_SECONDS)).toString()); + List orderList1 = medInoculateOrderService.queryMedInoculateOrder(orderQuery1); + if (CollectionUtils.isNotEmpty(orderList1)){ + for (MedInoculateOrder data : orderList1){ + + //接种的疫苗名称 + MedVaccineName medVaccineName = medVaccineNameService.getMedVaccineName(data.getIoVaccineId()); + + YmUserKidMapsQuery mapsQuery = new YmUserKidMapsQuery(); + mapsQuery.setYn(YnEnum.yes.getId()); + mapsQuery.setKidId(data.getIoKidId()); + List mapsList = ymUserKidMapsService.queryYmUserKidMaps(mapsQuery); + //得到和小孩关联的账号 + if (CollectionUtils.isNotEmpty(mapsList)){ + for (YmUserKidMaps temp : mapsList){ + + //得到小孩的名字 + MedKids medKids = medKidsService.getMedKids(temp.getKidId()); + + YmUsersQuery usersQuery = new YmUsersQuery(); + usersQuery.setYn(YnEnum.yes.getId()); + usersQuery.setId(temp.getUserId()); + //拿到最后所有需要发推送的用户 + List usersList = ymUsersService.queryYmUsers(usersQuery); + if (CollectionUtils.isNotEmpty(usersList)) { + YmUsers users = usersList.get(0); + PushPayload pushPayload = JPushUtil.buildPushObject_all_alias_alert(ALIAS + users.getPhone(), ALERT); + if (pushPayload != null) { + YmPushMessages messages = new YmPushMessages(); + messages.setCreated(new Date()); + messages.setContent((medKids == null ? "" : medKids.getName() == null ? "" : medKids.getName()) + "小朋友按计划1天后将会接种" + + (medVaccineName == null ? "" : medVaccineName.getVnName()) + ",请提前做好准备,准时到接种点接种疫苗。"); + messages.setReceptionId(users.getId()); + messages.setYn(YnEnum.yes.getId()); + ymPushMessagesService.addYmPushMessages(messages); + + //成功后推送提示消息 + try { + jPushClient.sendPush(pushPayload); + } catch (Exception e) { + + } + } + } + } + } + } + + + + } + + + } + +} diff --git a/pom.xml b/pom.xml index 1ed41c1..dfbf4c2 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,11 @@ + cn.jpush.api + jpush-client + 3.2.9 + + org.springframework spring-core ${org.springframework.version} diff --git a/webApi/src/main/java/com/lyms/yimiao/web/controller/v1/NotificationsController.java b/webApi/src/main/java/com/lyms/yimiao/web/controller/v1/NotificationsController.java index bb93189..18d5b71 100644 --- a/webApi/src/main/java/com/lyms/yimiao/web/controller/v1/NotificationsController.java +++ b/webApi/src/main/java/com/lyms/yimiao/web/controller/v1/NotificationsController.java @@ -8,7 +8,11 @@ import com.lymsh.mommybaby.basecommon.util.ResultUtils; import com.lymsh.yimiao.main.data.enumdata.YnEnum; import com.lymsh.yimiao.main.data.model.YmNotifications; import com.lymsh.yimiao.main.data.model.YmNotificationsQuery; +import com.lymsh.yimiao.main.data.model.YmPushMessages; +import com.lymsh.yimiao.main.data.model.YmPushMessagesQuery; import com.lymsh.yimiao.main.data.service.YmNotificationsService; +import com.lymsh.yimiao.main.data.service.YmPushMessagesService; +import com.lymsh.yimiao.main.data.util.LoginContext; import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @@ -17,11 +21,9 @@ import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import javax.management.Notification; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * @@ -35,17 +37,22 @@ public class NotificationsController extends BaseController{ @Autowired private YmNotificationsService notificationsService; + @Autowired + private YmPushMessagesService ymPushMessagesService; //获取消息(1.系统消息2.接种消息) @RequestMapping(value = "/notifications", method = RequestMethod.GET) @TokenRequired - public void getNotifications(HttpServletResponse response){ - + public void getNotifications(HttpServletResponse response, + HttpServletRequest request +// , @RequestParam(value = "time",required = false) long time + ){ + //系统消息 YmNotificationsQuery notificationsQuery = new YmNotificationsQuery(); notificationsQuery.setYn(YnEnum.yes.getId()); - notificationsQuery.setSort("created desc"); +// notificationsQuery.setCreated(new Date(time*1000)); List notificationsList = notificationsService.queryYmNotifications(notificationsQuery); List list = new ArrayList<>(); @@ -55,18 +62,62 @@ public class NotificationsController extends BaseController{ Map map = new HashMap<>(); map.put("id",data.getId()); map.put("created", DateUtil.getSecond(data.getCreated())); - map.put("type",data.getType()); + map.put("type",1); map.put("content",data.getContent()); list.add(map); } } + + //接种通知 + List inculateList = getInculateList(request); +// List inculateList = getInculateList(request,time); + if (CollectionUtils.isNotEmpty(inculateList)){ + list.addAll(inculateList); + } + + //对所有结果集重新排序 + Collections.sort(list, new Comparator() { + private String key ="created"; + @Override + public int compare(Map o1, Map o2) { + if(o1.containsKey(key)&&o2.containsKey(key)){ + if((Integer)o1.get(key)==(Integer)o2.get(key)){ + return 0; + } + if((Integer)o1.get(key)>(Integer)o2.get(key)){ + return -1; + }else{ + return 1; + } + } + return 0; + } + }); ResultUtils.buildSuccessResultAndWrite(response,list); } - //定时发送给当前登录用户接种通知 - /* - 查询当前登陆用户在当前时间距离下次接种时间3天和1天的时候,发送一条推送消息 - */ + private List getInculateList(HttpServletRequest request) { + List dataList = new ArrayList<>(); + + LoginContext loginContext = (LoginContext) request.getAttribute("loginContext"); + + YmPushMessagesQuery query = new YmPushMessagesQuery(); + query.setYn(YnEnum.yes.getId()); + query.setReceptionId(loginContext.getId()); +// query.setCreated(new Date(time * 1000)); + List messagesList = ymPushMessagesService.queryYmPushMessages(query); + if (CollectionUtils.isNotEmpty(messagesList)){ + for (YmPushMessages data : messagesList){ + Map map = new HashMap<>(); + map.put("id",data.getId()); + map.put("content",data.getContent()); + map.put("created",DateUtil.getSecond(data.getCreated())); + map.put("type",2); + dataList.add(map); + } + } + return dataList; + } } diff --git a/webApi/src/main/java/com/lyms/yimiao/web/controller/v1/UserController.java b/webApi/src/main/java/com/lyms/yimiao/web/controller/v1/UserController.java index fdc83b7..f10a29c 100644 --- a/webApi/src/main/java/com/lyms/yimiao/web/controller/v1/UserController.java +++ b/webApi/src/main/java/com/lyms/yimiao/web/controller/v1/UserController.java @@ -48,8 +48,8 @@ public class UserController extends BaseController { private String typeId; @Autowired private YmUsersService usersService; - @Autowired - private TokenService tokenService; +// @Autowired +// private TokenService tokenService; @Autowired private YmUserKidMapsService ymUserKidMapsService; @Autowired @@ -86,8 +86,7 @@ public class UserController extends BaseController { LoginContext result = LoginUtil.login(phone, code, typeId, token); Map map = new HashMap<>(); - map.put("errorcode", result.getErrorcode()); - map.put("errormsg", result.getErrormsg()); + //登陆的同时,在数据库创建账号,对手机号唯一性判断 if (result.getToken()!=null){ YmUsersQuery query = new YmUsersQuery(); @@ -115,33 +114,38 @@ public class UserController extends BaseController { map.put("token", result.getToken()); map.put("id", users.getId()); } + map.put("errorcode", ConstantInterface.SUCCESS); + map.put("errormsg", "成功"); + }else { + map.put("errorcode",ConstantInterface.SYSTEM_ERROR); + map.put("errormsg", "登录失败"); } writeJson(response, JsonUtil.obj2JsonString(map)); } //暫時獲取token接口 - @RequestMapping(value = "/userlogin", method = RequestMethod.POST) - public void userLogin(HttpServletResponse response, - @RequestParam("phone") String phone){ - YmUsersQuery query = new YmUsersQuery(); - query.setPhone(phone); - query.setYn(YnEnum.yes.getId()); - List usersList = usersService.queryYmUsers(query); - Map map = new HashMap<>(); - if (CollectionUtils.isNotEmpty(usersList)){ - YmUsers users = usersList.get(0); - String token = tokenService.createToken(users.getId()); - map.put("token", token); - map.put("id", users.getId()); - users.setLastLoginTime(new Date()); - usersService.updateYmUsers(users); - map.put("errorcode", 0); - map.put("errormsg", "成功"); - writeJson(response,JsonUtil.obj2JsonString(map)); - }else { - ResultUtils.buildParameterErrorResultAndWrite(response,"该手机号不存在,请注册"); - } - } +// @RequestMapping(value = "/userlogin", method = RequestMethod.POST) +// public void userLogin(HttpServletResponse response, +// @RequestParam("phone") String phone){ +// YmUsersQuery query = new YmUsersQuery(); +// query.setPhone(phone); +// query.setYn(YnEnum.yes.getId()); +// List usersList = usersService.queryYmUsers(query); +// Map map = new HashMap<>(); +// if (CollectionUtils.isNotEmpty(usersList)){ +// YmUsers users = usersList.get(0); +// String token = tokenService.createToken(users.getId()); +// map.put("token", token); +// map.put("id", users.getId()); +// users.setLastLoginTime(new Date()); +// usersService.updateYmUsers(users); +// map.put("errorcode", 0); +// map.put("errormsg", "成功"); +// writeJson(response,JsonUtil.obj2JsonString(map)); +// }else { +// ResultUtils.buildParameterErrorResultAndWrite(response,"该手机号不存在,请注册"); +// } +// } /** * 注册(暂时不需要注册接口) diff --git a/webApi/src/main/resources/config.properties b/webApi/src/main/resources/config.properties index 28569c0..a4ab0a6 100644 --- a/webApi/src/main/resources/config.properties +++ b/webApi/src/main/resources/config.properties @@ -42,3 +42,10 @@ token.prefix=ams #---------apptokentypeId-------- yimiao.token=ce303762-b5b4-4156-84aa-e159d3241319 yimiao.typeId=3 + + +#jpush properties +jpush.AppKey=e3e9f9d33c13abc3f38db26e +jpush.MasterSecret=3c56e298d8e1bb90b94ccc30 +jpush.apnsProduction=0 +jpush.timeToLive=86400 \ No newline at end of file diff --git a/webApi/src/main/resources/spring/applicationContext.xml b/webApi/src/main/resources/spring/applicationContext.xml index 11c7d6f..74af277 100644 --- a/webApi/src/main/resources/spring/applicationContext.xml +++ b/webApi/src/main/resources/spring/applicationContext.xml @@ -14,6 +14,7 @@ + @@ -88,4 +89,18 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/webApi/web-api.iml b/webApi/web-api.iml index 8f34529..4d66e65 100644 --- a/webApi/web-api.iml +++ b/webApi/web-api.iml @@ -46,6 +46,8 @@ + + @@ -139,7 +141,6 @@ - -- 1.8.3.1