DatasetReleasePanel.vue 13 KB

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