新增检验项目设置中的检验类型页面并实现相关逻辑
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
package com.openhis.web.lab.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.core.common.core.controller.BaseController;
|
||||
import com.core.common.core.domain.AjaxResult;
|
||||
import com.openhis.lab.domain.InspectionType;
|
||||
import com.openhis.lab.service.IInspectionTypeService;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 检验类型管理Controller
|
||||
*
|
||||
* @author system
|
||||
* @date 2025-12-09
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/system/inspection-type")
|
||||
@Slf4j
|
||||
@AllArgsConstructor
|
||||
public class InspectionTypeController extends BaseController {
|
||||
|
||||
// 辅助方法:将字节数组转换为十六进制字符串
|
||||
private static String bytesToHex(byte[] bytes) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : bytes) {
|
||||
sb.append(String.format("%02X ", b));
|
||||
}
|
||||
return sb.toString().trim();
|
||||
}
|
||||
|
||||
private final IInspectionTypeService inspectionTypeService;
|
||||
private final TransactionTemplate transactionTemplate;
|
||||
|
||||
/**
|
||||
* 获取检验类型列表
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
public AjaxResult list(InspectionType inspectionType) {
|
||||
// 使用Wrapper构建查询条件,确保order字段被正确处理
|
||||
QueryWrapper<InspectionType> queryWrapper = new QueryWrapper<>(inspectionType);
|
||||
List<InspectionType> list = inspectionTypeService.list(queryWrapper);
|
||||
return AjaxResult.success(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取检验类型详细
|
||||
*/
|
||||
@GetMapping("/{inspectionTypeId}")
|
||||
public AjaxResult getInfo(@PathVariable Long inspectionTypeId) {
|
||||
return AjaxResult.success(inspectionTypeService.getById(inspectionTypeId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增检验类型
|
||||
*/
|
||||
@PostMapping
|
||||
public AjaxResult add(@RequestBody InspectionType inspectionType) {
|
||||
// 确保新增时ID为null,强制使用数据库自增序列
|
||||
inspectionType.setId(null);
|
||||
|
||||
// 去除code字段的前后空格,确保唯一性验证准确
|
||||
if (inspectionType.getCode() != null) {
|
||||
inspectionType.setCode(inspectionType.getCode().trim());
|
||||
}
|
||||
|
||||
// 验证code字段是否唯一
|
||||
QueryWrapper<InspectionType> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("code", inspectionType.getCode());
|
||||
|
||||
// 输出调试信息
|
||||
System.out.println("检查编码唯一性:code=" + inspectionType.getCode() + ", 长度=" + inspectionType.getCode().length());
|
||||
System.out.println("code的十六进制表示:" + bytesToHex(inspectionType.getCode().getBytes()));
|
||||
|
||||
// 直接查询具体记录,而不仅仅是计数
|
||||
List<InspectionType> existingRecords = inspectionTypeService.list(queryWrapper);
|
||||
System.out.println("数据库中存在的记录数:" + existingRecords.size());
|
||||
for (InspectionType record : existingRecords) {
|
||||
System.out.println("已存在记录:id=" + record.getId() + ", code=" + record.getCode() + ", code长度=" + record.getCode().length() + ", code十六进制=" + bytesToHex(record.getCode().getBytes()));
|
||||
}
|
||||
|
||||
if (!existingRecords.isEmpty()) {
|
||||
return AjaxResult.error("检验类型编码已存在");
|
||||
}
|
||||
|
||||
// 保存前再次验证
|
||||
System.out.println("准备保存数据:" + inspectionType);
|
||||
|
||||
try {
|
||||
// 使用事务确保一致性
|
||||
return transactionTemplate.execute(status -> {
|
||||
// 再次检查,防止并发问题
|
||||
QueryWrapper<InspectionType> checkWrapper = new QueryWrapper<>();
|
||||
checkWrapper.eq("code", inspectionType.getCode());
|
||||
List<InspectionType> finalCheck = inspectionTypeService.list(checkWrapper);
|
||||
if (!finalCheck.isEmpty()) {
|
||||
return AjaxResult.error("检验类型编码已存在");
|
||||
}
|
||||
|
||||
boolean result = inspectionTypeService.save(inspectionType);
|
||||
System.out.println("保存结果:" + result);
|
||||
return toAjax(result);
|
||||
});
|
||||
} catch (Exception e) {
|
||||
System.out.println("保存失败,错误信息:" + e.getMessage());
|
||||
|
||||
// 捕获唯一性约束冲突异常
|
||||
if (e.getMessage().contains("uk_inspection_type_code") ||
|
||||
e.getMessage().contains("duplicate key value") ||
|
||||
e.getMessage().contains("检验类型编码已存在")) {
|
||||
return AjaxResult.error("检验类型编码已存在");
|
||||
}
|
||||
|
||||
return AjaxResult.error("保存失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改检验类型
|
||||
*/
|
||||
@PutMapping
|
||||
public AjaxResult edit(@RequestBody InspectionType inspectionType) {
|
||||
// 去除code字段的前后空格,确保唯一性验证准确
|
||||
if (inspectionType.getCode() != null) {
|
||||
inspectionType.setCode(inspectionType.getCode().trim());
|
||||
}
|
||||
|
||||
// 验证code字段是否唯一(排除自身)
|
||||
QueryWrapper<InspectionType> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("code", inspectionType.getCode())
|
||||
.ne("id", inspectionType.getId());
|
||||
if (inspectionTypeService.count(queryWrapper) > 0) {
|
||||
return AjaxResult.error("检验类型编码已存在");
|
||||
}
|
||||
|
||||
try {
|
||||
boolean result = inspectionTypeService.updateById(inspectionType);
|
||||
return toAjax(result);
|
||||
} catch (Exception e) {
|
||||
// 捕获唯一性约束冲突异常
|
||||
if (e.getMessage().contains("uk_inspection_type_code") ||
|
||||
e.getMessage().contains("duplicate key value") ||
|
||||
e.getMessage().contains("检验类型编码已存在")) {
|
||||
return AjaxResult.error("检验类型编码已存在");
|
||||
}
|
||||
|
||||
return AjaxResult.error("更新失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除检验类型
|
||||
*/
|
||||
@DeleteMapping("/{inspectionTypeId}")
|
||||
public AjaxResult remove(@PathVariable Long inspectionTypeId) {
|
||||
log.info("删除检验类型,ID: {}", inspectionTypeId);
|
||||
try {
|
||||
// 检查记录是否存在
|
||||
InspectionType existing = inspectionTypeService.getById(inspectionTypeId);
|
||||
log.info("删除前检查记录是否存在: {}", existing != null);
|
||||
|
||||
if (existing == null) {
|
||||
log.warn("删除失败:检验类型ID: {} 不存在", inspectionTypeId);
|
||||
return AjaxResult.error("删除失败: 记录不存在");
|
||||
}
|
||||
|
||||
// 使用MyBatis Plus的removeById方法执行逻辑删除
|
||||
boolean result = inspectionTypeService.removeById(inspectionTypeId);
|
||||
log.info("逻辑删除检验类型结果: {}", result);
|
||||
|
||||
AjaxResult response = toAjax(result);
|
||||
log.info("删除响应: {}", response);
|
||||
return response;
|
||||
} catch (Exception e) {
|
||||
log.error("删除检验类型失败,ID: {}, 错误: {}", inspectionTypeId, e.getMessage(), e);
|
||||
return AjaxResult.error("删除失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 测试删除接口,直接返回成功
|
||||
@DeleteMapping("/test-delete/{id}")
|
||||
public AjaxResult testDelete(@PathVariable Long id) {
|
||||
log.info("测试删除接口,ID: {}", id);
|
||||
return AjaxResult.success("测试删除成功");
|
||||
}
|
||||
}
|
||||
@@ -80,6 +80,11 @@ mybatis-plus:
|
||||
mapperLocations: classpath*:mapper/**/*Mapper.xml
|
||||
# 加载全局的配置文件
|
||||
configLocation: classpath:mybatis/mybatis-config.xml
|
||||
global-config:
|
||||
db-config:
|
||||
logic-delete-field: validFlag # 全局逻辑删除的实体字段名
|
||||
logic-delete-value: 0 # 逻辑已删除值(默认为 1)
|
||||
logic-not-delete-value: 1 # 逻辑未删除值(默认为 0)
|
||||
|
||||
# PageHelper分页插件
|
||||
pagehelper:
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.openhis.lab.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableLogic;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 检验类型
|
||||
*
|
||||
* @author system
|
||||
* @date 2025-12-09
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@TableName(value = "inspection_type", autoResultMap = true)
|
||||
public class InspectionType {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 主键ID */
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/** 检验类型编码 */
|
||||
private String code;
|
||||
|
||||
/** 检验类型名称 */
|
||||
private String name;
|
||||
|
||||
/** 所属科室 */
|
||||
private String department;
|
||||
|
||||
/** 排序 */
|
||||
@TableField("\"order\"")
|
||||
private Integer sortOrder;
|
||||
|
||||
/** 备注 */
|
||||
private String remark;
|
||||
|
||||
/** 有效标志(1:有效,0:无效) */
|
||||
@TableField("valid_flag")
|
||||
@TableLogic(value = "1", delval = "0")
|
||||
private Integer validFlag;
|
||||
|
||||
/** 创建时间 */
|
||||
@TableField("created_at")
|
||||
private LocalDateTime createdAt;
|
||||
|
||||
/** 更新时间 */
|
||||
@TableField("updated_at")
|
||||
private LocalDateTime updatedAt;
|
||||
|
||||
/** 版本号 */
|
||||
private Integer version;
|
||||
|
||||
/**
|
||||
* 以下字段数据库表中不存在,用于禁用MyBatis Plus自动添加的字段
|
||||
*/
|
||||
@TableField(exist = false)
|
||||
private String createBy;
|
||||
|
||||
@TableField(exist = false)
|
||||
private String updateBy;
|
||||
|
||||
@TableField(exist = false)
|
||||
private Integer tenantId;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.openhis.lab.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.openhis.lab.domain.InspectionType;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* 检验类型Mapper接口
|
||||
*
|
||||
* @author system
|
||||
* @date 2025-12-09
|
||||
*/
|
||||
@Repository
|
||||
public interface InspectionTypeMapper extends BaseMapper<InspectionType> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.openhis.lab.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.openhis.lab.domain.InspectionType;
|
||||
|
||||
/**
|
||||
* 检验类型Service接口
|
||||
*
|
||||
* @author system
|
||||
* @date 2025-12-09
|
||||
*/
|
||||
public interface IInspectionTypeService extends IService<InspectionType> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.openhis.lab.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.openhis.lab.domain.InspectionType;
|
||||
import com.openhis.lab.mapper.InspectionTypeMapper;
|
||||
import com.openhis.lab.service.IInspectionTypeService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 检验类型Service实现类
|
||||
*
|
||||
* @author system
|
||||
* @date 2025-12-09
|
||||
*/
|
||||
@Service
|
||||
public class InspectionTypeServiceImpl extends ServiceImpl<InspectionTypeMapper, InspectionType> implements IInspectionTypeService {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user