AutoTimeOutRemind.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package com.yihu.jw.util;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.yihu.jw.entity.hospital.consult.WlyyHospitalSysDictDO;
  4. import com.yihu.jw.entity.hospital.prescription.WlyyOutpatientDO;
  5. import com.yihu.jw.hospital.dict.WlyyHospitalSysDictDao;
  6. import com.yihu.jw.hospital.prescription.dao.OutpatientDao;
  7. import com.yihu.jw.hospital.prescription.service.PrescriptionService;
  8. import com.yihu.jw.im.util.ImUtil;
  9. import com.yihu.jw.util.date.DateUtil;
  10. import org.apache.commons.lang3.StringUtils;
  11. import org.bouncycastle.asn1.cmp.CertOrEncCert;
  12. import org.slf4j.Logger;
  13. import org.slf4j.LoggerFactory;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.beans.factory.annotation.Value;
  16. import org.springframework.context.annotation.Lazy;
  17. import org.springframework.scheduling.Trigger;
  18. import org.springframework.scheduling.TriggerContext;
  19. import org.springframework.scheduling.annotation.EnableScheduling;
  20. import org.springframework.scheduling.annotation.Scheduled;
  21. import org.springframework.scheduling.annotation.SchedulingConfigurer;
  22. import org.springframework.scheduling.config.ScheduledTaskRegistrar;
  23. import org.springframework.scheduling.support.CronTrigger;
  24. import org.springframework.stereotype.Component;
  25. import java.util.Date;
  26. import java.util.List;
  27. @Lazy(false)
  28. @Component
  29. @EnableScheduling
  30. public class AutoTimeOutRemind implements SchedulingConfigurer {
  31. private static final Logger logger = LoggerFactory.getLogger(AutoTimeOutRemind.class);
  32. private static String cron;
  33. private static String dictName = "remind_patient_job";
  34. @Autowired
  35. private WlyyHospitalSysDictDao wlyyHospitalSysDictDao;
  36. @Autowired
  37. public ImUtil imUtil;
  38. @Autowired
  39. private OutpatientDao outpatientDao;
  40. @Autowired
  41. private PrescriptionService prescriptionService;
  42. @Value("${wechat.ids}")
  43. private String wxId;
  44. public AutoTimeOutRemind() {
  45. cron = "0 /5 * * * ?";
  46. System.out.println("创建时的corn"+cron);
  47. }
  48. public String change(String corIn){
  49. if (StringUtils.isNotBlank(corIn)){
  50. cron = corIn;
  51. }else {
  52. List<WlyyHospitalSysDictDO> wlyyHospitalSysDictDO =wlyyHospitalSysDictDao.findByDictName(dictName);
  53. if (wlyyHospitalSysDictDO.size()>0){
  54. cron = wlyyHospitalSysDictDO.get(0).getDictValue();
  55. System.out.println("dict"+ cron);
  56. }
  57. }
  58. System.out.println(cron);
  59. return "success";
  60. }
  61. @Override
  62. public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
  63. taskRegistrar.addTriggerTask(new Runnable() {
  64. @Override
  65. public void run() {
  66. logger.info("启动发送超时提示消息开始");
  67. System.out.println(wxId);
  68. System.out.println("启动发送超时提示消息开始");
  69. Integer remindCount = 5;
  70. List<WlyyHospitalSysDictDO> countList = wlyyHospitalSysDictDao.findByDictName("remind_count");
  71. if (countList.size()>0){
  72. remindCount= null!=countList.get(0).getDictValue()?Integer.parseInt(countList.get(0).getDictValue()):5;
  73. }
  74. List<WlyyOutpatientDO> wlyyOutpatientDOS= outpatientDao.findWaitingOutpatient(remindCount);
  75. String senderId ="";
  76. String reciverId = "";
  77. long timeCount = 0l;
  78. String content = "您邀请的医师暂无应答,您可以选择继续等待或者取消邀请。";
  79. JSONObject object = new JSONObject();
  80. object.put("socket_sms_type",14);
  81. object.put("msg",content);
  82. object.put("msg_time", DateUtil.dateToStrLong(new Date()));
  83. if (null!=wlyyOutpatientDOS){
  84. for (WlyyOutpatientDO wlyyOutpatientDO:wlyyOutpatientDOS){
  85. long patientTime = wlyyOutpatientDO.getCreateTime().getTime();
  86. Integer currentRemindCount = wlyyOutpatientDO.getRemindCount();
  87. logger.info("接诊创建时间="+patientTime);
  88. List<WlyyHospitalSysDictDO> wlyyHospitalSysDictDOS = wlyyHospitalSysDictDao.findByDictName("outpatient_timeout_remind");
  89. if (wlyyHospitalSysDictDOS.size()>0){
  90. timeCount = Long.valueOf(wlyyHospitalSysDictDOS.get(0).getDictValue());
  91. }
  92. long currentTime = new Date().getTime();
  93. logger.info("当前时间="+currentTime);
  94. logger.info("数据库配置时间=0"+timeCount);
  95. if (currentTime-patientTime>timeCount*60*1000){
  96. logger.info("--便利发送消息");
  97. reciverId = wlyyOutpatientDO.getPatient();
  98. logger.info("---发送人id"+senderId);
  99. senderId= wlyyOutpatientDO.getDoctor();
  100. logger.info("---接受人Id"+reciverId);
  101. imUtil.sendMessage(senderId,reciverId,"1",object.toString());
  102. logger.info("--发送结束");
  103. logger.info("--模板发送开始");
  104. prescriptionService.sendWxTemplateMsg(wxId,wlyyOutpatientDO.getId(),null,null,"outPatientTimeOutRemind","");
  105. wlyyOutpatientDO.setRemindCount(currentRemindCount+1);
  106. outpatientDao.save(wlyyOutpatientDO);
  107. }
  108. }
  109. }
  110. }
  111. }, new Trigger() {
  112. @Override
  113. public Date nextExecutionTime(TriggerContext triggerContext) {
  114. // 任务触发,可修改任务的执行周期
  115. CronTrigger trigger = new CronTrigger(cron);
  116. System.out.println("任务触发,可修改任务的执行周期"+cron);
  117. Date nextExec = trigger.nextExecutionTime(triggerContext);
  118. return nextExec;
  119. }
  120. });
  121. }
  122. }