Authored by 李奇

添加上报

... ... @@ -6,6 +6,45 @@ const _ = require('lodash');
const logger = global.yoho.logger;
const mysqlCli = global.yoho.utils.mysqlCli;
const _sender = global.yoho.sender;
/**
* 上报
* @param type 0: packrain 1: turntable
* @param tags 一些基础参数
* @param fields 红包的金额
* @param isFail 接口是否失败
*/
const sendMessage = (type, tags, fields, isFail) => {
if (!_sender) {
return;
}
const hostname = (require('os').hostname());
let typeName = ['turntable-red', 'turntable-coupon'][type];
let initTags = {
hostname,
pid: process.pid || 0,
app: global.yoho.config.appName,
type: typeName
};
tags = Object.assign({}, initTags, tags);
isFail = isFail ? 1 : 0;
fields = Object.assign({}, fields);
let mesurement = ['monitor_success_report', 'monitor_error_report'][isFail];
try {
_sender.addMessage({
measurement: mesurement,
tags,
fields
});
} catch (e) {
console.error(e);
}
};
class ActWheelSurfModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
... ... @@ -21,12 +60,12 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
// if (data) {
// data = JSON.parse(data);
// } else {
// 从redis取,如果没有则去数据查询,然后再到放到redis
// 获取活动基础
// 从redis取,如果没有则去数据查询,然后再到放到redis
// 获取活动基础
data.conf = await ActWheelSurfConf.findOne({where: {act_id: obj.act_id}});
// 获取当前活动奖品
// 获取当前活动奖品
let prize = await ActWheelSurfPrize.findAll({where: {act_id: obj.act_id}});
data.prize = prize.length;
... ... @@ -69,8 +108,8 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
obj.create_time = {gt: startTime, lte: endTime};
residueCount = data.conf.day_limit_times - await ActWheelSurfUser.count({
where: obj
});
where: obj
});
} else {
// 活动最大次数
residueCount = data.conf.act_total_times - await ActWheelSurfUser.count({where: obj});
... ... @@ -119,14 +158,14 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
// 计算用户剩余次数校验
if (!conf.times_type) {
// 获取当天0点和23:59:59
// 获取当天0点和23:59:59
let endTime = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);
let startTime = new Date(new Date(new Date().toLocaleDateString()).getTime());
obj.create_time = {gt: startTime, lte: endTime};
residueCount = conf.day_limit_times - await ActWheelSurfUser.count({
where: obj
});
where: obj
});
} else {
// 活动最大次数
residueCount = conf.act_total_times - await ActWheelSurfUser.count({where: obj});
... ... @@ -140,7 +179,7 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
// 获取每个奖品获取它们的中奖范围
let arr = {}, oldValue;
prize.map(function(value, index) {
prize.map(function (value, index) {
if (value.chance) {
if (typeof(oldValue) === 'undefined') {
arr[index] = {maxValue: value.chance, leastValue: 0};
... ... @@ -170,7 +209,7 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
if (getPrize.type !== 1) {
// 校验剩余库存
if (getPrize.total_left > 0) {
// 减库存
// 减库存
await getPrize.decrement('total_left');
} else {
// 查询有没有等于type1(谢谢惠顾)的商品 如果没有 返回概率最商品
... ... @@ -188,28 +227,60 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
sendResult.code = 200;
getPrize.desc = '对不起,您没有中奖';
} else if (getPrize.type === 2) {
// 调用java接口
sendResult = await this.get({data: {
method: 'app.redpac.addRedpac',
actSource: 1001,
uid: uid,
redpacToken: getPrize.value}
});
if (sendResult && sendResult.code === 200) {
if (sendResult.data.isRepReceive === 'N') {
try {
// 调用java接口
sendResult = await this.get({
data: {
method: 'app.redpac.addRedpac',
actSource: 1001,
uid: uid,
redpacToken: getPrize.value
}
});
if (sendResult && sendResult.data) {
sendMessage(0, {}, {succeedTimes: sendResult.data.amount || 0}, 0);
if (sendResult && sendResult.code === 200) {
if (sendResult.data.isRepReceive === 'N') {
sendResult.code = 201; // 不能重复领取
} else {
} else {
getPrize.desc = '恭喜您获得' + sendResult.data.amount + '元红包';
}
}
} else {
sendMessage(0, {}, {
message: sendResult.message || 'no result',
stack: Object.assign({}, {code: sendResult.code})
}, 1);
}
} catch (e) {
sendMessage(0, {}, {message: e.message || 'interface error', stack: e}, 1);
}
} else if (getPrize.type === 3) {
sendResult = await this.get({
method: 'app.coupons.couponsSend',
uid: uid,
coupon_send_token: getPrize.value
});
if (sendResult && sendResult.code === 200) {
getPrize.desc = '恭喜您获得' + getPrize.name + '优惠券';
try {
sendResult = await this.get({
method: 'app.coupons.couponsSend',
uid: uid,
coupon_send_token: getPrize.value
});
if (sendResult && sendResult.data) {
sendMessage(1, {}, {succeedTimes: sendResult.data.amount || 0}, 0);
if (sendResult && sendResult.code === 200) {
getPrize.desc = '恭喜您获得' + getPrize.name + '优惠券';
}
} else {
sendMessage(1, {}, {
message: sendResult.message || 'no result',
stack: Object.assign({}, {code: sendResult.code})
}, 1);
}
} catch (e) {
sendMessage(1, {}, {message: e.message || 'interface error', stack: e}, 1);
}
} else if (getPrize.type === 4) {
sendResult.code = 200;
... ... @@ -250,7 +321,7 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
async getUserPrize(obj) {
try {
return await mysqlCli.query(
`select u.id, u.act_id act_id, u.prize_id prize_id,p.name name,p.type type ,p.value value,p.img img
`select u.id, u.act_id act_id, u.prize_id prize_id,p.name name,p.type type ,p.value value,p.img img
from act_wheel_surf_user u , act_wheel_surf_prize p where u.prize_id = p.id and u.act_id =:act_id and u.uid = :uid and type != :type
order by u.create_time desc`, {act_id: obj.act_id, uid: obj.uid, type: 1});
... ...
... ... @@ -2663,7 +2663,7 @@ debug@^4.0.0:
dependencies:
ms "^2.1.1"
debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "http://npm.yohops.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
... ... @@ -4857,7 +4857,7 @@ import-local@^1.0.0:
pkg-dir "^2.0.0"
resolve-cwd "^2.0.0"
imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "http://npm.yohops.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
... ... @@ -4894,7 +4894,7 @@ inflight@^1.0.4, inflight@~1.0.6:
once "^1.3.0"
wrappy "1"
influx-batch-sender@^0.1.6, influx-batch-sender@^0.1.7:
influx-batch-sender@^0.1.7, influx-batch-sender@^0.1.9:
version "0.1.10"
resolved "http://npm.yohops.com/influx-batch-sender/-/influx-batch-sender-0.1.10.tgz#697e51ab8e5aa20fb9cc3b07a2922e069b86c229"
dependencies:
... ... @@ -5829,10 +5829,6 @@ lodash._baseget@^3.0.0:
version "3.7.2"
resolved "http://npm.yohops.com/lodash._baseget/-/lodash._baseget-3.7.2.tgz#1b6ae1d5facf3c25532350a13c1197cb8bb674f4"
lodash._baseindexof@*:
version "3.1.0"
resolved "http://npm.yohops.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
lodash._baseisequal@^3.0.0:
version "3.0.7"
resolved "http://npm.yohops.com/lodash._baseisequal/-/lodash._baseisequal-3.0.7.tgz#d8025f76339d29342767dcc887ce5cb95a5b51f1"
... ... @@ -5856,25 +5852,15 @@ lodash._basevalues@^3.0.0:
version "3.0.0"
resolved "http://npm.yohops.com/lodash._basevalues/-/lodash._basevalues-3.0.0.tgz#5b775762802bde3d3297503e26300820fdf661b7"
lodash._bindcallback@*, lodash._bindcallback@^3.0.0:
lodash._bindcallback@^3.0.0:
version "3.0.1"
resolved "http://npm.yohops.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
lodash._cacheindexof@*:
version "3.0.2"
resolved "http://npm.yohops.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
lodash._createcache@*:
version "3.1.2"
resolved "http://npm.yohops.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
dependencies:
lodash._getnative "^3.0.0"
lodash._createset@~4.0.0:
version "4.0.3"
resolved "http://npm.yohops.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
lodash._getnative@*, lodash._getnative@^3.0.0:
lodash._getnative@^3.0.0:
version "3.9.1"
resolved "http://npm.yohops.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
... ... @@ -6033,7 +6019,7 @@ lodash.reject@^4.4.0:
version "4.6.0"
resolved "http://npm.yohops.com/lodash.reject/-/lodash.reject-4.6.0.tgz#80d6492dc1470864bbf583533b651f42a9f52415"
lodash.restparam@*, lodash.restparam@^3.0.0:
lodash.restparam@^3.0.0:
version "3.6.1"
resolved "http://npm.yohops.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
... ... @@ -9159,7 +9145,7 @@ readable-stream@~2.0.0:
string_decoder "~0.10.x"
util-deprecate "~1.0.1"
readdir-scoped-modules@*, readdir-scoped-modules@^1.0.0:
readdir-scoped-modules@^1.0.0:
version "1.0.2"
resolved "http://npm.yohops.com/readdir-scoped-modules/-/readdir-scoped-modules-1.0.2.tgz#9fafa37d286be5d92cbaebdee030dc9b5f406747"
dependencies:
... ... @@ -12081,7 +12067,7 @@ yoho-mlellipsis@0.0.3:
version "0.0.3"
resolved "http://npm.yohops.com/yoho-mlellipsis/-/yoho-mlellipsis-0.0.3.tgz#5f743eeb74ff6f2df58a802cff7cd8ad8e4b21f4"
yoho-node-lib@^0.6.23:
yoho-node-lib@^0.6.33:
version "0.6.34"
resolved "http://npm.yohops.com/yoho-node-lib/-/yoho-node-lib-0.6.34.tgz#c650e06bfc530cf13c79cf4b3e08bc5d15345933"
dependencies:
... ... @@ -12115,7 +12101,7 @@ yoho-swiper@^3.3.1:
version "3.3.2"
resolved "http://npm.yohops.com/yoho-swiper/-/yoho-swiper-3.3.2.tgz#a7b0f6e13de38d8ac8bb90badbbc316889a0c214"
yoho-zookeeper@^1.0.11:
yoho-zookeeper@^1.0.8:
version "1.0.11"
resolved "http://npm.yohops.com/yoho-zookeeper/-/yoho-zookeeper-1.0.11.tgz#a4fe2a7176de91971be4f03c9552720b93a9ffe3"
dependencies:
... ...