import dayjs from "dayjs"; import { message } from "@/utils/message"; import { addDialog } from "@/components/ReDialog"; import type { PaginationProps } from "@pureadmin/table"; import { deviceDetection } from "@pureadmin/utils"; import { getInventoryList, increaseStock, adjustStock } from "@/api/inventory"; import { getProductList } from "@/api/product"; import { getDeviceList } from "@/api/device"; import { type Ref, ref, toRaw, reactive, onMounted } from "vue"; import { ElForm, ElFormItem, ElInput, ElInputNumber, ElSelect, ElOption } from "element-plus"; import type { InventoryItem, InventorySearchForm, StockInForm, StockAdjustForm } from "./types"; export function useInventory(tableRef: Ref) { const form = reactive({ deviceId: "", productId: "", lowStock: false }); const formRef = ref(); const ruleFormRef = ref(); const dataList = ref([]); const loading = ref(true); const deviceOptions = ref([]); const productOptions = ref([]); const pagination = reactive({ total: 0, pageSize: 10, currentPage: 1, background: true }); const columns: TableColumnList = [ { label: "设备ID", prop: "deviceId", minWidth: 120 }, { label: "设备名称", prop: "deviceName", minWidth: 120 }, { label: "商品编码", prop: "productCode", minWidth: 120 }, { label: "商品名称", prop: "productName", minWidth: 150 }, { label: "库存数量", prop: "stock", minWidth: 100, cellRenderer: ({ row }) => ( {row.stock} ) }, { label: "货架号", prop: "shelfNum", minWidth: 80 }, { label: "位置", prop: "position", minWidth: 80 }, { label: "更新时间", prop: "lastUpdateTime", minWidth: 160, formatter: ({ lastUpdateTime }) => lastUpdateTime ? dayjs(lastUpdateTime).format("YYYY-MM-DD HH:mm:ss") : "-" }, { label: "操作", fixed: "right", width: 150, slot: "operation" } ]; // 搜索 async function onSearch() { loading.value = true; try { const searchParams: any = { page: pagination.currentPage, pageSize: pagination.pageSize }; // 只添加非空的搜索条件 if (form.deviceId) searchParams.deviceId = form.deviceId; if (form.productId) searchParams.productId = form.productId; if (form.lowStock) searchParams.lowStock = form.lowStock; const { data } = await getInventoryList(searchParams); if (data) { dataList.value = data.list || []; pagination.total = Number(data.total) || 0; } } catch (error) { console.error("获取库存列表失败:", error); } finally { setTimeout(() => { loading.value = false; }, 300); } } // 重置表单 const resetForm = formEl => { if (!formEl) return; formEl.resetFields(); pagination.currentPage = 1; onSearch(); }; // 分页 function handleSizeChange(val: number) { pagination.pageSize = val; onSearch(); } function handleCurrentChange(val: number) { pagination.currentPage = val; onSearch(); } // 上货 const stockInForm = reactive({ deviceId: "", productId: 0, productCode: "", productName: "", quantity: 1, shelfNum: 1, position: "" }); function handleStockIn() { addDialog({ title: "上货", width: "40%", draggable: true, fullscreen: deviceDetection(), closeOnClickModal: false, contentRenderer: () => ( {deviceOptions.value.map(item => ( ))} {productOptions.value.map(item => ( ))} ), beforeSure: async (done) => { const valid = await ruleFormRef.value.validate().catch(() => false); if (!valid) return; try { const res = await increaseStock(toRaw(stockInForm)); if (res.code === 200) { message("上货成功", { type: "success" }); done(); onSearch(); } else { message(res.message || "上货失败", { type: "error" }); } } catch (error) { message("上货失败", { type: "error" }); } } }); } // 库存调整 const adjustForm = reactive({ deviceId: "", productId: 0, newStock: 0, remark: "" }); function handleAdjust(row: InventoryItem) { adjustForm.deviceId = row.deviceId; adjustForm.productId = row.productId; adjustForm.newStock = row.stock; adjustForm.remark = ""; addDialog({ title: "库存调整", width: "30%", draggable: true, fullscreen: deviceDetection(), closeOnClickModal: false, contentRenderer: () => ( {row.stock} ), beforeSure: async (done) => { const valid = await ruleFormRef.value.validate().catch(() => false); if (!valid) return; try { const res = await adjustStock(toRaw(adjustForm)); if (res.code === 200) { message("库存调整成功", { type: "success" }); done(); onSearch(); } else { message(res.message || "调整失败", { type: "error" }); } } catch (error) { message("调整失败", { type: "error" }); } } }); } // 获取设备列表 async function fetchDeviceOptions() { try { const { data } = await getDeviceList({ page: 1, pageSize: 1000 }); deviceOptions.value = data.list || []; } catch (error) { console.error("获取设备列表失败:", error); } } // 获取商品列表 async function fetchProductOptions() { try { const { data } = await getProductList({ page: 1, pageSize: 1000 }); productOptions.value = data.list || []; } catch (error) { console.error("获取商品列表失败:", error); } } onMounted(async () => { await Promise.all([fetchDeviceOptions(), fetchProductOptions()]); onSearch(); }); return { form, loading, columns, dataList, pagination, deviceOptions, productOptions, onSearch, resetForm, handleStockIn, handleAdjust, handleSizeChange, handleCurrentChange }; }