From 2867cf7076a584a34e63174cec0a8b89cdd03e4c Mon Sep 17 00:00:00 2001
From: 陈昶聿 <chychen@nbjetron.com>
Date: 星期五, 06 十一月 2026 13:42:55 +0800
Subject: [PATCH] Merge branch 'master' into smartor_ls_261103

---
 smartor/src/main/java/com/smartor/service/impl/PatSpecificArchiveGenerateServiceImpl.java |  329 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 329 insertions(+), 0 deletions(-)

diff --git a/smartor/src/main/java/com/smartor/service/impl/PatSpecificArchiveGenerateServiceImpl.java b/smartor/src/main/java/com/smartor/service/impl/PatSpecificArchiveGenerateServiceImpl.java
new file mode 100644
index 0000000..91effdb
--- /dev/null
+++ b/smartor/src/main/java/com/smartor/service/impl/PatSpecificArchiveGenerateServiceImpl.java
@@ -0,0 +1,329 @@
+package com.smartor.service.impl;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.ruoyi.common.core.domain.entity.PatArchive;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.outpatientvisit.domain.PatMedOuthosp;
+import com.ruoyi.patarchive.domain.PatSpecificArchiveGenerateConfig;
+import com.ruoyi.patarchive.mapper.PatArchiveMapper;
+import com.ruoyi.patarchive.service.IPatSpecificArchiveGenerateConfigService;
+import com.smartor.common.SpecificArchiveConstants;
+import com.smartor.domain.PatChildArchiveDetail;
+import com.smartor.domain.PatMaternityArchiveDetail;
+import com.smartor.service.IPatChildArchiveService;
+import com.smartor.service.IPatMaternityArchiveService;
+import com.smartor.service.IPatSpecificArchiveGenerateService;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.collections4.CollectionUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * 涓撶妗f鐢熸垚Service涓氬姟灞傚鐞嗐��
+ *
+ * <p>鍙鐞嗕附姘村競濡囧辜淇濆仴闄紙orgid = 47226079133110211G1001锛夌殑闂ㄨ瘖鎺ヨ瘖瀹屾垚璁板綍锛�
+ * 鎸� {@code pat_specific_archive_generate_config} 閰嶇疆鍖归厤绉戝鍜岃瘖鏂悗锛�
+ * 鐢熸垚瀵瑰簲绫诲瀷鐨勫绉戞。妗堟垨鍎跨妗f銆�</p>
+ */
+@Slf4j
+@Service
+public class PatSpecificArchiveGenerateServiceImpl implements IPatSpecificArchiveGenerateService {
+
+    /** 閮ㄩ棬鍒嗙被锛氬绉� */
+    private static final String DEPT_CATEGORY_GYNECOLOGY = "1";
+    /** 閮ㄩ棬鍒嗙被锛氬効绉� */
+    private static final String DEPT_CATEGORY_PEDIATRICS = "2";
+    /** 閮ㄩ棬绫诲瀷锛氱瀹� */
+    private static final String DEPT_TYPE_DEPARTMENT = "1";
+    /** 鏉ユ簮绫诲瀷锛氶棬璇婅褰� */
+    private static final String SOURCE_TYPE_OUTHOSP = "1";
+
+    @Autowired
+    private IPatSpecificArchiveGenerateConfigService patSpecificArchiveGenerateConfigService;
+
+    @Autowired
+    private IPatMaternityArchiveService patMaternityArchiveService;
+
+    @Autowired
+    private IPatChildArchiveService patChildArchiveService;
+
+    @Autowired
+    private PatArchiveMapper patArchiveMapper;
+
+    @Override
+    public int generateByOuthosp(PatMedOuthosp patMedOuthosp, PatArchive patArchive) {
+        if (patMedOuthosp == null) {
+            return 0;
+        }
+        // 浠呬附姘村競濡囧辜淇濆仴闄㈢敓鏁堬紝閬垮厤褰卞搷鍏朵粬鏈烘瀯
+        if (!SpecificArchiveConstants.LISHUI_MCH_ORG_ID.equals(patMedOuthosp.getOrgid())) {
+            return 0;
+        }
+        if (StringUtils.isBlank(patMedOuthosp.getOuthospno())) {
+            log.warn("鐢熸垚涓撶妗f璺宠繃锛氶棬璇婄紪鍙蜂负绌猴紝serialnum={}", patMedOuthosp.getSerialnum());
+            return 0;
+        }
+
+        PatArchive targetArchive = resolvePatArchive(patMedOuthosp, patArchive);
+        if (targetArchive == null && patMedOuthosp.getPatid() != null) {
+            targetArchive = patArchiveMapper.selectPatArchiveByPatid(patMedOuthosp.getPatid());
+        }
+        Long patid = targetArchive != null ? targetArchive.getId() : patMedOuthosp.getPatid();
+        if (patid == null) {
+            log.warn("鐢熸垚涓撶妗f璺宠繃锛氭湭鎵惧埌鎮h�呮。妗堬紝outhospno={}锛宲atno={}",
+                    patMedOuthosp.getOuthospno(), patMedOuthosp.getPatno());
+            return 0;
+        }
+
+        String campusid = StringUtils.isNotBlank(patMedOuthosp.getCampusid())
+                ? patMedOuthosp.getCampusid() : "1";
+        List<PatSpecificArchiveGenerateConfig> configs =
+                patSpecificArchiveGenerateConfigService.selectEffectiveConfigList(patMedOuthosp.getOrgid(), campusid);
+        if (CollectionUtils.isEmpty(configs)) {
+            log.debug("鐢熸垚涓撶妗f璺宠繃锛氭満鏋勬湭閰嶇疆妗f鐢熸垚瑙勫垯锛宱rgid={}锛宑ampusid={}",
+                    patMedOuthosp.getOrgid(), campusid);
+            return 0;
+        }
+
+        // 鍚屼竴鏉¢棬璇婅褰曘�佸悓涓�閮ㄩ棬鍒嗙被銆佸悓涓�妗f绫诲瀷鍙敓鎴愪竴娆�
+        Set<String> generatedKeys = new HashSet<>();
+        int generatedCount = 0;
+        for (PatSpecificArchiveGenerateConfig config : configs) {
+            if (!matchConfig(config, patMedOuthosp)) {
+                continue;
+            }
+            String archiveType = config.getArchiveType();
+            String deptCategory = config.getDeptCategory();
+            if (StringUtils.isBlank(archiveType) || StringUtils.isBlank(deptCategory)) {
+                log.warn("鐢熸垚涓撶妗f璺宠繃锛氶厤缃笉瀹屾暣锛宑onfigId={}锛宒eptCategory={}锛宎rchiveType={}",
+                        config.getId(), deptCategory, archiveType);
+                continue;
+            }
+
+            String generatedKey = deptCategory + "|" + archiveType;
+            if (!generatedKeys.add(generatedKey)) {
+                continue;
+            }
+
+            try {
+                if (DEPT_CATEGORY_GYNECOLOGY.equals(deptCategory)) {
+                    PatMaternityArchiveDetail detail =
+                            buildMaternityDetail(patMedOuthosp, targetArchive, config, campusid);
+                    patMaternityArchiveService.insertOrUpdateMaternityArchive(detail);
+                    generatedCount++;
+                } else if (DEPT_CATEGORY_PEDIATRICS.equals(deptCategory)) {
+                    PatChildArchiveDetail detail =
+                            buildChildDetail(patMedOuthosp, targetArchive, config, campusid);
+                    patChildArchiveService.insertOrUpdateChildArchive(detail);
+                    generatedCount++;
+                } else {
+                    log.warn("鐢熸垚涓撶妗f璺宠繃锛氭湭鐭ラ儴闂ㄥ垎绫伙紝configId={}锛宒eptCategory={}",
+                            config.getId(), deptCategory);
+                }
+            } catch (Exception e) {
+                log.error("鐢熸垚涓撶妗f澶辫触锛宑onfigId={}锛宱uthospno={}锛宒eptCategory={}锛宎rchiveType={}",
+                        config.getId(), patMedOuthosp.getOuthospno(), deptCategory, archiveType, e);
+            }
+        }
+        return generatedCount;
+    }
+
+    /**
+     * 鍒ゆ柇闂ㄨ瘖璁板綍鏄惁鍛戒腑閰嶇疆銆�
+     *
+     * <p>閮ㄩ棬鏉′欢涓庤瘖鏂潯浠朵负 OR 鍏崇郴锛氫换涓�鏉′欢鍛戒腑鍗崇敓鎴愶紱涓や釜闆嗗悎閮戒负绌烘椂閰嶇疆鏃犳晥銆�</p>
+     */
+    private boolean matchConfig(PatSpecificArchiveGenerateConfig config, PatMedOuthosp patMedOuthosp) {
+        List<String> deptCodes = parseCodes(config.getDeptCodes());
+        List<String> icd10Codes = parseCodes(config.getIcd10Codes());
+        if (CollectionUtils.isEmpty(deptCodes) && CollectionUtils.isEmpty(icd10Codes)) {
+            return false;
+        }
+
+        boolean deptMatched = false;
+        if (CollectionUtils.isNotEmpty(deptCodes)) {
+            // 闂ㄨ瘖璁板綍鍙湁绉戝锛屾病鏈夌梾鍖猴紱dept_type=2锛堢梾鍖猴級涓嶅弬涓庨棬璇婂尮閰�
+            if (DEPT_TYPE_DEPARTMENT.equals(config.getDeptType())) {
+                deptMatched = containsIgnoreCase(deptCodes, patMedOuthosp.getDeptcode());
+            }
+        }
+
+        boolean diagnosisMatched = false;
+        if (CollectionUtils.isNotEmpty(icd10Codes)) {
+            String icd10code = patMedOuthosp.getIcd10code();
+            String diagname = patMedOuthosp.getDiagname();
+            diagnosisMatched = icd10Codes.stream().anyMatch(code -> {
+                if (equalsIgnoreCase(code, icd10code)) {
+                    return true;
+                }
+                // 閮ㄥ垎 HIS 鏁版嵁鍙湁璇婃柇鍚嶇О娌℃湁璇婃柇鐮侊紝姝ゆ椂鍏佽鎸夎瘖鏂悕绉板厹搴曞尮閰�
+                return StringUtils.isBlank(icd10code) && equalsIgnoreCase(code, diagname);
+            });
+        }
+
+        return deptMatched || diagnosisMatched;
+    }
+
+    private PatMaternityArchiveDetail buildMaternityDetail(PatMedOuthosp patMedOuthosp, PatArchive patArchive,
+                                                           PatSpecificArchiveGenerateConfig config, String campusid) {
+        PatMaternityArchiveDetail detail = new PatMaternityArchiveDetail();
+        detail.setArchiveType(config.getArchiveType());
+        detail.setSourceType(SOURCE_TYPE_OUTHOSP);
+        fillCommonDetail(detail, patMedOuthosp, patArchive, campusid);
+        if (patArchive != null) {
+            detail.setAddress(patArchive.getPlaceOfResidence());
+        }
+        return detail;
+    }
+
+    private PatChildArchiveDetail buildChildDetail(PatMedOuthosp patMedOuthosp, PatArchive patArchive,
+                                                   PatSpecificArchiveGenerateConfig config, String campusid) {
+        PatChildArchiveDetail detail = new PatChildArchiveDetail();
+        detail.setArchiveType(config.getArchiveType());
+        detail.setSourceType(SOURCE_TYPE_OUTHOSP);
+        fillCommonChildDetail(detail, patMedOuthosp, patArchive, campusid);
+        detail.setParentPhone(patArchive != null
+                ? firstNotBlank(patArchive.getTelcode(), patMedOuthosp.getTelcode())
+                : patMedOuthosp.getTelcode());
+        if (patArchive != null) {
+            detail.setParentAddress(patArchive.getPlaceOfResidence());
+        }
+        // 钀ュ吇鎬х柧鐥呮。妗堝悓姝ヨ瘖鏂俊鎭紝鏂逛究鍒楄〃鐩存帴灞曠ず
+        if ("1".equals(config.getArchiveType())) {
+            detail.setNutritionIcd10Code(patMedOuthosp.getIcd10code());
+            detail.setNutritionDiagName(patMedOuthosp.getDiagname());
+        }
+        return detail;
+    }
+
+    private void fillCommonDetail(PatMaternityArchiveDetail detail, PatMedOuthosp patMedOuthosp,
+                                  PatArchive patArchive, String campusid) {
+        detail.setOuthospno(patMedOuthosp.getOuthospno());
+        detail.setSerialnum(patMedOuthosp.getSerialnum());
+        detail.setPatid(patArchive != null ? patArchive.getId() : patMedOuthosp.getPatid());
+        detail.setPatname(patArchive != null
+                ? firstNotBlank(patArchive.getName(), patMedOuthosp.getPatname()) : patMedOuthosp.getPatname());
+        detail.setBirthdate(patArchive != null ? patArchive.getBirthdate() : null);
+        detail.setIdcardno(patArchive != null
+                ? firstNotBlank(patArchive.getIdcardno(), patMedOuthosp.getIdcardno()) : patMedOuthosp.getIdcardno());
+        detail.setPhone(patArchive != null
+                ? firstNotBlank(patArchive.getTelcode(), patMedOuthosp.getTelcode()) : patMedOuthosp.getTelcode());
+        detail.setSex(patArchive != null && patArchive.getSex() != null
+                ? patArchive.getSex() : patMedOuthosp.getSex());
+        detail.setDiagTime(patMedOuthosp.getAdmitdate());
+        detail.setArchiveTime(new Date());
+        detail.setDoctorCode(patMedOuthosp.getDrcode());
+        detail.setDoctorName(patMedOuthosp.getDrname());
+        detail.setManagementDoctorCode(patMedOuthosp.getDrcode());
+        detail.setManagementDoctorName(patMedOuthosp.getDrname());
+        detail.setFirstDoctorCode(patMedOuthosp.getDrcode());
+        detail.setFirstDoctorName(patMedOuthosp.getDrname());
+        detail.setDeptcode(patMedOuthosp.getDeptcode());
+        detail.setDeptname(patMedOuthosp.getDeptname());
+        detail.setIcd10code(patMedOuthosp.getIcd10code());
+        detail.setDiagname(patMedOuthosp.getDiagname());
+        detail.setRegisterUser(patMedOuthosp.getDrname());
+        detail.setOrgid(patMedOuthosp.getOrgid());
+        detail.setCampusid(campusid);
+        detail.setDelFlag("0");
+    }
+
+    private void fillCommonChildDetail(PatChildArchiveDetail detail, PatMedOuthosp patMedOuthosp,
+                                       PatArchive patArchive, String campusid) {
+        detail.setOuthospno(patMedOuthosp.getOuthospno());
+        detail.setSerialnum(patMedOuthosp.getSerialnum());
+        detail.setPatid(patArchive != null ? patArchive.getId() : patMedOuthosp.getPatid());
+        detail.setPatname(patArchive != null
+                ? firstNotBlank(patArchive.getName(), patMedOuthosp.getPatname()) : patMedOuthosp.getPatname());
+        detail.setBirthdate(patArchive != null ? patArchive.getBirthdate() : null);
+        detail.setIdcardno(patArchive != null
+                ? firstNotBlank(patArchive.getIdcardno(), patMedOuthosp.getIdcardno()) : patMedOuthosp.getIdcardno());
+        detail.setPhone(patArchive != null
+                ? firstNotBlank(patArchive.getTelcode(), patMedOuthosp.getTelcode()) : patMedOuthosp.getTelcode());
+        detail.setSex(patArchive != null && patArchive.getSex() != null
+                ? patArchive.getSex() : patMedOuthosp.getSex());
+        detail.setDiagTime(patMedOuthosp.getAdmitdate());
+        detail.setArchiveTime(new Date());
+        detail.setDoctorCode(patMedOuthosp.getDrcode());
+        detail.setDoctorName(patMedOuthosp.getDrname());
+        detail.setManagementDoctorCode(patMedOuthosp.getDrcode());
+        detail.setManagementDoctorName(patMedOuthosp.getDrname());
+        detail.setDeptcode(patMedOuthosp.getDeptcode());
+        detail.setDeptname(patMedOuthosp.getDeptname());
+        detail.setIcd10code(patMedOuthosp.getIcd10code());
+        detail.setDiagname(patMedOuthosp.getDiagname());
+        detail.setRegisterUser(patMedOuthosp.getDrname());
+        detail.setOrgid(patMedOuthosp.getOrgid());
+        detail.setCampusid(campusid);
+        detail.setDelFlag("0");
+    }
+
+    private PatArchive resolvePatArchive(PatMedOuthosp patMedOuthosp, PatArchive patArchive) {
+        if (patArchive != null && patArchive.getId() != null) {
+            return patArchive;
+        }
+
+        PatArchive query = new PatArchive();
+        if (StringUtils.isNotBlank(patMedOuthosp.getPatno())) {
+            query.setPatientno(patMedOuthosp.getPatno());
+        } else if (StringUtils.isNotBlank(patMedOuthosp.getIdcardno())) {
+            query.setIdcardno(patMedOuthosp.getIdcardno());
+        } else {
+            return null;
+        }
+        query.setOrgid(patMedOuthosp.getOrgid());
+
+        List<PatArchive> archives = patArchiveMapper.selectPatArchiveList(query);
+        return CollectionUtils.isNotEmpty(archives) ? archives.get(0) : null;
+    }
+
+    private List<String> parseCodes(String raw) {
+        if (StringUtils.isBlank(raw)) {
+            return Collections.emptyList();
+        }
+        String value = raw.trim();
+        try {
+            JSONArray array = JSON.parseArray(value);
+            if (array != null) {
+                List<String> result = new ArrayList<>();
+                for (Object item : array) {
+                    if (item != null && StringUtils.isNotBlank(String.valueOf(item))) {
+                        result.add(String.valueOf(item).trim());
+                    }
+                }
+                return result;
+            }
+        } catch (Exception e) {
+            log.warn("瑙f瀽妗f鐢熸垚閰嶇疆闆嗗悎澶辫触锛屾寜閫楀彿鍒嗛殧澶勭悊锛歿}", raw);
+        }
+        return Arrays.stream(value.split(","))
+                .map(String::trim)
+                .filter(StringUtils::isNotBlank)
+                .collect(Collectors.toList());
+    }
+
+    private String firstNotBlank(String first, String second) {
+        return StringUtils.isNotBlank(first) ? first : second;
+    }
+
+    private boolean containsIgnoreCase(List<String> source, String target) {
+        if (StringUtils.isBlank(target)) {
+            return false;
+        }
+        return source.stream().anyMatch(item -> equalsIgnoreCase(item, target));
+    }
+
+    private boolean equalsIgnoreCase(String first, String second) {
+        return first != null && second != null && first.trim().equalsIgnoreCase(second.trim());
+    }
+}

--
Gitblit v1.9.3