user.js
10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
* @Author: limin
* @Date: 2019-11-21 11:18:37
* @Last Modified by: limin
* @Last Modified time: 2020-01-04 12:53:16
*/
const Service = require("egg").Service;
// var moment = require('moment');/
class UserService extends Service {
constructor(ctx) {
super(ctx);
this.helper = ctx.helper;
this._reg_models_alias();
this._associate();
}
/**
* 绑定model 在 this 上的 别名
*/
_reg_models_alias() {
const {
TUserCityGender,
TUserCityCarbrand,
TUserCityDaily,
TUserCityDriveInfo,
TUserCityRealtime,
TUserNewActiveDetail
} = this.app.model.Screen.User;
const { TProvince, TCity } = this.app.model;
this.MUserRealTime = TUserCityRealtime;
this.MUserGender = TUserCityGender;
this.MUserCarBrand = TUserCityCarbrand;
this.MUserDaily = TUserCityDaily;
this.MUserDriveInfo = TUserCityDriveInfo;
this.MUserActive = TUserNewActiveDetail;
this.MProvice = TProvince;
this.MCity = TCity;
}
/**
* 绑定表关系
*/
_associate() {
this.MUserDaily.belongsTo(this.MProvice, {
foreignKey: "prov_code",
targetKey: "prov_code"
});
this.MUserDaily.belongsTo(this.MCity, {
foreignKey: "city_code",
targetKey: "city_code"
});
this.MUserRealTime.belongsTo(this.MProvice, {
foreignKey: "prov_code",
targetKey: "prov_code"
});
this.MUserRealTime.belongsTo(this.MCity, {
foreignKey: "city_code",
targetKey: "city_code"
});
this.MUserActive.belongsTo(this.MProvice, {
foreignKey: "prov_code",
targetKey: "prov_code"
});
this.MUserActive.belongsTo(this.MCity, {
foreignKey: "city_code",
targetKey: "city_code"
});
}
__build__option__(opt = {}) {
let { pid, cid, n, limit, order, day, beginDate, endDate } = opt;
let where = {};
const { between } = this.app.Sequelize.Op;
const { date, format_end_date } = this.helper;
if (cid) {
where.city_code = cid;
} else if (pid) {
where.prov_code = pid;
}
if (day) {
//如果有具体天数 就直接查询具体一天的数据(优先具体哪一天)
where.day = day;
} else if (n) {
// 查询近N天的数据 不包括今天
where.day = {
[between]: [date(n), format_end_date(date(-1))]
};
} else if (n === 0) {
// 查询
where.day = {
[between]: [date(0), format_end_date(date(0))]
};
} else if (beginDate) {
if (!endDate) {
endDate = beginDate;
}
if (!endDate.includes(":")) {
endDate = format_end_date(endDate);
}
where.day = {
[between]: [beginDate, endDate]
};
}
const option = {
where,
raw: true
};
if (limit) {
option.limit = +limit;
}
if (order) {
option.order = order;
}
return option;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 用户城市排行
*/
async city_range({ pid, limit }) {
const { app, MCity, MUserDaily } = this;
const { col, fn, Op } = app.Sequelize;
const option = this.__build__option__({
pid,
limit,
order: [["total_user", "DESC"]]
});
Object.assign(option.where, {
city_code: {
[Op.gt]: 0
}
});
Object.assign(option, {
attributes: [
"city_code",
[fn("SUM", col("total_user")), "total"],
col("t_city.city_name", "city")
],
include: { model: MCity, attributes: [] },
group: "city_code"
});
const data = await MUserDaily.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 用户车辆品牌分布
*/
async car_brand_range({ pid, cid, limit }) {
const { app, MUserCarBrand } = this;
const { fn, col } = app.Sequelize;
const option = this.__build__option__({
pid,
cid,
limit,
order: [["total", "DESC"]]
});
Object.assign(option, {
attributes: [
col("brand_name", "brand"),
[fn("SUM", col("brand_cars")), "total"],
col("update_time", "date")
],
group: "brand_name"
});
const data = await MUserCarBrand.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 用户日均里程分布
*/
async drive_range({ pid, cid }) {
const { app, MUserDriveInfo } = this;
const option = this.__build__option__({ pid, cid });
const { fn, col } = app.Sequelize;
Object.assign(option, {
attributes: [
[fn("SUM", col("d_0_5")), "d_0_5"],
[fn("SUM", col("d_5_10")), "d_5_10"],
[fn("SUM", col("d_10_15")), "d_10_15"],
[fn("SUM", col("d_15_20")), "d_15_20"],
[fn("SUM", col("d_20_25")), "d_20_25"],
[fn("SUM", col("d_25_31")), "d_25_31"],
[fn("SUM", col("km_0_40")), "km_0_40"],
[fn("SUM", col("km_40_70")), "km_40_70"],
[fn("SUM", col("km_70_100")), "km_70_100"],
[fn("SUM", col("km_100_")), "km_100_"],
[fn("SUM", col("w_0_10")), "w_0_10"],
[fn("SUM", col("w_10_20")), "w_10_20"],
[fn("SUM", col("w_20_30")), "w_20_30"],
[fn("SUM", col("w_30_50")), "w_30_50"],
[fn("SUM", col("w_50_")), "w_50_"]
]
});
const data = await MUserDriveInfo.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 获取某天城市用户总数列表
*/
async user_summary_city_by_day({ beginDate, endDate, pid, cid }) {
const { app, MUserDaily: MUserCity, MCity, MProvice } = this;
const { col } = app.Sequelize;
var option = this.__build__option__({ beginDate, endDate, pid, cid });
Object.assign(option, {
attributes: [
"city_code",
"prov_code",
"total_user",
"renew",
"recent_renew",
"active",
"increase",
"journey",
"miles",
"recent_total",
"recent_7_day_active",
"recent_30_day_active",
"online_time",
"day",
col("t_city.city_name"),
col("t_province.prov_name")
],
include: [
{ model: MCity, attributes: [] },
{ model: MProvice, attributes: [] }
]
});
const data = await MUserCity.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 获取实时数据(详细)
*/
async user_realtime({ pid, cid }) {
const { app, MUserRealTime, MCity, MProvice } = this;
const { col } = app.Sequelize;
var option = this.__build__option__({ pid, cid });
Object.assign(option, {
attributes: [
"city_code",
"prov_code",
"online_total",
"renew",
"active",
"increase",
"online_time",
col("t_city.city_name"),
col("t_province.prov_name")
],
include: [
{ model: MCity, attributes: [] },
{ model: MProvice, attributes: [] }
]
});
const data = await MUserRealTime.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 获取用户性别年龄数量占比
*/
async user_gender({ pid, cid }) {
const { app, MUserGender } = this;
var option = this.__build__option__({ pid, cid });
const { fn, col } = app.Sequelize;
Object.assign(option, {
attributes: [
"gender_range",
[fn("SUM", col("total")), "total"],
[fn("SUM", col("male")), "male"],
[fn("SUM", col("female")), "female"]
],
group: "gender_range"
});
const data = await MUserGender.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 获取用户激活详情
*/
async activation({ pid, limit, last_id }) {
const { app, service } = this;
const { Op } = app.Sequelize;
var option = service.screen.user.__build__option__({ pid, limit });
Object.assign(option.where, {
id: {
[Op.gt]: last_id
}
});
Object.assign(option, {
attributes: [
"id",
"sn",
"active_time",
"prov_code",
"city_code",
"device_type"
]
});
const data = await service.screen.user.MUserActive.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 根据id 获取 激活数据
*/
async user_active({ limit }) {
const { MUserActive } = this;
var option = this.__build__option__({ limit, order: [["id", "DESC"]] });
Object.assign(option, {
attributes: [
"id",
"sn",
"active_time",
"prov_code",
"city_code",
"device_type"
]
});
const data = await MUserActive.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 获取每个省的用户数
*/
async total_group_by_province({ pid }) {
const { app, MUserDaily, MProvice, MCity } = this;
var option = this.__build__option__({ pid });
const { fn, col } = app.Sequelize;
const _opt = pid
? {
attributes: [
"city_code",
[fn("SUM", col("total_user")), "total_user"],
col("t_city.city_name")
],
include: [{ model: MCity, attributes: [] }],
group: "city_code"
}
: {
attributes: [
"prov_code",
[fn("SUM", col("total_user")), "total_user"],
col("t_province.prov_name")
],
include: [{ model: MProvice, attributes: [] }],
group: "prov_code"
};
Object.assign(option, _opt);
const data = await MUserDaily.findAll(option);
return data;
}
}
module.exports = UserService;