tVehicleReportInfoService.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class TVehicleReportInfoService extends Service {
  4. // vhl_val_cnt车况数量
  5. // alarm_cnt 告警总数量
  6. // failure_cnt 故障总数量
  7. // 车辆上报分类信息统计
  8. async index({ type, startTime, endTime, seriesCode, modelCode }) {
  9. const { ctx } = this;
  10. const cond = [{ $match: {} }];
  11. if (seriesCode) {
  12. cond[0].$match['car._id.series_code'] = seriesCode;
  13. }
  14. if (modelCode) {
  15. cond[0].$match['car._id.model_code'] = modelCode;
  16. }
  17. const agg = [
  18. { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
  19. { $unwind: '$car' },
  20. ...cond,
  21. { $group: ctx.helper.getTimeGroup(type, { vhl_val_cnt: { $sum: '$car.vhl_val_cnt' },
  22. alarm_cnt: { $sum: '$car.alarm_cnt' }, failure_cnt: { $sum: '$car.failure_cnt' } }) },
  23. ];
  24. return await ctx.model.Local.TVehicleReportInfoModel.aggregateFix(agg);
  25. }
  26. // -------------------------------------------------------------------数据清洗----------------------------------------------------------------------
  27. async statistics({ timeRangData, initData, isForceUpdate }) {
  28. const { ctx } = this;
  29. const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.TVehicleReportInfoModel,
  30. { ...initData });
  31. if (hasData && !isForceUpdate) {
  32. return;
  33. }
  34. initData.start_time = new Date();
  35. const car = await this.group(timeRangData);
  36. ctx.logger.info('任务进行group');
  37. await ctx.service.statisticsService.save(ctx.model.Local.TVehicleReportInfoModel,
  38. { ...initData, car }, isForceUpdate);
  39. }
  40. async group({ startTime, endTime }) {
  41. const { ctx } = this;
  42. const agg = [
  43. { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'report_time') },
  44. { $group: { _id: '$vin',
  45. vhl_val_cnt: { $sum: '$vhl_val_cnt' }, alarm_cnt: { $sum: '$alarm_cnt' }, failure_cnt: { $sum: '$failure_cnt' } },
  46. },
  47. { $lookup: { from: 't_vehicle_record', localField: '_id', foreignField: 'vin', as: 'car' } },
  48. { $unwind: { path: '$car', preserveNullAndEmptyArrays: true } },
  49. { $group: { _id: { series_code: '$car.series_code', model_code: '$car.model_code' },
  50. vhl_val_cnt: { $sum: '$vhl_val_cnt' }, alarm_cnt: { $sum: '$alarm_cnt' }, failure_cnt: { $sum: '$failure_cnt' } },
  51. },
  52. ];
  53. return await ctx.model.TVehicleReportInfoModel.aggregateFix(agg);
  54. }
  55. }
  56. module.exports = TVehicleReportInfoService;