lrf 2 年之前
父節點
當前提交
570f7a3c33
共有 4 個文件被更改,包括 32 次插入7 次删除
  1. 8 1
      app/controller/group/config/.group.js
  2. 21 4
      app/service/group/group.js
  3. 2 2
      app/service/trade/afterSale.js
  4. 1 0
      app/z_router/group/group.js

+ 8 - 1
app/controller/group/config/.group.js

@@ -24,7 +24,7 @@ module.exports = {
         shop: 'shop',
         goods: 'goods._id',
         goodsSpec: 'goodsSpec._id',
-        person:'persons.customer',
+        person: 'persons.customer',
         leader: 'leader',
         status: 'status',
       },
@@ -40,4 +40,11 @@ module.exports = {
       count: true,
     },
   },
+  getGroup: {
+    parameters: {
+      query: {
+        order_id: 'order_id',
+      },
+    },
+  },
 };

+ 21 - 4
app/service/group/group.js

@@ -14,6 +14,7 @@ class GroupService extends CrudService {
     this.goodsModel = this.ctx.model.Shop.Goods;
     this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
     this.userModel = this.ctx.model.User.User;
+    this.orderModel = this.ctx.model.Trade.Order;
   }
 
   /**
@@ -82,17 +83,21 @@ class GroupService extends CrudService {
    * @param tran
    */
   async refund({ group, customer }, tran) {
-    const groupData = await this.model.findById(group);
+    let groupData = await this.model.findById(group);
     if (!groupData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到拼团数据');
-    const { persons, leader } = groupData;
+    groupData = JSON.parse(JSON.stringify(groupData));
+    const { persons, leader, status } = groupData;
+    // 检查团状态,如果已经结束,就不需要该团这边的数据了
+    if (status !== '0') return;
     const newPersons = JSON.parse(JSON.stringify(persons));
-    const findPersonCondition = (c1, c2) => c1 === c2;
+    const findPersonCondition = (c1, c2) => ObjectId(c1).equals(c2);
     const p = newPersons.find(f => findPersonCondition(f.customer, customer));
     if (!p) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到要退团的用户');
     const i = newPersons.findIndex(f => findPersonCondition(f.customer, customer));
     p.status = '1';
+    p.out_time = moment().format('YYYY-MM-DD HH:mm:ss');
     newPersons[i] = p;
-    const updateData = { newPersons };
+    const updateData = { persons: newPersons };
     // 团长退团了,根据入团时间降序,顺位成为团长
     if (leader === customer) {
       let newLeader;
@@ -106,6 +111,18 @@ class GroupService extends CrudService {
     tran.update('Group', group, updateData);
   }
 
+  /**
+   *  获取团信息
+   * @param {Object} query 地址参数
+   * @param {String} query.order_id 订单id
+   */
+  async getGroup({ order_id }) {
+    assert(order_id, '缺少订单信息');
+    const order = await this.orderModel.findById(order_id, { group: 1 });
+    if (!order) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到订单信息');
+    return _.get(order, 'group');
+  }
+
   async beforeQuery(filter) {
     const gsv = _.get(filter, 'goodsSpec._id');
     const gv = _.get(filter, 'goods._id');

+ 2 - 2
app/service/trade/afterSale.js

@@ -49,7 +49,7 @@ class AfterSaleService extends CrudService {
       const type = _.get(entity, 'type');
       const status = _.get(update, 'status');
       // 同意退款/退货,则直接进行退款,然后再将状态修改为已退款
-      if (type !== '2' && status === '1') {
+      if (type !== '2' && (status === '1' || status === '2')) {
         await this.toRefund({ afterSale_id: entity._id, goods_id: _.get(entity, 'goods._id') }, this.tran);
       }
       await this.tran.run();
@@ -117,7 +117,7 @@ class AfterSaleService extends CrudService {
     if (type === '1' && status !== '3') {
       // 团购单,走团购退货逻辑补充
       const { group, customer } = orderDetail;
-      await this.ctx.service.group.group.refund({ group, customer }, tran);
+      await this.ctx.service.group.group.refund({ group: group._id, customer: customer._id }, tran);
     }
     // #endregion
     // 检查优惠券是否都退了

+ 1 - 0
app/z_router/group/group.js

@@ -7,6 +7,7 @@ const rkey = 'group';
 const ckey = 'group.group';
 const keyZh = '团';
 const routes = [
+  { method: 'get', path: `${rkey}/getGroup`, controller: `${ckey}.getGroup`, name: `${ckey}getGroup`, zh: `${keyZh}获取订单信息` },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
   // { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, name: `${ckey}Create`, zh: `创建${keyZh}` },