package com.ruoyi.web.modules.stock.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.category.support.SellerShopContext;
import com.ruoyi.web.modules.stock.dto.StockQueryQuery;
import com.ruoyi.web.modules.stock.service.IStockQueryService;
import com.ruoyi.web.modules.stock.vo.StockQueryLogRowVO;
import com.ruoyi.web.modules.store.constant.ShopConstants;

/**
 * 商家端库存查询（只读）
 */
@RestController
@RequestMapping("/agri/seller/stock/query")
public class SellerStockQueryController extends BaseController
{
    @Autowired
    private IStockQueryService stockQueryService;

    @PreAuthorize("@ss.hasPermi('agri:seller:stock:query:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockQueryQuery query)
    {
        startPage();
        return getDataTable(stockQueryService.selectList(requireShopId(), query));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:stock:query:list')")
    @GetMapping("/{goodsId}/logs")
    public AjaxResult logs(@PathVariable("goodsId") Long goodsId)
    {
        List<StockQueryLogRowVO> rows = stockQueryService.listLogs(requireShopId(), goodsId);
        return success(rows);
    }

    private Long requireShopId()
    {
        Long shopId = SellerShopContext.getShopId();
        if (shopId == null || shopId <= 0)
        {
            throw new ServiceException(ShopConstants.MSG_SHOP_CONTEXT_REQUIRED);
        }
        return shopId;
    }
}
