Commit 4ec80cd2 by liimin

.

1 parent 43247fc8
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-21 11:20:01
* @Last Modified by: limin
* @Last Modified time: 2019-11-26 15:37:02
* @Last Modified time: 2019-11-27 19:11:39
*/
"use strict";
......@@ -16,7 +16,6 @@ class UserController extends Controller {
constructor(ctx) {
super(ctx);
this.validate = ctx.app.validator.validate;
this.session = ctx.session;
this.request = ctx.request;
this.helper = ctx.helper;
this.redis = ctx.app.redis;
......@@ -31,7 +30,13 @@ class UserController extends Controller {
* @Request query integer pid 省份id
*/
async total({ query: { pid } }) {
const statics = await this.UserService.total(pid);
var statics = await this.UserService.user_summary_provice_by_day(this.helper.date(-1),pid)
// console.log(this.app.cache.today);
// `user_summary_${day}`
// const data = await this.CacheService.get("test", () =>
// this.DemoService.hello(this.request.body)
// );
// const statics = await this.UserService.total(pid);
const data = { statics };
this.ctx.body = this.ServerResponse.Success(data);
}
......
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-21 11:19:43
* @Last Modified by: limin
* @Last Modified time: 2019-11-26 16:03:10
* @Last Modified time: 2019-11-27 19:10:48
*/
const moment = require("moment");
......@@ -16,16 +16,30 @@ exports.date = (n, f = "YYYY-MM-DD") =>
/**
* 简单封装 sequelize 查询参数
*/
exports.seqOption = function({ pid, n, limit, order }) {
exports.seqOption = function({ pid, n, limit, order, day,beginDate,endDate }) {
let where = {};
const { between } = this.ctx.app.Sequelize.Op
const { between } = this.ctx.app.Sequelize.Op;
if (pid) {
where.prov_code = pid;
}
if (n || n === 0) {
if (day) {
//如果有具体天数 就直接查询具体一天的数据(优先具体哪一天)
where.day = day;
} else if (n || n === 0) {
// 查询近N天的数据 0 表示当天
where.day = {
[between]: [this.date(-n), this.date(0)]
};
} else if(beginDate){
if(!endDate){
endDate = beginDate
}
if(!endDate.includes(':')){
endDate = `${endDate} 23:59:59`
}
where.day = {
[between]: [beginDate, endDate]
};
}
const option = {
where
......@@ -38,4 +52,4 @@ exports.seqOption = function({ pid, n, limit, order }) {
}
return option;
};
exports.constant = constant
exports.constant = constant;
......@@ -14,6 +14,10 @@ module.exports = app => {
type: DataTypes.STRING(32),
allowNull: false
},
day: {
type: DataTypes.DATE,
allowNull: false
},
city_code: {
type: DataTypes.STRING(32),
allowNull: false,
......
......@@ -22,25 +22,34 @@ class UpdateCache extends Subscription {
static get schedule() {
return {
// interval: '1m', // 定时任务 1 分钟间隔
cron: "0 30 0 * * *", // 定点任务 每天 凌晨 0点30 执行一次
type: "all", // 指定所有的 worker 都需要执行
immediate: true //启动立即执行一次
cron: "0 33 9 * * *", // 定点任务 每天 凌晨 0点30 执行一次
type: "worker", // 随机 worker 执行
immediate: true, //启动立即执行一次
// disable:true // 不启动
};
}
// subscribe 是真正定时任务执行时被运行的函数
async subscribe() {
this.logger.info('===============执行日常任务-缓存每日用户总数 begin==================')
const today_summary = await this.UserService.user_summary_today();
if (today_summary.length) {
const [{ day, total }] = today_summary;
const value = JSON.stringify({ day, total });
this.logger.info('today_summary:%s',value)
await this.CacheService.set(
`user_summary_${day}`,
value,
25 * 60 * 60 //25小时有效
);
}
var total_yesterday = await this.UserService.user_summary_provice_by_day(this.helper.date(-1))
// if(total_yesterday.length){
// total_yesterday.map(item=>)
// }
// const today_summary = await this.UserService.user_summary_today();
// if (today_summary.length) {
// const [{ day, total }] = today_summary;
// const value = JSON.stringify({ day, total });
// this.logger.info('today_summary:%s',value)
// await this.CacheService.set(
// `user_summary_${day}`,
// value,
// 25 * 60 * 60 //25小时有效
// );
// }
this.app.cache = {
today: this.helper.date(0)
};
this.logger.info('===============执行日常任务-缓存每日用户总数 end==================')
}
}
......
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-21 11:18:37
* @Last Modified by: limin
* @Last Modified time: 2019-11-26 15:51:19
* @Last Modified time: 2019-11-27 19:04:47
*/
const Service = require("egg").Service;
......@@ -10,24 +10,53 @@ const Service = require("egg").Service;
class UserService extends Service {
constructor(ctx) {
super(ctx);
this.session = ctx.session;
this.helper = ctx.helper;
this.Op = ctx.app.Sequelize.Op;
this.MCityActive = ctx.model.TUserCityActiveDaily;
this.MCityIncreased = ctx.model.TUserCityIncreasedDaily;
this.MCityRenew = ctx.model.TUserCityRenewDaily;
this.MCityGender = ctx.model.TUserCityGender;
this.MCityCarBrand = ctx.model.TUserCityCarbrand;
this.MCityCarPrice = ctx.model.TUserCityCarprice;
this.MCity = ctx.model.TUserCity;
this.MCityMilesDaily = ctx.model.TUserCityAverageMilesDay;
this.MCityDriveDaysMonth = ctx.model.TUserCityAverageDriveMonth;
this.MCityJourney = ctx.model.TUserCityJourneyDaily;
this.MUserSummary = ctx.model.TUserSummary;
this.ResponseCode = ctx.response.ResponseCode;
this.ServerResponse = ctx.response.ServerResponse;
this._reg_models_alias();
this._associate();
}
/**
* 绑定model 在 this 上的 别名
*/
_reg_models_alias() {
const {
TUserCityActiveDaily,
TUserCityIncreasedDaily,
TUserCityRenewDaily,
TUserCityGender,
TUserCityCarbrand,
TUserCityCarprice,
TUserCity,
TUserCityAverageMilesDay,
TUserCityAverageDriveMonth,
TUserCityJourneyDaily,
TUserSummary,
TProvince
} = this.app.model;
this.MCityActive = TUserCityActiveDaily;
this.MCityIncreased = TUserCityIncreasedDaily;
this.MCityRenew = TUserCityRenewDaily;
this.MCityGender = TUserCityGender;
this.MCityCarBrand = TUserCityCarbrand;
this.MCityCarPrice = TUserCityCarprice;
this.MUserCity = TUserCity;
this.MCityMilesDaily = TUserCityAverageMilesDay;
this.MCityDriveDaysMonth = TUserCityAverageDriveMonth;
this.MCityJourney = TUserCityJourneyDaily;
this.MUserSummary = TUserSummary;
this.MProvice = TProvince;
}
/**
* 绑定表关系
*/
_associate() {
this.MUserCity.belongsTo(this.MProvice, {
foreignKey: "prov_code",
targetKey: "prov_code"
});
}
/**
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
......@@ -195,10 +224,24 @@ class UserService extends Service {
* @param field {String}
* @param value {String}
* @return {Promise.<boolean>}
* @Description 获取当天用户总数
* @Description 获取某天用户总数
*/
async user_summary_today() {
const data = await this.MUserSummary.findAll({order:[['day', 'DESC']],limit:1, raw: true});
async user_summary_provice_by_day(beginDate, endDate, pid) {
const { app, helper, MProvice, MUserCity } = this;
const { fn, col } = app.Sequelize;
var option = helper.seqOption({ beginDate, endDate, pid });
Object.assign(option, {
attributes: [
"prov_code",
"day",
[fn("SUM", col("total")), "total"],
col("t_province.prov_name")
],
group: ["prov_code", "day"],
include: { model: MProvice, attributes: [] },
raw: true
});
const data = await MUserCity.findAll(option);
return data;
}
}
......
......@@ -2,7 +2,7 @@
* @Author: limin
* @Date: 2019-11-16 09:26:53
* @Last Modified by: limin
* @Last Modified time: 2019-11-26 16:01:04
* @Last Modified time: 2019-11-26 18:29:28
*/
/* eslint valid-jsdoc: "off" */
......@@ -83,7 +83,7 @@ module.exports = app => {
client: {
port: 6379, // Redis port
host: "127.0.0.1", // Redis host
password: "auth",
password: "",
db: 0
}
};
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!