package com.backendsys.modules.cms.banner.controller;

import com.backendsys.modules.cms.banner.entity.Banner;
import com.backendsys.modules.cms.banner.service.BannerService;
import com.backendsys.modules.common.aspect.SysLog;
import com.backendsys.modules.common.config.security.annotations.Anonymous;
import com.backendsys.modules.common.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
@Tag(name = "幻灯片管理")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @Operation(summary = "获取幻灯片列表")
    @PreAuthorize("@sr.hasPermission('10.1')")
    @GetMapping("/api/cms/banner/getBannerList")
    public Result getBanner(@Validated Banner banner) {
        return Result.success().put("data", bannerService.selectBannerList(banner));
    }

    @Anonymous
    @Operation(summary = "获取幻灯片列表 (公开)")
    @GetMapping("/api/public/cms/banner/getBannerList")
    public Result getBannerPublic(@Validated Banner banner) {
        banner.setStatus(1);
        return Result.success().put("data", bannerService.selectBannerList(banner));
    }

    @Operation(summary = "获取幻灯片详情")
    @PreAuthorize("@sr.hasPermission('10.1.1')")
    @GetMapping("/api/cms/banner/getBannerDetail")
    public Result getBannerDetail(@Validated(Banner.Detail.class) Banner banner) {
        return Result.success().put("data", bannerService.selectBannerDetail(banner));
    }

    @SysLog("创建幻灯片")
    @Operation(summary = "创建幻灯片")
    @PreAuthorize("@sr.hasPermission('10.1.2')")
    @PostMapping("/api/cms/banner/createBanner")
    public Result createBanner(@Validated(Banner.Create.class) @RequestBody Banner banner) {
        return Result.success().put("data", bannerService.insertBanner(banner));
    }

    @SysLog("编辑资讯")
    @Operation(summary = "编辑资讯")
    @PreAuthorize("@sr.hasPermission('10.1.3')")
    @PutMapping("/api/cms/banner/updateBanner")
    public Result updateBanner(@Validated(Banner.Update.class) @RequestBody Banner banner) {
        return Result.success().put("data", bannerService.updateBanner(banner));
    }

    @SysLog("删除资讯")
    @Operation(summary = "删除资讯")
    @PreAuthorize("@sr.hasPermission('10.1.4')")
    @DeleteMapping("/api/cms/banner/deleteBanner")
    public Result deleteBanner(@Validated(Banner.Delete.class) @RequestBody Banner banner) {
        return Result.success().put("data", bannerService.deleteBanner(banner));
    }

    /*
    @GetMapping("/api/public/cms/banner/getBanner")
    public Result getPublicBanner(@Validated PageDTO pageDTO, @Validated CmsBannerDTO cmsBannerDTO) {
        cmsBannerDTO.setStatus(1); // 已启用
        return Result.success(cmsBannerService.queryBanner(pageDTO.getPage_num(), pageDTO.getPage_size(), cmsBannerDTO));
    }
     */

}
