1
This commit is contained in:
@@ -306,4 +306,250 @@ public class JDOrderController extends BaseController {
|
||||
return AjaxResult.error("转链失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量创建礼金券并生成包含礼金的推广链接
|
||||
* 入参:skuId/materialUrl, amount, quantity, batchSize, owner, skuName
|
||||
* 返回:批量创建结果
|
||||
*/
|
||||
@PostMapping("/batchCreateGiftCoupons")
|
||||
public AjaxResult batchCreateGiftCoupons(@RequestBody Map<String, Object> body) {
|
||||
try {
|
||||
logger.info("批量创建礼金请求 - 参数: materialUrl={}, skuId={}, amount={}, quantity={}, batchSize={}, owner={}, skuName={}",
|
||||
body.get("materialUrl"), body.get("skuId"), body.get("amount"),
|
||||
body.get("quantity"), body.get("batchSize"), body.get("owner"), body.get("skuName"));
|
||||
|
||||
String url = requestUrl + "batchCreateGiftCoupons";
|
||||
JSONObject param = new JSONObject();
|
||||
param.put("skey", skey);
|
||||
if (body.get("materialUrl") != null) param.put("materialUrl", body.get("materialUrl"));
|
||||
if (body.get("skuId") != null) param.put("skuId", body.get("skuId"));
|
||||
if (body.get("amount") != null) param.put("amount", body.get("amount"));
|
||||
if (body.get("quantity") != null) param.put("quantity", body.get("quantity"));
|
||||
if (body.get("batchSize") != null) param.put("batchSize", body.get("batchSize"));
|
||||
if (body.get("owner") != null) param.put("owner", body.get("owner"));
|
||||
if (body.get("skuName") != null) param.put("skuName", body.get("skuName"));
|
||||
|
||||
logger.info("批量创建礼金请求 - 发送到内部服务: {}, 参数: {}", url, param.toJSONString());
|
||||
|
||||
String result = HttpUtils.sendJsonPost(url, param.toJSONString());
|
||||
logger.info("批量创建礼金响应 - 原始响应: {}", result);
|
||||
|
||||
try {
|
||||
Object parsed = JSON.parse(result);
|
||||
|
||||
if (parsed instanceof JSONObject) {
|
||||
JSONObject jsonObj = (JSONObject) parsed;
|
||||
|
||||
// 检查是否是错误响应
|
||||
if (jsonObj.containsKey("error")) {
|
||||
String errorMsg = jsonObj.getString("error");
|
||||
logger.error("批量创建礼金失败 - JD项目返回错误: {}", errorMsg);
|
||||
return AjaxResult.error(errorMsg);
|
||||
}
|
||||
|
||||
// 保存成功的礼金到数据库
|
||||
try {
|
||||
JSONArray results = jsonObj.getJSONArray("results");
|
||||
if (results != null) {
|
||||
int savedCount = 0;
|
||||
for (int i = 0; i < results.size(); i++) {
|
||||
JSONObject item = results.getJSONObject(i);
|
||||
if (Boolean.TRUE.equals(item.getBoolean("success"))) {
|
||||
String giftKey = item.getString("giftCouponKey");
|
||||
if (giftKey != null && !giftKey.trim().isEmpty()) {
|
||||
try {
|
||||
GiftCoupon giftCoupon = new GiftCoupon();
|
||||
giftCoupon.setGiftCouponKey(giftKey);
|
||||
|
||||
String skuId = (String) body.get("skuId");
|
||||
if (skuId == null || skuId.trim().isEmpty()) {
|
||||
String materialUrl = (String) body.get("materialUrl");
|
||||
if (materialUrl != null && !materialUrl.trim().isEmpty()) {
|
||||
java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("(?:item\\.jd\\.com|jd\\.com)/(\\d+)", java.util.regex.Pattern.CASE_INSENSITIVE);
|
||||
java.util.regex.Matcher matcher = pattern.matcher(materialUrl);
|
||||
if (matcher.find()) {
|
||||
skuId = matcher.group(1);
|
||||
} else if (materialUrl.matches("^\\d+$")) {
|
||||
skuId = materialUrl;
|
||||
}
|
||||
}
|
||||
}
|
||||
giftCoupon.setSkuId(skuId);
|
||||
giftCoupon.setSkuName((String) body.get("skuName"));
|
||||
giftCoupon.setOwner((String) body.get("owner"));
|
||||
|
||||
Object amountObj = body.get("amount");
|
||||
if (amountObj != null) {
|
||||
giftCoupon.setAmount(Double.parseDouble(String.valueOf(amountObj)));
|
||||
}
|
||||
Object quantityObj = body.get("quantity");
|
||||
if (quantityObj != null) {
|
||||
giftCoupon.setQuantity(Integer.parseInt(String.valueOf(quantityObj)));
|
||||
}
|
||||
giftCoupon.setCreateTime(new java.util.Date());
|
||||
|
||||
java.util.Calendar cal = java.util.Calendar.getInstance();
|
||||
if ("pop".equalsIgnoreCase((String) body.get("owner"))) {
|
||||
cal.add(java.util.Calendar.DAY_OF_YEAR, 7);
|
||||
} else {
|
||||
cal.add(java.util.Calendar.DAY_OF_YEAR, 1);
|
||||
}
|
||||
giftCoupon.setExpireTime(cal.getTime());
|
||||
|
||||
giftCouponService.insertGiftCoupon(giftCoupon);
|
||||
savedCount++;
|
||||
} catch (Exception e) {
|
||||
logger.warn("保存礼金信息到数据库失败 - giftCouponKey={}, error={}", giftKey, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
logger.info("批量创建礼金 - 保存到数据库: {}/{}", savedCount, results.size());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("保存礼金信息到数据库失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
return AjaxResult.success(parsed);
|
||||
} catch (Exception e) {
|
||||
logger.warn("批量创建礼金响应 - JSON解析失败,返回字符串: {}", result);
|
||||
return AjaxResult.success(result);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("批量创建礼金异常", e);
|
||||
return AjaxResult.error("批量创建礼金失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文本替换:将文本中的URL替换为包含礼金的推广链接
|
||||
* 入参:{ content, skuId/materialUrl, amount, quantity, batchSize, owner, skuName }
|
||||
* 返回:替换后的文本和礼金信息
|
||||
*/
|
||||
@PostMapping("/replaceUrlsWithGiftCoupons")
|
||||
public AjaxResult replaceUrlsWithGiftCoupons(@RequestBody Map<String, Object> body) {
|
||||
try {
|
||||
String content = (String) body.get("content");
|
||||
if (content == null || content.trim().isEmpty()) {
|
||||
return AjaxResult.error("content is required");
|
||||
}
|
||||
|
||||
logger.info("文本URL替换请求 - content长度={}, 参数: materialUrl={}, skuId={}, amount={}, quantity={}, batchSize={}, owner={}",
|
||||
content.length(), body.get("materialUrl"), body.get("skuId"), body.get("amount"),
|
||||
body.get("quantity"), body.get("batchSize"), body.get("owner"));
|
||||
|
||||
// 提取文本中的所有URL(京东链接)
|
||||
java.util.List<String> urls = new java.util.ArrayList<>();
|
||||
java.util.regex.Pattern urlPattern = java.util.regex.Pattern.compile(
|
||||
"(https?://[^\\s]+)|(u\\.jd\\.com/[^\\s]+)",
|
||||
java.util.regex.Pattern.CASE_INSENSITIVE);
|
||||
java.util.regex.Matcher matcher = urlPattern.matcher(content);
|
||||
|
||||
while (matcher.find()) {
|
||||
String url = matcher.group(0);
|
||||
if (url != null && !url.trim().isEmpty()) {
|
||||
urls.add(url.trim());
|
||||
}
|
||||
}
|
||||
|
||||
logger.info("文本URL替换 - 提取到{}个URL", urls.size());
|
||||
|
||||
if (urls.isEmpty()) {
|
||||
return AjaxResult.success(new JSONObject().fluentPut("replacedContent", content)
|
||||
.fluentPut("message", "未找到URL,无需替换"));
|
||||
}
|
||||
|
||||
// 批量创建礼金券
|
||||
Map<String, Object> batchParams = new java.util.HashMap<>();
|
||||
batchParams.put("materialUrl", body.get("materialUrl"));
|
||||
batchParams.put("skuId", body.get("skuId"));
|
||||
batchParams.put("amount", body.get("amount"));
|
||||
batchParams.put("quantity", body.get("quantity"));
|
||||
batchParams.put("batchSize", urls.size());
|
||||
batchParams.put("owner", body.get("owner"));
|
||||
batchParams.put("skuName", body.get("skuName"));
|
||||
|
||||
AjaxResult batchResult = batchCreateGiftCoupons(batchParams);
|
||||
|
||||
Integer code = (Integer) batchResult.get(AjaxResult.CODE_TAG);
|
||||
if (code == null || code != 200) {
|
||||
String msg = (String) batchResult.get(AjaxResult.MSG_TAG);
|
||||
return AjaxResult.error("批量创建礼金失败: " + (msg != null ? msg : "未知错误"));
|
||||
}
|
||||
|
||||
// 解析批量创建结果
|
||||
Object data = batchResult.get(AjaxResult.DATA_TAG);
|
||||
JSONObject batchData = null;
|
||||
if (data instanceof JSONObject) {
|
||||
batchData = (JSONObject) data;
|
||||
} else if (data instanceof String) {
|
||||
try {
|
||||
batchData = JSON.parseObject((String) data);
|
||||
} catch (Exception e) {
|
||||
logger.error("解析批量创建结果失败", e);
|
||||
return AjaxResult.error("解析批量创建结果失败");
|
||||
}
|
||||
}
|
||||
|
||||
if (batchData == null || !batchData.containsKey("results")) {
|
||||
return AjaxResult.error("批量创建结果格式错误");
|
||||
}
|
||||
|
||||
JSONArray results = batchData.getJSONArray("results");
|
||||
if (results == null || results.size() != urls.size()) {
|
||||
logger.warn("批量创建礼金数量不匹配 - 期望={}, 实际={}", urls.size(), results != null ? results.size() : 0);
|
||||
}
|
||||
|
||||
if (results == null) {
|
||||
return AjaxResult.error("批量创建结果为空");
|
||||
}
|
||||
|
||||
// 替换文本中的URL
|
||||
String replacedContent = content;
|
||||
JSONArray replacementInfo = new JSONArray();
|
||||
|
||||
int minSize = Math.min(urls.size(), results.size());
|
||||
for (int i = 0; i < minSize; i++) {
|
||||
String originalUrl = urls.get(i);
|
||||
JSONObject result = results.getJSONObject(i);
|
||||
|
||||
String newUrl = null;
|
||||
if (Boolean.TRUE.equals(result.getBoolean("success"))) {
|
||||
newUrl = result.getString("shortURL");
|
||||
if (newUrl == null || newUrl.trim().isEmpty()) {
|
||||
newUrl = originalUrl; // 如果转链失败,保持原URL
|
||||
}
|
||||
} else {
|
||||
newUrl = originalUrl; // 创建失败,保持原URL
|
||||
}
|
||||
|
||||
// 替换文本中的URL(支持多种格式)
|
||||
replacedContent = replacedContent.replace(originalUrl, newUrl != null ? newUrl : originalUrl);
|
||||
|
||||
JSONObject info = new JSONObject();
|
||||
info.put("index", i + 1);
|
||||
info.put("originalUrl", originalUrl);
|
||||
info.put("newUrl", newUrl);
|
||||
info.put("success", Boolean.TRUE.equals(result.getBoolean("success")));
|
||||
info.put("giftCouponKey", result.getString("giftCouponKey"));
|
||||
replacementInfo.add(info);
|
||||
}
|
||||
|
||||
logger.info("文本URL替换完成 - 替换了{}个URL", minSize);
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("replacedContent", replacedContent);
|
||||
response.put("originalContent", content);
|
||||
response.put("replacements", replacementInfo);
|
||||
response.put("totalUrls", urls.size());
|
||||
response.put("replacedCount", minSize);
|
||||
|
||||
return AjaxResult.success(response);
|
||||
} catch (Exception e) {
|
||||
logger.error("文本URL替换异常", e);
|
||||
return AjaxResult.error("文本URL替换失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user