|
@@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
import zs.payment.enums.CallbackMessageType;
|
|
import zs.payment.enums.CallbackMessageType;
|
|
|
|
|
+import zs.payment.enums.ProductSupplyRedis;
|
|
|
import zs.payment.messages.KafkaProducer;
|
|
import zs.payment.messages.KafkaProducer;
|
|
|
import zs.payment.req.ProductDetailReq;
|
|
import zs.payment.req.ProductDetailReq;
|
|
|
import zs.payment.req.ProductPageReq;
|
|
import zs.payment.req.ProductPageReq;
|
|
@@ -15,6 +16,7 @@ import zs.payment.resp.Result;
|
|
|
import zs.payment.service.supply.product.ProductSupplyService;
|
|
import zs.payment.service.supply.product.ProductSupplyService;
|
|
|
import zs.payment.service.supply.token.SupplyTokenService;
|
|
import zs.payment.service.supply.token.SupplyTokenService;
|
|
|
import zs.payment.utils.HttpUtil;
|
|
import zs.payment.utils.HttpUtil;
|
|
|
|
|
+import zs.payment.utils.RedisUtils;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
@@ -33,6 +35,9 @@ public class ProductSupplyServiceImpl implements ProductSupplyService {
|
|
|
@Autowired
|
|
@Autowired
|
|
|
private SupplyTokenService supplyTokenService;
|
|
private SupplyTokenService supplyTokenService;
|
|
|
|
|
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private RedisUtils redisUtils;
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public Result detailList(ProductDetailReq req) {
|
|
public Result detailList(ProductDetailReq req) {
|
|
|
Map<String, String> headers = supplyTokenService.buildAuthHeaders();
|
|
Map<String, String> headers = supplyTokenService.buildAuthHeaders();
|
|
@@ -103,10 +108,25 @@ public class ProductSupplyServiceImpl implements ProductSupplyService {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
- public Result syncToQiyun() {
|
|
|
|
|
|
|
+ public Result syncToQiyun(Integer uniacid) {
|
|
|
|
|
+ // 权限校验:仅允许 uniacid = 463 调用
|
|
|
|
|
+ if (uniacid == null) {
|
|
|
|
|
+ //初期-前端不传参数,因此默认为463(云悦生活)
|
|
|
|
|
+ uniacid = 463;
|
|
|
|
|
+ }
|
|
|
|
|
+ String redisSyncLock = ProductSupplyRedis.SYNC_QIYUN_LOCK + uniacid;
|
|
|
|
|
+
|
|
|
|
|
+ // 尝试加分布式锁,防止重复同步
|
|
|
|
|
+ boolean lockAcquired = redisUtils.setIfAbsent(redisSyncLock, "1", ProductSupplyRedis.SYNC_QIYUN_LOCK_TTL);
|
|
|
|
|
+ if (!lockAcquired) {
|
|
|
|
|
+ log.warn("syncToQiyun: 商品已经在同步中,请勿重复同步操作");
|
|
|
|
|
+ return Result.fail("商品已经在同步中,请勿重复同步操作");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
Map<String, String> headers = supplyTokenService.buildAuthHeaders();
|
|
Map<String, String> headers = supplyTokenService.buildAuthHeaders();
|
|
|
if (headers == null) {
|
|
if (headers == null) {
|
|
|
log.error("syncToQiyun: 获取token失败");
|
|
log.error("syncToQiyun: 获取token失败");
|
|
|
|
|
+ redisUtils.del(redisSyncLock); // 释放锁
|
|
|
return Result.fail("获取token失败");
|
|
return Result.fail("获取token失败");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -116,6 +136,8 @@ public class ProductSupplyServiceImpl implements ProductSupplyService {
|
|
|
// 查询第一页,获取总数并发送
|
|
// 查询第一页,获取总数并发送
|
|
|
JSONObject firstPageData = fetchStoragePage(1, pageSize, headers);
|
|
JSONObject firstPageData = fetchStoragePage(1, pageSize, headers);
|
|
|
if (firstPageData == null) {
|
|
if (firstPageData == null) {
|
|
|
|
|
+ log.error("syncToQiyun: 调用选品库列表接口失败");
|
|
|
|
|
+ redisUtils.del(redisSyncLock); // 释放锁
|
|
|
return Result.fail("调用选品库列表接口失败");
|
|
return Result.fail("调用选品库列表接口失败");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -131,7 +153,8 @@ public class ProductSupplyServiceImpl implements ProductSupplyService {
|
|
|
JSONObject taskMsg = new JSONObject();
|
|
JSONObject taskMsg = new JSONObject();
|
|
|
taskMsg.put("total", total);
|
|
taskMsg.put("total", total);
|
|
|
taskMsg.put("pageSize", pageSize);
|
|
taskMsg.put("pageSize", pageSize);
|
|
|
- kafkaProducer.sendMessage("sync.qiyun.remain", taskMsg.toJSONString());
|
|
|
|
|
|
|
+ taskMsg.put("uniacid", uniacid);
|
|
|
|
|
+ kafkaProducer.sendMessage(ProductSupplyRedis.SYNC_QIYUN_REMAIN, taskMsg.toJSONString());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
log.info("syncToQiyun: 同步任务已提交,总计{}条", total);
|
|
log.info("syncToQiyun: 同步任务已提交,总计{}条", total);
|
|
@@ -139,6 +162,7 @@ public class ProductSupplyServiceImpl implements ProductSupplyService {
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("syncToQiyun: 执行异常", e);
|
|
log.error("syncToQiyun: 执行异常", e);
|
|
|
|
|
+ redisUtils.del(redisSyncLock); // 释放锁
|
|
|
return Result.fail("执行异常: " + e.getMessage());
|
|
return Result.fail("执行异常: " + e.getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|