package cn.van.business.controller; import cn.hutool.core.util.StrUtil; import com.alibaba.fastjson2.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.*; import java.util.HashMap; import java.util.Map; /** * 小红书/抖音提示词模板配置Controller * * @author System */ @Slf4j @RestController @RequestMapping("/jarvis/social-media/prompt") public class SocialMediaPromptController { @Autowired(required = false) private StringRedisTemplate redisTemplate; // Redis Key 前缀 private static final String REDIS_KEY_PREFIX = "social_media:prompt:"; // 模板键名列表 private static final String[] TEMPLATE_KEYS = { "keywords", "content:xhs", "content:douyin", "content:both" }; // 模板说明 private static final Map TEMPLATE_DESCRIPTIONS = new HashMap() {{ put("keywords", "关键词提取提示词模板\n占位符:%s - 商品名称"); put("content:xhs", "小红书文案生成提示词模板\n占位符:%s - 商品名称,%s - 价格信息,%s - 关键词信息"); put("content:douyin", "抖音文案生成提示词模板\n占位符:%s - 商品名称,%s - 价格信息,%s - 关键词信息"); put("content:both", "通用文案生成提示词模板\n占位符:%s - 商品名称,%s - 价格信息,%s - 关键词信息"); }}; /** * 获取所有提示词模板 * * GET /jarvis/social-media/prompt/list */ @GetMapping("/list") public JSONObject listTemplates() { JSONObject response = new JSONObject(); try { Map templates = new HashMap<>(); for (String key : TEMPLATE_KEYS) { Map templateInfo = new HashMap<>(); templateInfo.put("key", key); templateInfo.put("description", TEMPLATE_DESCRIPTIONS.get(key)); String template = getTemplateFromRedis(key); templateInfo.put("template", template); templateInfo.put("isDefault", template == null); templates.put(key, templateInfo); } response.put("code", 200); response.put("msg", "操作成功"); response.put("data", templates); } catch (Exception e) { log.error("获取提示词模板列表失败", e); response.put("code", 500); response.put("msg", "获取失败: " + e.getMessage()); } return response; } /** * 获取单个提示词模板 * * GET /jarvis/social-media/prompt/{key} */ @GetMapping("/{key}") public JSONObject getTemplate(@PathVariable String key) { JSONObject response = new JSONObject(); try { if (!isValidKey(key)) { response.put("code", 400); response.put("msg", "无效的模板键名"); return response; } String template = getTemplateFromRedis(key); Map data = new HashMap<>(); data.put("key", key); data.put("description", TEMPLATE_DESCRIPTIONS.get(key)); data.put("template", template); data.put("isDefault", template == null); response.put("code", 200); response.put("msg", "操作成功"); response.put("data", data); } catch (Exception e) { log.error("获取提示词模板失败", e); response.put("code", 500); response.put("msg", "获取失败: " + e.getMessage()); } return response; } /** * 保存提示词模板 * * POST /jarvis/social-media/prompt/save * * { * "key": "keywords", * "template": "提示词模板内容..." * } */ @PostMapping("/save") public JSONObject saveTemplate(@RequestBody Map request) { JSONObject response = new JSONObject(); try { String key = (String) request.get("key"); String template = (String) request.get("template"); if (!isValidKey(key)) { response.put("code", 400); response.put("msg", "无效的模板键名"); return response; } if (StrUtil.isBlank(template)) { response.put("code", 400); response.put("msg", "模板内容不能为空"); return response; } if (redisTemplate == null) { response.put("code", 500); response.put("msg", "Redis未配置,无法保存模板"); return response; } String redisKey = REDIS_KEY_PREFIX + key; redisTemplate.opsForValue().set(redisKey, template); log.info("保存提示词模板成功: {}", key); response.put("code", 200); response.put("msg", "保存成功"); } catch (Exception e) { log.error("保存提示词模板失败", e); response.put("code", 500); response.put("msg", "保存失败: " + e.getMessage()); } return response; } /** * 删除提示词模板(恢复默认) * * DELETE /jarvis/social-media/prompt/{key} */ @DeleteMapping("/{key}") public JSONObject deleteTemplate(@PathVariable String key) { JSONObject response = new JSONObject(); try { if (!isValidKey(key)) { response.put("code", 400); response.put("msg", "无效的模板键名"); return response; } if (redisTemplate == null) { response.put("code", 500); response.put("msg", "Redis未配置,无法删除模板"); return response; } String redisKey = REDIS_KEY_PREFIX + key; redisTemplate.delete(redisKey); log.info("删除提示词模板成功: {}", key); response.put("code", 200); response.put("msg", "删除成功,已恢复默认模板"); } catch (Exception e) { log.error("删除提示词模板失败", e); response.put("code", 500); response.put("msg", "删除失败: " + e.getMessage()); } return response; } /** * 从 Redis 获取模板 */ private String getTemplateFromRedis(String key) { if (redisTemplate == null) { return null; } try { String redisKey = REDIS_KEY_PREFIX + key; return redisTemplate.opsForValue().get(redisKey); } catch (Exception e) { log.warn("读取Redis模板失败: {}", key, e); return null; } } /** * 验证模板键名是否有效 */ private boolean isValidKey(String key) { if (StrUtil.isBlank(key)) { return false; } for (String validKey : TEMPLATE_KEYS) { if (validKey.equals(key)) { return true; } } return false; } }