From 3883cd76b45c12b98c1f76a98bef86dd05178cc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8D=92?= Date: Sun, 31 Aug 2025 15:08:06 +0800 Subject: [PATCH] 1 --- .../controller/jd/JDInnerController.java | 113 +++++++++++++++++- 1 file changed, 112 insertions(+), 1 deletion(-) diff --git a/src/main/java/cn/van/business/controller/jd/JDInnerController.java b/src/main/java/cn/van/business/controller/jd/JDInnerController.java index f99ca44..02181a9 100644 --- a/src/main/java/cn/van/business/controller/jd/JDInnerController.java +++ b/src/main/java/cn/van/business/controller/jd/JDInnerController.java @@ -1,6 +1,9 @@ package cn.van.business.controller.jd; import cn.van.business.util.JDProductService; +import cn.van.business.util.JDUtil; +import cn.van.business.repository.CommentRepository; +import cn.van.business.model.pl.Comment; import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import lombok.extern.slf4j.Slf4j; @@ -10,6 +13,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.Map; +import java.util.*; +import java.util.stream.Collectors; @RestController @RequestMapping("/jd") @@ -24,10 +29,14 @@ public class JDInnerController { } private final JDProductService jdProductService; + private final JDUtil jdUtil; + private final CommentRepository commentRepository; @Autowired - public JDInnerController(JDProductService jdProductService) { + public JDInnerController(JDProductService jdProductService, JDUtil jdUtil, CommentRepository commentRepository) { this.jdProductService = jdProductService; + this.jdUtil = jdUtil; + this.commentRepository = commentRepository; } @PostMapping("/generatePromotionContent") @@ -45,6 +54,108 @@ public class JDInnerController { return arr; } + /** + * 获取评论可选类型(型号) + * 返回:[{name, value}],按 name 排序 + */ + @GetMapping("/comment/types") + public Object commentTypes(@RequestParam(value = "skey", required = false) String skey) { + if (checkSkey(skey)) { + return error("invalid skey"); + } + try { + HashMap map = jdUtil.getProductTypeMap(); + List list = map.entrySet().stream() + .map(e -> { + JSONObject o = new JSONObject(); + o.put("name", e.getKey()); + o.put("value", e.getValue()); + return o; + }) + .sorted(Comparator.comparing(o -> o.getString("name"))) + .collect(Collectors.toList()); + return list; + } catch (Exception e) { + logger.error("commentTypes error", e); + return error("commentTypes failed: " + e.getMessage()); + } + } + + /** + * 生成评论(优先返回未使用的评论,若无则返回已使用中的随机一条) + * 入参:{ skey, productType } + * 返回:{ productType, list: [ { commentText, images:[] } ] } + */ + @PostMapping("/comment/generate") + public Object commentGenerate(@RequestBody Map body) { + String skey = body.get("skey") != null ? String.valueOf(body.get("skey")) : null; + if (checkSkey(skey)) { + return error("invalid skey"); + } + String productType = body.get("productType") != null ? String.valueOf(body.get("productType")) : null; + if (productType == null || productType.trim().isEmpty()) { + return error("productType is required"); + } + try { + HashMap map = jdUtil.getProductTypeMap(); + String productId = map.get(productType); + if (productId == null || productId.trim().isEmpty()) { + return error("unknown productType"); + } + List available = commentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(productId, 1); + List used = commentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(productId, 0); + List candidates = !available.isEmpty() ? available : used; + if (candidates.isEmpty()) { + return error("no comment available"); + } + Collections.shuffle(candidates); + Comment chosen = candidates.get(0); + + JSONObject item = new JSONObject(); + item.put("commentText", chosen.getCommentText()); + item.put("images", parsePictureUrls(chosen.getPictureUrls())); + + JSONArray arr = new JSONArray(); + arr.add(item); + JSONObject resp = new JSONObject(); + resp.put("productType", productType); + resp.put("list", arr); + + // 标记为已使用(仅当原本未使用时) + try { + if (chosen.getIsUse() == null || chosen.getIsUse() == 0) { + chosen.setIsUse(1); + commentRepository.save(chosen); + } + } catch (Exception ignore) {} + + return resp; + } catch (Exception e) { + logger.error("commentGenerate error", e); + return error("commentGenerate failed: " + e.getMessage()); + } + } + + private static List parsePictureUrls(String raw) { + if (raw == null || raw.trim().isEmpty()) return Collections.emptyList(); + try { + Object parsed = com.alibaba.fastjson2.JSON.parse(raw); + if (parsed instanceof JSONArray) { + JSONArray ja = (JSONArray) parsed; + List list = new ArrayList<>(); + for (int i = 0; i < ja.size(); i++) { + Object v = ja.get(i); + if (v != null) list.add(String.valueOf(v)); + } + return list; + } + } catch (Exception ignore) {} + // 非 JSON,按逗号或空白分隔 + return Arrays.stream(raw.split("[\n,\t ]+")) + .filter(s -> s != null && !s.trim().isEmpty()) + .collect(Collectors.toList()); + } + @PostMapping("/createGiftCoupon") public Object createGiftCoupon(@RequestBody Map body) { String skey = body.get("skey") != null ? String.valueOf(body.get("skey")) : null;