SoSync.vue
8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<!-- 近期 SO 单同步:每周一次(补货规则里配置星期和时间)拉近 N 个月 SO(库存补充)单存到本地表,定时补货判「历史常备件」时只读本地表。
增量同步:订单头没变的单复用上次的明细,只给新单 / 有变化的单取明细。同步记录、零件表都是服务端分页。 -->
<script setup lang="ts">
import { Refresh, Search, VideoPlay } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { computed, onMounted, onUnmounted, ref, watch } from 'vue'
import { api } from '@/api'
import EmptyState from '@/components/EmptyState.vue'
import PageHeader from '@/components/PageHeader.vue'
import Pager from '@/components/Pager.vue'
import Panel from '@/components/Panel.vue'
import StatusPill from '@/components/StatusPill.vue'
import { usePaged } from '@/composables/usePaged'
import { fmtShort, fmtTime } from '@/format'
const d = ref<any>(null)
const busy = ref(false)
const detail = ref<any>(null)
const syncs = usePaged('/api/restock/so/syncs', { size: 10 })
const parts = usePaged('/api/restock/so/parts', { size: 50, filters: { q: '' } })
const q = ref('')
let t: any = null
let qt: any = null
const WEEK = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
const ST: Record<string, { text: string; tone: any }> = {
running: { text: '同步中', tone: 'primary' }, done: { text: '完成', tone: 'ok' }, failed: { text: '失败', tone: 'err' },
}
async function loadState(silent = false) {
try { d.value = await api.get('/api/restock/so') } catch (e: any) { if (!silent) ElMessage.error(e.message) }
}
function loadAll() { loadState(); syncs.load(); parts.load() }
// 同步进行中才轮询(看进度);结束的那一刻把记录和零件表刷新一下
async function poll() {
const was = d.value?.running
await loadState(true)
if (was || d.value?.running) syncs.load(true)
if (was && !d.value?.running) parts.load(true)
}
onMounted(() => { loadAll(); t = setInterval(poll, 5000) })
onUnmounted(() => { clearInterval(t); clearTimeout(qt) })
watch(q, (v) => { clearTimeout(qt); qt = setTimeout(() => (parts.filters.q = v.trim()), 300) })
const schedule = computed(() => d.value
? `每${d.value.schedule.weekdays.map((i: number) => WEEK[i]).join('、')} ${d.value.schedule.time} 同步近 ${d.value.schedule.months} 个月 SO 单`
: '')
async function syncNow() {
busy.value = true
try { ElMessage.success((await api.post('/api/restock/so/sync')).message); setTimeout(poll, 1000) }
catch (e: any) { ElMessage.error(e.message) } finally { busy.value = false }
}
async function openDetail(row: any) {
try { detail.value = await api.get(`/api/restock/so/syncs/${row.id}`) } catch (e: any) { ElMessage.error(e.message) }
}
// 同步详情:常用的几项单独列出来,其余原样
const STAT_LABEL: Record<string, string> = {
since: '范围起点', order_type: '订单类型', trusted: 'DMS 过滤生效', filter_error: '过滤报错', first_page_types: '第 1 页类型分布',
pages: '翻页数', stop_page: '停在第几页', scanned: '读到的单', orders: '符合条件的单', fetched: '新取明细', reused: '复用明细',
lines: '新取明细行数', parts: '零件种数',
}
const statRows = computed(() => {
const s = detail.value?.stats || {}
return Object.keys(STAT_LABEL).filter((k) => s[k] !== undefined && s[k] !== null && s[k] !== '')
.map((k) => ({ k: STAT_LABEL[k], v: typeof s[k] === 'object' ? JSON.stringify(s[k]) : s[k] === true ? '是' : s[k] === false ? '否' : s[k] }))
})
</script>
<template>
<div class="page">
<PageHeader title="SO 单同步" :desc="`${schedule}。出现在这里的出库件进「历史常备件」组。\n同步是增量的:订单没变的直接复用上次的明细,只取新单和有变化的单。`">
<template #actions>
<el-button :icon="Refresh" @click="loadAll">刷新</el-button>
<el-button type="primary" :icon="VideoPlay" :loading="busy" :disabled="d?.running" @click="syncNow">立即同步</el-button>
</template>
</PageHeader>
<div v-if="d?.running" class="notice primary mb-3">正在同步{{ d.progress ? `:${d.progress}` : '…' }}</div>
<div class="stack">
<Panel flush title="同步记录">
<el-table v-loading="syncs.loading.value" :data="syncs.rows.value" size="small" class="nowrap">
<el-table-column label="开始" width="120"><template #default="{ row }"><span class="num">{{ fmtShort(row.started_at) }}</span></template></el-table-column>
<el-table-column label="状态" width="90"><template #default="{ row }"><StatusPill :tone="ST[row.status]?.tone">{{ ST[row.status]?.text || row.status }}</StatusPill></template></el-table-column>
<el-table-column label="范围" width="120"><template #default="{ row }">{{ row.since }} 起</template></el-table-column>
<el-table-column label="触发" width="150" show-overflow-tooltip><template #default="{ row }">{{ row.trigger === 'schedule' ? '每周定时' : '手动' }} · {{ row.triggered_by }}</template></el-table-column>
<el-table-column label="单 / 零件" width="110" align="right"><template #default="{ row }"><span class="num">{{ row.orders ?? '—' }} / {{ row.parts ?? '—' }}</span></template></el-table-column>
<el-table-column label="新取 / 复用" width="110" align="right"><template #default="{ row }"><span class="num">{{ row.fetched ?? '—' }} / {{ row.reused ?? '—' }}</span></template></el-table-column>
<el-table-column label="耗时" width="90" align="right">
<template #default="{ row }"><span class="num">{{ row.finished_at ? Math.round((+new Date(row.finished_at) - +new Date(row.started_at)) / 1000) + ' 秒' : '—' }}</span></template>
</el-table-column>
<el-table-column label="DMS 过滤" width="110">
<template #default="{ row }"><span v-if="row.filter_trusted === null">—</span><el-tag v-else size="small" :type="row.filter_trusted ? 'success' : 'warning'">{{ row.filter_trusted ? '生效' : '全量翻页' }}</el-tag></template>
</el-table-column>
<el-table-column label="错误" min-width="160" show-overflow-tooltip><template #default="{ row }"><span class="small err">{{ row.error || '' }}</span></template></el-table-column>
<el-table-column label="" width="64" fixed="right"><template #default="{ row }"><el-button link type="primary" @click="openDetail(row)">详情</el-button></template></el-table-column>
<template #empty><EmptyState title="还没有同步过" /></template>
</el-table>
<Pager v-model:page="syncs.page.value" v-model:size="syncs.size.value" :total="syncs.total.value" :sizes="[10, 20, 50]" />
</Panel>
<Panel flush title="近期 SO 零件" desc="最近一次成功同步的结果。">
<template #toolbar><el-input v-model="q" :prefix-icon="Search" placeholder="搜索零件号 / 名称" clearable class="w-search" /></template>
<el-table v-loading="parts.loading.value" :data="parts.rows.value" size="small" class="nowrap">
<el-table-column label="零件号" width="150"><template #default="{ row }"><span class="mono">{{ row.part_no }}</span></template></el-table-column>
<el-table-column prop="name" label="名称" min-width="160" show-overflow-tooltip />
<el-table-column prop="order_count" label="出现单数" width="90" align="right" />
<el-table-column prop="qty_sum" label="订购合计" width="90" align="right" />
<el-table-column label="最近一张" min-width="210"><template #default="{ row }"><span class="mono">{{ row.last_order_no }}</span> <span class="small muted">{{ row.last_order_date }}</span></template></el-table-column>
<el-table-column label="同步时间" width="120"><template #default="{ row }"><span class="num">{{ fmtShort(row.synced_at) }}</span></template></el-table-column>
</el-table>
<Pager v-model:page="parts.page.value" v-model:size="parts.size.value" :total="parts.total.value" />
</Panel>
</div>
<el-dialog v-if="detail" :model-value="!!detail" :title="`同步记录 #${detail.id} · ${fmtTime(detail.started_at)}`" width="640px" @close="detail = null">
<dl class="kv">
<template v-for="r in statRows" :key="r.k"><dt>{{ r.k }}</dt><dd class="small">{{ r.v }}</dd></template>
</dl>
<template v-if="detail.stats?.detail_mismatch?.length">
<div class="sub-title">明细与订单头不一致({{ detail.stats.detail_mismatch.length }})</div>
<ul class="mismatch small"><li v-for="m in detail.stats.detail_mismatch" :key="m">{{ m }}</li></ul>
</template>
<div v-if="detail.error" class="notice err">{{ detail.error }}</div>
</el-dialog>
</div>
</template>
<style scoped>
.w-search { width: 240px; }
.mb-3 { margin-bottom: 12px; }
.nowrap :deep(.cell) { white-space: nowrap; }
.mismatch { margin: 6px 0 0; padding-left: 18px; max-height: 240px; overflow: auto; }
</style>