分页优化用户策略优化

parent 811edf6c
......@@ -363,7 +363,7 @@ public class SwaggerMapParamConfig {
"带条件分页查询订单数据",
"{\n" +
" \"sceneId\": \"SCENE001\",\n" +
" \"pageIndex\": 0,\n" +
" \"pageIndex\": 1,\n" +
" \"pageSize\": 10,\n" +
" \"conditions\": [\n" +
" {\n" +
......
......@@ -88,6 +88,9 @@ public class LanuchController {
// 这些参数前端可以不传;不传时后端会根据 sceneId 找场景创建人,再查这个人的策略。
Long userId = getLongParam(params, "userId");
Long baseRuleId = getLongParam(params, "baseRuleId");
if (baseRuleId == null) {
baseRuleId = getLongParam(params, "ruleId");
}
if (baseRuleId == null) {
baseRuleId = getLongParam(params, "referenceId");
}
......
......@@ -31,16 +31,16 @@ public class UserStrategyRuleController {
@Operation(
summary = "获取当前生效策略",
requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
description = "未传 userRuleId 时按 userId 获取用户最后保存的策略;旧的 USER:id 入参会被忽略。需要指定某条用户策略时传 userRuleId。",
description = "前端传 userId + baseRuleId,baseRuleId 为 STRATEGY_RULE.ID;后端优先返回该基础策略对应的用户策略,没有则返回全局策略。",
required = true,
content = @Content(
mediaType = "application/json",
examples = {
@ExampleObject(
name = "获取用户最后保存策略",
name = "按基础策略获取生效策略",
value = "{\n" +
" \"userId\": 10001,\n" +
" \"id\": \"USER:f3d7e0f0-3b8a-4d0c-8f2a-7d4e64f4c901\"\n" +
" \"baseRuleId\": 1\n" +
"}"
),
@ExampleObject(
......@@ -64,11 +64,12 @@ public class UserStrategyRuleController {
" \"code\": 200,\n" +
" \"msg\": \"success\",\n" +
" \"data\": {\n" +
" \"id\": 1,\n" +
" \"source\": \"USER\",\n" +
" \"userRuleId\": \"f3d7e0f0-3b8a-4d0c-8f2a-7d4e64f4c901\",\n" +
" \"baseRuleId\": 1,\n" +
" \"sceneId\": null,\n" +
" \"name\": \"交期优先-用户规则\",\n" +
" \"name\": \"交期优先\",\n" +
" \"isjit\": false,\n" +
" \"forwardScheduling\": [\n" +
" {\n" +
......@@ -107,7 +108,7 @@ public class UserStrategyRuleController {
@Operation(
summary = "保存用户策略",
requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
description = "保存当前用户策略。传下拉选项 id;如果是 GLOBAL:id,后端会创建/更新该用户对应全局策略的用户规则。",
description = "前端传 userId + baseRuleId 保存用户策略,baseRuleId 为 STRATEGY_RULE.ID;后端按 userId + baseRuleId 新增或更新 USER_STRATEGY_RULE。",
required = true,
content = @Content(
mediaType = "application/json",
......@@ -116,7 +117,7 @@ public class UserStrategyRuleController {
name = "从全局策略保存用户规则",
value = "{\n" +
" \"userId\": 10001,\n" +
" \"id\": \"GLOBAL:1\",\n" +
" \"baseRuleId\": 1,\n" +
" \"isjit\": \"true\",\n" +
" \"forwardScheduling\": [\n" +
" {\n" +
......@@ -146,7 +147,7 @@ public class UserStrategyRuleController {
name = "更新已有用户规则",
value = "{\n" +
" \"userId\": 10001,\n" +
" \"id\": \"USER:f3d7e0f0-3b8a-4d0c-8f2a-7d4e64f4c901\",\n" +
" \"baseRuleId\": 1,\n" +
" \"isjit\": \"false\",\n" +
" \"forwardScheduling\": [],\n" +
" \"kpiConfig\": []\n" +
......@@ -194,7 +195,7 @@ public class UserStrategyRuleController {
@GetMapping("/globalList")
@Operation(
summary = "获取策略下拉选项",
description = "按全局策略生成下拉。全局有几条就返回几条;当前用户已保存过的全局策略返回 USER,否则返回 GLOBAL。",
description = "按 STRATEGY_RULE 生成下拉。全局有几条就返回几条;当前用户已保存过的基础策略返回 USER,否则返回 GLOBAL。id/baseRuleId 都是 STRATEGY_RULE.ID。",
responses = @ApiResponse(
responseCode = "200",
description = "查询成功",
......@@ -207,10 +208,11 @@ public class UserStrategyRuleController {
" \"msg\": \"success\",\n" +
" \"data\": [\n" +
" {\n" +
" \"id\": \"USER:f3d7e0f0-3b8a-4d0c-8f2a-7d4e64f4c901\",\n" +
" \"id\": 1,\n" +
" \"source\": \"USER\",\n" +
" \"ruleId\": \"f3d7e0f0-3b8a-4d0c-8f2a-7d4e64f4c901\",\n" +
" \"name\": \"交期优先-用户规则\",\n" +
" \"baseRuleId\": 1,\n" +
" \"userRuleId\": \"f3d7e0f0-3b8a-4d0c-8f2a-7d4e64f4c901\",\n" +
" \"name\": \"交期优先\",\n" +
" \"globalRuleId\": 1,\n" +
" \"globalRuleName\": \"交期优先\",\n" +
" \"isjit\": false,\n" +
......@@ -227,9 +229,9 @@ public class UserStrategyRuleController {
" \"isDefault\": 1\n" +
" },\n" +
" {\n" +
" \"id\": \"GLOBAL:2\",\n" +
" \"id\": 2,\n" +
" \"source\": \"GLOBAL\",\n" +
" \"ruleId\": 2,\n" +
" \"baseRuleId\": 2,\n" +
" \"name\": \"齐套优先\",\n" +
" \"globalRuleId\": 2,\n" +
" \"globalRuleName\": \"齐套优先\",\n" +
......@@ -237,18 +239,18 @@ public class UserStrategyRuleController {
" \"forwardScheduling\": []\n" +
" },\n" +
" {\n" +
" \"id\": \"GLOBAL:3\",\n" +
" \"id\": 3,\n" +
" \"source\": \"GLOBAL\",\n" +
" \"ruleId\": 3,\n" +
" \"baseRuleId\": 3,\n" +
" \"name\": \"换型最少\",\n" +
" \"globalRuleId\": 3,\n" +
" \"globalRuleName\": \"换型最少\",\n" +
" \"forwardScheduling\": []\n" +
" },\n" +
" {\n" +
" \"id\": \"GLOBAL:4\",\n" +
" \"id\": 4,\n" +
" \"source\": \"GLOBAL\",\n" +
" \"ruleId\": 4,\n" +
" \"baseRuleId\": 4,\n" +
" \"name\": \"设备负载均衡\",\n" +
" \"globalRuleId\": 4,\n" +
" \"globalRuleName\": \"设备负载均衡\",\n" +
......
......@@ -46,9 +46,9 @@ public class ChromosomeDataController {
* 通用接口,根据实体名称查询Chromosome中的数据,支持分页和条件过滤
* 示例:
* - 文件实体: POST /queryChromosome/order/page
* Body: { "sceneId": "SCENE001", "pageIndex": 0, "pageSize": 10, "conditions": [...] }
* Body: { "sceneId": "SCENE001", "pageIndex": 1, "pageSize": 10, "conditions": [...] }
* - 数据库实体: POST /queryChromosome/user/page
* Body: { "pageIndex": 0, "pageSize": 10, "conditions": [...] }
* Body: { "pageIndex": 1, "pageSize": 10, "conditions": [...] }
*
* @param entityName 实体名称 (如: order, entry, machine, user, department等)
* @param paged 分页和条件对象
......@@ -235,7 +235,7 @@ public class ChromosomeDataController {
* "name": "订单列表",
* "table": "order",
* "data": {
* "pageIndex": 0,
* "pageIndex": 1,
* "pageSize": 10,
* "conditions": [
* { "fieldName": "sceneId", "fieldValue": "SCENE001", "conditionalType": "EQUAL" }
......
......@@ -849,7 +849,7 @@ public class ResourceGanttController {
}
// 获取分页参数
Integer pageindex = paged.getPageIndex() != null ? Math.max(0, paged.getPageIndex()) : 0;
Integer pageindex = paged.getPageIndex() != null ? Math.max(1, paged.getPageIndex()) : 1;
Integer pagesize = paged.getPageSize() != null ? paged.getPageSize() : 1000;
// 校验能否获取对应的文件
......@@ -888,7 +888,7 @@ public class ResourceGanttController {
int totalOrders = orderList.size();
// 执行工单分页
int startIndex = pageindex * pagesize;
int startIndex = (pageindex - 1) * pagesize;
int endIndex = Math.min(startIndex + pagesize, totalOrders);
List<TaskVO> pageOrders;
if (startIndex < totalOrders) {
......@@ -953,7 +953,7 @@ public class ResourceGanttController {
}
// 获取分页参数
Integer pageindex = paged.getPageIndex() != null ? Math.max(0, paged.getPageIndex()) : 0;
Integer pageindex = paged.getPageIndex() != null ? Math.max(1, paged.getPageIndex()) : 1;
Integer pagesize = paged.getPageSize() != null ? paged.getPageSize() : 1000;
// 校验能否获取对应的文件
......@@ -969,7 +969,7 @@ public class ResourceGanttController {
int total = productGanttVOs.size();
// 计算分页参数
int startIndex = pageindex * pagesize;
int startIndex = (pageindex - 1) * pagesize;
int endIndex = Math.min(startIndex + pagesize, total);
// 执行分页
......
......@@ -17,7 +17,7 @@ import java.util.Optional;
*/
public class Paged {
private Integer pageIndex=0; //当前页
private Integer pageIndex=1; //当前页
private Integer pageSize=1000; //每页多少条
private Integer total=0;// 特殊设置,总记录数,如果前台带有此值,则分页查询时不查询总数。
private List<String> sortByList=new ArrayList<>(); // 多字段排序列表,格式如["type asc", "id desc"]
......
......@@ -418,7 +418,7 @@ public class ChromosomeDataService {
Map<String, Object> emptyResult = new HashMap<>();
emptyResult.put("records", Collections.emptyList());
emptyResult.put("totalCount", 0);
emptyResult.put("pageIndex", paged.getPageIndex() != null ? Math.max(0, paged.getPageIndex()) : 0);
emptyResult.put("pageIndex", paged.getPageIndex() != null ? Math.max(1, paged.getPageIndex()) : 1);
emptyResult.put("size", paged.getPageSize() != null ? paged.getPageSize() : 10);
return emptyResult;
}
......@@ -817,9 +817,9 @@ public class ChromosomeDataService {
}
// 应用分页
if (paged.getPageIndex() != null && paged.getPageSize() != null && paged.getPageSize() > 0) {
int pageIndex = Math.max(0, paged.getPageIndex());
int pageIndex = Math.max(1, paged.getPageIndex());
int pageSize = paged.getPageSize();
int start = pageIndex * pageSize;
int start = (pageIndex - 1) * pageSize;
int end = Math.min(start + pageSize, result.size());
if (start >= result.size()) {
return new ArrayList<>();
......@@ -1573,7 +1573,7 @@ public class ChromosomeDataService {
}
Map<String, Object> result = new HashMap<>();
int page = paged.getPageIndex() != null ? Math.max(0, paged.getPageIndex()) : 0;
int page = paged.getPageIndex() != null ? Math.max(1, paged.getPageIndex()) : 1;
int size = paged.getPageSize() != null ? paged.getPageSize() : 10;
if (data instanceof List) {
......@@ -1601,7 +1601,7 @@ public class ChromosomeDataService {
}
int total = dataList.size();
int fromIndex = page * size;
int fromIndex = (page - 1) * size;
fromIndex = Math.min(fromIndex, total);
int toIndex = Math.min(fromIndex + size, total);
......@@ -1620,7 +1620,7 @@ public class ChromosomeDataService {
} else {
result.put("records", data);
result.put("totalCount", 1);
result.put("pageIndex", 0);
result.put("pageIndex", 1);
result.put("size", 1);
}
......
......@@ -65,7 +65,7 @@ public class DatabaseQueryService {
String orderBy = buildOrderBy(paged);
// 分页参数
int page = paged.getPageIndex() != null ? Math.max(0, paged.getPageIndex()) : 0;
int page = paged.getPageIndex() != null ? Math.max(1, paged.getPageIndex()) : 1;
int size = paged.getPageSize() != null ? paged.getPageSize() : 10;
// 返回列:fields 有值时只查指定列,否则 SELECT *
......@@ -157,7 +157,7 @@ public class DatabaseQueryService {
Map<String, Object> empty = new HashMap<>();
empty.put("records", Collections.emptyList());
empty.put("totalCount", 0);
empty.put("pageIndex", paged.getPageIndex() != null ? Math.max(0, paged.getPageIndex()) : 0);
empty.put("pageIndex", paged.getPageIndex() != null ? Math.max(1, paged.getPageIndex()) : 1);
empty.put("size", paged.getPageSize() != null ? paged.getPageSize() : 10);
return empty;
}
......@@ -165,7 +165,7 @@ public class DatabaseQueryService {
String groupColsStr = String.join(", ", groupCols);
// 修复分组查询时的ORDER BY语法错误
String orderBy = buildOrderByForGroupBy(paged, groupCols);
int page = paged.getPageIndex() != null ? Math.max(0, paged.getPageIndex()) : 0;
int page = paged.getPageIndex() != null ? Math.max(1, paged.getPageIndex()) : 1;
int size = paged.getPageSize() != null ? paged.getPageSize() : 10;
String countSql = "SELECT COUNT(*) FROM (SELECT " + groupColsStr + " FROM " + tableName + whereClause + " GROUP BY " + groupColsStr + ")";
......@@ -218,9 +218,10 @@ public class DatabaseQueryService {
}
private String buildOraclePaginationSqlForGroupBy(String innerSql, int page, int size) {
int safePage = Math.max(0, page);
int startRow = safePage * size + 1;
int endRow = (safePage + 1) * size;
int safePage = Math.max(1, page);
int offsetPage = safePage - 1;
int startRow = offsetPage * size + 1;
int endRow = safePage * size;
return "SELECT * FROM (SELECT a.*, ROWNUM rn FROM (" + innerSql + ") a WHERE ROWNUM <= " + endRow + ") WHERE rn >= " + startRow;
}
......@@ -442,9 +443,10 @@ public class DatabaseQueryService {
* 构建Oracle分页SQL;selectClause 为 null 或空时使用 *。
*/
private String buildOraclePaginationSql(String tableName, String whereClause, String orderBy, int page, int size, String selectClause) {
int safePage = Math.max(0, page);
int startRow = safePage * size + 1;
int endRow = (safePage + 1) * size;
int safePage = Math.max(1, page);
int offsetPage = safePage - 1;
int startRow = offsetPage * size + 1;
int endRow = safePage * size;
String cols = (selectClause != null && !selectClause.trim().isEmpty()) ? selectClause : "*";
StringBuilder sql = new StringBuilder();
......
......@@ -66,13 +66,26 @@ public class UserStrategyRuleServiceImpl extends ServiceImpl<UserStrategyRuleMap
String source = getSource(params);
String ruleId = getRuleId(params);
String userRuleId = "USER".equalsIgnoreCase(source) ? ruleId : getString(params, "userRuleId");
Long baseRuleId = resolveBaseRuleId(params, source, ruleId);
UserStrategyRule userRule = findUserRuleById(userRuleId, userId);
if (baseRuleId == null && userRule != null) {
baseRuleId = userRule.getBaseRuleId();
}
if (baseRuleId == null) {
userRule = findUserRule(userId, null);
if (userRule != null) {
baseRuleId = userRule.getBaseRuleId();
}
}
if (baseRuleId == null) {
throw new RuntimeException("baseRuleId不能为空");
}
UserStrategyRule userRule = findUserRule(userId, baseRuleId);
if (userRule == null || !baseRuleId.equals(userRule.getBaseRuleId())) {
userRule = findUserRule(userId, baseRuleId);
}
StrategyRule globalRule = strategyRuleService.getById(baseRuleId);
if (globalRule == null || isDeleted(globalRule.getIsDeleted())) {
throw new RuntimeException("baseRuleId不存在");
......@@ -195,11 +208,10 @@ public class UserStrategyRuleServiceImpl extends ServiceImpl<UserStrategyRuleMap
private Map<String, Object> buildGlobalOption(StrategyRule rule) {
Map<String, Object> item = new HashMap<>();
item.put("id", "GLOBAL:" + rule.getId());
item.put("id", rule.getId());
item.put("source", "GLOBAL");
item.put("userRuleId", null);
item.put("baseRuleId", rule.getId());
item.put("ruleId", rule.getId());
item.put("name", rule.getName());
item.put("globalRuleId", rule.getId());
item.put("globalRuleName", rule.getName());
......@@ -210,11 +222,10 @@ public class UserStrategyRuleServiceImpl extends ServiceImpl<UserStrategyRuleMap
private Map<String, Object> buildUserOption(UserStrategyRule rule, StrategyRule globalRule) {
Map<String, Object> item = new HashMap<>();
item.put("id", "USER:" + rule.getId());
item.put("id", globalRule.getId());
item.put("source", "USER");
item.put("userRuleId", rule.getId());
item.put("baseRuleId", rule.getBaseRuleId());
item.put("ruleId", rule.getId());
item.put("name", rule.getName());
item.put("isDefault", rule.getIsDefault());
item.put("globalRuleId", globalRule.getId());
......@@ -316,6 +327,7 @@ public class UserStrategyRuleServiceImpl extends ServiceImpl<UserStrategyRuleMap
private Map<String, Object> buildUserResult(UserStrategyRule rule) {
Map<String, Object> result = new HashMap<>();
result.put("id", rule.getBaseRuleId());
result.put("source", "USER");
result.put("userRuleId", rule.getId());
result.put("baseRuleId", rule.getBaseRuleId());
......@@ -329,6 +341,7 @@ public class UserStrategyRuleServiceImpl extends ServiceImpl<UserStrategyRuleMap
private Map<String, Object> buildGlobalResult(StrategyRule rule) {
Map<String, Object> result = new HashMap<>();
result.put("id", rule == null ? null : rule.getId());
result.put("source", "GLOBAL");
result.put("userRuleId", null);
result.put("baseRuleId", rule == null ? null : rule.getId());
......@@ -461,13 +474,16 @@ public class UserStrategyRuleServiceImpl extends ServiceImpl<UserStrategyRuleMap
private String resolveName(Map<String, Object> params, StrategyRule globalRule, boolean newUserRule) {
String name = getString(params, "name");
if (!StringUtils.hasText(name)) {
name = getString(params, "referenceName");
}
if (StringUtils.hasText(name)) {
return name;
}
if (globalRule == null) {
return null;
}
return newUserRule ? globalRule.getName() + "-用户规则" : globalRule.getName();
return globalRule.getName();
}
private Long getLong(Map<String, Object> params, String key) {
......@@ -481,6 +497,14 @@ public class UserStrategyRuleServiceImpl extends ServiceImpl<UserStrategyRuleMap
if ("GLOBAL".equalsIgnoreCase(source)) {
return toLong(ruleId);
}
Long strategyRuleId = getLongIfNumeric(params, "ruleId");
if (strategyRuleId != null) {
return strategyRuleId;
}
Long numericId = getLongIfNumeric(params, "id");
if (numericId != null) {
return numericId;
}
Long baseRuleId = getLong(params, "baseRuleId");
// 前端保存策略时可能传 referenceId;只有纯数字时才兼容成基础策略 ID,UUID 行引用要忽略。
if (baseRuleId != null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment