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<Comment> 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<Comment> list = commentService.selectCommentList(comment);
        ExcelUtil<Comment> util = new ExcelUtil<Comment>(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<CommentStatistics> 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<CommentApiStatistics> 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<String, String> map = commentService.getJdProductTypeMap();
        return success(map);
    }

    /**
     * 获取Redis产品类型映射（淘宝）
     */
    @PreAuthorize("@ss.hasPermi('jarvis:comment:list')")
    @GetMapping("/redis/tb/map")
    public AjaxResult getTbProductTypeMap() {
        Map<String, String> map = commentService.getTbProductTypeMap();
        return success(map);
    }
}

