package com.czg.controller.admin;

import com.czg.annotation.SaAdminCheckPermission;
import com.czg.market.dto.MkCarouselDTO;
import com.czg.market.entity.MkCarousel;
import com.czg.market.service.MkCarouselService;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 轮播图配置
 *
 * @author ww
 */
@RestController
@RequestMapping("/admin/carousel")
public class ACarouselController {

    @Resource
    private MkCarouselService mkCarouselService;

    /**
     * 轮播图配置
     */
    @GetMapping
    @SaAdminCheckPermission(parentName = "轮播图配置", value = "carousel:config", name = "轮播图-列表")
    public CzgResult<List<MkCarousel>> getCarousels(MkCarouselDTO carouselDTO) {
        carouselDTO.setShopId(StpKit.USER.getShopId());
        return CzgResult.success(mkCarouselService.getCarousels(carouselDTO));
    }

    /**
     * 轮播图配置:新增/修改
     */
    @PostMapping
    @SaAdminCheckPermission(parentName = "轮播图配置", value = "carousel:up", name = "轮播图-新增/修改")
    public CzgResult<Boolean> editCarousel(@RequestBody @Validated MkCarousel carousel) {
        carousel.setShopId(StpKit.USER.getShopId());
        if (carousel.getId() == null) {
            return CzgResult.success(mkCarouselService.save(carousel));
        } else {
            return CzgResult.success(mkCarouselService.updateById(carousel, false));
        }
    }

    /**
     * 轮播图配置:删除
     */
    @DeleteMapping("/{id}")
    @SaAdminCheckPermission(parentName = "轮播图配置", value = "carousel:up", name = "轮播图-新增/修改")
    public CzgResult<Boolean> deleteCarousel(@PathVariable("id") Long id) {
        return CzgResult.success(mkCarouselService.remove(QueryWrapper.create().eq(MkCarousel::getId, id).eq(MkCarousel::getShopId, StpKit.USER.getShopId())));
    }
}