|
@@ -11,6 +11,8 @@ class OrderService extends CrudService {
|
|
|
this.orderModel = this.ctx.model.Trade.Order;
|
|
|
this.platformActModel = this.ctx.model.System.PlatformAct;
|
|
|
this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
|
|
|
+ this.goodsModel = this.ctx.model.Shop.Goods;
|
|
|
+ this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
|
|
|
}
|
|
|
|
|
|
// #region 下单前计算函数
|
|
@@ -73,23 +75,27 @@ class OrderService extends CrudService {
|
|
|
}
|
|
|
/**
|
|
|
* 计算需要支付的金额
|
|
|
- * @param {Object} order 支付订单信息
|
|
|
- * @return {Object} 返回:{
|
|
|
- * 店铺id:金额
|
|
|
- * }
|
|
|
+ * @param {Object} list 店铺-商品列表
|
|
|
*/
|
|
|
- shopMoneyTotal(order) {
|
|
|
- let priceKey;
|
|
|
- if (_.get(order, 'type', '0') === '1') priceKey = 'ggrp';
|
|
|
- else priceKey = 'grp';
|
|
|
- const detail = this.moneyDetail(order);
|
|
|
- const result = {};
|
|
|
- for (const s in detail) {
|
|
|
- const values = Object.values(_.get(detail, s, {}));
|
|
|
- const realPay = values.reduce((p, n) => this.ctx.plus(p, n[priceKey]), 0);
|
|
|
- result[s] = realPay;
|
|
|
+ computedShopDetail(list) {
|
|
|
+ for (const i of list) {
|
|
|
+ let goods_total = 0,
|
|
|
+ freight_total = 0,
|
|
|
+ discount = 0;
|
|
|
+ for (const g of i.goods) {
|
|
|
+ // 如果有特价,那就使用特价,没有特价就是用正常销售价
|
|
|
+ goods_total = this.ctx.plus(goods_total, this.ctx.multiply(_.get(g, 'price'), _.get(g, 'buy_num')));
|
|
|
+ freight_total = this.ctx.plus(freight_total, this.ctx.multiply(_.get(g, 'freight'), _.get(g, 'num')));
|
|
|
+ if (_.isArray(g.act)) {
|
|
|
+ const actDiscount = g.act.reduce((p, n) => this.ctx.plus(p, n.discount), 0);
|
|
|
+ discount = this.ctx.plus(discount, actDiscount);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ i.goods_total = goods_total;
|
|
|
+ i.freight_total = freight_total;
|
|
|
+ i.discount = this.ctx.minus(0, discount);
|
|
|
}
|
|
|
- return result;
|
|
|
+ return list;
|
|
|
}
|
|
|
/**
|
|
|
* 计算店铺的金额明细
|
|
@@ -113,6 +119,8 @@ class OrderService extends CrudService {
|
|
|
const d = _.get(moneyDetail, s, {});
|
|
|
obj.goods_total = Object.values(d).reduce((p, n) => this.ctx.plus(p, _.get(n, priceKey, 0)), 0);
|
|
|
obj.freight_total = Object.values(d).reduce((p, n) => this.ctx.plus(p, _.get(n, 'ft', 0)), 0);
|
|
|
+ const act = Object.values(d).reduce((p, n) => [ ...p, ..._.get(n, 'ad', []) ], []);
|
|
|
+ obj.act = act.filter(f => f.platform_act_type === '5' || f.platform_act_type === '6');
|
|
|
result[s] = obj;
|
|
|
}
|
|
|
|
|
@@ -140,11 +148,6 @@ class OrderService extends CrudService {
|
|
|
** platform_act:活动金额
|
|
|
** }]
|
|
|
** at: 活动优惠总金额
|
|
|
- ** grp:商品实际支付价格(goods_real_pay: gt - dt - at)
|
|
|
- ** gsm:规格团购销售价格(group_sell_money)
|
|
|
- ** gst: 规格团购销售总价(group_sell_total: gsm * bn)
|
|
|
- ** ggt: 商品团购支付原价(goods_group_total: gst + ft)
|
|
|
- ** ggrp: 商品团购实际支付价格(goods_group_real_pay: ggt - dt)
|
|
|
** }
|
|
|
** }
|
|
|
** }
|
|
@@ -177,13 +180,101 @@ class OrderService extends CrudService {
|
|
|
// 活动部分
|
|
|
const at = ad.reduce((p, n) => this.ctx.plus(p, n.discount), 0);
|
|
|
const grp = this.ctx.minus(this.ctx.minus(gt, dt), at);
|
|
|
- const obj = { sm, f, bn, st, ft, gt, dd, dt, grp };
|
|
|
+ const obj = { sm, f, bn, st, ft, gt, dd, dt, grp, ad, at };
|
|
|
shopResult[_id] = obj;
|
|
|
}
|
|
|
result[shop] = shopResult;
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
+ // #region 创建订单数据组织部分
|
|
|
+ /**
|
|
|
+ * 组织商品数据
|
|
|
+ * @param {Array} goods 商品列表
|
|
|
+ * @property {Array} actList 活动列表
|
|
|
+ * @property {Array} goodsSpec 后面优惠券使用有关计算的数据
|
|
|
+ * @property {Array} goodsData 组织新的商品数据
|
|
|
+ */
|
|
|
+ async makeOrderGoodsData(goods) {
|
|
|
+ // 商店不做快照,但是商品和商品对应的规格做快照
|
|
|
+ const actList = [],
|
|
|
+ goodsSpecs = [],
|
|
|
+ goodsData = [];
|
|
|
+ for (const i of goods) {
|
|
|
+ const { goods: goodsList, ...others } = i;
|
|
|
+ const qp = [];
|
|
|
+ for (const g of goodsList) {
|
|
|
+ const { goodsSpec_id, goods_id } = g;
|
|
|
+ // 需要的订单数据
|
|
|
+ const orderNeedData = _.pick(g, [ 'act', 'price', 'sp_price', 'num', 'cart_id', 'gift' ]);
|
|
|
+ if (orderNeedData.num) {
|
|
|
+ orderNeedData.buy_num = orderNeedData.num;
|
|
|
+ delete orderNeedData.num;
|
|
|
+ }
|
|
|
+ let goodsSpec = await this.goodsSpecModel.findById(goodsSpec_id);
|
|
|
+ if (!goodsSpec) continue;
|
|
|
+ goodsSpec = JSON.parse(JSON.stringify(goodsSpec));
|
|
|
+ const goods = await this.goodsModel.findById(goods_id);
|
|
|
+ if (goods) goodsSpec.goods = JSON.parse(JSON.stringify(goods));
|
|
|
+ goodsSpec = { ...goodsSpec, ...orderNeedData };
|
|
|
+ qp.push(goodsSpec);
|
|
|
+ const ogs = _.pick(goodsSpec, [ '_id', 'buy_num', 'sell_money', 'price' ]);
|
|
|
+ if (ogs._id) {
|
|
|
+ ogs.id = ogs._id;
|
|
|
+ delete ogs._id;
|
|
|
+ }
|
|
|
+ goodsSpecs.push({ ...ogs });
|
|
|
+ // 将活动提取出来:只需要满减/折;买赠和特价跟着规格走,计算过程中已经处理;加价购是外面处理
|
|
|
+ const { act = [] } = goodsSpec;
|
|
|
+ let gAct = act.filter(f => f.platform_act_type === '5' || f.platform_act_type === '6');
|
|
|
+ gAct = gAct.map(i => ({ ...i, goodsSpec_id: goodsSpec._id }));
|
|
|
+ actList.push(...gAct);
|
|
|
+ }
|
|
|
+ goodsData.push({ ...others, goods: qp });
|
|
|
+ }
|
|
|
+ return { actList, goodsSpecs, goodsData };
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 将加购商品组织进商品数据中
|
|
|
+ * @param {Array} goodsData 上面组织过的数据
|
|
|
+ * @param {Array} plus_goods 加购商品
|
|
|
+ */
|
|
|
+ async addPlusGoods(goodsData, plus_goods) {
|
|
|
+ for (const i of plus_goods) {
|
|
|
+ const { shop, shop_name, goods: goods_id, spec: spec_id, _id, config, platform_act, platform_act_type } = i;
|
|
|
+ // 1,验证活动中是否有数据
|
|
|
+ let num = await this.platformActModel.count({ _id: platform_act });
|
|
|
+ // 没找到活动:下一个
|
|
|
+ if (num <= 0) continue;
|
|
|
+ num = await this.gjaModel.count({ _id });
|
|
|
+ // 没有商品数据:下一个
|
|
|
+ if (num <= 0) continue;
|
|
|
+ // 2.做商品,规格的快照
|
|
|
+ let goodsSpec = await this.goodsSpecModel.findById(spec_id);
|
|
|
+ if (!goodsSpec) continue;
|
|
|
+ goodsSpec = JSON.parse(JSON.stringify(goodsSpec));
|
|
|
+ const goods = await this.goodsModel.findById(goods_id);
|
|
|
+ if (goods) goodsSpec.goods = JSON.parse(JSON.stringify(goods));
|
|
|
+ // 3.向上面一样组织数据
|
|
|
+ const price = _.get(config, 'plus_money', _.get(goodsSpec, 'sell_money'));
|
|
|
+ const act = [{ platform_act, platform_act_type, goods: goods_id, spec: spec_id }];
|
|
|
+ goodsSpec.price = price;
|
|
|
+ goodsSpec.act = act;
|
|
|
+ // 商品数量固定为1
|
|
|
+ goodsSpec.buy_num = 1;
|
|
|
+ // 4.合并进goodsData
|
|
|
+ const r = goodsData.find(f => f.shop === shop);
|
|
|
+ if (r) r.goods.push(goodsSpec);
|
|
|
+ else {
|
|
|
+ goodsData.push({ shop, shop_name, goods: [ goodsSpec ] });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return goodsData;
|
|
|
+ }
|
|
|
+ // #endregion
|
|
|
+
|
|
|
+ // #region 活动部分
|
|
|
/**
|
|
|
* 检查商品是否满足加价购
|
|
|
* @param {Array} goodsList 平铺的商品列表
|
|
@@ -380,6 +471,7 @@ class OrderService extends CrudService {
|
|
|
}
|
|
|
return range;
|
|
|
}
|
|
|
+ // #endregion
|
|
|
}
|
|
|
|
|
|
module.exports = OrderService;
|