|
@@ -1,6 +1,8 @@
|
|
|
package com.github.jfcloud.gene.lis.service;
|
|
|
|
|
|
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.StrUtil;
|
|
|
import cn.hutool.db.Entity;
|
|
@@ -11,12 +13,9 @@ import com.github.jfcloud.gene.lis.config.LisDb;
|
|
|
import com.github.jfcloud.gene.lis.entity.HtScienceApply;
|
|
|
import com.github.jfcloud.gene.lis.entity.HtScienceApplyItem;
|
|
|
import com.github.jfcloud.gene.sample.entity.SampleCheckOrder;
|
|
|
-import com.github.jfcloud.gene.sample.entity.SampleInfo;
|
|
|
import com.github.jfcloud.gene.sample.enums.ScientificSampleTypeEnum;
|
|
|
import com.github.jfcloud.gene.sample.mapper.SampleCheckOrderMapper;
|
|
|
-import com.github.jfcloud.gene.sample.mapper.SampleInfoMapper;
|
|
|
import com.github.jfcloud.gene.sample.service.SampleAnimalService;
|
|
|
-import com.github.jfcloud.gene.sample.service.SampleCheckItemSerumService;
|
|
|
import com.github.jfcloud.gene.sample.vo.CheckItemSerumVo;
|
|
|
import com.github.jfcloud.gene.sample.vo.SampleAnimalVo;
|
|
|
import com.github.jfcloud.gene.sample.vo.SampleCheckSerumVo;
|
|
@@ -24,8 +23,8 @@ import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import java.time.format.DateTimeFormatter;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.function.Function;
|
|
@@ -37,10 +36,8 @@ import java.util.stream.Collectors;
|
|
|
public class LisSyncService {
|
|
|
|
|
|
private final LisDb lisDb;
|
|
|
- private final SampleInfoMapper sampleInfoMapper;
|
|
|
private final SampleCheckOrderMapper checkOrderMapper;
|
|
|
private final SampleAnimalService sampleAnimalService;
|
|
|
- private final SampleCheckItemSerumService sampleCheckItemSerumService;
|
|
|
|
|
|
/**
|
|
|
* 同步LIS
|
|
@@ -61,41 +58,25 @@ public class LisSyncService {
|
|
|
|
|
|
log.info("开始同步lis数据库 sample={}", sampleId);
|
|
|
|
|
|
- //HT_SCIENCE_APPLY主键非自增,为年份2位+月份2位+日期2位+483+7位序列号,为了避免冲突,这里使用480
|
|
|
- SampleInfo sampleInfo = sampleInfoMapper.selectById(sampleId);
|
|
|
- String idPrefix = sampleInfo.getApplyTime().format(DateTimeFormatter.ofPattern("yyMMdd")) + "480";
|
|
|
- long firstId = Long.parseLong(String.format("%s%07d", idPrefix, 1));
|
|
|
- long itemFirstId = Long.parseLong(String.valueOf(firstId).replace("480", "481"));
|
|
|
- //编号起始与结尾
|
|
|
- int beginNo = 1;
|
|
|
- int endNo = 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);
|
|
|
- Entity idEntity = lisDb.queryOne(sql);
|
|
|
- if (idEntity != null) {
|
|
|
- firstId = idEntity.getLong("id") + 1;
|
|
|
- String sampleNo = idEntity.getStr("SampleNo");
|
|
|
- String[] split = sampleNo.split("-");
|
|
|
- beginNo = Integer.parseInt(split[2]) + 1;
|
|
|
- }
|
|
|
- log.info("同步lis数据库 HT_SCIENCE_APPLY起始id={} HT_SCIENCE_APPLY_ITEM起始id={} ", firstId, itemFirstId);
|
|
|
-
|
|
|
- //查询当天的样本编号
|
|
|
- String noPrefix = "HKYBKY" + sampleInfo.getApplyTime().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
|
|
|
-
|
|
|
//查询科室表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()));
|
|
|
|
|
|
//根据昵称查询用户id
|
|
|
- Entity applyUser = lisDb.queryOne("select top 1 user_id from dbo.HT_INFO_USER_EXTEND where name = '" + serum.getApplyUserName() + "' order by user_id desc");
|
|
|
+ Entity applyUser = lisDb.queryOne("select top 1 user_id from dbo.HT_INFO_USER_EXTEND where name = N'" + serum.getApplyUserName() + "' order by user_id desc");
|
|
|
+ Assert.notNull(applyUser, "同步LIS失败,申请人姓名[{}]不存在LIS系统", serum.getApplyUserName());
|
|
|
+
|
|
|
+ //记录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.setId(firstId++);
|
|
|
if (serum.getSendOrgId() != null) {
|
|
|
scienceApply.setHspId(depMap.get(serum.getSendOrgId()).getStr("hsp_id"));
|
|
|
}
|
|
@@ -111,28 +92,78 @@ public class LisSyncService {
|
|
|
scienceApply.setApplyDep(serum.getSendOrgId().toString());
|
|
|
scienceApply.setApplyDocId(serum.getSendUserId().toString());
|
|
|
scienceApply.setApplyDoc(serum.getSendUserName());
|
|
|
- scienceApply.setApplyTime(serum.getApplyDate());
|
|
|
+ scienceApply.setApplyTime(new DateTime(item.getApplyDate()));
|
|
|
scienceApply.setDeliverOper(serum.getSendUserName());
|
|
|
scienceApply.setDeliverOperId(serum.getSendUserId().toString());
|
|
|
- scienceApply.setDeliverTime(serum.getApplyDate());
|
|
|
- scienceApply.setSampleCount(Integer.parseInt(item.getCodeSuffix()));
|
|
|
+ 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));
|
|
|
+ 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 id 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ scienceApply.setId(applyId);
|
|
|
+ applyIdMap.put(idPrefix, applyId);
|
|
|
|
|
|
- endNo = beginNo + scienceApply.getSampleCount() - 1;
|
|
|
+ //编号结尾
|
|
|
+ int endNo = beginNo + scienceApply.getSampleCount() - 1;
|
|
|
+ applyEndNoMap.put(noPrefix, endNo);
|
|
|
scienceApply.setSampleNo(noPrefix + "-" + beginNo + "-" + endNo);
|
|
|
applyList.add(scienceApply);
|
|
|
|
|
|
//检测指标
|
|
|
if (StrUtil.isNotBlank(item.getTarget())) {
|
|
|
- List<Long> itemIds = JSON.parseArray(item.getTarget(), Long.class);
|
|
|
- for (Long itemId : itemIds) {
|
|
|
+ //项目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(itemFirstId++);
|
|
|
+ applyItem.setId(itemPrimaryId++);
|
|
|
applyItem.setApplyId(scienceApply.getId());
|
|
|
- applyItem.setItemId(itemId);
|
|
|
+ applyItem.setItemId(Long.parseLong(itemId));
|
|
|
applyItemList.add(applyItem);
|
|
|
}
|
|
|
+ itemIdMap.put(idPrefix, itemPrimaryId);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -141,9 +172,17 @@ public class LisSyncService {
|
|
|
JSONObject finalExtObj = extObj;
|
|
|
lisDb.execute(db -> {
|
|
|
try {
|
|
|
- List<Entity> applyEntities = applyList.stream().map(x -> Entity.create("dbo.HT_SCIENCE_APPLY").parseBean(x)).collect(Collectors.toList());
|
|
|
+ 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);
|
|
|
- List<Entity> applyItemEntities = applyItemList.stream().map(x -> Entity.create("dbo.HT_SCIENCE_APPLY_ITEM").parseBean(x)).collect(Collectors.toList());
|
|
|
+ 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
|
|
@@ -184,6 +223,12 @@ 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);
|
|
|
}
|