Commit 4ec80cd2 by liimin

.

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