feat(web-admin): apply and save QC item templates in configure dialog.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zlwuandCursor committed 2026-08-17 12:47:01 +08:00
1 parent 6da51df503
commit ea5bddb1dc
1 file changed
+145 -2
@@ -53,6 +53,35 @@
</el-form-item>
</div>
<div
v-if="canUseTemplate || canSaveTemplate"
class="qc-batch-config__template-bar"
>
<el-select
v-if="canUseTemplate"
:model-value="selectedTemplateIdByIndex[index] || ''"
clearable
filterable
placeholder="选用检测项目模版"
style="min-width: 240px; flex: 1"
@change="(val) => onTemplateSelect(batch, index, val)"
>
<el-option
v-for="t in templateOptions"
:key="t.id"
:label="t.templateName"
:value="t.id"
/>
</el-select>
<el-button
v-if="canSaveTemplate"
class="btn-ghost"
@click="saveBatchAsTemplate(batch)"
>
<el-icon><DocumentAdd /></el-icon>存成新模版
</el-button>
</div>
<section class="qc-batch-config__selected">
<div class="qc-batch-config__selected-head">
<span class="qc-batch-config__selected-title">已选项目</span>
@@ -173,11 +202,18 @@
</template>
<script setup lang="ts">
import { ref, watch } from 'vue'
import { Delete, Plus } from '@element-plus/icons-vue'
import { ref, watch, computed, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Delete, Plus, DocumentAdd } from '@element-plus/icons-vue'
import type { QcInspectionBatchConfigureItem } from '@/utils/qc-inspection-batch'
import { createEmptyInspectionBatch } from '@/utils/qc-inspection-batch'
import { useUserStore } from '@/store/modules/user'
import { useMenuPerm } from '@/hooks/core/useMenuPerm'
import {
fetchQcInspectionItemTemplatesActive,
fetchCreateQcInspectionItemTemplate,
type QcInspectionItemTemplate
} from '@/api/qc-inspection-item-template'
type LabOption = { id: string; itemName: string; itemCode: string }
type ThirdOption = {
@@ -207,6 +243,101 @@
const activeTabKey = ref('')
const selectedCompanyByIndex = ref<Record<number, string>>({})
const { hasMenuPerm } = useMenuPerm()
const canUseTemplate = computed(
() =>
hasMenuPerm('biz:qcInspectionItemTemplate:list') ||
hasMenuPerm('biz:qcWorkOrder:list')
)
const canSaveTemplate = computed(() => hasMenuPerm('biz:qcInspectionItemTemplate:add'))
const templateOptions = ref<QcInspectionItemTemplate[]>([])
const selectedTemplateIdByIndex = ref<Record<number, string>>({})
async function reloadTemplates() {
if (!canUseTemplate.value && !canSaveTemplate.value) {
templateOptions.value = []
return
}
try {
templateOptions.value = (await fetchQcInspectionItemTemplatesActive()) || []
} catch {
templateOptions.value = []
}
}
async function onTemplateSelect(
batch: QcInspectionBatchConfigureItem,
index: number,
templateId: string | number | null | undefined
) {
const tid = String(templateId || '').trim()
if (!tid) {
selectedTemplateIdByIndex.value[index] = ''
return
}
const tpl = templateOptions.value.find((t) => t.id === tid)
if (!tpl) {
selectedTemplateIdByIndex.value[index] = ''
return
}
const labActive = new Set(props.labOptions.map((o) => o.id))
const thirdActive = new Set(props.thirdOptions.map((o) => o.id))
const keptLab = (tpl.labItemIds || []).filter((id) => labActive.has(id))
const keptThird = (tpl.thirdPartyItemIds || []).filter((id) => thirdActive.has(id))
const originalCount = (tpl.labItemIds || []).length + (tpl.thirdPartyItemIds || []).length
const keptCount = keptLab.length + keptThird.length
const skipped = originalCount - keptCount
if (!keptCount) {
ElMessage.warning('模版中的检测项目均已停用或删除,未覆盖当前批次')
selectedTemplateIdByIndex.value[index] = ''
return
}
const hasExisting = batch.labItemIds.length > 0 || batch.thirdPartyItemIds.length > 0
if (hasExisting) {
try {
await ElMessageBox.confirm('将覆盖当前批次已选检测项目,是否继续?', '提示', {
type: 'warning'
})
} catch {
selectedTemplateIdByIndex.value[index] = ''
return
}
}
batch.labItemIds = keptLab
batch.thirdPartyItemIds = keptThird
syncBatchFlags(batch)
selectedTemplateIdByIndex.value[index] = tid
if (skipped > 0) {
ElMessage.warning(`已跳过 ${skipped} 个停用或已删除的检测项目`)
}
}
async function saveBatchAsTemplate(batch: QcInspectionBatchConfigureItem) {
if (!batch.labItemIds.length && !batch.thirdPartyItemIds.length) {
ElMessage.warning('请先勾选检测项目')
return
}
try {
const { value } = await ElMessageBox.prompt('请输入模版名称', '存成新模版', {
confirmButtonText: '确定',
cancelButtonText: '取消',
inputPattern: /\S+/,
inputErrorMessage: '模版名称不能为空'
})
const name = String(value || '').trim()
if (!name) return
await fetchCreateQcInspectionItemTemplate({
templateName: name,
labItemIds: [...batch.labItemIds],
thirdPartyItemIds: [...batch.thirdPartyItemIds],
status: '1'
})
ElMessage.success('模版已保存')
await reloadTemplates()
} catch {
/* cancel or error toast by http */
}
}
function tabKey(batch: QcInspectionBatchConfigureItem, index: number) {
return batch.id || `new-${index}`
@@ -361,6 +492,10 @@
},
{ deep: true }
)
onMounted(() => {
void reloadTemplates()
})
</script>
<style scoped lang="scss">
@@ -393,6 +528,14 @@
margin-bottom: 16px;
}
.qc-batch-config__template-bar {
display: flex;
flex-wrap: wrap;
gap: 8px;
align-items: center;
margin-bottom: 12px;
}
.qc-config-split {
display: grid;
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);