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.ExportCategoryVo;
import com.ruoyi.export.domain.bo.ExportCategoryBo;
import com.ruoyi.export.service.IExportCategoryService;

/**
 * 分类
 *
 * @author ruoyi
 * @date 2025-07-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/export/category")
public class ExportCategoryController extends BaseController {

    private final IExportCategoryService iExportCategoryService;

    /**
     * 查询分类列表
     */
    @GetMapping("/list")
    public R<List<ExportCategoryVo>> list(ExportCategoryBo bo) {
        List<ExportCategoryVo> list = iExportCategoryService.queryList(bo);
        return R.ok(list);
    }

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

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

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

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

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