feat:策略责任链
This commit is contained in:
@@ -32,6 +32,11 @@
|
|||||||
<artifactId>nl-module-wms-api</artifactId>
|
<artifactId>nl-module-wms-api</artifactId>
|
||||||
<version>${revision}</version>
|
<version>${revision}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.nl.cloud</groupId>
|
||||||
|
<artifactId>nl-module-system-api</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 业务组件 -->
|
<!-- 业务组件 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package cn.code.nl.module.wms.controller.admin.warehousestrategy;
|
package cn.code.nl.module.wms.controller.admin.warehousestrategy;
|
||||||
|
|
||||||
|
import org.dromara.core.trans.anno.TransMethodResult;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
@@ -82,6 +83,7 @@ public class WarehouseStrategyController {
|
|||||||
|
|
||||||
@GetMapping("/page")
|
@GetMapping("/page")
|
||||||
@Operation(summary = "获得出入库策略分页")
|
@Operation(summary = "获得出入库策略分页")
|
||||||
|
@TransMethodResult
|
||||||
@PreAuthorize("@ss.hasPermission('wms:warehouse-strategy:query')")
|
@PreAuthorize("@ss.hasPermission('wms:warehouse-strategy:query')")
|
||||||
public CommonResult<PageResult<WarehouseStrategyRespVO>> getWarehouseStrategyPage(@Valid WarehouseStrategyPageReqVO pageReqVO) {
|
public CommonResult<PageResult<WarehouseStrategyRespVO>> getWarehouseStrategyPage(@Valid WarehouseStrategyPageReqVO pageReqVO) {
|
||||||
PageResult<WarehouseStrategyDO> pageResult = warehouseStrategyService.getWarehouseStrategyPage(pageReqVO);
|
PageResult<WarehouseStrategyDO> pageResult = warehouseStrategyService.getWarehouseStrategyPage(pageReqVO);
|
||||||
|
|||||||
@@ -1,8 +1,13 @@
|
|||||||
package cn.code.nl.module.wms.controller.admin.warehousestrategy.vo;
|
package cn.code.nl.module.wms.controller.admin.warehousestrategy.vo;
|
||||||
|
|
||||||
|
import cn.code.nl.module.system.api.user.AdminUserApi;
|
||||||
import io.swagger.v3.oas.annotations.media.Schema;
|
import io.swagger.v3.oas.annotations.media.Schema;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import org.dromara.core.trans.anno.Trans;
|
||||||
|
import org.dromara.core.trans.constant.TransType;
|
||||||
|
import org.dromara.core.trans.vo.VO;
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
import org.springframework.format.annotation.DateTimeFormat;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import cn.idev.excel.annotation.*;
|
import cn.idev.excel.annotation.*;
|
||||||
@@ -10,7 +15,7 @@ import cn.idev.excel.annotation.*;
|
|||||||
@Schema(description = "管理后台 - 出入库策略 Response VO")
|
@Schema(description = "管理后台 - 出入库策略 Response VO")
|
||||||
@Data
|
@Data
|
||||||
@ExcelIgnoreUnannotated
|
@ExcelIgnoreUnannotated
|
||||||
public class WarehouseStrategyRespVO {
|
public class WarehouseStrategyRespVO implements VO {
|
||||||
|
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@@ -32,12 +37,16 @@ public class WarehouseStrategyRespVO {
|
|||||||
|
|
||||||
@Schema(description = "创建者")
|
@Schema(description = "创建者")
|
||||||
@ExcelProperty("创建者")
|
@ExcelProperty("创建者")
|
||||||
|
@Trans(type = TransType.AUTO_TRANS, key = AdminUserApi.PREFIX, fields = "nickname", ref = "creatorName")
|
||||||
private String creator;
|
private String creator;
|
||||||
|
|
||||||
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
|
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||||
@ExcelProperty("创建时间")
|
@ExcelProperty("创建时间")
|
||||||
private LocalDateTime createTime;
|
private LocalDateTime createTime;
|
||||||
|
|
||||||
|
@Schema(description = "创建者名称")
|
||||||
|
private String creatorName;
|
||||||
|
|
||||||
@Schema(description = "更新者", requiredMode = Schema.RequiredMode.REQUIRED)
|
@Schema(description = "更新者", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||||
@ExcelProperty("更新者")
|
@ExcelProperty("更新者")
|
||||||
private String updater;
|
private String updater;
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package cn.code.nl.module.wms.enums;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @Author: liyongde
|
||||||
|
* @Date: 2026/7/18 14:15
|
||||||
|
*/
|
||||||
|
@Getter
|
||||||
|
public enum StrategyTypeEnum {
|
||||||
|
|
||||||
|
INBOUND_STRATEGY("1", "入库策略"),
|
||||||
|
OUTBOUND_STRATEGY("2", "出库策略");
|
||||||
|
|
||||||
|
private final String code;
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
StrategyTypeEnum(String code, String name) {
|
||||||
|
this.code = code;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,8 +17,8 @@ import java.util.List;
|
|||||||
* 子类通过 {@code @Service("策略编码")} 注册为 Spring Bean,
|
* 子类通过 {@code @Service("策略编码")} 注册为 Spring Bean,
|
||||||
* 启动时根据 Bean 名称自动从数据库加载对应的策略配置。
|
* 启动时根据 Bean 名称自动从数据库加载对应的策略配置。
|
||||||
*
|
*
|
||||||
* @param <T> 货位/库存数据类型
|
* @param <T> 返回类型:货位/库存数据类型
|
||||||
* @param <P> 决策参数类型
|
* @param <P> 入参类型:决策参数类型
|
||||||
* @Author: liyongde
|
* @Author: liyongde
|
||||||
* @Date: 2026/7/18 11:03
|
* @Date: 2026/7/18 11:03
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
package cn.code.nl.module.wms.manage;
|
||||||
|
|
||||||
|
import cn.code.nl.framework.common.exception.ServiceException;
|
||||||
|
import cn.code.nl.module.wms.dal.dataobject.warehousestrategy.WarehouseStrategyDO;
|
||||||
|
import cn.code.nl.module.wms.dal.mysql.warehousestrategy.WarehouseStrategyMapper;
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 策略链执行器
|
||||||
|
* <p>
|
||||||
|
* 根据库区编码和策略类型加载策略链配置,从 Spring 容器中按名称精确获取策略处理器,
|
||||||
|
* 按序执行,每个处理器的输出作为下一个处理器的输入。
|
||||||
|
*
|
||||||
|
* @Author: liyongde
|
||||||
|
* @Date: 2026/7/18
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class StrategyChainExecutor {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private WarehouseStrategyMapper warehouseStrategyMapper;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 执行策略链
|
||||||
|
*
|
||||||
|
* @param sectionCode 库区编码
|
||||||
|
* @param strategyType 策略类型(1=入库,2=出库)
|
||||||
|
* @param candidates 初始候选列表,入库时为可用货位列表,出库时传空列表由首个策略自行查询
|
||||||
|
* @param param 决策参数,类型由调用方决定(入库通常传 JSONObject,出库通常传强类型实体)
|
||||||
|
* @param <T> 货位/库存数据类型
|
||||||
|
* @param <P> 决策参数类型,需与策略链中所有处理器的泛型参数一致
|
||||||
|
* @return 策略链执行后的结果列表
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T, P> List<T> execute(String sectionCode, String strategyType, List<T> candidates, P param) {
|
||||||
|
// 1. 查询库区策略链配置
|
||||||
|
WarehouseStrategyDO strategy = warehouseStrategyMapper.selectOne(
|
||||||
|
WarehouseStrategyDO::getSectionCode, sectionCode,
|
||||||
|
WarehouseStrategyDO::getStrategyType, strategyType);
|
||||||
|
if (strategy == null) {
|
||||||
|
throw new ServiceException(500, "当前库区 " + sectionCode + " 未配置策略链");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 解析策略编码列表
|
||||||
|
List<String> strategyCodes = JSON.parseArray(strategy.getStrategy(), String.class);
|
||||||
|
if (CollUtil.isEmpty(strategyCodes)) {
|
||||||
|
throw new ServiceException(500, "当前库区 " + sectionCode + " 策略链为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 按序执行策略链
|
||||||
|
List<T> result = candidates;
|
||||||
|
for (String strategyCode : strategyCodes) {
|
||||||
|
DecisionManage<T, P> handler;
|
||||||
|
try {
|
||||||
|
handler = (DecisionManage<T, P>) applicationContext.getBean(strategyCode);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("策略 [{}] 未找到对应的处理器 Bean", strategyCode, e);
|
||||||
|
throw new ServiceException(500, "策略 " + strategyCode + " 未注册");
|
||||||
|
}
|
||||||
|
|
||||||
|
String strategyName = handler.strategyConfig.getStrategyName();
|
||||||
|
int inputSize = result != null ? result.size() : 0;
|
||||||
|
log.info("执行策略 [{}]:{},输入候选数量:{}", strategyCode, strategyName, inputSize);
|
||||||
|
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
result = handler.handler(result, param);
|
||||||
|
long cost = System.currentTimeMillis() - startTime;
|
||||||
|
|
||||||
|
int outputSize = result != null ? result.size() : 0;
|
||||||
|
log.info("策略 [{}] 执行完成,耗时:{}ms,输出数量:{}(过滤:{})",
|
||||||
|
strategyCode, cost, outputSize, inputSize - outputSize);
|
||||||
|
|
||||||
|
if (CollUtil.isEmpty(result)) {
|
||||||
|
throw new ServiceException(500, "策略 " + strategyName + " 执行后无可用货位");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,9 +5,9 @@ import lombok.Data;
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @Author: liyongde
|
* @Author: liyongde
|
||||||
* @Date: 2026/7/18 13:41
|
* @Date: 2026/7/18 14:12
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class DemoDTO {
|
public class AlleyAveHandleDTO {
|
||||||
private String id;
|
private String id;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package cn.code.nl.module.wms.manage.dto;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @Author: liyongde
|
||||||
|
* @Date: 2026/7/18 14:12
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
public class AlleyAveHandleParam {
|
||||||
|
}
|
||||||
@@ -1,23 +0,0 @@
|
|||||||
package cn.code.nl.module.wms.manage.handle;
|
|
||||||
|
|
||||||
import cn.code.nl.module.wms.manage.DecisionManage;
|
|
||||||
import cn.code.nl.module.wms.manage.dto.DemoDTO;
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @Author: liyongde
|
|
||||||
* @Date: 2026/7/18 13:39
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
@Service("alleyAve")
|
|
||||||
public class AlleyAveRuleHandler extends DecisionManage<DemoDTO, JSONObject> {
|
|
||||||
@Override
|
|
||||||
public List<DemoDTO> handler(List<DemoDTO> list, JSONObject param) {
|
|
||||||
return List.of();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package cn.code.nl.module.wms.manage.handle.base;
|
||||||
|
|
||||||
|
import cn.code.nl.module.wms.manage.DecisionManage;
|
||||||
|
import cn.code.nl.module.wms.manage.dto.AlleyAveHandleDTO;
|
||||||
|
import cn.code.nl.module.wms.manage.dto.AlleyAveHandleParam;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @Author: liyongde
|
||||||
|
* @Date: 2026/7/18 13:39
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Service("alleyAve")
|
||||||
|
public class AlleyAveRuleHandler extends DecisionManage<AlleyAveHandleDTO, AlleyAveHandleParam> {
|
||||||
|
@Override
|
||||||
|
public List<AlleyAveHandleDTO> handler(List<AlleyAveHandleDTO> list, AlleyAveHandleParam param) {
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
/**
|
||||||
|
* 所有出入库的处理器
|
||||||
|
* @Author: liyongde
|
||||||
|
* @Date: 2026/7/18 14:11
|
||||||
|
*/
|
||||||
|
package cn.code.nl.module.wms.manage.handle;
|
||||||
@@ -134,7 +134,7 @@ export function useGridColumns(): VxeTableGridOptions<WmsWarehouseStrategyApi.Wa
|
|||||||
minWidth: 120,
|
minWidth: 120,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
field: 'creator',
|
field: 'creatorName',
|
||||||
title: '创建者',
|
title: '创建者',
|
||||||
minWidth: 120,
|
minWidth: 120,
|
||||||
},
|
},
|
||||||
@@ -144,11 +144,6 @@ export function useGridColumns(): VxeTableGridOptions<WmsWarehouseStrategyApi.Wa
|
|||||||
minWidth: 120,
|
minWidth: 120,
|
||||||
formatter: 'formatDateTime',
|
formatter: 'formatDateTime',
|
||||||
},
|
},
|
||||||
{
|
|
||||||
field: 'updater',
|
|
||||||
title: '更新者',
|
|
||||||
minWidth: 120,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
field: 'updateTime',
|
field: 'updateTime',
|
||||||
title: '更新时间',
|
title: '更新时间',
|
||||||
|
|||||||
Reference in New Issue
Block a user