feat(巡店删除): 建单删除条件补形状校验——挡"服务端收下、执行器读出零条件"的静默不删
只校形状不校取值:取值枚举化要跨三张脸且受存量装机版本分布约束,已另挂 docs/audit/BACKLOG.md B2 的四步顺序锁。 六键取文与客户端 _normalize_delete_condition_texts 逐字对齐(后端少认一键就是静默不删的成因), 按 ,,;;、\r\n 拆分去重、单条 ≤500 对齐 V41 的 condition_text 列、条数 ≤100;addCondition 一字未动。 反向对照钉在用例里:删除商品信息草稿/库存状态/草稿 这些真在用的自由措辞必须放行。 表结构零改动(条件不落库;结果走 biz_file_task.result_json 与 biz_task_result_item.payload_json)。 测试:新类 12/12(surefire tests=12 failures=0);后端整量 4381 passed/0 failed/7 skipped rc=0 ——注意整量是在含另路未提交件的工作树上跑的,本笔只含 patroldelete 两支且不依赖那些件。
This commit is contained in:
+68
@@ -67,6 +67,18 @@ public class PatrolDeleteTaskService {
|
||||
private static final int RESULT_SUCCESS = 1;
|
||||
private static final String INTERRUPTED_MESSAGE = "Python 在该店铺结果提交完成前中断";
|
||||
|
||||
/**
|
||||
* 删除条件的取文键,与客户端 patrol_delete.py 的 _normalize_delete_condition_texts 逐字对齐:
|
||||
* 后端少认一个键就会出现"服务端收下、执行器读出零条件、任务停在执行中"的静默不删。
|
||||
*/
|
||||
private static final List<String> DELETE_CONDITION_TEXT_KEYS = List.of(
|
||||
"conditionText", "condition_text", "status", "text", "label", "name");
|
||||
/** 一条载荷里可以塞多条条件,拆分符与客户端的 re.split(r"[,,;;、\r\n]+") 同形。 */
|
||||
private static final String DELETE_CONDITION_SPLIT_REGEX = "[,,;;、\\r\\n]+";
|
||||
/** 单条上限取 V41__patrol_delete_condition.sql 里 condition_text VARCHAR(500) 的同档值。 */
|
||||
private static final int DELETE_CONDITION_MAX_TEXT_LENGTH = 500;
|
||||
private static final int DELETE_CONDITION_MAX_ENTRIES = 100;
|
||||
|
||||
private final FileTaskMapper fileTaskMapper;
|
||||
private final FileResultMapper fileResultMapper;
|
||||
private final PatrolDeleteResolveService patrolDeleteResolveService;
|
||||
@@ -246,6 +258,9 @@ public class PatrolDeleteTaskService {
|
||||
if (request.getDeleteConditions() == null || request.getDeleteConditions().isEmpty()) {
|
||||
throw new BusinessException("删除条件不能为空,请先选择删除条件");
|
||||
}
|
||||
// 空判只挡住"一条都没发",挡不住"发了但执行器读不出文本"(如只带 id 的对象)——
|
||||
// 那种载荷服务端照收、任务建到 RUNNING 后 Python 无事可做,与上面注释要避免的是同一事故。
|
||||
normalizeDeleteConditionTexts(request.getDeleteConditions());
|
||||
|
||||
List<PatrolDeleteTaskItemDto> uniqueItems = dedupeItems(request.getItems());
|
||||
if (uniqueItems.isEmpty()) {
|
||||
@@ -1196,6 +1211,59 @@ public class PatrolDeleteTaskService {
|
||||
return blank(value) ? null : value.trim();
|
||||
}
|
||||
|
||||
/**
|
||||
* 只校形状、不校取值。删除条件仍是用户自由输入、由执行器按双向包含匹配,在这里枚举化会把
|
||||
* 已经在用的措辞(例如"删除商品信息草稿")当场打挂;取值收口需先让执行器认得新代码并出包
|
||||
* 覆盖存量装机,见 docs/audit/BACKLOG.md 的 B2 顺序锁。
|
||||
*
|
||||
* <p>形状判据挡的是另一类事故:结构不对时服务端返回成功、执行器却读出零条件,任务停在执行中。</p>
|
||||
*/
|
||||
static List<String> normalizeDeleteConditionTexts(List<Object> deleteConditions) {
|
||||
List<String> texts = new ArrayList<>();
|
||||
if (deleteConditions == null || deleteConditions.isEmpty()) {
|
||||
// "一条都没发"归建单侧那道空判管;这里只管"发了但执行器读不出文本"的那一类。
|
||||
return texts;
|
||||
}
|
||||
for (Object condition : deleteConditions) {
|
||||
String raw = extractDeleteConditionText(condition);
|
||||
for (String part : raw.split(DELETE_CONDITION_SPLIT_REGEX)) {
|
||||
String text = part.trim();
|
||||
if (text.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
if (text.length() > DELETE_CONDITION_MAX_TEXT_LENGTH) {
|
||||
throw new BusinessException("删除条件过长(单条上限 " + DELETE_CONDITION_MAX_TEXT_LENGTH + " 字)");
|
||||
}
|
||||
if (!texts.contains(text)) {
|
||||
texts.add(text);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (texts.isEmpty()) {
|
||||
throw new BusinessException("删除条件不能为空,请先选择删除条件");
|
||||
}
|
||||
if (texts.size() > DELETE_CONDITION_MAX_ENTRIES) {
|
||||
throw new BusinessException("删除条件过多(上限 " + DELETE_CONDITION_MAX_ENTRIES + " 条)");
|
||||
}
|
||||
return texts;
|
||||
}
|
||||
|
||||
/** 取这条条件里的文本;取不到就判错,不留成"合法的空条件"被服务端收下。 */
|
||||
private static String extractDeleteConditionText(Object condition) {
|
||||
if (condition instanceof String text) {
|
||||
return text;
|
||||
}
|
||||
if (condition instanceof Map<?, ?> map) {
|
||||
for (String key : DELETE_CONDITION_TEXT_KEYS) {
|
||||
if (map.get(key) instanceof String text && !text.isBlank()) {
|
||||
return text;
|
||||
}
|
||||
}
|
||||
throw new BusinessException("删除条件缺少可识别的文本,请重新输入删除条件");
|
||||
}
|
||||
throw new BusinessException("删除条件格式不合法,请重新输入删除条件");
|
||||
}
|
||||
|
||||
private void validateUserId(Long userId) {
|
||||
if (userId == null || userId <= 0) {
|
||||
throw new BusinessException("user_id 不合法");
|
||||
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
package com.nanri.aiimage.modules.patroldelete.service;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* 建单侧删除条件的**形状**判据(不校取值,取值收口见 docs/audit/BACKLOG.md 的 B2)。
|
||||
*
|
||||
* <p>这格为什么值得钉:执行器读条件时按六键取文、再按双向包含匹配,所以"服务端返回成功"与
|
||||
* "执行器真的拿到了可执行条件"是两件事。只带 id 的对象、数字、布尔都能过空判,最后表现成
|
||||
* 任务停在执行中且一个商品都没删——正是既有那句空判注释要防的形状,只是它防不到这一类。</p>
|
||||
*/
|
||||
class PatrolDeleteTaskServiceDeleteConditionTest {
|
||||
|
||||
@Test
|
||||
void 用户端现行形状应原样取到文本() {
|
||||
List<String> texts = PatrolDeleteTaskService.normalizeDeleteConditionTexts(
|
||||
List.of(Map.of("id", "定价问题", "conditionText", "定价问题")));
|
||||
assertEquals(List.of("定价问题"), texts);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 六个取文键各自都能被认出() {
|
||||
for (String key : List.of("conditionText", "condition_text", "status", "text", "label", "name")) {
|
||||
List<String> texts = PatrolDeleteTaskService.normalizeDeleteConditionTexts(
|
||||
List.of(Map.of(key, "需要批准")));
|
||||
assertEquals(List.of("需要批准"), texts, key + " 是执行器认得的键,后端不许比它少认");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void 一条载荷里的多个条件要拆开() {
|
||||
List<String> texts = PatrolDeleteTaskService.normalizeDeleteConditionTexts(
|
||||
List.of("定价问题,需要批准、缺少报价;在售"));
|
||||
assertEquals(List.of("定价问题", "需要批准", "缺少报价", "在售"), texts);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 重复条件去重后只留一条() {
|
||||
List<String> texts = PatrolDeleteTaskService.normalizeDeleteConditionTexts(
|
||||
List.of("定价问题", "定价问题", " 定价问题 "));
|
||||
assertEquals(1, texts.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 自由文本措辞不许被取值判据拒掉() {
|
||||
// 今天真机在用的自然语言措辞:执行器靠双向包含命中草稿开关与状态名。
|
||||
// 枚举化会把这些打挂,所以这几条必须绿——它们是 B2 顺序锁的反例证据。
|
||||
for (String wording : List.of("删除商品信息草稿", "库存状态", "草稿", "商品信息草稿")) {
|
||||
List<String> texts = PatrolDeleteTaskService.normalizeDeleteConditionTexts(
|
||||
List.of(Map.of("conditionText", wording)));
|
||||
assertEquals(List.of(wording), texts, "形状校验不得收窄既有自由措辞:" + wording);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void 单条正好到上限仍放行再长一字判红() {
|
||||
String atLimit = "问".repeat(500);
|
||||
assertEquals(1, PatrolDeleteTaskService.normalizeDeleteConditionTexts(List.of(atLimit)).size());
|
||||
|
||||
BusinessException over = assertThrows(BusinessException.class,
|
||||
() -> PatrolDeleteTaskService.normalizeDeleteConditionTexts(List.of("问".repeat(501))));
|
||||
assertTrue(over.getMessage().contains("删除条件过长"), over.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 超过条数上限判红() {
|
||||
List<Object> many = new ArrayList<>();
|
||||
for (int i = 0; i < 101; i++) {
|
||||
many.add("条件" + i);
|
||||
}
|
||||
BusinessException tooMany = assertThrows(BusinessException.class,
|
||||
() -> PatrolDeleteTaskService.normalizeDeleteConditionTexts(many));
|
||||
assertTrue(tooMany.getMessage().contains("删除条件过多"), tooMany.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 只带id的对象必须当场判红而不是收下() {
|
||||
Map<String, Object> idOnly = new LinkedHashMap<>();
|
||||
idOnly.put("id", "x");
|
||||
BusinessException missing = assertThrows(BusinessException.class,
|
||||
() -> PatrolDeleteTaskService.normalizeDeleteConditionTexts(List.of(idOnly)));
|
||||
assertTrue(missing.getMessage().contains("缺少可识别的文本"), missing.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 取文键存在但全是空白也判红() {
|
||||
BusinessException blank = assertThrows(BusinessException.class,
|
||||
() -> PatrolDeleteTaskService.normalizeDeleteConditionTexts(List.of(Map.of("conditionText", " "))));
|
||||
assertTrue(blank.getMessage().contains("缺少可识别的文本"), blank.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 非字符串非对象的条件判为格式不合法() {
|
||||
for (Object bad : List.of(1, true, List.of("定价问题"))) {
|
||||
BusinessException illegal = assertThrows(BusinessException.class,
|
||||
() -> PatrolDeleteTaskService.normalizeDeleteConditionTexts(List.of(bad)));
|
||||
assertTrue(illegal.getMessage().contains("格式不合法"), illegal.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void 全是分隔符与空白时按空条件判红() {
|
||||
BusinessException empty = assertThrows(BusinessException.class,
|
||||
() -> PatrolDeleteTaskService.normalizeDeleteConditionTexts(List.of(",、;,")));
|
||||
assertTrue(empty.getMessage().contains("删除条件不能为空"), empty.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void null与空清单返回空表由建单侧既有空判兜住() {
|
||||
assertTrue(PatrolDeleteTaskService.normalizeDeleteConditionTexts(null).isEmpty());
|
||||
assertTrue(PatrolDeleteTaskService.normalizeDeleteConditionTexts(List.of()).isEmpty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user