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

132 lines
3.5 KiB
JavaScript
Raw Normal View History

2025-07-19 17:29:15 +08:00
import { listCategory } from '@/api/wms/category';
import { listProductWithBom } from '@/api/wms/product';
import { listRawMaterialWithBom } from '@/api/wms/rawMaterial';
2025-07-31 18:07:51 +08:00
import { listBomItem } from '@/api/wms/bomItem';
2025-07-19 17:29:15 +08:00
2025-08-02 13:38:04 +08:00
// 目前存在一个问题,当新增或删除,修改分类、产品、物料时,需要刷新整个页面,才能看到最新的数据
// 需要优化当新增或删除修改分类、产品、物料时只刷新相关的数据而不是整个页面修改和删除可以解决新增由于没有返回id所以需要重新获取整个列表
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 }) {
if (Object.keys(state.productMap).length > 0) {
return Promise.resolve(state.productMap);
}
return listProductWithBom({ pageNum: 1, pageSize: 10000 }).then(res => {
2025-07-28 18:25:02 +08:00
const map = {};
res.rows.forEach(item => {
map[item.productId] = item;
});
commit('SET_PRODUCT_MAP', map);
2025-07-30 10:53:06 +08:00
commit('SET_PRODUCT_LIST', res.rows || []);
2025-07-28 18:25:02 +08:00
return map;
});
},
getRawMaterialMap({ state, commit }) {
if (Object.keys(state.rawMaterialMap).length > 0) {
return Promise.resolve(state.rawMaterialMap);
}
return listRawMaterialWithBom({ pageNum: 1, pageSize: 10000 }).then(res => {
2025-07-28 18:25:02 +08:00
const map = {};
res.rows.forEach(item => {
map[item.rawMaterialId] = item;
});
commit('SET_RAW_MATERIAL_MAP', map);
2025-07-30 10:53:06 +08:00
commit('SET_RAW_MATERIAL_LIST', res.rows || []);
2025-07-28 18:25:02 +08:00
return map;
});
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: 10000 }).then(res => {
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
};