123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747 |
- 'use strict';
- const moment = require('moment');
- // 实销用户id
- exports.saledRoleId = 90;// '90'
- exports.timeMonthSeven = 1593532800000;// '2020年7月1日时间戳'
- // 远控行为ID
- exports.rcBehaviorId = 6016;
- // 远控结果
- exports.rcResult = {
- FAILED: { $in: [ 'false', 'FAILED', '请求超时,请稍后再试!' ] },
- SUCCEED: { $in: [ 'true', 'SUCCEED' ] },
- };
- // 车型车系
- exports.carType = {
- 电动车: [ 'E-HS3', 'E-HS9', 'E115', 'C105', 'NAT' ],
- 燃油车: [ 'H5', 'TEST_H9', 'H9', 'D357', 'HS7', 'HS730V', 'D365', 'HS5' ],
- };
- // 自动化测试类型
- exports.applicationDict = {
- 0: '连接认证',
- 1: '连接保持',
- 2: '远程控制',
- 3: '车况查询',
- 4: '远程配置',
- 5: '密钥更换',
- 6: '远程升级',
- 7: '紧急救援数据上报',
- 8: '车况数据上报',
- 9: '车辆定位',
- 10: '车辆告警',
- 11: '远程诊断',
- 12: '车辆延时控制状态上报',
- 13: '消息推送',
- 14: '用户问询',
- 15: '自动驾驶车辆控制',
- 16: '司机乘客报警处理状态反馈',
- 20: '证书申请',
- 21: 'CA连接认证',
- };
- // 远控类型
- exports.rcDict = {
- 车门解锁: { op: 'UNLOCK', behavior_id: 60160001 },
- 车门上锁: { op: 'LOCK', behavior_id: 60160001 },
- 车辆启动: { op: 'OPEN', behavior_id: 60160002 },
- 车辆熄火: { op: 'CLOSE', behavior_id: 60160002 },
- 空调开启: { op: 'OPEN', behavior_id: 60160003 },
- 空调关闭: { op: 'CLOSE', behavior_id: 60160003 },
- 空调温度控制: { behavior_id: 60160004 },
- 空调风量档位设置: { behavior_id: 60160005 },
- 前除霜模式开启: { op: 'OPEN', behavior_id: 60160006 },
- 前除霜模式关闭: { op: 'CLOSE', behavior_id: 60160006 },
- 后风窗加热开启: { op: 'OPEN', behavior_id: 60160007 },
- 后风窗加热关闭: { op: 'CLOSE', behavior_id: 60160007 },
- 空调运行时长设置: { behavior_id: 60160008 },
- 一键调温: { behavior_id: 60160009 },
- 主驾座椅加热开启: { behavior_id: 60160010, op: 'OPEN', opType: 'HOSTSEAT_HEAT' },
- 主驾座椅加热关闭: { behavior_id: 60160010, op: 'CLOSE', opType: 'HOSTSEAT_HEAT' },
- 副驾座椅加热开启: { behavior_id: 60160010, op: 'OPEN', opType: 'VICESEAT_HEAT' },
- 副驾座椅加热关闭: { behavior_id: 60160010, op: 'CLOSE', opType: 'VICESEAT_HEAT' },
- 左后座椅加热开启: { behavior_id: 60160010, op: 'OPEN', opType: 'LEFT_REAR_HEAT' },
- 左后座椅加热关闭: { behavior_id: 60160010, op: 'CLOSE', opType: 'LEFT_REAR_HEAT' },
- 右后座椅加热开启: { behavior_id: 60160010, op: 'OPEN', opType: 'RIGHT_REAR_HEAT' },
- 右后座椅加热关闭: { behavior_id: 60160010, op: 'CLOSE', opType: 'RIGHT_REAR_HEAT' },
- 空调预约: { behavior_id: 60160011 },
- 方向盘加热开启: { op: 'OPEN', behavior_id: 60160012 },
- 方向盘加热关闭: { op: 'CLOSE', behavior_id: 60160012 },
- 通风开启: { op: 'OPEN', behavior_id: 60160013 },
- 通风关闭: { op: 'CLOSE', behavior_id: 60160013 },
- 天窗开启: { op: 'OPEN', behavior_id: 60160014 },
- 天窗关闭: { op: 'CLOSE', behavior_id: 60160014 },
- 车灯开启: { op: 'OPEN', behavior_id: 60160015 },
- 车灯关闭: { op: 'CLOSE', behavior_id: 60160015 },
- 远程寻车: { behavior_id: 60160016 },
- 设置充电限值: { behavior_id: 60160017 },
- 设置放电电量限值: { behavior_id: 60160018 },
- 开始充电: { behavior_id: 60160019 },
- 结束充电: { behavior_id: 60160020 },
- 定时充电: { behavior_id: 60160021 },
- 充电完成提醒开启: { op: 'OPEN', behavior_id: 60160022 },
- 充电完成提醒关闭: { op: 'CLOSE', behavior_id: 60160022 },
- 充电状态查询: { behavior_id: 60160023 },
- 远程监控开启: { behavior_id: 60160024 },
- 远程监控正常关闭: { closeType: 'NORMAL', behavior_id: 60160025 },
- 远程监控异常关闭: { closeType: 'EXCEPTION', behavior_id: 60160025 },
- '控制充电桩(基于蓝牙)TBD': { behavior_id: 60160026 },
- 代客泊车: { behavior_id: 60160027 },
- 代客模式: { behavior_id: 60160028 },
- };
- // 故障类型
- exports.faultDict = {
- _S0100: '发动机管理系统故障',
- _S0101: '变速箱控制单元故障',
- _S0102: '排放系统故障',
- _S0103: '安全气囊系统故障',
- _S0104: '电子稳定系统故障',
- _S0105: '防抱死刹车系统故障',
- _S0107: '机油压力报警',
- _S0108: '油量低报警',
- _S0109: '制动液位报警',
- _S0110: '蓄电池充电故障',
- _S0111: '制动系统故障',
- _S0112: '胎压系统故障',
- _S0114: '外部灯光故障',
- _S0115: '电子转向柱锁故障',
- _S0116: '发动机水温过高报警',
- _S0117: '电子驻车单元系统故障',
- _S0118: '智能远光系统故障',
- _S0119: '自适应巡航系统故障',
- _S0121: '道路偏离预警系统故障',
- _S0122: '盲区检测系统故障',
- _S0123: '空调人为操作',
- _S0124: '高压系统故障',
- _S0125: '高压绝缘故障',
- _S0126: '高压互锁故障',
- _S0128: '动力电池故障',
- _S0129: '动力电机故障',
- _S0130: 'E-Park 故障',
- _S0131: '动力电池电量过低报警',
- _S0132: '动力电池温度过高报警',
- _S0133: '动力电机温度过高报警',
- _S0135: '充电机故障',
- _S0136: '空调故障',
- _S0137: '换道辅助系统故障',
- _S0138: '自动紧急制动系统故障',
- _S0139: '倒车雷达系统故障',
- _S0140: '电子换挡器系统故障',
- _S0142: '直流-直流转换器故障',
- _S0143: '整车控制器故障',
- _S0144: '纯电动继电器及线圈相关故障',
- _S0145: '自动驻车系统故障',
- _S0146: '陡坡缓降系统故障',
- _S0147: '灯具故障',
- _S0149: '12V 蓄电池系统故障',
- _S0150: '冷却液温度报警',
- _S0151: '冷却液液位低报警',
- _S0152: '机油温度报警',
- _S0153: 'HBB 故障',
- _S0154: '驾驶员安全带未系报警',
- _S0156: '清洗液液位低报警',
- _S0157: '制动摩擦片磨损过度报警',
- _S0158: '变速器温度过高报警',
- _S0159: 'DSM 故障',
- _S0160: 'DMS 故障',
- _S0161: '空气悬架系统故障',
- _S0163: '车桩通信故障',
- _S0164: '空调冷凝催化剂不足报警',
- _S0165: '动力电池SOC低无法启动放电报警',
- _S0166: '充放电枪连接时试图Ready报警',
- _S0167: '直流充电设备Station 故障',
- _S0168: '直流充电桩不匹配报警',
- _S0170: '交流充电设备供电异常',
- _S0171: '动力电池温度过低报警',
- _S0172: '动力电池温度差过大报警',
- _S0106: '电子助力转向系统故障',
- _S0113: '启停系统故障',
- _S0120: '前碰撞预警系统故障',
- _S0127: '动力电池充放电系统故障',
- _S0134: '定速巡航系统故障',
- _S0141: '轮胎报警',
- _S0148: '12V 蓄电池电量低报警',
- _S0155: '副驾驶安全带未系报警',
- _S0162: '暖风PTC故障',
- _S0169: '交流充电设备CP故障',
- // _S0100:'发动机常见故障',
- // _S0102:'发动机常见故障',
- // _S0116:'发动机常见故障',
- // _S0107:'发动机常见故障',
- // _S0152:'发动机常见故障',
- //
- // _S0149:'电气系统常见故障',
- // _S0148:'电气系统常见故障',
- // _S0110:'电气系统常见故障',
- // _S0123:'电气系统常见故障',
- // _S0136:'电气系统常见故障',
- // _S0164:'电气系统常见故障',
- // _S0147:'电气系统常见故障',
- // _S0130:'电气系统常见故障',
- // _S0135:'电气系统常见故障',
- // _S0118:'电气系统常见故障',
- // _S0169:'电气系统常见故障',
- // _S0104:'电气系统常见故障',
- // _S0114:'电气系统常见故障',
- // _S0162:'电气系统常见故障',
- // _S0170:'电气系统常见故障',
- // _S0165:'电气系统常见故障',
- // _S0171:'电气系统常见故障',
- // _S0172:'电气系统常见故障',
- // _S0127:'电气系统常见故障',
- // _S0131:'电气系统常见故障',
- // _S0132:'电气系统常见故障',
- // _S0159:'电气系统常见故障',
- // _S0160:'电气系统常见故障',
- // _S0166:'电气系统常见故障',
- // _S0167:'电气系统常见故障',
- // _S0168:'电气系统常见故障',
- // _S0126:'电气系统常见故障',
- //
- //
- // _S0101:'发动机常见故障',
- // _S0158:'发动机常见故障',
- //
- // _S0106:'传动系常见故障',
- // _S0115:'传动系常见故障',
- //
- // _S0105:'制动系常见故障',
- // _S0109:'制动系常见故障',
- // _S0111:'制动系常见故障',
- // _S0157:'制动系常见故障',
- // _S0145:'制动系常见故障',
- // _S0113:'制动系常见故障',
- // _S0117:'制动系常见故障',
- //
- // _S0141:'行驶系常见故障',
- // _S0112:'行驶系常见故障',
- // _S0161:'行驶系常见故障',
- //
- // _S0103:'车身常见故障',
- // _S0143:'车身常见故障',
- };
- // 用户出行评分 安全,节能评分
- exports.scoreDict = {
- 0: { $gte: 0, $lt: 60 },
- 1: { $gte: 60, $lt: 75 },
- 2: { $gte: 75, $lt: 90 },
- 3: { $gte: 90, $lte: 100 },
- };
- // 用户出行评分 安全评分mongo
- exports.drivingSafetyScoreProject = {
- driving_safety_score: 1,
- acceleration_score: 1,
- decelerate_score: 1,
- speeding_score: 1,
- fa_score: 1,
- fd_score: 1,
- turn_score: 1,
- clu_score: 1,
- flc_score: 1,
- io_score: 1,
- hs_turn_score: 1,
- at_night_drive_score: 1,
- un_tie_sb_score: 1,
- leave_sw_score: 1,
- dsm_score: 1,
- fcw_score: 1,
- bsd_score: 1,
- lka_score: 1,
- ldw_score: 1,
- };
- // 用户出行评分 节能评分mongo
- exports.energyConservationScoreProject = {
- energyConservationScore: '$energy_conservation_score',
- acceleration_score: 1,
- decelerate_score: 1,
- idling_score: 1,
- drive_anticipation_score: 1,
- oil_score: 1,
- sa_score: 1,
- sd_score: 1,
- steady_score: 1,
- };
- // 格式化时间
- exports.formatTime = time => moment(time).format('YYYY-MM-DD HH:mm:ss');
- exports.formatTime1 = time => moment(time).format('YYYY-MM-DD');
- exports.formatTime2 = time => moment(time).format('YYYY-MM-DD HH:mm:ss.SSS');
- exports.formatTime3 = time => moment(time).format('YYMM');
- // 获取昨日0:0:0
- exports.yesterday = () => moment().subtract(1, 'days').set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0)
- .valueOf();
- // 获取今日0:0:0
- exports.today = () => moment().set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0)
- .valueOf();
- // 解析时间字符串YYYY-MM-DD为时间戳
- exports.parse = str => moment(str, 'YYYY-MM-DD').valueOf();
- exports.parseS = str => parseInt(moment(str, 'YYYY-MM-DD HH:mm:ss.SSS').valueOf() / 1000);
- // 解析时间字符串为moment对象
- exports.momentDate = str => moment(str, 'YYYY-MM-DD');
- // 判断是否是今日
- exports.isToday = time => moment(new Date()).isSame(moment(time));
- // 当前年 今年
- exports.nowYear = time => moment(time).set('month', 0)
- .set('date', 1)
- .set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0)
- .valueOf();
- // 获取月头
- exports.getMonthTop = time => moment(time).set('date', 1)
- .set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0)
- .valueOf();
- // 上个月月头
- exports.preMonth = time => moment(time)
- .subtract(1, 'months')
- .set('date', 1)
- .set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0)
- .valueOf();
- // 获取转换时间戳对象project 默认转换字段$create_date
- exports.getTimeGenProject = (type, dateStr = 'create_date') => {
- // dateString: { $dateToString: { format: '%Y-%m-%d', date: { $toDate: dateStr } } },
- switch (type) {
- case '0':
- return {
- day: { $dayOfMonth: { date: { $toDate: `$${dateStr}` }, timezone: 'Asia/Shanghai' } },
- month: { $month: { date: { $toDate: `$${dateStr}` }, timezone: 'Asia/Shanghai' } },
- year: { $year: { date: { $toDate: `$${dateStr}` }, timezone: 'Asia/Shanghai' } },
- };
- case '1':
- return {
- month: { $month: { date: { $toDate: `$${dateStr}` }, timezone: 'Asia/Shanghai' } },
- year: { $year: { date: { $toDate: `$${dateStr}` }, timezone: 'Asia/Shanghai' } },
- };
- case '2':
- return {
- year: { $year: { date: { $toDate: `$${dateStr}` }, timezone: 'Asia/Shanghai' } },
- };
- default:
- break;
- }
- };
- // 获取转换时间戳范围match 默认字段$create_date
- exports.getTimeRangMatch = (startTime, endTime, dateStr = 'create_date') => {
- const match = {};
- match[dateStr] = { $gte: startTime, $lt: endTime };
- return match;
- };
- // 获取区域分组语句 (之前的前置语句或者结果必须存在 元素属性:city_name,area_name,provice_name,count)
- exports.getLocationMongo = () => {
- return [
- { $group: { _id: { area_name: '$area_name', provice_name: '$provice_name', city_name: '$city_name' },
- count: { $sum: '$count' } } },
- { $group: { _id: { area_name: '$_id.area_name', provice_name: '$_id.provice_name' },
- cities: { $push: { city_name: '$_id.city_name', count: '$count' } },
- count: { $sum: '$count' } } },
- { $group: { _id: { area_name: '$_id.area_name' },
- provinces: { $push: { provice_name: '$_id.provice_name', cities: '$cities', count: '$count' } },
- count: { $sum: '$count' } } },
- { $group: { _id: null,
- areas: { $push: { area_name: '$_id.area_name', provinces: '$provinces', count: '$count' } },
- count: { $sum: '$count' } } },
- ];
- };
- // 获取年龄和性别分组语句 (之前的前置语句或者结果必须存在 元素属性: id_card,gender)
- exports.getAggAndSexMongo = () => {
- return [
- {
- $bucket: {
- groupBy: { $cond: [ '$id_card',
- { $subtract: [{ $year: { date: { $toDate: new Date() }, timezone: 'Asia/Shanghai' } },
- { $convert: {
- input: { $substr: [ '$id_card', 6, 4 ] },
- to: 'int',
- onError: { $add: [{ $year: { date: { $toDate: new Date() }, timezone: 'Asia/Shanghai' } }, 1 ] } } },
- ] }, -1 ] },
- boundaries: [ 0, 25, 30, 35, 40, 200 ],
- default: 'Other',
- output: {
- mCount: { $sum: {
- $cond: [{ $eq: [ '$gender', 'M' ] }, 1, 0 ],
- } },
- fCount: { $sum: {
- $cond: [{ $eq: [ '$gender', 'F' ] }, 1, 0 ],
- } },
- count: { $sum: 1 },
- },
- },
- },
- ];
- };
- // 去重
- exports.unionArray = (a, b) => {
- return [ ...new Set([ ...a, ...b ]) ];
- };
- // 获取消息类型 时间分组语句
- exports.getMsgTimeGroupMongo = type => {
- switch (type) {
- case '0':
- return [
- { $group: { _id: { year: '$year', month: '$month', day: '$day', msgType: '$msgType._id' },
- count: { $sum: '$msgType.count' } } },
- { $group: { _id: { year: '$_id.year', month: '$_id.month', day: '$_id.day' },
- msgType: { $push: { _id: '$_id.msgType', count: '$count' } } } },
- ];
- case '1':
- return [
- { $group: { _id: { year: '$year', month: '$month', msgType: '$msgType._id' },
- count: { $sum: '$msgType.count' } } },
- { $group: { _id: { year: '$_id.year', month: '$_id.month' },
- msgType: { $push: { _id: '$_id.msgType', count: '$count' } } } },
- ];
- case '2':
- return [
- { $group: { _id: { year: '$year', msgType: '$msgType._id' },
- count: { $sum: '$msgType.count' } } },
- { $group: { _id: { year: '$_id.year' },
- msgType: { $push: { _id: '$_id.msgType', count: '$count' } } } },
- ];
- default:
- return [];
- }
- };
- // 获取车辆出行和疲劳次数分布 时间分组语句
- exports.getCarDSMTimeGroupMongo = type => {
- switch (type) {
- case '0':
- return [
- { $group: {
- _id: { year: '$year', month: '$month', day: '$day', _id: '$mileageStartTimeAndDsm._id._id' },
- count: { $sum: '$mileageStartTimeAndDsm.count' }, dsmCount: { $sum: '$mileageStartTimeAndDsm.dsmCount' } } },
- { $group: { _id: { year: '$_id.year', month: '$_id.month', day: '$_id.day' },
- data: { $push: { _id: '$_id._id', count: '$count', dsmCount: '$dsmCount' } } } },
- ];
- case '1':
- return [
- { $group: { _id: { year: '$year', month: '$month', _id: '$mileageStartTimeAndDsm._id._id' },
- count: { $sum: '$mileageStartTimeAndDsm.count' }, dsmCount: { $sum: '$mileageStartTimeAndDsm.dsmCount' } } },
- { $group: { _id: { year: '$_id.year', month: '$_id.month' },
- data: { $push: { _id: '$_id._id', count: '$count', dsmCount: '$dsmCount' } } } },
- ];
- case '2':
- return [
- { $group: { _id: { year: '$year', _id: '$mileageStartTimeAndDsm._id._id' },
- count: { $sum: '$mileageStartTimeAndDsm.count' }, dsmCount: { $sum: '$mileageStartTimeAndDsm.dsmCount' } } },
- { $group: { _id: { year: '$_id.year' },
- data: { $push: { _id: '$_id._id', count: '$count', dsmCount: '$dsmCount' } } } },
- ];
- default:
- return [];
- }
- };
- // 获取车辆根据 某个维度字段求和 进行时间分组语句
- exports.getCarTimeGroupMongo = (type, value) => {
- switch (type) {
- case '0':
- return [
- { $group: {
- _id: { year: '$year', month: '$month', day: '$day', _id: `$${value}._id._id` },
- count: { $sum: `$${value}.count` } } },
- { $group: { _id: { year: '$_id.year', month: '$_id.month', day: '$_id.day' },
- data: { $push: { _id: '$_id._id', count: '$count' } } } },
- ];
- case '1':
- return [
- { $group: { _id: { year: '$year', month: '$month', _id: `$${value}._id._id` },
- count: { $sum: `$${value}.count` } } },
- { $group: { _id: { year: '$_id.year', month: '$_id.month' },
- data: { $push: { _id: '$_id._id', count: '$count' } } } },
- ];
- case '2':
- return [
- { $group: { _id: { year: '$year', _id: `$${value}._id._id` },
- count: { $sum: `$${value}.count` } } },
- { $group: { _id: { year: '$_id.year' },
- data: { $push: { _id: '$_id._id', count: '$count' } } } },
- ];
- default:
- return [];
- }
- };
- // 获取车辆根据 某个维度字段求和 进行时间分组语句
- exports.getCarTimeGroupMongo2 = type => {
- switch (type) {
- case '0':
- return [
- { $group: {
- _id: { year: '$_id.year', month: '$_id.month', day: '$_id.day', _id: '$count._id._id' },
- count: { $sum: '$count.count' } } },
- { $group: { _id: { year: '$_id.year', month: '$_id.month', day: '$_id.day' },
- data: { $push: { _id: '$_id._id', count: '$count' } } } },
- ];
- case '1':
- return [
- { $group: { _id: { year: '$_id.year', month: '$_id.month', _id: '$count._id._id' },
- count: { $sum: '$count.count' } } },
- { $group: { _id: { year: '$_id.year', month: '$_id.month' },
- data: { $push: { _id: '$_id._id', count: '$count' } } } },
- ];
- case '2':
- return [
- { $group: { _id: { year: '$_id.year', _id: '$count._id._id' },
- count: { $sum: '$count.count' } } },
- { $group: { _id: { year: '$_id.year' },
- data: { $push: { _id: '$_id._id', count: '$count' } } } },
- ];
- default:
- return [];
- }
- };
- // 获取车辆根据 某个维度字段求平均值 进行时间分组语句
- exports.getCarAvgTimeGroupMongo = (type, value) => {
- switch (type) {
- case '0':
- return [
- { $group: {
- _id: { year: '$year', month: '$month', day: '$day', _id: `$${value}._id._id` },
- count: { $avg: `$${value}.count` } } },
- { $group: { _id: { year: '$_id.year', month: '$_id.month', day: '$_id.day' },
- data: { $push: { _id: '$_id._id', count: '$count' } } } },
- ];
- case '1':
- return [
- { $group: { _id: { year: '$year', month: '$month', _id: `$${value}._id._id` },
- count: { $avg: `$${value}.count` } } },
- { $group: { _id: { year: '$_id.year', month: '$_id.month' },
- data: { $push: { _id: '$_id._id', count: '$count' } } } },
- ];
- case '2':
- return [
- { $group: { _id: { year: '$year', _id: `$${value}._id._id` },
- count: { $avg: `$${value}.count` } } },
- { $group: { _id: { year: '$_id.year' },
- data: { $push: { _id: '$_id._id', count: '$count' } } } },
- ];
- default:
- return [];
- }
- };
- // 获取分页语句
- exports.getPageMongo = (pageNumber, pageSize) => {
- return [
- { $skip: (pageNumber - 1) * pageSize },
- { $limit: pageSize },
- ];
- };
- // 获取根据某个维度字段进行桶分组语句 -(区分车型车系)
- exports.getBucketMongo = (group, boundaries, sum = { $sum: 1 }, bucketF = {}) => {
- return [
- {
- $bucket: {
- groupBy: group,
- boundaries,
- default: 'Other',
- output: {
- v: { $push: { vin: '$vin', ...bucketF } },
- },
- },
- },
- { $unwind: '$v' },
- { $lookup: { from: 't_vehicle_record', localField: 'v.vin', foreignField: 'vin', as: 'car' } },
- { $unwind: { path: '$car', preserveNullAndEmptyArrays: true } },
- { $group: {
- _id: { series_code: '$car.series_code', model_code: '$car.model_code', _id: '$_id' },
- count: sum,
- } },
- ];
- };
- // 获取时间分组 type 0日 1月 2年
- exports.getTimeGroup = (type, baseCond = { count: { $sum: 1 } }) => {
- switch (type) {
- case '0':
- return {
- ...baseCond, _id: { year: '$year', month: '$month', day: '$day' },
- };
- case '1':
- return {
- ...baseCond, _id: { year: '$year', month: '$month' },
- };
- case '2':
- return {
- ...baseCond, _id: { year: '$year' },
- };
- default:
- break;
- }
- };
- // 通用的时间范围匹配 + 时间分组求和
- exports.getCommonAggSum = ({ type, startTime, endTime, value }) => {
- return [
- { $match: this.getTimeRangMatch(startTime, endTime) },
- { $group: this.getTimeGroup(type, { count: { $sum: `$${value}` } }) },
- ];
- };
- // 通用的时间范围匹配 + 时间分组平均值
- exports.getCommonAggAvg = ({ type, startTime, endTime, value }) => {
- return [
- { $match: this.getTimeRangMatch(startTime, endTime) },
- { $group: this.getTimeGroup(type, { count: { $avg: `$${value}` } }) },
- { $project: { count: { $trunc: '$count' } } },
- ];
- };
- // 通用的时间范围匹配 + 时间分组求最大(最近)时间数据
- exports.getCommonAggMax = ({ type, startTime, endTime, value }) => {
- return [
- { $match: this.getTimeRangMatch(startTime, endTime) },
- { $sort: { create_date: -1 } },
- { $group: this.getTimeGroup(type, { count: { $first: `$${value}` } }) },
- ];
- };
- // 车辆基本信息 字典字段映射
- exports.statusInfoDict = {
- statsAcceCnt: { k: 'stats_acce_cnt', num: 10, v: 'ar=' },
- statsSpRange: { k: 'stats_sp_range', num: 13, v: 'range=_cnt' },
- statsDece: { k: 'stats_dece', num: 10, v: 'dece=' },
- statsRotate: { k: 'stats_rotate', num: 8, v: 'rotate=' },
- statsSpRotate: { k: 'stats_sp_rotate', num: 8, v: 'rotate=', mode: [ 'sp_1_cnt', 'sp_2_cnt', 'sp_3_cnt' ] },
- statsHighSp: { k: 'stats_high_sp', num: 3, v: 'high_sp=' },
- statsStartSp: { k: 'stats_start_sp', num: 14, v: 'start_sp=' },
- yawPortraitAcces: { k: 'yaw_portrait_acces', num: 10, v: 'acce=' },
- statsDeceOther: { k: 'stats_dece_other', num: 16, v: 'dece=' },
- sidePortraitAcces: { k: 'side_portrait_acces', num: 10, v: 'acce=' },
- spPowerConsumption: { k: 'sp_power_consumption', num: 13, v: 'range=_cnt' },
- statsSpDeceCnt: { k: 'stats_sp_dece_cnt', num: 10, superNum: 7, v: 'sp_=_cnt.dece.dece=',
- mode: [ 'dr_mode_auto', 'dr_mode_eco', 'dr_mode_sport', 'dr_mode_confort' ] },
- statsSpAcceCnt: {
- multi: {
- acce: { k: 'stats_sp_acce_cnt', num: 10, superNum: 7, v: 'sp_=_cnt.acce.acce=' },
- as: { k: 'stats_sp_acce_cnt', num: 12, superNum: 7, v: 'sp_=_cnt.as.as=' },
- },
- },
- };
- // 根据车辆基本信息 字典字段映射 获取计算数组
- exports.getStatusInfoArr = () => {
- const arr = [];
- Object.keys(this.statusInfoDict).forEach(item => {
- const obj = this.statusInfoDict[item];
- if (obj.multi) {
- Object.keys(obj.multi).forEach(m => {
- const multiObj = obj.multi[m];
- if (multiObj.mode) {
- multiObj.mode.forEach(mode => {
- arr.push({ dbStr: `${multiObj.k}.${mode}.${multiObj.v}`, num: multiObj.num, superNum: multiObj.superNum });
- });
- } else {
- arr.push({ dbStr: `${multiObj.k}.${multiObj.v}`, num: multiObj.num, superNum: multiObj.superNum });
- }
- });
- } else {
- if (obj.mode) {
- obj.mode.forEach(mode => {
- arr.push({ dbStr: `${obj.k}.${mode}.${obj.v}`, num: obj.num, superNum: obj.superNum });
- });
- } else {
- arr.push({ dbStr: `${obj.k}.${obj.v}`, num: obj.num, superNum: obj.superNum });
- }
- }
- });
- return arr;
- };
- // 弃用 来源唯一
- exports.statusInfo = [
- [
- { dbStr: 'stats_acce_cnt.dr_mode_auto.ar=', num: 10 },
- { dbStr: 'stats_acce_cnt.dr_mode_eco.ar=', num: 10 },
- { dbStr: 'stats_acce_cnt.dr_mode_sport.ar=', num: 10 },
- { dbStr: 'stats_acce_cnt.dr_mode_confort.ar=', num: 10 },
- { dbStr: 'stats_sp_range.range=_cnt', num: 13 },
- { dbStr: 'stats_dece.dece=', num: 10 },
- { dbStr: 'stats_rotate.rotate=', num: 8 },
- { dbStr: 'stats_sp_rotate.sp_1_cnt.rotate=', num: 8 },
- { dbStr: 'stats_sp_rotate.sp_2_cnt.rotate=', num: 8 },
- { dbStr: 'stats_sp_rotate.sp_3_cnt.rotate=', num: 8 },
- { dbStr: 'stats_high_sp.high_sp=', num: 3 },
- { dbStr: 'stats_start_sp.start_sp=', num: 14 },
- { dbStr: 'stats_sp_dece_cnt.dr_mode_auto.sp_=_cnt.dece.dece=', superNum: 7, num: 10 },
- { dbStr: 'stats_sp_dece_cnt.dr_mode_eco.sp_=_cnt.dece.dece=', superNum: 7, num: 10 },
- { dbStr: 'stats_sp_dece_cnt.dr_mode_sport.sp_=_cnt.dece.dece=', superNum: 7, num: 10 },
- { dbStr: 'stats_sp_dece_cnt.dr_mode_confort.sp_=_cnt.dece.dece=', superNum: 7, num: 10 },
- { dbStr: 'stats_sp_acce_cnt.sp_=_cnt.acce.acce=', superNum: 6, num: 10 },
- { dbStr: 'stats_sp_acce_cnt.sp_=_cnt.as.as=', superNum: 6, num: 12 },
- ],
- ];
- // 测试使用 以下
- // 明年
- exports.nextYear = () => moment().add(1, 'year').set('month', 0)
- .set('date', 1)
- .set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0)
- .valueOf();
- // 3年前
- exports.before3Year = () => moment().subtract(2, 'year').set('month', 0)
- .set('date', 1)
- .set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0)
- .valueOf();
- // 下个月月头
- exports.nextMonth = time => moment(time).add(1, 'months').set('date', 1)
- .set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0)
- .valueOf();
- // 前一年的月
- exports.yMonth = () => moment().subtract(1, 'year')
- .add(1, 'months')
- .set('date', 1)
- .set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0)
- .valueOf();
- // 3个月月头
- exports.before3Month = time => moment(time).subtract(2, 'months').set('date', 1)
- .set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0)
- .valueOf();
- // 获取今日距离上个月
- exports.mToday = () => moment().subtract(1, 'months')
- .add(1, 'days')
- .set('hour', 0)
- .set('minute', 0)
- .set('second', 0)
- .set('millisecond', 0)
- .valueOf();
|