diff --git a/src/views/inventorys/stocktaking.vue b/src/views/inventorys/stocktaking.vue index fb4f7e2..d70cbaf 100644 --- a/src/views/inventorys/stocktaking.vue +++ b/src/views/inventorys/stocktaking.vue @@ -431,7 +431,7 @@ 重置 - + @@ -802,6 +802,7 @@ import { export default { data() { return { + linkingMaterials: false, baseUrl: process.env.VUE_APP_BASE_API || window.location.origin + '/', planSearch: { keyword: '', @@ -2031,11 +2032,8 @@ export default { this.updateTableSelection() }) }, - async searchMaterials() { - this.materialModal.loading = true - this.materialModal.isInitialLoad = true // 重置标志 - try { - const res = await index({ + getMaterialSearchParams() { + return { material_infos_plan_id: this.materialModal.currentPlanId, page_size: this.materialModal.pageSize, page: this.materialModal.pageIndex, @@ -2063,7 +2061,13 @@ export default { value: this.materialModal.keyword } ] - }) + } + }, + async searchMaterials() { + this.materialModal.loading = true + this.materialModal.isInitialLoad = true + try { + const res = await index(this.getMaterialSearchParams()) if (res && res.list) { this.materialList = res.list.data.map(item => { @@ -2185,34 +2189,54 @@ export default { }, async linkAllMaterials() { const planId = this.materialModal.currentPlanId - if (!planId) { - this.$Message.error('未找到计划ID') + if (!planId || this.linkingMaterials) { + if (!planId) this.$Message.error('未找到计划ID') return } + this.linkingMaterials = true try { - const confirmed = await this.$confirm('是否关联所有物资?', '提示', { - confirmButtonText: '确定', - cancelButtonText: '取消', - type: 'warning' - }) - .then(() => true) - .catch(() => false) - - if (confirmed) { - const params = { - id: planId, - material_infos_plan_links_all: 1 + // 固定当前条件,遍历所有分页;使用库存ID稳定排序,避免同编码设备跨页遗漏。 + const params = { ...this.getMaterialSearchParams(), page_size: 200, sort_name: 'id' } + const inventoryIds = new Set() + let page = 1 + while (true) { + const res = await index({ ...params, page }) + const list = res && res.list + if (!list || !Array.isArray(list.data) || !Number.isFinite(Number(list.total))) { + throw new Error('物资查询返回异常,请重试') + } + if (list.data.length === 0 && inventoryIds.size < Number(list.total)) { + throw new Error('物资列表发生变化,请重新查询后再试') } + list.data.forEach(item => inventoryIds.add(String(item.id))) + if (page >= Number(list.last_page || Math.ceil(Number(list.total) / params.page_size))) break + page++ + } - await saveStocktakingPlan(params) - this.$Message.success('关联所有物资成功') - this.handleMaterialModalClose() - this.searchPlans() + if (!inventoryIds.size) { + this.$Message.warning('当前筛选条件下没有物资可关联') + return } + const confirmed = await this.$confirm( + `将关联当前筛选条件下全部 ${inventoryIds.size} 条库存记录(包含所有分页),保留已有的关联和盘点记录。是否继续?`, + '关联筛选结果', + { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' } + ).then(() => true).catch(() => false) + if (!confirmed) return + + await saveStocktakingPlan({ + id: planId, + material_infos_plan_links: Array.from(inventoryIds, inventorys_id => ({ inventorys_id })) + }) + this.$Message.success('筛选结果关联成功') + this.handleMaterialModalClose() + this.searchPlans() } catch (error) { - this.$Message.error('关联所有物资失败') - console.error('关联所有物资失败:', error) + this.$Message.error(error.message || '关联筛选结果失败') + console.error('关联筛选结果失败:', error) + } finally { + this.linkingMaterials = false } }, async handleMaterialSubmit() {