package org.dromara.progress.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
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.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.web.core.BaseController;
import org.dromara.progress.domain.dto.progresscategory.PgsProgressCategoryCreateReq;
import org.dromara.progress.domain.dto.progresscategory.PgsProgressCategoryQueryReq;
import org.dromara.progress.domain.dto.progresscategory.PgsProgressCategoryUpdateReq;
import org.dromara.progress.domain.vo.progresscategory.PgsProgressCategoryVo;
import org.dromara.progress.service.IPgsProgressCategoryService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 进度类别
 *
 * @author lcj
 * @date 2025-05-26
 */
@Validated
@RestController
@RequestMapping("/progress/progressCategory")
public class PgsProgressCategoryController extends BaseController {

    @Resource
    private IPgsProgressCategoryService pgsProgressCategoryService;

    /**
     * 查询进度类别列表
     */
    @SaCheckPermission("progress:progressCategory:list")
    @GetMapping("/list")
    public R<List<PgsProgressCategoryVo>> list(PgsProgressCategoryQueryReq req) {
        List<PgsProgressCategoryVo> list = pgsProgressCategoryService.queryList(req);
        return R.ok(list);
    }

    /**
     * 导出进度类别列表
     */
    @SaCheckPermission("progress:progressCategory:export")
    @Log(title = "进度类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PgsProgressCategoryQueryReq req, HttpServletResponse response) {
        List<PgsProgressCategoryVo> list = pgsProgressCategoryService.queryList(req);
        ExcelUtil.exportExcel(list, "进度类别", PgsProgressCategoryVo.class, response);
    }

    /**
     * 获取进度类别详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("progress:progressCategory:query")
    @GetMapping("/{id}")
    public R<PgsProgressCategoryVo> getInfo(@NotNull(message = "主键不能为空")
                                            @PathVariable Long id) {
        return R.ok(pgsProgressCategoryService.queryById(id));
    }

    /**
     * 新增进度类别
     */
    @SaCheckPermission("progress:progressCategory:add")
    @Log(title = "进度类别", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody PgsProgressCategoryCreateReq req) {
        return R.ok(pgsProgressCategoryService.insertByBo(req));
    }

    /**
     * 修改进度类别
     */
    @SaCheckPermission("progress:progressCategory:edit")
    @Log(title = "进度类别", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PgsProgressCategoryUpdateReq req) {
        return toAjax(pgsProgressCategoryService.updateByBo(req));
    }

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