package com.yingpaipay.setting.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 com.yingpaipay.setting.domain.vo.CarouselSettingVo;
import com.yingpaipay.setting.domain.bo.CarouselSettingBo;
import com.yingpaipay.setting.service.ICarouselSettingService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 轮播图设置
 *
 * @author Huanyi
 * @date 2025-12-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/setting/carousel")
public class CarouselSettingController extends BaseController {

    private final ICarouselSettingService carouselSettingService;

    /**
     * 查询轮播图设置列表
     */
    @SaCheckPermission("setting:carousel:list")
    @GetMapping("/list")
    public TableDataInfo<CarouselSettingVo> list(CarouselSettingBo bo, PageQuery pageQuery) {
        return carouselSettingService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出轮播图设置列表
     */
    @SaCheckPermission("setting:carousel:export")
    @Log(title = "轮播图设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CarouselSettingBo bo, HttpServletResponse response) {
        List<CarouselSettingVo> list = carouselSettingService.queryList(bo);
        ExcelUtil.exportExcel(list, "轮播图设置", CarouselSettingVo.class, response);
    }

    /**
     * 获取轮播图设置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("setting:carousel:query")
    @GetMapping("/{id}")
    public R<CarouselSettingVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(carouselSettingService.queryById(id));
    }

    /**
     * 新增轮播图设置
     */
    @SaCheckPermission("setting:carousel:add")
    @Log(title = "轮播图设置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CarouselSettingBo bo) {
        return toAjax(carouselSettingService.insertByBo(bo));
    }

    /**
     * 修改轮播图设置
     */
    @SaCheckPermission("setting:carousel:edit")
    @Log(title = "轮播图设置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CarouselSettingBo bo) {
        return toAjax(carouselSettingService.updateByBo(bo));
    }

    /**
     * 删除轮播图设置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("setting:carousel:remove")
    @Log(title = "轮播图设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(carouselSettingService.deleteWithValidByIds(List.of(ids), true));
    }

    @GetMapping("/listOnApplet")
    public R<List<CarouselSettingVo>> listOnApplet() {
        return R.ok(carouselSettingService.queryList(new CarouselSettingBo()));
    }

}
