Browse Source

添加套餐,修改办卡时判断推广模式标准

lrf402788946 4 years ago
parent
commit
5acf0f2782

+ 23 - 2
app/controller/.card.js

@@ -13,7 +13,7 @@ module.exports = {
       "points",
       "points",
       "recommend",
       "recommend",
       "r_mobile",
       "r_mobile",
-      "zf",
+      "wxaccount",
       "create_time",
       "create_time",
     ],
     ],
   },
   },
@@ -36,7 +36,7 @@ module.exports = {
       "points",
       "points",
       "recommend",
       "recommend",
       "r_mobile",
       "r_mobile",
-      "zf",
+      "wxaccount",
       "create_time",
       "create_time",
     ],
     ],
   },
   },
@@ -60,6 +60,8 @@ module.exports = {
         r_mobile: "r_mobile",
         r_mobile: "r_mobile",
         car_show: "car_show",
         car_show: "car_show",
         stockholder: "stockholder",
         stockholder: "stockholder",
+        wxaccount: "%wxaccount%",
+        set: "set",
         "create_time@start": "create_time@start",
         "create_time@start": "create_time@start",
         "create_time@end": "create_time@end",
         "create_time@end": "create_time@end",
       },
       },
@@ -78,4 +80,23 @@ module.exports = {
     requestBody: ["!password"],
     requestBody: ["!password"],
     service: "passwd",
     service: "passwd",
   },
   },
+
+  group: {
+    parameters: {
+      query: {
+        r_mobile: "r_mobile",
+        province: "province",
+        set: "set",
+        "create_time@start": "create_time@start",
+        "create_time@end": "create_time@end",
+      },
+    },
+    service: "group",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      // count: true,
+    },
+  },
 };
 };

+ 37 - 0
app/controller/.set.js

@@ -0,0 +1,37 @@
+module.exports = {
+  create: {
+    requestBody: ["title", "contact", "has_group", "remark"],
+  },
+  destory: {
+    params: ["!id"],
+    service: "delete",
+  },
+  update: {
+    params: ["!id"],
+    requestBody: ["title", "contact", "has_group", "remark"],
+  },
+  show: {
+    parameters: {
+      params: ["!id"],
+    },
+    service: "fetch",
+  },
+  index: {
+    parameters: {
+      query: {
+        title: "%title%",
+        contact: "contact",
+        has_group: "has_group",
+        "create_time@start": "create_time@start",
+        "create_time@end": "create_time@end",
+      },
+    },
+    service: "query",
+    options: {
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 7 - 0
app/controller/card.js

@@ -9,5 +9,12 @@ class CardController extends Controller {
     super(ctx);
     super(ctx);
     this.service = this.ctx.service.card;
     this.service = this.ctx.service.card;
   }
   }
+
+  async group() {
+    let { total, list } = await this.service.group(this.ctx.query);
+    list = await this.ctx.service.set.changeName(list);
+    this.ctx.ok({ data: list, total });
+  }
+
 }
 }
 module.exports = CrudController(CardController, meta);
 module.exports = CrudController(CardController, meta);

+ 18 - 0
app/controller/set.js

@@ -0,0 +1,18 @@
+'use strict';
+const meta = require('./.set.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose/lib/controller');
+
+// 套餐
+class SetController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.set;
+  }
+
+  async range() {
+    const res = await this.service.range(this.ctx.query);
+    this.ctx.ok({ data: res });
+  }
+}
+module.exports = CrudController(SetController, meta);

+ 2 - 8
app/model/card.js

@@ -4,18 +4,12 @@ const moment = require('moment');
 const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 const { Secret } = require('naf-framework-mongoose/lib/model/schema');
 const { Secret } = require('naf-framework-mongoose/lib/model/schema');
 
 
-const zf = new Schema({
-  mobile: { type: String, maxLength: 200 },
-  account: { type: String, maxLength: 200 },
-  name: { type: String, maxLength: 200 },
-  remark: { type: String, maxLength: 200 },
-});
-
 //  办卡表
 //  办卡表
 const card = {
 const card = {
   mobile: { type: String, required: true, maxLength: 11, unique: true }, // 电话
   mobile: { type: String, required: true, maxLength: 11, unique: true }, // 电话
   password: { type: Secret, required: true, select: false }, // 密码
   password: { type: Secret, required: true, select: false }, // 密码
   province: { type: String, required: true }, // 省份
   province: { type: String, required: true }, // 省份
+  city: { type: String }, // 省份
   set: { type: String, required: true }, // 169/129 套餐
   set: { type: String, required: true }, // 169/129 套餐
   pay_type: { type: String }, // 支付方式
   pay_type: { type: String }, // 支付方式
   name: { type: String, required: true }, // 姓名
   name: { type: String, required: true }, // 姓名
@@ -24,9 +18,9 @@ const card = {
   points: { type: Number, default: 600 }, // 积分
   points: { type: Number, default: 600 }, // 积分
   recommend: { type: String }, // 推荐人
   recommend: { type: String }, // 推荐人
   r_mobile: { type: String }, // 推荐人电话
   r_mobile: { type: String }, // 推荐人电话
-  zf: { type: zf }, // 支付设置
   car_show: { type: Boolean, default: false }, // 车奖
   car_show: { type: Boolean, default: false }, // 车奖
   stockholder: { type: Boolean, default: false }, // 股东
   stockholder: { type: Boolean, default: false }, // 股东
+  wxaccount: { type: String }, // 微信号(支付账号)
   create_time: {
   create_time: {
     type: String,
     type: String,
     default: moment().format('YYYY-MM-DD HH:mm:ss'),
     default: moment().format('YYYY-MM-DD HH:mm:ss'),

+ 19 - 0
app/model/set.js

@@ -0,0 +1,19 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const moment = require('moment');
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+// 套餐表
+const set = {
+  title: { type: String }, // 套餐标题
+  contact: { type: String }, // 关联的市/直辖市 的code
+  has_group: { type: Boolean, default: false }, // 是否有推广制度
+  remark: { type: String, maxLength: 200 },
+  create_time: { type: String, default: moment().format('YYYY-MM-DD HH:mm:ss') },
+};
+const schema = new Schema(set, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Set', schema, 'set');
+};

+ 1 - 0
app/router.js

@@ -19,4 +19,5 @@ module.exports = app => {
   require('./router/count')(app); // 统计
   require('./router/count')(app); // 统计
   require('./router/carousel')(app); // 轮播
   require('./router/carousel')(app); // 轮播
   require('./router/xzqh')(app); // 行政区划
   require('./router/xzqh')(app); // 行政区划
+  require('./router/set')(app); // 套餐
 };
 };

+ 1 - 0
app/router/card.js

@@ -6,6 +6,7 @@ module.exports = app => {
   const prefix = '/api/htyd';
   const prefix = '/api/htyd';
   const index = 'card';
   const index = 'card';
   const { router, controller } = app;
   const { router, controller } = app;
+  router.get(index, `${prefix}/${index}/group`, controller[index].group);
   router.post(index, `${prefix}/${index}/passwd/:id`, controller[index].passwd);
   router.post(index, `${prefix}/${index}/passwd/:id`, controller[index].passwd);
   router.resources(index, `${prefix}/${index}`, controller[index]); // index、create、show、destroy
   router.resources(index, `${prefix}/${index}`, controller[index]); // index、create、show、destroy
   router.post(index, `${prefix}/${index}/update/:id`, controller[index].update);
   router.post(index, `${prefix}/${index}/update/:id`, controller[index].update);

+ 12 - 0
app/router/set.js

@@ -0,0 +1,12 @@
+'use strict';
+/**
+ * @param {Egg.Application} app - egg application
+ */
+module.exports = app => {
+  const prefix = '/api/htyd';
+  const index = 'set';
+  const { router, controller } = app;
+  router.get(index, `${prefix}/${index}/range`, controller[index].range);
+  router.resources(index, `${prefix}/${index}`, controller[index]); // index、create、show、destroy
+  router.post(index, `${prefix}/${index}/update/:id`, controller[index].update);
+};

+ 4 - 2
app/router/xzqh.js

@@ -6,6 +6,8 @@ module.exports = app => {
   const prefix = '/api/htyd';
   const prefix = '/api/htyd';
   const index = 'xzqh';
   const index = 'xzqh';
   const { router, controller } = app;
   const { router, controller } = app;
-  router.resources(index, `${prefix}/${index}`, controller[index]); // index、create、show、destroy
-  router.post(index, `${prefix}/${index}/update/:id`, controller[index].update);
+  // router.resources(index, `${prefix}/${index}`, controller[index]); // index、create、show、destroy
+  // router.post(index, `${prefix}/${index}/update/:id`, controller[index].update);
+  router.get(index, `${prefix}/${index}`, controller[index].index); // index、create、show、destroy
+
 };
 };

+ 36 - 20
app/service/card.js

@@ -17,6 +17,7 @@ class CardService extends CrudService {
     super(ctx, 'card');
     super(ctx, 'card');
     this.model = this.ctx.model.Card;
     this.model = this.ctx.model.Card;
     this.record = this.ctx.model.Record;
     this.record = this.ctx.model.Record;
+    this.set = this.ctx.model.Set;
     /**
     /**
      * @constant Number 车奖的积分 default:131400
      * @constant Number 车奖的积分 default:131400
      */
      */
@@ -46,16 +47,6 @@ class CardService extends CrudService {
      */
      */
     this.car_show_b_limit_person = 5;
     this.car_show_b_limit_person = 5;
   }
   }
-  /**
-   * 修改密码
-   * @param {Object} param 参数
-   */
-  async passwd({ id, password }) {
-    password = { secret: password };
-    const res = await this.model.update({ _id: ObjectId(id) }, { password });
-    return res;
-  }
-
   /**
   /**
    * 1创建卡用户;2检查推荐人
    * 1创建卡用户;2检查推荐人
    * @param {Object} data 参数
    * @param {Object} data 参数
@@ -89,8 +80,11 @@ class CardService extends CrudService {
         '推荐人信息错误,用户创建失败!'
         '推荐人信息错误,用户创建失败!'
       );
       );
     }
     }
-    // 4,判断是129套餐,还是169套餐,如果是129套餐,那就不需要继续了
-    if (set === '129') return user;
+    // 4,判断是套餐有没有推荐制度(此处因为套餐可维护,修改了)
+    const setInfo = await this.set.findById(set);
+    if (!setInfo) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到选择的套餐信息!');
+    const { has_group } = setInfo;
+    if (!has_group) return user;
     // 5,根据等级,找下推荐人应该加多少分
     // 5,根据等级,找下推荐人应该加多少分
     const { stockholder, level } = recommender;
     const { stockholder, level } = recommender;
     // 6,重新计算推荐人用户等级
     // 6,重新计算推荐人用户等级
@@ -128,7 +122,7 @@ class CardService extends CrudService {
     try {
     try {
       await this.record.create(record);
       await this.record.create(record);
     } catch (error) {
     } catch (error) {
-      this.logger.error(`line-131-积分记录添加失败${JSON.stringify(record)}`);
+      this.logger.error(`line-121-积分记录添加失败${JSON.stringify(record)}`);
     }
     }
     // 9,以上为用户创建,计算推荐人等级=>计算推荐人积分=>生成记录
     // 9,以上为用户创建,计算推荐人等级=>计算推荐人积分=>生成记录
     //         先算等级,避免分数基础部队
     //         先算等级,避免分数基础部队
@@ -185,7 +179,7 @@ class CardService extends CrudService {
       const group_number = b.length + c;
       const group_number = b.length + c;
       return group_number > this.stockholder_limit;
       return group_number > this.stockholder_limit;
     } catch (error) {
     } catch (error) {
-      this.ctx.logger.error(`line-188-${moment().format('YYYY-MM-DD HH:mm:ss')}-${user.name}-${user.mobile}:股东检查出错`);
+      this.ctx.logger.error(`line-178-${moment().format('YYYY-MM-DD HH:mm:ss')}-${user.name}-${user.mobile}:股东检查出错`);
       return undefined;
       return undefined;
     }
     }
   }
   }
@@ -209,12 +203,12 @@ class CardService extends CrudService {
           try {
           try {
             await this.record.create(record);
             await this.record.create(record);
           } catch (error) {
           } catch (error) {
-            this.logger.error(`line-212-积分记录添加失败${JSON.stringify(record)}`);
+            this.logger.error(`line-202-积分记录添加失败${JSON.stringify(record)}`);
           }
           }
         }
         }
       }
       }
     } catch (error) {
     } catch (error) {
-      this.ctx.logger.error(`line-217-${moment().format('YYYY-MM-DD HH:mm:ss')}-车奖检查出错`);
+      this.ctx.logger.error(`line-207-${moment().format('YYYY-MM-DD HH:mm:ss')}-车奖检查出错`);
       return undefined;
       return undefined;
     }
     }
 
 
@@ -257,10 +251,10 @@ class CardService extends CrudService {
         record.opera = '同级奖励';
         record.opera = '同级奖励';
         await this.record.create(record);
         await this.record.create(record);
       } catch (error) {
       } catch (error) {
-        this.logger.error(`line-260-积分记录添加失败${JSON.stringify(record)}`);
+        this.logger.error(`line-250-积分记录添加失败${JSON.stringify(record)}`);
       }
       }
     } catch (error) {
     } catch (error) {
-      this.logger.error(`line-263-捕获检查用户推荐人的推荐人失败:${JSON.stringify(error)}`);
+      this.logger.error(`line-253-捕获检查用户推荐人的推荐人失败:${JSON.stringify(error)}`);
     }
     }
 
 
   }
   }
@@ -289,10 +283,10 @@ class CardService extends CrudService {
         record.opera = '直推下级完成业绩反佣金';
         record.opera = '直推下级完成业绩反佣金';
         await this.record.create(record);
         await this.record.create(record);
       } catch (error) {
       } catch (error) {
-        this.logger.error(`line-292-积分记录添加失败${JSON.stringify(record)}`);
+        this.logger.error(`line-282-积分记录添加失败${JSON.stringify(record)}`);
       }
       }
     } catch (error) {
     } catch (error) {
-      this.logger.error(`line-295-捕获检查用户推荐人的推荐人失败:${JSON.stringify(error)}`);
+      this.logger.error(`line-285-捕获检查用户推荐人的推荐人失败:${JSON.stringify(error)}`);
     }
     }
   }
   }
 
 
@@ -376,6 +370,28 @@ class CardService extends CrudService {
     if (!obj) throw new BusinessError(ErrorCode.SERVICE_FAULT, '为找到推荐人适合的规则');
     if (!obj) throw new BusinessError(ErrorCode.SERVICE_FAULT, '为找到推荐人适合的规则');
     return _.get(obj, r_level);
     return _.get(obj, r_level);
   }
   }
+
+  /**
+   * 修改密码
+   * @param {Object} param 参数
+   */
+  async passwd({ id, password }) {
+    password = { secret: password };
+    const res = await this.model.update({ _id: ObjectId(id) }, { password });
+    return res;
+  }
+
+  /**
+   * 根据条件查询该用户的团队人员
+   * @param {Object} query 查询条件
+   */
+  async group(query) {
+    const condition = this.ctx.service.util.queryReset(query);
+    const { filter } = condition;
+    const bTerm = await this.model.find(filter);
+    const cTerm = await this.model.find({ r_mobile: bTerm.map(i => i.mobile) });
+    return { list: [ ...bTerm, ...cTerm ], total: bTerm.length + cTerm.length };
+  }
 }
 }
 
 
 module.exports = CardService;
 module.exports = CardService;

+ 52 - 0
app/service/set.js

@@ -0,0 +1,52 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { ObjectId } = require('mongoose').Types;
+const assert = require('assert');
+
+// 套餐
+class SetService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'set');
+    this.model = this.ctx.model.Set;
+    this.xzqh = this.ctx.model.Xzqh;
+    this.provinceList = [ '110000', '120000', '310000', '500000', '150000', '450000', '640000', '650000', '540000', '810000', '820000' ];
+
+  }
+
+  /**
+   * 将数组的id换成对应的套餐名称
+   * @param {Array} list card数组
+   */
+  async changeName(list) {
+    const ids = list.map(i => ObjectId(i.set));
+    const setList = await this.model.find({ _id: ids });
+    list = list.map(i => {
+      const res = setList.find(f => ObjectId(f._id).equals(i.set));
+      if (res) i.set = res.title;
+      return i;
+    });
+    return list;
+  }
+
+  /**
+   *
+   * @param {Object} condition 查询条件
+   * @property province 省份代码
+   */
+  async range(condition) {
+    const { contact, ...others } = condition;
+    assert(contact, '缺少城市范围');
+    console.log(contact);
+    if (this.provinceList.includes(contact)) {
+      others.contact = contact;
+    } else {
+      const xzqh = await this.xzqh.find({ pcode: contact });
+      others.contact = xzqh.map(i => i.code);
+    }
+    console.log(others);
+    const res = await this.model.find(others);
+    return res;
+  }
+}
+
+module.exports = SetService;

+ 61 - 19
app/service/util.js

@@ -7,25 +7,47 @@ const _ = require('lodash');
 class UtilService extends CrudService {
 class UtilService extends CrudService {
   constructor(ctx) {
   constructor(ctx) {
     super(ctx);
     super(ctx);
-    this.model = this.ctx.model.Card;
-    this.place = this.ctx.model.Place;
-    this.xzqh = this.ctx.model.Xzqh;
+    this.model = this.ctx.model.Card; // 卡
+    this.place = this.ctx.model.Place;// 行政区划(原生数据)
+    this.xzqh = this.ctx.model.Xzqh;// 行政区划
+    this.set = this.ctx.model.Set;// 套餐
   }
   }
   async utilMethod(query, body) {
   async utilMethod(query, body) {
-    // this.seed();
-    let res = await this.place.find();
-    res = JSON.parse(JSON.stringify(res));
-    const province = res.filter(f => f.code.endsWith('0000'));
-    let cList = [];
-    for (const p of province) {
-      const { code } = p;
-      const prefix = code.substr(0, 2);
-      let city = res.filter(f => f.code.startsWith(prefix) && !f.code.endsWith('0000') && f.code.endsWith('00'));
-      city = city.map(i => ({ ...i, pcode: code }));
-      cList = cList.concat(city);
+    this.initSetSeed();
+  }
+
+  async initSetSeed() {
+    let xzqh = await this.xzqh.find({ pcode: { $exists: false } });
+    xzqh = JSON.parse(JSON.stringify((xzqh)));
+    console.log(xzqh.length);
+    const set169 = code => ({ title: '169套餐', has_group: true, contact: code });
+    const set129 = code => ({ title: '129套餐', has_group: false, contact: code });
+    const setList = [];
+    const dirList = [ '110000', '120000', '310000', '500000', '150000', '450000', '640000', '650000', '540000', '810000', '820000' ];
+    for (const i of xzqh) {
+      const { code } = i;
+      if (code !== '110000') {
+        if (dirList.includes(code)) {
+          setList.push(set169(code));
+          setList.push(set129(code));
+        } else {
+          const citys = await this.xzqh.find({ pcode: code });
+          for (const c of citys) {
+            if (c.code !== '220100') {
+              setList.push(set169(c.code));
+              setList.push(set129(c.code));
+            }
+
+          }
+        }
+      }
     }
     }
-    await this.xzqh.insertMany([ ...province, ...cList ]);
-    // return province;
+    this.set.insertMany(setList);
+  }
+
+  // 设置card表的初始数据
+  async initCardSeed() {
+    this.seed();
   }
   }
 
 
   async seed() {
   async seed() {
@@ -38,7 +60,7 @@ class UtilService extends CrudService {
         password: '111111',
         password: '111111',
         province: '220000',
         province: '220000',
         city: '220100',
         city: '220100',
-        set: '168',
+        set: '169',
         name: `B梯队${i}`,
         name: `B梯队${i}`,
         id_card: '22010319950601161x',
         id_card: '22010319950601161x',
         recommend: '刘睿峰',
         recommend: '刘睿峰',
@@ -59,7 +81,7 @@ class UtilService extends CrudService {
           password: '111111',
           password: '111111',
           province: '220000',
           province: '220000',
           city: '220100',
           city: '220100',
-          set: '168',
+          set: '169',
           id_card: '22010319950601161x',
           id_card: '22010319950601161x',
           name: `C梯队${i}`,
           name: `C梯队${i}`,
           recommend: name,
           recommend: name,
@@ -72,7 +94,8 @@ class UtilService extends CrudService {
     }
     }
   }
   }
 
 
-  queryReset(filter, options) {
+  // 整理查询条件,重写query时可以选择使用,框架方法改了下
+  queryReset(filter, options = {}) {
     let { sort, desc } = options;
     let { sort, desc } = options;
     if (sort && _.isString(sort)) {
     if (sort && _.isString(sort)) {
       sort = { [sort]: desc ? -1 : 1 };
       sort = { [sort]: desc ? -1 : 1 };
@@ -130,6 +153,25 @@ class UtilService extends CrudService {
     }
     }
     return filter;
     return filter;
   }
   }
+
+  /**
+   * 整理行政区划
+   */
+  resetXzqh() {
+    // let res = await this.place.find();
+    // res = JSON.parse(JSON.stringify(res));
+    // const province = res.filter(f => f.code.endsWith('0000'));
+    // let cList = [];
+    // for (const p of province) {
+    //   const { code } = p;
+    //   const prefix = code.substr(0, 2);
+    //   let city = res.filter(f => f.code.startsWith(prefix) && !f.code.endsWith('0000') && f.code.endsWith('00'));
+    //   city = city.map(i => ({ ...i, pcode: code }));
+    //   cList = cList.concat(city);
+    // }
+    // await this.xzqh.insertMany([ ...province, ...cList ]);
+    // return province;
+  }
 }
 }
 
 
 module.exports = UtilService;
 module.exports = UtilService;