Files
klp-oa/klp-ui/src/store/modules/category.js

206 lines
5.7 KiB
JavaScript
Raw Normal View History

// import { listCategory } from '@/api/wms/category';
// import { listBomItem } from '@/api/wms/bomItem';
// import { listProduct } from '@/api/wms/product';
// import { listRawMaterial } from '@/api/wms/rawMaterial';
import veilReq from '@/utils/veilReq';
const listRawMaterial = (params) => veilReq({
url: '/wms/rawMaterial/list',
method: 'get',
params
});
const listProduct = (params) => veilReq({
url: '/wms/product/list',
method: 'get',
params
});
2025-08-02 13:38:04 +08:00
2025-07-19 17:29:15 +08:00
const state = {
2025-07-28 18:25:02 +08:00
categoryList: [],
productMap: {},
2025-07-30 10:53:06 +08:00
rawMaterialMap: {},
productList: [],
2025-07-31 18:07:51 +08:00
rawMaterialList: [],
bomMap: {}
2025-07-19 17:29:15 +08:00
};
const mutations = {
SET_CATEGORY_LIST(state, list) {
state.categoryList = list;
2025-07-28 18:25:02 +08:00
},
2025-08-02 13:38:04 +08:00
2025-07-28 18:25:02 +08:00
SET_PRODUCT_MAP(state, map) {
state.productMap = map;
},
SET_RAW_MATERIAL_MAP(state, map) {
state.rawMaterialMap = map;
2025-07-30 10:53:06 +08:00
},
SET_PRODUCT_LIST(state, list) {
state.productList = list;
},
SET_RAW_MATERIAL_LIST(state, list) {
state.rawMaterialList = list;
2025-07-31 18:07:51 +08:00
},
SET_BOM_MAP(state, map) {
state.bomMap = map;
2025-07-19 17:29:15 +08:00
}
};
const actions = {
getCategoryList({ state, commit }) {
if (state.categoryList.length > 0) {
return Promise.resolve(state.categoryList);
}
2025-07-31 18:07:51 +08:00
return listCategory({ pageNum: 1, pageSize: 10000 }).then(res => {
2025-07-19 17:29:15 +08:00
commit('SET_CATEGORY_LIST', res.rows || []);
return res.rows || [];
});
2025-07-28 18:25:02 +08:00
},
getProductMap({ state, commit }) {
// 若已有缓存数据,直接返回
2025-07-28 18:25:02 +08:00
if (Object.keys(state.productMap).length > 0) {
return Promise.resolve(state.productMap);
}
const pageSize = 2000; // 每次获取2000条
const allRows = []; // 存储所有批次的列表数据
const productMap = {}; // 最终的产品映射表
// 异步处理分批次获取逻辑
const fetchAllProducts = async () => {
// 1. 获取第一页数据拿到总条数total
let currentPage = 1;
const firstRes = await listProduct({ pageNum: currentPage, pageSize });
const total = firstRes.total || 0;
const firstRows = firstRes.rows || [];
// 处理第一页数据
allRows.push(...firstRows);
firstRows.forEach(item => {
productMap[item.productId.toString()] = item;
2025-07-28 18:25:02 +08:00
});
// 2. 计算总页数,循环获取剩余页面数据
const totalPages = Math.ceil(total / pageSize);
for (currentPage = 2; currentPage <= totalPages; currentPage++) {
const res = await listProduct({ pageNum: currentPage, pageSize });
const rows = res.rows || [];
// 合并当前页数据到总列表和映射表
allRows.push(...rows);
rows.forEach(item => {
productMap[item.productId.toString()] = item;
});
}
// 3. 所有数据获取完成后,更新状态
commit('SET_PRODUCT_MAP', productMap);
commit('SET_PRODUCT_LIST', allRows);
return productMap;
};
// 返回Promise确保外部可通过.then获取结果
return fetchAllProducts();
2025-07-28 18:25:02 +08:00
},
getRawMaterialMap({ state, commit }) {
// 若已有缓存数据,直接返回
2025-07-28 18:25:02 +08:00
if (Object.keys(state.rawMaterialMap).length > 0) {
return Promise.resolve(state.rawMaterialMap);
}
const pageSize = 2000; // 每次获取2000条
const allRows = []; // 存储所有批次的原材料列表
const rawMaterialMap = {}; // 最终的原材料映射表
// 异步处理分批次获取逻辑
const fetchAllRawMaterials = async () => {
// 1. 获取第一页数据拿到总条数total
let currentPage = 1;
const firstRes = await listRawMaterial({ pageNum: currentPage, pageSize });
const total = firstRes.total || 0;
const firstRows = firstRes.rows || [];
// 处理第一页数据
allRows.push(...firstRows);
firstRows.forEach(item => {
rawMaterialMap[item.rawMaterialId.toString()] = item;
2025-07-28 18:25:02 +08:00
});
// 2. 计算总页数,循环获取剩余页面数据
const totalPages = Math.ceil(total / pageSize);
for (currentPage = 2; currentPage <= totalPages; currentPage++) {
const res = await listRawMaterial({ pageNum: currentPage, pageSize });
const rows = res.rows || [];
// 合并当前页数据到总列表和映射表
allRows.push(...rows);
rows.forEach(item => {
rawMaterialMap[item.rawMaterialId.toString()] = item;
});
}
// 3. 所有数据获取完成后,更新状态
commit('SET_RAW_MATERIAL_MAP', rawMaterialMap);
commit('SET_RAW_MATERIAL_LIST', allRows);
return rawMaterialMap;
};
// 返回Promise确保外部可通过.then获取结果
return fetchAllRawMaterials();
2025-07-31 18:07:51 +08:00
},
getBomMap({ state, commit }) {
if (Object.keys(state.bomMap).length > 0) {
return Promise.resolve(state.bomMap);
}
return listBomItem({ pageNum: 1, pageSize: 100000 }).then(res => {
2025-07-31 18:07:51 +08:00
console.log('bomItem', res)
const map = {};
res.rows.forEach(item => {
if (!map[item.bomId]) {
map[item.bomId] = [];
}
map[item.bomId].push(item);
});
commit('SET_BOM_MAP', map);
return map;
})
2025-07-19 17:29:15 +08:00
}
};
2025-08-02 13:38:04 +08:00
export function findItemWithBom(itemType, itemId) {
if (!itemType || !itemId) {
return null;
}
let map = {}
if (itemType === 'product') {
map = state.productMap;
} else if (itemType === 'raw_material') {
map = state.rawMaterialMap;
} else {
return null;
}
const item = map[itemId];
if (!item) {
return null;
}
const bomId = item.bomId
if (!bomId) {
return null;
}
const bomItems = state.bomMap[bomId];
return {
...item,
boms: bomItems || [],
itemName: itemType === 'product' ? item.productName : item.rawMaterialName,
itemType,
};
}
2025-07-19 17:29:15 +08:00
export default {
namespaced: true,
state,
mutations,
2025-07-29 15:00:15 +08:00
actions,
2025-07-19 17:29:15 +08:00
};