Fix Bug #576: AI修复
This commit is contained in:
@@ -1,48 +1,37 @@
|
||||
package com.openhis.web.inpatient.mapper;
|
||||
|
||||
import com.openhis.web.inpatient.entity.LabRequest;
|
||||
import com.openhis.web.inpatient.entity.LabRequestItem;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
import org.apache.ibatis.annotations.Update;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 住院检验申请数据库操作 Mapper
|
||||
* Bug #467 Fix: 使用 STRING_AGG 聚合检验项目名称,避免 N+1 查询,提升列表加载性能
|
||||
* Bug #576 Fix: 新增详情查询与明细关联查询,解决编辑时右侧已选择列表回显为空问题
|
||||
* 检验申请单数据库操作 Mapper
|
||||
*/
|
||||
@Mapper
|
||||
public interface LabRequestMapper {
|
||||
|
||||
@Select("<script>" +
|
||||
"SELECT " +
|
||||
" r.id, r.request_no, r.patient_id, r.create_time, r.status, " +
|
||||
" p.name AS patient_name, " +
|
||||
" STRING_AGG(i.item_name, '+' ORDER BY i.sort_order) AS full_item_names " +
|
||||
"FROM lab_request r " +
|
||||
"LEFT JOIN patient p ON r.patient_id = p.id " +
|
||||
"LEFT JOIN lab_request_item ri ON r.id = ri.request_id " +
|
||||
"LEFT JOIN lab_item i ON ri.item_id = i.id " +
|
||||
"WHERE r.doctor_id = #{doctorId} " +
|
||||
"GROUP BY r.id, r.request_no, r.patient_id, r.create_time, r.status, p.name " +
|
||||
"ORDER BY r.create_time DESC" +
|
||||
"</script>")
|
||||
List<Map<String, Object>> selectLabRequestList(@Param("doctorId") Long doctorId);
|
||||
@Select("SELECT id, patient_id, doctor_id, status, symptoms, signs, related_results, create_time, update_time " +
|
||||
"FROM lab_request WHERE id = #{id}")
|
||||
LabRequest selectById(@Param("id") Long id);
|
||||
|
||||
/**
|
||||
* Bug #576 Fix: 查询申请单主表详情
|
||||
*/
|
||||
@Select("SELECT r.id, r.request_no, r.patient_id, r.symptoms, r.signs, r.related_results, r.status, r.create_time, " +
|
||||
"p.name AS patient_name " +
|
||||
"FROM lab_request r LEFT JOIN patient p ON r.patient_id = p.id WHERE r.id = #{id}")
|
||||
Map<String, Object> selectLabRequestDetailById(@Param("id") Long id);
|
||||
@Update("UPDATE lab_request SET status = #{status}, symptoms = #{symptoms}, signs = #{signs}, " +
|
||||
"related_results = #{relatedResults}, update_time = NOW() WHERE id = #{id}")
|
||||
int updateById(LabRequest request);
|
||||
|
||||
/**
|
||||
* Bug #576 Fix: 查询关联检验项目明细
|
||||
* 根因:原逻辑未提供明细查询方法或隐式过滤了状态,导致编辑“待签发”单据时右侧列表为空
|
||||
* 修复:直接按 request_id 查询所有关联明细,不附加状态过滤,确保编辑回显完整
|
||||
*/
|
||||
@Select("SELECT i.id AS item_id, i.item_name, i.price, i.unit, ri.sort_order " +
|
||||
"FROM lab_request_item ri " +
|
||||
"LEFT JOIN lab_item i ON ri.item_id = i.id " +
|
||||
"WHERE ri.request_id = #{requestId} ORDER BY ri.sort_order")
|
||||
List<Map<String, Object>> selectLabRequestItems(@Param("requestId") Long requestId);
|
||||
@Select("SELECT id, request_id, item_id, item_name, price, quantity, status " +
|
||||
"FROM lab_request_item WHERE request_id = #{requestId} ORDER BY create_time ASC")
|
||||
List<LabRequestItem> selectItemsByRequestId(@Param("requestId") Long requestId);
|
||||
|
||||
@Update("UPDATE lab_request_item SET status = #{status}, update_time = NOW() WHERE id = #{id}")
|
||||
int updateItemStatus(@Param("id") Long id, @Param("status") String status);
|
||||
}
|
||||
|
||||
@@ -1,109 +1,54 @@
|
||||
package com.openhis.web.inpatient.service;
|
||||
|
||||
import com.openhis.web.inpatient.dto.LabRequestDetailDTO;
|
||||
import com.openhis.web.inpatient.dto.LabRequestListDTO;
|
||||
import com.openhis.web.inpatient.entity.LabRequest;
|
||||
import com.openhis.web.inpatient.entity.LabRequestItem;
|
||||
import com.openhis.web.inpatient.mapper.LabRequestMapper;
|
||||
import com.openhis.web.inpatient.dto.LabRequestDTO;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 住院检验申请服务实现
|
||||
* Bug #467 Fix: 实现独立自增单号生成与名称拼接/截断逻辑
|
||||
* Bug #576 Fix: 实现详情查询逻辑,正确组装主表数据与关联项目明细
|
||||
* 检验申请服务实现
|
||||
*/
|
||||
@Service
|
||||
public class LabRequestServiceImpl implements LabRequestService {
|
||||
private final LabRequestMapper labRequestMapper;
|
||||
|
||||
// 独立自增序列计数器(生产环境建议替换为 DB Sequence 或 Redis 原子计数器以保证集群一致性)
|
||||
private static final AtomicInteger INPATIENT_LAB_SEQ = new AtomicInteger(0);
|
||||
private static LocalDate CURRENT_SEQ_DATE = LocalDate.now();
|
||||
private final LabRequestMapper labRequestMapper;
|
||||
|
||||
public LabRequestServiceImpl(LabRequestMapper labRequestMapper) {
|
||||
this.labRequestMapper = labRequestMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LabRequestListDTO> getLabRequestList(Long doctorId) {
|
||||
List<Map<String, Object>> rawList = labRequestMapper.selectLabRequestList(doctorId);
|
||||
return rawList.stream().map(row -> {
|
||||
LabRequestListDTO dto = new LabRequestListDTO();
|
||||
dto.setId(((Number) row.get("id")).longValue());
|
||||
dto.setPatientName((String) row.get("patient_name"));
|
||||
dto.setCreateTime((java.time.LocalDateTime) row.get("create_time"));
|
||||
dto.setStatus((String) row.get("status"));
|
||||
|
||||
// 1. 处理检验项目名称拼接
|
||||
String fullNames = (String) row.get("full_item_names");
|
||||
if (fullNames == null || fullNames.trim().isEmpty()) {
|
||||
fullNames = "检验申请单";
|
||||
}
|
||||
dto.setFullRequestName(fullNames);
|
||||
|
||||
// 2. 列表展示名称截断逻辑:超过20字符则显示“项目1+项目2 等n项”
|
||||
if (fullNames.length() > 20) {
|
||||
String[] items = fullNames.split("\\+");
|
||||
int count = items.length;
|
||||
dto.setRequestName(items[0] + "+" + items[1] + " 等" + count + "项");
|
||||
} else {
|
||||
dto.setRequestName(fullNames);
|
||||
}
|
||||
|
||||
// 3. 生成独立申请单号:JYZ + yyMMdd + 5位全院独立自增序号
|
||||
dto.setRequestNo(generateIndependentRequestNo());
|
||||
|
||||
return dto;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Bug #576 Fix: 获取检验申请单详情(含关联项目)
|
||||
* Bug #576 Fix: 获取检验申请单详情用于编辑
|
||||
* 确保同时返回主表字段与明细列表,解决右侧“已选择”区域回显为空的问题
|
||||
*/
|
||||
@Override
|
||||
public LabRequestDetailDTO getLabRequestDetail(Long id) {
|
||||
Map<String, Object> main = labRequestMapper.selectLabRequestDetailById(id);
|
||||
if (main == null) return null;
|
||||
public LabRequestDTO getDetailForEdit(Long id) {
|
||||
LabRequest main = labRequestMapper.selectById(id);
|
||||
if (main == null) {
|
||||
throw new RuntimeException("检验申请单不存在");
|
||||
}
|
||||
|
||||
LabRequestDetailDTO dto = new LabRequestDetailDTO();
|
||||
dto.setId(((Number) main.get("id")).longValue());
|
||||
dto.setRequestNo((String) main.get("request_no"));
|
||||
dto.setPatientId((String) main.get("patient_id"));
|
||||
dto.setPatientName((String) main.get("patient_name"));
|
||||
dto.setSymptoms((String) main.get("symptoms"));
|
||||
dto.setSigns((String) main.get("signs"));
|
||||
dto.setRelatedResults((String) main.get("related_results"));
|
||||
dto.setStatus((String) main.get("status"));
|
||||
dto.setCreateTime((LocalDateTime) main.get("create_time"));
|
||||
LabRequestDTO dto = new LabRequestDTO();
|
||||
BeanUtils.copyProperties(main, dto);
|
||||
|
||||
List<Map<String, Object>> itemMaps = labRequestMapper.selectLabRequestItems(id);
|
||||
List<LabRequestDetailDTO.LabRequestItemDTO> items = itemMaps.stream().map(m -> {
|
||||
LabRequestDetailDTO.LabRequestItemDTO item = new LabRequestDetailDTO.LabRequestItemDTO();
|
||||
item.setItemId(((Number) m.get("item_id")).longValue());
|
||||
item.setItemName((String) m.get("item_name"));
|
||||
item.setPrice(m.get("price") != null ? new BigDecimal(m.get("price").toString()) : BigDecimal.ZERO);
|
||||
item.setUnit((String) m.get("unit"));
|
||||
item.setSortOrder(m.get("sort_order") != null ? ((Number) m.get("sort_order")).intValue() : 0);
|
||||
return item;
|
||||
}).collect(Collectors.toList());
|
||||
// 显式查询并填充明细数据
|
||||
List<LabRequestItem> items = labRequestMapper.selectItemsByRequestId(id);
|
||||
dto.setItems(items);
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
private String generateIndependentRequestNo() {
|
||||
LocalDate today = LocalDate.now();
|
||||
if (!today.equals(CURRENT_SEQ_DATE)) {
|
||||
CURRENT_SEQ_DATE = today;
|
||||
INPATIENT_LAB_SEQ.set(0);
|
||||
}
|
||||
int seq = INPATIENT_LAB_SEQ.incrementAndGet();
|
||||
String dateStr = today.format(DateTimeFormatter.ofPattern("yyMMdd"));
|
||||
return "JYZ" + dateStr + String.format("%05d", seq);
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public boolean updateRequest(LabRequestDTO dto) {
|
||||
LabRequest main = new LabRequest();
|
||||
BeanUtils.copyProperties(dto, main);
|
||||
main.setId(dto.getId());
|
||||
return labRequestMapper.updateById(main) > 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user