Files
JayuCloud/JC-Salon-Frontend/src/views/admin/InventoryView.vue
T

345 lines
12 KiB
Vue

<template>
<div class="inventory">
<a-page-header v-if="!isMobile" title="库存管理" sub-title="维护门店耗材库存和预警阈值" />
<section v-else class="admin-page-intro">
<span class="admin-page-intro__eyebrow">库存</span>
<h1 class="admin-page-intro__title">库存管理</h1>
<div class="admin-page-intro__meta">
<span class="admin-page-chip">物料 {{ pagination.total }}</span>
<span class="admin-page-chip">预警 {{ warningCount }}</span>
</div>
</section>
<a-card :bordered="false">
<template #title v-if="isMobile">库存列表</template>
<template #extra>
<div class="admin-toolbar" :class="{ 'admin-toolbar--stacked': isMobile }">
<a-input-search
v-model:value="keyword"
placeholder="搜索物料或分类"
:style="isMobile ? { width: '100%' } : { width: '220px' }"
/>
<a-button type="primary" @click="openCreateModal">
<template #icon>
<PlusOutlined />
</template>
新增物料
</a-button>
</div>
</template>
<a-alert
v-if="warningCount"
class="inventory-alert"
type="warning"
show-icon
:message="`当前有 ${warningCount} 个物料低于预警阈值`"
/>
<a-table
v-if="!isMobile"
:columns="columns"
:data-source="filteredItems"
:loading="loading"
:pagination="pagination"
row-key="id"
:scroll="{ x: 'max-content' }"
@change="handleTableChange"
>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'stock'">
<a-tag :color="record.warning ? 'red' : 'green'">{{ numberText(record.stockQuantity) }} {{ record.unit || '' }}</a-tag>
</template>
<template v-else-if="column.key === 'warning'">{{ numberText(record.warningThreshold) }} {{ record.unit || '' }}</template>
<template v-else-if="column.key === 'status'">
<a-tag :color="record.status === 0 ? 'red' : 'green'">{{ record.status === 0 ? '停用' : '启用' }}</a-tag>
</template>
<template v-else-if="column.key === 'actions'">
<a-space>
<a-button type="link" size="small" @click="openAdjustModal(record as InventoryItemVO, 'IN')">入库</a-button>
<a-button type="link" size="small" @click="openAdjustModal(record as InventoryItemVO, 'OUT')">出库</a-button>
</a-space>
</template>
</template>
</a-table>
<AdminMobileCardList v-else :items="filteredItems" empty-description="暂无库存物料">
<template #item="{ item }">
<article class="admin-mobile-card">
<div class="admin-mobile-card__head">
<div class="admin-mobile-card__body">
<div class="admin-mobile-card__title">{{ item.itemName }}</div>
<div class="admin-mobile-card__subtitle">{{ item.category || '未分类' }}</div>
</div>
<a-tag :color="item.warning ? 'red' : 'green'">{{ item.warning ? '预警' : '正常' }}</a-tag>
</div>
<div class="admin-mobile-card__rows">
<div class="admin-mobile-card__row">
<span>库存</span>
<strong>{{ numberText(item.stockQuantity) }} {{ item.unit || '' }}</strong>
</div>
<div class="admin-mobile-card__row">
<span>预警阈值</span>
<strong>{{ numberText(item.warningThreshold) }} {{ item.unit || '' }}</strong>
</div>
</div>
<div class="admin-mobile-card__actions">
<a-button size="small" @click="openAdjustModal(item, 'IN')">入库</a-button>
<a-button size="small" @click="openAdjustModal(item, 'OUT')">出库</a-button>
</div>
</article>
</template>
</AdminMobileCardList>
</a-card>
<AdminResponsiveModal
v-model:open="itemModalOpen"
title="新增物料"
:desktop-width="680"
:confirm-loading="savingItem"
@ok="saveItem"
@cancel="closeItemModal"
>
<a-form ref="itemFormRef" :model="itemForm" :rules="itemRules" :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }">
<a-form-item label="物料名称" name="itemName">
<a-input v-model:value="itemForm.itemName" placeholder="例如:底胶" />
</a-form-item>
<a-form-item label="分类">
<a-input v-model:value="itemForm.category" placeholder="例如:胶类/工具/耗材" />
</a-form-item>
<a-form-item label="单位">
<a-input v-model:value="itemForm.unit" placeholder="瓶、盒、个" />
</a-form-item>
<a-form-item label="初始库存">
<a-input-number v-model:value="itemForm.stockQuantity" :min="0" :step="1" style="width: 100%" />
</a-form-item>
<a-form-item label="预警阈值">
<a-input-number v-model:value="itemForm.warningThreshold" :min="0" :step="1" style="width: 100%" />
</a-form-item>
<a-form-item label="状态">
<a-select v-model:value="itemForm.status">
<a-select-option :value="1">启用</a-select-option>
<a-select-option :value="0">停用</a-select-option>
</a-select>
</a-form-item>
</a-form>
</AdminResponsiveModal>
<AdminResponsiveModal
v-model:open="adjustModalOpen"
:title="adjustForm.changeType === 'OUT' ? '出库调整' : '入库调整'"
:desktop-width="600"
:confirm-loading="adjusting"
@ok="submitAdjust"
@cancel="closeAdjustModal"
>
<a-form ref="adjustFormRef" :model="adjustForm" :rules="adjustRules" layout="vertical">
<a-alert
v-if="adjustTarget"
class="inventory-alert"
type="info"
show-icon
:message="adjustTarget.itemName"
:description="`当前库存:${numberText(adjustTarget.stockQuantity)} ${adjustTarget.unit || ''}`"
/>
<a-form-item label="调整数量" name="changeQuantity">
<a-input-number v-model:value="adjustForm.changeQuantity" :step="1" style="width: 100%" />
</a-form-item>
<a-form-item label="备注">
<a-textarea v-model:value="adjustForm.remark" :rows="3" :maxlength="200" />
</a-form-item>
</a-form>
</AdminResponsiveModal>
</div>
</template>
<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import type { FormInstance } from 'ant-design-vue'
import { PlusOutlined } from '@ant-design/icons-vue'
import { adminInventoryApi, type InventoryItemVO } from '@/api/admin'
import { runWithGlobalLoading } from '@/utils/action-loading'
import { useAdminViewport } from '@/composables/useAdminViewport'
import AdminMobileCardList from '@/components/admin/AdminMobileCardList.vue'
import AdminResponsiveModal from '@/components/admin/AdminResponsiveModal.vue'
const { isMobile } = useAdminViewport()
const loading = ref(false)
const savingItem = ref(false)
const adjusting = ref(false)
const keyword = ref('')
const items = ref<InventoryItemVO[]>([])
const itemModalOpen = ref(false)
const adjustModalOpen = ref(false)
const adjustTarget = ref<InventoryItemVO | null>(null)
const itemFormRef = ref<FormInstance>()
const adjustFormRef = ref<FormInstance>()
const pagination = reactive({
current: 1,
pageSize: 10,
total: 0,
showSizeChanger: true,
showTotal: (total: number) => `共 ${total} 条`
})
const itemForm = reactive({
itemName: '',
category: '',
unit: '',
stockQuantity: 0,
warningThreshold: 0,
status: 1
})
const adjustForm = reactive({
changeType: 'IN' as 'IN' | 'OUT',
changeQuantity: 1,
remark: ''
})
const itemRules = {
itemName: [{ required: true, message: '请输入物料名称', trigger: 'blur' }]
} as any
const adjustRules = {
changeQuantity: [{ required: true, message: '请输入调整数量', trigger: 'blur' }]
} as any
const columns = [
{ title: '物料名称', dataIndex: 'itemName', key: 'itemName' },
{ title: '分类', dataIndex: 'category', key: 'category', width: 140 },
{ title: '库存', key: 'stock', width: 140 },
{ title: '预警阈值', key: 'warning', width: 140 },
{ title: '状态', key: 'status', width: 100 },
{ title: '操作', key: 'actions', width: 130 }
]
const warningCount = computed(() => items.value.filter(item => item.warning).length)
const filteredItems = computed(() => {
const value = keyword.value.trim().toLowerCase()
if (!value) return items.value
return items.value.filter(item => [item.itemName, item.category, item.unit].join(' ').toLowerCase().includes(value))
})
const numberText = (value?: number) => Number(value || 0).toFixed(2).replace(/\.00$/, '')
const resetItemForm = () => {
Object.assign(itemForm, {
itemName: '',
category: '',
unit: '',
stockQuantity: 0,
warningThreshold: 0,
status: 1
})
}
const loadItems = async () => {
loading.value = true
try {
const result = await adminInventoryApi.listItems({
page: pagination.current,
size: pagination.pageSize
})
items.value = result.list || []
pagination.total = result.total || 0
} catch (error) {
console.error('加载库存失败:', error)
message.error('加载库存失败')
} finally {
loading.value = false
}
}
const handleTableChange = (pag: any) => {
pagination.current = pag.current || 1
pagination.pageSize = pag.pageSize || pagination.pageSize
void loadItems()
}
const openCreateModal = () => {
resetItemForm()
itemModalOpen.value = true
}
const closeItemModal = () => {
itemModalOpen.value = false
itemFormRef.value?.resetFields()
}
const saveItem = async () => {
try {
await itemFormRef.value?.validate()
savingItem.value = true
await runWithGlobalLoading(async () => {
await adminInventoryApi.saveItem({ ...itemForm })
await loadItems()
})
message.success('物料已保存')
closeItemModal()
} catch (error: any) {
if (error?.errorFields) return
console.error('保存物料失败:', error)
message.error(error?.message || '保存物料失败')
} finally {
savingItem.value = false
}
}
const openAdjustModal = (record: InventoryItemVO, changeType: 'IN' | 'OUT') => {
adjustTarget.value = record
adjustForm.changeType = changeType
adjustForm.changeQuantity = changeType === 'OUT' ? -1 : 1
adjustForm.remark = ''
adjustModalOpen.value = true
}
const closeAdjustModal = () => {
adjustModalOpen.value = false
adjustTarget.value = null
adjustFormRef.value?.resetFields()
}
const submitAdjust = async () => {
if (!adjustTarget.value) return
const quantity = Number(adjustForm.changeQuantity || 0)
if (quantity === 0) {
message.warning('调整数量不能为 0')
return
}
adjusting.value = true
try {
await runWithGlobalLoading(async () => {
await adminInventoryApi.adjustStock(adjustTarget.value!.id, {
changeType: adjustForm.changeType,
changeQuantity: quantity,
remark: adjustForm.remark.trim() || undefined
})
await loadItems()
})
message.success('库存已调整')
closeAdjustModal()
} catch (error: any) {
console.error('库存调整失败:', error)
message.error(error?.message || '库存调整失败')
} finally {
adjusting.value = false
}
}
onMounted(loadItems)
</script>
<style scoped>
.inventory {
padding: 0;
}
.inventory-alert {
margin-bottom: 16px;
}
</style>