package com.ruoyi.export.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.export.domain.vo.ExportArticleCategoryVo;
import com.ruoyi.export.domain.bo.ExportArticleCategoryBo;
import com.ruoyi.export.service.IExportArticleCategoryService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章分类
 *
 * @author JR
 * @date 2025-07-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/export/articleCategory")
public class ExportArticleCategoryController extends BaseController {

    private final IExportArticleCategoryService iExportArticleCategoryService;

    /**
     * 查询文章分类列表
     */
    @GetMapping("/list")
    public TableDataInfo<ExportArticleCategoryVo> list(ExportArticleCategoryBo bo, PageQuery pageQuery) {
        return iExportArticleCategoryService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出文章分类列表
     */
    @Log(title = "文章分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ExportArticleCategoryBo bo, HttpServletResponse response) {
        List<ExportArticleCategoryVo> list = iExportArticleCategoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "文章分类", ExportArticleCategoryVo.class, response);
    }

    /**
     * 获取文章分类详细信息
     *
     * @param categoryId 主键
     */
    @GetMapping("/{categoryId}")
    public R<ExportArticleCategoryVo> getInfo(@NotNull(message = "主键不能为空")
                                              @PathVariable Long categoryId) {
        return R.ok(iExportArticleCategoryService.queryById(categoryId));
    }

    /**
     * 新增文章分类
     */
    @Log(title = "文章分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ExportArticleCategoryBo bo) {
        return toAjax(iExportArticleCategoryService.insertByBo(bo));
    }

    /**
     * 修改文章分类
     */
    @Log(title = "文章分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ExportArticleCategoryBo bo) {
        return toAjax(iExportArticleCategoryService.updateByBo(bo));
    }

    /**
     * 删除文章分类
     *
     * @param categoryIds 主键串
     */
    @Log(title = "文章分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{categoryIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] categoryIds) {
        return toAjax(iExportArticleCategoryService.deleteWithValidByIds(Arrays.asList(categoryIds), true));
    }
}
