DatasetReleasePanel.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <script setup lang="ts">
  2. import { computed, onMounted, ref } from 'vue';
  3. import { ElMessage, ElMessageBox } from 'element-plus';
  4. import GovernancePackageEditor from './GovernancePackageEditor.vue';
  5. import ApiPullWorkspace from './ApiPullWorkspace.vue';
  6. import EdgeDeviceWorkspace from './EdgeDeviceWorkspace.vue';
  7. import IngestBatchStatus from './IngestBatchStatus.vue';
  8. import IngestRawRetention from './IngestRawRetention.vue';
  9. import IngestPushSample from './IngestPushSample.vue';
  10. import RelationalTrialPanel from './RelationalTrialPanel.vue';
  11. import { changeLifecycle, releaseAction, saveCandidate, versions, type Candidate, type IngestDataset, type VersionWorkspace } from '../api/ingestDatasets';
  12. const props = defineProps<{ dataset: IngestDataset; factoryId: string; factoryName: string }>();
  13. const emit = defineEmits<{ close: []; published: [] }>();
  14. const model = ref<VersionWorkspace>();
  15. const busy = ref(false);
  16. const source = ref(props.dataset.bindings.find((b) => b.sourceEnabled)?.sourceId ?? '');
  17. const version = ref('');
  18. const fields = ['PoNo', 'PoLine', 'ItemCode', 'OrderQty', 'SupplierCode', 'OrderDate', 'DueDate', 'Status', 'sourceUpdatedAt'];
  19. const names = ref<Record<string, string>>(Object.fromEntries(fields.map((f) => [f, f])));
  20. const selected = ref('');
  21. const candidate = computed(() => model.value?.history.find((c) => c.id === selected.value));
  22. const currentSourceRelease = computed(() => model.value?.history.find((row) => row.isCurrent && row.sourceId === source.value));
  23. const obsolete = computed(() => candidate.value?.baseRevision !== props.dataset.revision);
  24. const trialSample = ref('');
  25. const expectedValid = ref(2);
  26. const expectedIsolated = ref(1);
  27. const expectedQuantity = ref(15.5);
  28. const migrationReference = ref('');
  29. const evidence = computed(() => (candidate.value?.trialJson ? JSON.parse(candidate.value.trialJson) : undefined));
  30. const labels: Record<string, string> = {
  31. DRAFT: '草稿',
  32. PREFLIGHT: '预检通过',
  33. RECONCILING: '对账中',
  34. REVIEW: '待审批',
  35. PUBLISHED: '版本已启用',
  36. AWAITING_HANDOFF: '等待有效交接回执',
  37. HANDED_OVER: '已交接',
  38. PAUSED: '已暂停',
  39. RETIRED: '已退役',
  40. Approved: '审批通过',
  41. Running: '审批中',
  42. Rejected: '已退回',
  43. Cancelled: '已撤销',
  44. Terminated: '已终止',
  45. };
  46. const actions: Record<string, string> = { DRAFT: '创建草稿', precheck: '预检通过', trial: '完成沙箱试跑', submit: '提交审批', publish: '启用版本', pause: '暂停', resume: '恢复', retire: '退役', 'schema-migration-ready': '登记迁移完成' };
  47. const mappingRows = computed(() => {
  48. if (!candidate.value) return [];
  49. const live = model.value?.history.find((row) => row.state === 'PUBLISHED' && row.isCurrent && row.sourceId === candidate.value?.sourceId);
  50. const current = live ? fieldNames(live.mappingJson) : {};
  51. const proposed = fieldNames(candidate.value.mappingJson);
  52. return fields.map((field) => ({ field, current: current[field] ?? (props.dataset.activeSchemaVersion ? field : '—'), proposed: proposed[field] ?? field }));
  53. });
  54. function fieldNames(mapping: string): Record<string, string> {
  55. const parsed = JSON.parse(mapping);
  56. return parsed.format === 'mdp-governance-v1'
  57. ? Object.fromEntries(parsed.rules.map((rule: { target: string; source: string }) => [rule.target, rule.source]))
  58. : parsed;
  59. }
  60. function example(row: Candidate) {
  61. const parsed = JSON.parse(row.mappingJson);
  62. if (parsed.format === 'mdp-governance-v1') {
  63. trialSample.value = '';
  64. return;
  65. }
  66. const map = fieldNames(row.mappingJson);
  67. const list = [12.5, 3, 2].map((qty, i) => {
  68. const item: Record<string, string | number> = { PoNo: 'SANDBOX-PO', PoLine: String(i + 1), OrderQty: qty, sourceUpdatedAt: '2026-11-20T09:00:00+08:00' };
  69. if (i < 2) item.ItemCode = 'ITEM-' + (i + 1);
  70. return Object.fromEntries(Object.entries(item).map(([k, v]) => [map[k] ?? k, v]));
  71. });
  72. trialSample.value = JSON.stringify({ data: { list } }, null, 2);
  73. }
  74. async function refresh() {
  75. model.value = await versions(props.dataset.id, props.factoryId);
  76. if (!model.value.history.some((c) => c.id === selected.value)) selected.value = model.value.history[0]?.id ?? '';
  77. }
  78. onMounted(async () => {
  79. await refresh();
  80. const active = currentSourceRelease.value;
  81. if (active) names.value = fieldNames(active.mappingJson);
  82. if (candidate.value) example(candidate.value);
  83. });
  84. async function run(work: () => Promise<unknown>, published = false) {
  85. if (busy.value) return;
  86. busy.value = true;
  87. try {
  88. await work();
  89. if (published) emit('published');
  90. else {
  91. await refresh();
  92. ElMessage.success('操作已保存');
  93. }
  94. } finally {
  95. busy.value = false;
  96. }
  97. }
  98. function draft() {
  99. return run(async () => {
  100. const response = await saveCandidate(props.dataset.id, props.factoryId, {
  101. version: version.value.trim(),
  102. sourceId: source.value,
  103. revision: props.dataset.revision,
  104. mappingJson: JSON.stringify(names.value),
  105. });
  106. selected.value = response.data.id;
  107. await refresh();
  108. if (candidate.value) example(candidate.value);
  109. });
  110. }
  111. function transition(action: string) {
  112. if (!candidate.value) return;
  113. const id = candidate.value.id;
  114. return run(
  115. () =>
  116. releaseAction(
  117. props.dataset.id,
  118. props.factoryId,
  119. id,
  120. action,
  121. action === 'trial' ? { sample: trialSample.value, expectedValid: expectedValid.value, expectedIsolated: expectedIsolated.value, expectedQuantity: expectedQuantity.value } : {}
  122. ),
  123. action === 'publish'
  124. );
  125. }
  126. function recordMigration() {
  127. const row = candidate.value;
  128. if (!row || !migrationReference.value.trim()) return;
  129. return run(async () => {
  130. await releaseAction(props.dataset.id, props.factoryId, row.id, 'migration-ready', { evidenceReference: migrationReference.value.trim() });
  131. migrationReference.value = '';
  132. });
  133. }
  134. async function savedPackage(id: string, sample: string) {
  135. selected.value = id;
  136. await refresh();
  137. trialSample.value = sample;
  138. expectedValid.value = 1;
  139. expectedIsolated.value = 0;
  140. }
  141. async function lifecycle(action: string) {
  142. const result = await ElMessageBox.prompt('填写原因', '数据集' + (action === 'pause' ? '暂停' : action === 'resume' ? '恢复' : '退役'), { inputPattern: /\S/ });
  143. await run(() => changeLifecycle(props.dataset, props.factoryId, action, result.value), true);
  144. }
  145. </script>
  146. <template>
  147. <el-dialog :model-value="true" :title="factoryName + ' · ' + dataset.name" width="min(780px,95vw)" append-to-body @close="emit('close')">
  148. <p>
  149. 当前来源版本 {{ currentSourceRelease?.version || dataset.activeSchemaVersion || '尚未发布' }} · {{ model?.environment }} ·
  150. {{ model?.lifecycle === 'RETIRED' ? '已退役' : model?.lifecycle === 'PAUSED' ? '已暂停' : dataset.enabled ? '接收中' : '未启用' }}
  151. </p>
  152. <el-collapse v-if="model && model.lifecycle !== 'RETIRED'">
  153. <el-collapse-item title="新建采购接收候选版本" name="draft">
  154. <el-form label-width="110px">
  155. <el-form-item label="试跑来源"
  156. ><el-select v-model="source"><el-option v-for="b in dataset.bindings.filter((x) => x.sourceEnabled)" :key="b.sourceId" :label="b.sourceName" :value="b.sourceId" /></el-select
  157. ></el-form-item>
  158. <el-form-item label="版本号"><el-input v-model="version" maxlength="64" placeholder="例如 po-source-v2" /></el-form-item>
  159. <el-form-item v-for="f in fields" :key="f" :label="f"><el-input v-model="names[f]" placeholder="来源字段名" /></el-form-item>
  160. <el-button :disabled="!version || !source || busy" @click="draft">保存为候选</el-button>
  161. </el-form>
  162. </el-collapse-item>
  163. <el-collapse-item title="标准化配置包与模板" name="governance">
  164. <el-select v-model="source" aria-label="配置包来源" placeholder="选择已绑定来源">
  165. <el-option v-for="b in dataset.bindings.filter((x) => x.sourceEnabled)" :key="b.sourceId" :label="b.sourceName" :value="b.sourceId" />
  166. </el-select>
  167. <GovernancePackageEditor :dataset-id="dataset.id" :factory-id="factoryId" :source-id="source" :revision="dataset.revision"
  168. :can-trial="model.canTrial" @saved="savedPackage" />
  169. </el-collapse-item>
  170. </el-collapse>
  171. <el-table
  172. :data="model?.history || []"
  173. highlight-current-row
  174. @row-click="
  175. (c: Candidate) => {
  176. selected = c.id;
  177. migrationReference = '';
  178. example(c);
  179. }
  180. "
  181. >
  182. <el-table-column prop="version" label="版本" /><el-table-column prop="environmentKey" label="环境" />
  183. <el-table-column label="状态"
  184. ><template #default="{ row }">{{ labels[row.approval] && row.state === 'REVIEW' ? labels[row.approval] : labels[row.projectState ?? row.state] }}</template></el-table-column
  185. >
  186. </el-table>
  187. <section v-if="candidate">
  188. <p>{{ candidate.version }} · {{ dataset.bindings.find((b) => b.sourceId === candidate?.sourceId)?.sourceName ?? candidate.sourceId }} · {{ labels[candidate.state] }} <span v-if="obsolete && candidate.state !== 'PUBLISHED'">· 配置已变更,请新建候选</span></p>
  189. <p v-if="candidate.state === 'PUBLISHED'">项目状态(可见工厂):{{ labels[candidate.projectState] || '尚无可核实回执' }}</p>
  190. <p v-if="candidate.handoff">
  191. {{ candidate.handoff.accepted > 0 ? '首个有效交接回执' : '零接收回执' }} · 批次 {{ candidate.handoff.batchId }}
  192. · 工厂 {{ candidate.handoff.factoryId }} · 接收 {{ candidate.handoff.accepted }} 行 · 隔离 {{ candidate.handoff.isolated }} 行
  193. · 确认时间 {{ candidate.handoff.acknowledgedAt }}
  194. </p>
  195. <el-collapse>
  196. <el-collapse-item title="查看与当前启用版本的字段差异" name="mapping">
  197. <el-table :data="mappingRows">
  198. <el-table-column prop="field" label="采购字段" /><el-table-column prop="current" label="当前来源字段" />
  199. <el-table-column label="所选版本来源字段"
  200. ><template #default="{ row }"
  201. ><strong v-if="row.current !== row.proposed">{{ row.proposed }}</strong
  202. ><span v-else>{{ row.proposed }}</span></template
  203. ></el-table-column
  204. >
  205. </el-table>
  206. </el-collapse-item>
  207. </el-collapse>
  208. <p v-if="candidate.flowId">审批实例 {{ candidate.flowId }} · {{ labels[candidate.approval || ''] }}</p>
  209. <template v-if="candidate.schemaMigrationRequired">
  210. <p v-if="candidate.schemaMigrationReady">调用方迁移已登记</p>
  211. <p v-else-if="model?.lifecycle !== 'PAUSED'">字段映射有变更。请暂停接收,并按暂停后的修订新建候选。</p>
  212. <p v-else>字段映射有变更。处理完在途批次后登记调用方迁移记录。</p>
  213. <template v-if="!candidate.schemaMigrationReady && !obsolete && model?.lifecycle === 'PAUSED' && model.canPublish && candidate.approval === 'Approved'">
  214. <el-input v-model="migrationReference" maxlength="250" aria-label="迁移完成凭据" placeholder="填写调用方迁移完成的工单或联测记录" />
  215. <el-button :disabled="busy || !migrationReference.trim()" @click="recordMigration">登记迁移完成</el-button>
  216. </template>
  217. </template>
  218. <el-button v-if="candidate.state === 'DRAFT'" :disabled="busy || obsolete" @click="transition('precheck')">预检</el-button>
  219. <template v-if="['PREFLIGHT', 'RECONCILING'].includes(candidate.state) && model?.canTrial">
  220. <el-input v-model="trialSample" type="textarea" :rows="5" aria-label="采购试跑样本" />
  221. <p>
  222. 预期有效行 <el-input-number v-model="expectedValid" :min="1" size="small" /> 隔离行 <el-input-number v-model="expectedIsolated" :min="0" size="small" /> 数量合计
  223. <el-input-number v-model="expectedQuantity" :min="0" size="small" />
  224. </p>
  225. <el-button :disabled="busy || obsolete" @click="transition('trial')">沙箱试跑</el-button>
  226. </template>
  227. <template v-if="evidence"
  228. ><p>
  229. 批次 {{ evidence.batchId }} · 接收 {{ evidence.received }} · 隔离 {{ evidence.isolated }} · 贴源 {{ evidence.staged }} 行 / {{ evidence.stagedQuantity }} · 标准 {{ evidence.standard }} 行 /
  230. {{ evidence.standardQuantity }} · {{ evidence.matched ? '对账通过' : '存在差额' }}
  231. </p>
  232. <p v-if="evidence.expectedValid != null">预期有效 {{ evidence.expectedValid }} 行,隔离 {{ evidence.expectedIsolated }} 行,数量合计 {{ evidence.expectedQuantity }}。</p>
  233. <p v-for="q in evidence.quarantines" :key="q.rawEventId">{{ q.field }} · {{ q.reason }} · {{ q.rawEventId }}</p>
  234. </template>
  235. <el-button v-if="candidate.state === 'RECONCILING' && model?.canTrial" :disabled="busy || obsolete || !evidence?.matched" @click="transition('submit')">提交审批</el-button>
  236. <el-button v-if="candidate.state === 'REVIEW' && model?.canPublish" :disabled="busy || obsolete || candidate.approval !== 'Approved' || (candidate.schemaMigrationRequired && !candidate.schemaMigrationReady)" type="primary" @click="transition('publish')"
  237. >启用 {{ candidate.version }}</el-button
  238. >
  239. </section>
  240. <IngestBatchStatus :key="factoryId + '/' + dataset.id" :dataset-id="dataset.id" :factory-id="factoryId" :source-id="source" :has-schema="!!dataset.activeSchemaVersion" :can-inspect="model?.canTrial" />
  241. <IngestRawRetention v-if="model?.canTrial" :key="factoryId + '/retention/' + dataset.id" :dataset-id="dataset.id" :factory-id="factoryId" />
  242. <IngestPushSample v-if="dataset.activeSchemaVersion" :key="'sample/' + factoryId + '/' + dataset.id" :dataset-id="dataset.id" :factory-id="factoryId" :source-id="source" />
  243. <ApiPullWorkspace v-if="model?.canTrial" :key="factoryId + '/api-pull/' + dataset.id" :dataset="dataset" :factory-id="factoryId" />
  244. <EdgeDeviceWorkspace v-if="model?.canTrial" :key="factoryId + '/edge/' + dataset.id" :dataset="dataset" :factory-id="factoryId" />
  245. <RelationalTrialPanel v-if="model?.canTrial" :dataset-id="dataset.id" :factory-id="factoryId" :active="model.lifecycle === 'ACTIVE' && dataset.enabled" />
  246. <el-collapse v-if="model?.timeline.length">
  247. <el-collapse-item title="查看操作记录" name="history">
  248. <p v-for="(event, index) in model.timeline" :key="index">{{ event.createdAt }} · {{ actions[event.action] ?? event.action }} · {{ event.detail }} · 操作人 {{ event.actor }}</p>
  249. </el-collapse-item>
  250. </el-collapse>
  251. <template #footer>
  252. <el-button :disabled="busy" @click="refresh">刷新审批与交接状态</el-button>
  253. <el-button v-if="model?.canPublish && dataset.enabled" :disabled="busy" @click="lifecycle('pause')">暂停接收</el-button>
  254. <el-button v-if="model?.canPublish && model.lifecycle === 'PAUSED'" :disabled="busy" @click="lifecycle('resume')">恢复当前版本</el-button>
  255. <el-button v-if="model?.canPublish && model.lifecycle === 'PAUSED'" :disabled="busy" @click="lifecycle('retire')">退役</el-button>
  256. </template>
  257. </el-dialog>
  258. </template>