'use strict'; const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; const _ = require('lodash'); const assert = require('assert'); const moment = require('moment'); // 定位信息 class PositionService extends CrudService { constructor(ctx) { super(ctx, 'position'); this.model = this.ctx.model.Position; this.util = this.ctx.service.util; } // async query(filter, { skip, limit, sort, desc, projection } = {}) { // // 处理排序 // 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 }), {}); // } // let condition = _.cloneDeep(filter); // condition = this.util.dealFilter(condition); // const pipeline = [{ $match: condition }]; // // 先排序 // if (sort) pipeline.push({ $sort: sort }); // // 再进行分页 // if (skip && limit) { // pipeline.push({ $skip: parseInt(skip) }); // pipeline.push({ $limit: parseInt(limit) }); // } // // 再将数据过滤 // if (projection) pipeline.push({ $project: projection }); // // console.log(JSON.stringify(pipeline)); // // const query = [ // // { $addFields: { time: '$meta.createdAt' } }, // // { $match: { time: { $gte: new Date('2021-12-03 12:45:00'), $lte: new Date('2021-12-03 12:45:08') } } }, // // { $skip: 0 }, // // { $limit: 1 }, // // ]; // const rs = await this.model.aggregate(pipeline); // // const rs = await this.model.find(trimData(condition), projection, { skip, limit, sort }).exec(); // for (const i of rs) { // const { time } = i; // console.log(typeof time); // } // return rs; // } async map(condition = {}) { // 当前时间 const format = 'YYYY-MM-DD HH:mm:ss'; const end = moment().format(format); const start = moment(end).subtract(2, 'm').format(format); condition['meta.createdAt@start'] = start; condition['meta.createdAt@end'] = end; condition = this.util.dealQuery(condition); const data = await this.model.aggregate([ { $sort: { 'meta.createdAt': -1 } }, // 倒序排序 { $match: { ...condition } }, // 只取出第一个数据 { $group: { _id: '$user_id', data_id: { $first: '$_id' }, user_id: { $first: '$user_id' }, company_id: { $first: '$company_id' }, company_name: { $first: '$company_name' }, company_service_object_id: { $first: '$company_service_object_id' }, company_service_object_name: { $first: '$company_service_object_name' }, longitude: { $first: '$longitude' }, latitude: { $first: '$latitude' }, name: { $first: '$name' }, card: { $first: '$card' }, is_class: { $first: '$is_class' }, }, }, ]); return data; } async delete({ id }) { console.log(id); const req = this.ctx.request; const dkey = _.get(req, 'header.dkey'); if (dkey !== 'free') { throw new BusinessError(ErrorCode.SERVICE_FAULT, '您谁,不认识,这边不行,要不您去数据库删吧!'); } return this.model.deleteOne({ _id: id }); } } module.exports = PositionService;