feat(跟价多状态): 全量跟价支持勾选多个商品状态,取值以派发落库值为准

原来「按商品状态(全量)」写死在售。现在用户端可勾选 在售/缺少的报价/定价问题
(默认只勾在售),勾几个跑几个,状态清单随循环任务落库并在每次派发子任务时下发。

- 载荷的 mode 与状态清单都取派发上下文(childTaskRequest),不再读界面当前勾选:
  循环跑到第 N 轮时用户可能已经改过勾选,用界面值会把没勾的状态跑出去。
- listingStatuses 白名单 Active/PricingIssue/MissingOffer 三值,校验放在 try 之外——
  try 里的 catch 会把「不支持的商品状态」改写成「循环任务序列化失败」,那是句假话。
- V159 给 biz_price_track_loop_run 加 listing_statuses_json(可空=本特性之前的行=在售)。
- 状态标识用我们自己的键 MissingOffer,页面真实取值是 OfferMissing(2026-10-17 黄建新
  英国站真机实锤),两者不同形是有意的:改键名会断载荷兼容。
This commit is contained in:
2026-10-17 00:07:31 +08:00
parent 6b1105f841
commit 8a20f85ca6
14 changed files with 384 additions and 7 deletions
@@ -19,6 +19,11 @@ public class PriceTrackCreateTaskRequest {
@Schema(description = "是否启用按商品状态处理模式")
private boolean statusMode;
@Schema(description = "statusMode 下要跑的商品状态清单(Active/PricingIssue/MissingOffer);"
+ "为空时客户端按在售执行,与未升级的存量载荷等价",
example = "[\"Active\",\"PricingIssue\"]")
private List<String> listingStatuses;
@Schema(description = "是否启用按指定 ASIN 文件处理模式")
private boolean asinMode;
@@ -20,6 +20,8 @@ public class PriceTrackLoopRunCreateRequest {
private boolean statusMode;
private List<String> listingStatuses = new ArrayList<>();
private boolean asinMode;
@NotEmpty(message = "items不能为空")
@@ -22,6 +22,8 @@ public class PriceTrackLoopRunEntity {
private Integer currentRound;
private Integer currentShopIndex;
private Boolean statusMode;
/** 建单时勾选的商品状态清单(JSON 数组,V159)。NULL=这条记录早于多状态功能,按在售跑。 */
private String listingStatusesJson;
private Boolean asinMode;
private String asinFilesJson;
private String countryCodesJson;
@@ -9,6 +9,7 @@ import java.util.List;
public class PriceTrackCreateTaskRequestVo {
private Long userId;
private boolean statusMode;
private List<String> listingStatuses = new ArrayList<>();
private boolean asinMode;
private List<PriceTrackMatchShopsVo.PriceTrackShopQueueItem> items = new ArrayList<>();
private List<String> asinFiles = new ArrayList<>();
@@ -22,6 +22,7 @@ public class PriceTrackLoopRunVo {
private Boolean stopRequested;
private String errorMessage;
private Boolean statusMode;
private List<String> listingStatuses = new ArrayList<>();
private Boolean asinMode;
private List<String> asinFiles = new ArrayList<>();
private List<String> countryCodes = new ArrayList<>();
@@ -38,6 +38,13 @@ public class PriceTrackLoopRunService {
private static final String STATUS_FAILED = "FAILED";
private static final String STATUS_STOPPED = "STOPPED";
private static final String EXECUTION_MODE_FINITE = "FINITE";
/**
* 跟价界面上可勾选的商品状态(2026-10-16 多状态立项)。取值以客户端
* {@code amazon_base_v2.LISTING_STATUS_CATALOG} 为准,这里只挡清单外的 junk——
* 不让它存进循环记录、再派发给客户端去猜页面。
*/
private static final List<String> ALLOWED_LISTING_STATUSES =
List.of("Active", "PricingIssue", "MissingOffer");
private static final String EXECUTION_MODE_INFINITE = "INFINITE";
/** 客户端异常中断的任务 errorMessage 前缀(TaskHeartbeatService.markInterrupted 写入)。 */
private static final String CLIENT_INTERRUPT_ERROR_PREFIX = "客户端异常中断";
@@ -60,6 +67,9 @@ public class PriceTrackLoopRunService {
if (items.isEmpty()) {
throw new BusinessException("items不能为空");
}
// 状态清单的校验必须在下面那个 try 之外:try 里的 catch 会把任何异常改写成
// 「循环任务序列化失败」,那等于对人说"勾了个不支持的状态"是 Jackson 的锅。
List<String> listingStatuses = normalizeListingStatuses(request);
PriceTrackLoopRunEntity entity = new PriceTrackLoopRunEntity();
entity.setUserId(request.getUserId());
entity.setStatus(STATUS_RUNNING);
@@ -77,6 +87,7 @@ public class PriceTrackLoopRunService {
try {
entity.setAsinFilesJson(objectMapper.writeValueAsString(request.getAsinFiles() == null ? List.of() : request.getAsinFiles()));
entity.setCountryCodesJson(objectMapper.writeValueAsString(request.getCountryCodes()));
entity.setListingStatusesJson(objectMapper.writeValueAsString(listingStatuses));
entity.setShopsJson(objectMapper.writeValueAsString(items));
} catch (Exception ex) {
throw new BusinessException("循环任务序列化失败");
@@ -129,6 +140,9 @@ public class PriceTrackLoopRunService {
PriceTrackCreateTaskRequestVo childRequest = new PriceTrackCreateTaskRequestVo();
childRequest.setUserId(entity.getUserId());
childRequest.setStatusMode(Boolean.TRUE.equals(entity.getStatusMode()));
// 逐轮派发的子任务必须吃循环建单时勾的状态,不能吃界面当下的勾选态——
// 页面刷新后本地勾选会回到默认(只有在售),INFINITE 循环的后续轮就会少跑状态。
childRequest.setListingStatuses(parseStringList(entity.getListingStatusesJson()));
childRequest.setAsinMode(Boolean.TRUE.equals(entity.getAsinMode()));
childRequest.setAsinFiles(parseStringList(entity.getAsinFilesJson()));
childRequest.setCountryCodes(parseStringList(entity.getCountryCodesJson()));
@@ -485,6 +499,30 @@ public class PriceTrackLoopRunService {
return entity;
}
/**
* 清洗并校验勾选的商品状态。
*
* <p>空清单**不判错**:未升级的用户端不会带这个字段,那时按在售跑(客户端默认值),
* 后端先于前端发布也不会把建单打挂。"至少要勾一个"由界面保证,服务端只守取值白名单。
*/
private List<String> normalizeListingStatuses(PriceTrackLoopRunCreateRequest request) {
List<String> picked = request.getListingStatuses() == null ? List.of() : request.getListingStatuses();
List<String> cleaned = new ArrayList<>();
for (String status : picked) {
String key = status == null ? "" : status.trim();
if (key.isEmpty()) {
continue;
}
if (!ALLOWED_LISTING_STATUSES.contains(key)) {
throw new BusinessException("不支持的商品状态:" + key);
}
if (!cleaned.contains(key)) {
cleaned.add(key);
}
}
return cleaned;
}
private void validateCreateRequest(PriceTrackLoopRunCreateRequest request) {
if (request == null) {
throw new BusinessException("请求不能为空");
@@ -539,6 +577,7 @@ public class PriceTrackLoopRunService {
vo.setStopRequested(Boolean.TRUE.equals(entity.getStopRequested()));
vo.setErrorMessage(entity.getErrorMessage());
vo.setStatusMode(Boolean.TRUE.equals(entity.getStatusMode()));
vo.setListingStatuses(parseStringList(entity.getListingStatusesJson()));
vo.setAsinMode(Boolean.TRUE.equals(entity.getAsinMode()));
vo.setAsinFiles(parseStringList(entity.getAsinFilesJson()));
vo.setCountryCodes(parseStringList(entity.getCountryCodesJson()));
@@ -548,6 +548,9 @@ public class PriceTrackTaskService {
Map<String, Object> ctx = new LinkedHashMap<>();
ctx.put("userId", request.getUserId());
ctx.put("statusMode", request.isStatusMode());
// 勾选的商品状态原样进快照:Python 侧按它逐状态筛,排查时也看得到这单跑的是哪些状态
ctx.put("listingStatuses", request.getListingStatuses() == null
? List.of() : request.getListingStatuses());
ctx.put("asinMode", request.isAsinMode());
ctx.put("countryCodes", request.getCountryCodes());
// 不再保存全量 ASIN 数据到 requestJson
@@ -0,0 +1,24 @@
-- 跟价「按商品状态(全量)」多状态(2026-10-16 立项):循环任务落库建单时勾选的状态清单。
--
-- 为什么必须落库、不能让界面现取:每一轮子任务的 Python 载荷由用户端现场拼,而页面刷新后
-- 本地勾选态会回到默认(只有在售)。INFINITE 循环跑几小时期间用户刷新一次页面,后续轮就会
-- 静默少跑「缺少的报价 / 定价问题」——用户以为还在跑,实际只跟在售。所以派发必须回读这里。
--
-- NULL=这条记录早于本功能(含所有存量循环),语义=按在售跑,与客户端默认值一致,不回填。
SET @db_name = DATABASE();
SET @col_exists := (
SELECT COUNT(*)
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = @db_name
AND TABLE_NAME = 'biz_price_track_loop_run'
AND COLUMN_NAME = 'listing_statuses_json'
);
SET @sql := IF(@col_exists = 0,
'ALTER TABLE biz_price_track_loop_run ADD COLUMN listing_statuses_json VARCHAR(512) NULL COMMENT ''json array of listing statuses: Active/PricingIssue/MissingOffer; NULL = pre-feature row, means Active'' AFTER status_mode',
'SELECT 1'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
@@ -1,13 +1,18 @@
package com.nanri.aiimage.modules.pricetrack.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.modules.pricetrack.mapper.PriceTrackLoopRunMapper;
import com.nanri.aiimage.modules.pricetrack.model.dto.PriceTrackLoopRunCreateRequest;
import com.nanri.aiimage.modules.pricetrack.model.entity.PriceTrackLoopRunEntity;
import com.nanri.aiimage.modules.pricetrack.model.vo.PriceTrackLoopRunDispatchVo;
import com.nanri.aiimage.modules.pricetrack.model.vo.PriceTrackLoopRunVo;
import com.nanri.aiimage.modules.pricetrack.model.vo.PriceTrackMatchShopsVo;
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
import com.nanri.aiimage.modules.ziniao.service.ZiniaoShopSwitchService;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.util.List;
import java.util.Map;
@@ -16,6 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
@@ -315,6 +322,101 @@ class PriceTrackLoopRunServiceTest {
assertEquals(0, loop.getResumeAttempt());
}
// ---------- 跟价多状态(2026-10-16):勾选的状态必须落在循环记录上 ----------
private PriceTrackLoopRunService newLoopService(PriceTrackLoopRunMapper loopRunMapper,
FileTaskMapper fileTaskMapper,
ObjectMapper objectMapper) {
ZiniaoShopSwitchService ziniao = mock(ZiniaoShopSwitchService.class);
when(ziniao.normalizeShopName(any())).thenAnswer(call -> call.getArgument(0));
return new PriceTrackLoopRunService(loopRunMapper, fileTaskMapper, objectMapper, ziniao);
}
private PriceTrackLoopRunCreateRequest statusModeRequest(List<String> statuses) {
PriceTrackLoopRunCreateRequest request = new PriceTrackLoopRunCreateRequest();
request.setUserId(1L);
request.setStatusMode(true);
request.setAsinMode(false);
request.setItems(new java.util.ArrayList<>(List.of(shop("魏振峰"))));
request.setCountryCodes(new java.util.ArrayList<>(List.of("DE")));
request.setExecutionMode("FINITE");
request.setTargetRounds(1);
request.setListingStatuses(statuses == null ? null : new java.util.ArrayList<>(statuses));
return request;
}
@Test
void createLoopRunPersistsCheckedListingStatusesTrimmedAndDeduped() {
PriceTrackLoopRunMapper loopRunMapper = mock(PriceTrackLoopRunMapper.class);
ObjectMapper objectMapper = new ObjectMapper();
PriceTrackLoopRunService service = newLoopService(loopRunMapper, mock(FileTaskMapper.class), objectMapper);
service.createLoopRun(statusModeRequest(List.of("Active", " PricingIssue ", "Active", "")));
ArgumentCaptor<PriceTrackLoopRunEntity> captor = ArgumentCaptor.forClass(PriceTrackLoopRunEntity.class);
verify(loopRunMapper).insert(captor.capture());
assertEquals("[\"Active\",\"PricingIssue\"]", captor.getValue().getListingStatusesJson(),
"勾选清单要去空白、去重后落库——派发每一轮都从这里回读");
}
@Test
void createLoopRunRejectsStatusOutsideTheCatalog() {
PriceTrackLoopRunMapper loopRunMapper = mock(PriceTrackLoopRunMapper.class);
PriceTrackLoopRunService service = newLoopService(loopRunMapper, mock(FileTaskMapper.class), new ObjectMapper());
BusinessException error = assertThrows(BusinessException.class,
() -> service.createLoopRun(statusModeRequest(List.of("NotAStatus"))));
assertTrue(error.getMessage().contains("NotAStatus"), "报错要点名被拒的取值:" + error.getMessage());
verify(loopRunMapper, never()).insert(any(PriceTrackLoopRunEntity.class));
}
@Test
void createLoopRunWithoutListingStatusesStillSucceedsForOlderWebBundle() {
// 后端先于用户端发布时,老界面不会带这个字段:空=按在售跑(客户端默认),不能把建单打挂
PriceTrackLoopRunMapper loopRunMapper = mock(PriceTrackLoopRunMapper.class);
PriceTrackLoopRunService service = newLoopService(loopRunMapper, mock(FileTaskMapper.class), new ObjectMapper());
PriceTrackLoopRunVo vo = service.createLoopRun(statusModeRequest(null));
ArgumentCaptor<PriceTrackLoopRunEntity> captor = ArgumentCaptor.forClass(PriceTrackLoopRunEntity.class);
verify(loopRunMapper).insert(captor.capture());
assertEquals("[]", captor.getValue().getListingStatusesJson());
assertTrue(vo.getListingStatuses().isEmpty());
}
@Test
void dispatchNextCarriesPersistedStatusesInsteadOfUiState() throws Exception {
// 这一条是"必须落库"的全部理由:载荷由用户端现场拼,页面刷新后本地勾选回到默认
// (只有在售),INFINITE 循环的后续轮就会静默少跑「缺少的报价 / 定价问题」。
ObjectMapper objectMapper = new ObjectMapper();
PriceTrackLoopRunMapper loopRunMapper = mock(PriceTrackLoopRunMapper.class);
PriceTrackLoopRunService service = newLoopService(loopRunMapper, mock(FileTaskMapper.class), objectMapper);
PriceTrackLoopRunEntity loop = new PriceTrackLoopRunEntity();
loop.setId(900L);
loop.setUserId(1L);
loop.setStatus("RUNNING");
loop.setExecutionMode("FINITE");
loop.setTargetRounds(2);
loop.setCurrentRound(1);
loop.setCurrentShopIndex(0);
loop.setStatusMode(true);
loop.setAsinMode(false);
loop.setStopRequested(false);
loop.setActiveTaskId(null);
loop.setCountryCodesJson(objectMapper.writeValueAsString(List.of("DE")));
loop.setListingStatusesJson(objectMapper.writeValueAsString(List.of("Active", "MissingOffer")));
loop.setShopsJson(objectMapper.writeValueAsString(List.of(shop("魏振峰"))));
when(loopRunMapper.selectById(900L)).thenReturn(loop);
PriceTrackLoopRunDispatchVo dispatched = service.dispatchNext(900L, 1L);
assertEquals(List.of("Active", "MissingOffer"), dispatched.getChildTaskRequest().getListingStatuses(),
"子任务派发必须回读循环记录上的状态清单");
assertEquals(List.of("Active", "MissingOffer"), dispatched.getLoopRun().getListingStatuses(),
"循环 VO 也要回显,界面才显示得出这单在跑哪些状态");
}
private PriceTrackMatchShopsVo.PriceTrackShopQueueItem shop(String name) {
PriceTrackMatchShopsVo.PriceTrackShopQueueItem item = new PriceTrackMatchShopsVo.PriceTrackShopQueueItem();
item.setShopName(name);