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

import java.util.List;
import java.util.Map;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
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.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.category.support.SellerShopContext;
import com.ruoyi.web.modules.stock.dto.StockInboundCreateDTO;
import com.ruoyi.web.modules.stock.dto.StockInboundQuery;
import com.ruoyi.web.modules.stock.service.IStockInboundService;
import com.ruoyi.web.modules.stock.vo.StockGoodsPickVO;
import com.ruoyi.web.modules.stock.vo.StockOperatorVO;
import com.ruoyi.web.modules.store.constant.ShopConstants;

/**
 * 商家端商品入库
 */
@RestController
@RequestMapping("/agri/seller/stock/inbound")
public class SellerStockInboundController extends BaseController
{
    @Autowired
    private IStockInboundService stockInboundService;

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

    @PreAuthorize("@ss.hasPermi('agri:seller:stock:inbound:query')")
    @GetMapping("/{inboundId}")
    public AjaxResult detail(@PathVariable("inboundId") Long inboundId)
    {
        return success(stockInboundService.getDetail(requireShopId(), inboundId));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:stock:inbound:add')")
    @Log(title = "商品入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult create(@RequestBody StockInboundCreateDTO dto)
    {
        Map<String, Object> data = stockInboundService.createManual(requireShopId(), dto,
                getUserId(), getUsername());
        return success(data);
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:stock:inbound:list')")
    @GetMapping("/operators")
    public AjaxResult operators()
    {
        List<StockOperatorVO> rows = stockInboundService.listOperators(requireShopId());
        return success(rows);
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:stock:inbound:add')")
    @GetMapping("/goods/options")
    public TableDataInfo goodsOptions(@RequestParam(value = "keyword", required = false) String keyword)
    {
        startPage();
        return getDataTable(stockInboundService.listGoodsOptions(requireShopId(), keyword));
    }

    @PreAuthorize("@ss.hasPermi('agri:seller:stock:inbound:add')")
    @GetMapping("/goods/byBarcode")
    public AjaxResult goodsByBarcode(@RequestParam("barcode") String barcode)
    {
        List<StockGoodsPickVO> rows = stockInboundService.listGoodsByBarcode(requireShopId(), barcode);
        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;
    }
}
