|
@@ -56,7 +56,6 @@ class OrderService extends CrudService {
|
|
const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(i, false);
|
|
const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(i, false);
|
|
if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
|
|
if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|
|
const orderData = {};
|
|
const orderData = {};
|
|
// 数据做快照处理
|
|
// 数据做快照处理
|
|
@@ -104,7 +103,7 @@ class OrderService extends CrudService {
|
|
if (coupon.length > 0) await this.ctx.service.user.userCoupon.useCoupon(coupon, this.tran);
|
|
if (coupon.length > 0) await this.ctx.service.user.userCoupon.useCoupon(coupon, this.tran);
|
|
await this.tran.run();
|
|
await this.tran.run();
|
|
// 创建定时任务(mq死信机制任务)
|
|
// 创建定时任务(mq死信机制任务)
|
|
- await this.toMakeTask(order_id);
|
|
|
|
|
|
+ // await this.toMakeTask(order_id);
|
|
return order_id;
|
|
return order_id;
|
|
} catch (error) {
|
|
} catch (error) {
|
|
await this.tran.rollback();
|
|
await this.tran.rollback();
|
|
@@ -178,36 +177,40 @@ class OrderService extends CrudService {
|
|
*/
|
|
*/
|
|
async dealGoodsNum(list) {
|
|
async dealGoodsNum(list) {
|
|
for (const i of list) {
|
|
for (const i of list) {
|
|
- for (const g of i.goods) {
|
|
|
|
- const { _id, buy_num, cart_id, is_set = '1', set_id, goods } = g;
|
|
|
|
- if (is_set === '1') {
|
|
|
|
|
|
+ const { is_set = '1' } = i;
|
|
|
|
+ if (is_set === '1') {
|
|
|
|
+ for (const g of i.goods) {
|
|
|
|
+ const { _id, buy_num, cart_id } = g;
|
|
const goodsSpec = await this.goodsSpecModel.findById(_id);
|
|
const goodsSpec = await this.goodsSpecModel.findById(_id);
|
|
const newNum = this.ctx.minus(goodsSpec.num, buy_num);
|
|
const newNum = this.ctx.minus(goodsSpec.num, buy_num);
|
|
this.tran.update('GoodsSpec', _id, { num: newNum });
|
|
this.tran.update('GoodsSpec', _id, { num: newNum });
|
|
|
|
+ if (cart_id) {
|
|
|
|
+ const num = await this.cartModel.count({ _id: cart_id });
|
|
|
|
+ if (num > 0) { this.tran.remove('Cart', cart_id); }
|
|
|
|
+ }
|
|
// 活动相关:
|
|
// 活动相关:
|
|
// 买赠:不一定非要处理赠品,赠品可能不在库中;
|
|
// 买赠:不一定非要处理赠品,赠品可能不在库中;
|
|
// 特价,满减/折:没关系
|
|
// 特价,满减/折:没关系
|
|
// 加价购,作为另一个商品出现,一样会减少库存,不需要处理
|
|
// 加价购,作为另一个商品出现,一样会减少库存,不需要处理
|
|
// 先不处理活动库存问题.先有逻辑可以做到
|
|
// 先不处理活动库存问题.先有逻辑可以做到
|
|
-
|
|
|
|
- } else {
|
|
|
|
- // 套装减库存
|
|
|
|
- for (const i of goods) {
|
|
|
|
- const { spec, set_num, single_stock, stock } = i;
|
|
|
|
- if (single_stock === '1') {
|
|
|
|
- const specData = await this.goodsSpecModel.findById(spec._id, { num: 1 }).lean();
|
|
|
|
- const newNum = this.ctx.minus(specData.num, this.ctx.multiply(set_num, buy_num));
|
|
|
|
- this.tran.update('GoodsSpec', spec._id, { num: newNum });
|
|
|
|
- } else {
|
|
|
|
- const newNum = this.ctx.minus(stock, buy_num);
|
|
|
|
- this.tran.update('GoodsSet', set_id, { stock: newNum });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ // 套装减库存
|
|
|
|
+ const { goods, buy_num, cart_id } = i;
|
|
|
|
+ for (const i of goods) {
|
|
|
|
+ const { spec, set_num } = i;
|
|
|
|
+ const specData = await this.goodsSpecModel.findById(spec._id, { num: 1 }).lean();
|
|
|
|
+ const newNum = this.ctx.minus(specData.num, this.ctx.multiply(set_num, buy_num));
|
|
|
|
+ this.tran.update('GoodsSpec', spec._id, { num: newNum });
|
|
|
|
+ }
|
|
|
|
+ if (cart_id) {
|
|
|
|
+ const num = await this.cartModel.count({ _id: cart_id });
|
|
|
|
+ if (num > 0) {
|
|
|
|
+ this.tran.remove('Cart', cart_id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- if (cart_id) this.tran.remove('Cart', cart_id);
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -433,13 +436,13 @@ class OrderService extends CrudService {
|
|
const setData = await this.setModel.findById(set_id).lean();
|
|
const setData = await this.setModel.findById(set_id).lean();
|
|
const { _id, set = [], sell_money, name, freight } = setData;
|
|
const { _id, set = [], sell_money, name, freight } = setData;
|
|
const newSet = [];
|
|
const newSet = [];
|
|
- const obj = { _id, name, sell_money, is_set: '0', num, cart_id, freight, shop };
|
|
|
|
|
|
+ const obj = { _id, set_id: _id, name, sell_money, is_set: '0', num, cart_id, freight, shop };
|
|
for (const s of set) {
|
|
for (const s of set) {
|
|
const { goods, goods_name, spec, spec_name, set_num } = s;
|
|
const { goods, goods_name, spec, spec_name, set_num } = s;
|
|
const goodsData = await this.goodsModel.findById(goods, { file: 1 }).lean();
|
|
const goodsData = await this.goodsModel.findById(goods, { file: 1 }).lean();
|
|
const specData = await this.goodsSpecModel.findById(spec, { file: 1 }).lean();
|
|
const specData = await this.goodsSpecModel.findById(spec, { file: 1 }).lean();
|
|
const file = [ ..._.get(specData, 'file', []), ..._.get(goodsData, 'file', []) ];
|
|
const file = [ ..._.get(specData, 'file', []), ..._.get(goodsData, 'file', []) ];
|
|
- const newSetData = { goods_name, spec_name, file, set_num };
|
|
|
|
|
|
+ const newSetData = { goods_name, goods_id: goods, spec_name, file, set_num };
|
|
newSet.push(newSetData);
|
|
newSet.push(newSetData);
|
|
}
|
|
}
|
|
obj.goods = newSet;
|
|
obj.goods = newSet;
|