util.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const moment = require('moment');
  4. const _ = require('lodash');
  5. // 工具
  6. class UtilService extends CrudService {
  7. constructor(ctx) {
  8. super(ctx);
  9. this.model = this.ctx.model.Card; // 卡
  10. this.place = this.ctx.model.Place;// 行政区划(原生数据)
  11. this.xzqh = this.ctx.model.Xzqh;// 行政区划
  12. this.set = this.ctx.model.Set;// 套餐
  13. }
  14. async utilMethod(query, body) {
  15. this.initSetSeed();
  16. }
  17. async initSetSeed() {
  18. let xzqh = await this.xzqh.find({ pcode: { $exists: false } });
  19. xzqh = JSON.parse(JSON.stringify((xzqh)));
  20. console.log(xzqh.length);
  21. const set169 = code => ({ title: '169套餐', has_group: true, contact: code });
  22. const set129 = code => ({ title: '129套餐', has_group: false, contact: code });
  23. const setList = [];
  24. const dirList = [ '110000', '120000', '310000', '500000', '150000', '450000', '640000', '650000', '540000', '810000', '820000' ];
  25. for (const i of xzqh) {
  26. const { code } = i;
  27. if (code !== '110000') {
  28. if (dirList.includes(code)) {
  29. setList.push(set169(code));
  30. setList.push(set129(code));
  31. } else {
  32. const citys = await this.xzqh.find({ pcode: code });
  33. for (const c of citys) {
  34. if (c.code !== '220100') {
  35. setList.push(set169(c.code));
  36. setList.push(set129(c.code));
  37. }
  38. }
  39. }
  40. }
  41. }
  42. this.set.insertMany(setList);
  43. }
  44. // 设置card表的初始数据
  45. async initCardSeed() {
  46. this.seed();
  47. }
  48. async seed() {
  49. // b梯队
  50. const b = [];
  51. for (let i = 1; i <= 5; i++) {
  52. const mobile = i < 10 ? `2222222220${i}` : `222222222${i}`;
  53. const data = {
  54. mobile,
  55. password: '111111',
  56. province: '220000',
  57. city: '220100',
  58. set: '169',
  59. name: `B梯队${i}`,
  60. id_card: '22010319950601161x',
  61. recommend: '刘睿峰',
  62. r_mobile: '13089419810',
  63. };
  64. b.push(data);
  65. await this.ctx.service.card.create(data);
  66. }
  67. // c梯队
  68. const c = [];
  69. let num = 1;
  70. for (const binfo of b) {
  71. const { name, mobile } = binfo;
  72. for (let i = num; i < num + 5; i++) {
  73. const m = i < 10 ? `3333333330${i}` : `333333333${i}`;
  74. const data = {
  75. mobile: m,
  76. password: '111111',
  77. province: '220000',
  78. city: '220100',
  79. set: '169',
  80. id_card: '22010319950601161x',
  81. name: `C梯队${i}`,
  82. recommend: name,
  83. r_mobile: mobile,
  84. };
  85. c.push(data);
  86. await this.ctx.service.card.create(data);
  87. }
  88. num = num + 5;
  89. }
  90. }
  91. // 整理查询条件,重写query时可以选择使用,框架方法改了下
  92. queryReset(filter, options = {}) {
  93. let { sort, desc } = options;
  94. if (sort && _.isString(sort)) {
  95. sort = { [sort]: desc ? -1 : 1 };
  96. } else if (sort && _.isArray(sort)) {
  97. sort = sort.map(f => ({ [f]: desc ? -1 : 1 }))
  98. .reduce((p, c) => ({ ...p, ...c }), {});
  99. }
  100. options.sort = sort;
  101. // 模糊查询
  102. filter = this.turnFilter(filter);
  103. // 日期范围查询
  104. filter = this.turnDateRangeQuery(filter);
  105. return { filter, options };
  106. }
  107. turnFilter(filter) {
  108. const str = /^%\w*%$/;
  109. const keys = Object.keys(filter);
  110. for (const key of keys) {
  111. const res = key.match(str);
  112. if (res) {
  113. const newKey = key.slice(1, key.length - 1);
  114. filter[newKey] = new RegExp(filter[key]);
  115. delete filter[key];
  116. }
  117. }
  118. return filter;
  119. }
  120. /**
  121. * 将时间转换成对应查询Object
  122. * @param {Object} filter 查询条件
  123. */
  124. turnDateRangeQuery(filter) {
  125. const keys = Object.keys(filter);
  126. for (const k of keys) {
  127. if (k.includes('@')) {
  128. const karr = k.split('@');
  129. // 因为是针对处理范围日期,所以必须只有,开始时间和结束时间
  130. if (karr.length === 2) {
  131. const type = karr[1];
  132. if (type === 'start') {
  133. filter[karr[0]] = {
  134. ..._.get(filter, karr[0], {}),
  135. $gte: filter[k],
  136. };
  137. } else {
  138. filter[karr[0]] = {
  139. ..._.get(filter, karr[0], {}),
  140. $lte: filter[k],
  141. };
  142. }
  143. delete filter[k];
  144. }
  145. }
  146. }
  147. return filter;
  148. }
  149. /**
  150. * 整理行政区划
  151. */
  152. resetXzqh() {
  153. // let res = await this.place.find();
  154. // res = JSON.parse(JSON.stringify(res));
  155. // const province = res.filter(f => f.code.endsWith('0000'));
  156. // let cList = [];
  157. // for (const p of province) {
  158. // const { code } = p;
  159. // const prefix = code.substr(0, 2);
  160. // let city = res.filter(f => f.code.startsWith(prefix) && !f.code.endsWith('0000') && f.code.endsWith('00'));
  161. // city = city.map(i => ({ ...i, pcode: code }));
  162. // cList = cList.concat(city);
  163. // }
  164. // await this.xzqh.insertMany([ ...province, ...cList ]);
  165. // return province;
  166. }
  167. }
  168. module.exports = UtilService;