|
@@ -1,15 +1,31 @@
|
|
|
'use strict';
|
|
|
const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
const moment = require('moment');
|
|
|
+const _ = require('lodash');
|
|
|
|
|
|
// 工具
|
|
|
class UtilService extends CrudService {
|
|
|
constructor(ctx) {
|
|
|
super(ctx);
|
|
|
this.model = this.ctx.model.Card;
|
|
|
+ this.place = this.ctx.model.Place;
|
|
|
+ this.xzqh = this.ctx.model.Xzqh;
|
|
|
}
|
|
|
async utilMethod(query, body) {
|
|
|
- this.seed();
|
|
|
+ // 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);
|
|
|
+ }
|
|
|
+ await this.xzqh.insertMany([ ...province, ...cList ]);
|
|
|
+ // return province;
|
|
|
}
|
|
|
|
|
|
async seed() {
|
|
@@ -55,6 +71,65 @@ class UtilService extends CrudService {
|
|
|
num = num + 5;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ queryReset(filter, options) {
|
|
|
+ let { sort, desc } = options;
|
|
|
+ if (sort && _.isString(sort)) {
|
|
|
+ sort = { [sort]: desc ? -1 : 1 };
|
|
|
+ } else if (sort && _.isArray(sort)) {
|
|
|
+ sort = sort.map(f => ({ [f]: desc ? -1 : 1 }))
|
|
|
+ .reduce((p, c) => ({ ...p, ...c }), {});
|
|
|
+ }
|
|
|
+ options.sort = sort;
|
|
|
+ // 模糊查询
|
|
|
+ filter = this.turnFilter(filter);
|
|
|
+ // 日期范围查询
|
|
|
+ filter = this.turnDateRangeQuery(filter);
|
|
|
+ return { filter, options };
|
|
|
+ }
|
|
|
+
|
|
|
+ turnFilter(filter) {
|
|
|
+ const str = /^%\w*%$/;
|
|
|
+ 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;
|