'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;// 行政区划 this.set = this.ctx.model.Set;// 套餐 } async utilMethod(query, body) { // this.initSetSeed(); } async initSetSeed() { let xzqh = await this.xzqh.find({ pcode: { $exists: false } }); xzqh = JSON.parse(JSON.stringify((xzqh))); const set169 = code => ({ title: '169套餐', has_group: true, contact: code }); const set129 = code => ({ title: '129套餐', has_group: false, contact: code }); const setList = []; const dirList = [ '110000', '120000', '310000', '500000', '150000', '450000', '640000', '650000', '540000', '810000', '820000' ]; for (const i of xzqh) { const { code } = i; if (code !== '110000') { if (dirList.includes(code)) { setList.push(set169(code)); setList.push(set129(code)); } else { const citys = await this.xzqh.find({ pcode: code }); for (const c of citys) { if (c.code !== '220100') { setList.push(set169(c.code)); setList.push(set129(c.code)); } } } } } this.set.insertMany(setList); } // 设置card表的初始数据 async initCardSeed() { this.seed(); } async seed() { // b梯队 const b = []; for (let i = 1; i <= 5; i++) { const mobile = i < 10 ? `2222222220${i}` : `222222222${i}`; const data = { mobile, password: '111111', province: '220000', city: '220100', set: '169', name: `B梯队${i}`, id_card: '22010319950601161x', recommend: '刘睿峰', r_mobile: '13089419810', }; b.push(data); await this.ctx.service.card.create(data); } // c梯队 const c = []; let num = 1; for (const binfo of b) { const { name, mobile } = binfo; for (let i = num; i < num + 5; i++) { const m = i < 10 ? `3333333330${i}` : `333333333${i}`; const data = { mobile: m, password: '111111', province: '220000', city: '220100', set: '169', id_card: '22010319950601161x', name: `C梯队${i}`, recommend: name, r_mobile: mobile, }; c.push(data); await this.ctx.service.card.create(data); } num = num + 5; } } // 整理查询条件,重写query时可以选择使用,框架方法改了下 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; } /** * 整理行政区划 */ resetXzqh() { // 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; } } module.exports = UtilService;