|
@@ -18,6 +18,8 @@ class OrderService extends CrudService {
|
|
|
this.addressModel = this.ctx.model.User.Address;
|
|
|
this.cartModel = this.ctx.model.Trade.Cart;
|
|
|
this.userCouponModel = this.ctx.model.User.UserCoupon;
|
|
|
+ this.platformActModel = this.ctx.model.System.PlatformAct;
|
|
|
+ this.gjaModel = this.ctx.model.Shop.GoodsJoinAct;
|
|
|
this.tran = new Transaction();
|
|
|
}
|
|
|
|
|
@@ -202,13 +204,14 @@ class OrderService extends CrudService {
|
|
|
if (!_.isArray(data)) throw new BusinessError(ErrorCode.DATA_INVALID, '数据不正确,请重新下单');
|
|
|
const head = _.head(data);
|
|
|
if (_.isString(head)) {
|
|
|
- // 购物车来的
|
|
|
- } else if (_.isObject(head)) {
|
|
|
- // 直接购买来的
|
|
|
- const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(data, false);
|
|
|
- if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
|
|
|
- specsData = await this.getPageData(data);
|
|
|
- } else throw new BusinessError(ErrorCode.DATA_INVALID, '数据不正确,请重新下单');
|
|
|
+ // 购物车来的,将购物车中的数据拿出来转换下
|
|
|
+ const carts = await this.cartModel.find({ _id: data });
|
|
|
+ data = carts;
|
|
|
+ }
|
|
|
+ const { result, msg } = await this.ctx.service.util.trade.checkCanBuy(data, false);
|
|
|
+ if (!result) throw new BusinessError(ErrorCode.DATA_INVALID, msg);
|
|
|
+ // 正常整理商品的内容
|
|
|
+ specsData = await this.getPageData(data);
|
|
|
// 组装页面的数据
|
|
|
const user = this.ctx.user;
|
|
|
const customer = _.get(user, '_id');
|
|
@@ -227,9 +230,74 @@ class OrderService extends CrudService {
|
|
|
const couponList = await this.ctx.service.user.userCoupon.toMakeOrder_getList(specsData);
|
|
|
pageData.couponList = couponList;
|
|
|
// 返现部分:添加推荐人信息
|
|
|
- pageData.inviter = _.get(data, 'inviter');
|
|
|
+ const inviter = data.find(f => ObjectId.isValid(f.inviter));
|
|
|
+ pageData.inviter = inviter;
|
|
|
+ // 活动部分
|
|
|
+ // 每个数据中都有act字段.这个字段表明商品参加了哪些活动
|
|
|
+ // await this.dealAct();
|
|
|
return pageData;
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 处理该订单活动部分
|
|
|
+ * * 该商品可能会参加多个活动,活动之间有叠加问题
|
|
|
+ * * 买赠:没关系,只要去找赠品就行
|
|
|
+ * * 特价:需要找到特价,将价格更改为特价
|
|
|
+ * * 加价购: 满足下限金额时,组织数据,允许前端进行加价购
|
|
|
+ * * 满减/折: 针对整个订单而言. 满足就处理,不满足就是不处理
|
|
|
+ * * 套装:暂不处理
|
|
|
+ * * ps: 特价与满减/折叠加, 按特价计算总价再看满减/折; 加价购不在满减/折的金额下限计算范围内
|
|
|
+ * @param {Array} data 购物车数据(直接购买也会组织成购物车数据)
|
|
|
+ */
|
|
|
+ async dealAct(data) {
|
|
|
+ const actList = [];
|
|
|
+ for (const i of data) {
|
|
|
+ const { act = [], spec } = i;
|
|
|
+ if (act.length <= 0) continue;
|
|
|
+ for (const a of act) {
|
|
|
+ const platformAct = await this.platformActModel.findById(a);
|
|
|
+ // 没有找到活动,略过
|
|
|
+ if (!platformAct) continue;
|
|
|
+ // 活动未开启,略过
|
|
|
+ if (_.get(platformAct, 'is_use') !== '0') continue;
|
|
|
+ // 活动类型为 0&1不需要处理
|
|
|
+ const type = _.get(platformAct, 'type');
|
|
|
+ if (type === '1' || type === '0') continue;
|
|
|
+ // 先找下是否处于活动设置的程序时间
|
|
|
+ const start = _.get(platformAct, 'config.time_start');
|
|
|
+ const end = _.get(platformAct, 'config.time_end');
|
|
|
+ const r = moment().isBetween(start, end, null, '[]');
|
|
|
+ // 不在程序设定的活动时间内,下一个
|
|
|
+ if (!r) continue;
|
|
|
+ // 有关最后活动总结数据问题:
|
|
|
+ // 1.买赠:需要具体到规格
|
|
|
+ // 2.特价:需要具体到规格
|
|
|
+ // 3.加价购:需要和商品一起进行判断
|
|
|
+ // 4&5:满减/折:需要和商品一起判断
|
|
|
+ // 6.套装:先不考虑
|
|
|
+ if (type === '2') {
|
|
|
+ // 买赠,直接去到活动中找到赠品
|
|
|
+ const gja = await this.gjaModel.findOne({ platform_act: platformAct._id, 'spec._id': spec }, { config: 1 });
|
|
|
+ const gift = _.get(gja, 'config.gift', []);
|
|
|
+ actList.push({ platform_act_type: type, spec, gift });
|
|
|
+ } else if (type === '3') {
|
|
|
+ // 特价,找出特价
|
|
|
+ const gja = await this.gjaModel.findOne({ platform_act: platformAct._id, 'spec._id': spec }, { config: 1 });
|
|
|
+ const sp_price = _.get(gja, 'config.sp_price');
|
|
|
+ } else if (type === '4') {
|
|
|
+ // 加价购,找出加价购下限;如果判断下限够了,那就可以让前端去加价购
|
|
|
+ const plus_money = _.get(platformAct, 'config.plus_money', 0);
|
|
|
+ } else if (type === '5') {
|
|
|
+ // 满减
|
|
|
+ const discount = _.get(platformAct, 'discount', []);
|
|
|
+ } else if (type === '6') {
|
|
|
+ // 满折
|
|
|
+ const discount = _.get(platformAct, 'discount', []);
|
|
|
+ } else if (type === '7') {
|
|
|
+ // 套装先不考虑
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
// 直接购买&购物车,这俩字段基本没差, 组织订单页商品数据
|
|
|
async getPageData(data) {
|
|
@@ -238,6 +306,7 @@ class OrderService extends CrudService {
|
|
|
const { goodsSpec, num } = i;
|
|
|
const d = await this.goodsSpecModel.aggregate([
|
|
|
{ $match: { _id: ObjectId(goodsSpec) } },
|
|
|
+ // #region 处理店铺与商品部分
|
|
|
{ $addFields: { goods_id: { $toObjectId: '$goods' } } },
|
|
|
{
|
|
|
$lookup: {
|
|
@@ -261,6 +330,7 @@ class OrderService extends CrudService {
|
|
|
},
|
|
|
},
|
|
|
{ $unwind: '$goods' },
|
|
|
+ // #endregion
|
|
|
{
|
|
|
$project: {
|
|
|
_id: 0,
|
|
@@ -294,75 +364,6 @@ class OrderService extends CrudService {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 单商品整理数据,剩下的可以简略
|
|
|
- * @param {Array} list 规格数组
|
|
|
- * @param num 购买数量
|
|
|
- */
|
|
|
- setGoodsToPageData(list, num) {
|
|
|
- // 按店铺分组,精简字段
|
|
|
- const arr = [];
|
|
|
- for (const i of list) {
|
|
|
- const obj = {};
|
|
|
- obj.shop_name = _.get(i.goods, 'shop.name');
|
|
|
- obj.shop = _.get(i.goods, 'shop._id');
|
|
|
- const goods = {};
|
|
|
- goods.goods_id = _.get(i.goods, '_id');
|
|
|
- goods.goods_name = _.get(i.goods, 'name');
|
|
|
- goods.goodsSpec_id = _.get(i, '_id');
|
|
|
- goods.goodsSpec_name = _.get(i, 'name');
|
|
|
- goods.freight = _.get(i, 'freight');
|
|
|
- goods.money = _.get(i, 'sell_money');
|
|
|
- goods.num = num;
|
|
|
- goods.file = _.get(i.goods, 'file');
|
|
|
- goods.tags = _.get(i.goods, 'tags');
|
|
|
- // #region 团购部分
|
|
|
- // 团购价
|
|
|
- goods.group_sell_money = _.get(i, 'group_config.money');
|
|
|
- // #endregion
|
|
|
- obj.goods = [ goods ];
|
|
|
- arr.push(obj);
|
|
|
- }
|
|
|
- return arr;
|
|
|
- }
|
|
|
- /**
|
|
|
- *购物车数据整理
|
|
|
- * @param {Array} list 规格数组
|
|
|
- */
|
|
|
- setCartsGoodsToPageData(list) {
|
|
|
- const arr = [];
|
|
|
- list = _.groupBy(list, 'shop._id');
|
|
|
- for (const key in list) {
|
|
|
- const data = list[key];
|
|
|
- const shopData = _.get(_.head(data), 'shop');
|
|
|
- const obj = {};
|
|
|
- obj.shop = _.get(shopData, '_id');
|
|
|
- obj.shop_name = _.get(shopData, 'name');
|
|
|
- const goodsList = [];
|
|
|
- for (const i of data) {
|
|
|
- const goods = {};
|
|
|
- goods.cart_id = _.get(i, '_id');
|
|
|
- goods.goods_id = _.get(i.goods, '_id');
|
|
|
- goods.goods_name = _.get(i.goods, 'name');
|
|
|
- goods.goodsSpec_id = _.get(i.goodsSpec, '_id');
|
|
|
- goods.goodsSpec_name = _.get(i.goodsSpec, 'name');
|
|
|
- goods.freight = _.get(i.goodsSpec, 'freight');
|
|
|
- goods.money = _.get(i.goodsSpec, 'sell_money');
|
|
|
- goods.num = _.get(i, 'num');
|
|
|
- goods.file = _.get(i.goods, 'file', []);
|
|
|
- goods.tags = _.get(i.goods, 'tags');
|
|
|
- // #region 团购部分
|
|
|
- // 团购价
|
|
|
- goods.group_sell_money = _.get(i.goodsSpec, 'group_config.money');
|
|
|
- // #endregion
|
|
|
- goodsList.push(goods);
|
|
|
- }
|
|
|
- obj.goods = goodsList;
|
|
|
- arr.push(obj);
|
|
|
- }
|
|
|
- return arr;
|
|
|
- }
|
|
|
-
|
|
|
async afterQuery(filter, data) {
|
|
|
data = JSON.parse(JSON.stringify(data));
|
|
|
for (const i of data) {
|