TriageService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package thyyxxk.webserver.service.outpatient.triage;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.stereotype.Service;
  9. import org.springframework.transaction.annotation.Transactional;
  10. import org.springframework.web.client.RestTemplate;
  11. import thyyxxk.webserver.config.exception.ExceptionEnum;
  12. import thyyxxk.webserver.constants.Capacity;
  13. import thyyxxk.webserver.dao.his.outpatient.triage.TriageDao;
  14. import thyyxxk.webserver.entity.ResultVo;
  15. import thyyxxk.webserver.entity.dictionary.CodeName;
  16. import thyyxxk.webserver.entity.outpatient.triage.*;
  17. import thyyxxk.webserver.entity.socketmessage.ApiMessageBody;
  18. import thyyxxk.webserver.service.externalhttp.WebSocketService;
  19. import thyyxxk.webserver.utils.DateUtil;
  20. import thyyxxk.webserver.utils.ResultVoUtil;
  21. import thyyxxk.webserver.utils.StringUtil;
  22. import thyyxxk.webserver.utils.TokenUtil;
  23. import java.util.*;
  24. /**
  25. * @author dj
  26. */
  27. @Slf4j
  28. @Service
  29. public class TriageService {
  30. private final TriageDao dao;
  31. @Value("${triage-notify-url}")
  32. private String triageNotifyUrl;
  33. private final WebSocketService socketService;
  34. public TriageService(TriageDao dao, WebSocketService socketService) {
  35. this.dao = dao;
  36. this.socketService = socketService;
  37. }
  38. public ResultVo<Map<String, Object>> getUnTriagedPatients(Integer currentPage, Integer pageSize) {
  39. String[] depts = StringUtil.triageDeptString2Array(dao.selectChosenDepts(TokenUtil.getInstance().getTokenUserId()));
  40. if (depts.length == 0) {
  41. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请先前往【科室设置】分配分诊科室!");
  42. }
  43. Map<String, Object> map = new HashMap<>(Capacity.TWO);
  44. IPage<MzfzPatientOrder> iPage = new Page<>(currentPage, pageSize);
  45. iPage = dao.getUnTriagedPatients(iPage, depts);
  46. map.put("totalSize", iPage.getTotal());
  47. map.put("list", iPage.getRecords());
  48. return ResultVoUtil.success(map);
  49. }
  50. public ResultVo<List<String>> getPatientIdByIdNo(String idNo) {
  51. List<String> ids = dao.selectPatientIdByIdNo(idNo);
  52. if (ids.isEmpty()) {
  53. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "患者在我院的就诊卡没有绑定身份证,无法查找。");
  54. }
  55. return ResultVoUtil.success(ids);
  56. }
  57. public ResultVo<List<MzfzPatientOrder>> getTriagedPatients() {
  58. String[] depts = StringUtil.triageDeptString2Array(dao.selectChosenDepts(TokenUtil.getInstance().getTokenUserId()));
  59. if (depts.length == 0) {
  60. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请先前往【科室设置】分配分诊科室!");
  61. }
  62. return ResultVoUtil.success(dao.getTriagedPatients(depts));
  63. }
  64. public ResultVo<List<MzfzPatientOrder>> getTreatedPatients() {
  65. String[] depts = StringUtil.triageDeptString2Array(dao.selectChosenDepts(TokenUtil.getInstance().getTokenUserId()));
  66. if (depts.length == 0) {
  67. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请先前往【科室设置】分配分诊科室!");
  68. }
  69. return ResultVoUtil.success(dao.getTreatedPatients(depts));
  70. }
  71. public ResultVo<List<FloorScreen>> getBigScreenData(Boolean fullName) {
  72. String[] depts = StringUtil.triageDeptString2Array(dao.selectChosenDepts(TokenUtil.getInstance().getTokenUserId()));
  73. if (depts.length == 0) {
  74. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请先前往【科室设置】分配分诊科室!");
  75. }
  76. List<MzfzPatientOrder> list = dao.getBigScreenData(depts);
  77. Map<String, FloorScreen> map = new HashMap<>(Capacity.DEFAULT);
  78. for (MzfzPatientOrder item : list) {
  79. if (!fullName) {
  80. String name = item.getName();
  81. if (name.length() == 2) {
  82. name = name.charAt(0) + "*";
  83. } else {
  84. name = name.charAt(0) + "*" + name.substring(2);
  85. }
  86. item.setName(name);
  87. }
  88. String key = String.format("%s_%s", item.getRoomCode(), item.getDoctorCode());
  89. if (!map.containsKey(key)) {
  90. FloorScreen fl = new FloorScreen();
  91. fl.setDeptName(item.getDeptName());
  92. fl.setDoctorTitle(item.getDoctorTitle());
  93. fl.setDoctorName(item.getDoctorName());
  94. fl.setRoomNo(item.getRoomNo());
  95. List<MzfzPatientOrder> temp = new ArrayList<>();
  96. temp.add(item);
  97. fl.setPatients(temp);
  98. map.put(key, fl);
  99. } else {
  100. map.get(key).getPatients().add(item);
  101. }
  102. }
  103. List<FloorScreen> ret = new ArrayList<>();
  104. for (Map.Entry<String, FloorScreen> entry : map.entrySet()) {
  105. ret.add(entry.getValue());
  106. }
  107. for (FloorScreen item : ret) {
  108. int size = item.getPatients().size();
  109. if (size % 4 == 0) {
  110. item.setPageNum(size / 4);
  111. } else {
  112. item.setPageNum(size / 4 + 1);
  113. }
  114. }
  115. return ResultVoUtil.success(ret);
  116. }
  117. public ResultVo<List<CodeName>> getChosenDept() {
  118. String[] depts = StringUtil.triageDeptString2Array(dao.selectChosenDepts(TokenUtil.getInstance().getTokenUserId()));
  119. if (depts.length == 0) {
  120. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请先前往【科室设置】分配分诊科室!");
  121. }
  122. return ResultVoUtil.success(dao.getDept(depts));
  123. }
  124. public ResultVo<List<MzfzZdDeptRoom>> getRooms(String deptCode) {
  125. return ResultVoUtil.success(dao.getRooms(deptCode));
  126. }
  127. public ResultVo<String> hasDoneCovidAssessment(String patientId) {
  128. final String socialNo = dao.getSocialNo(patientId);
  129. final Integer has = dao.getCovidAssessment(socialNo);
  130. if (null == has) {
  131. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "此患者近期未做流调。");
  132. }
  133. return ResultVoUtil.success();
  134. }
  135. @Transactional(rollbackFor = Exception.class)
  136. public ResultVo<String> fenZhen(MzfzZdDeptRoom room) {
  137. room.setTriageStaff(TokenUtil.getInstance().getTokenUserId());
  138. int patientNum = dao.getPatientNum(room.getDeptCode(), room.getRoomCode());
  139. if (patientNum >= 0) {
  140. room.setPatientNum(patientNum);
  141. }
  142. dao.updatePatientNum(room.getDeptCode(), room.getRoomCode(), patientNum);
  143. dao.fenZhen(room);
  144. log.info("分诊: update mzfz_patient_order set status_flag=1, slow_flag=0, dept_code={}, room_code={}, " +
  145. "doctor_code={}, fz_no={}, fz_flag={} where serial_no={}",
  146. room.getDeptCode(), room.getRoomCode(), room.getDoctorCode(),
  147. room.getPatientNum(), room.getFuzhenFlag(), room.getSerialNo());
  148. dao.deleteNotifyRelation(room.getSerialNo());
  149. dao.insertNotifyRelation(room.getSerialNo(), TokenUtil.getInstance().getTokenUserId(), room.getRoomCode());
  150. return ResultVoUtil.success("分诊成功。");
  151. }
  152. public void notifyComplete(Integer serialNo) {
  153. log.info("通知完毕>>> {}", serialNo);
  154. dao.updateNotifyDatetime(serialNo);
  155. }
  156. public ResultVo<Integer> fuZhen(Integer serialNo) {
  157. return ResultVoUtil.success(dao.fuZhen(serialNo));
  158. }
  159. public ResultVo<Integer> cancelTriage(Integer serialNo) {
  160. Integer ret = dao.cancelTriage(serialNo);
  161. dao.deleteNotifyRelation(serialNo);
  162. return ResultVoUtil.success(ret);
  163. }
  164. public ResultVo<Map<String, Object>> getAllPatients(String searchContent, Integer currentPage, Integer pageSize) {
  165. String[] depts = StringUtil.triageDeptString2Array(dao.selectChosenDepts(TokenUtil.getInstance().getTokenUserId()));
  166. if (depts.length == 0) {
  167. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请先前往【科室设置】分配分诊科室!");
  168. }
  169. Map<String, Object> map = new HashMap<>(Capacity.TWO);
  170. searchContent = StringUtil.isBlank(searchContent) ? "%%" : searchContent + "%";
  171. IPage<MzfzPatientOrder> iPage = new Page<>(currentPage, pageSize);
  172. iPage = dao.getAllPatients(iPage, depts, searchContent);
  173. map.put("totalSize", iPage.getTotal());
  174. map.put("list", iPage.getRecords());
  175. return ResultVoUtil.success(map);
  176. }
  177. public ResultVo<MzYshTzxx> getMzVitalSigns(String patientId) {
  178. log.info("获取生命体征:{}", patientId);
  179. MzYshTzxx vitalSigns = dao.getMzVitalSigns(patientId);
  180. if (null == vitalSigns) {
  181. vitalSigns = new MzYshTzxx();
  182. }
  183. vitalSigns.setPatientId(patientId);
  184. vitalSigns.setVisitDate(new Date());
  185. return ResultVoUtil.success(vitalSigns);
  186. }
  187. public ResultVo<String> saveMzVitalSigns(MzYshTzxx param) {
  188. log.info("保存生命体征:{}", param);
  189. if (null == dao.getMzVitalSigns(param.getPatientId())) {
  190. dao.insertMzVitalSigns(param);
  191. } else {
  192. dao.updateMzVitalSigns(param);
  193. }
  194. return ResultVoUtil.success("保存成功");
  195. }
  196. public ResultVo<String> notifyMessage(MessageForPush param) {
  197. if (null != param.getAction() && param.getAction() == 99) {
  198. return notifyVipReq(param.getSerialNo());
  199. }
  200. MessageForPush messageForPush = dao.selectMessageForPush(param.getSerialNo());
  201. if (null == messageForPush) {
  202. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "没有找到此患者的分诊信息!");
  203. }
  204. messageForPush.setAction(param.getAction());
  205. String text = String.format("请%s号%s,到%s%s号诊室就诊。", messageForPush.getFzNo(), messageForPush.getName(),
  206. messageForPush.getDeptName(), messageForPush.getRoomNo());
  207. log.info("消息推送>>> {}\n{}", messageForPush, text);
  208. socketService.textToSpeech(text, String.valueOf(param.getSerialNo()));
  209. final String msg = JSON.toJSONString(messageForPush);
  210. TriageNotifyRelation notifyRelation = dao.selectTriageNotifyRelation(messageForPush.getSerialNo());
  211. if (null == notifyRelation) {
  212. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "没有找到对应的分诊关系。");
  213. }
  214. ApiMessageBody messageBody = new ApiMessageBody(msg);
  215. messageBody.setSid(notifyRelation.getSocketSid());
  216. messageBody.setRoomCode(notifyRelation.getRoomCode());
  217. socketService.sendFloorTriageMessage(messageBody);
  218. dao.updateNotifyDatetime(param.getSerialNo());
  219. return socketService.sendRoomTriageMessage(messageBody);
  220. }
  221. private ResultVo<String> notifyVipReq(int serialNo) {
  222. MessageForPush vipRegMessage = dao.selectVipRegMessage(serialNo);
  223. String message = String.format("患者<span style=\"color:red;font-weight:bold\">%s</span>" +
  224. "预约了国际医疗部,预约就诊时间为<span style=\"font-weight:bold;color: red\">%s</span>",
  225. vipRegMessage.getName(), DateUtil.formatDatetime(vipRegMessage.getVisitDate()));
  226. JSONObject messageWrapper = new JSONObject();
  227. messageWrapper.put("name", "systemNotification");
  228. JSONObject messageBody = new JSONObject();
  229. messageBody.put("title", "提醒");
  230. messageBody.put("message", message);
  231. messageWrapper.put("message", messageBody);
  232. List<String> vipTriageUsers = dao.selectVipTriageUser();
  233. if (vipTriageUsers.isEmpty()) {
  234. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "没有国际医疗部的分诊人员。");
  235. }
  236. ApiMessageBody body = new ApiMessageBody(messageWrapper.toJSONString());
  237. for (String userCode : vipTriageUsers) {
  238. body.setUserCode(userCode);
  239. socketService.sendMessageByUserCode(body);
  240. }
  241. return ResultVoUtil.success();
  242. }
  243. }