MessageService.java 7.2 KB

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