lrf402788946 4 سال پیش
والد
کامیت
4aaebd9855
1فایلهای تغییر یافته به همراه32 افزوده شده و 1 حذف شده
  1. 32 1
      app/service/card.js

+ 32 - 1
app/service/card.js

@@ -130,6 +130,8 @@ class CardService extends CrudService {
     } catch (error) {
       this.logger.error(`积分记录添加失败${JSON.stringify(record)}`);
     }
+    // TODO 添加推荐人等级检查与同级加分
+    this.toCheckRecommender(recommender._id);
     return user;
   }
   /**
@@ -196,7 +198,7 @@ class CardService extends CrudService {
           user.car_show = true;
           user.points = user.points + this.car_point;
           await user.save();
-          // TODO 添加积分记录
+          // 添加积分记录
           const record = _.pick(user, [ 'mobile', 'name' ]);
           record.points = this.car_point;
           record.opera = '车奖';
@@ -227,6 +229,35 @@ class CardService extends CrudService {
     return { b: b_echelon, c: c_echelon_number };
   }
 
+  /**
+   * 添加用户后,检查推荐人是否和他自己的推荐人同级了
+   * @param {String} id 该用户的推荐人id
+   */
+  async toCheckRecommender(id) {
+    const rec = await this.model.findById(id);
+    if (!rec) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到推荐人信息');
+    const { r_mobile, level } = rec;
+    // TODO 如果之给1次,就在这里判断是否给过,给过就不给了
+    // 查出推荐人的推荐人
+    const r_rec = await this.model.findOne({ mobile: r_mobile });
+    if (!r_rec) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到推荐人的推荐人信息');
+    const { level: rlevel } = r_rec;
+    // 推荐人等级已经计算过了,直接比较
+    if (level === rlevel) {
+      rec.points = rec.points + 20;
+      await rec.save();
+    }
+    const record = _.pick(rec, [ 'mobile', 'name' ]);
+    record.points = rec.points + 20;
+    record.opera = '同级奖励';
+    record.params = { name: user.name, mobile: user.mobile };
+      await this.record.create(record);
+    } catch (error) {
+      this.logger.error(`积分记录添加失败${JSON.stringify(record)}`);
+    }
+
+  }
+
   /**
    *  等级设置
    * @property rank 等级:1,业务员;2,经理;3一星经理;4,二星经理;5,三星经理;6,四星经理;