valueService.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. 'use strict';
  2. const Service = require('../service/baseService');
  3. class ValueService extends Service {
  4. tag() {
  5. return this.ctx.model.ValueModel;
  6. }
  7. async userValueDetails(query) {
  8. const { ctx } = this;
  9. const { model } = ctx;
  10. const { id } = query;
  11. if (id) {
  12. const valueInfoList = await model.ValueModel.aggregate([
  13. { $match: { userid: this.app.mongoose.Types.ObjectId(id), type: '0' } },
  14. { $lookup: { from: 'info', localField: 'infoId', foreignField: '_id', as: 'infos' } },
  15. { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
  16. { $project: {
  17. _id: 0,
  18. oldName: '$infos.name',
  19. time: '$infos.time',
  20. } },
  21. { $sort: { time: -1 } },
  22. ]).allowDiskUse(true);// 允许使用磁盘缓存
  23. const valueVisitList = await model.ValueModel.aggregate([
  24. { $match: { userid: this.app.mongoose.Types.ObjectId(id), type: '1' } },
  25. { $lookup: { from: 'info', localField: 'infoId', foreignField: '_id', as: 'infos' } },
  26. { $unwind: { path: '$infos', preserveNullAndEmptyArrays: true } },
  27. { $project: {
  28. _id: 0,
  29. oldName: '$infos.name',
  30. time: '$time',
  31. } },
  32. { $sort: { time: -1 } },
  33. ]).allowDiskUse(true);// 允许使用磁盘缓存
  34. const valueInfoCount = await model.ValueModel.find({ userid: id, type: '0' }).countDocuments();
  35. const valueVisitCount = await model.ValueModel.find({ userid: id, type: '1' }).countDocuments();
  36. const userTotalValue = 5 * valueInfoCount + valueVisitCount;
  37. return {
  38. valueInfoCount, valueVisitCount, userTotalValue, valueInfoList, valueVisitList,
  39. };
  40. }
  41. return null;
  42. }
  43. }
  44. module.exports = ValueService;