diff --git a/pom.xml b/pom.xml index 94c8094..d725f79 100644 --- a/pom.xml +++ b/pom.xml @@ -88,7 +88,7 @@ com.jd jdk - 2.0 + 3.0 diff --git a/src/main/java/cn/van/business/util/JDUtil.java b/src/main/java/cn/van/business/util/JDUtil.java index 067126b..0d0422c 100644 --- a/src/main/java/cn/van/business/util/JDUtil.java +++ b/src/main/java/cn/van/business/util/JDUtil.java @@ -5,6 +5,7 @@ import cn.van.business.model.jd.OrderRow; import cn.van.business.model.jd.ProductOrder; import cn.van.business.repository.OrderRowRepository; import cn.van.business.repository.ProductOrderRepository; +import com.alibaba.fastjson2.JSON; import com.fasterxml.jackson.databind.ObjectMapper; import com.jd.open.api.sdk.DefaultJdClient; import com.jd.open.api.sdk.JdClient; @@ -674,7 +675,7 @@ public class JDUtil { try { String skuId = ""; double amount = 0.0; - String owner = "自营"; + String owner = "pop"; switch (state.getCurrentStep()) { case STEP_PRODUCT_LINK: skuId = parseSkuFromUrl(message); @@ -698,6 +699,7 @@ public class JDUtil { // //logger.debug("商品信息已收集:SKU={}, Owner={}, WDIS={}", skuId, owner, productInfo.getBaseBigFieldInfo().getWdis()); // 新增 String productInfo = skuId; + state.getCollectedFields().put("skuId", skuId); state.setCurrentStep(STEP_AMOUNT); String prompt = String.format("商品SKU:\n %s\n 请输入开通金额(元):", productInfo); wxUtil.sendTextMessage(fromWxid, prompt, 1, fromWxid); @@ -739,7 +741,10 @@ public class JDUtil { logger.debug("礼金参数准备完成:SKU={},金额={}元,数量={},Owner={}", state.getCollectedFields().get("skuId"), amount, quantity, state.getCollectedFields().get("owner")); // 新增 - String giftKey = createGiftCoupon(skuId, amount, quantity, owner); + String giftKey = createGiftCoupon(state.getCollectedFields().get("skuId"), + Double.parseDouble(state.getCollectedFields().get("amount")), + quantity, + owner); if (giftKey == null) { logger.error("用户 {} 礼金创建失败:SKU={}, 金额={}, 数量={}, Owner={}", fromWxid, skuId, amount, quantity, owner); // 新增 wxUtil.sendTextMessage(fromWxid, "❌ 礼金创建失败,请检查商品是否符合要求", 1, fromWxid); @@ -801,58 +806,65 @@ public class JDUtil { // 新增礼金创建方法 - public String createGiftCoupon(String skuId, double amount, int quantity, String owner) throws Exception { +public String createGiftCoupon(String skuId, double amount, int quantity, String owner) throws Exception { + logger.debug("准备创建礼金:SKU={}, 金额={}元,数量={}, Owner={}", skuId, amount, quantity, owner); - logger.debug("准备创建礼金:SKU={}, 金额={}元,数量={}, Owner={}", skuId, amount, quantity, owner); // 新增 - - JdClient client = new DefaultJdClient(SERVER_URL, ACCESS_TOKEN, LPF_APP_KEY_WZ, LPF_APP_KEY_WZ); - - UnionOpenCouponGiftGetRequest request = new UnionOpenCouponGiftGetRequest(); - CreateGiftCouponReq couponReq = new CreateGiftCouponReq(); - couponReq.setSkuMaterialId(skuId); // 使用SKU或链接 - couponReq.setDiscount(amount); - couponReq.setAmount(quantity); - //owner g=自营,p=pop - // 自营的只能设置一天,pop的只能设置7天,默认为自营 - String startTime; - String endTime; - if ("pop".equals(owner)) { - startTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH")); - endTime = LocalDateTime.now().plusDays(6).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH")); - } else { - startTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH")); - endTime = LocalDateTime.now().plusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH")); - - } - couponReq.setReceiveStartTime(startTime); - couponReq.setReceiveEndTime(endTime); - couponReq.setEffectiveDays(7); - couponReq.setIsSpu(1); - couponReq.setExpireType(1); - couponReq.setShare(-1); - couponReq.setCouponTitle(skuId); - couponReq.setContentMatch(1); - - request.setCouponReq(couponReq); - request.setVersion("1.0"); - request.setSignmethod("md5"); - request.setTimestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); - - logger.debug("请求参数:{}", request); // 新增 - - UnionOpenCouponGiftGetResponse response = client.execute(request); - logger.debug("API响应:{}", response); // 新增 - - if ("200".equals(response.getCode())) { - String giftKey = response.getGetResult().getData().getGiftCouponKey(); - logger.debug("礼金创建成功:批次ID={}, 返回数据:{}", giftKey, response.getGetResult().getData()); // 新增 - return giftKey; - } else { - logger.error("礼金创建失败:错误码={}, 错误信息={}", response.getCode(), response.getMsg()); // 新增 - } + // 参数校验 + if (skuId == null || amount <= 0 || quantity <= 0) { + logger.error("礼金创建失败:参数错误,SKU={}, 金额={}元,数量={}", skuId, amount, quantity); return null; } + // 设置默认值 + owner = (owner != null) ? owner : "自营"; + + JdClient client = new DefaultJdClient(SERVER_URL, ACCESS_TOKEN, LPF_APP_KEY_WZ, LPF_APP_KEY_WZ); + + UnionOpenCouponGiftGetRequest request = new UnionOpenCouponGiftGetRequest(); + CreateGiftCouponReq couponReq = new CreateGiftCouponReq(); + couponReq.setSkuMaterialId(skuId); // 使用SKU或链接 + couponReq.setDiscount(amount); + couponReq.setAmount(quantity); + + // 自营的只能设置一天,pop的只能设置7天,默认为自营 + String startTime; + String endTime; + if ("pop".equals(owner)) { + startTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH")); + endTime = LocalDateTime.now().plusDays(6).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH")); + } else { + startTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH")); + endTime = LocalDateTime.now().plusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH")); + } + + couponReq.setReceiveStartTime(startTime); + couponReq.setReceiveEndTime(endTime); + couponReq.setEffectiveDays(7); + couponReq.setIsSpu(1); + couponReq.setExpireType(1); + couponReq.setShare(-1); + couponReq.setCouponTitle(skuId); + couponReq.setContentMatch(1); + + request.setCouponReq(couponReq); + request.setVersion("1.0"); + + logger.debug("请求参数:{}", JSON.toJSONString(request)); + + UnionOpenCouponGiftGetResponse response = client.execute(request); + logger.debug("API响应:{}", JSON.toJSONString(response)); + + if ("200".equals(response.getCode())) { + String giftKey = response.getGetResult().getData().getGiftCouponKey(); + logger.debug("礼金创建成功:批次ID={}, 返回数据:{}", giftKey, response.getGetResult().getData()); + return giftKey; + } else { + logger.error("礼金创建失败:错误码={}, 错误信息={}", response.getCode(), response.getMsg()); + } + return null; +} + + // 修改activateGiftMoney方法调用真实接口 private boolean activateGiftMoney(String skuId, double amount, int quantity, String owner) { try { @@ -893,7 +905,7 @@ public class JDUtil { } try { double amount = Double.parseDouble(input); - return amount > 1 && amount <= 50; + return amount >= 1 && amount <= 50; } catch (NumberFormatException e) { return false; } diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 044dc82..5faf7cd 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -61,5 +61,5 @@ rocketmq: consume-thread-max: 64 # 消费线程池最大线程数 consume-message-batch-max-size: 64 # 批量消费最大消息数 isRunning: - wx: true + wx: false jd: false