123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const moment = require('moment');
- const _ = require('lodash');
- const fs = require('fs');
- // 工具
- 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) {
- console.log(await this.app.redis.get('5ff575b0a3f2771eb4d03114'));
- // this.initSetSeed();
- // console.log('in function:');
- // const file = await this.ctx.curl('https://vd3.bdstatic.com/mda-kar9n2ip1u508ewq/hd/mda-kar9n2ip1u508ewq.mp4');
- // console.log(file.data);
- // return file.data;
- // const ms = fs.create
- }
- 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;
|