diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/CommentController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/CommentController.java new file mode 100644 index 0000000..1faad19 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/CommentController.java @@ -0,0 +1,137 @@ +package com.ruoyi.web.controller.jarvis; + +import com.ruoyi.common.annotation.Log; +import com.ruoyi.common.core.controller.BaseController; +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.core.page.TableDataInfo; +import com.ruoyi.common.enums.BusinessType; +import com.ruoyi.common.utils.poi.ExcelUtil; +import com.ruoyi.jarvis.domain.Comment; +import com.ruoyi.jarvis.domain.dto.CommentApiStatistics; +import com.ruoyi.jarvis.domain.dto.CommentStatistics; +import com.ruoyi.jarvis.service.ICommentService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletResponse; +import java.util.List; +import java.util.Map; + +/** + * 评论管理 Controller + */ +@RestController +@RequestMapping("/jarvis/comment") +public class CommentController extends BaseController { + + @Autowired + private ICommentService commentService; + + /** + * 查询京东评论列表 + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:list')") + @GetMapping("/jd/list") + public TableDataInfo list(Comment comment) { + startPage(); + List list = commentService.selectCommentList(comment); + return getDataTable(list); + } + + /** + * 导出京东评论列表 + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:export')") + @Log(title = "京东评论", businessType = BusinessType.EXPORT) + @PostMapping("/jd/export") + public void export(HttpServletResponse response, Comment comment) { + List list = commentService.selectCommentList(comment); + ExcelUtil util = new ExcelUtil(Comment.class); + util.exportExcel(response, list, "京东评论数据"); + } + + /** + * 获取京东评论详细信息 + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:query')") + @GetMapping("/jd/{id}") + public AjaxResult getInfo(@PathVariable("id") Long id) { + return success(commentService.selectCommentById(id)); + } + + /** + * 修改评论使用状态 + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:edit')") + @Log(title = "评论管理", businessType = BusinessType.UPDATE) + @PutMapping("/jd") + public AjaxResult edit(@RequestBody Comment comment) { + return toAjax(commentService.updateCommentIsUse(comment)); + } + + /** + * 删除评论 + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:remove')") + @Log(title = "评论管理", businessType = BusinessType.DELETE) + @DeleteMapping("/jd/{ids}") + public AjaxResult remove(@PathVariable Long[] ids) { + return toAjax(commentService.deleteCommentByIds(ids)); + } + + /** + * 重置评论使用状态(按商品ID) + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:edit')") + @Log(title = "评论管理", businessType = BusinessType.UPDATE) + @PutMapping("/jd/reset/{productId}") + public AjaxResult resetByProductId(@PathVariable String productId) { + return toAjax(commentService.resetCommentIsUseByProductId(productId)); + } + + /** + * 获取评论统计信息 + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:list')") + @GetMapping("/statistics") + public AjaxResult getStatistics(@RequestParam(required = false) String source) { + List statistics = commentService.getCommentStatistics(source); + return success(statistics); + } + + /** + * 获取接口调用统计 + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:list')") + @GetMapping("/api/statistics") + public AjaxResult getApiStatistics( + @RequestParam(required = false) String apiType, + @RequestParam(required = false) String productType, + @RequestParam(required = false) String startDate, + @RequestParam(required = false) String endDate) { + List statistics = commentService.getApiStatistics(apiType, productType, startDate, endDate); + return success(statistics); + } + + /** + * 获取Redis产品类型映射(京东) + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:list')") + @GetMapping("/redis/jd/map") + public AjaxResult getJdProductTypeMap() { + Map map = commentService.getJdProductTypeMap(); + return success(map); + } + + /** + * 获取Redis产品类型映射(淘宝) + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:list')") + @GetMapping("/redis/tb/map") + public AjaxResult getTbProductTypeMap() { + Map map = commentService.getTbProductTypeMap(); + return success(map); + } +} + diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/TaobaoCommentController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/TaobaoCommentController.java new file mode 100644 index 0000000..a26f643 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/jarvis/TaobaoCommentController.java @@ -0,0 +1,90 @@ +package com.ruoyi.web.controller.jarvis; + +import com.ruoyi.common.annotation.Log; +import com.ruoyi.common.core.controller.BaseController; +import com.ruoyi.common.core.domain.AjaxResult; +import com.ruoyi.common.core.page.TableDataInfo; +import com.ruoyi.common.enums.BusinessType; +import com.ruoyi.common.utils.poi.ExcelUtil; +import com.ruoyi.jarvis.domain.TaobaoComment; +import com.ruoyi.jarvis.service.ITaobaoCommentService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletResponse; +import java.util.List; + +/** + * 淘宝评论管理 Controller + */ +@RestController +@RequestMapping("/jarvis/taobaoComment") +public class TaobaoCommentController extends BaseController { + + @Autowired + private ITaobaoCommentService taobaoCommentService; + + /** + * 查询淘宝评论列表 + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:list')") + @GetMapping("/list") + public TableDataInfo list(TaobaoComment taobaoComment) { + startPage(); + List list = taobaoCommentService.selectTaobaoCommentList(taobaoComment); + return getDataTable(list); + } + + /** + * 导出淘宝评论列表 + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:export')") + @Log(title = "淘宝评论", businessType = BusinessType.EXPORT) + @PostMapping("/export") + public void export(HttpServletResponse response, TaobaoComment taobaoComment) { + List list = taobaoCommentService.selectTaobaoCommentList(taobaoComment); + ExcelUtil util = new ExcelUtil(TaobaoComment.class); + util.exportExcel(response, list, "淘宝评论数据"); + } + + /** + * 获取淘宝评论详细信息 + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:query')") + @GetMapping("/{id}") + public AjaxResult getInfo(@PathVariable("id") Integer id) { + return success(taobaoCommentService.selectTaobaoCommentById(id)); + } + + /** + * 修改评论使用状态 + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:edit')") + @Log(title = "评论管理", businessType = BusinessType.UPDATE) + @PutMapping + public AjaxResult edit(@RequestBody TaobaoComment taobaoComment) { + return toAjax(taobaoCommentService.updateTaobaoCommentIsUse(taobaoComment)); + } + + /** + * 删除评论 + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:remove')") + @Log(title = "评论管理", businessType = BusinessType.DELETE) + @DeleteMapping("/{ids}") + public AjaxResult remove(@PathVariable Integer[] ids) { + return toAjax(taobaoCommentService.deleteTaobaoCommentByIds(ids)); + } + + /** + * 重置评论使用状态(按商品ID) + */ + @PreAuthorize("@ss.hasPermi('jarvis:comment:edit')") + @Log(title = "评论管理", businessType = BusinessType.UPDATE) + @PutMapping("/reset/{productId}") + public AjaxResult resetByProductId(@PathVariable String productId) { + return toAjax(taobaoCommentService.resetTaobaoCommentIsUseByProductId(productId)); + } +} + diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/publicapi/CommentPublicController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/publicapi/CommentPublicController.java index a6f8feb..03ceeed 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/publicapi/CommentPublicController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/publicapi/CommentPublicController.java @@ -6,6 +6,8 @@ import com.ruoyi.common.core.domain.AjaxResult; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONObject; import com.ruoyi.common.utils.http.HttpUtils; +import com.ruoyi.jarvis.service.ICommentService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.*; @@ -22,18 +24,28 @@ public class CommentPublicController extends BaseController { private static final String JD_BASE = "http://192.168.8.88:6666/jd"; private static final String SKEY = "2192057370ef8140c201079969c956a3"; + @Autowired(required = false) + private ICommentService commentService; + /** * 获取可选型号/类型(示例) */ @GetMapping("/types") public AjaxResult types() { + boolean success = false; try { String url = JD_BASE + "/comment/types?skey=" + SKEY; String result = HttpUtils.sendGet(url); Object parsed = JSON.parse(result); + success = true; return AjaxResult.success(parsed); } catch (Exception e) { return AjaxResult.error("types failed: " + e.getMessage()); + } finally { + // 记录接口调用统计 + if (commentService != null) { + commentService.recordApiCall("jd", "types", success); + } } } @@ -43,18 +55,27 @@ public class CommentPublicController extends BaseController { */ @PostMapping("/generate") public AjaxResult generate(@RequestBody Map body) { + boolean success = false; + String productType = null; try { String url = JD_BASE + "/comment/generate"; JSONObject param = new JSONObject(); param.put("skey", SKEY); if (body != null && body.get("productType") != null) { - param.put("productType", body.get("productType")); + productType = body.get("productType"); + param.put("productType", productType); } String result = HttpUtils.sendJsonPost(url, param.toJSONString()); Object parsed = JSON.parse(result); + success = true; return AjaxResult.success(parsed); } catch (Exception e) { return AjaxResult.error("generate failed: " + e.getMessage()); + } finally { + // 记录接口调用统计 + if (commentService != null && productType != null) { + commentService.recordApiCall("jd", productType, success); + } } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/domain/Comment.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/domain/Comment.java new file mode 100644 index 0000000..f077685 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/domain/Comment.java @@ -0,0 +1,60 @@ +package com.ruoyi.jarvis.domain; + +import com.ruoyi.common.core.domain.BaseEntity; +import com.ruoyi.common.annotation.Excel; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.util.Date; + +/** + * 京东评论对象 comments + */ +@Data +@EqualsAndHashCode(callSuper = false) +public class Comment extends BaseEntity { + private static final long serialVersionUID = 1L; + + /** 主键ID */ + @Excel(name = "ID") + private Long id; + + /** 商品ID */ + @Excel(name = "商品ID") + private String productId; + + /** 用户名 */ + @Excel(name = "用户名") + private String userName; + + /** 评论内容 */ + @Excel(name = "评论内容") + private String commentText; + + /** 评论ID */ + @Excel(name = "评论ID") + private String commentId; + + /** 图片URLs */ + @Excel(name = "图片URLs") + private String pictureUrls; + + /** 创建时间 */ + @Excel(name = "创建时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss") + private Date createdAt; + + /** 评论日期 */ + @Excel(name = "评论日期", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss") + private Date commentDate; + + /** 是否已使用 0-未使用 1-已使用 */ + @Excel(name = "使用状态", readConverterExp = "0=未使用,1=已使用") + private Integer isUse; + + /** 产品类型(从Redis映射获取) */ + private String productType; + + /** Redis映射的产品ID */ + private String mappedProductId; +} + diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/domain/TaobaoComment.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/domain/TaobaoComment.java new file mode 100644 index 0000000..cdb33aa --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/domain/TaobaoComment.java @@ -0,0 +1,58 @@ +package com.ruoyi.jarvis.domain; + +import com.ruoyi.common.core.domain.BaseEntity; +import com.ruoyi.common.annotation.Excel; +import lombok.Data; + +import java.util.Date; + +/** + * 淘宝评论对象 taobao_comments + */ +@Data +public class TaobaoComment extends BaseEntity { + private static final long serialVersionUID = 1L; + + /** 主键ID */ + @Excel(name = "ID") + private Integer id; + + /** 商品ID */ + @Excel(name = "商品ID") + private String productId; + + /** 用户名 */ + @Excel(name = "用户名") + private String userName; + + /** 评论内容 */ + @Excel(name = "评论内容") + private String commentText; + + /** 评论ID */ + @Excel(name = "评论ID") + private String commentId; + + /** 图片URLs */ + @Excel(name = "图片URLs") + private String pictureUrls; + + /** 创建时间 */ + @Excel(name = "创建时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss") + private Date createdAt; + + /** 评论日期 */ + @Excel(name = "评论日期") + private String commentDate; + + /** 是否已使用 0-未使用 1-已使用 */ + @Excel(name = "使用状态", readConverterExp = "0=未使用,1=已使用") + private Integer isUse; + + /** 产品类型(从Redis映射获取) */ + private String productType; + + /** Redis映射的产品ID */ + private String mappedProductId; +} + diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/domain/dto/CommentApiStatistics.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/domain/dto/CommentApiStatistics.java new file mode 100644 index 0000000..b986ccf --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/domain/dto/CommentApiStatistics.java @@ -0,0 +1,30 @@ +package com.ruoyi.jarvis.domain.dto; + +import lombok.Data; + +import java.util.Date; + +/** + * 评论接口调用统计 + */ +@Data +public class CommentApiStatistics { + /** 统计日期 */ + private Date statDate; + + /** 接口类型:jd-京东,tb-淘宝 */ + private String apiType; + + /** 产品类型 */ + private String productType; + + /** 调用次数 */ + private Long callCount; + + /** 成功次数 */ + private Long successCount; + + /** 失败次数 */ + private Long failCount; +} + diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/domain/dto/CommentStatistics.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/domain/dto/CommentStatistics.java new file mode 100644 index 0000000..79f9992 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/domain/dto/CommentStatistics.java @@ -0,0 +1,34 @@ +package com.ruoyi.jarvis.domain.dto; + +import lombok.Data; + +/** + * 评论统计信息 + */ +@Data +public class CommentStatistics { + /** 评论来源:jd-京东,tb-淘宝 */ + private String source; + + /** 产品类型 */ + private String productType; + + /** 产品ID */ + private String productId; + + /** 总评论数 */ + private Long totalCount; + + /** 可用评论数(未使用) */ + private Long availableCount; + + /** 已使用评论数 */ + private Long usedCount; + + /** 接口调用次数 */ + private Long apiCallCount; + + /** 今日调用次数 */ + private Long todayCallCount; +} + diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/mapper/CommentMapper.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/mapper/CommentMapper.java new file mode 100644 index 0000000..758fa94 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/mapper/CommentMapper.java @@ -0,0 +1,42 @@ +package com.ruoyi.jarvis.mapper; + +import com.ruoyi.jarvis.domain.Comment; +import java.util.List; +import java.util.Map; + +/** + * 京东评论 Mapper 接口 + */ +public interface CommentMapper { + + /** + * 查询京东评论列表 + */ + List selectCommentList(Comment comment); + + /** + * 根据ID查询京东评论 + */ + Comment selectCommentById(Long id); + + /** + * 根据商品ID查询评论统计 + */ + Map selectCommentStatisticsByProductId(String productId); + + /** + * 更新评论使用状态 + */ + int updateCommentIsUse(Comment comment); + + /** + * 批量删除评论 + */ + int deleteCommentByIds(Long[] ids); + + /** + * 重置评论使用状态(批量) + */ + int resetCommentIsUseByProductId(String productId); +} + diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/mapper/TaobaoCommentMapper.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/mapper/TaobaoCommentMapper.java new file mode 100644 index 0000000..1fd242c --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/mapper/TaobaoCommentMapper.java @@ -0,0 +1,42 @@ +package com.ruoyi.jarvis.mapper; + +import com.ruoyi.jarvis.domain.TaobaoComment; +import java.util.List; +import java.util.Map; + +/** + * 淘宝评论 Mapper 接口 + */ +public interface TaobaoCommentMapper { + + /** + * 查询淘宝评论列表 + */ + List selectTaobaoCommentList(TaobaoComment taobaoComment); + + /** + * 根据ID查询淘宝评论 + */ + TaobaoComment selectTaobaoCommentById(Integer id); + + /** + * 根据商品ID查询评论统计 + */ + Map selectTaobaoCommentStatisticsByProductId(String productId); + + /** + * 更新评论使用状态 + */ + int updateTaobaoCommentIsUse(TaobaoComment taobaoComment); + + /** + * 批量删除评论 + */ + int deleteTaobaoCommentByIds(Integer[] ids); + + /** + * 重置评论使用状态(批量) + */ + int resetTaobaoCommentIsUseByProductId(String productId); +} + diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/ICommentService.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/ICommentService.java new file mode 100644 index 0000000..eaaf328 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/ICommentService.java @@ -0,0 +1,65 @@ +package com.ruoyi.jarvis.service; + +import com.ruoyi.jarvis.domain.Comment; +import com.ruoyi.jarvis.domain.dto.CommentStatistics; +import com.ruoyi.jarvis.domain.dto.CommentApiStatistics; + +import java.util.List; +import java.util.Map; + +/** + * 评论管理 Service 接口 + */ +public interface ICommentService { + + /** + * 查询京东评论列表 + */ + List selectCommentList(Comment comment); + + /** + * 根据ID查询京东评论 + */ + Comment selectCommentById(Long id); + + /** + * 更新评论使用状态 + */ + int updateCommentIsUse(Comment comment); + + /** + * 批量删除评论 + */ + int deleteCommentByIds(Long[] ids); + + /** + * 重置评论使用状态(批量) + */ + int resetCommentIsUseByProductId(String productId); + + /** + * 获取评论统计信息(包含Redis映射) + */ + List getCommentStatistics(String source); + + /** + * 记录接口调用统计 + */ + void recordApiCall(String apiType, String productType, boolean success); + + /** + * 获取接口调用统计 + */ + List getApiStatistics(String apiType, String productType, String startDate, String endDate); + + /** + * 获取Redis产品类型映射(京东) + */ + Map getJdProductTypeMap(); + + /** + * 获取Redis产品类型映射(淘宝) + */ + Map getTbProductTypeMap(); +} + diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/ITaobaoCommentService.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/ITaobaoCommentService.java new file mode 100644 index 0000000..daf45a6 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/ITaobaoCommentService.java @@ -0,0 +1,37 @@ +package com.ruoyi.jarvis.service; + +import com.ruoyi.jarvis.domain.TaobaoComment; + +import java.util.List; + +/** + * 淘宝评论管理 Service 接口 + */ +public interface ITaobaoCommentService { + + /** + * 查询淘宝评论列表 + */ + List selectTaobaoCommentList(TaobaoComment taobaoComment); + + /** + * 根据ID查询淘宝评论 + */ + TaobaoComment selectTaobaoCommentById(Integer id); + + /** + * 更新评论使用状态 + */ + int updateTaobaoCommentIsUse(TaobaoComment taobaoComment); + + /** + * 批量删除评论 + */ + int deleteTaobaoCommentByIds(Integer[] ids); + + /** + * 重置评论使用状态(批量) + */ + int resetTaobaoCommentIsUseByProductId(String productId); +} + diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/CommentServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/CommentServiceImpl.java new file mode 100644 index 0000000..baa5b49 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/CommentServiceImpl.java @@ -0,0 +1,282 @@ +package com.ruoyi.jarvis.service.impl; + +import com.ruoyi.jarvis.domain.Comment; +import com.ruoyi.jarvis.domain.dto.CommentApiStatistics; +import com.ruoyi.jarvis.domain.dto.CommentStatistics; +import com.ruoyi.jarvis.mapper.CommentMapper; +import com.ruoyi.jarvis.service.ICommentService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Service; + +import java.text.SimpleDateFormat; +import java.util.*; +import java.util.concurrent.TimeUnit; + +/** + * 评论管理 Service 实现 + */ +@Service +public class CommentServiceImpl implements ICommentService { + + private static final Logger log = LoggerFactory.getLogger(CommentServiceImpl.class); + + private static final String PRODUCT_TYPE_MAP_PREFIX = "product_type_map"; + private static final String PRODUCT_TYPE_MAP_PREFIX_TB = "product_type_map_tb"; + private static final String API_CALL_STAT_PREFIX = "comment:api:stat:"; + private static final String API_CALL_TODAY_PREFIX = "comment:api:today:"; + + @Autowired + private CommentMapper commentMapper; + + @Autowired(required = false) + private com.ruoyi.jarvis.mapper.TaobaoCommentMapper taobaoCommentMapper; + + @Autowired(required = false) + private StringRedisTemplate stringRedisTemplate; + + @Override + public List selectCommentList(Comment comment) { + List list = commentMapper.selectCommentList(comment); + // 填充Redis映射的产品类型信息 + if (stringRedisTemplate != null) { + Map jdMap = getJdProductTypeMap(); + for (Comment c : list) { + // 查找对应的产品类型 + String productId = c.getProductId(); + if (jdMap != null) { + for (Map.Entry entry : jdMap.entrySet()) { + if (entry.getValue().equals(productId)) { + c.setProductType(entry.getKey()); + c.setMappedProductId(productId); + break; + } + } + } + } + } + return list; + } + + @Override + public Comment selectCommentById(Long id) { + Comment comment = commentMapper.selectCommentById(id); + if (comment != null && stringRedisTemplate != null) { + Map jdMap = getJdProductTypeMap(); + if (jdMap != null) { + String productId = comment.getProductId(); + for (Map.Entry entry : jdMap.entrySet()) { + if (entry.getValue().equals(productId)) { + comment.setProductType(entry.getKey()); + comment.setMappedProductId(productId); + break; + } + } + } + } + return comment; + } + + @Override + public int updateCommentIsUse(Comment comment) { + return commentMapper.updateCommentIsUse(comment); + } + + @Override + public int deleteCommentByIds(Long[] ids) { + return commentMapper.deleteCommentByIds(ids); + } + + @Override + public int resetCommentIsUseByProductId(String productId) { + return commentMapper.resetCommentIsUseByProductId(productId); + } + + @Override + public List getCommentStatistics(String source) { + List statisticsList = new ArrayList<>(); + + Map productTypeMap = null; + if ("jd".equals(source) || source == null) { + productTypeMap = getJdProductTypeMap(); + } else if ("tb".equals(source)) { + productTypeMap = getTbProductTypeMap(); + } + + if (productTypeMap == null || productTypeMap.isEmpty()) { + return statisticsList; + } + + for (Map.Entry entry : productTypeMap.entrySet()) { + String productType = entry.getKey(); + String productId = entry.getValue(); + + CommentStatistics stats = new CommentStatistics(); + stats.setSource("jd".equals(source) ? "京东评论" : "淘宝评论"); + stats.setProductType(productType); + stats.setProductId(productId); + + // 查询评论统计 + Map statMap = null; + if ("jd".equals(source) || source == null) { + statMap = commentMapper.selectCommentStatisticsByProductId(productId); + } else if ("tb".equals(source) && taobaoCommentMapper != null) { + statMap = taobaoCommentMapper.selectTaobaoCommentStatisticsByProductId(productId); + } + + if (statMap != null) { + stats.setTotalCount(((Number) statMap.get("totalCount")).longValue()); + stats.setAvailableCount(((Number) statMap.get("availableCount")).longValue()); + stats.setUsedCount(((Number) statMap.get("usedCount")).longValue()); + } + + // 获取接口调用统计 + if (stringRedisTemplate != null) { + String todayKey = API_CALL_TODAY_PREFIX + source + ":" + productType + ":" + getTodayDate(); + String todayCount = stringRedisTemplate.opsForValue().get(todayKey); + stats.setTodayCallCount(todayCount != null ? Long.parseLong(todayCount) : 0L); + + // 获取总调用次数(从Redis中统计) + String statKey = API_CALL_STAT_PREFIX + source + ":" + productType; + String totalCount = stringRedisTemplate.opsForValue().get(statKey); + stats.setApiCallCount(totalCount != null ? Long.parseLong(totalCount) : 0L); + } + + statisticsList.add(stats); + } + + return statisticsList; + } + + @Override + public void recordApiCall(String apiType, String productType, boolean success) { + if (stringRedisTemplate == null) { + return; + } + + try { + String today = getTodayDate(); + String todayKey = API_CALL_TODAY_PREFIX + apiType + ":" + productType + ":" + today; + stringRedisTemplate.opsForValue().increment(todayKey); + stringRedisTemplate.expire(todayKey, 7, TimeUnit.DAYS); // 保留7天 + + String statKey = API_CALL_STAT_PREFIX + apiType + ":" + productType; + stringRedisTemplate.opsForValue().increment(statKey); + + // 记录成功/失败统计 + String successKey = API_CALL_STAT_PREFIX + apiType + ":" + productType + ":success"; + String failKey = API_CALL_STAT_PREFIX + apiType + ":" + productType + ":fail"; + if (success) { + stringRedisTemplate.opsForValue().increment(successKey); + } else { + stringRedisTemplate.opsForValue().increment(failKey); + } + } catch (Exception e) { + log.error("记录接口调用统计失败", e); + } + } + + @Override + public List getApiStatistics(String apiType, String productType, String startDate, String endDate) { + List statisticsList = new ArrayList<>(); + + if (stringRedisTemplate == null) { + return statisticsList; + } + + try { + // 如果指定了产品类型,只查询该类型的统计 + if (productType != null && !productType.isEmpty()) { + CommentApiStatistics stats = new CommentApiStatistics(); + stats.setApiType(apiType); + stats.setProductType(productType); + + String statKey = API_CALL_STAT_PREFIX + apiType + ":" + productType; + String totalCount = stringRedisTemplate.opsForValue().get(statKey); + stats.setCallCount(totalCount != null ? Long.parseLong(totalCount) : 0L); + + String successKey = statKey + ":success"; + String successCount = stringRedisTemplate.opsForValue().get(successKey); + stats.setSuccessCount(successCount != null ? Long.parseLong(successCount) : 0L); + + String failKey = statKey + ":fail"; + String failCount = stringRedisTemplate.opsForValue().get(failKey); + stats.setFailCount(failCount != null ? Long.parseLong(failCount) : 0L); + + statisticsList.add(stats); + } else { + // 查询所有产品类型的统计 + Map productTypeMap = "jd".equals(apiType) ? getJdProductTypeMap() : getTbProductTypeMap(); + if (productTypeMap != null) { + for (String pt : productTypeMap.keySet()) { + CommentApiStatistics stats = new CommentApiStatistics(); + stats.setApiType(apiType); + stats.setProductType(pt); + + String statKey = API_CALL_STAT_PREFIX + apiType + ":" + pt; + String totalCount = stringRedisTemplate.opsForValue().get(statKey); + stats.setCallCount(totalCount != null ? Long.parseLong(totalCount) : 0L); + + String successKey = statKey + ":success"; + String successCount = stringRedisTemplate.opsForValue().get(successKey); + stats.setSuccessCount(successCount != null ? Long.parseLong(successCount) : 0L); + + String failKey = statKey + ":fail"; + String failCount = stringRedisTemplate.opsForValue().get(failKey); + stats.setFailCount(failCount != null ? Long.parseLong(failCount) : 0L); + + statisticsList.add(stats); + } + } + } + } catch (Exception e) { + log.error("获取接口调用统计失败", e); + } + + return statisticsList; + } + + @Override + public Map getJdProductTypeMap() { + if (stringRedisTemplate == null) { + return new HashMap<>(); + } + try { + Map rawMap = stringRedisTemplate.opsForHash().entries(PRODUCT_TYPE_MAP_PREFIX); + Map result = new LinkedHashMap<>(); + for (Map.Entry entry : rawMap.entrySet()) { + result.put(entry.getKey().toString(), entry.getValue().toString()); + } + return result; + } catch (Exception e) { + log.error("获取京东产品类型映射失败", e); + return new HashMap<>(); + } + } + + @Override + public Map getTbProductTypeMap() { + if (stringRedisTemplate == null) { + return new HashMap<>(); + } + try { + Map rawMap = stringRedisTemplate.opsForHash().entries(PRODUCT_TYPE_MAP_PREFIX_TB); + Map result = new LinkedHashMap<>(); + for (Map.Entry entry : rawMap.entrySet()) { + result.put(entry.getKey().toString(), entry.getValue().toString()); + } + return result; + } catch (Exception e) { + log.error("获取淘宝产品类型映射失败", e); + return new HashMap<>(); + } + } + + private String getTodayDate() { + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + return sdf.format(new Date()); + } +} + diff --git a/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/TaobaoCommentServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/TaobaoCommentServiceImpl.java new file mode 100644 index 0000000..16c07dd --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/jarvis/service/impl/TaobaoCommentServiceImpl.java @@ -0,0 +1,100 @@ +package com.ruoyi.jarvis.service.impl; + +import com.ruoyi.jarvis.domain.TaobaoComment; +import com.ruoyi.jarvis.mapper.TaobaoCommentMapper; +import com.ruoyi.jarvis.service.ITaobaoCommentService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; + +/** + * 淘宝评论管理 Service 实现 + */ +@Service +public class TaobaoCommentServiceImpl implements ITaobaoCommentService { + + private static final String PRODUCT_TYPE_MAP_PREFIX_TB = "product_type_map_tb"; + + @Autowired + private TaobaoCommentMapper taobaoCommentMapper; + + @Autowired(required = false) + private StringRedisTemplate stringRedisTemplate; + + @Override + public List selectTaobaoCommentList(TaobaoComment taobaoComment) { + List list = taobaoCommentMapper.selectTaobaoCommentList(taobaoComment); + // 填充Redis映射的产品类型信息 + if (stringRedisTemplate != null) { + Map tbMap = getTbProductTypeMap(); + for (TaobaoComment c : list) { + // 查找对应的产品类型 + String productId = c.getProductId(); + if (tbMap != null) { + for (Map.Entry entry : tbMap.entrySet()) { + if (entry.getValue().equals(productId)) { + c.setProductType(entry.getKey()); + c.setMappedProductId(productId); + break; + } + } + } + } + } + return list; + } + + @Override + public TaobaoComment selectTaobaoCommentById(Integer id) { + TaobaoComment comment = taobaoCommentMapper.selectTaobaoCommentById(id); + if (comment != null && stringRedisTemplate != null) { + Map tbMap = getTbProductTypeMap(); + if (tbMap != null) { + String productId = comment.getProductId(); + for (Map.Entry entry : tbMap.entrySet()) { + if (entry.getValue().equals(productId)) { + comment.setProductType(entry.getKey()); + comment.setMappedProductId(productId); + break; + } + } + } + } + return comment; + } + + @Override + public int updateTaobaoCommentIsUse(TaobaoComment taobaoComment) { + return taobaoCommentMapper.updateTaobaoCommentIsUse(taobaoComment); + } + + @Override + public int deleteTaobaoCommentByIds(Integer[] ids) { + return taobaoCommentMapper.deleteTaobaoCommentByIds(ids); + } + + @Override + public int resetTaobaoCommentIsUseByProductId(String productId) { + return taobaoCommentMapper.resetTaobaoCommentIsUseByProductId(productId); + } + + private Map getTbProductTypeMap() { + if (stringRedisTemplate == null) { + return null; + } + try { + Map rawMap = stringRedisTemplate.opsForHash().entries(PRODUCT_TYPE_MAP_PREFIX_TB); + Map result = new java.util.LinkedHashMap<>(); + for (Map.Entry entry : rawMap.entrySet()) { + result.put(entry.getKey().toString(), entry.getValue().toString()); + } + return result; + } catch (Exception e) { + return null; + } + } +} + diff --git a/ruoyi-system/src/main/resources/mapper/jarvis/CommentMapper.xml b/ruoyi-system/src/main/resources/mapper/jarvis/CommentMapper.xml new file mode 100644 index 0000000..b899f8b --- /dev/null +++ b/ruoyi-system/src/main/resources/mapper/jarvis/CommentMapper.xml @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + select id, product_id, user_name, comment_text, comment_id, picture_urls, + created_at, comment_date, is_use + from comments + + + + + + + + + + update comments + + is_use = #{isUse}, + + where id = #{id} + + + + update comments + set is_use = 0 + where product_id = #{productId} + + + + delete from comments where id in + + #{id} + + + + + diff --git a/ruoyi-system/src/main/resources/mapper/jarvis/TaobaoCommentMapper.xml b/ruoyi-system/src/main/resources/mapper/jarvis/TaobaoCommentMapper.xml new file mode 100644 index 0000000..f465f6c --- /dev/null +++ b/ruoyi-system/src/main/resources/mapper/jarvis/TaobaoCommentMapper.xml @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + select id, product_id, user_name, comment_text, comment_id, picture_urls, + created_at, comment_date, is_use + from taobao_comments + + + + + + + + + + update taobao_comments + + is_use = #{isUse}, + + where id = #{id} + + + + update taobao_comments + set is_use = 0 + where product_id = #{productId} + + + + delete from taobao_comments where id in + + #{id} + + + + +