position.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const moment = require('moment');
  7. // 定位信息
  8. class PositionService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'position');
  11. this.model = this.ctx.model.Position;
  12. this.util = this.ctx.service.util;
  13. }
  14. // async query(filter, { skip, limit, sort, desc, projection } = {}) {
  15. // // 处理排序
  16. // if (sort && _.isString(sort)) {
  17. // sort = { [sort]: desc ? -1 : 1 };
  18. // } else if (sort && _.isArray(sort)) {
  19. // sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
  20. // }
  21. // let condition = _.cloneDeep(filter);
  22. // condition = this.util.dealFilter(condition);
  23. // const pipeline = [{ $match: condition }];
  24. // // 先排序
  25. // if (sort) pipeline.push({ $sort: sort });
  26. // // 再进行分页
  27. // if (skip && limit) {
  28. // pipeline.push({ $skip: parseInt(skip) });
  29. // pipeline.push({ $limit: parseInt(limit) });
  30. // }
  31. // // 再将数据过滤
  32. // if (projection) pipeline.push({ $project: projection });
  33. // // console.log(JSON.stringify(pipeline));
  34. // // const query = [
  35. // // { $addFields: { time: '$meta.createdAt' } },
  36. // // { $match: { time: { $gte: new Date('2021-12-03 12:45:00'), $lte: new Date('2021-12-03 12:45:08') } } },
  37. // // { $skip: 0 },
  38. // // { $limit: 1 },
  39. // // ];
  40. // const rs = await this.model.aggregate(pipeline);
  41. // // const rs = await this.model.find(trimData(condition), projection, { skip, limit, sort }).exec();
  42. // for (const i of rs) {
  43. // const { time } = i;
  44. // console.log(typeof time);
  45. // }
  46. // return rs;
  47. // }
  48. async map(condition = {}) {
  49. // 当前时间
  50. const format = 'YYYY-MM-DD HH:mm:ss';
  51. const end = moment().format(format);
  52. const start = moment(end).subtract(2, 'm').format(format);
  53. condition['meta.createdAt@start'] = start;
  54. condition['meta.createdAt@end'] = end;
  55. condition = this.util.dealQuery(condition);
  56. const data = await this.model.aggregate([
  57. { $sort: { 'meta.createdAt': -1 } }, // 倒序排序
  58. { $match: { ...condition } },
  59. // 只取出第一个数据
  60. {
  61. $group: {
  62. _id: '$user_id',
  63. data_id: { $first: '$_id' },
  64. user_id: { $first: '$user_id' },
  65. company_id: { $first: '$company_id' },
  66. company_name: { $first: '$company_name' },
  67. company_service_object_id: { $first: '$company_service_object_id' },
  68. company_service_object_name: { $first: '$company_service_object_name' },
  69. longitude: { $first: '$longitude' },
  70. latitude: { $first: '$latitude' },
  71. name: { $first: '$name' },
  72. card: { $first: '$card' },
  73. is_class: { $first: '$is_class' },
  74. },
  75. },
  76. ]);
  77. return data;
  78. }
  79. async delete({ id }) {
  80. console.log(id);
  81. const req = this.ctx.request;
  82. const dkey = _.get(req, 'header.dkey');
  83. if (dkey !== 'free') {
  84. throw new BusinessError(ErrorCode.SERVICE_FAULT, '您谁,不认识,这边不行,要不您去数据库删吧!');
  85. }
  86. return this.model.deleteOne({ _id: id });
  87. }
  88. }
  89. module.exports = PositionService;