Jelajahi Sumber

钢坯定尺配置新增修改时增加唯一校验

lingpeng.li 6 bulan lalu
induk
melakukan
a3c3bc1bfb

+ 44 - 11
zgztBus/jeecg-module-sbm/src/main/java/org/jeecg/modules/actualControl/billetActual/billetActual/controller/BilletRulerConfigController.java

@@ -21,7 +21,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.util.Arrays;
 
- /**
+/**
  * @Description: 钢坯定尺配置表
  * @Author: jeecg-boot
  * @Date:   2024-11-30
@@ -35,7 +35,7 @@ public class BilletRulerConfigController extends JeecgController<BilletRulerConf
 
 	@Autowired
 	private IBilletRulerConfigService billetRulerConfigService;
-	
+
 	/**
 	 * 分页列表查询
 	 *
@@ -57,7 +57,7 @@ public class BilletRulerConfigController extends JeecgController<BilletRulerConf
 		IPage<BilletRulerConfig> pageList = billetRulerConfigService.page(page, queryWrapper);
 		return Result.OK(pageList);
 	}
-	
+
 	/**
 	 *   添加
 	 *
@@ -65,14 +65,18 @@ public class BilletRulerConfigController extends JeecgController<BilletRulerConf
 	 * @return
 	 */
 	@AutoLog(value = "钢坯定尺配置表-添加")
-	@ApiOperation(value="钢坯定尺配置表-添加", notes="钢坯定尺配置表-添加")
+	@ApiOperation(value = "钢坯定尺配置表-添加", notes = "钢坯定尺配置表-添加")
 	@RequiresPermissions("billetRulerConfig:billet_ruler_config:add")
 	@PostMapping(value = "/add")
 	public Result<String> add(@RequestBody BilletRulerConfig billetRulerConfig) {
+		if (isLengthExists(billetRulerConfig.getLength())) {
+			return Result.error("当前定尺已经存在,请重新添加!");
+		}
+
 		billetRulerConfigService.save(billetRulerConfig);
 		return Result.OK("添加成功!");
 	}
-	
+
 	/**
 	 *  编辑
 	 *
@@ -80,14 +84,22 @@ public class BilletRulerConfigController extends JeecgController<BilletRulerConf
 	 * @return
 	 */
 	@AutoLog(value = "钢坯定尺配置表-编辑")
-	@ApiOperation(value="钢坯定尺配置表-编辑", notes="钢坯定尺配置表-编辑")
+	@ApiOperation(value = "钢坯定尺配置表-编辑", notes = "钢坯定尺配置表-编辑")
 	@RequiresPermissions("billetRulerConfig:billet_ruler_config:edit")
-	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
+	@RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
 	public Result<String> edit(@RequestBody BilletRulerConfig billetRulerConfig) {
+
+		// 校验是否存在重复的 length 值(排除当前记录自身)
+		if (isLengthExists(billetRulerConfig.getLength(), billetRulerConfig.getId())) {
+			return Result.error("当前定尺已经存在,请重新修改!");
+		}
+
+		// 更新记录
 		billetRulerConfigService.updateById(billetRulerConfig);
-		return Result.OK("编辑成功!");
+
+		return Result.OK("编辑成功!");
 	}
-	
+
 	/**
 	 *   通过id删除
 	 *
@@ -102,7 +114,7 @@ public class BilletRulerConfigController extends JeecgController<BilletRulerConf
 		billetRulerConfigService.removeById(id);
 		return Result.OK("删除成功!");
 	}
-	
+
 	/**
 	 *  批量删除
 	 *
@@ -117,7 +129,7 @@ public class BilletRulerConfigController extends JeecgController<BilletRulerConf
 		this.billetRulerConfigService.removeByIds(Arrays.asList(ids.split(",")));
 		return Result.OK("批量删除成功!");
 	}
-	
+
 	/**
 	 * 通过id查询
 	 *
@@ -160,4 +172,25 @@ public class BilletRulerConfigController extends JeecgController<BilletRulerConf
         return super.importExcel(request, response, BilletRulerConfig.class);
     }
 
+
+	private boolean isLengthExists(Integer length) {
+		return billetRulerConfigService.lambdaQuery()
+				.eq(BilletRulerConfig::getLength, length)
+				.exists();
+	}
+
+	/**
+	 * 校验是否存在重复的 length 值(可排除特定 ID)。
+	 *
+	 * @param length 定尺值
+	 * @param excludeId 排除的记录 ID
+	 * @return true 如果存在重复的 length 值
+	 */
+	private boolean isLengthExists(Integer length, String excludeId) {
+		return billetRulerConfigService.lambdaQuery()
+				.eq(BilletRulerConfig::getLength, length)
+				.ne(excludeId != null, BilletRulerConfig::getId, excludeId)
+				.exists();
+	}
+
 }