package com.gxwebsoft.cms.controller;

import com.gxwebsoft.cms.entity.CmsBanner;
import com.gxwebsoft.cms.param.CmsBannerParam;
import com.gxwebsoft.cms.service.CmsBannerService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * 轮播图控制器
 *
 * @author WorkBuddy
 * @since 2026-07-17
 */
@Tag(name = "轮播图管理")
@RestController
@RequestMapping("/api/cms/cms-banner")
public class CmsBannerController extends BaseController {
    @Resource
    private CmsBannerService cmsBannerService;

    @Operation(summary = "分页查询轮播图")
    @GetMapping("/page")
    public ApiResult<PageResult<CmsBanner>> page(CmsBannerParam param) {
        return success(cmsBannerService.pageRel(param));
    }

    @Operation(summary = "查询全部轮播图")
    @GetMapping()
    public ApiResult<List<CmsBanner>> list(CmsBannerParam param) {
        return success(cmsBannerService.listRel(param));
    }

    @Operation(summary = "根据id查询轮播图")
    @GetMapping("/{id}")
    public ApiResult<CmsBanner> get(@PathVariable("id") Integer id) {
        return success(cmsBannerService.getByIdRel(id));
    }

    @Operation(summary = "添加轮播图")
    @PreAuthorize("hasAuthority('cms:cmsBanner:save')")
    @PostMapping()
    public ApiResult<?> save(@RequestBody CmsBanner cmsBanner) {
        if (cmsBannerService.save(cmsBanner)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改轮播图")
    @PreAuthorize("hasAuthority('cms:cmsBanner:update')")
    @PutMapping()
    public ApiResult<?> update(@RequestBody CmsBanner cmsBanner) {
        if (cmsBannerService.updateById(cmsBanner)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "删除轮播图")
    @PreAuthorize("hasAuthority('cms:cmsBanner:remove')")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (cmsBannerService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "修改轮播图状态(启用/停用)")
    @PreAuthorize("hasAuthority('cms:cmsBanner:update')")
    @PutMapping("/status")
    public ApiResult<?> updateStatus(@RequestBody CmsBanner cmsBanner) {
        CmsBanner entity = new CmsBanner();
        entity.setBannerId(cmsBanner.getBannerId());
        entity.setStatus(cmsBanner.getStatus());
        if (cmsBannerService.updateById(entity)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "统计概览")
    @GetMapping("/stats")
    public ApiResult<Map<String, Object>> stats() {
        Map<String, Object> map = new HashMap<>();
        map.put("total", cmsBannerService.count());
        map.put("enabled", cmsBannerService.count(new LambdaQueryWrapper<CmsBanner>().eq(CmsBanner::getStatus, 1)));
        map.put("disabled", cmsBannerService.count(new LambdaQueryWrapper<CmsBanner>().eq(CmsBanner::getStatus, 0)));
        return success(map);
    }
}
