MessageService.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. msgJson.put("msgTime",msgArray[1] != null? DateUtil.dateToStr((Date) msgArray[1],DateUtil.YYYY_MM_DD):"");
  82. consultJson.put("lastMessage",msgJson);
  83. }
  84. }
  85. }
  86. // 签约未读消息总数
  87. int sign = messageDao.amountUnreadByReceiver(doctor);
  88. JSONObject signJson = new JSONObject();
  89. signJson.put("amount",sign);
  90. if(sign > 0){
  91. PageRequest pageRequest = new PageRequest(0,1);
  92. Page<Message> msgs = messageDao.amountUnreadLastByReceiver(doctor,pageRequest);
  93. if(msgs != null && msgs.getSize() > 0){
  94. for(Message msg : msgs){
  95. JSONObject msgJson = new JSONObject();
  96. if(msg.getSignStatus().equals("4")){
  97. msgJson.put("msg", msg.getSenderName() + "申请与您解除家庭签约");
  98. }else{
  99. msgJson.put("msg", msg.getSenderName() + "申请与您签约家庭医生");
  100. }
  101. msgJson.put("msgTime",msg.getCzrq() != null? DateUtil.dateToStr(msg.getCzrq(),DateUtil.YYYY_MM_DD):"");
  102. signJson.put("lastMessage",msgJson);
  103. }
  104. }
  105. }
  106. // 体征指标未读消息总数
  107. int healthIndex = messageDao.amountUnreadHealthByReceiver(doctor);
  108. JSONObject indexJson = new JSONObject();
  109. indexJson.put("amount",healthIndex);
  110. if(sign > 0){
  111. PageRequest pageRequest = new PageRequest(0,1);
  112. Page<Message> msgs = messageDao.amountUnreadHealthLastByReceiver(doctor,pageRequest);
  113. if(msgs != null && msgs.getSize() > 0){
  114. for(Message msg : msgs){
  115. JSONObject msgJson = new JSONObject();
  116. msgJson.put("msg",msg.getContent());
  117. msgJson.put("msgTime",msg.getCzrq() != null? DateUtil.dateToStr(msg.getCzrq(),DateUtil.YYYY_MM_DD):"");
  118. indexJson.put("lastMessage",msgJson);
  119. }
  120. }
  121. }
  122. JSONObject json = new JSONObject();
  123. json.put("consult", consultJson);
  124. json.put("sign", signJson);
  125. json.put("healthIndex", indexJson);
  126. return json;
  127. }
  128. /**
  129. * 查询健康咨询列表
  130. * @param doctor 医生标识
  131. * @param id
  132. * @param pagesize 分页大小
  133. * @return
  134. */
  135. public Page<ConsultTeam> findConsultListByDoctor(String doctor, long id, int pagesize) {
  136. if (pagesize <= 0) {
  137. pagesize = 10;
  138. }
  139. // 排序
  140. Sort sort = new Sort(Direction.DESC, "id");
  141. // 分页信息
  142. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  143. //健康咨询
  144. if (id > 0) {
  145. return consultTeamDao.findListByUnreadDoctor(doctor, id, pageRequest);
  146. } else {
  147. return consultTeamDao.findListByUnreadDoctor(doctor, pageRequest);
  148. }
  149. }
  150. /**
  151. * 查询体征消息列表
  152. * @param doctor 医生标识
  153. * @param id
  154. * @param pagesize 分页大小
  155. * @return
  156. */
  157. public Page<Message> findHealthListByDoctor(String doctor, long id, int pagesize, String isRead) {
  158. if (pagesize <= 0) {
  159. pagesize = 10;
  160. }
  161. // 排序
  162. Sort sort = new Sort(Direction.DESC, "id");
  163. // 分页信息
  164. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  165. // 设置查询条件
  166. Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
  167. filters.put("receiver", new SearchFilter("receiver", Operator.EQ, doctor));
  168. filters.put("type", new SearchFilter("type", Operator.EQ, "2"));
  169. if (id > 0) {
  170. filters.put("id", new SearchFilter("id", Operator.LT, id));
  171. }
  172. if (StringUtils.isNoneEmpty(isRead)) {
  173. filters.put("read", new SearchFilter("read", Operator.EQ, Integer.parseInt(isRead)));
  174. }
  175. Specification<Message> spec = DynamicSpecifications.bySearchFilter(filters.values(), Message.class);
  176. return messageDao.findAll(spec, pageRequest);
  177. }
  178. /**
  179. * 更新体征消息为已读
  180. * @param msgid
  181. */
  182. public int readHealth(long msgid) {
  183. return messageDao.read(msgid);
  184. }
  185. /**
  186. * 根据参数查询指定的消息
  187. * @return
  188. */
  189. public Message findMessage(String sender, String receiver, String signStatus) {
  190. return messageDao.findByParams(sender, receiver, signStatus);
  191. }
  192. }