package com.xindazhou.business.controller.admin.community.banner;

import com.xindazhou.business.controller.admin.community.banner.vo.BannerPageReqVO;
import com.xindazhou.business.controller.admin.community.banner.vo.BannerRespVO;
import com.xindazhou.business.controller.admin.community.banner.vo.BannerSaveReqVO;
import com.xindazhou.business.service.community.banner.BannerService;
import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - Banner管理 Controller
 *
 * @author xindazhou
 */
@Tag(name = "管理后台 - Banner管理")
@RestController("communityBannerController")
@RequestMapping("/admin/community/banner")
@Validated
@Slf4j
public class BannerController {

    @Resource
    private BannerService bannerService;

    @GetMapping("/page")
    @Operation(summary = "获得Banner分页列表")
    @PreAuthorize("@ss.hasPermission('community:banner:query')")
    public CommonResult<PageResult<BannerRespVO>> getBannerPage(@Valid BannerPageReqVO pageReqVO) {
        PageResult<BannerRespVO> pageResult = bannerService.getBannerPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/{bannerId}")
    @Operation(summary = "获得Banner")
    @Parameter(name = "bannerId", description = "Banner ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('community:banner:query')")
    public CommonResult<BannerRespVO> getBanner(@PathVariable("bannerId") Long bannerId) {
        BannerRespVO banner = bannerService.getBanner(bannerId);
        return success(banner);
    }

    @PostMapping
    @Operation(summary = "创建Banner")
    @PreAuthorize("@ss.hasPermission('community:banner:create')")
    public CommonResult<Long> createBanner(@Valid @RequestBody BannerSaveReqVO createReqVO) {
        Long id = bannerService.createBanner(createReqVO);
        return success(id);
    }

    @PutMapping("/{bannerId}")
    @Operation(summary = "更新Banner")
    @Parameter(name = "bannerId", description = "Banner ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('community:banner:update')")
    public CommonResult<Boolean> updateBanner(@PathVariable("bannerId") Long bannerId,
                                             @Valid @RequestBody BannerSaveReqVO updateReqVO) {
        updateReqVO.setId(bannerId);
        bannerService.updateBanner(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/{bannerId}")
    @Operation(summary = "删除Banner")
    @Parameter(name = "bannerId", description = "Banner ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('community:banner:delete')")
    public CommonResult<Boolean> deleteBanner(@PathVariable("bannerId") Long bannerId) {
        bannerService.deleteBanner(bannerId);
        return success(true);
    }

    @PutMapping("/{bannerId}/status")
    @Operation(summary = "更新Banner状态")
    @Parameter(name = "bannerId", description = "Banner ID", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('community:banner:update')")
    public CommonResult<Boolean> updateBannerStatus(@PathVariable("bannerId") Long bannerId,
                                                    @RequestParam("status") Integer status) {
        bannerService.updateBannerStatus(bannerId, status);
        return success(true);
    }

}

