helper.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. 'use strict';
  2. const moment = require('moment');
  3. // 实销用户id
  4. exports.saledRoleId = 90;// '90'
  5. // 远控行为ID
  6. exports.rcBehaviorId = 6016;
  7. exports.rcResult = {
  8. FAILED: [
  9. { 'remoteControl._id.rc_execution_result': 'FAILED' },
  10. { 'remoteControl._id.rc_execution_result': 'false' },
  11. ],
  12. SUCCEED: [
  13. { 'remoteControl._id.rc_execution_result': 'SUCCEED' },
  14. { 'remoteControl._id.rc_execution_result': 'true' },
  15. ],
  16. };
  17. exports.rcDict = {
  18. 车门解锁: { op: 'UNLOCK', behavior_id: 60160001 },
  19. 车门上锁: { op: 'LOCK', behavior_id: 60160001 },
  20. 车辆启动: { op: 'OPEN', behavior_id: 60160002 },
  21. 车辆熄火: { op: 'CLOSE', behavior_id: 60160002 },
  22. 空调开启: { op: 'OPEN', behavior_id: 60160003 },
  23. 空调关闭: { op: 'CLOSE', behavior_id: 60160003 },
  24. 空调温度控制: { behavior_id: 60160004 },
  25. 空调风量档位设置: { behavior_id: 60160005 },
  26. 前除霜模式开启: { op: 'OPEN', behavior_id: 60160006 },
  27. 前除霜模式关闭: { op: 'CLOSE', behavior_id: 60160006 },
  28. 后风窗加热开启: { op: 'OPEN', behavior_id: 60160007 },
  29. 后风窗加热关闭: { op: 'CLOSE', behavior_id: 60160007 },
  30. 空调运行时长设置: { behavior_id: 60160008 },
  31. 一键调温: { behavior_id: 60160009 },
  32. 主驾座椅加热开启: { behavior_id: 60160010, op: 'OPEN', opType: 'HOSTSEAT_HEAT' },
  33. 主驾座椅加热关闭: { behavior_id: 60160010, op: 'CLOSE', opType: 'HOSTSEAT_HEAT' },
  34. 副驾座椅加热开启: { behavior_id: 60160010, op: 'OPEN', opType: 'VICESEAT_HEAT' },
  35. 副驾座椅加热关闭: { behavior_id: 60160010, op: 'CLOSE', opType: 'VICESEAT_HEAT' },
  36. 左后座椅加热开启: { behavior_id: 60160010, op: 'OPEN', opType: 'LEFT_REAR_HEAT' },
  37. 左后座椅加热关闭: { behavior_id: 60160010, op: 'CLOSE', opType: 'LEFT_REAR_HEAT' },
  38. 右后座椅加热开启: { behavior_id: 60160010, op: 'OPEN', opType: 'RIGHT_REAR_HEAT' },
  39. 右后座椅加热关闭: { behavior_id: 60160010, op: 'CLOSE', opType: 'RIGHT_REAR_HEAT' },
  40. 空调预约: { behavior_id: 60160011 },
  41. 方向盘加热开启: { op: 'OPEN', behavior_id: 60160012 },
  42. 方向盘加热关闭: { op: 'CLOSE', behavior_id: 60160012 },
  43. 通风开启: { op: 'OPEN', behavior_id: 60160013 },
  44. 通风关闭: { op: 'CLOSE', behavior_id: 60160013 },
  45. 天窗开启: { op: 'OPEN', behavior_id: 60160014 },
  46. 天窗关闭: { op: 'CLOSE', behavior_id: 60160014 },
  47. 车灯开启: { op: 'OPEN', behavior_id: 60160015 },
  48. 车灯关闭: { op: 'CLOSE', behavior_id: 60160015 },
  49. 远程寻车: { behavior_id: 60160016 },
  50. 设置充电限值: { behavior_id: 60160017 },
  51. 设置放电电量限值: { behavior_id: 60160018 },
  52. 开始充电: { behavior_id: 60160019 },
  53. 结束充电: { behavior_id: 60160020 },
  54. 定时充电: { behavior_id: 60160021 },
  55. 充电完成提醒开启: { op: 'OPEN', behavior_id: 60160022 },
  56. 充电完成提醒关闭: { op: 'CLOSE', behavior_id: 60160022 },
  57. 充电状态查询: { behavior_id: 60160023 },
  58. 远程监控开启: { behavior_id: 60160024 },
  59. 远程监控正常关闭: { closeType: 'NORMAL', behavior_id: 60160025 },
  60. 远程监控异常关闭: { closeType: 'EXCEPTION', behavior_id: 60160025 },
  61. '控制充电桩(基于蓝牙)TBD': { behavior_id: 60160026 },
  62. 代客泊车: { behavior_id: 60160027 },
  63. 代客模式: { behavior_id: 60160028 },
  64. };
  65. exports.drivingSafetyScoreProject = {
  66. acceleration_score: 1,
  67. decelerate_score: 1,
  68. speeding_score: 1,
  69. fa_score: 1,
  70. fd_score: 1,
  71. turn_score: 1,
  72. clu_score: 1,
  73. flc_score: 1,
  74. io_score: 1,
  75. hs_turn_score: 1,
  76. at_night_drive_score: 1,
  77. un_tie_sb_score: 1,
  78. leave_sw_score: 1,
  79. dsm_score: 1,
  80. fcw_score: 1,
  81. bsd_score: 1,
  82. lka_score: 1,
  83. ldw_score: 1,
  84. };
  85. exports.energyConservationScoreProject = {
  86. energyConservationScore: 1,
  87. acceleration_score: 1,
  88. decelerate_score: 1,
  89. idling_score: 1,
  90. drive_anticipation_score: 1,
  91. oil_score: 1,
  92. sa_score: 1,
  93. sd_score: 1,
  94. steady_score: 1,
  95. };
  96. // 格式化时间
  97. exports.formatTime = time => moment(time).format('YYYY-MM-DD HH:mm:ss');
  98. // 获取昨日0:0:0
  99. exports.yesterday = () => moment().subtract(1, 'days').set('hour', 0)
  100. .set('minute', 0)
  101. .set('second', 0)
  102. .set('millisecond', 0)
  103. .valueOf();
  104. // 获取今日0:0:0
  105. exports.today = () => moment().set('hour', 0)
  106. .set('minute', 0)
  107. .set('second', 0)
  108. .set('millisecond', 0)
  109. .valueOf();
  110. // 解析时间字符串YYYY-MM-DD为时间戳
  111. exports.parse = str => moment(str, 'YYYY-MM-DD').valueOf();
  112. // 解析时间字符串为moment对象
  113. exports.momentDate = str => moment(str, 'YYYY-MM-DD');
  114. // 判断是否是今日
  115. exports.isToday = time => moment(new Date()).isSame(moment(time));
  116. // 当前年 今年
  117. exports.nowYear = () => moment().set('month', 0)
  118. .set('date', 1)
  119. .set('hour', 0)
  120. .set('minute', 0)
  121. .set('second', 0)
  122. .set('millisecond', 0)
  123. .valueOf();
  124. // 获取月头
  125. exports.getMonthTop = time => moment(time).set('date', 1)
  126. .set('hour', 0)
  127. .set('minute', 0)
  128. .set('second', 0)
  129. .set('millisecond', 0)
  130. .valueOf();
  131. // 上个月
  132. exports.preMonth = time => moment(time).subtract(1, 'months').set('date', 1)
  133. .set('hour', 0)
  134. .set('minute', 0)
  135. .set('second', 0)
  136. .set('millisecond', 0)
  137. .valueOf();
  138. // 获取转换时间戳对象project 默认转换字段$create_date
  139. exports.getTimeGenProject = (type, dateStr = 'create_date') => {
  140. // dateString: { $dateToString: { format: '%Y-%m-%d', date: { $toDate: dateStr } } },
  141. switch (type) {
  142. case '0':
  143. return {
  144. day: { $dayOfMonth: { $toDate: `$${dateStr}` } },
  145. month: { $month: { $toDate: `$${dateStr}` } },
  146. year: { $year: { $toDate: `$${dateStr}` } },
  147. };
  148. case '1':
  149. return {
  150. month: { $month: { $toDate: `$${dateStr}` } },
  151. year: { $year: { $toDate: `$${dateStr}` } },
  152. };
  153. case '2':
  154. return {
  155. year: { $year: { $toDate: `$${dateStr}` } },
  156. };
  157. default:
  158. break;
  159. }
  160. };
  161. // 获取转换时间戳范围match 默认字段$create_date
  162. exports.getTimeRangMatch = (startTime, endTime, dateStr = 'create_date') => {
  163. const match = {};
  164. match[dateStr] = { $gte: startTime, $lt: endTime };
  165. return match;
  166. };
  167. // 获取活跃app的条件
  168. exports.getAppActiveCond = ({ startTime, endTime }) => {
  169. return [
  170. { $match: { ...this.getTimeRangMatch(startTime, endTime, 'login_time'), login_state: 1 } },
  171. { $sort: { login_time: 1 } },
  172. { $group: {
  173. _id: '$user_Id',
  174. login_count: { $sum: 1 },
  175. // area_code: { $first: '$area_code' }, area_name: { $first: '$area_name' },
  176. // provice_code: { $first: '$provice_code' },city_code: { $first: '$city_code' },
  177. provice_name: { $first: '$provice_name' }, city_name: { $first: '$city_name' },
  178. } },
  179. { $lookup: { from: 't_sync_province', localField: 'provice_name', foreignField: 'province_name', as: 'pro' } },
  180. { $unwind: '$pro' },
  181. { $lookup: {
  182. from: 't_sync_city',
  183. let: { city_name: '$city_name', province_id: '$pro.province_id' },
  184. pipeline: [{ $match: { $expr: { $and:
  185. [
  186. { $eq: [ '$city_name', '$$city_name' ] },
  187. { $eq: [ '$province_id', '$$province_id' ] },
  188. ] } } }], as: 'city',
  189. } },
  190. { $unwind: '$city' },
  191. { $lookup: { from: 't_sync_county', localField: 'provice_name', foreignField: 'province_name', as: 'area' } },
  192. { $project: {
  193. login_count: 1,
  194. area_code: { $arrayElemAt: [ '$area.area_code', 0 ] },
  195. area_name: { $arrayElemAt: [ '$area.area_name', 0 ] },
  196. provice_code: '$pro.pro_code',
  197. provice_name: '$pro.province_name',
  198. city_code: '$city.city_code',
  199. city_name: '$city.city_name',
  200. } },
  201. { $lookup:
  202. {
  203. from: 'app_behavior_record',
  204. let: { user_id: '$_id' },
  205. pipeline: [{ $match: { $expr: { $and:
  206. [
  207. { $eq: [ '$user_id', '$$user_id' ] },
  208. { $gte: [ '$create_time', startTime ] },
  209. { $lt: [ '$create_time', endTime ] },
  210. ] } } }], as: 'behaviors',
  211. },
  212. },
  213. { $unwind: { path: '$behaviors', preserveNullAndEmptyArrays: true } },
  214. { $group: {
  215. _id: '$_id',
  216. login_count: { $first: '$login_count' },
  217. use_duration: { $sum: { $cond: [ '$behaviors.use_duration', '$behaviors.use_duration', 0 ] } },
  218. area_code: { $first: '$area_code' }, area_name: { $first: '$area_name' },
  219. provice_code: { $first: '$provice_code' }, provice_name: { $first: '$provice_name' },
  220. city_code: { $first: '$city_code' }, city_name: { $first: '$city_name' },
  221. } },
  222. { $match: { $or: [{ use_duration: { $gt: 30 * 60 * 1000 } }, { login_count: { $gte: 3 } }] } },
  223. ];
  224. };
  225. // 获取活跃car的条件
  226. exports.getCarActiveCond = ({ startTime, endTime }) => {
  227. return [
  228. { $lookup: {
  229. from: 'driving_behavior_info',
  230. let: { vin: '$vin' },
  231. pipeline: [{ $match: { $expr: { $and:
  232. [
  233. { $eq: [ '$vin', '$$vin' ] },
  234. { $gte: [ '$start_time', this.getMonthTop(startTime) ] },
  235. { $lt: [ '$start_time', endTime ] },
  236. ] } } }], as: 'drive',
  237. } },
  238. { $unwind: { path: '$drive', preserveNullAndEmptyArrays: true } },
  239. { $group: {
  240. _id: '$vin',
  241. mileage: { $sum: { $cond: [ '$drive.mileage', { $toDouble: '$drive.mileage' }, 0 ] } },
  242. pro_code: { $first: '$pro_code' }, city_code: { $first: '$city_code' },
  243. is_saled_car: { $first: '$is_saled_car' }, sale_date: { $first: '$sale_date' },
  244. user_id: { $first: '$user_id' },
  245. series_code: { $first: '$series_code' }, model_code: { $first: '$model_code' },
  246. } },
  247. { $lookup: {
  248. from: 't-box_online_info',
  249. let: { vin: '$_id' },
  250. pipeline: [{ $match: { $expr: { $and:
  251. [
  252. { $eq: [ '$vin', '$$vin' ] },
  253. { $gte: [ '$online_time', this.getMonthTop(startTime) ] },
  254. { $lt: [ '$online_time', endTime ] },
  255. ] } } }], as: 'tbox',
  256. } },
  257. { $addFields: {
  258. online_count: { $size: '$tbox' },
  259. } },
  260. ];
  261. };
  262. // 获取年龄和性别分组语句 (之前的前置语句或者结果必须存在 元素属性: id_card,gender)
  263. exports.getAggAndSexMongo = () => {
  264. return [
  265. {
  266. $bucket: {
  267. groupBy: { $cond: [ '$id_card',
  268. { $subtract: [{ $year: { $toDate: new Date() } },
  269. { $convert: {
  270. input: { $substr: [ '$id_card', 6, 4 ] },
  271. to: 'int',
  272. onError: { $add: [{ $year: { $toDate: new Date() } }, 1 ] } } },
  273. ] }, -1 ] },
  274. boundaries: [ 18, 25, 30, 35, 40, 200 ],
  275. default: 'Other',
  276. output: {
  277. mCount: { $sum: {
  278. $cond: [{ $eq: [ '$gender', 'M' ] }, 1, 0 ],
  279. } },
  280. fCount: { $sum: {
  281. $cond: [{ $eq: [ '$gender', 'F' ] }, 1, 0 ],
  282. } },
  283. count: { $sum: 1 },
  284. },
  285. },
  286. },
  287. ];
  288. };
  289. // 获取区域分组语句 (之前的前置语句或者结果必须存在 元素属性:city_code,city_name,area_code,area_name,provice_code,provice_name)
  290. exports.getLocationMongo = () => {
  291. return [
  292. { $group: { _id: { city_code: '$city_code', city_name: '$city_name' },
  293. area_code: { $first: '$area_code' }, area_name: { $first: '$area_name' },
  294. provice_code: { $first: '$provice_code' }, provice_name: { $first: '$provice_name' },
  295. count: { $sum: 1 } } },
  296. { $group: { _id: { provice_code: '$provice_code', provice_name: '$provice_name' },
  297. area_code: { $first: '$area_code' }, area_name: { $first: '$area_name' },
  298. cities: { $push: { city_code: '$_id.city_code', city_name: '$_id.city_name', count: '$count' } },
  299. count: { $sum: '$count' } } },
  300. { $group: { _id: { area_code: '$area_code', area_name: '$area_name' },
  301. provinces: { $push: { provice_code: '$_id.provice_code', provice_name: '$_id.provice_name',
  302. cities: '$cities',
  303. count: '$count' } },
  304. count: { $sum: '$count' } } },
  305. { $group: { _id: null,
  306. areas: { $push: { area_code: '$_id.area_code', area_name: '$_id.area_name',
  307. provinces: '$provinces',
  308. count: '$count' } },
  309. count: { $sum: '$count' } } },
  310. ];
  311. };
  312. // 获取消息类型 时间分组语句
  313. exports.getMsgTimeGroupMongo = type => {
  314. switch (type) {
  315. case '0':
  316. return [
  317. { $group: { _id: { year: '$year', month: '$month', day: '$day', msgType: '$msgType._id' },
  318. count: { $sum: '$msgType.count' } } },
  319. { $group: { _id: { year: '$_id.year', month: '$_id.month', day: '$_id.day' },
  320. msgType: { $push: { _id: '$_id.msgType', count: '$count' } } } },
  321. ];
  322. case '1':
  323. return [
  324. { $group: { _id: { year: '$year', month: '$month', msgType: '$msgType._id' },
  325. count: { $sum: '$msgType.count' } } },
  326. { $group: { _id: { year: '$_id.year', month: '$_id.month' },
  327. msgType: { $push: { _id: '$_id.msgType', count: '$count' } } } },
  328. ];
  329. case '2':
  330. return [
  331. { $group: { _id: { year: '$year', msgType: '$msgType._id' },
  332. count: { $sum: '$msgType.count' } } },
  333. { $group: { _id: { year: '$_id.year' },
  334. msgType: { $push: { _id: '$_id.msgType', count: '$count' } } } },
  335. ];
  336. default:
  337. return [];
  338. }
  339. };
  340. // 获取车辆出行和疲劳次数分布 时间分组语句
  341. exports.getCarDSMTimeGroupMongo = type => {
  342. switch (type) {
  343. case '0':
  344. return [
  345. { $group: {
  346. _id: { year: '$year', month: '$month', day: '$day', _id: '$mileageStartTimeAndDsm._id._id' },
  347. count: { $sum: '$mileageStartTimeAndDsm.count' }, dsmCount: { $sum: '$mileageStartTimeAndDsm.dsmCount' } } },
  348. { $group: { _id: { year: '$_id.year', month: '$_id.month', day: '$_id.day' },
  349. data: { $push: { _id: '$_id._id', count: '$count', dsmCount: '$dsmCount' } } } },
  350. ];
  351. case '1':
  352. return [
  353. { $group: { _id: { year: '$year', month: '$month', _id: '$mileageStartTimeAndDsm._id._id' },
  354. count: { $sum: '$mileageStartTimeAndDsm.count' }, dsmCount: { $sum: '$mileageStartTimeAndDsm.dsmCount' } } },
  355. { $group: { _id: { year: '$_id.year', month: '$_id.month' },
  356. data: { $push: { _id: '$_id._id', count: '$count', dsmCount: '$dsmCount' } } } },
  357. ];
  358. case '2':
  359. return [
  360. { $group: { _id: { year: '$year', _id: '$mileageStartTimeAndDsm._id._id' },
  361. count: { $sum: '$mileageStartTimeAndDsm.count' }, dsmCount: { $sum: '$mileageStartTimeAndDsm.dsmCount' } } },
  362. { $group: { _id: { year: '$_id.year' },
  363. data: { $push: { _id: '$_id._id', count: '$count', dsmCount: '$dsmCount' } } } },
  364. ];
  365. default:
  366. return [];
  367. }
  368. };
  369. // 获取车辆根据 某个维度字段求和 进行时间分组语句
  370. exports.getCarTimeGroupMongo = (type, value) => {
  371. switch (type) {
  372. case '0':
  373. return [
  374. { $group: {
  375. _id: { year: '$year', month: '$month', day: '$day', _id: `$${value}._id._id` },
  376. count: { $sum: `$${value}.count` } } },
  377. { $group: { _id: { year: '$_id.year', month: '$_id.month', day: '$_id.day' },
  378. data: { $push: { _id: '$_id._id', count: '$count' } } } },
  379. ];
  380. case '1':
  381. return [
  382. { $group: { _id: { year: '$year', month: '$month', _id: `$${value}._id._id` },
  383. count: { $sum: `$${value}.count` } } },
  384. { $group: { _id: { year: '$_id.year', month: '$_id.month' },
  385. data: { $push: { _id: '$_id._id', count: '$count' } } } },
  386. ];
  387. case '2':
  388. return [
  389. { $group: { _id: { year: '$year', _id: `$${value}._id._id` },
  390. count: { $sum: `$${value}.count` } } },
  391. { $group: { _id: { year: '$_id.year' },
  392. data: { $push: { _id: '$_id._id', count: '$count' } } } },
  393. ];
  394. default:
  395. return [];
  396. }
  397. };
  398. // 获取车辆根据 某个维度字段求平均值 进行时间分组语句
  399. exports.getCarAvgTimeGroupMongo = (type, value) => {
  400. switch (type) {
  401. case '0':
  402. return [
  403. { $group: {
  404. _id: { year: '$year', month: '$month', day: '$day', _id: `$${value}._id._id` },
  405. count: { $avg: `$${value}.count` } } },
  406. { $group: { _id: { year: '$_id.year', month: '$_id.month', day: '$_id.day' },
  407. data: { $push: { _id: '$_id._id', count: '$count' } } } },
  408. ];
  409. case '1':
  410. return [
  411. { $group: { _id: { year: '$year', month: '$month', _id: `$${value}._id._id` },
  412. count: { $avg: `$${value}.count` } } },
  413. { $group: { _id: { year: '$_id.year', month: '$_id.month' },
  414. data: { $push: { _id: '$_id._id', count: '$count' } } } },
  415. ];
  416. case '2':
  417. return [
  418. { $group: { _id: { year: '$year', _id: `$${value}._id._id` },
  419. count: { $avg: `$${value}.count` } } },
  420. { $group: { _id: { year: '$_id.year' },
  421. data: { $push: { _id: '$_id._id', count: '$count' } } } },
  422. ];
  423. default:
  424. return [];
  425. }
  426. };
  427. // 获取分页语句
  428. exports.getPageMongo = (pageNumber, pageSize) => {
  429. return [
  430. { $skip: (pageNumber - 1) * pageSize },
  431. { $limit: pageSize },
  432. ];
  433. };
  434. // 获取根据某个维度字段进行桶分组语句 -(区分车型车系)
  435. exports.getBucketMongo = (group, boundaries, sum = { $sum: 1 }, bucketF) => {
  436. return [
  437. {
  438. $bucket: {
  439. groupBy: group,
  440. boundaries,
  441. default: 'Other',
  442. output: {
  443. v: { $push: { vin: '$vin', ...bucketF } },
  444. },
  445. },
  446. },
  447. { $unwind: '$v' },
  448. { $lookup: { from: 't_vehicle_record', localField: 'v.vin', foreignField: 'vin', as: 'car' } },
  449. { $unwind: '$car' },
  450. { $group: {
  451. _id: { series_code: '$car.series_code', model_code: '$car.model_code', _id: '$_id' },
  452. count: sum,
  453. } },
  454. ];
  455. };
  456. // 获取时间分组 type 0日 1月 2年
  457. exports.getTimeGroup = (type, baseCond = { count: { $sum: 1 } }) => {
  458. switch (type) {
  459. case '0':
  460. return {
  461. ...baseCond, _id: { year: '$year', month: '$month', day: '$day' },
  462. };
  463. case '1':
  464. return {
  465. ...baseCond, _id: { year: '$year', month: '$month' },
  466. };
  467. case '2':
  468. return {
  469. ...baseCond, _id: { year: '$year' },
  470. };
  471. default:
  472. break;
  473. }
  474. };
  475. // 通用的时间范围匹配 + 时间分组求和
  476. exports.getCommonAggSum = ({ type, startTime, endTime, value }) => {
  477. return [
  478. { $match: this.getTimeRangMatch(startTime, endTime) },
  479. { $group: this.getTimeGroup(type, { count: { $sum: `$${value}` } }) },
  480. ];
  481. };
  482. // 通用的时间范围匹配 + 时间分组平均值
  483. exports.getCommonAggAvg = ({ type, startTime, endTime, value }) => {
  484. return [
  485. { $match: this.getTimeRangMatch(startTime, endTime) },
  486. { $group: this.getTimeGroup(type, { count: { $avg: `$${value}` } }) },
  487. ];
  488. };
  489. // 通用的时间范围匹配 + 时间分组求最大(最近)时间数据
  490. exports.getCommonAggMax = ({ type, startTime, endTime, value }) => {
  491. return [
  492. { $match: this.getTimeRangMatch(startTime, endTime) },
  493. { $sort: { create_date: -1 } },
  494. { $group: this.getTimeGroup(type, { count: { $first: `$${value}` } }) },
  495. ];
  496. };
  497. // 车辆基本信息 字典字段映射
  498. exports.statusInfoDict = {
  499. statsAcceCnt: { k: 'stats_acce_cnt', num: 10, v: 'ar=', mode: [ 'dr_mode_auto', 'dr_mode_eco', 'dr_mode_sport', 'dr_mode_confort' ] },
  500. statsSpRange: { k: 'stats_sp_range', num: 13, v: 'range=_cnt' },
  501. statsDece: { k: 'stats_dece', num: 10, v: 'dece=' },
  502. statsRotate: { k: 'stats_rotate', num: 8, v: 'rotate=' },
  503. statsSpRotate: { k: 'stats_sp_rotate', num: 8, v: 'rotate=', mode: [ 'sp_1_cnt', 'sp_2_cnt', 'sp_3_cnt' ] },
  504. statsHighSp: { k: 'stats_high_sp', num: 3, v: 'high_sp=' },
  505. statsStartSp: { k: 'stats_start_sp', num: 14, v: 'start_sp=' },
  506. statsSpDeceCnt: { k: 'stats_sp_dece_cnt', num: 10, superNum: 7, v: 'sp_=_cnt.dece.dece=',
  507. mode: [ 'dr_mode_auto', 'dr_mode_eco', 'dr_mode_sport', 'dr_mode_confort' ] },
  508. statsSpAcceCnt: {
  509. multi: {
  510. acce: { k: 'stats_sp_acce_cnt', num: 10, superNum: 6, v: 'sp_=_cnt.acce.acce=' },
  511. as: { k: 'stats_sp_acce_cnt', num: 12, superNum: 6, v: 'sp_=_cnt.as.as=' },
  512. },
  513. },
  514. };
  515. // 根据车辆基本信息 字典字段映射 获取计算数组
  516. exports.getStatusInfoArr = () => {
  517. const arr = [];
  518. Object.keys(this.statusInfoDict).forEach(item => {
  519. const obj = this.statusInfoDict[item];
  520. if (obj.multi) {
  521. Object.keys(obj.multi).forEach(m => {
  522. const multiObj = obj.multi[m];
  523. if (multiObj.mode) {
  524. multiObj.mode.forEach(mode => {
  525. arr.push({ dbStr: `${multiObj.k}.${mode}.${multiObj.v}`, num: multiObj.num, superNum: multiObj.superNum });
  526. });
  527. } else {
  528. arr.push({ dbStr: `${multiObj.k}.${multiObj.v}`, num: multiObj.num, superNum: multiObj.superNum });
  529. }
  530. });
  531. } else {
  532. if (obj.mode) {
  533. obj.mode.forEach(mode => {
  534. arr.push({ dbStr: `${obj.k}.${mode}.${obj.v}`, num: obj.num, superNum: obj.superNum });
  535. });
  536. } else {
  537. arr.push({ dbStr: `${obj.k}.${obj.v}`, num: obj.num, superNum: obj.superNum });
  538. }
  539. }
  540. });
  541. return arr;
  542. };
  543. // 弃用 来源唯一
  544. exports.statusInfo = [
  545. [
  546. { dbStr: 'stats_acce_cnt.dr_mode_auto.ar=', num: 10 },
  547. { dbStr: 'stats_acce_cnt.dr_mode_eco.ar=', num: 10 },
  548. { dbStr: 'stats_acce_cnt.dr_mode_sport.ar=', num: 10 },
  549. { dbStr: 'stats_acce_cnt.dr_mode_confort.ar=', num: 10 },
  550. { dbStr: 'stats_sp_range.range=_cnt', num: 13 },
  551. { dbStr: 'stats_dece.dece=', num: 10 },
  552. { dbStr: 'stats_rotate.rotate=', num: 8 },
  553. { dbStr: 'stats_sp_rotate.sp_1_cnt.rotate=', num: 8 },
  554. { dbStr: 'stats_sp_rotate.sp_2_cnt.rotate=', num: 8 },
  555. { dbStr: 'stats_sp_rotate.sp_3_cnt.rotate=', num: 8 },
  556. { dbStr: 'stats_high_sp.high_sp=', num: 3 },
  557. { dbStr: 'stats_start_sp.start_sp=', num: 14 },
  558. { dbStr: 'stats_sp_dece_cnt.dr_mode_auto.sp_=_cnt.dece.dece=', superNum: 7, num: 10 },
  559. { dbStr: 'stats_sp_dece_cnt.dr_mode_eco.sp_=_cnt.dece.dece=', superNum: 7, num: 10 },
  560. { dbStr: 'stats_sp_dece_cnt.dr_mode_sport.sp_=_cnt.dece.dece=', superNum: 7, num: 10 },
  561. { dbStr: 'stats_sp_dece_cnt.dr_mode_confort.sp_=_cnt.dece.dece=', superNum: 7, num: 10 },
  562. { dbStr: 'stats_sp_acce_cnt.sp_=_cnt.acce.acce=', superNum: 6, num: 10 },
  563. { dbStr: 'stats_sp_acce_cnt.sp_=_cnt.as.as=', superNum: 6, num: 12 },
  564. ],
  565. ];