This commit is contained in:
2025-08-31 15:08:06 +08:00
parent 49320e35ed
commit 3883cd76b4

View File

@@ -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<String, String> map = jdUtil.getProductTypeMap();
List<JSONObject> 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<String, Object> 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<String, String> map = jdUtil.getProductTypeMap();
String productId = map.get(productType);
if (productId == null || productId.trim().isEmpty()) {
return error("unknown productType");
}
List<Comment> available = commentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(productId, 1);
List<Comment> used = commentRepository.findByProductIdAndIsUseNotAndPictureUrlsIsNotNull(productId, 0);
List<Comment> 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<String> 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<String> 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<String, Object> body) {
String skey = body.get("skey") != null ? String.valueOf(body.get("skey")) : null;