set.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose/lib/service');
  3. const { ObjectId } = require('mongoose').Types;
  4. const assert = require('assert');
  5. // 套餐
  6. class SetService extends CrudService {
  7. constructor(ctx) {
  8. super(ctx, 'set');
  9. this.model = this.ctx.model.Set;
  10. this.xzqh = this.ctx.model.Xzqh;
  11. this.provinceList = [ '110000', '120000', '310000', '500000', '150000', '450000', '640000', '650000', '540000', '810000', '820000' ];
  12. }
  13. /**
  14. * 将数组的id换成对应的套餐名称
  15. * @param {Array} list card数组
  16. */
  17. async changeName(list) {
  18. const ids = list.map(i => ObjectId(i.set));
  19. const setList = await this.model.find({ _id: ids });
  20. list = list.map(i => {
  21. const res = setList.find(f => ObjectId(f._id).equals(i.set));
  22. if (res) i.set = res.title;
  23. return i;
  24. });
  25. return list;
  26. }
  27. /**
  28. *
  29. * @param {Object} condition 查询条件
  30. * @property province 省份代码
  31. */
  32. async range(condition) {
  33. const { contact, ...others } = condition;
  34. assert(contact, '缺少城市范围');
  35. if (this.provinceList.includes(contact)) {
  36. others.contact = contact;
  37. } else {
  38. const xzqh = await this.xzqh.find({ pcode: contact });
  39. others.contact = xzqh.map(i => i.code);
  40. }
  41. const res = await this.model.find(others);
  42. return res;
  43. }
  44. }
  45. module.exports = SetService;