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.TpCategoryVo;
import org.dromara.work.domain.bo.TpCategoryBo;
import org.dromara.work.service.ITpCategoryService;

/**
 * 作品类目
 *
 * @author Maosw
 * @date 2025-04-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/category")
public class TpCategoryController extends BaseController {

    private final ITpCategoryService tpCategoryService;

    /**
     * 查询作品类目列表
     */
    @SaCheckPermission("work:category:list")
    @GetMapping("/list")
    public R<List<TpCategoryVo>> list(TpCategoryBo bo) {
        List<TpCategoryVo> list = tpCategoryService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出作品类目列表
     */
    @SaCheckPermission("work:category:export")
    @Log(title = "作品类目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpCategoryBo bo, HttpServletResponse response) {
        List<TpCategoryVo> list = tpCategoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "作品类目", TpCategoryVo.class, response);
    }

    /**
     * 获取作品类目详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:category:query")
    @GetMapping("/{id}")
    public R<TpCategoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpCategoryService.queryById(id));
    }

    /**
     * 新增作品类目
     */
    @SaCheckPermission("work:category:add")
    @Log(title = "作品类目", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpCategoryBo bo) {
        return toAjax(tpCategoryService.insertByBo(bo));
    }

    /**
     * 修改作品类目
     */
    @SaCheckPermission("work:category:edit")
    @Log(title = "作品类目", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpCategoryBo bo) {
        return toAjax(tpCategoryService.updateByBo(bo));
    }

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