|
@@ -36,7 +36,7 @@ class OrderService extends CrudService {
|
|
|
const user = this.ctx.user;
|
|
|
const customer = _.get(user, '_id');
|
|
|
if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户信息');
|
|
|
- const { address, goods, total_detail, coupon = [], type = '0', inviter } = body;
|
|
|
+ const { address, goods, total_detail, coupon = [], plus_goods = [], type = '0', inviter } = body;
|
|
|
if (coupon.length > 1) throw new BusinessError(ErrorCode.DATA_INVALID, '目前只允许使用1张优惠券');
|
|
|
// 检测商品是否可以下单
|
|
|
for (const i of goods) {
|
|
@@ -56,23 +56,30 @@ class OrderService extends CrudService {
|
|
|
// 2.商品快照
|
|
|
const goodsData = [];
|
|
|
// 商店不做快照,但是商品和商品对应的规格做快照
|
|
|
- const { populate } = this.ctx.service.shop.goodsSpec.getRefMods();
|
|
|
for (const i of goods) {
|
|
|
const { goods: goodsList, ...others } = i;
|
|
|
const qp = [];
|
|
|
for (const g of goodsList) {
|
|
|
- const { goodsSpec_id, num, cart_id } = g;
|
|
|
- let goodsSpec = await this.goodsSpecModel.findById(goodsSpec_id).populate(populate);
|
|
|
+ const { goodsSpec_id, goods_id } = g;
|
|
|
+ // 需要的订单数据
|
|
|
+ const orderNeedData = _.pick(g, [ 'act', 'price', 'sp_price', 'num', 'cart_id' ]);
|
|
|
+ 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: gd, ...dOthers } = goodsSpec;
|
|
|
- const gdOthers = _.omit(gd, [ 'shop' ]);
|
|
|
- const obj = { ...dOthers, goods: gdOthers, buy_num: num, tags: _.get(gdOthers, 'tags') };
|
|
|
- if (cart_id) obj.cart_id = cart_id;
|
|
|
- qp.push(obj);
|
|
|
-
|
|
|
- goodsSpecs.push({ id: goodsSpec_id, buy_num: num, sell_money: obj.sell_money, group_sell_money: _.get(obj, 'group_config.money'), type });
|
|
|
+ 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, type });
|
|
|
}
|
|
|
goodsData.push({ ...others, goods: qp });
|
|
|
}
|
|
@@ -80,31 +87,31 @@ class OrderService extends CrudService {
|
|
|
// 计算优惠券的明细
|
|
|
const discountDetail = await this.ctx.service.user.userCoupon.computedOrderCouponDiscount(coupon, goodsSpecs);
|
|
|
const totalDetailData = { ...total_detail, discount_detail: JSON.parse(JSON.stringify(discountDetail)) };
|
|
|
- // 接下来组织订单数据
|
|
|
- orderData.address = addressData;
|
|
|
- orderData.goods = goodsData;
|
|
|
- orderData.total_detail = totalDetailData;
|
|
|
- // 1.用户数据
|
|
|
- orderData.customer = customer;
|
|
|
- // 2.下单时间
|
|
|
- orderData.buy_time = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
|
- // 3.订单号
|
|
|
- const str = this.ctx.service.util.trade.createNonceStr();
|
|
|
- orderData.no = `${moment().format('YYYYMMDDHHmmss')}-${str}`;
|
|
|
- // 4.状态
|
|
|
- orderData.status = '0';
|
|
|
- // 5.返现部分:邀请人: 自己发链接自己买不行
|
|
|
- if (customer !== inviter && ObjectId.isValid(inviter)) orderData.inviter = inviter;
|
|
|
- // 生成数据
|
|
|
- const order_id = this.tran.insert('Order', orderData);
|
|
|
- // 处理库存,删除购物车
|
|
|
- await this.dealGoodsNum(goodsData);
|
|
|
- // 处理优惠券,改为使用过
|
|
|
- if (coupon.length > 0) await this.ctx.service.user.userCoupon.useCoupon(coupon, this.tran);
|
|
|
- await this.tran.run();
|
|
|
- // 创建定时任务(mq死信机制任务)
|
|
|
- await this.toMakeTask(order_id);
|
|
|
- return order_id;
|
|
|
+ // // 接下来组织订单数据
|
|
|
+ // orderData.address = addressData;
|
|
|
+ // orderData.goods = goodsData;
|
|
|
+ // orderData.total_detail = totalDetailData;
|
|
|
+ // // 1.用户数据
|
|
|
+ // orderData.customer = customer;
|
|
|
+ // // 2.下单时间
|
|
|
+ // orderData.buy_time = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
|
+ // // 3.订单号
|
|
|
+ // const str = this.ctx.service.util.trade.createNonceStr();
|
|
|
+ // orderData.no = `${moment().format('YYYYMMDDHHmmss')}-${str}`;
|
|
|
+ // // 4.状态
|
|
|
+ // orderData.status = '0';
|
|
|
+ // // 5.返现部分:邀请人: 自己发链接自己买不行
|
|
|
+ // if (customer !== inviter && ObjectId.isValid(inviter)) orderData.inviter = inviter;
|
|
|
+ // // 生成数据
|
|
|
+ // const order_id = this.tran.insert('Order', orderData);
|
|
|
+ // // 处理库存,删除购物车
|
|
|
+ // await this.dealGoodsNum(goodsData);
|
|
|
+ // // 处理优惠券,改为使用过
|
|
|
+ // if (coupon.length > 0) await this.ctx.service.user.userCoupon.useCoupon(coupon, this.tran);
|
|
|
+ // await this.tran.run();
|
|
|
+ // // 创建定时任务(mq死信机制任务)
|
|
|
+ // await this.toMakeTask(order_id);
|
|
|
+ // return order_id;
|
|
|
} catch (error) {
|
|
|
await this.tran.rollback();
|
|
|
console.error(error);
|
|
@@ -229,8 +236,8 @@ class OrderService extends CrudService {
|
|
|
// 活动部分
|
|
|
// 将加价购拿出来,给前端,需要特殊处理
|
|
|
pageData.actList = actList.filter(f => f.platform_act_type === '4');
|
|
|
- // 满减/折 直接放在orderTotal中,作为明细,反正后面也不用
|
|
|
- const daList = actList.filter(f => f.platform_act_type === '5' || f.platform_act_type === '6');
|
|
|
+ // 满减/折 直接放在orderTotal中,作为明细,反正后面也不用; 只有discount为数字(金额)的情况,说明满足该满减/折的优惠,数组是不满足的
|
|
|
+ const daList = actList.filter(f => (f.platform_act_type === '5' || f.platform_act_type === '6') && _.isNumber(f.discount));
|
|
|
for (const da of daList) {
|
|
|
const obj = { zh: da.title, key: da.platform_act, money: da.discount };
|
|
|
shopTotalDetail.push(obj);
|