Commit aa901e5e by liimin

新增商户统计

1 parent 37640c23
...@@ -51,7 +51,7 @@ npm install -g egg-sequelize-auto mysql2 ...@@ -51,7 +51,7 @@ npm install -g egg-sequelize-auto mysql2
-e 数据库类型 -e 数据库类型
-a json定义文件路径,可以追加一些自定义配置,如{"timestamps": false} -a json定义文件路径,可以追加一些自定义配置,如{"timestamps": false}
- create model: $ egg-sequelize-auto -o "./app/model" -d glsx_data_screen -h 192.168.0.39 -u root -p 3306 -x 123456 -t usedrow -e mysql - create model: $ egg-sequelize-auto -o "./app/model/screen/merchant" -d glsx_data_screen -h 14.17.100.222 -u dev_limin -p 3307 -x fdseooui -t xxx -e mysql
- 不指定-t 为所有表 - 不指定-t 为所有表
-- shell -- shell
......
...@@ -51,33 +51,25 @@ const Queue = require("./app/common/queue"); ...@@ -51,33 +51,25 @@ const Queue = require("./app/common/queue");
const yesterday = date(-1); const yesterday = date(-1);
const today = date(0); const today = date(0);
var { init_date, user_cache_update_time_diff } = this.app.config.sys; var { init_date, user_cache_update_time_diff } = this.app.config.sys;
const up_time = moment().subtract(user_cache_update_time_diff +1 ,'milliseconds');
await service.cache.set(constant.DATES,{ await service.cache.set(constant.DATES,{
today, today,
yesterday, yesterday,
cache_data_last_date:init_date, cache_data_last_date:init_date,
cache_update_time:moment().subtract(user_cache_update_time_diff +1 ,'milliseconds'), cache_data_last_date_merchant:init_date,
cache_update_time:up_time,
cache_update_time_merchant:up_time
}); });
} }
async _init_user_activation_queue_(ctx) { async _init_user_activation_queue_(ctx) {
let queue = new Queue({size:this.app.config.sys.user_activation_queue_size}); let queue = new Queue({size:this.app.config.sys.user_activation_queue_size});
queue.enqueue(...[
{ id: 0, name: "张三", pid: 33 },
{ id: 1, name: "李四", pid: 34 },
{ id: 2, name: "王五", pid: 11 },
{ id: 3, name: "赵六", pid: 15 },
{ id: 4, name: "张三", pid: 33 },
{ id: 5, name: "李四", pid: 34 },
{ id: 6, name: "王五", pid: 11 },
{ id: 7, name: "赵六", pid: 15 },
{ id: 8, name: "赵六", pid: 15 },
{ id: 9, name: "赵六", pid: 15 }
]);
this.app.queue = queue; this.app.queue = queue;
//调用一次真实数据 覆盖初始化数据 //调用一次真实数据 覆盖初始化数据
const { service, helper } = ctx; const { service, helper } = ctx;
const { USER_ACTIVE_DATA_LASI_ID } = helper.constant; const { USER_ACTIVE_DATA_LASI_ID } = helper.constant;
const _last_id = await service.cache.get(USER_ACTIVE_DATA_LASI_ID); const _last_id = await service.cache.get(USER_ACTIVE_DATA_LASI_ID) || 0;
// const _last_data = await service.screen.user.user_active({id:_last_id});
const data = this.app.queue.peek(); const data = this.app.queue.peek();
this.app.queue.fill(async ()=>await service.screen.user.activation({ limit: queue.size(), last_id:_last_id }),data) this.app.queue.fill(async ()=>await service.screen.user.activation({ limit: queue.size(), last_id:_last_id }),data)
.then(last_id=>last_id && service.cache.set(USER_ACTIVE_DATA_LASI_ID,last_id)) .then(last_id=>last_id && service.cache.set(USER_ACTIVE_DATA_LASI_ID,last_id))
......
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
module.exports = { module.exports = {
PREFIX: '_glsx_data_screen_', PREFIX: '_glsx_data_screen_',
USER_SUMMARY_30_DAY:'user_summary_30_day', USER_SUMMARY_30_DAY:'user_summary_30_day',
MERCHANT_SUMMARY_30_DAY:'merchant_summary_30_day',
MERCHANT_SUMMARY_LAST:'merchant_summary_last',
DATES:'dates', DATES:'dates',
USER_ACTIVE_DATA_LASI_ID:'user_active_data_last_id' USER_ACTIVE_DATA_LASI_ID:'user_active_data_last_id'
}; };
\ No newline at end of file \ No newline at end of file
/*
* @Author: limin
* @Date: 2019-11-21 11:20:01
* @Last Modified by: limin
* @Last Modified time: 2019-12-25 21:32:08
*/
"use strict";
const Controller = require("egg").Controller;
/**
* @Controller screen/user
* @Description 数据大屏-用户
*/
class MerchantController extends Controller {
constructor(ctx) {
super(ctx);
this.validate = ctx.app.validator.validate;
this.request = ctx.request;
this.helper = ctx.helper;
this.redis = ctx.app.redis;
this.MerchantService = ctx.service.screen.merchant;
this.CacheService = ctx.service.cache; //redis 缓存服务
this.ServerResponseCode = ctx.response.ServerResponseCode;
this.ServerResponse = ctx.response.ServerResponse;
}
/**
* 从内存获取日期(昨天,今天)
*/
async _fetch_date_in_cache_(){
const { DATES } = this.helper.constant;
return await this.CacheService.get(DATES);
}
/**
* 对比和获取内存中的数据(总共30天,每天通过定时任务更新)
* 如果获取的时候数据还是最新则手动触发定时任务更新一次
*/
async _compare_get_(isRunTask){
var { yesterday, cache_data_last_date_merchant } = await this._fetch_date_in_cache_();
if(!this.helper.moment(cache_data_last_date_merchant).isSame(yesterday) && isRunTask){ //如果 内存中的数据的日期不是昨天的 则手动启动定时器 更新缓存数据
await this.app.runSchedule('update_cache_merchant');
}
const { MERCHANT_SUMMARY_30_DAY, MERCHANT_SUMMARY_LAST } = this.helper.constant;
const summary_30_day = await this.CacheService.get(MERCHANT_SUMMARY_30_DAY) || [];
const summary_last = await this.CacheService.get(MERCHANT_SUMMARY_LAST)||[];
return {summary_30_day,summary_last}
}
/**
* 内部方法 从内存获取 昨天的统计数据
* 比较数据时间 如果不是昨天的数据 则启动定时任务 更新一遍数据 再次获取数据并返回
*/
async _fetch_summary_( { beginDate,endDate,isGetLast }, fn ){
var {summary_30_day,summary_last}= await this._compare_get_(isGetLast);
console.log(summary_30_day,summary_last);
const { moment } =this.helper
var result=[];
if(isGetLast){
// const { cache_data_last_date_merchant } = await this._fetch_date_in_cache_();
result = summary_last;
// fn && fn(cache_data_last_date_merchant,summary_last)
}else if(beginDate){
endDate = endDate || beginDate;
if(moment(beginDate).isSame(endDate) ){
result.push(summary_30_day.find(obj=>moment(beginDate).isSame(obj.day)))
// fn && fn(beginDate,result)
}else{
result = summary_30_day.filter(
obj=>moment(obj.day).isBetween(beginDate,endDate)
||moment(obj.day).isSame(beginDate)
||moment(obj.day).isSame(endDate))
// fn && fn(day,result)
}
}else{
result = summary_30_day
}
return result
}
async _fetch_total_({ field,isGetLast } ){
const _summary = await this._fetch_summary_({isGetLast});
return this._set_(field,(f)=>(_summary.reduce((pre,next)=>pre + +next[f],0)));
}
/**
* 内部帮助方法
* @param {*} field
* @param {*} fn
*/
async _set_(field,fn){
var total;
if(typeof field ==='string'){
total = await fn(field)
}else{
total = {}
for (const f of field) {
total[f] = await fn(f)
}
}
return total;
}
/**
* @Router GET /screen/merchant/total
* @Description 数据大屏-商户总数统计.---- 广联商户总数 商户累计订单数 当日订单数
*/
async total() {
var merchant_total = await this._fetch_total_({ field:'merchant_total', isGetLast:true})
var order_total = await this._fetch_total_({ field:'order_total', isGetLast:true})
const realtime = await this.MerchantService.merchant_realtime();
const realtime_new_order = realtime.reduce((pre,next)=>pre + +next['new_order'],0);
const realtime_new_merchant = realtime.reduce((pre,next)=>pre + +next['new_merchant'],0);
merchant_total += ~~realtime_new_merchant;
order_total += ~~realtime_new_order;
const data = { merchant_total, order_total, realtime_new_order };
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/merchant/statistics
* @Description 数据大屏-数据统计 1. 近七天订单数 2.商户订购设备数 3.商户订购设备总额 4.商户活跃度 5. 广联系统使用率
* @Request query integer pid 省份id
*/
async statistics() {
const summary = await this._fetch_total_({ field:['recent_7day_active','recent_7day_use','recent_7day_order','total_device','total_amount','active_merchant','use_sys_merchant']})
console.log(summary);
const data = { summary }
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/gender
* @Description 数据大屏-用户性别、年龄段、以及数量的占比 2: 00后,3: 90后,4: 80后,5: 70后,6: 60后,7:其他
* @Request query integer pid 省份id
* @Request query integer cid 城市id
*/
async gender({ query: { pid,cid } }) {
const data = await this.UserService.user_gender({ pid,cid })
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/city_range
* @Description 数据大屏-城市排行
* @Request query integer pid 省份id
* @Request query integer n 前 n名 默认 5
*/
async city_range({ query: { pid, n = 5 } }) {
const data = await this.UserService.city_range( { pid, limit:n })
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/city_range
* @Description 数据大屏-车辆品牌分布
* @Request query integer pid 省份id
* @Request query integer cid 城市id
* @Request query integer n 前 n名 默认 6
*/
async car_brand_range({ query: { pid, cid, n = 6 } }) {
const data = await this.UserService.car_brand_range( { pid, cid, limit:n })
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/drive_range
* @Description 数据大屏-出行分布
* @Request query integer pid 省份id
* @Request query integer cid 城市id
*/
async drive_range({ query: { pid, cid } }) {
const data = await this.UserService.drive_range( { pid, cid })
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/trend
* @Description 数据大屏-1 用户新增趋势 2.用户活跃趋势, 3.用户付费趋势 4.用户车辆出行趋势
* @Request query integer pid 省份id
* @Request query integer cid 城市id
* @Request query string type 趋势类别 u:用户总数 i: 用户新增趋势 a: 用户活跃趋势, r:用户付费趋势 j:用户车辆出行趋势(多个',' 隔开 默认:['u','i','a','r','j'])
* @Request query integer n 近N天数据 默认 30天
* @Request query string beginDate 查询开始时间 (2019-12-12)
* @Request query string endDate 查询结束时间 (2019-12-12)
*/
async trend({ query: { pid, cid, type, n = 30, beginDate , endDate } }) {
if(n){
if(n===30){
beginDate = endDate = null;
}else{
beginDate = this.helper.date(-n);
var { yesterday:endDate } = await this._fetch_date_in_cache_();
}
}
type = type ? ( type.includes(',') ? type.split(',') : type ) : [ 'u', 'i', 'a', 'r', 'j' ];
const data = await this._fetch_detail_by_range_( { pid, cid, field:type,beginDate, endDate } )
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/activation
* @Description 实时激活数据播报
* @Request query integer pid 省份id
*/
async activation({ query: { pid } }) {
const data = this.queue.peek();
const { USER_ACTIVE_DATA_LASI_ID } = this.helper.constant;
const _last_id = await this.CacheService.get(USER_ACTIVE_DATA_LASI_ID);
this.queue.fill(async ()=>await this.UserService.activation({ pid, limit: this.queue.size(), last_id:_last_id }))
.then(last_id=>last_id && this.CacheService.set(USER_ACTIVE_DATA_LASI_ID,last_id))
this.ctx.body = this.ServerResponse.Success(data);
}
/**
* @Router GET /screen/user/total_group_by_province
* @Description 获取每个省的总用户数
* @Request query integer pid 省份id
*/
async total_group_by_province({ query: { pid } }) {
const data = await this.UserService.total_group_by_province({ pid });
this.ctx.body = this.ServerResponse.Success(data);
}
}
module.exports = MerchantController;
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-21 11:20:01 * @Date: 2019-11-21 11:20:01
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-12-17 16:48:14 * @Last Modified time: 2019-12-23 17:25:22
*/ */
"use strict"; "use strict";
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-21 11:19:43 * @Date: 2019-11-21 11:19:43
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-12-17 16:46:08 * @Last Modified time: 2019-12-25 10:33:43
*/ */
const moment = require("moment"); const moment = require("moment");
......
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_device_online', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false,
unique: true
},
online_ddbox: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
online_gps: {
type: DataTypes.INTEGER(20),
allowNull: true,
defaultValue: '0'
},
online_obd: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_device_online'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_merchant_channel_stat', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
channel: {
type: DataTypes.STRING(64),
allowNull: false
},
merchant_total: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_merchant_channel_stat'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_merchant_daily_stat', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false
},
total_merchant: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
total_device: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
total_amount: {
type: "DOUBLE(12,2)",
allowNull: true,
defaultValue: '0.00'
},
active_merchant: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
use_sys_merchant: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
total_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
today_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
recent_7day_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
recent_7day_active: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
recent_7day_use: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_merchant_daily_stat'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_merchant_device_order_stat', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
month: {
type: DataTypes.STRING(16),
allowNull: false
},
order_total: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
sale_total: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_merchant_device_order_stat'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_merchant_order_detail', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
order_time: {
type: DataTypes.DATE,
allowNull: true
},
merchant: {
type: DataTypes.STRING(256),
allowNull: true
},
order_no: {
type: DataTypes.STRING(64),
allowNull: true
},
device_type: {
type: DataTypes.STRING(64),
allowNull: true
},
total: {
type: DataTypes.INTEGER(10),
allowNull: true
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_merchant_order_detail'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_merchant_order_device_stat', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
device_type: {
type: DataTypes.STRING(64),
allowNull: false
},
device_total: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_merchant_order_device_stat'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_merchant_order_stat', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false
},
maintain_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
repair_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
insurance_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
device_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_merchant_order_stat'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_merchant_realtime', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false,
unique: true
},
new_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
new_merchant: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_merchant_realtime'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_merchant_summary_stat', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false,
unique: true
},
type_4s: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
type_weixiu: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
type_yingai: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
type_jingpin: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
type_meibao: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
merchant_total: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
order_total: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
maintain_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
repair_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
insurance_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
device_order: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_merchant_summary_stat'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_carbrand', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
brand_name: {
type: DataTypes.STRING(32),
allowNull: false
},
brand_cars: {
type: DataTypes.INTEGER(10),
allowNull: true
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_city_carbrand'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_daily', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
day: {
type: DataTypes.DATE,
allowNull: false
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
total_user: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
renew: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
active: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
increase: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
journey: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
miles: {
type: "DOUBLE(12,2)",
allowNull: true,
defaultValue: '0.00'
},
online_time: {
type: DataTypes.BIGINT,
allowNull: true,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
},
recent_total: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
recent_7_day_active: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
recent_30_day_active: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
}
}, {
tableName: 't_user_city_daily'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_drive_info', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false,
unique: true
},
total: {
type: DataTypes.INTEGER(10),
allowNull: true
},
d_0_5: {
type: DataTypes.INTEGER(10),
allowNull: true
},
d_5_10: {
type: DataTypes.INTEGER(10),
allowNull: true
},
d_10_15: {
type: DataTypes.INTEGER(10),
allowNull: true
},
d_15_20: {
type: DataTypes.INTEGER(10),
allowNull: true
},
d_20_25: {
type: DataTypes.INTEGER(10),
allowNull: true
},
d_25_31: {
type: DataTypes.INTEGER(10),
allowNull: true
},
km_0_40: {
type: DataTypes.INTEGER(10),
allowNull: true
},
km_40_70: {
type: DataTypes.INTEGER(10),
allowNull: true
},
km_70_100: {
type: DataTypes.INTEGER(10),
allowNull: true
},
km_100_: {
type: DataTypes.INTEGER(10),
allowNull: true
},
w_0_10: {
type: DataTypes.INTEGER(10),
allowNull: true
},
w_10_20: {
type: DataTypes.INTEGER(10),
allowNull: true
},
w_20_30: {
type: DataTypes.INTEGER(10),
allowNull: true
},
w_30_50: {
type: DataTypes.INTEGER(10),
allowNull: true
},
w_50_: {
type: DataTypes.INTEGER(10),
allowNull: true
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_city_drive_info'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_gender', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
gender_range: {
type: DataTypes.STRING(32),
allowNull: false
},
total: {
type: DataTypes.INTEGER(10),
allowNull: false,
defaultValue: '0'
},
male: {
type: DataTypes.INTEGER(11),
allowNull: false,
defaultValue: '0'
},
female: {
type: DataTypes.INTEGER(11),
allowNull: false,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_city_gender'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_city_realtime', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false,
unique: true
},
online_total: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
online_time: {
type: DataTypes.BIGINT,
allowNull: true,
defaultValue: '0'
},
increase: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
active: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
renew: {
type: DataTypes.INTEGER(10),
allowNull: true,
defaultValue: '0'
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_city_realtime'
});
Model.associate = function() {
}
return Model;
};
/* indent size: 2 */
module.exports = app => {
const DataTypes = app.Sequelize;
const Model = app.model.define('t_user_new_active_detail', {
id: {
type: DataTypes.INTEGER(10),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
sn: {
type: DataTypes.STRING(32),
allowNull: false
},
active_time: {
type: DataTypes.DATE,
allowNull: false
},
prov_code: {
type: DataTypes.STRING(32),
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false
},
device_type: {
type: DataTypes.STRING(32),
allowNull: false
},
create_time: {
type: DataTypes.DATE,
allowNull: true
},
update_time: {
type: DataTypes.DATE,
allowNull: true
}
}, {
tableName: 't_user_new_active_detail'
});
Model.associate = function() {
}
return Model;
};
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-21 11:18:57 * @Date: 2019-11-21 11:18:57
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-11-21 11:18:57 * @Last Modified time: 2019-12-25 21:13:40
*/ */
'use strict'; 'use strict';
...@@ -17,4 +17,5 @@ module.exports = app => { ...@@ -17,4 +17,5 @@ module.exports = app => {
require('./router/demo')(app); require('./router/demo')(app);
require('./router/base')(app); require('./router/base')(app);
require('./router/screen/user')(app); require('./router/screen/user')(app);
require('./router/screen/merchant')(app);
}; };
/*
* @Author: limin
* @Date: 2019-11-21 11:18:45
* @Last Modified by: limin
* @Last Modified time: 2019-12-25 21:16:31
*/
module.exports = app => {
app.router.get('/screen/merchant/total', app.controller.screen.merchant.total);
app.router.get('/screen/merchant/statistics', app.controller.screen.merchant.statistics);
};
\ No newline at end of file \ No newline at end of file
...@@ -4,6 +4,7 @@ class UpdateCache extends Subscription { ...@@ -4,6 +4,7 @@ class UpdateCache extends Subscription {
constructor(ctx) { constructor(ctx) {
super(ctx); super(ctx);
this.UserService = ctx.service.screen.user; this.UserService = ctx.service.screen.user;
this.MerchantService = ctx.service.screen.merchant;
this.helper = ctx.helper; this.helper = ctx.helper;
this.CacheService = ctx.service.cache; //redis 缓存服务 this.CacheService = ctx.service.cache; //redis 缓存服务
} }
...@@ -86,10 +87,11 @@ class UpdateCache extends Subscription { ...@@ -86,10 +87,11 @@ class UpdateCache extends Subscription {
// subscribe 是真正定时任务执行时被运行的函数 // subscribe 是真正定时任务执行时被运行的函数
async subscribe() { async subscribe() {
this.logger.info("===============执行日常任务-缓存每日统计数据(总30天) begin=================="); this.logger.info("===============执行日常任务-缓存每日统计数据(用户总30天) begin==================");
const { moment, constant } = this.helper; const { moment, constant } = this.helper;
const { DATES, USER_SUMMARY_30_DAY } = constant; const { DATES,USER_SUMMARY_30_DAY } = constant;
var { cache_update_time, cache_data_last_date } = await this.CacheService.get(DATES); const dates = await this.CacheService.get(DATES);
var { cache_update_time, cache_data_last_date } = dates
const { user_cache_update_time_diff } = this.app.config.sys; const { user_cache_update_time_diff } = this.app.config.sys;
if( moment().diff(moment(cache_update_time)) < user_cache_update_time_diff ) { if( moment().diff(moment(cache_update_time)) < user_cache_update_time_diff ) {
this.logger.info( "=============== 距离上一次更新不到 %s 毫秒.不执行更新操作 ==================",user_cache_update_time_diff ); this.logger.info( "=============== 距离上一次更新不到 %s 毫秒.不执行更新操作 ==================",user_cache_update_time_diff );
...@@ -116,17 +118,13 @@ class UpdateCache extends Subscription { ...@@ -116,17 +118,13 @@ class UpdateCache extends Subscription {
} else { } else {
this.logger.warn("warnning=============user_summary_30_day===========is empty!"); this.logger.warn("warnning=============user_summary_30_day===========is empty!");
} }
dates.today = today
dates.yesterday = yesterday
dates.cache_data_last_date = cache_data_last_date
dates.cache_update_time = today
//缓存日期.方便业务使用 //缓存日期.方便业务使用
await this.CacheService.set( await this.CacheService.set(DATES,dates);
DATES, this.logger.info("===============执行日常任务-缓存每日统计数据(用户总30天) end==================");
{
today,
yesterday,
cache_data_last_date,
cache_update_time:moment()
}
);
this.logger.info("===============执行日常任务-缓存每日统计数据(总30天) end==================");
} }
} }
......
const Subscription = require("egg").Subscription;
class UpdateCacheMerchant extends Subscription {
constructor(ctx) {
super(ctx);
this.MerchantService = ctx.service.screen.merchant;
this.helper = ctx.helper;
this.CacheService = ctx.service.cache; //redis 缓存服务
}
/*
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ |
│ │ │ │ │ └ 星期 (0 - 7) (0或7都是星期日)
│ │ │ │ └───── 月份 (1 - 12)
│ │ │ └────────── 日期 (1 - 31)
│ │ └─────────────── 小时 (0 - 23)
│ └──────────────────── 分钟 (0 - 59)
└───────────────────────── 秒 (0 - 59, optional)
*/
// 通过 schedule 属性来设置定时任务的执行间隔等配置
static get schedule() {
return {
// interval: '1m', // 定时任务 1 分钟间隔
cron: "0 30 6 * * *", // 定点任务 每天 凌晨 6点30 执行一次
type: "worker", // 随机 worker 执行
immediate: true //启动立即执行一次
// disable:true // 不启动
};
}
// subscribe 是真正定时任务执行时被运行的函数
async subscribe() {
this.logger.info("===============执行日常任务-缓存每日统计数据(商户总30天) begin==================");
const { moment, constant } = this.helper;
const { DATES,MERCHANT_SUMMARY_30_DAY } = constant;
const dates = await this.CacheService.get(DATES);
var { cache_update_time_merchant, cache_data_last_date_merchant } = dates
const { user_cache_update_time_diff } = this.app.config.sys;
if( moment().diff(moment(cache_update_time_merchant)) < user_cache_update_time_diff ) {
this.logger.info( "=============== 距离上一次更新不到 %s 毫秒.不执行更新操作 ==================",user_cache_update_time_diff );
return ;
}
const yesterday = this.helper.date(-1);
const day30before = this.helper.date(-30);
// 查询昨天 所有城市 分别 对应的 用户总数 列表
const _merchants_summary = await this.MerchantService.merchant_summary_last()
const _merchants_daily = await this.MerchantService.merchant_summary_by_day({beginDate: day30before, endDate: yesterday})
if (_merchants_daily.length) {
const date = this.helper.moment(_merchants_daily[_merchants_daily.length-1].day)
if( moment(date).isAfter( cache_data_last_date_merchant) ){ //当前数据最新时间 大于 上一次缓存数据的最新时间 才写入缓存
await this.CacheService.set(
MERCHANT_SUMMARY_30_DAY,
_merchants_daily
);
await this.CacheService.set(
MERCHANT_SUMMARY_LAST,
_merchants_summary
);
this.logger.info("merchant_summary_%s",date);
}else{
this.logger.info("cache_date:%s date:%s 不写缓存",cache_data_last_date_merchant,date);
}
}else {
this.logger.warn("warnning=============merchant_summary_30_day===========is empty!");
}
dates.cache_data_last_date_merchant = cache_data_last_date_merchant
dates.cache_update_time_merchant = moment()
//缓存日期.方便业务使用
await this.CacheService.set(DATES,dates);
this.logger.info("===============执行日常任务-缓存每日商户统计数据(商户总30天) end==================");
}
}
module.exports = UpdateCacheMerchant;
/*
* @Author: limin
* @Date: 2019-11-21 11:18:37
* @Last Modified by: limin
* @Last Modified time: 2019-12-25 21:25:11
*/
const Service = require("egg").Service;
// var moment = require('moment');/
class MerchantService extends Service {
constructor(ctx) {
super(ctx);
this.helper = ctx.helper;
this._reg_models_alias();
this._associate();
}
/**
* 绑定model 在 this 上的 别名
*/
_reg_models_alias() {
const {
TDeviceOnline,
TMerchantChannerStat,
TMerchantDailyStat,
TMerchantDeviceOrderStat,
TMerchantOrderDetail,
TMerchantOrderDeviceStat,
TMerchantOrderStat,
TMerchantSummaryStat,
TMerchantRealtime,
TProvince,
TCity
} = this.app.model.Screen.Merchant;
this.MDO = TDeviceOnline;
this.MCS = TMerchantChannerStat;
this.MCDS = TMerchantDailyStat;
this.MDOS = TMerchantDeviceOrderStat;
this.MOD = TMerchantOrderDetail;
this.MODS = TMerchantOrderDeviceStat;
this.MOS = TMerchantOrderStat;
this.MSS = TMerchantSummaryStat;
this.MRT = TMerchantRealtime;
this.MProvice = TProvince;
this.MCity = TCity;
}
/**
* 绑定表关系
*/
_associate() {
}
__build__option__( opt={} ){
let {day,beginDate,endDate,limit,order} = opt
const { between } = this.app.Sequelize.Op;
let where = {};
if (day) {
//如果有具体天数 就直接查询具体一天的数据(优先具体哪一天)
where.day = day;
} else if (beginDate) {
if (!endDate) {
endDate = beginDate;
}
if (!endDate.includes(":")) {
endDate = this.helper.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 merchant_summary_last() {
var option = this.__build__option__();
const data = await this.MSS.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 获取商户统计数 (天)
*/
async merchant_summary_by_day({beginDate, endDate}) {
var option = this.__build__option__({beginDate, endDate});
const data = await this.MCDS.findAll(option);
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 获取实时数据(详细)
*/
async merchant_realtime() {
var option = this.__build__option__();
const data = await this.MRT.findAll(option);
return data;
}
}
module.exports = MerchantService;
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-21 11:18:37 * @Date: 2019-11-21 11:18:37
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-12-20 19:00:31 * @Last Modified time: 2019-12-25 10:44:44
*/ */
const Service = require("egg").Service; const Service = require("egg").Service;
...@@ -82,21 +82,11 @@ class UserService extends Service { ...@@ -82,21 +82,11 @@ class UserService extends Service {
limit, limit,
order: [["total_user", "DESC"]] order: [["total_user", "DESC"]]
}); });
if(pid){
Object.assign(option.where,{
[Op.and]:{
city_code:{
[Op.gt]: 0,
}
}
})
}else{
Object.assign(option.where,{ Object.assign(option.where,{
city_code:{ city_code:{
[Op.gt]: 0, [Op.gt]: 0,
} }
}) })
}
Object.assign(option, { Object.assign(option, {
attributes: [ attributes: [
'city_code', 'city_code',
...@@ -259,7 +249,7 @@ class UserService extends Service { ...@@ -259,7 +249,7 @@ class UserService extends Service {
* @param field {String} * @param field {String}
* @param value {String} * @param value {String}
* @return {Promise.<boolean>} * @return {Promise.<boolean>}
* @Description 获取用户性别年龄数量占比 * @Description 获取用户激活详情
*/ */
async activation({ pid, limit, last_id }) { async activation({ pid, limit, last_id }) {
const { app, helper, MUserActive } = this; const { app, helper, MUserActive } = this;
...@@ -278,6 +268,18 @@ class UserService extends Service { ...@@ -278,6 +268,18 @@ class UserService extends Service {
* @param field {String} * @param field {String}
* @param value {String} * @param value {String}
* @return {Promise.<boolean>} * @return {Promise.<boolean>}
* @Description 根据id 获取 激活数据
*/
async user_active({ id }) {
const { MUserActive } = this;
const data = await MUserActive.findOne({where:{id},raw:true});
return data;
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 获取每个省的用户数 * @Description 获取每个省的用户数
*/ */
async total_group_by_province({ pid }) { async total_group_by_province({ pid }) {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-16 09:26:53 * @Date: 2019-11-16 09:26:53
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-12-19 16:38:17 * @Last Modified time: 2019-12-24 11:26:23
*/ */
/* eslint valid-jsdoc: "off" */ /* eslint valid-jsdoc: "off" */
...@@ -97,16 +97,12 @@ module.exports = app => { ...@@ -97,16 +97,12 @@ module.exports = app => {
underscored: true, // 注意需要加上这个, egg-sequelize只是简单的使用Object.assign对配置和默认配置做了merge, 如果不加这个 update_at会被转变成 updateAt故报错 underscored: true, // 注意需要加上这个, egg-sequelize只是简单的使用Object.assign对配置和默认配置做了merge, 如果不加这个 update_at会被转变成 updateAt故报错
freezeTableName: true, // 禁止修改表名,默认情况下,sequelize将自动将所有传递的模型名称(define的第一个参数)转换为复数 freezeTableName: true, // 禁止修改表名,默认情况下,sequelize将自动将所有传递的模型名称(define的第一个参数)转换为复数
// 但是为了安全着想,复数的转换可能会发生变化,所以禁止该行为 // 但是为了安全着想,复数的转换可能会发生变化,所以禁止该行为
timestamps: false, // 不自动维护时间戳 [ created_at、updated_at ] timestamps: true, // 不自动维护时间戳 [ created_at、updated_at ]
createdAt: false,
updatedAt: false,
dialectOptions: { dialectOptions: {
dateStrings: true, dateStrings: true,
typeCast(field, next) { typeCast: true
// for reading from database
if (field.type === "DATETIME") {
return field.string();
}
return next();
}
} }
} }
}; };
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* @Author: limin * @Author: limin
* @Date: 2019-11-16 09:26:47 * @Date: 2019-11-16 09:26:47
* @Last Modified by: limin * @Last Modified by: limin
* @Last Modified time: 2019-12-19 18:07:10 * @Last Modified time: 2019-12-24 11:14:36
*/ */
/* eslint valid-jsdoc: "off" */ /* eslint valid-jsdoc: "off" */
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!