Commit 5cba348b by gonglei

加入框架代码

1 parent 31816bff
......@@ -111,20 +111,53 @@ public class AuctionController {
*/
@PostMapping("pageRandomAuctionVehicle")
public Page<AuctionVehicle> pageRandomAuctionVehicle(@RequestBody Pagination<AuctionVehicle> pagination, @RequestAttribute(value = LOGIN_ACCOUNT, required = false) Account account) {
pagination.addFetch(root -> root.fetch(AuctionVehicle_.vehicle, JoinType.LEFT));
pagination.addFetch(root -> root.fetch(AuctionVehicle_.auction, JoinType.LEFT));
String cityCode = (String) pagination.getDataValue("cities");
pagination.addCondition((root, query, builder) -> {
Expression<Double> randomExp = builder.function("RAND", Double.class); // MySQL 用 RAND()
if (StringUtils.hasText(cityCode)) {
CriteriaBuilder.Case<Number> caseExpr = builder.selectCase();
caseExpr.when(builder.like(root.get(AuctionVehicle_.auction).get(Auction_.cities), "%" + cityCode + "%"), 1).otherwise(0);
query.orderBy(builder.desc(caseExpr), builder.asc(randomExp)); // 按随机值升序(或降序,效果相同)
} else {
query.orderBy(builder.asc(randomExp)); // 按随机值升序(或降序,效果相同)
}
return null; // 无过滤条件,返回所有数据并随机排序
});
return pageAuctionVehicle(pagination, account);
// 已审核的拍卖场次
pagination.addFilter(List.of(Auction.AuctionState.PREVIEW, Auction.AuctionState.AUCTIONING), Operator.in, AuctionVehicle_.auctionState);
// 查询用户所有的标签
List<AccountTag> accountTags = null;
if (Objects.nonNull(account)) {
accountTags = accountService.getAccountTagsByAccount(account);
}
if (CollectionUtils.isEmpty(accountTags)) {
pagination.addFilter(Auction.AccessScope.GLOBAL, AuctionVehicle_.auction, Auction_.accessScope);
pagination.addCondition((root, query, builder) -> {
Expression<Double> randomExp = builder.function("RAND", Double.class); // MySQL 用 RAND()
if (StringUtils.hasText(cityCode)) {
CriteriaBuilder.Case<Number> caseExpr = builder.selectCase();
caseExpr.when(builder.like(root.get(AuctionVehicle_.auction).get(Auction_.cities), "%" + cityCode + "%"), 1).otherwise(0);
query.orderBy(builder.desc(caseExpr), builder.asc(randomExp)); // 按随机值升序(或降序,效果相同)
} else {
query.orderBy(builder.asc(randomExp)); // 按随机值升序(或降序,效果相同)
}
return null; // 无过滤条件,返回所有数据并随机排序
});
} else {
List<AccountTag> finalAccountTags = accountTags;
pagination.addCondition((root, query, builder) -> {
// 关联AccountTag(左连接,确保有角色关联的用户被筛选)
Join auctionJoin = root.join(AuctionVehicle_.auction, JoinType.LEFT);
Join<Auction, AccountTag> tagsJoin = auctionJoin.join(Auction_.accountTags, JoinType.LEFT);
Predicate tagsIn = tagsJoin.in(finalAccountTags);
query.distinct(true); // 去重
Expression<Double> randomExp = builder.function("RAND", Double.class); // MySQL 用 RAND()
if (StringUtils.hasText(cityCode)) {
CriteriaBuilder.Case<Number> caseExpr = builder.selectCase();
caseExpr.when(builder.like(auctionJoin.get(Auction_.cities), "%" + cityCode + "%"), 1).otherwise(0);
query.orderBy(builder.desc(caseExpr), builder.asc(randomExp)); // 按随机值升序(或降序,效果相同)
} else {
query.orderBy(builder.asc(randomExp)); // 按随机值升序(或降序,效果相同)
}
return builder.or(builder.equal(auctionJoin.get(Auction_.accessScope), Auction.AccessScope.GLOBAL), tagsIn);
});
}
return auctionService.pageAuctionVehicle(pagination);
}
/**
......@@ -156,9 +189,10 @@ public class AuctionController {
List<AccountTag> finalAccountTags = accountTags;
pagination.addCondition((root, query, builder) -> {
// 关联AccountTag(左连接,确保有角色关联的用户被筛选)
Join<Auction, AccountTag> tagsJoin = root.join(AuctionVehicle_.auction, JoinType.LEFT).join(Auction_.accountTags, JoinType.LEFT);
Join auctionJoin = root.join(AuctionVehicle_.auction, JoinType.LEFT);
Join<Auction, AccountTag> tagsJoin = auctionJoin.join(Auction_.accountTags, JoinType.LEFT);
Predicate tagsIn = tagsJoin.in(finalAccountTags);
// query.distinct(true); // 去重
query.distinct(true); // 去重
return builder.or(builder.equal(root.get(AuctionVehicle_.auction).get(Auction_.accessScope), Auction.AccessScope.GLOBAL), tagsIn);
});
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!