|
@@ -32,7 +32,7 @@ class CartService extends CrudService {
|
|
let data = await this.model.find({ customer });
|
|
let data = await this.model.find({ customer });
|
|
if (data.length > 0) {
|
|
if (data.length > 0) {
|
|
data = JSON.parse(JSON.stringify(data));
|
|
data = JSON.parse(JSON.stringify(data));
|
|
- data = data.map(i => ({ ...i, cart_id: i._id }));
|
|
|
|
|
|
+ data = data.map((i) => ({ ...i, cart_id: i._id }));
|
|
}
|
|
}
|
|
data = await this.getCartGoodsAct(data);
|
|
data = await this.getCartGoodsAct(data);
|
|
const actList = await this.ctx.service.trade.order.getActList(data);
|
|
const actList = await this.ctx.service.trade.order.getActList(data);
|
|
@@ -44,10 +44,10 @@ class CartService extends CrudService {
|
|
* @param {Array} data 购物车商品
|
|
* @param {Array} data 购物车商品
|
|
*/
|
|
*/
|
|
async getCartGoodsAct(data) {
|
|
async getCartGoodsAct(data) {
|
|
- data = data.map(i => ({ ...i, act: [] }));
|
|
|
|
|
|
+ data = data.map((i) => ({ ...i, act: [] }));
|
|
// 实时匹配活动问题
|
|
// 实时匹配活动问题
|
|
const platformActList = await this.platformActModel.find({ is_use: '0' });
|
|
const platformActList = await this.platformActModel.find({ is_use: '0' });
|
|
- const platform_act = platformActList.map(i => ObjectId(i._id).toString());
|
|
|
|
|
|
+ const platform_act = platformActList.map((i) => ObjectId(i._id).toString());
|
|
for (const cart of data) {
|
|
for (const cart of data) {
|
|
const { goodsSpec: spec_id, goods: goods_id, act = [], is_set = '1', set_id } = cart;
|
|
const { goodsSpec: spec_id, goods: goods_id, act = [], is_set = '1', set_id } = cart;
|
|
if (is_set === '1' || !set_id) {
|
|
if (is_set === '1' || !set_id) {
|
|
@@ -88,7 +88,7 @@ class CartService extends CrudService {
|
|
} else assert(set_id, '缺少套装id');
|
|
} else assert(set_id, '缺少套装id');
|
|
if (is_set === '1') {
|
|
if (is_set === '1') {
|
|
// 非套装 添加购物车
|
|
// 非套装 添加购物车
|
|
- const query = _.pick(body, [ 'customer', 'shop', 'goods', 'goodsSpec' ]);
|
|
|
|
|
|
+ const query = _.pick(body, ['customer', 'shop', 'goods', 'goodsSpec']);
|
|
const data = await this.model.findOne(query);
|
|
const data = await this.model.findOne(query);
|
|
if (data) {
|
|
if (data) {
|
|
const buyNum = this.ctx.plus(data.num, num);
|
|
const buyNum = this.ctx.plus(data.num, num);
|
|
@@ -103,7 +103,7 @@ class CartService extends CrudService {
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
// 套装添加购物车
|
|
// 套装添加购物车
|
|
- const query = _.pick(body, [ 'customer', 'set_id' ]);
|
|
|
|
|
|
+ const query = _.pick(body, ['customer', 'set_id']);
|
|
const data = await this.model.findOne(query);
|
|
const data = await this.model.findOne(query);
|
|
if (data) {
|
|
if (data) {
|
|
// 计算套装购买数量
|
|
// 计算套装购买数量
|
|
@@ -128,31 +128,25 @@ class CartService extends CrudService {
|
|
async checkSetGoodsNum(data, buyNum) {
|
|
async checkSetGoodsNum(data, buyNum) {
|
|
const { set_id } = data;
|
|
const { set_id } = data;
|
|
const setData = await this.setModel.findById(set_id).lean();
|
|
const setData = await this.setModel.findById(set_id).lean();
|
|
- const { set, single_stock = '1', stock } = setData;
|
|
|
|
|
|
+ const { shop } = setData;
|
|
|
|
+ const shopData = await this.shopModel.findById(shop);
|
|
|
|
+ const shopRes = this.ctx.service.util.trade.checkShop(shopData);
|
|
|
|
+ if (shopRes.result !== true) throw new BusinessError(ErrorCode.DATA_INVALID, shopRes.msg);
|
|
|
|
+ const { set } = setData;
|
|
const stockList = [];
|
|
const stockList = [];
|
|
for (const sd of set) {
|
|
for (const sd of set) {
|
|
- const { shop, goods, spec, set_num } = sd;
|
|
|
|
- const shopData = await this.shopModel.findById(shop);
|
|
|
|
- const shopRes = this.ctx.service.util.trade.checkShop(shopData);
|
|
|
|
- if (shopRes.result !== true) throw new BusinessError(ErrorCode.DATA_INVALID, shopRes.msg);
|
|
|
|
|
|
+ const { goods, spec, set_num } = sd;
|
|
const goodsData = await this.goodsModel.findById(goods);
|
|
const goodsData = await this.goodsModel.findById(goods);
|
|
const goodsRes = this.ctx.service.util.trade.checkGoods(goodsData);
|
|
const goodsRes = this.ctx.service.util.trade.checkGoods(goodsData);
|
|
if (goodsRes.result !== true) throw new BusinessError(ErrorCode.DATA_INVALID, goodsRes.msg);
|
|
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;
|
|
|
|
- // 应该用 库存/每套的件数 向下取整作为库存量
|
|
|
|
- const stock_num = _.floor(this.ctx.divide(gsnum, set_num));
|
|
|
|
- stockList.push(stock_num);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- if (single_stock !== '1') {
|
|
|
|
- // 走单独的库存
|
|
|
|
- if (this.ctx.minus(stock, buyNum) < 0) throw new BusinessError(ErrorCode.DATA_INVALID, '套装库存不足');
|
|
|
|
- stockList.push(stock);
|
|
|
|
|
|
+ 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;
|
|
|
|
+ // 应该用 库存/每套的件数 向下取整作为库存量
|
|
|
|
+ const stock_num = _.floor(this.ctx.divide(gsnum, set_num));
|
|
|
|
+ stockList.push(stock_num);
|
|
}
|
|
}
|
|
const total = _.min(stockList);
|
|
const total = _.min(stockList);
|
|
return { enough: true, total };
|
|
return { enough: true, total };
|
|
@@ -195,8 +189,6 @@ class CartService extends CrudService {
|
|
await this.model.updateOne({ _id: cartId }, { num });
|
|
await this.model.updateOne({ _id: cartId }, { num });
|
|
}
|
|
}
|
|
return { enough: true, total: gsnum };
|
|
return { enough: true, total: gsnum };
|
|
-
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|