package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.validation.constraints.NotEmpty;
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.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.system.domain.bo.SysAppletIndexSlideshowBo;
import org.dromara.system.domain.vo.SysAppletIndexSlideshowVo;
import org.dromara.system.service.ISysAppletIndexSlideshowService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 小程序首页轮播图管理
 *
 * @Author: Trae
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/applet/slideshow")
public class SysAppletIndexSlideshowController extends BaseController {

    private final ISysAppletIndexSlideshowService slideshowService;

    /**
     * 查询全部轮播图
     */
    @SaIgnore
    @GetMapping("/list")
    public R<List<SysAppletIndexSlideshowVo>> list() {
        return R.ok(slideshowService.queryAll());
    }

    /**
     * 根据ID查询轮播图
     */
    @SaIgnore
    @GetMapping("/{id}")
    public R<SysAppletIndexSlideshowVo> getById(@PathVariable Long id) {
        return R.ok(slideshowService.queryById(id));
    }

    /**
     * 新增轮播图
     */
    @Log(title = "轮播图管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysAppletIndexSlideshowBo bo) {
        slideshowService.insertByBo(bo);
        return R.ok();
    }

    /**
     * 修改轮播图
     */
    @Log(title = "轮播图管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysAppletIndexSlideshowBo bo) {
        slideshowService.updateByBo(bo);
        return R.ok();
    }

    /**
     * 删除轮播图
     */
    @Log(title = "轮播图管理", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        slideshowService.deleteByIds(List.of(ids));
        return R.ok();
    }

}
