<template>
|
<div class="app-container">
|
<PageHeader content="产品结构详情">
|
<template #right-button>
|
<el-button v-if="!dataValue.isEdit && !isOrderPage"
|
type="primary"
|
@click="dataValue.isEdit = true">编辑
|
</el-button>
|
<el-button v-if="dataValue.isEdit && !isOrderPage"
|
type="primary"
|
@click="cancelEdit">取消
|
</el-button>
|
<el-button v-if="!isOrderPage"
|
type="primary"
|
:loading="dataValue.loading"
|
@click="submit"
|
:disabled="!dataValue.isEdit">确认
|
</el-button>
|
</template>
|
</PageHeader>
|
<el-table :data="tableData"
|
border
|
:preserve-expanded-content="false"
|
:default-expand-all="true"
|
style="width: 100%">
|
<el-table-column type="expand">
|
<template #default="props">
|
<el-form ref="form" :model="dataValue">
|
<div class="tree-container">
|
<div class="tree-legend">
|
<el-tag type="" size="small" effect="dark">成品</el-tag>
|
<span style="margin:0 4px">← 最上层(产出物)</span>
|
<el-divider direction="vertical" />
|
<el-tag type="warning" size="small" effect="dark">半成品</el-tag>
|
<span style="margin:0 4px">(可继续展开)</span>
|
<el-divider direction="vertical" />
|
<span style="margin:0 4px">最下层(投入物)→</span>
|
<el-tag type="success" size="small" effect="dark">原料</el-tag>
|
</div>
|
|
<div v-if="dataValue.dataList.length === 0 && dataValue.isEdit" class="empty-hint">
|
请点击下方按钮添加成品
|
</div>
|
|
<MaterialCard
|
v-for="(item, index) in dataValue.dataList"
|
:key="item.tempId"
|
:row="item"
|
:depth="0"
|
:editable="dataValue.isEdit"
|
:process-options="dataValue.processOptions"
|
@remove="(id: string) => removeItem(id)"
|
@add="(id: string, nodeType: string) => addChildItem(id, nodeType)"
|
@select-product="(tempId: string, _data: any) => { dataValue.currentRowName = tempId; dataValue.showProductDialog = true }"
|
@process-change="(row: any, v: any) => handleProcessChange(row, v)"
|
@quantity-change="handleUnitQuantityChange"
|
/>
|
|
<el-button v-if="dataValue.isEdit"
|
type="primary"
|
plain
|
style="margin-top:12px"
|
@click="addRootItem">
|
+ 添加成品
|
</el-button>
|
</div>
|
</el-form>
|
</template>
|
</el-table-column>
|
<el-table-column label="BOM编号"
|
prop="bomNo" />
|
<el-table-column label="产品名称"
|
prop="productName" />
|
<el-table-column label="规格型号"
|
prop="model" />
|
</el-table>
|
<product-select-dialog v-if="dataValue.showProductDialog"
|
v-model:model-value="dataValue.showProductDialog"
|
:single="true"
|
@confirm="handleProduct" />
|
</div>
|
</template>
|
|
<script setup lang="ts">
|
import {
|
computed,
|
defineAsyncComponent,
|
defineComponent,
|
onMounted,
|
reactive,
|
ref,
|
} from "vue";
|
import {
|
queryList,
|
addBomDetail,
|
} from "@/api/productionManagement/productStructure.js";
|
import { listProcessBom } from "@/api/productionManagement/productionOrder.js";
|
import { list } from "@/api/productionManagement/productionProcess";
|
import { ElMessage } from "element-plus";
|
import { useRoute, useRouter } from "vue-router";
|
|
defineComponent({
|
name: "StructureEdit",
|
});
|
|
const ProductSelectDialog = defineAsyncComponent(
|
() => import("@/views/basicData/product/ProductSelectDialog.vue")
|
);
|
import MaterialCard from "./MaterialCard.vue";
|
const emit = defineEmits(["update:router"]);
|
const form = ref();
|
|
const route = useRoute();
|
const router = useRouter();
|
const routeId = computed({
|
get() {
|
return route.query.id;
|
},
|
|
set(val) {
|
emit("update:router", val);
|
},
|
});
|
|
// 从路由参数获取产品信息
|
const routeBomNo = computed(() => route.query.bomNo || "");
|
const routeProductName = computed(() => route.query.productName || "");
|
const routeProductModelName = computed(
|
() => route.query.productModelName || ""
|
);
|
const routeOrderId = computed(() => route.query.orderId);
|
const pageType = computed(() => route.query.type);
|
const isOrderPage = computed(
|
() => pageType.value === "order" && routeOrderId.value
|
);
|
|
const dataValue = reactive({
|
dataList: [],
|
productOptions: [],
|
processOptions: [],
|
showProductDialog: false,
|
currentRowIndex: null,
|
currentRowName: null,
|
loading: false,
|
isEdit: false,
|
});
|
|
const normalizeListData = (source: any) => {
|
if (Array.isArray(source)) {
|
return source;
|
}
|
if (Array.isArray(source?.records)) {
|
return source.records;
|
}
|
return [];
|
};
|
|
const getProcessOptionById = (id: any) => {
|
if (id === undefined || id === null || id === "") {
|
return null;
|
}
|
return (
|
normalizeListData(dataValue.processOptions).find(
|
option => String(option.id) === String(id)
|
) || null
|
);
|
};
|
|
const syncProcessOperationFields = (item: any) => {
|
const processId = item.processId ?? item.operationId ?? "";
|
if (!processId) {
|
item.processId = "";
|
item.operationId = "";
|
item.processName = "";
|
item.operationName = "";
|
return;
|
}
|
|
const option = getProcessOptionById(processId);
|
const processName =
|
option?.name || item.processName || item.operationName || "";
|
|
item.processId = processId;
|
item.operationId = processId;
|
item.processName = processName;
|
item.operationName = processName;
|
};
|
|
const normalizeTreeData = (items: any[], depth: number = 0) => {
|
items.forEach((item: any) => {
|
item.tempId = item.tempId || item.id || `${Date.now()}_${Math.random()}`;
|
syncProcessOperationFields(item);
|
if (depth > 0 && !item.nodeType) {
|
item.nodeType = Array.isArray(item.children) && item.children.length > 0
|
? 'semiFinished'
|
: 'rawMaterial';
|
}
|
if (Array.isArray(item.children) && item.children.length > 0) {
|
normalizeTreeData(item.children, depth + 1);
|
}
|
});
|
};
|
|
const toQuantityNumber = (value: any) => {
|
const numberValue = Number(value);
|
if (!Number.isFinite(numberValue)) {
|
return 0;
|
}
|
return Number(numberValue.toFixed(2));
|
};
|
|
const syncDemandedQuantityTree = (
|
items: any[],
|
parentDemandedQuantity: number | null = null
|
) => {
|
items.forEach((item: any) => {
|
if (parentDemandedQuantity !== null) {
|
item.demandedQuantity = toQuantityNumber(
|
parentDemandedQuantity * toQuantityNumber(item.unitQuantity)
|
);
|
}
|
|
if (Array.isArray(item.children) && item.children.length > 0) {
|
syncDemandedQuantityTree(
|
item.children,
|
toQuantityNumber(item.demandedQuantity)
|
);
|
}
|
});
|
};
|
|
const recalculateDemandedQuantities = () => {
|
if (!isOrderPage.value) {
|
return;
|
}
|
|
syncDemandedQuantityTree(dataValue.dataList);
|
};
|
|
const buildSubmitTree = (items: any[]) => {
|
return items.map((item: any) => {
|
const current = { ...item };
|
syncProcessOperationFields(current);
|
current.children = Array.isArray(current.children)
|
? buildSubmitTree(current.children)
|
: [];
|
return current;
|
});
|
};
|
|
const findSiblings = (items: any[], tempId: string): any[] | null => {
|
if (!items || items.length === 0) return null;
|
// 检查当前层级
|
if (items.some(item => item.tempId === tempId)) {
|
return items;
|
}
|
// 递归查找子级
|
for (const item of items) {
|
if (item.children && item.children.length > 0) {
|
const result = findSiblings(item.children, tempId);
|
if (result) return result;
|
}
|
}
|
return null;
|
};
|
|
const handleProcessChange = (row: any, value: any) => {
|
row.processId = value || "";
|
syncProcessOperationFields(row);
|
|
// 检查同一层级是否已经有其他不同的工序被选中
|
const siblings = findSiblings(dataValue.dataList, row.tempId);
|
if (siblings && value) {
|
const hasDifferentProcess = siblings.some(sibling => {
|
return sibling.tempId !== row.tempId && sibling.processId && sibling.processId !== value;
|
});
|
if (hasDifferentProcess) {
|
ElMessage.warning("同一层级已存在不同的工序,请先统一工序后再进行修改");
|
}
|
}
|
};
|
|
const handleUnitQuantityChange = () => {
|
recalculateDemandedQuantities();
|
};
|
|
const tableData = reactive([
|
{
|
productName: "",
|
model: "",
|
bomNo: "",
|
},
|
]);
|
|
const openDialog = (tempId: any) => {
|
console.log(tempId, "tempId");
|
dataValue.currentRowName = tempId;
|
dataValue.showProductDialog = true;
|
};
|
|
const fetchData = async () => {
|
if (isOrderPage.value) {
|
// 订单情况:使用订单的产品结构接口
|
const { data } = await listProcessBom({ orderId: routeOrderId.value });
|
dataValue.dataList = (data as any) || [];
|
normalizeTreeData(dataValue.dataList);
|
recalculateDemandedQuantities();
|
} else {
|
// 非订单情况:使用原来的接口
|
const { data } = await queryList(routeId.value);
|
dataValue.dataList = (data as any) || [];
|
console.log(dataValue);
|
normalizeTreeData(dataValue.dataList);
|
console.log(dataValue.dataList, "dataValue.dataList");
|
}
|
};
|
|
const fetchProcessOptions = async () => {
|
const { data } = await list({});
|
console.log(data, "dataValue.dataList");
|
dataValue.processOptions = normalizeListData(data);
|
};
|
|
const handleProduct = (row: any) => {
|
if (!Array.isArray(row) || row.length === 0) {
|
ElMessage.warning("请选择一个产品");
|
return;
|
}
|
// 只允许一个:如果上游返回了多个,默认使用最后一次选择并覆盖当前值
|
const productData = row[row.length - 1];
|
|
// 最外层组件中,与当前产品相同的产品只能有一个
|
const isTopLevel = dataValue.dataList.some(
|
item => (item as any).tempId === dataValue.currentRowName
|
);
|
if (isTopLevel) {
|
if (
|
productData.productName === tableData[0].productName &&
|
productData.model === tableData[0].model
|
) {
|
// 查找是否已经有其他顶层行已经是这个产品
|
const hasOther = dataValue.dataList.some(
|
item =>
|
(item as any).tempId !== dataValue.currentRowName &&
|
(item as any).productName === tableData[0].productName &&
|
(item as any).model === tableData[0].model
|
);
|
if (hasOther) {
|
ElMessage.warning("最外层和当前产品一样的一级只能有一个");
|
return;
|
}
|
}
|
}
|
// dataValue.dataList[dataValue.currentRowIndex].productName =
|
// row[0].productName;
|
// dataValue.dataList[dataValue.currentRowIndex].model = row[0].model;
|
// dataValue.dataList[dataValue.currentRowIndex].productModelId = row[0].id;
|
// dataValue.dataList[dataValue.currentRowIndex].unit = row[0].unit || "";
|
dataValue.dataList.map(item => {
|
if (item.tempId === dataValue.currentRowName) {
|
item.productName = productData.productName;
|
item.model = productData.model;
|
item.productModelId = productData.id;
|
item.unit = productData.unit || "";
|
return;
|
}
|
childItem(item, dataValue.currentRowName, productData);
|
});
|
dataValue.showProductDialog = false;
|
};
|
const childItem = (item: any, tempId: any, productData: any) => {
|
if (item.tempId === tempId) {
|
item.productName = productData.productName;
|
item.model = productData.model;
|
item.productModelId = productData.id;
|
item.unit = productData.unit || "";
|
return true;
|
}
|
if (item.children && item.children.length > 0) {
|
for (let child of item.children) {
|
if (childItem(child, tempId, productData)) {
|
return true;
|
}
|
}
|
}
|
return false;
|
};
|
|
// 递归校验所有层级的表单数据
|
const validateAll = () => {
|
let isValid = true;
|
|
// 校验一组兄弟节点的工序是否都相同
|
const checkProcessUniqueness = (items: any[]) => {
|
if (!items || items.length === 0 || !isValid) return;
|
|
// 获取第一个非空的工序ID作为参考
|
const firstProcessId = items.find(item => item.processId)?.processId;
|
|
// 如果有工序ID,检查所有项是否都使用相同的工序
|
if (firstProcessId) {
|
for (const item of items) {
|
if (item.processId && item.processId !== firstProcessId) {
|
const option1 = getProcessOptionById(firstProcessId);
|
const option2 = getProcessOptionById(item.processId);
|
const processName1 = option1?.name || "未知工序";
|
const processName2 = option2?.name || "未知工序";
|
ElMessage.error(
|
`当前层级下工序不一致,请使用相同的工序。存在「${processName1}」和「${processName2}」`
|
);
|
isValid = false;
|
return;
|
}
|
}
|
}
|
|
// 递归校验子级的兄弟节点
|
for (const item of items) {
|
if (item.children && item.children.length > 0) {
|
checkProcessUniqueness(item.children);
|
}
|
}
|
};
|
|
// 校验函数
|
const validateItem = (item: any, isTopLevel = false) => {
|
if (!isValid) return;
|
// 校验当前项的必填字段
|
if (!item.model) {
|
ElMessage.error("请选择规格");
|
isValid = false;
|
return;
|
}
|
if (!isTopLevel && !item.processId) {
|
ElMessage.error("请选择消耗工序");
|
isValid = false;
|
return;
|
}
|
if (!item.unitQuantity) {
|
ElMessage.error("请输入单位产出所需数量");
|
isValid = false;
|
return;
|
}
|
if (isOrderPage.value && !item.demandedQuantity) {
|
ElMessage.error("请输入需求总量");
|
isValid = false;
|
return;
|
}
|
// if (!item.unit) {
|
// ElMessage.error("请输入单位");
|
// isValid = false;
|
// return;
|
// }
|
|
// 递归校验子项字段
|
if (item.children && item.children.length > 0) {
|
item.children.forEach(child => {
|
validateItem(child, false);
|
});
|
}
|
};
|
|
// 1. 首先校验同一父级下的同层消耗工序是否唯一
|
checkProcessUniqueness(dataValue.dataList);
|
if (!isValid) return false;
|
|
// 2. 然后遍历校验所有顶层项的字段必填情况
|
dataValue.dataList.forEach(item => {
|
validateItem(item, true);
|
});
|
|
return isValid;
|
};
|
|
const submit = () => {
|
dataValue.loading = true;
|
normalizeTreeData(dataValue.dataList);
|
recalculateDemandedQuantities();
|
|
// 先进行表单校验
|
const valid = validateAll();
|
console.log(dataValue.dataList, "dataValue.dataList");
|
if (valid) {
|
addBomDetail({
|
bomId: routeId.value,
|
children: buildSubmitTree(dataValue.dataList || []),
|
})
|
.then(res => {
|
router.go(-1);
|
ElMessage.success("保存成功");
|
dataValue.loading = false;
|
})
|
.catch(() => {
|
dataValue.loading = false;
|
});
|
} else {
|
dataValue.loading = false;
|
}
|
};
|
|
const removeItem = (tempId: string) => {
|
const topIndex = dataValue.dataList.findIndex(item => item.tempId === tempId);
|
if (topIndex !== -1) {
|
dataValue.dataList.splice(topIndex, 1);
|
return;
|
}
|
|
const delchildItem = (items: any[], tempId: any) => {
|
for (let i = 0; i < items.length; i++) {
|
const item = items[i];
|
if (item.tempId === tempId) {
|
items.splice(i, 1);
|
return true;
|
}
|
if (item.children && item.children.length > 0) {
|
if (delchildItem(item.children, tempId)) {
|
return true;
|
}
|
}
|
}
|
return false;
|
};
|
|
dataValue.dataList.forEach(item => {
|
if (item.children && item.children.length > 0) {
|
delchildItem(item.children, tempId);
|
}
|
});
|
};
|
|
const newChildNode = (parentItem: any, nodeType: string = 'rawMaterial') => ({
|
parentId: parentItem.id || "",
|
parentTempId: parentItem.tempId || "",
|
productName: "",
|
productId: "",
|
model: undefined,
|
productModelId: undefined,
|
processId: "",
|
processName: "",
|
operationId: "",
|
operationName: "",
|
unitQuantity: 1,
|
demandedQuantity: 0,
|
unit: "",
|
nodeType,
|
children: [],
|
tempId: new Date().getTime(),
|
});
|
|
const addRootItem = () => {
|
dataValue.dataList.push(newChildNode({ id: "", tempId: "" }));
|
};
|
|
const addChildItem = (parentTempId: string, nodeType: string = 'rawMaterial') => {
|
const addToItem = (items: any[]): boolean => {
|
for (const item of items) {
|
if (item.tempId === parentTempId) {
|
if (!item.children) item.children = [];
|
item.children.push(newChildNode(item, nodeType));
|
recalculateDemandedQuantities();
|
return true;
|
}
|
if (item.children?.length > 0) {
|
if (addToItem(item.children)) return true;
|
}
|
}
|
return false;
|
};
|
addToItem(dataValue.dataList);
|
};
|
|
const getPropPath = (row, field) => {
|
// 为每个row生成唯一的路径
|
// 使用row.id或索引作为唯一标识
|
let path = "dataList";
|
|
// 简单实现:使用row的id或一个唯一标识
|
const uniqueId = row.id || Math.floor(Math.random() * 10000);
|
path += `.${uniqueId}`;
|
|
return path + `.${field}`;
|
};
|
|
const cancelEdit = () => {
|
dataValue.isEdit = false;
|
// dataValue.dataList = dataValue.dataList.filter(item => item.id !== undefined);
|
fetchData();
|
};
|
|
onMounted(async () => {
|
// 从路由参数回显数据
|
tableData[0].productName = routeProductName.value as string;
|
tableData[0].model = routeProductModelName.value as string;
|
tableData[0].bomNo = routeBomNo.value as string;
|
|
// 订单情况下禁用编辑
|
if (isOrderPage.value) {
|
dataValue.isEdit = false;
|
}
|
|
// 先加载工序选项,再加载数据,确保el-select能够正确回显
|
await fetchProcessOptions();
|
await fetchData();
|
});
|
</script>
|
|
<style scoped>
|
.tree-container {
|
padding: 8px 0;
|
}
|
.tree-legend {
|
display: flex;
|
align-items: center;
|
margin-bottom: 12px;
|
padding: 8px 12px;
|
background: #f5f7fa;
|
border-radius: 6px;
|
font-size: 13px;
|
color: #606266;
|
}
|
.empty-hint {
|
text-align: center;
|
color: #909399;
|
padding: 24px 0;
|
}
|
</style>
|