package com.czg.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.czg.product.dto.storage.ShopStorageGoodDTO;
import com.czg.product.entity.ShopStorage;
import com.czg.product.entity.ShopStorageGood;
import com.czg.product.service.ShopStorageGoodService;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.PageUtil;
import com.czg.validator.group.InsertGroup;
import com.czg.validator.group.UpdateGroup;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 存酒商品管理
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/storageGood")
public class ShopStorageGoodController {
    @Resource
    private ShopStorageGoodService shopStorageGoodService;

    /**
     * 获取存酒商品列表
     * @param name 商品名
     * @return 列表
     */
    @SaAdminCheckPermission(parentName = "存酒相关", value = "storageGood:list", name = "获取存酒商品列表")
    @GetMapping
    public CzgResult<Page<ShopStorageGood>> list(String name) {
        QueryWrapper queryWrapper = new QueryWrapper().eq(ShopStorageGood::getShopId, StpKit.USER.getShopId());
        if (StrUtil.isNotBlank(name)) {
            queryWrapper.eq(ShopStorageGood::getName, name);
        }
        queryWrapper.orderBy(ShopStorageGood::getId, false);
        return CzgResult.success(shopStorageGoodService.page(PageUtil.buildPage(), queryWrapper));
    }

    /**
     * 获取存酒商品详情
     * @param id 商品id
     * @return 列表
     */
    @SaAdminCheckPermission(parentName = "存酒相关", value = "storageGood:detail", name = "获取存酒商品详情")
    @GetMapping("/detail")
    public CzgResult<ShopStorageGood> detail(@RequestParam Integer id) {
        return CzgResult.success(shopStorageGoodService.getOne(new QueryWrapper().eq(ShopStorageGood::getShopId, StpKit.USER.getShopId()).eq(ShopStorageGood::getId, id)));
    }

    /**
     * 编辑存酒商品
     * @return 列表
     */
    @SaAdminCheckPermission(parentName = "存酒相关", value = "storageGood:edit", name = "存酒商品编辑")
    @PutMapping("/edit")
    public CzgResult<Boolean> edit(@RequestBody @Validated({UpdateGroup.class}) ShopStorageGoodDTO shopStorageGoodDTO) {
        return CzgResult.success(shopStorageGoodService.edit(StpKit.USER.getShopId(), shopStorageGoodDTO));
    }

    /**
     * 添加存酒商品
     * @return 列表
     */
    @SaAdminCheckPermission(parentName = "存酒相关", value = "storageGood:add", name = "存酒商品新增")
    @PostMapping
    public CzgResult<Boolean> add(@RequestBody @Validated({InsertGroup.class}) ShopStorageGoodDTO shopStorageGoodDTO) {
        return CzgResult.success(shopStorageGoodService.saveInfo(StpKit.USER.getShopId(), shopStorageGoodDTO));
    }

    /**
     * 删除存酒商品
     * @return 列表
     */
    @SaAdminCheckPermission(parentName = "存酒相关", value = "storageGood:del", name = "存酒商品删除")
    @DeleteMapping
    public CzgResult<Boolean> del(@RequestParam Integer id) {
        return CzgResult.success(shopStorageGoodService.remove(new QueryWrapper().eq(ShopStorageGood::getShopId, StpKit.USER.getShopId()).eq(ShopStorageGood::getId, id)));
    }

}
