util.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. const _ = require('lodash');
  3. const moment = require('moment');
  4. const { CrudService } = require('naf-framework-mongoose/lib/service');
  5. const { ObjectId } = require('mongoose').Types;
  6. const fs = require('fs');
  7. class UtilService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx);
  10. this.mq = this.ctx.mq;
  11. }
  12. async utilMethod(query, body) {
  13. console.log('in function: util method');
  14. }
  15. /**
  16. * 整理参数
  17. * @param {Object} query 参数
  18. */
  19. dealQuery(query) {
  20. return this.turnFilter(this.turnDateRangeQuery(query));
  21. }
  22. /**
  23. * 将查询条件中模糊查询的标识转换成对应object
  24. * @param {Object} filter 查询条件
  25. */
  26. turnFilter(filter) {
  27. const str = /^%\S*%$/;
  28. const keys = Object.keys(filter);
  29. for (const key of keys) {
  30. const res = key.match(str);
  31. if (res) {
  32. const newKey = key.slice(1, key.length - 1);
  33. filter[newKey] = new RegExp(filter[key]);
  34. delete filter[key];
  35. }
  36. }
  37. return filter;
  38. }
  39. /**
  40. * 将时间转换成对应查询Object
  41. * @param {Object} filter 查询条件
  42. */
  43. turnDateRangeQuery(filter) {
  44. const keys = Object.keys(filter);
  45. for (const k of keys) {
  46. if (k.includes('@')) {
  47. const karr = k.split('@');
  48. // 因为是针对处理范围日期,所以必须只有,开始时间和结束时间
  49. if (karr.length === 2) {
  50. const type = karr[1];
  51. if (type === 'start') {
  52. filter[karr[0]] = {
  53. ..._.get(filter, karr[0], {}),
  54. $gte: filter[k],
  55. };
  56. } else {
  57. filter[karr[0]] = {
  58. ..._.get(filter, karr[0], {}),
  59. $lte: filter[k],
  60. };
  61. }
  62. delete filter[k];
  63. }
  64. }
  65. }
  66. return filter;
  67. }
  68. }
  69. module.exports = UtilService;