package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
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.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysPictureBo;
import org.dromara.system.domain.vo.SysPictureVo;
import org.dromara.system.service.ISysPictureService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 画册
 *
 * @author Maosw
 * @date 2025-02-26
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/picture")
public class SysPictureController extends BaseController {

    private final ISysPictureService sysPictureService;

    /**
     * 查询画册列表
     */
    @SaCheckPermission("system:picture:list")
    @GetMapping("/list")
    public TableDataInfo<SysPictureVo> list(SysPictureBo bo, PageQuery pageQuery) {
        return sysPictureService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出画册列表
     */
    @SaCheckPermission("system:picture:export")
    @Log(title = "画册", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysPictureBo bo, HttpServletResponse response) {
        List<SysPictureVo> list = sysPictureService.queryList(bo);
        ExcelUtil.exportExcel(list, "画册", SysPictureVo.class, response);
    }

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

    /**
     * 新增画册
     */
    @SaCheckPermission("system:picture:add")
    @Log(title = "画册", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysPictureBo bo) {
        return toAjax(sysPictureService.insertByBo(bo));
    }

    /**
     * 修改画册
     */
    @SaCheckPermission("system:picture:edit")
    @Log(title = "画册", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysPictureBo bo) {
        return toAjax(sysPictureService.updateByBo(bo));
    }

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