package com.ruoyi.web.controller.warehouse;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
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.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.utils.poi.ExcelUtil;
import com.ruoyi.warehouse.domain.OutRecord;
import com.ruoyi.warehouse.domain.StockInfo;
import com.ruoyi.warehouse.service.IStockInfoService;

@RestController
@RequestMapping("/warehouse/stock")
public class StockInfoController extends BaseController
{
    @Autowired
    private IStockInfoService stockInfoService;

    @PreAuthorize("@ss.hasPermi('warehouse:stock:list')")
    @GetMapping("/list")
    public TableDataInfo list(StockInfo stockInfo)
    {
        startPage();
        return getDataTable(stockInfoService.selectStockInfoList(stockInfo));
    }

    @PreAuthorize("@ss.hasPermi('warehouse:stock:export')")
    @Log(title = "库存信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StockInfo stockInfo)
    {
        List<StockInfo> list = stockInfoService.selectStockInfoList(stockInfo);
        ExcelUtil<StockInfo> util = new ExcelUtil<StockInfo>(StockInfo.class);
        util.exportExcel(response, list, "库存数据");
    }

    @PreAuthorize("@ss.hasPermi('warehouse:stock:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id)
    {
        return success(stockInfoService.selectStockInfoById(id));
    }

    @PreAuthorize("@ss.hasPermi('warehouse:stock:out')")
    @Log(title = "库存出库", businessType = BusinessType.INSERT)
    @PostMapping("/out")
    public AjaxResult outStock(@RequestBody OutRecord outRecord)
    {
        outRecord.setOperator(getUsername());
        return toAjax(stockInfoService.outStock(outRecord));
    }
}
