userInfoService.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class UserInforService extends Service {
  4. async list({ userName, phone, pageNumber = 1, pageSize = 10 }) {
  5. const { ctx } = this;
  6. const cond = {};
  7. if (userName) {
  8. cond.user_real_name = userName;
  9. }
  10. if (phone) {
  11. cond.user_mobileno = phone;
  12. }
  13. const agg = [
  14. { $match: cond },
  15. ...ctx.helper.getPageMongo(pageNumber, pageSize),
  16. ];
  17. const list = await ctx.model.TRbacUserModel.aggregate(agg).allowDiskUse(true);
  18. const total = await ctx.model.TRbacUserModel.find(cond).countDocuments();
  19. return { total, list };
  20. }
  21. // TODO 这个后期如果改成月 年方式计算后计算方式要改变目前 一个人可能最多能连 10辆车,也就是1年1*10*365 *10(日均10个行程) = 3万
  22. async index({ user_id }) {
  23. const { ctx } = this;
  24. const agg = [
  25. { $match: { user_id } },
  26. { $lookup: { from: 'driving_behavior_info', localField: 'vin', foreignField: 'vin', as: 'info' } },
  27. { $unwind: { path: '$info' } },
  28. { $unwind: { path: '$info.mileage_list' } },
  29. { $group: { _id: null,
  30. steadyTotal: { $sum: { $cond: [{ $ne: [{ $indexOfCP: [ '$info.mileage_list.drive_style', '稳健' ] }, -1 ] }, 1, 0 ] } },
  31. commonlyTotal: { $sum: { $cond: [{ $ne: [{ $indexOfCP: [ '$info.mileage_list.drive_style', '一般' ] }, -1 ] }, 1, 0 ] } },
  32. radicalTotal: { $sum: { $cond: [{ $ne: [{ $indexOfCP: [ '$info.mileage_list.drive_style', '激进' ] }, -1 ] }, 1, 0 ] } } } },
  33. ];
  34. const result = await ctx.model.TVehicleRecordModel.aggregateNGroup(agg);
  35. return Object.assign({ steadyTotal: 0, commonlyTotal: 0, radicalTotal: 0 }, result);
  36. }
  37. }
  38. module.exports = UserInforService;