liusheng
3 天以前 c462811cd33e81a80bc089e3677975c069d9cda5
smartor/src/main/java/com/smartor/service/impl/ServiceSLTDHealthcareRecordServiceImpl.java
@@ -79,7 +79,8 @@
        try {
            Map<String, Object> requestParams = buildRequestParams(reqVO);
            Map<String, String> headers = buildRequestHeaders();
            String result = HttpUtils.sendPostByHeader(sltdPubPath + "osj/hbos-thirdparty-integration/standard/common/healthcareRecord/dtcQueryHealthcareRecordList", new Gson().toJson(requestParams), headers);
            log.info("请求参数出院数据地址:{},appKey:{}", sltdPubPath + "/osj/hbos-thirdparty-integration/standard/common/healthcareRecord/dtcQueryHealthcareRecordList", APP_KEY);
            String result = HttpUtils.sendPostByHeader(sltdPubPath + "/osj/hbos-thirdparty-integration/standard/common/healthcareRecord/dtcQueryHealthcareRecordList", new Gson().toJson(requestParams), headers);
            String cry = determineCry(reqVO);
            log.info("cry的值为:{}", cry);
@@ -88,7 +89,7 @@
            return serviceSLTDInhospResDTOS;
        } catch (Exception e) {
            log.error("【queryHealthcareRecordList】调用省立同德健康记录查询接口异常,请求参数:{}", reqVO, e);
            throw new RuntimeException("调用省立同德健康记录查询接口失败", e);
            throw new RuntimeException("调用省立同德健康记录查询接口失败:" + e.getMessage());
        }
    }
@@ -166,10 +167,15 @@
                sysDept.setUpdateTime(new Date());
                sysDept.setUpdateBy(null);
                SysDept sysDept1 = sysDeptMapper.selectDeptByCode(sysDept.getDeptCode(), sysDept.getOrgid());
                // 按 deptCode + orgid + campusid 查重,避免跨院区科室重复
                SysDept sysDept1 = sysDeptMapper.selectDeptByCodeAndCampus(sysDept.getDeptCode(), sysDept.getOrgid(), sysDept.getCampusid());
                if (Objects.isNull(sysDept1)) {
                    int i = sysDeptMapper.insertDept(sysDept);
                    log.info("ServiceExternalServiceImpl---addDeptInfo是否新增成功:{}", i);
                    try {
                        int i = sysDeptMapper.insertDept(sysDept);
                        log.info("ServiceExternalServiceImpl---addDeptInfo是否新增成功:{}", i);
                    } catch (org.springframework.dao.DuplicateKeyException e) {
                        log.warn("【queryDeptWardAreaInfoList】科室已存在(并发插入),跳过:deptCode={}, orgid={}, campusid={}", sysDept.getDeptCode(), sysDept.getOrgid(), sysDept.getCampusid());
                    }
                } else {
                    sysDept.setDeptId(sysDept1.getDeptId());
                    int i = sysDeptMapper.updateDept(sysDept);
@@ -247,87 +253,94 @@
            sysUser.setUpdateTime(new Date());
            sysUser.setUpdateBy("admin");
            sysUser.setCreateBy("admin");
            sysUser.setCreateBy("admin");
            sysUser.setOrgid("" + getLongValue(dataItem, "orgId"));
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            sysUser.setPassword(passwordEncoder.encode(userPwd));
            SysUser sysUser1 = sysUser2Mapper.selectUserByUserName(sysUser.getUserName());
            if (ObjectUtils.isNotEmpty(sysUser1)) {
                //由人工维护的字段是不需要更新的
                //已存在:由人工维护的字段是不需要更新的,orgid/campusid保留原值不覆盖
                sysUser.setSearchscope(null);
                sysUser.setPassword(null);
                sysUser.setUserId(sysUser1.getUserId());
                sysUser.setOrgid(null);
                sysUser.setCampusid(null);
                sysUser2Mapper.updateUser(sysUser);
            } else {
                sysUser2Mapper.insertUser(sysUser);
                try {
                    sysUser2Mapper.insertUser(sysUser);
                } catch (org.springframework.dao.DuplicateKeyException e) {
                    //并发插入场景:重新查询获取已存在记录,避免重复
                    log.warn("【queryHospUserInfoList】用户已存在(并发插入),跳过:userName={}", sysUser.getUserName());
                    SysUser existingUser = sysUser2Mapper.selectUserByUserName(sysUser.getUserName());
                    if (ObjectUtils.isNotEmpty(existingUser)) {
                        sysUser.setUserId(existingUser.getUserId());
                    }
                }
            }
            // 处理用户与部门的关系
            List businessDepts = (List<?>) dataItem.get("businessDepts");
            if (CollectionUtils.isEmpty(businessDepts)) {
                continue;
            }
            if (CollectionUtils.isNotEmpty(businessDepts)) {
                List<List<String>> deptInfoList = new ArrayList<>();
                List<List<String>> hospInfoList = new ArrayList<>();
                log.info("------businessDepts是否有值:{}", businessDepts.size());
                for (Object businessDept : businessDepts) {
                    if (businessDept instanceof Map) {
                        Map<String, Object> businessDeptMap = (Map<String, Object>) businessDept;
                        log.info("------businessDeptMap是否有值:{}", businessDeptMap);
                        String hisDeptId = getStringValue(businessDeptMap, "deptId");
                        log.info("------hisDeptId是否有值:{}, reqVO.getOrgId()的值为:{}", hisDeptId, reqVO.getCampusId());
                        //在这里,hisDeptId就是deptCode,按院区+机构查科室,避免跨院区查错
                        SysDept sysDept = sysDeptMapper.selectDeptByCodeAndCampus(hisDeptId, reqVO.getOrgId(), reqVO.getCampusId() != null ? reqVO.getCampusId().toString() : null);
                        log.info("------hisDeptId是否有值:{}, reqVO.getCampusId()的值为:{}", hisDeptId, reqVO.getCampusId());
                        if (Objects.isNull(sysDept)) continue;
                        SysUserDept sysUserDept = new SysUserDept();
                        sysUserDept.setUserId(sysUser.getUserId());
                        sysUserDept.setDeptType(sysDept.getDeptType());
                        sysUserDept.setDeptId(sysDept.getDeptId());
                        sysUserDept.setDeptCode(sysDept.getDeptCode());
                        sysUserDept.setDeptName(sysDept.getDeptName());
                        sysUserDept.setOrgid(sysDept.getOrgid());
                        sysUserDept.setCreateTime(new Date());
                        sysUserDept.setDelFlag(0L);
            List<List<String>> deptInfoList = new ArrayList<>();
            List<List<String>> hospInfoList = new ArrayList<>();
            log.info("------businessDepts是否有值:{}", businessDepts.size());
            for (Object businessDept : businessDepts) {
                if (businessDept instanceof Map) {
                    Map<String, Object> businessDeptMap = (Map<String, Object>) businessDept;
                    log.info("------businessDeptMap是否有值:{}", businessDeptMap);
//                    Long hisDeptId = (Long) personnelDeptMap.get("deptId");
                    String hisDeptId = getStringValue(businessDeptMap, "deptId");
                    log.info("------hisDeptId是否有值:{}, reqVO.getOrgId()的值为:{}", hisDeptId, reqVO.getCampusId());
                    //在这里,hisDeptId就是deptCode
                    SysDept sysDept = sysDeptMapper.selectDeptByCode(hisDeptId, reqVO.getOrgId());
                    log.info("------hisDeptId是否有值:{}, reqVO.getCampusId()的值为:{}", hisDeptId, reqVO.getCampusId());
                    if (Objects.isNull(sysDept)) continue;
                    SysUserDept sysUserDept = new SysUserDept();
                    sysUserDept.setUserId(sysUser.getUserId());
                    sysUserDept.setDeptType(sysDept.getDeptType());
                    sysUserDept.setDeptId(sysDept.getDeptId());
                    sysUserDept.setDeptCode(sysDept.getDeptCode());
                    sysUserDept.setDeptName(sysDept.getDeptName());
                    sysUserDept.setOrgid(sysDept.getOrgid());
                    sysUserDept.setCreateTime(new Date());
                    sysUserDept.setDelFlag(0L);
                    //判断一下用户-部门关系是不是已经存在了(无论新用户还是老用户都需要去重)
                    if (ObjectUtils.isNotEmpty(sysUser.getUserId()) && ObjectUtils.isNotEmpty(sysDept)) {
                        SysUserDept sud = new SysUserDept();
                        sud.setUserId(sysUser.getUserId());
                        sud.setDeptId(sysDept.getDeptId());
                        sud.setOrgid(sysDept.getOrgid());
                        List<SysUserDept> sysUserDepts = sysUserDeptMapper.selectSysUserDeptList(sud);
                        if (CollectionUtils.isNotEmpty(sysUserDepts)) {
                            sysUserDept.setId(sysUserDepts.get(0).getId());
                            sysUserDeptMapper.updateSysUserDept(sysUserDept);
                        } else {
                            sysUserDeptMapper.insertSysUserDept(sysUserDept);
                        //判断一下用户-部门关系是不是已经存在了(无论新用户还是老用户都需要去重)
                        if (ObjectUtils.isNotEmpty(sysUser.getUserId()) && ObjectUtils.isNotEmpty(sysDept)) {
                            SysUserDept sud = new SysUserDept();
                            sud.setUserId(sysUser.getUserId());
                            sud.setDeptId(sysDept.getDeptId());
                            sud.setOrgid(sysDept.getOrgid());
                            List<SysUserDept> sysUserDepts = sysUserDeptMapper.selectSysUserDeptList(sud);
                            if (CollectionUtils.isNotEmpty(sysUserDepts)) {
                                sysUserDept.setId(sysUserDepts.get(0).getId());
                                sysUserDeptMapper.updateSysUserDept(sysUserDept);
                            } else {
                                sysUserDeptMapper.insertSysUserDept(sysUserDept);
                            }
                        }
                        if (StringUtils.isNotEmpty(sysDept.getDeptType()) && sysDept.getDeptType().equals("1")) {
                            hospInfoList.add(Arrays.asList(sysDept.getDeptCode()));
                        } else if (StringUtils.isNotEmpty(sysDept.getDeptType()) && sysDept.getDeptType().equals("2")) {
                            deptInfoList.add(Arrays.asList(sysDept.getDeptCode()));
                        }
                    }
                    if (StringUtils.isNotEmpty(sysDept.getDeptType()) && sysDept.getDeptType().equals("1")) {
                        hospInfoList.add(Arrays.asList(sysDept.getDeptCode()));
                    } else if (StringUtils.isNotEmpty(sysDept.getDeptType()) && sysDept.getDeptType().equals("2")) {
                        deptInfoList.add(Arrays.asList(sysDept.getDeptCode()));
                    }
                }
                //将部门我病区放到指定字段中
                Gson gson = new Gson();
                sysUser.setHospInfo(gson.toJson(hospInfoList));
                sysUser.setDeptInfo(gson.toJson(deptInfoList));
                sysUser2Mapper.updateUser(sysUser);
            }
            //将部门我病区放到指定字段中
            Gson gson = new Gson();
            sysUser.setHospInfo(gson.toJson(hospInfoList));
            sysUser.setDeptInfo(gson.toJson(deptInfoList));
            sysUser2Mapper.updateUser(sysUser);
            //设置一个默认角色(医生角色)
            List<SysUserRole> userRoleList = new ArrayList();
            SysUserRole sur = new SysUserRole();
            sur.setUserId(sysUser.getUserId());
            sur.setRoleId(3L);
            sur.setRoleId(4L);
            sur.setOrgid(sysUser.getOrgid());
            //先查询一下,是否存在
            SysUserRole sysUserRole = sysUserRoleMapper.selectUserRoleByRoleIdAndUserId(3L, sysUser.getUserId());
            SysUserRole sysUserRole = sysUserRoleMapper.selectUserRoleByRoleIdAndUserId(4L, sysUser.getUserId());
            if (ObjectUtils.isNotEmpty(sysUserRole)) continue;
            userRoleList.add(sur);
@@ -597,8 +610,10 @@
                queryInhosp.setInhospstate("3");
                List<PatMedInhosp> patMedInhospList = patMedInhospService.selectPatMedInhosp(queryInhosp);
                if (CollectionUtils.isNotEmpty(patMedInhospList)) {
                    //如果预入院不为空,则进行修改
                    //如果预入院不为空,则进行修改(预入院→入院)
                    patMedInhosp.setInhospid(patMedInhospList.get(0).getInhospid());
                    patMedInhospService.updatePatMedInhosp(patMedInhosp);
                    log.info("由预入院更新为入院:serialnum={}", patMedInhosp.getSerialnum());
                } else {
                    //如果出院不为空,则不进行处理
                    queryInhosp.setInhospstate("1");
@@ -675,6 +690,8 @@
            if (locked == null || Boolean.TRUE.equals(locked)) {
                try {
                    queryInhosp.setInhospstate(null);
                    queryInhosp.setSchemestatus(null);
                    queryInhosp.setPatno(null);
                    List<PatMedInhosp> patMedInhospList3 = patMedInhospService.selectPatMedInhosp(queryInhosp);
                    if (CollectionUtils.isEmpty(patMedInhospList3)) {
                        try {
@@ -710,11 +727,43 @@
        patMedInhosp.setSerialnum(dto.getHealthcareRecordNo());
        patMedInhosp.setInhospno(dto.getHealthcareRecordNo());
        patMedInhosp.setFuflag("1");
        if (StringUtils.isNotEmpty(patArchive.getNotrequiredFlag()) && patArchive.getNotrequiredFlag().equals("1"))
            patMedInhosp.setFuflag("0");
        if ("0".equals(cry)) patMedInhosp.setInhospstate("0");
        if ("1".equals(cry)) patMedInhosp.setInhospstate("1");
        if ("3".equals(cry)) patMedInhosp.setInhospstate("3");
        if ("0".equals(cry)) {
            patMedInhosp.setInhospstate("0");
            if (CollectionUtils.isNotEmpty(dto.getDiagnosisList()) && dto.getDiagnosisList().size() > 0) {
                for (ServiceSLTDDiagnosisResDTO diagnosis : dto.getDiagnosisList()) {
                    if (diagnosis.getIsMainDiagnosis() == 1) {
                        patMedInhosp.setDiagname(diagnosis.getDiagnosisDictName() == null ? "" : diagnosis.getDiagnosisDictName());
                        patMedInhosp.setIcd10code(diagnosis.getDiagnosisDictCode() == null ? "" : diagnosis.getDiagnosisDictCode());
                    }
                }
            }
        }
        if ("1".equals(cry)) {
            patMedInhosp.setInhospstate("1");
            ServiceSLTDDiagnosisResDTO mainDischargeDiagnosis = dto.getMainDischargeDiagnosis();
            log.info("-----------mainDischargeDiagnosis的值有了吗?{}", mainDischargeDiagnosis);
            if (!Objects.isNull(mainDischargeDiagnosis)) {
                if (mainDischargeDiagnosis.getIsMainDiagnosis() == 1) {
                    patMedInhosp.setLeavediagname(StringUtils.defaultString(mainDischargeDiagnosis.getDiagnosisDictName(), ""));
                    patMedInhosp.setLeaveicd10code(StringUtils.defaultString(mainDischargeDiagnosis.getDiagnosisDictCode(), ""));
                }
            }
        }
        if ("3".equals(cry)) {
            patMedInhosp.setInhospstate("3");
            if (CollectionUtils.isNotEmpty(dto.getDiagnosisList()) && dto.getDiagnosisList().size() > 0) {
                for (ServiceSLTDDiagnosisResDTO diagnosis : dto.getDiagnosisList()) {
                    if (diagnosis.getIsMainDiagnosis() == 1) {
                        patMedInhosp.setDiagname(diagnosis.getDiagnosisDictName() == null ? "" : diagnosis.getDiagnosisDictName());
                        patMedInhosp.setIcd10code(diagnosis.getDiagnosisDictCode() == null ? "" : diagnosis.getDiagnosisDictCode());
                    }
                }
            }
        }
        patMedInhosp.setHospitalcode(dto.getOrgId());
        patMedInhosp.setBedNo(dto.getBedNumber());
        patMedInhosp.setStarttime(parseDate(dto.getStartTime()));
@@ -761,24 +810,46 @@
    private PatArchive processPatientArchive(ServiceSLTDInhospResDTO dto) {
        List<PatArchive> existingArchives = null;
        // 优先按 patientno 精确查重,查不到再按 idcardno 查重(AND条件会漏查同一人不同patientno的情况)
        if (dto.getPatientId() != null) {
        // 第一步:按 patientno 精确查重(与插入时使用的 medicalRecordNo 一致)
        String patientno = dto.getMedicalRecordNo() == null ? null : String.valueOf(dto.getMedicalRecordNo());
        if (StringUtils.isNotEmpty(patientno)) {
            PatArchive queryByPatientNo = new PatArchive();
            queryByPatientNo.setPatientno(String.valueOf(dto.getPatientId()));
            queryByPatientNo.setIdcardno(StringUtils.isNotEmpty(dto.getIdCardNo()) ? dto.getIdCardNo().trim() : null);
            queryByPatientNo.setPatientno(patientno);
            existingArchives = patArchiveService.selectPatArchiveList(queryByPatientNo);
        }
//        if (CollectionUtils.isEmpty(existingArchives) && !StringUtils.isEmpty(dto.getIdCardNo())) {
//            PatArchive queryByIdCard = new PatArchive();
//            queryByIdCard.setIdcardno(dto.getIdCardNo().trim());
//            existingArchives = patArchiveService.selectPatArchiveList(queryByIdCard);
//        }
        // 第二步:按 patientno 查不到时,按 idcardno 查重(分步OR,避免AND条件漏查)
        if (CollectionUtils.isEmpty(existingArchives) && StringUtils.isNotEmpty(dto.getIdCardNo())) {
            PatArchive queryByIdCard = new PatArchive();
            queryByIdCard.setIdcardno(dto.getIdCardNo().trim());
            existingArchives = patArchiveService.selectPatArchiveList(queryByIdCard);
        }
        // 第三步:按 patidHis 查重(兼容旧数据,旧数据可能以 patientId 存为 patientno)
        if (CollectionUtils.isEmpty(existingArchives) && dto.getPatientId() != null) {
            PatArchive queryByPatidHis = new PatArchive();
            queryByPatidHis.setPatientno(String.valueOf(dto.getPatientId()));
            existingArchives = patArchiveService.selectPatArchiveList(queryByPatidHis);
        }
        PatArchive patArchive = buildPatientArchive(dto);
        if (CollectionUtils.isEmpty(existingArchives)) {
            patArchiveService.insertPatArchive(patArchive);
            log.debug("【processPatientArchive】新增患者档案,患者编号:{}", patArchive.getPatientno());
            try {
                patArchiveService.insertPatArchive(patArchive);
                log.debug("【processPatientArchive】新增患者档案,患者编号:{}", patArchive.getPatientno());
            } catch (org.springframework.dao.DuplicateKeyException e) {
                log.warn("【processPatientArchive】患者档案已存在(并发插入),跳过:patientno={}, idcardno={}", patArchive.getPatientno(), patArchive.getIdcardno());
                // 并发插入场景,重新查询获取已存在的记录
                PatArchive queryRetry = new PatArchive();
                queryRetry.setPatientno(patArchive.getPatientno());
                existingArchives = patArchiveService.selectPatArchiveList(queryRetry);
                if (CollectionUtils.isNotEmpty(existingArchives)) {
                    patArchive.setId(existingArchives.get(0).getId());
                    patArchive.setNotrequiredFlag(existingArchives.get(0).getNotrequiredFlag());
                    patArchive.setNotrequiredreason(existingArchives.get(0).getNotrequiredreason());
                }
            }
        } else {
            patArchive.setId(existingArchives.get(0).getId());
            patArchive.setNotrequiredFlag(existingArchives.get(0).getNotrequiredFlag());
@@ -872,7 +943,17 @@
        dto.setCostNatureName(getStringValue(dataItem, "costNatureName"));
        dto.setCostNatureCode(getStringValue(dataItem, "costNatureCode"));
        dto.setFurtherConsultationStatus(getIntegerValue(dataItem, "furtherConsultationStatus"));
        Object mainDiagObj = dataItem.get("mainDischargeDiagnosis");
        if (mainDiagObj instanceof Map) {
            Map<String, Object> mainDiagMap = (Map<String, Object>) mainDiagObj;
            ServiceSLTDDiagnosisResDTO mainDiag = new ServiceSLTDDiagnosisResDTO();
            mainDiag.setDiagnosisDictCode(getStringValue(mainDiagMap, "diagnosisDictCode"));
            mainDiag.setDiagnosisDictName(getStringValue(mainDiagMap, "diagnosisDictName"));
            mainDiag.setDiagnosisCategory(getStringValue(mainDiagMap, "diagnosisCategory"));
            mainDiag.setRecordType(getIntegerValue(mainDiagMap, "recordType"));
            mainDiag.setIsMainDiagnosis(getIntegerValue(mainDiagMap, "isMainDiagnosis"));
            dto.setMainDischargeDiagnosis(mainDiag);
        }
        Object contactsObj = dataItem.get("residentContactInfos");
        if (contactsObj instanceof List) {
            dto.setResidentContactInfos(parseContacts((List<?>) contactsObj));