DpccController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package thyyxxk.webserver.api.dpcc;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.http.HttpEntity;
  7. import org.springframework.http.HttpHeaders;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RequestParam;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import org.springframework.web.client.RestTemplate;
  13. import thyyxxk.webserver.api.dpcc.model.MedicalCheckResult;
  14. import thyyxxk.webserver.api.dpcc.model.MedicalPrescription;
  15. import thyyxxk.webserver.api.dpcc.model.PatientInfo;
  16. import thyyxxk.webserver.api.medicallaboratory.model.Category;
  17. import thyyxxk.webserver.config.auth.PassToken;
  18. import thyyxxk.webserver.dao.his.api.DpccDao;
  19. import thyyxxk.webserver.entity.examinations.inspections.request.PatType;
  20. import thyyxxk.webserver.entity.examinations.inspections.request.ReportIndexInquiry;
  21. import thyyxxk.webserver.factory.examreport.ExamReportService;
  22. import thyyxxk.webserver.factory.examreport.model.*;
  23. import thyyxxk.webserver.service.TokenService;
  24. import thyyxxk.webserver.utils.DateUtil;
  25. import thyyxxk.webserver.utils.StringUtil;
  26. import java.util.ArrayList;
  27. import java.util.Date;
  28. import java.util.List;
  29. import java.util.Map;
  30. @Slf4j
  31. @RestController
  32. @RequestMapping("/api/dpcc")
  33. public class DpccController {
  34. private static final String TOKEN_URL = "https://manage.national-dpcc.com/api/sino-auth/oauth/token";
  35. private static final String CHECK_URL = "https://manage.national-dpcc.com/api/sino-common/access-client/uploadHisCheckData";
  36. private static final String DRUG_URL = "https://manage.national-dpcc.com/api/sino-common/access-client/uploadHisDrugData";
  37. private final DpccDao dao;
  38. private final TokenService tokenService;
  39. @Autowired
  40. public DpccController(DpccDao dao, TokenService tokenService) {
  41. this.dao = dao;
  42. this.tokenService = tokenService;
  43. }
  44. @PassToken
  45. @GetMapping("/getToken")
  46. public String getToken() {
  47. String clientName = "sino-client-csthyy";
  48. String clientSecret = "sino-client-csthyy";
  49. HttpHeaders headers = new HttpHeaders();
  50. headers.setBasicAuth(clientName, clientSecret);
  51. String url = TOKEN_URL + "?grant_type=access_client&" +
  52. "username=sino-client-csthyy&" +
  53. "password=d60dc4bf4acfa8956cd6819dba6966d0dab1ddac";
  54. JSONObject response = new RestTemplate().postForObject(url,
  55. new HttpEntity<>(null, headers), JSONObject.class);
  56. log.info("DPCC获取TOKEN:{}", response);
  57. if (null == response) {
  58. return "ERROR:NETWORK_ERROR";
  59. }
  60. Integer code = response.getInteger("code");
  61. if (null == code) {
  62. return "ERROR:NETWORK_ERROR";
  63. }
  64. if (code != 200) {
  65. return "ERROR:" + response.getString("msg");
  66. }
  67. return response.getJSONObject("data").getString("access_token");
  68. }
  69. @PassToken
  70. @GetMapping("/uploadHisData")
  71. public String uploadHisData(@RequestParam("token") String token) throws Exception {
  72. log.info("DPCC上传请求:{}", token);
  73. List<PatientInfo> inpatientList = dao.getInpatientList();
  74. List<PatientInfo> outpatientList = dao.getOutpatientList();
  75. if (inpatientList.isEmpty() && outpatientList.isEmpty()) {
  76. return "没有需要上传的数据。";
  77. }
  78. ReportIndexInquiry inquiry = getReportIndexInquiry();
  79. uploadInpatientCheck(inpatientList, inquiry, token);
  80. uploadOutpatientCheck(outpatientList, inquiry, token);
  81. uploadInpatientDrug(inpatientList, token);
  82. uploadOutpatientDrug(outpatientList, token);
  83. return "OK";
  84. }
  85. @PassToken
  86. @GetMapping("/uploadMzDpcc")
  87. public String uploadMzYp(@RequestParam("pat") String pat,
  88. @RequestParam("day") Integer day) throws Exception {
  89. String token = tokenService.getDpccToken();
  90. ReportIndexInquiry inquiry = getReportIndexInquiry();
  91. log.info("DPCC上传请求:{}, {}", pat, day);
  92. List<PatientInfo> outpatientList;
  93. if (pat.contains("_")) {
  94. String[] patinfo = pat.split("_");
  95. String patientId = patinfo[0];
  96. int times = Integer.parseInt(patinfo[1]);
  97. String socialNo = dao.getSocialNo(patientId);
  98. PatientInfo pi = new PatientInfo();
  99. pi.setPatNo(patientId);
  100. pi.setTimes(times);
  101. pi.setSource("1");
  102. pi.setIdCard(socialNo);
  103. outpatientList = new ArrayList<>();
  104. outpatientList.add(pi);
  105. Map<String, String> map = dao.getStartAndEndDate(patientId, times);
  106. if (null != map) {
  107. inquiry.setReqStartTime(map.get("start"));
  108. inquiry.setReqEndTime(map.get("endd"));
  109. }
  110. } else {
  111. outpatientList = dao.getOutpatientList2(day);
  112. }
  113. if (outpatientList.isEmpty()) {
  114. return "没有需要上传的数据。";
  115. }
  116. uploadOutpatientCheck(outpatientList,inquiry,token);
  117. uploadOutpatientDrug(outpatientList, token);
  118. return "OK";
  119. }
  120. private ReportIndexInquiry getReportIndexInquiry() {
  121. String now = DateUtil.formatDate(new Date());
  122. String start = now + " 00:00:00";
  123. String end = now + " 23:59:59";
  124. ReportIndexInquiry inquiry = new ReportIndexInquiry();
  125. inquiry.setReqStartTime(start);
  126. inquiry.setReqEndTime(end);
  127. inquiry.setReportCategory(Category.JY);
  128. return inquiry;
  129. }
  130. private void uploadInpatientCheck(List<PatientInfo> inpatientList, ReportIndexInquiry inquiry, String token) throws Exception {
  131. inquiry.setPatType(PatType.InPatient);
  132. for (PatientInfo inpatient : inpatientList) {
  133. queryCheckAndUpload(inquiry, inpatient, token);
  134. }
  135. }
  136. private void uploadOutpatientCheck(List<PatientInfo> outpatientList, ReportIndexInquiry inquiry, String token) throws Exception {
  137. inquiry.setPatType(PatType.OutPatient);
  138. for (PatientInfo outpatient : outpatientList) {
  139. queryCheckAndUpload(inquiry, outpatient, token);
  140. }
  141. }
  142. private List<ExamIndexResponse> getExamIndex(ReportIndexInquiry inquiry) {
  143. ExamIndexRequest request = new ExamIndexRequest.Builder()
  144. .patientNumType(inquiry.getPatType()).patientNum(inquiry.getPatNo())
  145. .startDate(inquiry.getReqStartTime()).endDate(inquiry.getReqEndTime()).build();
  146. ExamReportService examReportService = new ExamReportService();
  147. return examReportService.queryExamIndex(request);
  148. }
  149. private ExamDetailResponse getExamDetail(String reportId) {
  150. ExamReportService examReportService = new ExamReportService();
  151. return examReportService.queryExamDetail(reportId);
  152. }
  153. private boolean reportTimeValid(ExamDetailOrder order) {
  154. Date auditTime = DateUtil.parse(order.getAudtTime());
  155. if (null == auditTime) {
  156. return false;
  157. }
  158. Date now = new Date();
  159. long diff = now.getTime() - auditTime.getTime();
  160. return diff / 1000 / 60 / 60 <= 72;
  161. }
  162. private void queryCheckAndUpload(ReportIndexInquiry inquiry, PatientInfo patient, String token) throws Exception {
  163. List<MedicalCheckResult> checkResultBatches = new ArrayList<>();
  164. inquiry.setPatNo(patient.getPatNo());
  165. List<ExamIndexResponse> indexList = getExamIndex(inquiry);
  166. for (ExamIndexResponse index : indexList) {
  167. ExamDetailResponse detail = getExamDetail(index.getReportId());
  168. if (reportTimeValid(detail.getOrder())) {
  169. patient.setReportId(index.getReportId());
  170. patient.setInspectTime(detail.getOrder().getInspectTime());
  171. if (StringUtil.isBlank(patient.getInspectTime())) {
  172. patient.setInspectTime(detail.getOrder().getAudtTime());
  173. }
  174. for (ExamDetailItem item : detail.getItems()) {
  175. if (StringUtil.isBlank(item.getItmUnit())) {
  176. continue;
  177. }
  178. if (StringUtil.isBlank(item.getItmValue()) && StringUtil.isBlank(item.getItmStrValue())) {
  179. continue;
  180. }
  181. checkResultBatches.add(getMedicalCheckResult(patient, item));
  182. if (checkResultBatches.size() == 100) {
  183. executeUploadChecks(checkResultBatches, token, patient.getPatNo(), patient.getTimes());
  184. }
  185. }
  186. }
  187. }
  188. if (!checkResultBatches.isEmpty()) {
  189. executeUploadChecks(checkResultBatches, token, patient.getPatNo(), patient.getTimes());
  190. }
  191. }
  192. private MedicalCheckResult getMedicalCheckResult(PatientInfo patient, ExamDetailItem item) {
  193. MedicalCheckResult checkResult = new MedicalCheckResult();
  194. checkResult.setSource(patient.getSource());
  195. checkResult.setIdCard(patient.getIdCard());
  196. checkResult.setPatientNo(patient.getPatNo());
  197. checkResult.setCheckNo(patient.getReportId());
  198. checkResult.setTestTime(patient.getInspectTime());
  199. checkResult.setHisItemCode(item.getItmCode());
  200. checkResult.setHisItemName(item.getItmName());
  201. checkResult.setHisItemResult(item.getItmValue());
  202. if (StringUtil.isBlank(checkResult.getHisItemResult())) {
  203. checkResult.setHisItemResult(item.getItmStrValue());
  204. }
  205. checkResult.setHisItemUnit(item.getItmUnit());
  206. checkResult.setUniqueIndex(patient.getReportId() + "_" + item.getItmId());
  207. return checkResult;
  208. }
  209. private void uploadInpatientDrug(List<PatientInfo> inpatientList, String token) throws Exception {
  210. List<MedicalPrescription> drugBatches = new ArrayList<>();
  211. for (PatientInfo inpatient : inpatientList) {
  212. String patNo = inpatient.getPatNo();
  213. List<MedicalPrescription> drugs = dao.getInpatientDrugs(inpatient);
  214. for (MedicalPrescription item : drugs) {
  215. item.setIdCard(inpatient.getIdCard());
  216. drugBatches.add(item);
  217. if (drugBatches.size() == 100) {
  218. executeUploadDrugs(drugBatches, token, patNo, -1);
  219. drugBatches.clear();
  220. }
  221. }
  222. if (!drugBatches.isEmpty()) {
  223. executeUploadDrugs(drugBatches, token, patNo, -1);
  224. }
  225. }
  226. }
  227. private void uploadOutpatientDrug(List<PatientInfo> outpatientList, String token) throws Exception {
  228. for (PatientInfo outpatient : outpatientList) {
  229. String patNo = outpatient.getPatNo();
  230. int times = outpatient.getTimes();
  231. List<MedicalPrescription> drugBatches = new ArrayList<>();
  232. List<MedicalPrescription> drugs = dao.getOutpatientDrugs(outpatient);
  233. for (MedicalPrescription item : drugs) {
  234. item.setIdCard(outpatient.getIdCard());
  235. drugBatches.add(item);
  236. if (drugBatches.size() == 100) {
  237. executeUploadDrugs(drugBatches, token, patNo, times);
  238. drugBatches.clear();
  239. }
  240. }
  241. if (!drugBatches.isEmpty()) {
  242. executeUploadDrugs(drugBatches, token, patNo, times);
  243. }
  244. }
  245. }
  246. private void executeUploadChecks(List<MedicalCheckResult> checkBatches,
  247. String token, String patNo, int times) throws Exception {
  248. String json = JSON.toJSONString(checkBatches);
  249. log.info("DPCC上传检验:{}, {}", patNo, times);
  250. uploadToSino(DpccEncryptUtil.encrypt(json), Type.CHECK, token);
  251. }
  252. private void executeUploadDrugs(List<MedicalPrescription> drugBatches,
  253. String token, String patNo, int times) throws Exception {
  254. String json = JSON.toJSONString(drugBatches);
  255. log.info("DPCC上传药品:{}, {}", patNo, times);
  256. int res = uploadToSino(DpccEncryptUtil.encrypt(json), Type.DRUG, token);
  257. if (res == 200 && times != -1) {
  258. dao.updateDpccFlag(patNo, times);
  259. }
  260. }
  261. private int uploadToSino(String encrypt, Type type, String token) {
  262. String url = type == Type.CHECK ? CHECK_URL : DRUG_URL;
  263. String label = type == Type.CHECK ? "检验" : "药品";
  264. HttpHeaders headers = new HttpHeaders();
  265. headers.add("Sino-auth", token);
  266. JSONObject data = new JSONObject();
  267. data.put("data", encrypt);
  268. HttpEntity<JSONObject> body = new HttpEntity<>(data, headers);
  269. String response = new RestTemplate().postForObject(url, body, String.class);
  270. log.info("DPCC上传{}返回:{}", label, response);
  271. if (StringUtil.notBlank(response)) {
  272. JSONObject json = JSON.parseObject(response);
  273. Integer code = json.getInteger("code");
  274. return null == code ? -1 : code;
  275. }
  276. return -1;
  277. }
  278. enum Type {
  279. CHECK,
  280. DRUG,
  281. }
  282. }