package thyyxxk.webserver.api.dpcc; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import thyyxxk.webserver.api.dpcc.model.MedicalCheckResult; import thyyxxk.webserver.api.dpcc.model.MedicalPrescription; import thyyxxk.webserver.api.dpcc.model.PatientInfo; import thyyxxk.webserver.api.medicallaboratory.model.Category; import thyyxxk.webserver.config.auth.PassToken; import thyyxxk.webserver.dao.his.api.DpccDao; import thyyxxk.webserver.entity.examinations.inspections.request.PatType; import thyyxxk.webserver.entity.examinations.inspections.request.ReportIndexInquiry; import thyyxxk.webserver.factory.examreport.ExamReportService; import thyyxxk.webserver.factory.examreport.model.*; import thyyxxk.webserver.service.TokenService; import thyyxxk.webserver.utils.DateUtil; import thyyxxk.webserver.utils.StringUtil; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; @Slf4j @RestController @RequestMapping("/api/dpcc") public class DpccController { private static final String TOKEN_URL = "https://manage.national-dpcc.com/api/sino-auth/oauth/token"; private static final String CHECK_URL = "https://manage.national-dpcc.com/api/sino-common/access-client/uploadHisCheckData"; private static final String DRUG_URL = "https://manage.national-dpcc.com/api/sino-common/access-client/uploadHisDrugData"; private final DpccDao dao; private final TokenService tokenService; @Autowired public DpccController(DpccDao dao, TokenService tokenService) { this.dao = dao; this.tokenService = tokenService; } @PassToken @GetMapping("/getToken") public String getToken() { String clientName = "sino-client-csthyy"; String clientSecret = "sino-client-csthyy"; HttpHeaders headers = new HttpHeaders(); headers.setBasicAuth(clientName, clientSecret); String url = TOKEN_URL + "?grant_type=access_client&" + "username=sino-client-csthyy&" + "password=d60dc4bf4acfa8956cd6819dba6966d0dab1ddac"; JSONObject response = new RestTemplate().postForObject(url, new HttpEntity<>(null, headers), JSONObject.class); log.info("DPCC获取TOKEN:{}", response); if (null == response) { return "ERROR:NETWORK_ERROR"; } Integer code = response.getInteger("code"); if (null == code) { return "ERROR:NETWORK_ERROR"; } if (code != 200) { return "ERROR:" + response.getString("msg"); } return response.getJSONObject("data").getString("access_token"); } @PassToken @GetMapping("/uploadHisData") public String uploadHisData(@RequestParam("token") String token) throws Exception { log.info("DPCC上传请求:{}", token); List inpatientList = dao.getInpatientList(); List outpatientList = dao.getOutpatientList(); if (inpatientList.isEmpty() && outpatientList.isEmpty()) { return "没有需要上传的数据。"; } ReportIndexInquiry inquiry = getReportIndexInquiry(); uploadInpatientCheck(inpatientList, inquiry, token); uploadOutpatientCheck(outpatientList, inquiry, token); uploadInpatientDrug(inpatientList, token); uploadOutpatientDrug(outpatientList, token); return "OK"; } @PassToken @GetMapping("/uploadMzDpcc") public String uploadMzYp(@RequestParam("pat") String pat, @RequestParam("day") Integer day) throws Exception { String token = tokenService.getDpccToken(); ReportIndexInquiry inquiry = getReportIndexInquiry(); log.info("DPCC上传请求:{}, {}", pat, day); List outpatientList; if (pat.contains("_")) { String[] patinfo = pat.split("_"); String patientId = patinfo[0]; int times = Integer.parseInt(patinfo[1]); String socialNo = dao.getSocialNo(patientId); PatientInfo pi = new PatientInfo(); pi.setPatNo(patientId); pi.setTimes(times); pi.setSource("1"); pi.setIdCard(socialNo); outpatientList = new ArrayList<>(); outpatientList.add(pi); Map map = dao.getStartAndEndDate(patientId, times); if (null != map) { inquiry.setReqStartTime(map.get("start")); inquiry.setReqEndTime(map.get("endd")); } } else { outpatientList = dao.getOutpatientList2(day); } if (outpatientList.isEmpty()) { return "没有需要上传的数据。"; } uploadOutpatientCheck(outpatientList,inquiry,token); uploadOutpatientDrug(outpatientList, token); return "OK"; } private ReportIndexInquiry getReportIndexInquiry() { String now = DateUtil.formatDate(new Date()); String start = now + " 00:00:00"; String end = now + " 23:59:59"; ReportIndexInquiry inquiry = new ReportIndexInquiry(); inquiry.setReqStartTime(start); inquiry.setReqEndTime(end); inquiry.setReportCategory(Category.JY); return inquiry; } private void uploadInpatientCheck(List inpatientList, ReportIndexInquiry inquiry, String token) throws Exception { inquiry.setPatType(PatType.InPatient); for (PatientInfo inpatient : inpatientList) { queryCheckAndUpload(inquiry, inpatient, token); } } private void uploadOutpatientCheck(List outpatientList, ReportIndexInquiry inquiry, String token) throws Exception { inquiry.setPatType(PatType.OutPatient); for (PatientInfo outpatient : outpatientList) { queryCheckAndUpload(inquiry, outpatient, token); } } private List getExamIndex(ReportIndexInquiry inquiry) { ExamIndexRequest request = new ExamIndexRequest.Builder() .patientNumType(inquiry.getPatType()).patientNum(inquiry.getPatNo()) .startDate(inquiry.getReqStartTime()).endDate(inquiry.getReqEndTime()).build(); ExamReportService examReportService = new ExamReportService(); return examReportService.queryExamIndex(request); } private ExamDetailResponse getExamDetail(String reportId) { ExamReportService examReportService = new ExamReportService(); return examReportService.queryExamDetail(reportId); } private boolean reportTimeValid(ExamDetailOrder order) { Date auditTime = DateUtil.parse(order.getAudtTime()); if (null == auditTime) { return false; } Date now = new Date(); long diff = now.getTime() - auditTime.getTime(); return diff / 1000 / 60 / 60 <= 72; } private void queryCheckAndUpload(ReportIndexInquiry inquiry, PatientInfo patient, String token) throws Exception { List checkResultBatches = new ArrayList<>(); inquiry.setPatNo(patient.getPatNo()); List indexList = getExamIndex(inquiry); for (ExamIndexResponse index : indexList) { ExamDetailResponse detail = getExamDetail(index.getReportId()); if (reportTimeValid(detail.getOrder())) { patient.setReportId(index.getReportId()); patient.setInspectTime(detail.getOrder().getInspectTime()); if (StringUtil.isBlank(patient.getInspectTime())) { patient.setInspectTime(detail.getOrder().getAudtTime()); } for (ExamDetailItem item : detail.getItems()) { if (StringUtil.isBlank(item.getItmUnit())) { continue; } if (StringUtil.isBlank(item.getItmValue()) && StringUtil.isBlank(item.getItmStrValue())) { continue; } checkResultBatches.add(getMedicalCheckResult(patient, item)); if (checkResultBatches.size() == 100) { executeUploadChecks(checkResultBatches, token, patient.getPatNo(), patient.getTimes()); } } } } if (!checkResultBatches.isEmpty()) { executeUploadChecks(checkResultBatches, token, patient.getPatNo(), patient.getTimes()); } } private MedicalCheckResult getMedicalCheckResult(PatientInfo patient, ExamDetailItem item) { MedicalCheckResult checkResult = new MedicalCheckResult(); checkResult.setSource(patient.getSource()); checkResult.setIdCard(patient.getIdCard()); checkResult.setPatientNo(patient.getPatNo()); checkResult.setCheckNo(patient.getReportId()); checkResult.setTestTime(patient.getInspectTime()); checkResult.setHisItemCode(item.getItmCode()); checkResult.setHisItemName(item.getItmName()); checkResult.setHisItemResult(item.getItmValue()); if (StringUtil.isBlank(checkResult.getHisItemResult())) { checkResult.setHisItemResult(item.getItmStrValue()); } checkResult.setHisItemUnit(item.getItmUnit()); checkResult.setUniqueIndex(patient.getReportId() + "_" + item.getItmId()); return checkResult; } private void uploadInpatientDrug(List inpatientList, String token) throws Exception { List drugBatches = new ArrayList<>(); for (PatientInfo inpatient : inpatientList) { String patNo = inpatient.getPatNo(); List drugs = dao.getInpatientDrugs(inpatient); for (MedicalPrescription item : drugs) { item.setIdCard(inpatient.getIdCard()); drugBatches.add(item); if (drugBatches.size() == 100) { executeUploadDrugs(drugBatches, token, patNo, -1); drugBatches.clear(); } } if (!drugBatches.isEmpty()) { executeUploadDrugs(drugBatches, token, patNo, -1); } } } private void uploadOutpatientDrug(List outpatientList, String token) throws Exception { for (PatientInfo outpatient : outpatientList) { String patNo = outpatient.getPatNo(); int times = outpatient.getTimes(); List drugBatches = new ArrayList<>(); List drugs = dao.getOutpatientDrugs(outpatient); for (MedicalPrescription item : drugs) { item.setIdCard(outpatient.getIdCard()); drugBatches.add(item); if (drugBatches.size() == 100) { executeUploadDrugs(drugBatches, token, patNo, times); drugBatches.clear(); } } if (!drugBatches.isEmpty()) { executeUploadDrugs(drugBatches, token, patNo, times); } } } private void executeUploadChecks(List checkBatches, String token, String patNo, int times) throws Exception { String json = JSON.toJSONString(checkBatches); log.info("DPCC上传检验:{}, {}", patNo, times); uploadToSino(DpccEncryptUtil.encrypt(json), Type.CHECK, token); } private void executeUploadDrugs(List drugBatches, String token, String patNo, int times) throws Exception { String json = JSON.toJSONString(drugBatches); log.info("DPCC上传药品:{}, {}", patNo, times); int res = uploadToSino(DpccEncryptUtil.encrypt(json), Type.DRUG, token); if (res == 200 && times != -1) { dao.updateDpccFlag(patNo, times); } } private int uploadToSino(String encrypt, Type type, String token) { String url = type == Type.CHECK ? CHECK_URL : DRUG_URL; String label = type == Type.CHECK ? "检验" : "药品"; HttpHeaders headers = new HttpHeaders(); headers.add("Sino-auth", token); JSONObject data = new JSONObject(); data.put("data", encrypt); HttpEntity body = new HttpEntity<>(data, headers); String response = new RestTemplate().postForObject(url, body, String.class); log.info("DPCC上传{}返回:{}", label, response); if (StringUtil.notBlank(response)) { JSONObject json = JSON.parseObject(response); Integer code = json.getInteger("code"); return null == code ? -1 : code; } return -1; } enum Type { CHECK, DRUG, } }