123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- 'use strict';
- const _ = require('lodash');
- const moment = require('moment');
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { ObjectId } = require('mongoose').Types;
- const fs = require('fs');
- class UtilService extends CrudService {
- constructor(ctx) {
- super(ctx);
- this.mq = this.ctx.mq;
- }
- async utilMethod(query, body) {
- console.log('in function: util method');
- }
- /**
- * 整理参数
- * @param {Object} query 参数
- */
- dealQuery(query) {
- return this.turnFilter(this.turnDateRangeQuery(query));
- }
- /**
- * 将查询条件中模糊查询的标识转换成对应object
- * @param {Object} filter 查询条件
- */
- turnFilter(filter) {
- const str = /^%\S*%$/;
- const keys = Object.keys(filter);
- for (const key of keys) {
- const res = key.match(str);
- if (res) {
- const newKey = key.slice(1, key.length - 1);
- filter[newKey] = new RegExp(filter[key]);
- delete filter[key];
- }
- }
- return filter;
- }
- /**
- * 将时间转换成对应查询Object
- * @param {Object} filter 查询条件
- */
- turnDateRangeQuery(filter) {
- const keys = Object.keys(filter);
- for (const k of keys) {
- if (k.includes('@')) {
- const karr = k.split('@');
- // 因为是针对处理范围日期,所以必须只有,开始时间和结束时间
- if (karr.length === 2) {
- const type = karr[1];
- if (type === 'start') {
- filter[karr[0]] = {
- ..._.get(filter, karr[0], {}),
- $gte: filter[k],
- };
- } else {
- filter[karr[0]] = {
- ..._.get(filter, karr[0], {}),
- $lte: filter[k],
- };
- }
- delete filter[k];
- }
- }
- }
- return filter;
- }
- }
- module.exports = UtilService;
|