package com.czg.controller.admin;

import com.czg.product.dto.storage.CountStorageDTO;
import com.czg.product.dto.storage.ShopStorageAddDTO;
import com.czg.product.dto.storage.ShopStorageEditDTO;
import com.czg.product.entity.ShopStorage;
import com.czg.product.entity.ShopStorageRecord;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.product.service.ShopStorageService;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 存酒记录管理
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/shopStorage")
public class ShopStorageController {
    @Resource
    private ShopStorageService shopStorageService;

    /**
     * 存酒记录
     * @param key 酒名/用户昵称
     * @param phone 手机号
     * @param status 0：已取完；1：未取完；2：已过期
     * @return 分页数据
     */
    @SaAdminCheckPermission(parentName = "存酒相关", value = "shopStorage:list", name = "获取存酒记录列表")
    @GetMapping
    public CzgResult<Page<ShopStorage>> list(String key, String phone, Integer status) {
        return CzgResult.success(shopStorageService.pageInfo(StpKit.USER.getShopId(), key, phone, status));
    }

    /**
     * 存酒记录添加
     * @return 分页数据
     */
    @SaAdminCheckPermission(parentName = "存酒相关", value = "shopStorage:add", name = "添加存酒")
    @PostMapping
    public CzgResult<Boolean> add(@RequestBody @Validated ShopStorageAddDTO shopStorageAddDTO) {
        return CzgResult.success(shopStorageService.add(StpKit.USER.getShopId(), shopStorageAddDTO));
    }

    /**
     * 存酒取酒
     * @return 分页数据
     */
    @SaAdminCheckPermission(parentName = "存酒相关", value = "shopStorage:edit", name = "修改存酒数量")
    @PutMapping
    public CzgResult<Boolean> edit(@RequestBody @Validated ShopStorageEditDTO shopStorageEditDTO) {
        if (shopStorageEditDTO.getNum() == 0) {
            return CzgResult.failure("数量不为0");
        }
        return CzgResult.success(shopStorageService.edit(StpKit.USER.getShopId(), shopStorageEditDTO));
    }

    /**
     * 查询存取酒记录
     * @param id 存酒id
     * @return 分页数据
     */
    @SaAdminCheckPermission(parentName = "存酒相关", value = "shopStorage:record", name = "查询存取酒记录")
    @GetMapping("/record")
    public CzgResult<List<ShopStorageRecord>> recordList(@RequestParam Integer id) {
        return CzgResult.success(shopStorageService.getRecord(StpKit.USER.getShopId(), id));
    }

    /**
     * 存酒统计
     * @return 统计信息
     */
    @SaAdminCheckPermission(parentName = "存酒相关", value = "shopStorage:count", name = "存酒统计")
    @GetMapping("/count")
    public CzgResult<List<CountStorageDTO>> countRecord() {
        return CzgResult.success(shopStorageService.countRecord(StpKit.USER.getShopId()));
    }
}
