AppointmentService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. package thyyxxk.wxservice_server.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.web.client.RestTemplate;
  8. import thyyxxk.wxservice_server.config.exception.ExceptionEnum;
  9. import thyyxxk.wxservice_server.constant.Constants;
  10. import thyyxxk.wxservice_server.dao.AppointmentDao;
  11. import thyyxxk.wxservice_server.entity.PureCodeName;
  12. import thyyxxk.wxservice_server.entity.ResultVo;
  13. import thyyxxk.wxservice_server.entity.appointment.*;
  14. import thyyxxk.wxservice_server.entity.assessment.CovidQuestionnaire;
  15. import thyyxxk.wxservice_server.entity.hrgresponse.SourcesResponse;
  16. import thyyxxk.wxservice_server.entity.hrgresponse.HrgCommonResponse;
  17. import thyyxxk.wxservice_server.entity.hrgresponse.MzClassResponse;
  18. import thyyxxk.wxservice_server.utils.*;
  19. import java.text.SimpleDateFormat;
  20. import java.util.*;
  21. /**
  22. * @author dj
  23. */
  24. @Slf4j
  25. @Service
  26. public class AppointmentService {
  27. private static List<MzClass> mzClasses;
  28. private final AppointmentDao dao;
  29. @Value("${hrgApiUrl}")
  30. private String hrgApiUrl;
  31. @Autowired
  32. public AppointmentService(AppointmentDao dao) {
  33. this.dao = dao;
  34. }
  35. public ResultVo<List<MzClass>> getAllDepartments() {
  36. if (null != mzClasses && !mzClasses.isEmpty()) {
  37. return ResultVoUtil.success(mzClasses);
  38. }
  39. log.info("重新获取门诊科室并缓存。");
  40. RestTemplate template = new RestTemplate();
  41. MzClassResponse response = template.getForObject(hrgApiUrl + "/getMzClass", MzClassResponse.class);
  42. if (null == response || -1 == response.getResultCode()) {
  43. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "获取科室分类失败,请联系服务中心。");
  44. }
  45. mzClasses = new ArrayList<>();
  46. JSONObject param = new JSONObject();
  47. for (Map<String, Object> item : response.getData()) {
  48. MzClass pojo = new MzClass();
  49. pojo.setId(item.get("code").toString());
  50. pojo.setText(item.get("name").toString());
  51. param.put("mzClass", pojo.getId());
  52. MzClassResponse response2 = template.postForObject(hrgApiUrl + "/getUnitCodeByMzClass", param, MzClassResponse.class);
  53. if (null != response2 && response2.getResultCode() == 0) {
  54. List<MzDept> children = new ArrayList<>();
  55. for (Map<String, Object> child : response2.getData()) {
  56. MzDept dept = new MzDept();
  57. dept.setId(child.get("code").toString());
  58. dept.setText(child.get("name").toString());
  59. List<Map<String, Object>> grandChildrenMap = CastUtil.cast(child.get("children"));
  60. if (null != grandChildrenMap && grandChildrenMap.size() > 0) {
  61. List<MzDept> grandChildren = new ArrayList<>();
  62. for (Map<String, Object> temp : grandChildrenMap) {
  63. MzDept mzDept = new MzDept();
  64. mzDept.setId(temp.get("code").toString());
  65. mzDept.setText(temp.get("name").toString());
  66. grandChildren.add(mzDept);
  67. }
  68. dept.setChildren(grandChildren);
  69. }
  70. children.add(dept);
  71. }
  72. pojo.setChildren(children);
  73. mzClasses.add(pojo);
  74. }
  75. }
  76. return ResultVoUtil.success(mzClasses);
  77. }
  78. public ResultVo<String> refreshMzClasses() {
  79. log.info("门诊科室缓存已重置。");
  80. mzClasses = null;
  81. return ResultVoUtil.success("门诊科室缓存已重置。");
  82. }
  83. public ResultVo<Integer[]> getSourcesByDate(GetSourcesByDateParam param) {
  84. Integer[] retArr = new Integer[7];
  85. RestTemplate restTemplate = new RestTemplate();
  86. Calendar calendar = Calendar.getInstance();
  87. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  88. for (int i = 0; i < 7; i++) {
  89. retArr[i] = 0;
  90. int addNum = i == 0 ? 0 : 1;
  91. calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + addNum);
  92. String date = format.format(calendar.getTime());
  93. String url = String.format(hrgApiUrl + "/getDoctorByDateAndDept?requestDay=%s&unitCode=%s", date, param.getDept());
  94. SourcesResponse hrgResponse = restTemplate.getForObject(url, SourcesResponse.class);
  95. if (null == hrgResponse || hrgResponse.getResultCode() == -1) {
  96. retArr[i] = 0;
  97. continue;
  98. }
  99. for (Map<String, Object> item : hrgResponse.getData()) {
  100. if ((int) item.get("leftNum") > 0) {
  101. retArr[i] = 1;
  102. break;
  103. }
  104. }
  105. }
  106. return ResultVoUtil.success(retArr);
  107. }
  108. public ResultVo<Object> getDoctorSources(GetDoctorSourcesParam param) {
  109. String url = String.format(hrgApiUrl + "/getDoctorByDateAndDept?requestDay=%s&unitCode=%s", param.getDate(), param.getDeptCode());
  110. RestTemplate template = new RestTemplate();
  111. SourcesResponse data = template.getForObject(url, SourcesResponse.class);
  112. if (null == data || null == data.getResultCode()) {
  113. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  114. }
  115. if (data.getResultCode() == -1) {
  116. return ResultVoUtil.fail(ExceptionEnum.SLIGHTLY_ERROR, data.getResultMessage());
  117. }
  118. data.getData().removeIf(map -> (int) map.get("leftNum") == 0);
  119. if (data.getData().size() == 0) {
  120. return ResultVoUtil.fail(ExceptionEnum.SLIGHTLY_ERROR);
  121. }
  122. for (Map<String, Object> map : data.getData()) {
  123. PureCodeName info = dao.selectPortraitAndIntroduction(map.get("doctorCode").toString());
  124. if (null != info) {
  125. map.put("portrait", info.getCode());
  126. map.put("introduction", info.getName());
  127. }
  128. }
  129. data.getData().sort((o1, o2) -> {
  130. Integer i1 = Integer.parseInt(o1.get("empTitCode").toString());
  131. Integer i2 = Integer.parseInt(o2.get("empTitCode").toString());
  132. int diff = i1 - i2;
  133. return Integer.compare(diff, 0);
  134. });
  135. return ResultVoUtil.success(data.getData());
  136. }
  137. public ResultVo<Object> getDoctorArrangement(GetDoctorSourcesParam param) {
  138. String url = String.format(hrgApiUrl + "/getRequestByDateAndDeptAndDoctor?requestDay=%s&unitCode=%s&doctorCode=%s",
  139. param.getDate(), param.getDeptCode(), param.getDoctorCode());
  140. RestTemplate template = new RestTemplate();
  141. SourcesResponse data = template.getForObject(url, SourcesResponse.class);
  142. if (null == data || null == data.getResultCode()) {
  143. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  144. }
  145. if (data.getResultCode() == -1) {
  146. return ResultVoUtil.fail(ExceptionEnum.SLIGHTLY_ERROR, data.getResultMessage());
  147. }
  148. data.getData().removeIf(map -> (int) map.get("leftNum") == 0);
  149. if (data.getData().size() == 0) {
  150. return ResultVoUtil.fail(ExceptionEnum.SLIGHTLY_ERROR);
  151. }
  152. return ResultVoUtil.success(data.getData());
  153. }
  154. public ResultVo<Integer[]> getSourcesByDateAndDoctor(GetDoctorSourcesParam param) {
  155. Integer[] source = new Integer[7];
  156. source[0] = getDoctorArrangement(param).getCode();
  157. Calendar calendar = Calendar.getInstance();
  158. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
  159. for (int i = 1; i < source.length; i++) {
  160. calendar.add(Calendar.DATE, 1);
  161. param.setDate(dateFormat.format(calendar.getTime()));
  162. source[i] = getDoctorArrangement(param).getCode();
  163. }
  164. return ResultVoUtil.success(source);
  165. }
  166. public ResultVo<Map<String, Object>> getGhFee(GetGhFeeParam param) {
  167. String url = String.format(hrgApiUrl + "/getMzChargeTypeByRequestIdForHaiCi?mzyRequestId=%d&patientId=%s",
  168. param.getMzyRequestId(), param.getPatientId());
  169. RestTemplate template = new RestTemplate();
  170. HrgCommonResponse data = template.getForObject(url, HrgCommonResponse.class);
  171. log.info("获取挂号费用:\n参数:{},\n结果:{}", param, data);
  172. if (null == data) {
  173. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "网络服务错误!");
  174. }
  175. if (-1 == data.getCode()) {
  176. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, data.getMessage());
  177. }
  178. Map<String, Object> map = new HashMap<>(Constants.Capacity.TWO);
  179. map.put("fee", data.getData());
  180. map.put("message", data.getMessage());
  181. return ResultVoUtil.success(map);
  182. }
  183. public ResultVo<DoctorInfo> getDoctorInfo(String doctorCode, String openId) {
  184. DoctorInfo doctor = dao.selectDoctorInfo(doctorCode, openId);
  185. if (null == doctor) {
  186. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "未找到医生信息!");
  187. }
  188. return ResultVoUtil.success(doctor);
  189. }
  190. public ResultVo<String> hasValidCovidAssessment(String patientId, String deptCode) {
  191. PureCodeName sexSocial = dao.selectSexAndSocialNo(patientId);
  192. String mzClass = dao.selectMzClass(deptCode);
  193. if (Constants.Sex.MALE.equals(sexSocial.getCode()) && Constants.MzClass.GYNAECOLOGY.equals(mzClass)) {
  194. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "男性无法在妇产科挂号,请选择其他科室。");
  195. }
  196. int age = IdCardUtil.calAgeBySocialNo(sexSocial.getName());
  197. if (age == -1) {
  198. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "您在我院绑定的身份证号不是有效的身份证号," +
  199. "请前往【个人中心 - 我的就诊人 - 就诊人信息】进行修改。");
  200. }
  201. if (Constants.MzDept.PEDIATRICS.equals(deptCode) && age >= 18) {
  202. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "成年人无法在儿科挂号,请选择其他科室。");
  203. }
  204. CovidQuestionnaire covid = dao.validCovidAssessment(patientId);
  205. if (null == covid) {
  206. return ResultVoUtil.success("no valid assessment");
  207. } else {
  208. if (covid.getTemperature() == 2 ||
  209. covid.getItem1() != 14 ||
  210. covid.getItem2() != 24 ||
  211. covid.getItem3() != 32 ||
  212. covid.getItem4() != 42) {
  213. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "您新型冠状病毒感染流行病学史问卷未通过,请挂发热门诊。");
  214. } else {
  215. return ResultVoUtil.success("valid assessment");
  216. }
  217. }
  218. }
  219. public ResultVo<String> getDoctorQrCode(String doctorCode) {
  220. final String token = PropertiesUtil.getProperty("qywxToken");
  221. final String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=" + token +
  222. "&userid=" + dao.getCodeRsByCode(doctorCode);
  223. RestTemplate restTemplate = new RestTemplate();
  224. final String result = restTemplate.getForObject(url, String.class);
  225. JSONObject json = JSONObject.parseObject(result);
  226. return ResultVoUtil.success(json.getString("qr_code"));
  227. }
  228. public ResultVo<List<Map<String, String>>> getPaidMzGhList(String patientId) {
  229. log.info("获取门诊挂号记录列表:{}", patientId);
  230. RestTemplate template = new RestTemplate();
  231. JSONObject obj = new JSONObject();
  232. obj.put("patientId", patientId);
  233. SourcesResponse hrgResponse = template.postForObject(hrgApiUrl + "/getRegistrationForPaid", obj, SourcesResponse.class);
  234. return ThmzUtil.getResultVoCompletableFuture(hrgResponse);
  235. }
  236. }