package org.dromara.controller.communityNews;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.tenant.helper.TenantHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.domain.communityNews.CommentIndex;
import org.dromara.domain.communityNews.bo.CommentIndexBo;
import org.dromara.domain.communityNews.vo.CommentChildrenVo;
import org.dromara.domain.communityNews.vo.CommentIndexShVo;
import org.dromara.domain.communityNews.vo.CommentIndexVo;
import org.dromara.domain.communityNews.vo.CommentIndexVoOld;
import org.dromara.service.ICommentIndexService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 社区资讯评论
 *
 * @author boman
 * @date 2025-04-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wuYe/commentIndex")
public class CommentIndexController extends BaseController {

    private final ICommentIndexService commentIndexService;

    /**
     * 查询社区资讯评论列表
     */
    @SaCheckPermission("wuYe:commentIndex:list")
    @GetMapping("/list")
    public TableDataInfo<CommentIndexVo> list(CommentIndexBo bo, PageQuery pageQuery) {
        return commentIndexService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询社区资讯评论-----审核列表
     */
    @SaCheckPermission("wuYe:commentIndex:commentSh")
    @GetMapping("/commentSh")
    public TableDataInfo<CommentIndexShVo> commentSh(CommentIndexShVo commentIndexShVo, PageQuery pageQuery) {
        //commentIndexShVo.setTenantId(LoginHelper.getTenantId());
        return commentIndexService.commentSh(commentIndexShVo, pageQuery);

    }

    /**
     * 根据查询所有一级评论组带子集总数组装成前端所需要的数据结构 带分页
     */
    @SaIgnore
    @PostMapping(value = "/getRootComment", name = "查询一级评论")
    public TableDataInfo<CommentIndexVoOld> getRootComment(@RequestBody CommentIndex commentIndex, PageQuery pageQuery) {
        commentIndex.setTenantId(LoginHelper.getTenantId());
        return TenantHelper.ignore(() -> {
            return commentIndexService.getRootComment(commentIndex, pageQuery);
        });
    }

    /**
     * 根据parent_id查询所有评论组装成前端所需要的子集数据结构 带分页
     */
    @SaIgnore
    @PostMapping(value = "/getParentComment", name = "查询子级评论")
    public TableDataInfo<CommentChildrenVo> getParentComment(@RequestBody CommentIndex commentIndex, PageQuery pageQuery) {
        commentIndex.setTenantId(LoginHelper.getTenantId());
        return TenantHelper.ignore(() -> {
            return commentIndexService.getParentComment(commentIndex, pageQuery);
        });
    }

    /**
     * 导出社区资讯评论列表
     */
    @SaCheckPermission("wuYe:commentIndex:export")
    @Log(title = "社区资讯评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CommentIndexBo bo, HttpServletResponse response) {
        List<CommentIndexVo> list = commentIndexService.queryList(bo);
        ExcelUtil.exportExcel(list, "社区资讯评论", CommentIndexVo.class, response);
    }

    /**
     * 获取社区资讯评论详细信息
     *
     * @param commentId 主键
     */
    @SaCheckPermission("wuYe:commentIndex:query")
    @GetMapping("/{commentId}")
    public R<CommentIndexVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long commentId) {
        return R.ok(commentIndexService.queryById(commentId));
    }

    /**
     * 新增社区资讯评论
     */
    @SaCheckPermission("wuYe:commentIndex:add")
    @Log(title = "社区资讯评论", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CommentIndexBo bo) {
        return toAjax(commentIndexService.insertByBo(bo));
    }

    /**
     * 修改社区资讯评论
     */
    @SaCheckPermission("wuYe:commentIndex:edit")
    @Log(title = "社区资讯评论", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/put")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CommentIndexBo bo) {
        return toAjax(commentIndexService.updateByBo(bo));
    }

    /*    *//**
     * 删除社区资讯评论
     *
     * @param commentIds 主键串
     *//*
    @SaCheckPermission("wuYe:commentIndex:remove")
    @Log(title = "社区资讯评论", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{commentIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] commentIds) {
        return toAjax(commentIndexService.deleteWithValidByIds(List.of(commentIds), true));
    }*/

    /**
     * 逻辑删除社区资讯评论
     */
    @SaCheckPermission("wuYe:commentIndex:remove")
    @Log(title = "社区资讯评论", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{commentId}")
    public R<Void> remove(@PathVariable Long commentId) {
        return toAjax(commentIndexService.deleteCommentIndexByCommentId(commentId));
    }
}
