增加抢购货品接口
This commit is contained in:
@@ -42,8 +42,9 @@ public class HxrAdminBuyService {
|
||||
* @return {@code apiCode} 为 -1 表示 HTTP 或解析失败
|
||||
*/
|
||||
public BuyApiResult buy(long goodsId, long sellerId, String token) throws Exception {
|
||||
if (token == null || token.isBlank()) {
|
||||
return BuyApiResult.failure(-1, "token不能为空");
|
||||
String resolvedToken = resolveToken(token);
|
||||
if (resolvedToken == null) {
|
||||
return BuyApiResult.failure(-1, "未提供 token 且未配置 hxr.admin.goods-api-token");
|
||||
}
|
||||
String appStr = properties.getGoodsApiAppStr();
|
||||
if (appStr == null || appStr.isBlank()) {
|
||||
@@ -78,7 +79,7 @@ public class HxrAdminBuyService {
|
||||
.header("Origin", properties.getGoodsApiOrigin())
|
||||
.header("Referer", properties.getGoodsApiReferer())
|
||||
.header("User-Agent", USER_AGENT)
|
||||
.header("token", token.trim())
|
||||
.header("token", resolvedToken)
|
||||
.header("S", sign)
|
||||
.header("T", String.valueOf(timestamp))
|
||||
.header("N", noncestr)
|
||||
@@ -108,6 +109,18 @@ public class HxrAdminBuyService {
|
||||
return new BuyApiResult(code == 0, code, msg);
|
||||
}
|
||||
|
||||
/** 优先使用入参 token,否则回退到配置 {@code goods-api-token}。 */
|
||||
private String resolveToken(String token) {
|
||||
if (token != null && !token.isBlank()) {
|
||||
return token.trim();
|
||||
}
|
||||
String configured = properties.getGoodsApiToken();
|
||||
if (configured != null && !configured.isBlank()) {
|
||||
return configured.trim();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public record BuyApiResult(boolean success, int apiCode, String apiMsg) {
|
||||
|
||||
public static BuyApiResult failure(int code, String msg) {
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.rj.service;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.rj.entity.LbGoods;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface ILbGoodsService extends IService<LbGoods> {
|
||||
@@ -35,7 +34,7 @@ public interface ILbGoodsService extends IService<LbGoods> {
|
||||
Map<String, Object> syncFromHxrGoods(String tenantId);
|
||||
|
||||
/**
|
||||
* 按 id 列表查询 {@code lb_goods},使用 token 调用 hxrd {@code /api/order/buy} 抢购。
|
||||
* 按 {@code total_money} 从大到小选取 {@code lb_goods},调用 hxrd {@code /api/order/buy} 抢购。
|
||||
*/
|
||||
Map<String, Object> rushBuy(String token, Integer maxBuyCount, List<Long> idList);
|
||||
Map<String, Object> rushBuy(String token, Integer maxBuyCount);
|
||||
}
|
||||
|
||||
@@ -375,81 +375,50 @@ public class LbGoodsServiceImpl extends ServiceImpl<LbGoodsMapper, LbGoods> impl
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> rushBuy(String token, Integer maxBuyCount, List<Long> idList) {
|
||||
public Map<String, Object> rushBuy(String token, Integer maxBuyCount) {
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
try {
|
||||
if (token == null || token.isBlank()) {
|
||||
result.put("success", false);
|
||||
result.put("message", "token不能为空");
|
||||
return result;
|
||||
}
|
||||
if (maxBuyCount == null || maxBuyCount <= 0) {
|
||||
result.put("success", false);
|
||||
result.put("message", "maxBuyCount必须大于0");
|
||||
return result;
|
||||
}
|
||||
if (idList == null || idList.isEmpty()) {
|
||||
result.put("success", false);
|
||||
result.put("message", "idList不能为空");
|
||||
return result;
|
||||
}
|
||||
|
||||
List<Long> distinctIds = idList.stream()
|
||||
.filter(id -> id != null)
|
||||
.distinct()
|
||||
.toList();
|
||||
if (distinctIds.isEmpty()) {
|
||||
LambdaQueryWrapper<LbGoods> w = new LambdaQueryWrapper<>();
|
||||
w.isNotNull(LbGoods::getSellerId);
|
||||
w.orderByDesc(LbGoods::getTotalMoney).orderByDesc(LbGoods::getId);
|
||||
List<LbGoods> goodsList = this.list(w);
|
||||
if (goodsList.isEmpty()) {
|
||||
result.put("success", false);
|
||||
result.put("message", "idList无有效id");
|
||||
result.put("message", "lb_goods 中无可抢购货品");
|
||||
result.put("successCount", 0);
|
||||
result.put("failCount", 0);
|
||||
result.put("details", List.of());
|
||||
return result;
|
||||
}
|
||||
|
||||
List<LbGoods> found = this.listByIds(distinctIds);
|
||||
Map<Long, LbGoods> goodsById = new LinkedHashMap<>();
|
||||
for (LbGoods goods : found) {
|
||||
if (goods.getId() != null) {
|
||||
goodsById.put(goods.getId(), goods);
|
||||
}
|
||||
}
|
||||
|
||||
List<Map<String, Object>> details = new ArrayList<>();
|
||||
int successCount = 0;
|
||||
int failCount = 0;
|
||||
boolean stoppedByMax = false;
|
||||
|
||||
for (Long goodsId : idList) {
|
||||
if (goodsId == null) {
|
||||
continue;
|
||||
}
|
||||
for (LbGoods goods : goodsList) {
|
||||
if (successCount >= maxBuyCount) {
|
||||
stoppedByMax = true;
|
||||
break;
|
||||
}
|
||||
if (goods.getId() == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Map<String, Object> item = new LinkedHashMap<>();
|
||||
item.put("id", goodsId);
|
||||
|
||||
LbGoods goods = goodsById.get(goodsId);
|
||||
if (goods == null) {
|
||||
item.put("success", false);
|
||||
item.put("message", "lb_goods中未找到该货品");
|
||||
details.add(item);
|
||||
failCount++;
|
||||
continue;
|
||||
}
|
||||
if (goods.getSellerId() == null) {
|
||||
item.put("success", false);
|
||||
item.put("message", "seller_id为空,无法抢购");
|
||||
details.add(item);
|
||||
failCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
item.put("id", goods.getId());
|
||||
item.put("sellerId", goods.getSellerId());
|
||||
item.put("title", goods.getTitle());
|
||||
item.put("totalMoney", goods.getTotalMoney());
|
||||
|
||||
HxrAdminBuyService.BuyApiResult buyResult =
|
||||
hxrAdminBuyService.buy(goodsId, goods.getSellerId(), token);
|
||||
hxrAdminBuyService.buy(goods.getId(), goods.getSellerId(), token);
|
||||
item.put("apiCode", buyResult.apiCode());
|
||||
item.put("message", buyResult.apiMsg());
|
||||
item.put("success", buyResult.success());
|
||||
|
||||
Reference in New Issue
Block a user