MessageService.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package com.yihu.wlyy.service.app.message;
  2. import java.util.Date;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import javax.transaction.Transactional;
  6. import com.yihu.wlyy.entity.message.Message;
  7. import com.yihu.wlyy.repository.message.MessageDao;
  8. import com.yihu.wlyy.util.DateUtil;
  9. import org.apache.commons.lang3.StringUtils;
  10. import org.json.JSONObject;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.data.domain.Page;
  13. import org.springframework.data.domain.PageRequest;
  14. import org.springframework.data.domain.Sort;
  15. import org.springframework.data.domain.Sort.Direction;
  16. import org.springframework.data.jpa.domain.Specification;
  17. import org.springframework.stereotype.Component;
  18. import org.springside.modules.persistence.DynamicSpecifications;
  19. import org.springside.modules.persistence.SearchFilter;
  20. import org.springside.modules.persistence.SearchFilter.Operator;
  21. import com.yihu.wlyy.entity.consult.ConsultTeam;
  22. import com.yihu.wlyy.repository.consult.ConsultTeamDao;
  23. import com.yihu.wlyy.service.BaseService;
  24. /**
  25. * 消息业务处理类
  26. * @author George
  27. *
  28. */
  29. @Component
  30. @Transactional(rollbackOn = Exception.class)
  31. public class MessageService extends BaseService {
  32. @Autowired
  33. private ConsultTeamDao consultTeamDao;
  34. @Autowired
  35. private MessageDao messageDao;
  36. /**
  37. * 汇总查询医生的消息总数
  38. * @param doctor
  39. * @return
  40. */
  41. public JSONObject findDoctorAllMessageAmount(String doctor) {
  42. // 三师咨询未读消息总数
  43. int consult = 0;
  44. try {
  45. consult = consultTeamDao.amountAllDoctorUnread(doctor);
  46. } catch (Exception e) {
  47. }
  48. // 签约未读消息总数
  49. int sign = messageDao.amountUnreadByReceiver(doctor);
  50. // 体征指标未读消息总数
  51. int healthIndex = messageDao.amountUnreadHealthByReceiver(doctor);
  52. JSONObject json = new JSONObject();
  53. json.put("consultTeam", consult);
  54. json.put("sign", sign);
  55. json.put("healthIndex", healthIndex);
  56. return json;
  57. }
  58. /**
  59. * 查询医生未读消息和最后消息
  60. *
  61. * @param doctor
  62. * @return
  63. */
  64. public JSONObject findDoctorAllMessage(String doctor){
  65. // 三师咨询未读消息总数
  66. int consult = 0;
  67. JSONObject consultJson = new JSONObject();
  68. try {
  69. consult = consultTeamDao.amountAllDoctorUnread(doctor);
  70. } catch (Exception e) {
  71. }
  72. consultJson.put("amount",consult);
  73. if(consult > 0){
  74. PageRequest pageRequest = new PageRequest(0,1);
  75. Page<Object> msgs = consultTeamDao.AllDoctorUnreadLast(doctor,pageRequest);
  76. if(msgs != null && msgs.getSize() > 0){
  77. for(Object msg : msgs){
  78. Object[] msgArray = (Object[])msg;
  79. JSONObject msgJson = new JSONObject();
  80. msgJson.put("msg",msgArray[0].toString() + "向您发来一个咨询");
  81. if(msgArray[1] != null) {
  82. if (DateUtil.dateToStr(new Date(), DateUtil.YYYY_MM_DD).equals(DateUtil.dateToStr((Date) msgArray[1], DateUtil.YYYY_MM_DD))) {
  83. msgJson.put("msgTime", DateUtil.dateToStr((Date) msgArray[1], DateUtil.HH_MM));
  84. }else{
  85. msgJson.put("msgTime", DateUtil.dateToStr((Date) msgArray[1], DateUtil.YYYY_MM_DD_HH_MM));
  86. }
  87. }
  88. consultJson.put("lastMessage",msgJson);
  89. }
  90. }
  91. }
  92. int famousConsult = 0;
  93. JSONObject famousJson = new JSONObject();
  94. try {
  95. famousConsult = consultTeamDao.amountAlFamouslDoctorUnread(doctor);
  96. } catch (Exception e) {
  97. }
  98. famousJson.put("amount",famousConsult);
  99. if(famousConsult > 0){
  100. PageRequest pageRequest = new PageRequest(0,1);
  101. Page<Object> msgs = consultTeamDao.AllDoctorFamousUnreadLast(doctor,pageRequest);
  102. if(msgs != null && msgs.getSize() > 0){
  103. for(Object msg : msgs){
  104. Object[] msgArray = (Object[])msg;
  105. JSONObject msgJson = new JSONObject();
  106. msgJson.put("msg",msgArray[0].toString() + "向您发来一个咨询");
  107. msgJson.put("msgTime",msgArray[1] != null? DateUtil.dateToStr((Date) msgArray[1],DateUtil.YYYY_MM_DD):"");
  108. famousJson.put("lastMessage",msgJson);
  109. }
  110. }
  111. }
  112. // 签约未读消息总数
  113. int sign = messageDao.amountUnreadByReceiver(doctor);
  114. JSONObject signJson = new JSONObject();
  115. signJson.put("amount",sign);
  116. if(sign > 0){
  117. PageRequest pageRequest = new PageRequest(0,1);
  118. Page<Message> msgs = messageDao.amountUnreadLastByReceiver(doctor,pageRequest);
  119. if(msgs != null && msgs.getSize() > 0){
  120. for(Message msg : msgs){
  121. JSONObject msgJson = new JSONObject();
  122. if(msg.getSignStatus().equals("4")){
  123. msgJson.put("msg", msg.getSenderName() + "申请与您解除家庭签约");
  124. }else{
  125. msgJson.put("msg", msg.getSenderName() + "申请与您签约家庭医生");
  126. }
  127. msgJson.put("msgTime",msg.getCzrq() != null? DateUtil.dateToStr(msg.getCzrq(),DateUtil.YYYY_MM_DD):"");
  128. signJson.put("lastMessage",msgJson);
  129. }
  130. }
  131. }
  132. // 体征指标未读消息总数
  133. int healthIndex = messageDao.amountUnreadHealthByReceiver(doctor);
  134. JSONObject indexJson = new JSONObject();
  135. indexJson.put("amount",healthIndex);
  136. if(sign > 0){
  137. PageRequest pageRequest = new PageRequest(0,1);
  138. Page<Message> msgs = messageDao.amountUnreadHealthLastByReceiver(doctor,pageRequest);
  139. if(msgs != null && msgs.getSize() > 0){
  140. for(Message msg : msgs){
  141. JSONObject msgJson = new JSONObject();
  142. msgJson.put("msg",msg.getContent());
  143. msgJson.put("msgTime",msg.getCzrq() != null? DateUtil.dateToStr(msg.getCzrq(),DateUtil.YYYY_MM_DD):"");
  144. indexJson.put("lastMessage",msgJson);
  145. }
  146. }
  147. }
  148. JSONObject json = new JSONObject();
  149. json.put("consult", consultJson);
  150. json.put("famousConsult", famousJson);
  151. json.put("sign", signJson);
  152. json.put("healthIndex", indexJson);
  153. return json;
  154. }
  155. /**
  156. * 查询健康咨询列表
  157. * @param doctor 医生标识
  158. * @param id
  159. * @param pagesize 分页大小
  160. * @return
  161. */
  162. public Page<ConsultTeam> findConsultListByDoctor(String doctor, long id, int pagesize) {
  163. if (pagesize <= 0) {
  164. pagesize = 10;
  165. }
  166. // 排序
  167. Sort sort = new Sort(Direction.DESC, "id");
  168. // 分页信息
  169. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  170. //健康咨询
  171. if (id > 0) {
  172. return consultTeamDao.findListByUnreadDoctor(doctor, id, pageRequest);
  173. } else {
  174. return consultTeamDao.findListByUnreadDoctor(doctor, pageRequest);
  175. }
  176. }
  177. /**
  178. * 查询体征消息列表
  179. * @param doctor 医生标识
  180. * @param id
  181. * @param pagesize 分页大小
  182. * @return
  183. */
  184. public Page<Message> findHealthListByDoctor(String doctor, long id, int pagesize, String isRead) {
  185. if (pagesize <= 0) {
  186. pagesize = 10;
  187. }
  188. // 排序
  189. Sort sort = new Sort(Direction.DESC, "id");
  190. // 分页信息
  191. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  192. // 设置查询条件
  193. Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
  194. filters.put("receiver", new SearchFilter("receiver", Operator.EQ, doctor));
  195. filters.put("type", new SearchFilter("type", Operator.EQ, "2"));
  196. if (id > 0) {
  197. filters.put("id", new SearchFilter("id", Operator.LT, id));
  198. }
  199. if (StringUtils.isNoneEmpty(isRead)) {
  200. filters.put("read", new SearchFilter("read", Operator.EQ, Integer.parseInt(isRead)));
  201. }
  202. Specification<Message> spec = DynamicSpecifications.bySearchFilter(filters.values(), Message.class);
  203. return messageDao.findAll(spec, pageRequest);
  204. }
  205. /**
  206. * 更新体征消息为已读
  207. * @param msgid
  208. */
  209. public int readHealth(long msgid) {
  210. return messageDao.read(msgid);
  211. }
  212. /**
  213. * 根据参数查询指定的消息
  214. * @return
  215. */
  216. public Message findMessage(String sender, String receiver, String signStatus) {
  217. return messageDao.findByParams(sender, receiver, signStatus);
  218. }
  219. }