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.StockOutboundCreateDTO;
import com.ruoyi.web.modules.stock.dto.StockOutboundQuery;
import com.ruoyi.web.modules.stock.service.IStockOutboundService;
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/outbound")
public class SellerStockOutboundController extends BaseController
{
    @Autowired
    private IStockOutboundService stockOutboundService;

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

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

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

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

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

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