Преглед изворни кода

样本送检-动物样本-血清送检,LIS同步调整

陈长荣 пре 1 месец
родитељ
комит
2ebdaf7d51

+ 124 - 143
jfcloud-gene-biz/src/main/java/com/github/jfcloud/gene/lis/service/LisSyncService.java

@@ -30,14 +30,13 @@ import lombok.RequiredArgsConstructor;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.context.event.EventListener;
+import org.springframework.data.redis.core.StringRedisTemplate;
 import org.springframework.stereotype.Service;
 
 import java.lang.reflect.Field;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
-import java.util.function.Function;
+import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 
 @Slf4j
@@ -50,6 +49,8 @@ public class LisSyncService {
     private final SampleAnimalService sampleAnimalService;
     private final SampleInfoService sampleInfoService;
     private final DBSystemPropertiesService systemPropertiesService;
+    private final StringRedisTemplate redisTemplate;
+    private static final String LIS_PREFIX = "gene:sample:lis:";
 
     /**
      * 同步LIS
@@ -64,168 +65,148 @@ public class LisSyncService {
         SampleAnimalVo animalDetail = sampleAnimalService.getDetail(sampleId);
         Assert.notNull(animalDetail, "同步LIS失败,动物样本检测不存在");
         Assert.isTrue(StrUtil.isNotBlank(animalDetail.getScientificSampleType()) &&
-                animalDetail.getScientificSampleType().contains(ScientificSampleTypeEnum.serum.getDescription()), "同步LIS失败,只有包含血清样本送检才可以同步");
+                animalDetail.getScientificSampleType().contains(ScientificSampleTypeEnum.SERUM.getDescription()), "同步LIS失败,只有包含血清样本送检才可以同步");
         SampleCheckSerumVo serum = animalDetail.getOrder().getSerum();
+        Assert.notEmpty(serum.getItems(), "同步LIS失败,检测条目为空");
 
         SampleCheckOrder checkOrder = checkOrderMapper.selectById(serum.getId());
-        JSONObject extObj = new JSONObject();
         if (StrUtil.isNotBlank(checkOrder.getExtData())) {
-            extObj = JSON.parseObject(checkOrder.getExtData());
+            JSONObject extObj = JSON.parseObject(checkOrder.getExtData());
             Assert.isFalse(extObj.containsKey("lisId"), "同步LIS失败,已同步过");
         }
 
         log.info("开始同步lis数据库 sample={}", sampleId);
 
         //查询科室表HT_INFO_DEP对应的机构
-        List<Entity> depEntities = lisDb.query("select id, name, hsp_id from dbo.HT_INFO_DEP");
-        Map<Long, Entity> depMap = depEntities.stream().collect(Collectors.toMap(e -> e.getLong("id"), Function.identity()));
+        Entity depEntity = new Entity();
+        if (serum.getSendOrgId() != null) {
+            depEntity = lisDb.queryOne("select id, name, hsp_id from dbo.HT_INFO_DEP where name = N'"
+                    + serum.getSendOrgName() + "' and HSP_ID=" + serum.getSendOrgId());
+        }
 
         //根据昵称查询用户id
         Entity applyUser = lisDb.queryOne("select top 1 user_id from dbo.HT_INFO_USER_EXTEND where name = N'" + sampleInfo.getApplicantName() + "' order by user_id desc");
         Assert.notNull(applyUser, "同步LIS失败,申请人姓名[{}]不存在LIS系统", sampleInfo.getApplicantName());
 
-        //记录HtScienceApply的id
-        Map<String, Long> applyIdMap = new HashMap<>();
-        Map<String, Long> itemIdMap = new HashMap<>();
-        //记录HtScienceApply的编号最后
-        Map<String, Integer> applyEndNoMap = new HashMap<>();
-
         //同步LIS
-        List<HtScienceApply> applyList = new ArrayList<>();
-        List<HtScienceApplyItem> applyItemList = new ArrayList<>();
-        for (CheckItemSerumVo item : serum.getItems()) {
-            HtScienceApply scienceApply = new HtScienceApply();
-            scienceApply.setPetType(item.getLisPetTypeId());
-            scienceApply.setCreateId(applyUser.getStr("user_id"));
-            scienceApply.setCreateName(sampleInfo.getApplicantName());
-            scienceApply.setApplyDocId(scienceApply.getCreateId());
-            scienceApply.setApplyDoc(scienceApply.getCreateName());
-            scienceApply.setEmail(StrUtil.nullToDefault(serum.getApplyUserEmail(), sampleInfo.getApplicantEmail()));
-            scienceApply.setPhone(StrUtil.nullToDefault(serum.getApplyUserMobile(), sampleInfo.getApplicantPhone()));
-
-            if (serum.getSendOrgId() != null) {
-                scienceApply.setHspId(depMap.get(serum.getSendOrgId()).getStr("hsp_id"));
-                scienceApply.setApplyDep(serum.getSendOrgId().toString());
-            }
-            if (serum.getSendOrgId() != null) {
-                scienceApply.setDeliverOperId(serum.getSendUserId().toString());
-                scienceApply.setDeliverOper(serum.getSendUserName());
-            }
-            if (item.getLisSampleTypeId() != null) {
-                scienceApply.setSampleId(item.getLisSampleTypeId().toString());
-            }
+        CheckItemSerumVo item = serum.getItems().get(0);
+
+        HtScienceApply scienceApply = new HtScienceApply();
+        scienceApply.setPetType(item.getLisPetTypeId());
+        scienceApply.setCreateId(applyUser.getStr("user_id"));
+        scienceApply.setCreateName(sampleInfo.getApplicantName());
+        scienceApply.setApplyDocId(scienceApply.getCreateId());
+        scienceApply.setApplyDoc(scienceApply.getCreateName());
+        scienceApply.setEmail(StrUtil.nullToDefault(serum.getApplyUserEmail(), sampleInfo.getApplicantEmail()));
+        scienceApply.setPhone(StrUtil.nullToDefault(serum.getApplyUserMobile(), sampleInfo.getApplicantPhone()));
+
+        //编号前缀
+        String redisPrefix = redisTemplate.opsForValue().get(LIS_PREFIX + DateUtil.today());
+        if (StrUtil.isNotBlank(redisPrefix)) {
+            //获取最后一个字母
+            char lastChar = redisPrefix.charAt(redisPrefix.length() - 1);
+            //如果是Z,后面添加A;否则,将最后一个字母+1
+            redisPrefix = lastChar == 'Z' ? (redisPrefix + 'A') :
+                    (redisPrefix.substring(0, redisPrefix.length() - 1) + (char) (lastChar + 1));
+        } else {
+            redisPrefix = "A";
+        }
+        scienceApply.setPreText(redisPrefix);
 
-            if (item.getLisSexId() != null) {
-                scienceApply.setSex(item.getLisSexId());
-            }
-            if (StrUtil.isNotBlank(item.getAge())) {
-                scienceApply.setAge(Integer.parseInt(item.getAge()));
-                scienceApply.setAgeUnit("hdmy".indexOf(item.getAgeUnit().charAt(0)));
-                JSONObject ageObj = JSONObject.parseObject(scienceApply.getAgeDisplay());
-                if (scienceApply.getAgeUnit() == 3) {
-                    ageObj.put("Year", item.getAge());
-                } else if (scienceApply.getAgeUnit() == 2) {
-                    ageObj.put("Month", item.getAge());
-                } else if (scienceApply.getAgeUnit() == 1) {
-                    ageObj.put("Day", item.getAge());
-                }
-                scienceApply.setAgeDisplay(ageObj.toJSONString());
-            }
+        if (serum.getSendOrgId() != null) {
+            scienceApply.setHspId(serum.getSendOrgId().toString());
+            scienceApply.setApplyDep(depEntity.getStr("id"));
+        }
+        if (serum.getSendUserId() != null) {
+            scienceApply.setDeliverOperId(serum.getSendUserId().toString());
+            scienceApply.setDeliverOper(serum.getSendUserName());
+        }
+        if (item.getLisSampleTypeId() != null) {
+            scienceApply.setSampleId(item.getLisSampleTypeId().toString());
+        } else if (animalDetail.getLisSampleTypeId() != null) {
+            scienceApply.setSampleId(animalDetail.getLisSampleTypeId().toString());
+        }
 
-            if (StrUtil.isNotBlank(item.getQuick())) {
-                scienceApply.setFastFlag(Integer.parseInt(item.getQuick()));
+        if (item.getLisSexId() != null) {
+            scienceApply.setSex(item.getLisSexId());
+        }
+        if (StrUtil.isNotBlank(item.getAge())) {
+            scienceApply.setAge(Integer.parseInt(item.getAge()));
+            scienceApply.setAgeUnit("hdmy".indexOf(item.getAgeUnit().charAt(0)));
+            JSONObject ageObj = JSONObject.parseObject(scienceApply.getAgeDisplay());
+            if (scienceApply.getAgeUnit() == 3) {
+                ageObj.put("Year", item.getAge());
+            } else if (scienceApply.getAgeUnit() == 2) {
+                ageObj.put("Month", item.getAge());
+            } else if (scienceApply.getAgeUnit() == 1) {
+                ageObj.put("Day", item.getAge());
             }
+            scienceApply.setAgeDisplay(ageObj.toJSONString());
+        }
 
-            scienceApply.setApplyTime(new DateTime(item.getApplyDate()));
-            scienceApply.setDeliverTime(new DateTime(item.getSendDate()));
-            scienceApply.setSampleCount(Integer.parseInt(item.getNum()));
-            Assert.isTrue(scienceApply.getSampleCount() > 0, "同步LIS失败,样本数量不能为0");
-            scienceApply.setPreText(item.getCodePrefix());
-            scienceApply.setBeginNo(String.format("%0" + item.getCodeSuffix().length() + "d", 1));
-            Assert.isTrue(Integer.parseInt(item.getCodeSuffix()) > 0, "同步LIS失败,样本数量不能为0");
-
-            long applyId;
-            //编号起始
-            int beginNo = 1;
-
-            //HT_SCIENCE_APPLY主键非自增,为年份2位+月份2位+日期2位+483+7位序列号,为了避免冲突,这里使用480
-            String idPrefix = DateUtil.format(scienceApply.getApplyTime(), "yyMMdd") + "480";
-            String noPrefix = "HKYBKY" + item.getApplyDate().replace("-", "");
-
-            if (applyIdMap.containsKey(idPrefix)) {
-                applyId = applyIdMap.get(idPrefix) + 1;
-                beginNo = applyEndNoMap.get(noPrefix) + 1;
-            } else {
-                applyId = Long.parseLong(String.format("%s%07d", idPrefix, 1));
-
-                //查询当前最大的id与编号(编号格式HKYBKY20250326-14-15)
-                String sql = String.format("select top 1 id, SampleNo from dbo.HT_SCIENCE_APPLY where id like '%s%%' order by SampleNo desc ", idPrefix);
-                Entity idEntity = lisDb.queryOne(sql);
-                if (idEntity != null) {
-                    applyId = idEntity.getLong("id") + 1;
-                    String sampleNo = idEntity.getStr("SampleNo");
-                    String[] split = sampleNo.split("-");
-                    beginNo = Integer.parseInt(split[2]) + 1;
-                }
-            }
+        if (StrUtil.isNotBlank(item.getQuick())) {
+            scienceApply.setFastFlag(Integer.parseInt(item.getQuick()));
+        } else if (StrUtil.isNotBlank(animalDetail.getHandoverRemark())) {
+            boolean quick = animalDetail.getHandoverRemark().contains("急检");
+            scienceApply.setFastFlag(quick ? 1 : 0);
+        }
 
-            scienceApply.setId(applyId);
-            applyIdMap.put(idPrefix, applyId);
-
-            //编号结尾
-            int endNo = beginNo + scienceApply.getSampleCount() - 1;
-            applyEndNoMap.put(noPrefix, endNo);
-            scienceApply.setSampleNo(noPrefix + "-" + beginNo + "-" + endNo);
-            fillNullWithDefault(scienceApply);
-            applyList.add(scienceApply);
-
-            //检测指标
-            if (StrUtil.isNotBlank(item.getTarget())) {
-                //项目id,将480编号替换为481
-                idPrefix = idPrefix.replace("480", "481");
-                long itemPrimaryId;
-                if (itemIdMap.containsKey(idPrefix)) {
-                    itemPrimaryId = itemIdMap.get(idPrefix);
-                } else {
-                    itemPrimaryId = Long.parseLong(String.format("%s%07d", idPrefix, 1));
-
-                    //查询当前最大的id
-                    String sql = String.format("select top 1 id from dbo.HT_SCIENCE_APPLY_ITEM where id like '%s%%' order by id desc ", idPrefix);
-                    Entity idEntity = lisDb.queryOne(sql);
-                    if (idEntity != null) {
-                        itemPrimaryId = idEntity.getLong("id") + 1;
-                    }
-                }
-
-                log.info("同步lis数据库 HT_SCIENCE_APPLY起始id={} HT_SCIENCE_APPLY_ITEM起始id={} ", applyId, itemPrimaryId);
-
-                for (String itemId : item.getTarget().split(",")) {
-                    HtScienceApplyItem applyItem = new HtScienceApplyItem();
-                    applyItem.setId(itemPrimaryId++);
-                    applyItem.setApplyId(scienceApply.getId());
-                    applyItem.setItemId(Long.parseLong(itemId));
-                    fillNullWithDefault(applyItem);
-                    applyItemList.add(applyItem);
-                }
-                itemIdMap.put(idPrefix, itemPrimaryId);
+        scienceApply.setApplyTime(new DateTime(item.getApplyDate()));
+        scienceApply.setDeliverTime(new DateTime(item.getSendDate()));
+        scienceApply.setSampleCount(Integer.parseInt(item.getNum()));
+        Assert.isTrue(scienceApply.getSampleCount() > 0, "同步LIS失败,样本数量不能为0");
+        scienceApply.setBeginNo(String.format("%0" + scienceApply.getSampleCount().toString().length() + "d", 1));
+
+        //HT_SCIENCE_APPLY主键非自增,为年份2位+月份2位+日期2位+483+7位序列号,为了避免冲突,这里使用480
+        String idPrefix = DateUtil.format(scienceApply.getApplyTime(), "yyMMdd") + "480";
+        long applyId = Long.parseLong(String.format("%s%07d", idPrefix, 1));
+        scienceApply.setId(applyId);
+
+        //编号起始
+        int beginNo = 1;
+        //查询当前最大的id与编号(编号格式HKYBKY20250326-14-15)
+        String sql = String.format("select top 1 id, SampleNo from dbo.HT_SCIENCE_APPLY where id like '%s%%' order by SampleNo desc ", idPrefix);
+        Entity idEntity = lisDb.queryOne(sql);
+        if (idEntity != null) {
+            applyId = idEntity.getLong("id") + 1;
+            String sampleNo = idEntity.getStr("SampleNo");
+            String[] split = sampleNo.split("-");
+            beginNo = Integer.parseInt(split[2]) + 1;
+        }
+
+        //编号结尾
+        int endNo = beginNo + scienceApply.getSampleCount() - 1;
+        scienceApply.setSampleNo(String.format("HKYBKY%s-%d-%d", item.getApplyDate().replace("-", ""), beginNo, endNo));
+        fillNullWithDefault(scienceApply);
+
+        //检测指标
+        List<HtScienceApplyItem> applyItemList = new ArrayList<>();
+        if (StrUtil.isNotBlank(item.getTarget())) {
+            //项目id,将480编号替换为481
+            idPrefix = idPrefix.replace("480", "481");
+            long itemPrimaryId = Long.parseLong(String.format("%s%07d", idPrefix, 1));
+
+            //查询当前最大的id
+            sql = String.format("select top 1 id from dbo.HT_SCIENCE_APPLY_ITEM where id like '%s%%' order by id desc ", idPrefix);
+            idEntity = lisDb.queryOne(sql);
+            if (idEntity != null) {
+                itemPrimaryId = idEntity.getLong("id") + 1;
+            }
+            log.info("同步lis数据库 HT_SCIENCE_APPLY起始id={} HT_SCIENCE_APPLY_ITEM起始id={} ", applyId, itemPrimaryId);
+
+            for (String itemId : item.getTarget().split(",")) {
+                HtScienceApplyItem applyItem = new HtScienceApplyItem();
+                applyItem.setId(itemPrimaryId++);
+                applyItem.setApplyId(scienceApply.getId());
+                applyItem.setItemId(Long.parseLong(itemId));
+                fillNullWithDefault(applyItem);
+                applyItemList.add(applyItem);
             }
         }
 
-        Assert.notEmpty(applyList, "同步LIS失败,无血清送检单");
-        Assert.notEmpty(applyItemList, "同步LIS失败,无检测指标");
-        JSONObject finalExtObj = extObj;
         lisDb.execute(db -> {
             try {
-                List<Entity> applyEntities = applyList.stream()
-                        .map(x -> {
-                            Entity entity = Entity.create("dbo.HT_SCIENCE_APPLY").parseBean(x, true, false);
-                            //sampleNo保持原样
-                            entity.remove("sample_no");
-                            entity.set("SampleNo", x.getSampleNo());
-                            return entity;
-                        })
-                        .collect(Collectors.toList());
-                db.insert(applyEntities);
+                db.insert(Entity.create("dbo.HT_SCIENCE_APPLY").parseBean(scienceApply, true, false));
                 List<Entity> applyItemEntities = applyItemList.stream().map(x -> Entity.create("dbo.HT_SCIENCE_APPLY_ITEM").parseBean(x, true, false)).collect(Collectors.toList());
                 db.insert(applyItemEntities);
             } catch (Exception e) {
@@ -233,16 +214,16 @@ public class LisSyncService {
                 throw new RuntimeException("同步LIS失败");
             }
 
-            //保存id
-            List<Long> applyIdList = applyList.stream().map(HtScienceApply::getId).collect(Collectors.toList());
-            finalExtObj.put("lisId", applyIdList);
             SampleCheckOrder co = new SampleCheckOrder();
             co.setId(checkOrder.getId());
-            co.setExtData(finalExtObj.toString());
+            JSONObject ext = StrUtil.isNotBlank(checkOrder.getExtData()) ? JSON.parseObject(checkOrder.getExtData()) : new JSONObject();
+            ext.fluentPut("lisId", scienceApply.getId()).fluentPut("lisSampleNo", scienceApply.getSampleNo());
+            co.setExtData(ext.toString());
             co.updateById();
 
             log.info("同步lis数据库完成 sampleId={} ", sampleId);
         });
+        redisTemplate.opsForValue().set(LIS_PREFIX + DateUtil.today(), redisPrefix, 1, TimeUnit.DAYS);
     }
 
     /**