helper.js 22 KB

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