package org.dromara.work.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
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.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.work.domain.vo.TpCommentsVo;
import org.dromara.work.domain.bo.TpCommentsBo;
import org.dromara.work.service.ITpCommentsService;

/**
 * 评论
 *
 * @author Maosw
 * @date 2025-04-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/comments")
public class TpCommentsController extends BaseController {

    private final ITpCommentsService tpCommentsService;

    /**
     * 查询评论列表
     */
    @SaCheckPermission("work:comments:list")
    @GetMapping("/list")
    public R<List<TpCommentsVo>> list(TpCommentsBo bo) {
        List<TpCommentsVo> list = tpCommentsService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出评论列表
     */
    @SaCheckPermission("work:comments:export")
    @Log(title = "评论", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpCommentsBo bo, HttpServletResponse response) {
        List<TpCommentsVo> list = tpCommentsService.queryList(bo);
        ExcelUtil.exportExcel(list, "评论", TpCommentsVo.class, response);
    }

    /**
     * 获取评论详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:comments:query")
    @GetMapping("/{id}")
    public R<TpCommentsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpCommentsService.queryById(id));
    }

    /**
     * 新增评论
     */
    @SaCheckPermission("work:comments:add")
    @Log(title = "评论", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpCommentsBo bo) {
        return toAjax(tpCommentsService.insertByBo(bo));
    }

    /**
     * 修改评论
     */
    @SaCheckPermission("work:comments:edit")
    @Log(title = "评论", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpCommentsBo bo) {
        return toAjax(tpCommentsService.updateByBo(bo));
    }

    /**
     * 删除评论
     *
     * @param ids 主键串
     */
    @SaCheckPermission("work:comments:remove")
    @Log(title = "评论", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tpCommentsService.deleteWithValidByIds(List.of(ids), true));
    }
}
