|
|
@@ -8,6 +8,7 @@ import zs.payment.req.CommodityProcureReq;
|
|
|
import zs.payment.req.GoodsSupplyReq;
|
|
|
import zs.payment.resp.Result;
|
|
|
import zs.payment.service.commodity.CommodityProcureService;
|
|
|
+import zs.payment.utils.RedisUtils;
|
|
|
|
|
|
import javax.validation.Valid;
|
|
|
|
|
|
@@ -18,6 +19,13 @@ public class EweiShopGoodsController {
|
|
|
|
|
|
@Autowired
|
|
|
private CommodityProcureService commodityProcureService;
|
|
|
+ @Autowired
|
|
|
+ private RedisUtils redisUtils;
|
|
|
+
|
|
|
+ /** 防重复提交锁前缀 */
|
|
|
+ private static final String LOCK_PREFIX_FULL_OUTPUT = "lock:fullOutput:";
|
|
|
+ /** 锁过期时间(秒),防止死锁 */
|
|
|
+ private static final int LOCK_EXPIRE_SECONDS = 30;
|
|
|
|
|
|
/**
|
|
|
* 进货:把云商城(悦店管理)里的商品全进货到本地
|
|
|
@@ -34,14 +42,27 @@ public class EweiShopGoodsController {
|
|
|
/**
|
|
|
* 供货/停止供货:把本地商品推送到云商城,或停止供货
|
|
|
* issupply=1: 供货 issupply=0: 停止供货
|
|
|
+ * 加入Redis分布式锁防止用户多次点击重复提交
|
|
|
*/
|
|
|
@PostMapping("/fullOutput")
|
|
|
public Result fullOutput(@Valid @RequestBody GoodsSupplyReq req) {
|
|
|
+ // 防重复提交:基于 uniacid + issupply 加分布式锁
|
|
|
+ String lockKey = LOCK_PREFIX_FULL_OUTPUT + req.getUniacid() + ":" + req.getIssupply();
|
|
|
+ boolean locked = redisUtils.setIfAbsent(lockKey, "1", LOCK_EXPIRE_SECONDS);
|
|
|
+ if (!locked) {
|
|
|
+ return Result.fail("操作频繁,请勿重复提交!");
|
|
|
+ }
|
|
|
try {
|
|
|
return commodityProcureService.fullOutput(req);
|
|
|
} catch (ApiException e) {
|
|
|
+ // 发送Kafka失败时立即释放锁
|
|
|
+ redisUtils.del(lockKey);
|
|
|
return Result.fail(e.getMsg());
|
|
|
+ } catch (Exception e) {
|
|
|
+ redisUtils.del(lockKey);
|
|
|
+ throw e;
|
|
|
}
|
|
|
+ // 正常情况不释放锁,由Kafka消费者处理完所有任务后释放
|
|
|
}
|
|
|
|
|
|
}
|