增加抢单接口

This commit is contained in:
2026-05-28 18:45:12 +08:00
parent b0494ff5b6
commit 412aa1f8d8
7 changed files with 304 additions and 1 deletions

View File

@@ -7,6 +7,7 @@ import com.rj.dto.hxr.HxrLbGoodsPageData;
import com.rj.dto.hxr.HxrLbGoodsSelectResponse;
import com.rj.entity.LbGoods;
import com.rj.mapper.LbGoodsMapper;
import com.rj.service.HxrAdminBuyService;
import com.rj.service.HxrAdminGoodsService;
import com.rj.service.ILbGoodsService;
import com.rj.tenant.TenantContextHolder;
@@ -35,6 +36,8 @@ public class LbGoodsServiceImpl extends ServiceImpl<LbGoodsMapper, LbGoods> impl
private final HxrAdminGoodsService hxrAdminGoodsService;
private final HxrAdminBuyService hxrAdminBuyService;
@Override
public Map<String, Object> add(LbGoods entity) {
Map<String, Object> result = new HashMap<>();
@@ -371,6 +374,111 @@ public class LbGoodsServiceImpl extends ServiceImpl<LbGoodsMapper, LbGoods> impl
}
}
@Override
public Map<String, Object> rushBuy(String token, Integer maxBuyCount, List<Long> idList) {
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()) {
result.put("success", false);
result.put("message", "idList无有效id");
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;
}
if (successCount >= maxBuyCount) {
stoppedByMax = true;
break;
}
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("sellerId", goods.getSellerId());
item.put("title", goods.getTitle());
HxrAdminBuyService.BuyApiResult buyResult =
hxrAdminBuyService.buy(goodsId, goods.getSellerId(), token);
item.put("apiCode", buyResult.apiCode());
item.put("message", buyResult.apiMsg());
item.put("success", buyResult.success());
details.add(item);
if (buyResult.success()) {
successCount++;
} else {
failCount++;
}
}
result.put("success", successCount > 0);
result.put("message", successCount > 0
? "抢购完成,成功 " + successCount + " 笔,失败 " + failCount + ""
: "抢购未成功");
result.put("successCount", successCount);
result.put("failCount", failCount);
result.put("stoppedByMax", stoppedByMax);
result.put("details", details);
return result;
} catch (Exception e) {
log.error("rushBuy failed", e);
result.put("success", false);
result.put("message", "抢购异常:" + e.getMessage());
return result;
}
}
private static void applyDefaults(LbGoods entity) {
if (entity.getPrice() == null) {
entity.setPrice(BigDecimal.ZERO);