index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <template>
  2. <div class="capacity-management">
  3. <el-card class="setting-card">
  4. <div class="setting-bar">
  5. <div class="setting-item">
  6. <span class="setting-label">总库容边界</span>
  7. <el-input-number v-model="capacityLimit" :min="1" :step="1000" size="small" />
  8. <span class="setting-unit">件</span>
  9. </div>
  10. <div class="setting-item">
  11. <span class="setting-label">爆仓预警线</span>
  12. <el-input-number v-model="warningBoundary" :min="50" :max="100" :step="1" size="small" />
  13. <span class="setting-unit">%</span>
  14. </div>
  15. <el-button type="primary" size="small" @click="refreshData" :loading="loading">
  16. <i class="el-icon-refresh"></i> 刷新
  17. </el-button>
  18. </div>
  19. </el-card>
  20. <el-row :gutter="20" class="metrics-row">
  21. <el-col :span="6">
  22. <el-card class="metric-card">
  23. <div class="metric-content">
  24. <div class="metric-icon blue"><i class="el-icon-data-analysis"></i></div>
  25. <div class="metric-info">
  26. <div class="metric-label">库容利用率</div>
  27. <div class="metric-value">{{ formatPercent(capacityUtilization) }}</div>
  28. <div class="metric-unit">当前库存 / 总库容</div>
  29. </div>
  30. </div>
  31. </el-card>
  32. </el-col>
  33. <el-col :span="6">
  34. <el-card class="metric-card">
  35. <div class="metric-content">
  36. <div class="metric-icon green"><i class="el-icon-box"></i></div>
  37. <div class="metric-info">
  38. <div class="metric-label">根据库容边界可存储量</div>
  39. <div class="metric-value">{{ formatInteger(availableByBoundary) }}</div>
  40. <div class="metric-unit">件</div>
  41. </div>
  42. </div>
  43. </el-card>
  44. </el-col>
  45. <el-col :span="6">
  46. <el-card class="metric-card">
  47. <div class="metric-content">
  48. <div class="metric-icon orange"><i class="el-icon-warning-outline"></i></div>
  49. <div class="metric-info">
  50. <div class="metric-label">爆仓预警</div>
  51. <div class="metric-value status-value" :class="overflowWarning.type">{{ overflowWarning.text }}</div>
  52. <div class="metric-unit">预警线 {{ warningBoundary }}%</div>
  53. </div>
  54. </div>
  55. </el-card>
  56. </el-col>
  57. <el-col :span="6">
  58. <el-card class="metric-card">
  59. <div class="metric-content">
  60. <div class="metric-icon red"><i class="el-icon-s-grid"></i></div>
  61. <div class="metric-info">
  62. <div class="metric-label">当前库存占用</div>
  63. <div class="metric-value">{{ formatInteger(currentInventory) }}</div>
  64. <div class="metric-unit">件</div>
  65. </div>
  66. </div>
  67. </el-card>
  68. </el-col>
  69. </el-row>
  70. <el-row :gutter="20" class="charts-row">
  71. <el-col :span="12">
  72. <el-card>
  73. <template slot="header">
  74. <span>各品类库存库容占比</span>
  75. </template>
  76. <div ref="categoryCapacityChart" class="chart" />
  77. <el-empty v-if="!loading && categoryCapacityRows.length === 0" description="暂无品类库存数据" />
  78. </el-card>
  79. </el-col>
  80. <el-col :span="12">
  81. <el-card>
  82. <template slot="header">
  83. <span>库容边界结构</span>
  84. </template>
  85. <div ref="capacityGaugeChart" class="chart" />
  86. </el-card>
  87. </el-col>
  88. </el-row>
  89. <el-card class="table-card">
  90. <template slot="header">
  91. <div class="card-header">
  92. <span>品类库容明细</span>
  93. <el-tag :type="overflowWarning.tagType">{{ overflowWarning.text }}</el-tag>
  94. </div>
  95. </template>
  96. <el-table :data="categoryCapacityRows" stripe v-loading="loading">
  97. <el-table-column prop="category" label="品类" min-width="160" />
  98. <el-table-column label="库存量" width="140" align="right">
  99. <template slot-scope="scope">{{ formatInteger(scope.row.inventory) }}</template>
  100. </el-table-column>
  101. <el-table-column label="占总库容" width="140" align="right">
  102. <template slot-scope="scope">{{ formatPercent(scope.row.capacityRatio) }}</template>
  103. </el-table-column>
  104. <el-table-column label="占当前库存" width="140" align="right">
  105. <template slot-scope="scope">{{ formatPercent(scope.row.inventoryRatio) }}</template>
  106. </el-table-column>
  107. <el-table-column label="边界剩余可存" width="160" align="right">
  108. <template slot-scope="scope">{{ formatInteger(scope.row.availableByBoundary) }}</template>
  109. </el-table-column>
  110. <el-table-column label="状态" width="120">
  111. <template slot-scope="scope">
  112. <el-tag :type="getCategoryStatus(scope.row).type">{{ getCategoryStatus(scope.row).text }}</el-tag>
  113. </template>
  114. </el-table-column>
  115. </el-table>
  116. </el-card>
  117. </div>
  118. </template>
  119. <script>
  120. import request from '@/utils/request'
  121. import * as echarts from 'echarts'
  122. require('echarts/theme/macarons')
  123. export default {
  124. name: 'StorageStock',
  125. data() {
  126. return {
  127. loading: false,
  128. capacityLimit: 100000,
  129. warningBoundary: 85,
  130. overviewData: {
  131. totalInventory: 0
  132. },
  133. skuRows: [],
  134. categoryCapacityChart: null,
  135. capacityGaugeChart: null
  136. }
  137. },
  138. computed: {
  139. currentInventory() {
  140. const totalFromSku = this.skuRows.reduce((sum, row) => sum + this.getRowInventory(row), 0)
  141. return totalFromSku > 0 ? totalFromSku : Number(this.overviewData.totalInventory || 0)
  142. },
  143. boundaryCapacity() {
  144. return Number(this.capacityLimit || 0) * Number(this.warningBoundary || 0) / 100
  145. },
  146. capacityUtilization() {
  147. if (!this.capacityLimit) return 0
  148. return this.currentInventory / this.capacityLimit * 100
  149. },
  150. availableByBoundary() {
  151. return Math.max(this.boundaryCapacity - this.currentInventory, 0)
  152. },
  153. overflowWarning() {
  154. if (this.currentInventory >= this.capacityLimit) {
  155. return { text: '已爆仓', type: 'danger', tagType: 'danger' }
  156. }
  157. if (this.currentInventory >= this.boundaryCapacity) {
  158. return { text: '接近爆仓', type: 'warning', tagType: 'warning' }
  159. }
  160. return { text: '库容正常', type: 'success', tagType: 'success' }
  161. },
  162. categoryCapacityRows() {
  163. const map = {}
  164. this.skuRows.forEach(row => {
  165. const category = row.category || row.productType || row.attribute || '未分类'
  166. if (!map[category]) map[category] = 0
  167. map[category] += this.getRowInventory(row)
  168. })
  169. return Object.keys(map)
  170. .map(category => {
  171. const inventory = map[category]
  172. return {
  173. category,
  174. inventory,
  175. capacityRatio: this.capacityLimit ? inventory / this.capacityLimit * 100 : 0,
  176. inventoryRatio: this.currentInventory ? inventory / this.currentInventory * 100 : 0,
  177. availableByBoundary: Math.max(this.boundaryCapacity * (inventory / Math.max(this.currentInventory, 1)) - inventory, 0)
  178. }
  179. })
  180. .sort((a, b) => b.inventory - a.inventory)
  181. }
  182. },
  183. mounted() {
  184. this.loadCapacitySettings()
  185. this.refreshData()
  186. window.addEventListener('resize', this.resizeCharts)
  187. },
  188. beforeDestroy() {
  189. window.removeEventListener('resize', this.resizeCharts)
  190. if (this.categoryCapacityChart) this.categoryCapacityChart.dispose()
  191. if (this.capacityGaugeChart) this.capacityGaugeChart.dispose()
  192. },
  193. watch: {
  194. capacityLimit() {
  195. this.saveCapacitySettings()
  196. this.$nextTick(this.updateCharts)
  197. },
  198. warningBoundary() {
  199. this.saveCapacitySettings()
  200. this.$nextTick(this.updateCharts)
  201. },
  202. categoryCapacityRows() {
  203. this.$nextTick(this.updateCharts)
  204. }
  205. },
  206. methods: {
  207. normalizeResponse(res) {
  208. if (!res) return null
  209. if (res.code === 200) return res.data
  210. if (res.data) return res.data
  211. return null
  212. },
  213. loadCapacitySettings() {
  214. try {
  215. const raw = localStorage.getItem('storage-capacity-settings')
  216. if (!raw) return
  217. const data = JSON.parse(raw)
  218. if (data.capacityLimit) this.capacityLimit = Number(data.capacityLimit)
  219. if (data.warningBoundary) this.warningBoundary = Number(data.warningBoundary)
  220. } catch (e) {
  221. console.error('读取库容设置失败:', e)
  222. }
  223. },
  224. saveCapacitySettings() {
  225. localStorage.setItem('storage-capacity-settings', JSON.stringify({
  226. capacityLimit: this.capacityLimit,
  227. warningBoundary: this.warningBoundary
  228. }))
  229. },
  230. async refreshData() {
  231. this.loading = true
  232. try {
  233. await Promise.all([this.fetchOverview(), this.fetchSkuSummary()])
  234. } finally {
  235. this.loading = false
  236. this.$nextTick(() => {
  237. this.initCharts()
  238. this.updateCharts()
  239. })
  240. }
  241. },
  242. async fetchOverview() {
  243. try {
  244. const res = await request({ url: '/api/inventory/overview', method: 'get', timeout: 120000 })
  245. const data = this.normalizeResponse(res)
  246. if (data) this.overviewData = { ...this.overviewData, ...data }
  247. } catch (e) {
  248. console.error('获取库存概览失败:', e)
  249. }
  250. },
  251. async fetchSkuSummary() {
  252. try {
  253. const res = await request({ url: '/api/inventory/sku-summary', method: 'get', timeout: 120000 })
  254. const data = this.normalizeResponse(res)
  255. this.skuRows = Array.isArray(data) ? data : []
  256. } catch (e) {
  257. console.error('获取SKU库存失败:', e)
  258. this.skuRows = []
  259. }
  260. },
  261. getRowInventory(row) {
  262. return Number(row.inventory || row.currentInventory || row.remainingInventory || row.quantity || 0)
  263. },
  264. initCharts() {
  265. if (!this.categoryCapacityChart && this.$refs.categoryCapacityChart) {
  266. this.categoryCapacityChart = echarts.init(this.$refs.categoryCapacityChart, 'macarons')
  267. }
  268. if (!this.capacityGaugeChart && this.$refs.capacityGaugeChart) {
  269. this.capacityGaugeChart = echarts.init(this.$refs.capacityGaugeChart, 'macarons')
  270. }
  271. },
  272. updateCharts() {
  273. this.updateCategoryChart()
  274. this.updateGaugeChart()
  275. },
  276. updateCategoryChart() {
  277. if (!this.categoryCapacityChart) return
  278. const rows = this.categoryCapacityRows
  279. this.categoryCapacityChart.setOption({
  280. tooltip: {
  281. trigger: 'axis',
  282. axisPointer: { type: 'shadow' },
  283. formatter: params => {
  284. const item = params && params.length ? params[0] : null
  285. const row = item ? rows[item.dataIndex] : null
  286. if (!row) return ''
  287. return `${row.category}<br/>库存: ${this.formatInteger(row.inventory)} 件<br/>占总库容: ${this.formatPercent(row.capacityRatio)}`
  288. }
  289. },
  290. grid: { left: '3%', right: '5%', bottom: '3%', containLabel: true },
  291. xAxis: { type: 'value', name: '%' },
  292. yAxis: {
  293. type: 'category',
  294. inverse: true,
  295. data: rows.map(item => item.category),
  296. axisLabel: { width: 110, overflow: 'truncate' }
  297. },
  298. series: [{
  299. name: '库容占比',
  300. type: 'bar',
  301. barMaxWidth: 18,
  302. data: rows.map(item => Number(item.capacityRatio.toFixed(2))),
  303. itemStyle: { color: '#409eff' },
  304. label: { show: true, position: 'right', formatter: '{c}%' }
  305. }]
  306. }, true)
  307. },
  308. updateGaugeChart() {
  309. if (!this.capacityGaugeChart) return
  310. this.capacityGaugeChart.setOption({
  311. tooltip: {
  312. formatter: `库容利用率<br/>${this.formatPercent(this.capacityUtilization)}`
  313. },
  314. series: [{
  315. name: '库容利用率',
  316. type: 'gauge',
  317. min: 0,
  318. max: 100,
  319. progress: { show: true, width: 14 },
  320. axisLine: { lineStyle: { width: 14 } },
  321. pointer: { width: 4 },
  322. detail: {
  323. formatter: value => `${Number(value || 0).toFixed(1)}%`,
  324. fontSize: 24
  325. },
  326. data: [{ value: Number(this.capacityUtilization.toFixed(1)), name: `预警线 ${this.warningBoundary}%` }]
  327. }]
  328. }, true)
  329. },
  330. resizeCharts() {
  331. if (this.categoryCapacityChart) this.categoryCapacityChart.resize()
  332. if (this.capacityGaugeChart) this.capacityGaugeChart.resize()
  333. },
  334. getCategoryStatus(row) {
  335. if (row.capacityRatio >= this.warningBoundary) return { text: '高占用', type: 'danger' }
  336. if (row.capacityRatio >= this.warningBoundary * 0.7) return { text: '偏高', type: 'warning' }
  337. return { text: '正常', type: 'success' }
  338. },
  339. formatInteger(value) {
  340. return Number(value || 0).toLocaleString()
  341. },
  342. formatPercent(value) {
  343. return `${Number(value || 0).toFixed(1)}%`
  344. }
  345. }
  346. }
  347. </script>
  348. <style scoped>
  349. .capacity-management {
  350. width: 100%;
  351. }
  352. .setting-card,
  353. .metrics-row,
  354. .charts-row,
  355. .table-card {
  356. margin-bottom: 20px;
  357. }
  358. .setting-bar {
  359. display: flex;
  360. align-items: center;
  361. gap: 18px;
  362. flex-wrap: wrap;
  363. }
  364. .setting-item {
  365. display: flex;
  366. align-items: center;
  367. gap: 8px;
  368. }
  369. .setting-label {
  370. font-size: 14px;
  371. color: #606266;
  372. }
  373. .setting-unit,
  374. .metric-unit {
  375. font-size: 12px;
  376. color: #909399;
  377. }
  378. .metric-card {
  379. transition: transform 0.3s;
  380. }
  381. .metric-card:hover {
  382. transform: translateY(-5px);
  383. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  384. }
  385. .metric-content {
  386. display: flex;
  387. align-items: center;
  388. gap: 15px;
  389. }
  390. .metric-icon {
  391. width: 58px;
  392. height: 58px;
  393. border-radius: 12px;
  394. display: flex;
  395. align-items: center;
  396. justify-content: center;
  397. color: #fff;
  398. font-size: 28px;
  399. }
  400. .metric-icon.blue {
  401. background: #409eff;
  402. }
  403. .metric-icon.green {
  404. background: #67c23a;
  405. }
  406. .metric-icon.orange {
  407. background: #e6a23c;
  408. }
  409. .metric-icon.red {
  410. background: #f56c6c;
  411. }
  412. .metric-info {
  413. min-width: 0;
  414. flex: 1;
  415. }
  416. .metric-label {
  417. font-size: 14px;
  418. color: #909399;
  419. margin-bottom: 8px;
  420. }
  421. .metric-value {
  422. font-size: 28px;
  423. font-weight: 700;
  424. color: #303133;
  425. line-height: 1.1;
  426. }
  427. .status-value {
  428. font-size: 24px;
  429. }
  430. .status-value.success {
  431. color: #67c23a;
  432. }
  433. .status-value.warning {
  434. color: #e6a23c;
  435. }
  436. .status-value.danger {
  437. color: #f56c6c;
  438. }
  439. .chart {
  440. height: 320px;
  441. }
  442. .card-header {
  443. display: flex;
  444. justify-content: space-between;
  445. align-items: center;
  446. }
  447. </style>