|
@@ -51,6 +51,14 @@ export class CartService extends BaseService<modelType> {
|
|
|
async specialCreate(data: CDTO_Cart): Promise<object> {
|
|
|
const { user, supplier_id, goods, spec, num, money } = data;
|
|
|
let res;
|
|
|
+ // 查询库存 如果没有库存 不允许添加购物车
|
|
|
+ res = await this.specModel.findById(spec).lean();
|
|
|
+ if (res.num <= 0) {
|
|
|
+ throw new ServiceError(
|
|
|
+ '商品库存不足 无法假如购物车',
|
|
|
+ FrameworkErrorEnum.NOT_FOUND_DATA
|
|
|
+ );
|
|
|
+ }
|
|
|
// 查询是否数据库中有相同数据
|
|
|
const arr: any = await this.model
|
|
|
.findOne({ user, supplier_id, goods, spec })
|
|
@@ -168,6 +176,7 @@ export class CartService extends BaseService<modelType> {
|
|
|
};
|
|
|
await this.orderDetailModel.create(info);
|
|
|
}
|
|
|
+ await this.checkGoodsNum(order);
|
|
|
} else {
|
|
|
throw new ServiceError(
|
|
|
'未找到该用户',
|
|
@@ -196,4 +205,28 @@ export class CartService extends BaseService<modelType> {
|
|
|
}
|
|
|
return 'ok';
|
|
|
}
|
|
|
+
|
|
|
+ // 计算销量和库存量
|
|
|
+ async checkGoodsNum(data) {
|
|
|
+ let res;
|
|
|
+ if (data) {
|
|
|
+ for (const val of data.goods) {
|
|
|
+ res = await this.specModel.findById(val.spec).lean();
|
|
|
+ if (res) {
|
|
|
+ const num = res.num - val.num;
|
|
|
+ await this.specModel.updateOne({ _id: res._id }, { num }).lean();
|
|
|
+ }
|
|
|
+ res = await this.orderDetailModel.find({ good: val.goods }).lean();
|
|
|
+ if (res) {
|
|
|
+ let num = 0;
|
|
|
+ for (const as of res) {
|
|
|
+ num = num + as.num;
|
|
|
+ }
|
|
|
+ await this.goodModel
|
|
|
+ .updateOne({ _id: val.goods }, { sell_num: num })
|
|
|
+ .lean();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|