iviBehaviorRecordService.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class IviBehaviorRecordService extends Service {
  4. async index({ type, startTime, endTime }) {
  5. const { ctx } = this;
  6. const agg = [
  7. { $match: ctx.helper.getTimeRangMatch(startTime, endTime) },
  8. { $group: ctx.helper.getTimeGroup(type, { total: { $sum: '$total' },
  9. musicTotal: { $sum: '$musicTotal' }, fwTotal: { $sum: '$fwTotal' },
  10. newsTotal: { $sum: '$newsTotal' }, videoTotal: { $sum: '$videoTotal' } }) },
  11. ];
  12. return await ctx.model.Local.IviBehaciorRecordModel.aggregateFix(agg);
  13. }
  14. async ext() {
  15. const { ctx } = this;
  16. const musicMonthAgg = [
  17. { $group: { _id: { year: '$year', month: '$month' }, count: { $sum: '$musicTotal' } } },
  18. { $sort: { count: -1 } },
  19. ];
  20. const musicYearAgg = [
  21. { $group: { _id: { year: '$year' }, count: { $sum: '$musicTotal' } } },
  22. { $sort: { count: -1 } },
  23. ];
  24. const musicMResult = await ctx.model.Local.IviBehaciorRecordModel.aggregateFix(musicMonthAgg);
  25. const musicYResult = await ctx.model.Local.IviBehaciorRecordModel.aggregateFix(musicYearAgg);
  26. let musicMaxMonth = {};
  27. let musicMinMonth = {};
  28. let musicMaxYear = {};
  29. let musicMinYear = {};
  30. if (musicMResult && musicMResult.length > 0) {
  31. musicMaxMonth = musicMResult[0];
  32. musicMinMonth = musicMResult[musicMResult.length - 1];
  33. }
  34. if (musicYResult && musicYResult.length > 0) {
  35. musicMaxYear = musicYResult[0];
  36. musicMinYear = musicYResult[musicYResult.length - 1];
  37. }
  38. const fwMonthAgg = [
  39. { $group: { _id: { year: '$year', month: '$month' }, count: { $sum: '$fwTotal' } } },
  40. { $sort: { count: -1 } },
  41. ];
  42. const fwYearAgg = [
  43. { $group: { _id: { year: '$year' }, count: { $sum: '$fwTotal' } } },
  44. { $sort: { count: -1 } },
  45. ];
  46. const fwMResult = await ctx.model.Local.IviBehaciorRecordModel.aggregateFix(fwMonthAgg);
  47. const fwYResult = await ctx.model.Local.IviBehaciorRecordModel.aggregateFix(fwYearAgg);
  48. let fwMaxMonth = {};
  49. let fwMinMonth = {};
  50. let fwMaxYear = {};
  51. let fwMinYear = {};
  52. if (fwMResult && fwMResult.length > 0) {
  53. fwMaxMonth = fwMResult[0];
  54. fwMinMonth = fwMResult[fwMResult.length - 1];
  55. }
  56. if (fwYResult && fwYResult.length > 0) {
  57. fwMaxYear = fwYResult[0];
  58. fwMinYear = fwYResult[fwMResult.length - 1];
  59. }
  60. const videoMonthAgg = [
  61. { $group: { _id: { year: '$year', month: '$month' }, count: { $sum: '$videoTotal' } } },
  62. { $sort: { count: -1 } },
  63. ];
  64. const videoYearAgg = [
  65. { $group: { _id: { year: '$year' }, count: { $sum: '$videoTotal' } } },
  66. { $sort: { count: -1 } },
  67. ];
  68. const videoMResult = await ctx.model.Local.IviBehaciorRecordModel.aggregateFix(videoMonthAgg);
  69. const videoYResult = await ctx.model.Local.IviBehaciorRecordModel.aggregateFix(videoYearAgg);
  70. let videoMaxMonth = {};
  71. let videoMinMonth = {};
  72. let videoMaxYear = {};
  73. let videoMinYear = {};
  74. if (videoMResult && videoMResult.length > 0) {
  75. videoMaxMonth = videoMResult[0];
  76. videoMinMonth = videoMResult[videoMResult.length - 1];
  77. }
  78. if (videoYResult && videoYResult.length > 0) {
  79. videoMaxYear = videoYResult[0];
  80. videoMinYear = videoMResult[videoMResult.length - 1];
  81. }
  82. return {
  83. musicMaxMonth, musicMinMonth, musicMaxYear, musicMinYear,
  84. fwMaxMonth, fwMinMonth, fwMaxYear, fwMinYear,
  85. videoMaxMonth, videoMinMonth, videoMaxYear, videoMinYear,
  86. };
  87. }
  88. // ====================清洗分割线==================================================================
  89. async statistics({ timeRangData, initData, isForceUpdate }) {
  90. const { ctx } = this;
  91. const hasData = await ctx.service.statisticsService.saveBefore(ctx.model.Local.IviBehaciorRecordModel,
  92. { ...initData });
  93. if (hasData && !isForceUpdate) {
  94. return;
  95. }
  96. initData.start_time = new Date();
  97. const obj = await this.total(timeRangData);
  98. const vins = await this.geneTemp(timeRangData);
  99. let musicTotal = 0;
  100. let fwTotal = 0;
  101. let newsTotal = 0;
  102. let videoTotal = 0;
  103. const iviBehavior = await this.group(timeRangData, vins);
  104. iviBehavior.forEach(item => {
  105. switch (item._id) {
  106. case 20020001:
  107. musicTotal = item.count;
  108. break;
  109. case 20010001:
  110. fwTotal = item.count;
  111. break;
  112. case 20050001:
  113. newsTotal = item.count;
  114. break;
  115. case 20030001:
  116. videoTotal = item.count;
  117. break;
  118. default:
  119. break;
  120. }
  121. });
  122. await ctx.service.statisticsService.save(ctx.model.Local.IviBehaciorRecordModel,
  123. { ...initData, musicTotal, fwTotal, newsTotal, videoTotal, ...obj }, isForceUpdate);
  124. }
  125. async geneTemp({ startTime, endTime }) {
  126. const { ctx } = this;
  127. await ctx.model.TempModel.deleteMany();
  128. const agg = [
  129. { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'create_time') },
  130. { $out: 'temp' },
  131. ];
  132. await ctx.model.IviBehaviorRecordModel.aggregate(agg);
  133. const aggVin = [
  134. { $group: { _id: '$vin' } },
  135. ];
  136. const result = await ctx.model.TempModel.aggregate(aggVin);
  137. return result.map(item => item._id);
  138. }
  139. async total({ startTime, endTime }) {
  140. const { ctx } = this;
  141. const agg = [
  142. { $match: ctx.helper.getTimeRangMatch(startTime, endTime, 'start_time') },
  143. { $group: { _id: null,
  144. total: { $sum: { $toLong: '$mileage_cnt' } } } },
  145. ];
  146. return await ctx.model.DrivingBehaviorInfoModel.aggregateNGroup(agg);
  147. }
  148. async group({ startTime, endTime }, vins) {
  149. const { ctx } = this;
  150. const agg = [
  151. { $match: { ...ctx.helper.getTimeRangMatch(startTime, endTime, 'start_time'), vin: { $in: vins } } },
  152. { $unwind: '$mileage_list' },
  153. { $lookup:
  154. {
  155. from: 'temp',
  156. let: { vin: '$vin', stime: '$mileage_list.start_time', etime: '$mileage_list.end_time' },
  157. pipeline: [{ $match: { $expr: { $and:
  158. [
  159. { $gte: [ '$create_time', '$$stime' ] },
  160. { $lt: [ '$create_time', '$$etime' ] },
  161. { $eq: [ '$vin', '$$vin' ] },
  162. ] } } }], as: 'ivi',
  163. },
  164. },
  165. { $unwind: '$ivi' },
  166. { $group: {
  167. _id: '$mileage_list.start_time',
  168. behavior_id: { $addToSet: '$ivi.behavior_id' },
  169. } },
  170. { $unwind: '$behavior_id' },
  171. { $group: {
  172. _id: '$behavior_id',
  173. count: { $sum: 1 },
  174. } },
  175. ];
  176. return await ctx.model.DrivingBehaviorInfoModel.aggregateFix(agg);
  177. }
  178. }
  179. module.exports = IviBehaviorRecordService;