package org.dromara.system.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.system.domain.vo.TblArticleVo;
import org.dromara.system.domain.bo.TblArticleBo;
import org.dromara.system.service.ITblArticleService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 文章
 *
 * @author Lion Li
 * @date 2024-07-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/Article")
public class TblArticleController extends BaseController {

    private final ITblArticleService tblArticleService;

    /**
     * 查询文章列表
     */
//    //@SaCheckPermission("system:Article:list")
    @GetMapping("/list")
    public TableDataInfo<TblArticleVo> list(TblArticleBo bo, PageQuery pageQuery) {
        return tblArticleService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文章列表
     */
//    //@SaCheckPermission("system:Article:export")
    @Log(title = "文章", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TblArticleBo bo, HttpServletResponse response) {
        List<TblArticleVo> list = tblArticleService.queryList(bo);
        ExcelUtil.exportExcel(list, "文章", TblArticleVo.class, response);
    }

    /**
     * 获取文章详细信息
     *
     * @param id 主键
     */
//    //@SaCheckPermission("system:Article:query")
    @GetMapping("/{id}")
    public R<TblArticleVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tblArticleService.queryById(id));
    }

    /**
     * 新增文章
     */
//    //@SaCheckPermission("system:Article:add")
    @Log(title = "文章", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void>Add(@Validated(AddGroup.class) @RequestBody TblArticleBo bo) {
        return toAjax(tblArticleService.insertByBo(bo));
    }

    /**
     * 修改文章
     */
//    //@SaCheckPermission("system:Article:edit")
    @Log(title = "文章", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TblArticleBo bo) {
        return toAjax(tblArticleService.updateByBo(bo));
    }

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