优化商品库存耗材

This commit is contained in:
gyq
2026-04-14 18:28:54 +08:00
parent 6a20930a7d
commit c7f22e193a
15 changed files with 967 additions and 528 deletions

View File

@@ -3,6 +3,7 @@ import WebSocketManager, { type ApifoxModel, msgType } from "@/utils/websocket";
import orderApi from "@/api/order/order";
import { useUserStoreHook } from "@/store/modules/user";
import productApi from "@/api/product/index";
import categoryApi from "@/api/product/productclassification";
import shopUserApi from '@/api/account/shopUser'
import limitTimeDiscountApi from '@/api/market/limitTimeDiscount.js'
import { BigNumber } from "bignumber.js";
@@ -82,8 +83,6 @@ export const useCartsStore = defineStore("carts", () => {
default: productType = GoodsType.NORMAL;
}
return {
...item,
id: item.id,
@@ -350,11 +349,61 @@ export const useCartsStore = defineStore("carts", () => {
}
});
const consList = await productApi.consStock({ shopId: localStorage.getItem("shopId") })
goods.value = addGoodsSoldOutStatus(goods.value, consList)
console.log('代客下单页面商品缓存.goods.value', goods.value);
setGoodsMap(goods.value);
}
/**
* 给商品列表批量添加 isSoldOut 售罄状态字段
* @param {Array} goodsList - 商品列表 [ { consList, isAutoSoldStock } ]
* @param {Array} consStockList - 真实耗材库存列表 [ { consId, stockNumber } ]
* @returns 带 isSoldOut 字段的新商品列表
*/
function addGoodsSoldOutStatus(goodsList, consStockList) {
// console.log('addGoodsSoldOutStatus.goodsList', goodsList);
// console.log('addGoodsSoldOutStatus.consStockList', consStockList);
// 耗材ID映射真实库存保留
const consMap = _.keyBy(consStockList, item => String(item.consId));
return _.map(goodsList, goods => {
let isSoldOut = false;
// 开启自动售罄才判断
if (goods.isAutoSoldStock === 1 || goods.isAutoSoldStock === true) {
const goodsConsList = goods.consList || [];
// 无耗材 → 不售罄
if (goodsConsList.length === 0) {
isSoldOut = false;
} else {
// 核心:只要有一个耗材 真实库存 < 商品需要量 → 售罄
isSoldOut = _.some(goodsConsList, consItem => {
// 商品绑定的耗材ID对应真实库存ID
const consId = String(consItem.consInfoId);
// 商品需要消耗的数量(你的需求量)
const needStock = consItem.surplusStock || 0;
// 起售数量
const suitNum = goods.type == 'single' ? goods.skuList[0].suitNum : 1;
// 真实库存
const realStock = _.get(consMap, [consId, 'stockNumber'], 0);
// 真实库存 < 需要量 * 起售数量 → 不足 → 售罄
return realStock < needStock * suitNum;
});
}
}
return { ...goods, isSoldOut };
});
}
function setGoodsMap(goods: any[]) {
for (let item of goods) {
goodsMap[item.id] = item;
@@ -660,7 +709,9 @@ export const useCartsStore = defineStore("carts", () => {
sendMessage('add', { ...basic_msg, ...data });
}
// 添加购物车/编辑购物车
function add(data: any) {
// console.log('添加购物车/编辑购物车===', data);
goods.value.map(item => {
if (item.id == data.product_id) {
data.is_time_discount = item.is_time_discount ? 1 : 0
@@ -675,8 +726,29 @@ export const useCartsStore = defineStore("carts", () => {
console.log('carts.add===', data);
if (hasCart) {
// console.log('编辑', msg);
if (hasCart.number * 1 + msg.number * 1 == 2) {
ElMessage({
type: 'warning',
message: '购物车已有该商品,请确认是否重复',
duration: 4000
})
}
update({ ...hasCart, ...msg, number: hasCart.number * 1 + msg.number * 1 });
} else {
// console.log('添加', msg);
let arr = _.flatten(_.values(oldOrder.value.detailMap))
// console.log('添加.arr===', arr);
const isExist = _.some(arr, item => Number(item.productId) === msg.product_id)
// console.log('添加.isExist===', isExist);
if (msg.number == 1 && isExist) {
ElMessage({
type: 'warning',
message: '该商品已下单过,请确认是否重复',
duration: 4000
})
}
sendMessage('add', msg);
}
}
@@ -906,7 +978,7 @@ export const useCartsStore = defineStore("carts", () => {
console.log("收到消息:", msg);
if (!msg.status) {
if (msg.hasOwnProperty('status') && msg.status !== 1 && msg.operate_type !== 'bulk_edit') {
return ElMessage.error(msg.message || '操作失败');
return ElMessage.error(msg.msg || '操作失败');
}
}
if (msg?.data) {
@@ -1068,8 +1140,6 @@ export const useCartsStore = defineStore("carts", () => {
newUserDiscount.value = {}
}
return {
disconnect,
dinnerType,