1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- 'use strict';
- const Service = require('egg').Service;
- class UserInforService extends Service {
- async list({ userName, phone, pageNumber = 1, pageSize = 10 }) {
- const { ctx } = this;
- const cond = {};
- if (userName) {
- cond.user_real_name = userName;
- }
- if (phone) {
- cond.user_mobileno = phone;
- }
- const agg = [
- { $match: cond },
- ...ctx.helper.getPageMongo(pageNumber, pageSize),
- ];
- const list = await ctx.model.TRbacUserModel.aggregate(agg).allowDiskUse(true);
- const total = await ctx.model.TRbacUserModel.find(cond).countDocuments();
- return { total, list };
- }
- // TODO 这个后期如果改成月 年方式计算后计算方式要改变目前 一个人可能最多能连 10辆车,也就是1年1*10*365 *10(日均10个行程) = 3万
- async index({ user_id }) {
- const { ctx } = this;
- const agg = [
- { $match: { user_id } },
- { $lookup: { from: 'driving_behavior_info', localField: 'vin', foreignField: 'vin', as: 'info' } },
- { $unwind: { path: '$info' } },
- { $unwind: { path: '$info.mileage_list' } },
- { $group: { _id: null,
- steadyTotal: { $sum: { $cond: [{ $ne: [{ $indexOfCP: [ '$info.mileage_list.drive_style', '稳健' ] }, -1 ] }, 1, 0 ] } },
- commonlyTotal: { $sum: { $cond: [{ $ne: [{ $indexOfCP: [ '$info.mileage_list.drive_style', '一般' ] }, -1 ] }, 1, 0 ] } },
- radicalTotal: { $sum: { $cond: [{ $ne: [{ $indexOfCP: [ '$info.mileage_list.drive_style', '激进' ] }, -1 ] }, 1, 0 ] } } } },
- ];
- const result = await ctx.model.TVehicleRecordModel.aggregateNGroup(agg);
- return Object.assign({ steadyTotal: 0, commonlyTotal: 0, radicalTotal: 0 }, result);
- }
- }
- module.exports = UserInforService;
|