MessageService.java 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. *
  27. * @author George
  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. *
  39. * @param doctor
  40. * @return
  41. */
  42. public JSONObject findDoctorAllMessageAmount(String doctor) {
  43. // 三师咨询未读消息总数
  44. int consult = 0;
  45. try {
  46. consult = consultTeamDao.amountAllDoctorUnread(doctor);
  47. } catch (Exception e) {
  48. }
  49. // 签约未读消息总数
  50. int sign = messageDao.amountUnreadByReceiver(doctor);
  51. // 体征指标未读消息总数
  52. int healthIndex = messageDao.amountUnreadHealthByReceiver(doctor);
  53. JSONObject json = new JSONObject();
  54. json.put("consultTeam", consult);
  55. json.put("sign", sign);
  56. json.put("healthIndex", healthIndex);
  57. return json;
  58. }
  59. /**
  60. * 查询医生未读消息和最后消息
  61. *
  62. * @param doctor
  63. * @return
  64. */
  65. public JSONObject findDoctorAllMessage(String doctor) {
  66. // 三师咨询未读消息总数
  67. int consult = 0;
  68. JSONObject consultJson = new JSONObject();
  69. try {
  70. consult = consultTeamDao.amountAllDoctorUnread(doctor);
  71. } catch (Exception e) {
  72. }
  73. consultJson.put("amount", consult);
  74. if (consult > 0) {
  75. PageRequest pageRequest = new PageRequest(0, 1);
  76. Page<Object> msgs = consultTeamDao.AllDoctorUnreadLast(doctor, pageRequest);
  77. if (msgs != null && msgs.getSize() > 0) {
  78. for (Object msg : msgs) {
  79. Object[] msgArray = (Object[]) msg;
  80. JSONObject msgJson = new JSONObject();
  81. msgJson.put("msg", msgArray[0].toString() + "向您发来一个咨询");
  82. if (msgArray[1] != null) {
  83. if (DateUtil.dateToStr(new Date(), DateUtil.YYYY_MM_DD).equals(DateUtil.dateToStr((Date) msgArray[1], DateUtil.YYYY_MM_DD))) {
  84. msgJson.put("msgTime", DateUtil.dateToStr((Date) msgArray[1], DateUtil.HH_MM));
  85. } else {
  86. msgJson.put("msgTime", DateUtil.dateToStr((Date) msgArray[1], DateUtil.YYYY_MM_DD_HH_MM));
  87. }
  88. } else {
  89. msgJson.put("msgTime", "");
  90. }
  91. consultJson.put("lastMessage", msgJson);
  92. }
  93. }
  94. }
  95. int famousConsult = 0;
  96. JSONObject famousJson = new JSONObject();
  97. try {
  98. famousConsult = consultTeamDao.amountAlFamouslDoctorUnread(doctor);
  99. } catch (Exception e) {
  100. }
  101. famousJson.put("amount", famousConsult);
  102. if (famousConsult > 0) {
  103. PageRequest pageRequest = new PageRequest(0, 1);
  104. Page<Object> msgs = consultTeamDao.AllDoctorFamousUnreadLast(doctor, pageRequest);
  105. if (msgs != null && msgs.getSize() > 0) {
  106. for (Object msg : msgs) {
  107. Object[] msgArray = (Object[]) msg;
  108. JSONObject msgJson = new JSONObject();
  109. msgJson.put("msg", msgArray[0].toString() + "向您发来一个咨询");
  110. if (msgArray[1] != null) {
  111. if (DateUtil.dateToStr(new Date(), DateUtil.YYYY_MM_DD).equals(DateUtil.dateToStr((Date) msgArray[1], DateUtil.YYYY_MM_DD))) {
  112. msgJson.put("msgTime", DateUtil.dateToStr((Date) msgArray[1], DateUtil.HH_MM));
  113. } else {
  114. msgJson.put("msgTime", DateUtil.dateToStr((Date) msgArray[1], DateUtil.YYYY_MM_DD_HH_MM));
  115. }
  116. } else {
  117. msgJson.put("msgTime", "");
  118. }
  119. famousJson.put("lastMessage", msgJson);
  120. }
  121. }
  122. }
  123. // 签约未读消息总数
  124. int sign = messageDao.amountUnreadByReceiver(doctor);
  125. JSONObject signJson = new JSONObject();
  126. signJson.put("amount", sign);
  127. if (sign > 0) {
  128. PageRequest pageRequest = new PageRequest(0, 1);
  129. Page<Message> msgs = messageDao.amountUnreadLastByReceiver(doctor, pageRequest);
  130. if (msgs != null && msgs.getSize() > 0) {
  131. for (Message msg : msgs) {
  132. JSONObject msgJson = new JSONObject();
  133. if (msg.getSignStatus().equals("4")) {
  134. msgJson.put("msg", msg.getSenderName() + "申请与您解除家庭签约");
  135. } else {
  136. msgJson.put("msg", msg.getSenderName() + "申请与您签约家庭医生");
  137. }
  138. msgJson.put("msgTime", msg.getCzrq() != null ? DateUtil.dateToStr(msg.getCzrq(), DateUtil.YYYY_MM_DD) : "");
  139. signJson.put("lastMessage", msgJson);
  140. }
  141. }
  142. }
  143. // 体征指标未读消息总数
  144. int healthIndex = messageDao.amountUnreadHealthByReceiver(doctor);
  145. JSONObject indexJson = new JSONObject();
  146. indexJson.put("amount", healthIndex);
  147. if (sign > 0) {
  148. PageRequest pageRequest = new PageRequest(0, 1);
  149. Page<Message> msgs = messageDao.amountUnreadHealthLastByReceiver(doctor, pageRequest);
  150. if (msgs != null && msgs.getSize() > 0) {
  151. for (Message msg : msgs) {
  152. JSONObject msgJson = new JSONObject();
  153. msgJson.put("msg", msg.getContent());
  154. msgJson.put("msgTime", msg.getCzrq() != null ? DateUtil.dateToStr(msg.getCzrq(), DateUtil.YYYY_MM_DD) : "");
  155. indexJson.put("lastMessage", msgJson);
  156. }
  157. }
  158. }
  159. JSONObject json = new JSONObject();
  160. json.put("consult", consultJson);
  161. json.put("famousConsult", famousJson);
  162. json.put("sign", signJson);
  163. json.put("healthIndex", indexJson);
  164. return json;
  165. }
  166. /**
  167. * 查询健康咨询列表
  168. *
  169. * @param doctor 医生标识
  170. * @param id
  171. * @param pagesize 分页大小
  172. * @return
  173. */
  174. public Page<ConsultTeam> findConsultListByDoctor(String doctor, long id, int pagesize) {
  175. if (pagesize <= 0) {
  176. pagesize = 10;
  177. }
  178. // 排序
  179. Sort sort = new Sort(Direction.DESC, "id");
  180. // 分页信息
  181. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  182. //健康咨询
  183. if (id > 0) {
  184. return consultTeamDao.findListByUnreadDoctor(doctor, id, pageRequest);
  185. } else {
  186. return consultTeamDao.findListByUnreadDoctor(doctor, pageRequest);
  187. }
  188. }
  189. /**
  190. * 查询体征消息列表
  191. *
  192. * @param doctor 医生标识
  193. * @param id
  194. * @param pagesize 分页大小
  195. * @return
  196. */
  197. public Page<Message> findHealthListByDoctor(String doctor, long id, int pagesize, String isRead) {
  198. if (pagesize <= 0) {
  199. pagesize = 10;
  200. }
  201. // 排序
  202. Sort sort = new Sort(Direction.DESC, "id");
  203. // 分页信息
  204. PageRequest pageRequest = new PageRequest(0, pagesize, sort);
  205. // 设置查询条件
  206. Map<String, SearchFilter> filters = new HashMap<String, SearchFilter>();
  207. filters.put("receiver", new SearchFilter("receiver", Operator.EQ, doctor));
  208. filters.put("type", new SearchFilter("type", Operator.EQ, "2"));
  209. if (id > 0) {
  210. filters.put("id", new SearchFilter("id", Operator.LT, id));
  211. }
  212. if (StringUtils.isNoneEmpty(isRead)) {
  213. filters.put("read", new SearchFilter("read", Operator.EQ, Integer.parseInt(isRead)));
  214. }
  215. Specification<Message> spec = DynamicSpecifications.bySearchFilter(filters.values(), Message.class);
  216. return messageDao.findAll(spec, pageRequest);
  217. }
  218. /**
  219. * 更新体征消息为已读
  220. *
  221. * @param msgid
  222. */
  223. public int readHealth(long msgid) {
  224. return messageDao.read(msgid);
  225. }
  226. /**
  227. * 根据参数查询指定的消息
  228. *
  229. * @return
  230. */
  231. public Message findMessage(String sender, String receiver, String signStatus) {
  232. return messageDao.findByParams(sender, receiver, signStatus);
  233. }
  234. }