Browse Source

动物样本血清送检lis同步默认值修改

陈长荣 1 week ago
parent
commit
9a8e17a3c2

+ 7 - 2
jfcloud-gene-biz/src/main/java/com/github/jfcloud/gene/lis/config/LisDb.java

@@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
+import java.sql.SQLException;
 import java.util.Collections;
 import java.util.List;
 import java.util.function.Consumer;
@@ -69,12 +70,16 @@ public class LisDb {
      *
      * @param consumer
      */
-    @SneakyThrows
     public void execute(Consumer<Db> consumer) {
         if (!enable) {
             log.info("LIS未启用,不执行sql语句");
             return;
         }
-        getDb().tx(consumer::accept);
+        try {
+            getDb().tx(consumer::accept);
+        } catch (SQLException e) {
+            log.error(e.getMessage(), e);
+            throw new RuntimeException(e);
+        }
     }
 }

+ 10 - 10
jfcloud-gene-biz/src/main/java/com/github/jfcloud/gene/lis/entity/HtScienceApply.java

@@ -17,7 +17,7 @@ public class HtScienceApply {
     /**
      * 机构ID,HT_INFO_HSP机构信息表 HT_INFO_DEP
      */
-    private String hspId;
+    private String hspId  = "-1";
 
     /**
      * 中心机构ID,HT_INFO_HSP机构信息表 (2101101001惠康动物检测中心)
@@ -57,17 +57,17 @@ public class HtScienceApply {
     /**
      * 创建时间
      */
-    private Date createTime;
+    private Date createTime = new Date();
 
     /**
      * 性别1002,字典
      */
-    private Integer sex;
+    private Integer sex = -1;
 
     /**
      * 年龄
      */
-    private Integer age;
+    private Integer age = -1;
 
     /**
      * 年龄单位1013,字典 0时 1天 2月 3岁
@@ -82,22 +82,22 @@ public class HtScienceApply {
     /**
      * 加急标志 0/1
      */
-    private Integer fastFlag;
+    private Integer fastFlag = 0;
 
     /**
      * 申请科室,HT_BAS_DEP_USER科室关联人员信息表
      */
-    private String applyDep;
+    private String applyDep = "-1";
 
     /**
      * 申请医生
      */
-    private String applyDoc;
+    private String applyDoc = "";
 
     /**
      * 申请医生ID,HT_INFO_USER_EXTEND人员信息扩展表
      */
-    private String applyDocId;
+    private String applyDocId = "-1";
 
     /**
      * 申请时间
@@ -107,12 +107,12 @@ public class HtScienceApply {
     /**
      * 送检人,HT_INFO_USER_EXTEND人员信息扩展表
      */
-    private String deliverOper;
+    private String deliverOper = "";
 
     /**
      * 送检人ID,HT_INFO_USER_EXTEND人员信息扩展表
      */
-    private String deliverOperId;
+    private String deliverOperId = "-1";
 
     /**
      * 送检时间

+ 69 - 38
jfcloud-gene-biz/src/main/java/com/github/jfcloud/gene/lis/service/LisSyncService.java

@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.date.DateTime;
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.lang.Assert;
+import cn.hutool.core.util.ReflectUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.db.Entity;
 import com.alibaba.fastjson.JSON;
@@ -22,9 +23,11 @@ import com.github.jfcloud.gene.sample.vo.CheckItemSerumVo;
 import com.github.jfcloud.gene.sample.vo.SampleAnimalVo;
 import com.github.jfcloud.gene.sample.vo.SampleCheckSerumVo;
 import lombok.RequiredArgsConstructor;
+import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Service;
 
+import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -81,32 +84,40 @@ public class LisSyncService {
         List<HtScienceApplyItem> applyItemList = new ArrayList<>();
         for (CheckItemSerumVo item : serum.getItems()) {
             HtScienceApply scienceApply = new HtScienceApply();
-            if (serum.getSendOrgId() != null) {
-                scienceApply.setHspId(depMap.get(serum.getSendOrgId()).getStr("hsp_id"));
-            }
             scienceApply.setPetType(item.getLisPetTypeId());
-            scienceApply.setEmail(serum.getApplyUserEmail());
-            scienceApply.setPhone(serum.getApplyUserMobile());
-            scienceApply.setCreateName(sampleInfo.getApplicantName());
             scienceApply.setCreateId(applyUser.getStr("user_id"));
-            scienceApply.setCreateTime(checkOrder.getCreateTime());
-            scienceApply.setSex(item.getLisSexId());
-            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.setSampleId(item.getLisSampleTypeId().toString());
-            scienceApply.setFastFlag(Integer.parseInt(item.getQuick()));
+            scienceApply.setCreateName(sampleInfo.getApplicantName());
+            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 (item.getLisSampleTypeId() != null) {
+                scienceApply.setSampleId(item.getLisSampleTypeId().toString());
+            }
+
+            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 (StrUtil.isNotBlank(item.getQuick())) {
+                scienceApply.setFastFlag(Integer.parseInt(item.getQuick()));
+            }
             if (serum.getSendUserId() != null) {
                 scienceApply.setApplyDocId(serum.getSendUserId().toString());
                 scienceApply.setDeliverOperId(serum.getSendUserId().toString());
@@ -136,7 +147,7 @@ public class LisSyncService {
                 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 id desc ", idPrefix);
+                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;
@@ -153,6 +164,7 @@ public class LisSyncService {
             int endNo = beginNo + scienceApply.getSampleCount() - 1;
             applyEndNoMap.put(noPrefix, endNo);
             scienceApply.setSampleNo(noPrefix + "-" + beginNo + "-" + endNo);
+            fillNullWithDefault(scienceApply);
             applyList.add(scienceApply);
 
             //检测指标
@@ -180,6 +192,7 @@ public class LisSyncService {
                     applyItem.setId(itemPrimaryId++);
                     applyItem.setApplyId(scienceApply.getId());
                     applyItem.setItemId(Long.parseLong(itemId));
+                    fillNullWithDefault(applyItem);
                     applyItemList.add(applyItem);
                 }
                 itemIdMap.put(idPrefix, itemPrimaryId);
@@ -203,22 +216,39 @@ public class LisSyncService {
                 db.insert(applyEntities);
                 List<Entity> applyItemEntities = applyItemList.stream().map(x -> Entity.create("dbo.HT_SCIENCE_APPLY_ITEM").parseBean(x, true, false)).collect(Collectors.toList());
                 db.insert(applyItemEntities);
-
-                //保存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());
-                co.updateById();
-
-                log.info("同步lis数据库完成 sampleId={} ", sampleId);
             } catch (Exception e) {
                 log.error("同步LIS失败", e);
+                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());
+            co.updateById();
+
+            log.info("同步lis数据库完成 sampleId={} ", sampleId);
         });
     }
 
+    /**
+     * 使用默认值填充对象属性为null的字段
+     */
+    @SneakyThrows
+    private void fillNullWithDefault(Object o) {
+        Object defaultObject = o.getClass().newInstance();
+
+        Field[] fields = ReflectUtil.getFields(o.getClass());
+        for (Field field : fields) {
+            field.setAccessible(true);
+            if (field.get(o) == null && field.get(defaultObject) != null) {
+                field.set(o, field.get(defaultObject));
+            }
+        }
+    }
+
     /**
      * 取消同步LIS
      */
@@ -242,15 +272,16 @@ public class LisSyncService {
                 db.execute("delete from dbo.HT_SCIENCE_APPLY where id in (" + applyIdJoin + ")");
                 db.execute("delete from dbo.HT_SCIENCE_APPLY_ITEM where apply_id in (" + applyIdJoin + ")");
                 log.info("取消同步lis数据库完成 sampleId={} ", sampleId);
-
-                extObj.remove("lisId");
-                SampleCheckOrder co = new SampleCheckOrder();
-                co.setId(checkOrder.getId());
-                co.setExtData(extObj.toString());
-                co.updateById();
             } catch (Exception e) {
                 log.error("取消同步LIS失败", e);
+                throw new RuntimeException("取消同步LIS失败");
             }
+
+            extObj.remove("lisId");
+            SampleCheckOrder co = new SampleCheckOrder();
+            co.setId(checkOrder.getId());
+            co.setExtData(extObj.toString());
+            co.updateById();
         });
     }
 }