|
@@ -12,6 +12,7 @@ class CartService extends CrudService {
|
|
|
this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
|
|
|
this.platformActModel = this.ctx.model.System.PlatformAct;
|
|
|
this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
|
|
|
+ this.setModel = this.ctx.model.Shop.GoodsSet;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -46,22 +47,24 @@ class CartService extends CrudService {
|
|
|
const platformActList = await this.platformActModel.find({ is_use: '0' });
|
|
|
const platform_act = platformActList.map(i => ObjectId(i._id).toString());
|
|
|
for (const cart of data) {
|
|
|
- const { goodsSpec: spec_id, goods: goods_id, act = [] } = cart;
|
|
|
- const gjaList = await this.gjaModel.find({ platform_act, 'goods._id': ObjectId(goods_id).toString(), 'spec._id': ObjectId(spec_id).toString() });
|
|
|
- for (const gja of gjaList) {
|
|
|
- const { platform_act_type: type, platform_act } = gja;
|
|
|
- // 加价购需要额外判断是否是基础商品
|
|
|
- if (type === '4') {
|
|
|
- const goods_type = _.get(gja, 'config.goods_type');
|
|
|
- if (goods_type === 'basic') act.push(platform_act);
|
|
|
- } else act.push(platform_act);
|
|
|
+ const { goodsSpec: spec_id, goods: goods_id, act = [], is_set = '1', set_id } = cart;
|
|
|
+ if (is_set === '1' || !set_id) {
|
|
|
+ // 非套装处理, 套装不参与其他活动
|
|
|
+ const gjaList = await this.gjaModel.find({ platform_act, 'goods._id': ObjectId(goods_id).toString(), 'spec._id': ObjectId(spec_id).toString() });
|
|
|
+ for (const gja of gjaList) {
|
|
|
+ const { platform_act_type: type, platform_act } = gja;
|
|
|
+ // 加价购需要额外判断是否是基础商品
|
|
|
+ if (type === '4') {
|
|
|
+ const goods_type = _.get(gja, 'config.goods_type');
|
|
|
+ if (goods_type === 'basic') act.push(platform_act);
|
|
|
+ } else act.push(platform_act);
|
|
|
+ }
|
|
|
+ cart.act = _.uniq(act);
|
|
|
}
|
|
|
- cart.act = _.uniq(act);
|
|
|
}
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
** 创建购物车信息,若找到该店的该商品规格.进行库存校验
|
|
|
** 数量进行合并;
|
|
@@ -74,24 +77,79 @@ class CartService extends CrudService {
|
|
|
* @param body.num 数量
|
|
|
*/
|
|
|
async create({ num, ...body }) {
|
|
|
- const { customer, shop, goods, goodsSpec } = body;
|
|
|
+ const { customer, shop, goods, goodsSpec, is_set = '1', set_id } = body;
|
|
|
assert(customer, '缺少顾客信息');
|
|
|
- assert(shop, '缺少店铺信息');
|
|
|
- assert(goods, '缺少商品信息');
|
|
|
- assert(goodsSpec, '缺少商品规格信息');
|
|
|
- const query = _.pick(body, [ 'customer', 'shop', 'goods', 'goodsSpec' ]);
|
|
|
- const data = await this.model.findOne(query);
|
|
|
- if (data) {
|
|
|
- const buyNum = this.ctx.plus(data.num, num);
|
|
|
- const { enough, msg } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num: buyNum });
|
|
|
- if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, msg);
|
|
|
- data.num = buyNum;
|
|
|
- await data.save();
|
|
|
+ if (is_set === '1') {
|
|
|
+ assert(shop, '缺少店铺信息');
|
|
|
+ assert(goods, '缺少商品信息');
|
|
|
+ assert(goodsSpec, '缺少商品规格信息');
|
|
|
+ } else assert(set_id, '缺少套装id');
|
|
|
+ if (is_set === '1') {
|
|
|
+ // 非套装 添加购物车
|
|
|
+ const query = _.pick(body, [ 'customer', 'shop', 'goods', 'goodsSpec' ]);
|
|
|
+ const data = await this.model.findOne(query);
|
|
|
+ if (data) {
|
|
|
+ const buyNum = this.ctx.plus(data.num, num);
|
|
|
+ const { enough, msg } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num: buyNum });
|
|
|
+ if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, msg);
|
|
|
+ data.num = buyNum;
|
|
|
+ await data.save();
|
|
|
+ } else {
|
|
|
+ const { enough, msg } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num });
|
|
|
+ if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, msg);
|
|
|
+ await this.model.create({ num, ...body });
|
|
|
+ }
|
|
|
} else {
|
|
|
- const { enough, msg } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num });
|
|
|
- if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, msg);
|
|
|
- await this.model.create({ num, ...body });
|
|
|
+ // 套装添加购物车
|
|
|
+ const query = _.pick(body, [ 'customer', 'set_id' ]);
|
|
|
+ const data = await this.model.findOne(query);
|
|
|
+ if (data) {
|
|
|
+ // 计算套装购买数量
|
|
|
+ const buyNum = this.ctx.plus(data.num, num);
|
|
|
+ const { enough, msg } = await this.checkSetGoodsNum(data, buyNum);
|
|
|
+ if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, msg);
|
|
|
+ data.num = buyNum;
|
|
|
+ await data.save();
|
|
|
+ } else {
|
|
|
+ const { enough, msg } = await this.checkSetGoodsNum(data, num);
|
|
|
+ if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, msg);
|
|
|
+ await this.model.create({ num, ...body });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查套装购物车库存是否足够
|
|
|
+ * @param {Object} data 套装的购物车数据
|
|
|
+ * @param {Number} buyNum 套装购买数量
|
|
|
+ */
|
|
|
+ async checkSetGoodsNum(data, buyNum) {
|
|
|
+ const { set_id } = data;
|
|
|
+ const setData = await this.setModel.findById(set_id).lean();
|
|
|
+ const { set, single_stock = '1', stock } = setData;
|
|
|
+ const stockList = [];
|
|
|
+ for (const sd of set) {
|
|
|
+ const { shop, goods, spec, set_num } = sd;
|
|
|
+ const shopRes = this.ctx.service.util.trade.checkShop(shop);
|
|
|
+ if (shopRes.result !== true) throw new BusinessError(ErrorCode.DATA_INVALID, shopRes.msg);
|
|
|
+ const goodsRes = this.ctx.service.util.trade.checkGoods(goods);
|
|
|
+ if (goodsRes.result !== true) throw new BusinessError(ErrorCode.DATA_INVALID, goodsRes.msg);
|
|
|
+ if (single_stock === '1') {
|
|
|
+ const goodsSpecData = await this.goodsSpecModel.findById(spec);
|
|
|
+ const setGoodsNum = this.ctx.multiply(buyNum, set_num);
|
|
|
+ const gsRes = this.ctx.service.util.trade.checkGoodsSpec(goodsSpecData, setGoodsNum, '0');
|
|
|
+ if (gsRes.result !== true) throw new BusinessError(ErrorCode.DATA_INVALID, gsRes.msg);
|
|
|
+ const { num: gsnum = 0 } = goodsSpecData;
|
|
|
+ stockList.push(gsnum);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (single_stock !== '1') {
|
|
|
+ // 走单独的库存
|
|
|
+ if (this.ctx.minus(stock, buyNum) < 0) throw new BusinessError(ErrorCode.DATA_INVALID, '套装库存不足');
|
|
|
+ stockList.push(stock);
|
|
|
}
|
|
|
+ const total = _.min(stockList);
|
|
|
+ return { enough: true, total };
|
|
|
}
|
|
|
|
|
|
/**
|