package com.ruoyi.web.controller.communityNews;

import com.ruoyi.common.annotation.Anonymous;
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.system.domain.communityNews.CommentIndex;
import com.ruoyi.system.domain.communityNews.vo.CommentIndexShVo;
import com.ruoyi.system.domain.communityNews.vo.CommentIndexVo;
import com.ruoyi.system.service.ICommentIndexService;
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
 *
 * @author boman
 * @date 2025-02-25
 */
@RestController
@RequestMapping("/wuYe/commentIndex")
public class CommentIndexController extends BaseController {
    @Autowired
    private ICommentIndexService commentIndexService;

    /**
     * 查询社区资讯评论列表
     */
    @PreAuthorize("@ss.hasPermi('wuYe:commentIndex:list')")
    @GetMapping("/list")
    public TableDataInfo list(CommentIndex commentIndex) {
        startPage();
        List<CommentIndex> list = commentIndexService.selectCommentIndexList(commentIndex);
        return getDataTable(list);
    }


    /**
     * 查询社区资讯评论-----审核列表
     */
    @PreAuthorize("@ss.hasPermi('wuYe:commentIndex:commentSh')")
    @GetMapping("/commentSh")
    public TableDataInfo commentSh(CommentIndexShVo commentIndexShVo) {
        startPage();
        List<CommentIndexShVo> list = commentIndexService.commentSh(commentIndexShVo);
        return getDataTable(list);
    }

    /**
     * 导出社区资讯评论列表
     */
    @PreAuthorize("@ss.hasPermi('wuYe:commentIndex:export')")
    @Log(title = "社区资讯评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CommentIndex commentIndex) {
        List<CommentIndex> list = commentIndexService.selectCommentIndexList(commentIndex);
        ExcelUtil<CommentIndex> util = new ExcelUtil<CommentIndex>(CommentIndex.class);
        util.exportExcel(response, list, "社区资讯评论数据");
    }


    /**
     * 根据查询所有一级评论组带子集总数组装成前端所需要的数据结构 带分页
     */
    @Anonymous
    @PostMapping(value = "/getRootComment", name = "查询一级评论")
    public TableDataInfo getRootComment(@RequestBody CommentIndex commentIndex) {
        startPage();
        List<CommentIndexVo> rootComment = commentIndexService.getRootComment(commentIndex);
        return getDataTable(rootComment);
    }

    /**
     * 根据parent_id查询所有评论组装成前端所需要的子集数据结构 带分页
     */
    @Anonymous
    @PostMapping(value = "/getParentComment", name = "查询子级评论")
    public AjaxResult getParentComment(@RequestBody CommentIndex commentIndex) {
        startPage();
        return commentIndexService.getParentComment(commentIndex);
    }


    /**
     * 获取社区资讯评论详细信息
     */
    @PreAuthorize("@ss.hasPermi('wuYe:commentIndex:query')")
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId) {
        return success(commentIndexService.selectCommentIndexByCommentId(commentId));
    }

    /**
     * 新增社区资讯评论
     */
    @PreAuthorize("@ss.hasPermi('wuYe:commentIndex:add')")
    @Log(title = "社区资讯评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CommentIndex commentIndex) {
        return toAjax(commentIndexService.insertCommentIndex(commentIndex));
    }

    /**
     * 修改社区资讯评论
     */
    @PreAuthorize("@ss.hasPermi('wuYe:commentIndex:edit')")
    @Log(title = "社区资讯评论", businessType = BusinessType.UPDATE)
    @PostMapping("/put")
    public AjaxResult edit(@RequestBody CommentIndex commentIndex) {
        return toAjax(commentIndexService.updateCommentIndex(commentIndex));
    }

    /**
     * 删除社区资讯评论
     */
    @PreAuthorize("@ss.hasPermi('wuYe:commentIndex:remove')")
    @Log(title = "社区资讯评论", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{commentId}")
    public AjaxResult remove(@PathVariable Long commentId) {
        return toAjax(commentIndexService.deleteCommentIndexByCommentId(commentId));
    }
}
