|
@@ -24,28 +24,10 @@ class CartService extends CrudService {
|
|
*/
|
|
*/
|
|
async selfCart({ customer }) {
|
|
async selfCart({ customer }) {
|
|
assert(customer, '缺少顾客信息');
|
|
assert(customer, '缺少顾客信息');
|
|
- const { populate } = this.getRefMods();
|
|
|
|
- let list = await this.model.find({ customer }).populate(populate);
|
|
|
|
- list = JSON.parse(JSON.stringify(list));
|
|
|
|
- list = list.map(i => {
|
|
|
|
- const obj = {};
|
|
|
|
- obj.shop_name = _.get(i.shop, 'name');
|
|
|
|
- obj.shop = _.get(i.shop, '_id');
|
|
|
|
- obj.goods = this.setCartGoodsData(i);
|
|
|
|
- return obj;
|
|
|
|
- });
|
|
|
|
- list = _.groupBy(list, 'shop');
|
|
|
|
- const arr = [];
|
|
|
|
- for (const key in list) {
|
|
|
|
- const shopGroup = list[key];
|
|
|
|
- const goods = shopGroup.reduce((p, n) => {
|
|
|
|
- p.push(n.goods);
|
|
|
|
- return p;
|
|
|
|
- }, []);
|
|
|
|
- const shop = _.omit(_.head(shopGroup), [ 'goods' ]);
|
|
|
|
- arr.push({ ...shop, goods });
|
|
|
|
- }
|
|
|
|
- return arr;
|
|
|
|
|
|
+ const data = await this.model.find({ customer });
|
|
|
|
+ const actList = await this.ctx.service.trade.order.getActList(data);
|
|
|
|
+ const pageData = await this.ctx.service.trade.order.getPageData(data, actList);
|
|
|
|
+ return pageData;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -80,17 +62,19 @@ class CartService extends CrudService {
|
|
* @param body.num 数量
|
|
* @param body.num 数量
|
|
*/
|
|
*/
|
|
async create({ num, ...body }) {
|
|
async create({ num, ...body }) {
|
|
- const { customer, shop, goods, goodsSpec } = body;
|
|
|
|
|
|
+ const { customer, shop, goods, goodsSpec, act } = body;
|
|
assert(customer, '缺少顾客信息');
|
|
assert(customer, '缺少顾客信息');
|
|
assert(shop, '缺少店铺信息');
|
|
assert(shop, '缺少店铺信息');
|
|
assert(goods, '缺少商品信息');
|
|
assert(goods, '缺少商品信息');
|
|
assert(goodsSpec, '缺少商品规格信息');
|
|
assert(goodsSpec, '缺少商品规格信息');
|
|
- const data = await this.model.findOne(body);
|
|
|
|
|
|
+ const query = _.pick(body, [ 'customer', 'shop', 'goods', 'goodsSpec' ]);
|
|
|
|
+ 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);
|
|
const { enough } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num: buyNum });
|
|
const { enough } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num: buyNum });
|
|
if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, '库存不足,无法添加至购物车');
|
|
if (!enough) throw new BusinessError(ErrorCode.SERVICE_FAULT, '库存不足,无法添加至购物车');
|
|
data.num = buyNum;
|
|
data.num = buyNum;
|
|
|
|
+ if (act) data.act = act;
|
|
await data.save();
|
|
await data.save();
|
|
} else {
|
|
} else {
|
|
const { enough } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num });
|
|
const { enough } = await this.checkGoodsNum({ goodsSpecId: goodsSpec, num });
|