package org.dromara.work.controller;

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.work.domain.bo.TpPictureBo;
import org.dromara.work.domain.vo.TpPictureVo;
import org.dromara.work.service.ITpPictureService;
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("/work/picture")
public class TpPictureController extends BaseController {

    private final ITpPictureService TpPictureService;

    /**
     * 查询 轮播/作品/动态列表
     */
    @SaCheckPermission("work:picture:list")
    @GetMapping("/list")
    public TableDataInfo<TpPictureVo> list(TpPictureBo bo, PageQuery pageQuery) {
        return TpPictureService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出 轮播/作品/动态列表
     */
    @SaCheckPermission("work:picture:export")
    @Log(title = "轮播/作品/动态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpPictureBo bo, HttpServletResponse response) {
        List<TpPictureVo> list = TpPictureService.queryList(bo);
        ExcelUtil.exportExcel(list, "轮播/作品/动态", TpPictureVo.class, response);
    }

    /**
     * 获取 轮播/作品/动态详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:picture:query")
    @GetMapping("/{id}")
    public R<TpPictureVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(TpPictureService.queryById(id));
    }

    /**
     * 新增 轮播/作品/动态
     */
    @SaCheckPermission("work:picture:add")
    @Log(title = "轮播/作品/动态", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody TpPictureBo bo) {
        return R.ok(TpPictureService.insertByBo(bo));
    }

    /**
     * 修改 轮播/作品/动态
     */
    @SaCheckPermission("work:picture:edit")
    @Log(title = "轮播/作品/动态", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpPictureBo bo) {
        return toAjax(TpPictureService.updateByBo(bo));
    }

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