NoticeJob.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package com.yihu.wlyy.job;
  2. import com.yihu.wlyy.entity.doctor.profile.Doctor;
  3. import com.yihu.wlyy.entity.job.QuartzJobLog;
  4. import com.yihu.wlyy.repository.doctor.DoctorDao;
  5. import com.yihu.wlyy.repository.job.QuartzJobLogDao;
  6. import com.yihu.wlyy.repository.message.MessageDao;
  7. import com.yihu.wlyy.service.app.scheduling.DoctorWorkTimeService;
  8. import com.yihu.wlyy.task.NoticeJobPushMsgTask;
  9. import com.yihu.wlyy.task.PushMsgTask;
  10. import com.yihu.wlyy.util.HttpClientUtil;
  11. import net.sf.json.JSONArray;
  12. import net.sf.json.JSONObject;
  13. import org.quartz.Job;
  14. import org.quartz.JobExecutionContext;
  15. import org.quartz.JobExecutionException;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.beans.factory.annotation.Value;
  18. import org.springframework.context.annotation.Scope;
  19. import org.springframework.jdbc.core.JdbcTemplate;
  20. import org.springframework.stereotype.Component;
  21. import org.springframework.transaction.annotation.Transactional;
  22. import java.util.Date;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.Map;
  26. /**
  27. * Created by Administrator on 2016/11/7.
  28. */
  29. @Component
  30. @Scope("prototype")
  31. public class NoticeJob implements Job {
  32. public static String jobKey = "Notice_JOB";
  33. public static String jobCron = "0 0 8,14 * * ?"; //每天8点,14点,20 点执行一次
  34. private static String noticeKey1 = "D_ST_01";//专科
  35. private static String noticeKey2 = "D_ST_02";//全科
  36. private static String noticeKey3 = "D_ST_03";//健康管理师
  37. private static String noticeTitle = "系统消息";
  38. @Autowired
  39. private DoctorWorkTimeService doctorWorkTimeService;
  40. @Autowired
  41. private QuartzJobLogDao dbStorage;
  42. @Autowired
  43. private DoctorDao doctorDao;
  44. @Value("${systemConfig.msg_push_server}")
  45. private String url;
  46. @Autowired
  47. private JdbcTemplate jdbcTemplate;
  48. @Autowired
  49. private MessageDao messageDao;
  50. @Autowired
  51. private HttpClientUtil httpClientUtil;
  52. @Transactional
  53. public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
  54. //新建任务日志对象
  55. QuartzJobLog quartzJobLog = new QuartzJobLog();
  56. quartzJobLog.setJobStartTime(new Date());
  57. quartzJobLog.setJobId(jobKey);
  58. quartzJobLog.setJobName(jobKey);
  59. try {
  60. List<Doctor> doctors = doctorDao.findAllDoctors();
  61. //得到所有未发送的医生的通知
  62. Map<String, JSONObject> maps = new HashMap<>();
  63. //发送通知
  64. for (Doctor doctor : doctors) {
  65. //判断医生是不是在工作时间内
  66. String noticeContent1 = " 您当前有[zxsize]条未处理消息,其中[qysize]条为签约消息。请尽快处理~";
  67. String noticeContent2 = " 您当前有[zxsize]条未处理消息,请尽快处理~";
  68. String noticeContent3 = " 您当前有[qysize]条签约消息待处理,请尽快处理~";
  69. String workTime = "";
  70. try {
  71. workTime = doctorWorkTimeService.isDoctorWorking(doctor.getCode(), -1).getString("status");
  72. } catch (Exception e) {
  73. }
  74. if ("1".equals(workTime)) {
  75. //得到医生多少条咨询消息
  76. Integer zxsize = getDoctorMesssageCount(doctor.getCode(), 1);
  77. //得到医生多少条签约消息
  78. Integer qysize = getDoctorMesssageCount(doctor.getCode(), 2);
  79. //如果既有签约又有咨询
  80. if (zxsize > 0 && qysize > 0) {
  81. JSONObject jo = new JSONObject();
  82. noticeContent1 = noticeContent1.replace("[zxsize]", zxsize + "");
  83. noticeContent1 = noticeContent1.replace("[qysize]", qysize + "");
  84. jo.put("content", noticeContent1);
  85. jo.put("level", doctor.getLevel());
  86. maps.put(doctor.getCode(), jo);
  87. }
  88. //只有咨询
  89. if (zxsize > 0 && qysize == 0) {
  90. JSONObject jo = new JSONObject();
  91. noticeContent2 = noticeContent2.replace("[zxsize]", zxsize + "");
  92. jo.put("content", noticeContent2);
  93. jo.put("level", doctor.getLevel());
  94. maps.put(doctor.getCode(), jo);
  95. }
  96. //只有咨询
  97. if (zxsize == 0 && qysize > 0) {
  98. JSONObject jo = new JSONObject();
  99. noticeContent3 = noticeContent3.replace("[qysize]", qysize + "");
  100. jo.put("content", noticeContent3);
  101. jo.put("level", doctor.getLevel());
  102. maps.put(doctor.getCode(), jo);
  103. }
  104. }
  105. }
  106. JSONArray jsonArray = new JSONArray();
  107. //发送通知
  108. for (Map.Entry<String, JSONObject> entry : maps.entrySet()) {
  109. // 异常通知
  110. JSONObject json = new JSONObject();
  111. json.put("receiver", entry.getKey());
  112. json.put("type", getType(entry.getValue().getInt("level")));
  113. json.put("title", noticeTitle);
  114. json.put("msg", entry.getValue().getString("content"));
  115. json.put("data", "");
  116. jsonArray.add(json);
  117. }
  118. // 推送消息给医生
  119. NoticeJobPushMsgTask.url = url;
  120. // 推送消息给医生
  121. NoticeJobPushMsgTask.getInstance().put(jsonArray);
  122. //保存日志
  123. quartzJobLog.setJobEndTime(new Date());
  124. quartzJobLog.setJobContent("通知成功");
  125. quartzJobLog.setJobType("1");
  126. dbStorage.save(quartzJobLog);
  127. } catch (Exception e) {
  128. //保存日志
  129. quartzJobLog.setJobEndTime(new Date());
  130. quartzJobLog.setJobContent("通知失败");
  131. quartzJobLog.setJobType("0");
  132. dbStorage.save(quartzJobLog);
  133. e.printStackTrace();
  134. }
  135. }
  136. private String getType(Integer level) {
  137. switch (level) {
  138. case 1: {
  139. //专科
  140. return noticeKey1;
  141. }
  142. case 2: {
  143. //全科
  144. return noticeKey2;
  145. }
  146. case 3: {
  147. //健康管理师
  148. return noticeKey3;
  149. }
  150. }
  151. return "";
  152. }
  153. /**
  154. * 得到医生的未读数目
  155. *
  156. * @param doctorCode 医生code
  157. * @param type 1是咨询 2个签约 3是体征
  158. * @return
  159. */
  160. private Integer getDoctorMesssageCount(String doctorCode, int type) throws Exception {
  161. switch (type) {
  162. case 1: {
  163. return getImMsgAmount(doctorCode);
  164. }
  165. case 2: {
  166. String sql = "select count(a.id) from wlyy_message a where a.type =1 and a.has_read=1 and a.receiver=?";
  167. return jdbcTemplate.queryForObject(sql, Integer.class, doctorCode);
  168. }
  169. }
  170. return 0;
  171. }
  172. private int findDoctorAllMessage(String doctor) {
  173. // 签约未读消息总数
  174. int sign = messageDao.amountUnreadByReceiver(doctor);
  175. // 体征指标未读消息总数
  176. int healthIndex = messageDao.amountUnreadHealthByReceiver(doctor);
  177. //系统消息
  178. int systemMessage = messageDao.amountUnreadSystemByReceiver(doctor);
  179. Integer doctorCount = 0;
  180. Integer patientCount = 0;
  181. JSONObject json = new JSONObject();
  182. getImMsgAmount(json, doctor);
  183. if (json.containsKey("imMsgCount")) {
  184. JSONObject jsonC = json.getJSONObject("imMsgCount");
  185. if (jsonC.containsKey("doctor")) {
  186. doctorCount = jsonC.getInt("doctor");
  187. }
  188. if (jsonC.containsKey("patient")) {
  189. patientCount = jsonC.getInt("patient");
  190. }
  191. }
  192. return sign + healthIndex + systemMessage + doctorCount + patientCount;
  193. }
  194. private void getImMsgAmount(JSONObject obj, String doctor) {
  195. String urlall = url + "/api/v1/chats/msg/amount?user_id=" + doctor;
  196. String response = httpClientUtil.get(urlall, "UTF-8");
  197. obj.put("imMsgCount", response);
  198. }
  199. private Integer getImMsgAmount(String doctor) throws Exception {
  200. /**
  201. * {"msg":"获取消息总数成功!","data":{"imMsgCount":"{\"doctor\":0,\"patient\":0}","healthIndex":{"amount":0},"sign":{"amount":0}},"status":200}
  202. */
  203. return findDoctorAllMessage(doctor);
  204. }
  205. }