|
|
@@ -1,15 +1,27 @@
|
|
|
package com.fs.his.service.impl;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
|
+import com.fs.his.domain.FsHealthRecord;
|
|
|
+import com.fs.his.dto.HealthHistoryBlockDTO;
|
|
|
+import com.fs.his.dto.HealthHistoryQuestionDTO;
|
|
|
+import com.fs.his.mapper.FsHealthRecordMapper;
|
|
|
import com.fs.his.param.FsHealthRecordListParam;
|
|
|
+import com.fs.his.service.IFsHealthRecordService;
|
|
|
import com.fs.his.vo.FsHealthRecordListPVO;
|
|
|
+import com.fs.his.vo.HealthRecordHistoryChangeVO;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import com.fs.his.mapper.FsHealthRecordMapper;
|
|
|
-import com.fs.his.domain.FsHealthRecord;
|
|
|
-import com.fs.his.service.IFsHealthRecordService;
|
|
|
|
|
|
/**
|
|
|
* 健康档案Service业务层处理
|
|
|
@@ -17,6 +29,7 @@ import com.fs.his.service.IFsHealthRecordService;
|
|
|
* @author fs
|
|
|
* @date 2024-09-02
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class FsHealthRecordServiceImpl implements IFsHealthRecordService
|
|
|
{
|
|
|
@@ -102,6 +115,124 @@ public class FsHealthRecordServiceImpl implements IFsHealthRecordService
|
|
|
return fsHealthRecordMapper.selectFsHealthRecordByUserId(userId);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public List<FsHealthRecord> selectFsHealthRecordHistoryByUserId(Long userId) {
|
|
|
+ return fsHealthRecordMapper.selectFsHealthRecordHistoryByUserId(userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<HealthRecordHistoryChangeVO> selectHealthRecordHistoryChanges(Long userId) {
|
|
|
+ List<FsHealthRecord> historyList = selectFsHealthRecordHistoryByUserId(userId);
|
|
|
+ if (historyList == null || historyList.size() < 2) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按id升序,相邻两条做对比
|
|
|
+ historyList.sort(Comparator.comparing(FsHealthRecord::getId, Comparator.nullsLast(Long::compareTo)));
|
|
|
+
|
|
|
+ List<HealthRecordHistoryChangeVO> changeList = new ArrayList<>();
|
|
|
+
|
|
|
+ // 先锁定一对新旧版本
|
|
|
+ for (int i = 1; i < historyList.size(); i++) {
|
|
|
+ // 倒叙查询的,所以最大的索引是最老的数据
|
|
|
+ FsHealthRecord oldRecord = historyList.get(i - 1);
|
|
|
+ FsHealthRecord newRecord = historyList.get(i);
|
|
|
+
|
|
|
+ List<HealthHistoryBlockDTO> oldTempList = Collections.emptyList();
|
|
|
+ List<HealthHistoryBlockDTO> newTempList = Collections.emptyList();
|
|
|
+ try {
|
|
|
+ if (StrUtil.isNotBlank(oldRecord.getHealthHistory())) {
|
|
|
+ oldTempList = JSON.parseArray(oldRecord.getHealthHistory(), HealthHistoryBlockDTO.class);
|
|
|
+ }
|
|
|
+ if (StrUtil.isNotBlank(newRecord.getHealthHistory())) {
|
|
|
+ newTempList = JSON.parseArray(newRecord.getHealthHistory(), HealthHistoryBlockDTO.class);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("解析 health_history 失败, recordId={}/{}: {}", oldRecord.getId(), newRecord.getId(), e.getMessage());
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (oldTempList == null || oldTempList.isEmpty() || newTempList == null || newTempList.isEmpty()) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 旧版模板:id -> 模板
|
|
|
+ Map<Integer, HealthHistoryBlockDTO> oldTempMap = new HashMap<>();
|
|
|
+ for (HealthHistoryBlockDTO oldTemp : oldTempList) {
|
|
|
+ if (oldTemp != null && oldTemp.getId() != null) {
|
|
|
+ oldTempMap.put(oldTemp.getId(), oldTemp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 再按照模板对比
|
|
|
+ for (HealthHistoryBlockDTO newTemp : newTempList) {
|
|
|
+ if (newTemp == null || newTemp.getId() == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ HealthHistoryBlockDTO oldTemp = oldTempMap.get(newTemp.getId());
|
|
|
+ if (oldTemp == null) {
|
|
|
+ // 新模板,忽略
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 旧版题目:name -> writeVal
|
|
|
+ Map<String, String> oldAnswerMap = new HashMap<>();
|
|
|
+ if (oldTemp.getQuestion() != null) {
|
|
|
+ for (HealthHistoryQuestionDTO oldItem : oldTemp.getQuestion()) {
|
|
|
+ if (oldItem != null && StrUtil.isNotBlank(oldItem.getName())) {
|
|
|
+ oldAnswerMap.put(oldItem.getName(), oldItem.getWriteVal() == null ? "" : oldItem.getWriteVal());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (newTemp.getQuestion() == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 再对比具体的值,和组装数据
|
|
|
+ for (HealthHistoryQuestionDTO newItem : newTemp.getQuestion()) {
|
|
|
+ if (newItem == null || StrUtil.isBlank(newItem.getName())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (!oldAnswerMap.containsKey(newItem.getName())) {
|
|
|
+ // 新题目
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String oldValue = oldAnswerMap.get(newItem.getName());
|
|
|
+ String newValue = newItem.getWriteVal() == null ? "" : newItem.getWriteVal();
|
|
|
+ if (Objects.equals(oldValue, newValue)) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ HealthRecordHistoryChangeVO change = new HealthRecordHistoryChangeVO();
|
|
|
+ change.setId(newRecord.getId());
|
|
|
+ change.setTempleName(newTemp.getName());
|
|
|
+ change.setOperateTime(newRecord.getCreateTime());
|
|
|
+ change.setOperateStr(newItem.getName() + "由'" + oldValue + "'修改为'" + newValue + "'");
|
|
|
+ changeList.add(change);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 最新变更在前
|
|
|
+ Collections.reverse(changeList);
|
|
|
+ return changeList;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Integer selectMaxVersionByUserId(Long userId) {
|
|
|
+ return fsHealthRecordMapper.selectMaxVersionByUserId(userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int insertFsHealthRecordNewVersion(FsHealthRecord fsHealthRecord, Long userId) {
|
|
|
+ FsHealthRecord existing = selectFsHealthRecordByUserId(userId);
|
|
|
+ if (existing == null) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ fsHealthRecord.setId(null);
|
|
|
+ fsHealthRecord.setUserId(userId);
|
|
|
+ Integer maxVersion = selectMaxVersionByUserId(userId);
|
|
|
+ fsHealthRecord.setVersion(maxVersion + 1);
|
|
|
+ return insertFsHealthRecord(fsHealthRecord);
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public List<FsHealthRecordListPVO> selectFsHealthRecordListVO(FsHealthRecordListParam fsHealthRecord) {
|
|
|
return fsHealthRecordMapper.selectFsHealthRecordListVO(fsHealthRecord);
|