陈昶聿
6 天以前 879bc177f31717f6d4a4998840e786a668256e97
smartor/src/main/java/com/smartor/service/impl/ServiceSLTDHealthcareRecordServiceImpl.java
@@ -111,28 +111,57 @@
    }
    @Override
    public Boolean queryMedicationItemList(ServiceSLTDInhospReqVO reqVO) {
    /**
     * 查询药品信息列表
     * @return 返回实际获取到的药品数据条数,如果出错则返回 null
     */ public Integer queryMedicationItemList(ServiceSLTDInhospReqVO reqVO) {
        try {
            Map<String, Object> requestParams = buildRequestParams(reqVO);
            Map<String, String> headers = buildRequestHeaders();
            log.info("请求参药品信息地址:{},appKey:{}", sltdPubPath + "/hbos-thirdparty-integration/standard/base/mc/service/medication/queryMedicationInfoPage", APP_KEY);
            String result = HttpUtils.sendPostByHeader(sltdPubPath + "/hbos-thirdparty-integration/standard/base/mc/service/medication/queryMedicationInfoPage", new Gson().toJson(requestParams), headers);
            List<Map<String, Object>> dataList = getDataList(result, 2);
            log.info("【queryMedicationItemList】共获取到{}条药品数据", dataList.size());
            int successCount = 0;
            int failCount = 0;
            for (Map<String, Object> dataItem : dataList) {
                MedicationItem medicationItem = convertToMedication(dataItem);
                log.info("medicationItem的值为:{}", medicationItem);
                medicationItemMapper.insertMedicationItem(medicationItem);
                List<MedicationDoseInfo> doseInfoList = medicationItem.getDoseInfoList();
                for (MedicationDoseInfo doseInfo : doseInfoList) {
                    doseInfo.setMedicationItemId(medicationItem.getId());
                    medicationDoseInfoMapper.insertMedicationDoseInfo(doseInfo);
                try {
                    MedicationItem medicationItem = convertToMedication(dataItem);
                    log.info("准备插入药品数据:itemName={}, itemCode={}", medicationItem.getItemName(), medicationItem.getItemCode());
                    medicationItem.setCreateTime(new Date());
                    medicationItemMapper.insertMedicationItem(medicationItem);
                    // 如果插入成功且返回了自增 ID,则插入子表
                    if (medicationItem.getId() != null) {
                        List<MedicationDoseInfo> doseInfoList = medicationItem.getDoseInfoList();
                        if (CollectionUtils.isNotEmpty(doseInfoList)) {
                            for (MedicationDoseInfo doseInfo : doseInfoList) {
                                try {
                                    doseInfo.setMedicationItemId(medicationItem.getId());
                                    medicationDoseInfoMapper.insertMedicationDoseInfo(doseInfo);
                                    log.debug("插入剂量信息成功:itemId={}, doseUnit={}", medicationItem.getId(), doseInfo.getDoseUnit());
                                } catch (Exception e) {
                                    log.warn("插入剂量信息失败:itemId={}, doseUnit={}, 错误:{}", medicationItem.getId(), doseInfo.getDoseUnit(), e.getMessage(), e);
                                    failCount++;
                                }
                            }
                        }
                    }
                    successCount++;
                    log.info("✓ 药品数据插入成功:{}", medicationItem.getItemName());
                } catch (Exception e) {
                    log.warn("【queryMedicationItemList】单条药品数据插入失败:itemName={}, itemCode={}, 错误:{}", dataItem.get("itemName"), dataItem.get("itemCode"), e.getMessage(), e);
                    failCount++;
                    // 继续处理下一条数据,不中断整个循环
                }
            }
            log.info("【queryMedicationItemList】批量插入完成,总数:{}, 成功:{}, 失败:{}", dataList.size(), successCount, failCount);
            // 返回实际获取到的药品数据条数,用于分页判断是否还有更多数据
            return dataList.size();
        } catch (Exception e) {
            log.error("【queryMedicationItemList】调用省立同德药品信息查询接口异常,请求参数:{}", reqVO, e);
            throw new RuntimeException(e);
        }
        return true;
    }
@@ -868,7 +897,160 @@
            }
        }
        if (active.equals("nhfy") || active.equals("sltd")) {
            Map<String, String> drugInfoMap = queryMedicalOrders(dto);
            if (drugInfoMap != null && !drugInfoMap.isEmpty()) {
                patMedInhosp.setMedicalName(drugInfoMap.get("drugItemNames"));
                patMedInhosp.setMedicalCode(drugInfoMap.get("drugItemIds"));
                patMedInhosp.setMedicalCompany(drugInfoMap.get("drugManufacturerNames"));
                log.info("获取药品信息成功:名称={}, 编码={}, 厂家={}", drugInfoMap.get("drugItemNames"), drugInfoMap.get("drugItemIds"), drugInfoMap.get("drugManufacturerNames"));
            }
        }
        return patMedInhosp;
    }
    /**
     * 查询医嘱信息并拼接药品信息
     *
     * @param dto 健康记录请求对象
     * @return 返回包含三个字段 Map:
     * - drugItemNames: 药品名称列表(逗号分隔)
     * - drugItemIds: 药品编码列表(逗号分隔)
     * - drugManufacturerNames: 药品厂家列表(逗号分隔)
     * <p>
     * 规则:
     * 1. 三个字段顺序位置保持一致
     * 2. 如果三个字段都为空,则跳过该记录
     * 3. 如果至少有一个字段有值,则其他两个为空用空字符串代替
     */
    private Map<String, String> queryMedicalOrders(ServiceSLTDInhospResDTO dto) {
        Map<String, String> result = new HashMap<>();
        List<String> itemNames = new ArrayList<>();
        List<String> itemIds = new ArrayList<>();
        List<String> manufacturerNames = new ArrayList<>();
        try {
            // 获取患者的医嘱信息
            Map<String, Object> medicineInfoReq = new HashMap<>();
            medicineInfoReq.put("orgId", dto.getOrgId());
            medicineInfoReq.put("patientId", dto.getPatientId());
            medicineInfoReq.put("campusId", dto.getCampusId());
            medicineInfoReq.put("medicalRecordNo", dto.getMedicalRecordNo());
            log.info("【queryMedicalOrders】请求参数:{}", medicineInfoReq);
            Map<String, String> headers = buildRequestHeaders();
            String resultStr = HttpUtils.sendPostByHeader(sltdPubPath + "/osj/hbos-thirdparty-integration/standard/common/doctorOrder/queryDoctorOrderList", new Gson().toJson(medicineInfoReq), headers);
            if (StringUtils.isEmpty(resultStr)) {
                log.warn("【queryMedicalOrders】接口返回为空,patientId={}", dto.getPatientId());
                return result;
            }
            // 解析返回数据
            Gson gson = new Gson();
            Type mapType = new TypeToken<Map<String, Object>>() {
            }.getType();
            Map<String, Object> responseMap = gson.fromJson(resultStr, mapType);
            // 检查返回码
            Object codeObj = responseMap.get("code");
            if (codeObj != null) {
                String code = BigDecimal.valueOf(((Number) codeObj).longValue()).toPlainString();
                if (!"200".equals(code)) {
                    log.error("【queryMedicalOrders】接口返回失败,code={}, message={}", code, responseMap.get("message"));
                    return result;
                }
            }
            // 获取 data 数组
            Object dataObj = responseMap.get("data");
            if (dataObj instanceof List) {
                List<?> dataList = (List<?>) dataObj;
                // 遍历医嘱列表,提取药品信息
                Set<String> processedItemIds = new HashSet<>(); // 用于去重(只根据 itemId 去重)
                for (Object item : dataList) {
                    if (item instanceof Map) {
                        Map<String, Object> itemMap = (Map<String, Object>) item;
                        // 只处理药品相关的医嘱(itemDomain 为 FH0134.01 表示药品)
                        Object itemDomainObj = itemMap.get("itemDomain");
                        if (itemDomainObj == null || !"FH0134.01".equals(itemDomainObj.toString())) {
                            continue;
                        }
                        // 获取三个字段
                        Object itemNameObj = itemMap.get("itemName");
                        Object itemIdObj = itemMap.get("itemId");
                        Object manufacturerNameObj = itemMap.get("manufacturerName");
                        // 判断是否都为空
                        boolean allEmpty = (itemNameObj == null || StringUtils.isEmpty(itemNameObj.toString())) && (itemIdObj == null || StringUtils.isEmpty(itemIdObj.toString())) && (manufacturerNameObj == null || StringUtils.isEmpty(manufacturerNameObj.toString()));
                        // 如果三个都为空,则跳过
                        if (allEmpty) {
                            continue;
                        }
                        // 至少有一个有值,处理每个字段
                        String itemName = (itemNameObj != null && StringUtils.isNotEmpty(itemNameObj.toString())) ? itemNameObj.toString() : "";
                        // itemId 特殊处理:避免科学计数法(Gson 可能将数字解析为 Double)
                        String itemId = "";
                        if (itemIdObj != null && StringUtils.isNotEmpty(itemIdObj.toString())) {
                            if (itemIdObj instanceof Number) {
                                // 如果是数值类型,转换为整数字符串,避免科学计数法
                                itemId = String.valueOf(((Number) itemIdObj).longValue());
                            } else {
                                itemId = itemIdObj.toString();
                            }
                        }
                        String manufacturerName = (manufacturerNameObj != null && StringUtils.isNotEmpty(manufacturerNameObj.toString())) ? manufacturerNameObj.toString() : "";
                        // 只根据 itemId 去重(同一编码的药品只保留一个)
                        if (StringUtils.isNotEmpty(itemId)) {
                            if (processedItemIds.contains(itemId)) {
                                log.info("【queryMedicalOrders】跳过重复药品(相同编码):名称={}, 编码={}", itemName, itemId);
                                continue;
                            }
                            processedItemIds.add(itemId);
                        } else {
                            // itemId 为空时,使用 itemName 作为去重依据
                            if (processedItemIds.contains(itemName)) {
                                log.info("【queryMedicalOrders】跳过重复药品(相同名称):名称={}", itemName);
                                continue;
                            }
                            processedItemIds.add(itemName);
                        }
                        // 添加到列表
                        itemNames.add(itemName);
                        itemIds.add(itemId);
                        manufacturerNames.add(manufacturerName);
                        log.info("【queryMedicalOrders】提取药品信息:名称={}, 编码={}, 厂家={}", itemName, itemId, manufacturerName);
                    }
                }
            } else {
                log.info("【queryMedicalOrders】data 不是数组类型,patientId={}", dto.getPatientId());
            }
            // 将列表拼接成逗号分隔的字符串
            if (!itemNames.isEmpty()) {
                result.put("drugItemNames", String.join(",", itemNames));
                result.put("drugItemIds", String.join(",", itemIds));
                result.put("drugManufacturerNames", String.join(",", manufacturerNames));
            }
            log.info("【queryMedicalOrders】成功提取{}条药品信息,patientId={}", itemNames.size(), dto.getPatientId());
        } catch (Exception e) {
            log.error("【queryMedicalOrders】查询医嘱信息失败,patientId={}", dto.getPatientId(), e);
        }
        return result;
    }
    private void setDischargeInfo(PatMedInhosp patMedInhosp, ServiceSLTDInhospResDTO dto) {
@@ -928,11 +1110,7 @@
                if (importanceFlagCodeListObj instanceof List) {
                    List<?> importanceFlagCodeList = (List<?>) importanceFlagCodeListObj;
                    // 过滤掉 null 值
                    List<String> nonNullList = importanceFlagCodeList.stream()
                            .filter(Objects::nonNull)
                            .map(Object::toString)
                            .filter(StringUtils::isNotEmpty)
                            .collect(Collectors.toList());
                    List<String> nonNullList = importanceFlagCodeList.stream().filter(Objects::nonNull).map(Object::toString).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
                    if (!nonNullList.isEmpty()) {
                        String importanceFlagLabel;