|
@@ -38,16 +38,25 @@ class OrderService extends CrudService {
|
|
const user = this.ctx.user;
|
|
const user = this.ctx.user;
|
|
const customer = _.get(user, '_id');
|
|
const customer = _.get(user, '_id');
|
|
if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户信息');
|
|
if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户信息');
|
|
- const { address, goods, coupon = [], plus_goods = [], type = '0', inviter } = body;
|
|
|
|
|
|
+ const { address, goods, coupon = [], plus_goods = [], inviter } = body;
|
|
if (coupon.length > 1) throw new BusinessError(ErrorCode.DATA_INVALID, '目前只允许使用1张优惠券');
|
|
if (coupon.length > 1) throw new BusinessError(ErrorCode.DATA_INVALID, '目前只允许使用1张优惠券');
|
|
// 检测商品是否可以下单
|
|
// 检测商品是否可以下单
|
|
|
|
+ const resetGoodsList = [];
|
|
for (const i of goods) {
|
|
for (const i of goods) {
|
|
- const { shop } = i;
|
|
|
|
- for (const g of i.goods) {
|
|
|
|
- const { goods_id: goods, goodsSpec_id: goodsSpec, num } = g;
|
|
|
|
- const { result, msg } = await this.ctx.service.util.trade.checkCanBuy({ shop, goods, goodsSpec, num }, false);
|
|
|
|
|
|
+ const { shop, is_set = '1' } = i;
|
|
|
|
+ if (is_set === '1') {
|
|
|
|
+ // 非套装
|
|
|
|
+ for (const g of i.goods) {
|
|
|
|
+ const { goods_id: goods, goodsSpec_id: goodsSpec, num } = g;
|
|
|
|
+ const { result, msg } = await this.ctx.service.util.trade.checkCanBuy({ shop, goods, goodsSpec, num }, false);
|
|
|
|
+ if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
|
|
|
|
+ }
|
|
|
|
+ resetGoodsList.push(i);
|
|
|
|
+ } else {
|
|
|
|
+ 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 = {};
|
|
// 数据做快照处理
|
|
// 数据做快照处理
|
|
@@ -125,18 +134,23 @@ class OrderService extends CrudService {
|
|
}
|
|
}
|
|
// 退单分为 2 部分, 涉及价格不需要管.因为这是交钱之前的的操作,不涉及退款
|
|
// 退单分为 2 部分, 涉及价格不需要管.因为这是交钱之前的的操作,不涉及退款
|
|
// 1.货物的库存
|
|
// 1.货物的库存
|
|
- const { goods: shopGoods, total_detail } = order;
|
|
|
|
- // 需要归还库存的商品规格列表:{_id:商品规格id,buy_num:购买的数量}
|
|
|
|
- for (const sg of shopGoods) {
|
|
|
|
- const { goods: goodsList } = sg;
|
|
|
|
- const list = goodsList.map(i => _.pick(i, [ '_id', 'buy_num' ]));
|
|
|
|
- for (const i of list) {
|
|
|
|
- const goodsSpec = await this.goodsSpecModel.findById(i._id, { num: 1 });
|
|
|
|
- if (!goodsSpec) continue;
|
|
|
|
- const newNum = this.ctx.plus(goodsSpec.num, i.buy_num);
|
|
|
|
- this.tran.update('GoodsSpec', i._id, { num: newNum });
|
|
|
|
|
|
+ const { goods: shopGoods, total_detail, is_set = '1' } = order;
|
|
|
|
+ if (is_set === '1') {
|
|
|
|
+ // 需要归还库存的商品规格列表:{_id:商品规格id,buy_num:购买的数量}
|
|
|
|
+ for (const sg of shopGoods) {
|
|
|
|
+ const { goods: goodsList } = sg;
|
|
|
|
+ const list = goodsList.map(i => _.pick(i, [ '_id', 'buy_num' ]));
|
|
|
|
+ for (const i of list) {
|
|
|
|
+ const goodsSpec = await this.goodsSpecModel.findById(i._id, { num: 1 });
|
|
|
|
+ if (!goodsSpec) continue;
|
|
|
|
+ const newNum = this.ctx.plus(goodsSpec.num, i.buy_num);
|
|
|
|
+ this.tran.update('GoodsSpec', i._id, { num: newNum });
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ } else {
|
|
|
|
+ // 套装退库存
|
|
}
|
|
}
|
|
|
|
+
|
|
// 2.优惠券
|
|
// 2.优惠券
|
|
const { discount_detail } = total_detail;
|
|
const { discount_detail } = total_detail;
|
|
if (discount_detail) {
|
|
if (discount_detail) {
|
|
@@ -165,16 +179,34 @@ 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) {
|
|
for (const g of i.goods) {
|
|
- const { _id, buy_num, cart_id } = g;
|
|
|
|
- const goodsSpec = await this.goodsSpecModel.findById(_id);
|
|
|
|
- const newNum = this.ctx.minus(goodsSpec.num, buy_num);
|
|
|
|
- this.tran.update('GoodsSpec', _id, { num: newNum });
|
|
|
|
- // 活动相关:
|
|
|
|
- // 买赠:不一定非要处理赠品,赠品可能不在库中;
|
|
|
|
- // 特价,满减/折:没关系
|
|
|
|
- // 加价购,作为另一个商品出现,一样会减少库存,不需要处理
|
|
|
|
- // 先不处理活动库存问题.先有逻辑可以做到
|
|
|
|
|
|
+ const { _id, buy_num, cart_id, is_set = '1', set_id, goods } = g;
|
|
|
|
+ if (is_set === '1') {
|
|
|
|
+ const goodsSpec = await this.goodsSpecModel.findById(_id);
|
|
|
|
+ const newNum = this.ctx.minus(goodsSpec.num, buy_num);
|
|
|
|
+ this.tran.update('GoodsSpec', _id, { num: newNum });
|
|
|
|
+ // 活动相关:
|
|
|
|
+ // 买赠:不一定非要处理赠品,赠品可能不在库中;
|
|
|
|
+ // 特价,满减/折:没关系
|
|
|
|
+ // 加价购,作为另一个商品出现,一样会减少库存,不需要处理
|
|
|
|
+ // 先不处理活动库存问题.先有逻辑可以做到
|
|
|
|
+
|
|
|
|
+ } 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 });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
if (cart_id) this.tran.remove('Cart', cart_id);
|
|
if (cart_id) this.tran.remove('Cart', cart_id);
|
|
|
|
+
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|