Authored by 李奇

添加上报

@@ -6,6 +6,45 @@ const _ = require('lodash'); @@ -6,6 +6,45 @@ const _ = require('lodash');
6 const logger = global.yoho.logger; 6 const logger = global.yoho.logger;
7 const mysqlCli = global.yoho.utils.mysqlCli; 7 const mysqlCli = global.yoho.utils.mysqlCli;
8 8
  9 +const _sender = global.yoho.sender;
  10 +
  11 +/**
  12 + * 上报
  13 + * @param type 0: packrain 1: turntable
  14 + * @param tags 一些基础参数
  15 + * @param fields 红包的金额
  16 + * @param isFail 接口是否失败
  17 + */
  18 +const sendMessage = (type, tags, fields, isFail) => {
  19 + if (!_sender) {
  20 + return;
  21 + }
  22 + const hostname = (require('os').hostname());
  23 + let typeName = ['turntable-red', 'turntable-coupon'][type];
  24 + let initTags = {
  25 + hostname,
  26 + pid: process.pid || 0,
  27 + app: global.yoho.config.appName,
  28 + type: typeName
  29 + };
  30 +
  31 + tags = Object.assign({}, initTags, tags);
  32 + isFail = isFail ? 1 : 0;
  33 + fields = Object.assign({}, fields);
  34 +
  35 + let mesurement = ['monitor_success_report', 'monitor_error_report'][isFail];
  36 +
  37 + try {
  38 + _sender.addMessage({
  39 + measurement: mesurement,
  40 + tags,
  41 + fields
  42 + });
  43 + } catch (e) {
  44 + console.error(e);
  45 + }
  46 +};
  47 +
9 class ActWheelSurfModel extends global.yoho.BaseModel { 48 class ActWheelSurfModel extends global.yoho.BaseModel {
10 constructor(ctx) { 49 constructor(ctx) {
11 super(ctx); 50 super(ctx);
@@ -21,12 +60,12 @@ class ActWheelSurfModel extends global.yoho.BaseModel { @@ -21,12 +60,12 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
21 // if (data) { 60 // if (data) {
22 // data = JSON.parse(data); 61 // data = JSON.parse(data);
23 // } else { 62 // } else {
24 - // 从redis取,如果没有则去数据查询,然后再到放到redis  
25 - // 获取活动基础 63 + // 从redis取,如果没有则去数据查询,然后再到放到redis
  64 + // 获取活动基础
26 65
27 data.conf = await ActWheelSurfConf.findOne({where: {act_id: obj.act_id}}); 66 data.conf = await ActWheelSurfConf.findOne({where: {act_id: obj.act_id}});
28 67
29 - // 获取当前活动奖品 68 + // 获取当前活动奖品
30 let prize = await ActWheelSurfPrize.findAll({where: {act_id: obj.act_id}}); 69 let prize = await ActWheelSurfPrize.findAll({where: {act_id: obj.act_id}});
31 70
32 data.prize = prize.length; 71 data.prize = prize.length;
@@ -69,8 +108,8 @@ class ActWheelSurfModel extends global.yoho.BaseModel { @@ -69,8 +108,8 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
69 108
70 obj.create_time = {gt: startTime, lte: endTime}; 109 obj.create_time = {gt: startTime, lte: endTime};
71 residueCount = data.conf.day_limit_times - await ActWheelSurfUser.count({ 110 residueCount = data.conf.day_limit_times - await ActWheelSurfUser.count({
72 - where: obj  
73 - }); 111 + where: obj
  112 + });
74 } else { 113 } else {
75 // 活动最大次数 114 // 活动最大次数
76 residueCount = data.conf.act_total_times - await ActWheelSurfUser.count({where: obj}); 115 residueCount = data.conf.act_total_times - await ActWheelSurfUser.count({where: obj});
@@ -119,14 +158,14 @@ class ActWheelSurfModel extends global.yoho.BaseModel { @@ -119,14 +158,14 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
119 158
120 // 计算用户剩余次数校验 159 // 计算用户剩余次数校验
121 if (!conf.times_type) { 160 if (!conf.times_type) {
122 - // 获取当天0点和23:59:59 161 + // 获取当天0点和23:59:59
123 let endTime = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1); 162 let endTime = new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000 - 1);
124 let startTime = new Date(new Date(new Date().toLocaleDateString()).getTime()); 163 let startTime = new Date(new Date(new Date().toLocaleDateString()).getTime());
125 164
126 obj.create_time = {gt: startTime, lte: endTime}; 165 obj.create_time = {gt: startTime, lte: endTime};
127 residueCount = conf.day_limit_times - await ActWheelSurfUser.count({ 166 residueCount = conf.day_limit_times - await ActWheelSurfUser.count({
128 - where: obj  
129 - }); 167 + where: obj
  168 + });
130 } else { 169 } else {
131 // 活动最大次数 170 // 活动最大次数
132 residueCount = conf.act_total_times - await ActWheelSurfUser.count({where: obj}); 171 residueCount = conf.act_total_times - await ActWheelSurfUser.count({where: obj});
@@ -140,7 +179,7 @@ class ActWheelSurfModel extends global.yoho.BaseModel { @@ -140,7 +179,7 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
140 // 获取每个奖品获取它们的中奖范围 179 // 获取每个奖品获取它们的中奖范围
141 let arr = {}, oldValue; 180 let arr = {}, oldValue;
142 181
143 - prize.map(function(value, index) { 182 + prize.map(function (value, index) {
144 if (value.chance) { 183 if (value.chance) {
145 if (typeof(oldValue) === 'undefined') { 184 if (typeof(oldValue) === 'undefined') {
146 arr[index] = {maxValue: value.chance, leastValue: 0}; 185 arr[index] = {maxValue: value.chance, leastValue: 0};
@@ -170,7 +209,7 @@ class ActWheelSurfModel extends global.yoho.BaseModel { @@ -170,7 +209,7 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
170 if (getPrize.type !== 1) { 209 if (getPrize.type !== 1) {
171 // 校验剩余库存 210 // 校验剩余库存
172 if (getPrize.total_left > 0) { 211 if (getPrize.total_left > 0) {
173 - // 减库存 212 + // 减库存
174 await getPrize.decrement('total_left'); 213 await getPrize.decrement('total_left');
175 } else { 214 } else {
176 // 查询有没有等于type1(谢谢惠顾)的商品 如果没有 返回概率最商品 215 // 查询有没有等于type1(谢谢惠顾)的商品 如果没有 返回概率最商品
@@ -188,28 +227,60 @@ class ActWheelSurfModel extends global.yoho.BaseModel { @@ -188,28 +227,60 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
188 sendResult.code = 200; 227 sendResult.code = 200;
189 getPrize.desc = '对不起,您没有中奖'; 228 getPrize.desc = '对不起,您没有中奖';
190 } else if (getPrize.type === 2) { 229 } else if (getPrize.type === 2) {
191 - // 调用java接口  
192 - sendResult = await this.get({data: {  
193 - method: 'app.redpac.addRedpac',  
194 - actSource: 1001,  
195 - uid: uid,  
196 - redpacToken: getPrize.value}  
197 - });  
198 - if (sendResult && sendResult.code === 200) {  
199 - if (sendResult.data.isRepReceive === 'N') { 230 +
  231 + try {
  232 + // 调用java接口
  233 + sendResult = await this.get({
  234 + data: {
  235 + method: 'app.redpac.addRedpac',
  236 + actSource: 1001,
  237 + uid: uid,
  238 + redpacToken: getPrize.value
  239 + }
  240 + });
  241 +
  242 + if (sendResult && sendResult.data) {
  243 + sendMessage(0, {}, {succeedTimes: sendResult.data.amount || 0}, 0);
  244 +
  245 + if (sendResult && sendResult.code === 200) {
  246 + if (sendResult.data.isRepReceive === 'N') {
200 sendResult.code = 201; // 不能重复领取 247 sendResult.code = 201; // 不能重复领取
201 - } else { 248 + } else {
202 getPrize.desc = '恭喜您获得' + sendResult.data.amount + '元红包'; 249 getPrize.desc = '恭喜您获得' + sendResult.data.amount + '元红包';
  250 + }
203 } 251 }
  252 + } else {
  253 + sendMessage(0, {}, {
  254 + message: sendResult.message || 'no result',
  255 + stack: Object.assign({}, {code: sendResult.code})
  256 + }, 1);
  257 + }
  258 +
  259 + } catch (e) {
  260 + sendMessage(0, {}, {message: e.message || 'interface error', stack: e}, 1);
204 } 261 }
205 } else if (getPrize.type === 3) { 262 } else if (getPrize.type === 3) {
206 - sendResult = await this.get({  
207 - method: 'app.coupons.couponsSend',  
208 - uid: uid,  
209 - coupon_send_token: getPrize.value  
210 - });  
211 - if (sendResult && sendResult.code === 200) {  
212 - getPrize.desc = '恭喜您获得' + getPrize.name + '优惠券'; 263 + try {
  264 + sendResult = await this.get({
  265 + method: 'app.coupons.couponsSend',
  266 + uid: uid,
  267 + coupon_send_token: getPrize.value
  268 + });
  269 +
  270 + if (sendResult && sendResult.data) {
  271 + sendMessage(1, {}, {succeedTimes: sendResult.data.amount || 0}, 0);
  272 +
  273 + if (sendResult && sendResult.code === 200) {
  274 + getPrize.desc = '恭喜您获得' + getPrize.name + '优惠券';
  275 + }
  276 + } else {
  277 + sendMessage(1, {}, {
  278 + message: sendResult.message || 'no result',
  279 + stack: Object.assign({}, {code: sendResult.code})
  280 + }, 1);
  281 + }
  282 + } catch (e) {
  283 + sendMessage(1, {}, {message: e.message || 'interface error', stack: e}, 1);
213 } 284 }
214 } else if (getPrize.type === 4) { 285 } else if (getPrize.type === 4) {
215 sendResult.code = 200; 286 sendResult.code = 200;
@@ -250,7 +321,7 @@ class ActWheelSurfModel extends global.yoho.BaseModel { @@ -250,7 +321,7 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
250 async getUserPrize(obj) { 321 async getUserPrize(obj) {
251 try { 322 try {
252 return await mysqlCli.query( 323 return await mysqlCli.query(
253 - `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 324 + `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
254 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 325 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
255 order by u.create_time desc`, {act_id: obj.act_id, uid: obj.uid, type: 1}); 326 order by u.create_time desc`, {act_id: obj.act_id, uid: obj.uid, type: 1});
256 327
@@ -2663,7 +2663,7 @@ debug@^4.0.0: @@ -2663,7 +2663,7 @@ debug@^4.0.0:
2663 dependencies: 2663 dependencies:
2664 ms "^2.1.1" 2664 ms "^2.1.1"
2665 2665
2666 -debuglog@*, debuglog@^1.0.1: 2666 +debuglog@^1.0.1:
2667 version "1.0.1" 2667 version "1.0.1"
2668 resolved "http://npm.yohops.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492" 2668 resolved "http://npm.yohops.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
2669 2669
@@ -4857,7 +4857,7 @@ import-local@^1.0.0: @@ -4857,7 +4857,7 @@ import-local@^1.0.0:
4857 pkg-dir "^2.0.0" 4857 pkg-dir "^2.0.0"
4858 resolve-cwd "^2.0.0" 4858 resolve-cwd "^2.0.0"
4859 4859
4860 -imurmurhash@*, imurmurhash@^0.1.4: 4860 +imurmurhash@^0.1.4:
4861 version "0.1.4" 4861 version "0.1.4"
4862 resolved "http://npm.yohops.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea" 4862 resolved "http://npm.yohops.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
4863 4863
@@ -4894,7 +4894,7 @@ inflight@^1.0.4, inflight@~1.0.6: @@ -4894,7 +4894,7 @@ inflight@^1.0.4, inflight@~1.0.6:
4894 once "^1.3.0" 4894 once "^1.3.0"
4895 wrappy "1" 4895 wrappy "1"
4896 4896
4897 -influx-batch-sender@^0.1.6, influx-batch-sender@^0.1.7: 4897 +influx-batch-sender@^0.1.7, influx-batch-sender@^0.1.9:
4898 version "0.1.10" 4898 version "0.1.10"
4899 resolved "http://npm.yohops.com/influx-batch-sender/-/influx-batch-sender-0.1.10.tgz#697e51ab8e5aa20fb9cc3b07a2922e069b86c229" 4899 resolved "http://npm.yohops.com/influx-batch-sender/-/influx-batch-sender-0.1.10.tgz#697e51ab8e5aa20fb9cc3b07a2922e069b86c229"
4900 dependencies: 4900 dependencies:
@@ -5829,10 +5829,6 @@ lodash._baseget@^3.0.0: @@ -5829,10 +5829,6 @@ lodash._baseget@^3.0.0:
5829 version "3.7.2" 5829 version "3.7.2"
5830 resolved "http://npm.yohops.com/lodash._baseget/-/lodash._baseget-3.7.2.tgz#1b6ae1d5facf3c25532350a13c1197cb8bb674f4" 5830 resolved "http://npm.yohops.com/lodash._baseget/-/lodash._baseget-3.7.2.tgz#1b6ae1d5facf3c25532350a13c1197cb8bb674f4"
5831 5831
5832 -lodash._baseindexof@*:  
5833 - version "3.1.0"  
5834 - resolved "http://npm.yohops.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"  
5835 -  
5836 lodash._baseisequal@^3.0.0: 5832 lodash._baseisequal@^3.0.0:
5837 version "3.0.7" 5833 version "3.0.7"
5838 resolved "http://npm.yohops.com/lodash._baseisequal/-/lodash._baseisequal-3.0.7.tgz#d8025f76339d29342767dcc887ce5cb95a5b51f1" 5834 resolved "http://npm.yohops.com/lodash._baseisequal/-/lodash._baseisequal-3.0.7.tgz#d8025f76339d29342767dcc887ce5cb95a5b51f1"
@@ -5856,25 +5852,15 @@ lodash._basevalues@^3.0.0: @@ -5856,25 +5852,15 @@ lodash._basevalues@^3.0.0:
5856 version "3.0.0" 5852 version "3.0.0"
5857 resolved "http://npm.yohops.com/lodash._basevalues/-/lodash._basevalues-3.0.0.tgz#5b775762802bde3d3297503e26300820fdf661b7" 5853 resolved "http://npm.yohops.com/lodash._basevalues/-/lodash._basevalues-3.0.0.tgz#5b775762802bde3d3297503e26300820fdf661b7"
5858 5854
5859 -lodash._bindcallback@*, lodash._bindcallback@^3.0.0: 5855 +lodash._bindcallback@^3.0.0:
5860 version "3.0.1" 5856 version "3.0.1"
5861 resolved "http://npm.yohops.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e" 5857 resolved "http://npm.yohops.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
5862 5858
5863 -lodash._cacheindexof@*:  
5864 - version "3.0.2"  
5865 - resolved "http://npm.yohops.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"  
5866 -  
5867 -lodash._createcache@*:  
5868 - version "3.1.2"  
5869 - resolved "http://npm.yohops.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"  
5870 - dependencies:  
5871 - lodash._getnative "^3.0.0"  
5872 -  
5873 lodash._createset@~4.0.0: 5859 lodash._createset@~4.0.0:
5874 version "4.0.3" 5860 version "4.0.3"
5875 resolved "http://npm.yohops.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26" 5861 resolved "http://npm.yohops.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
5876 5862
5877 -lodash._getnative@*, lodash._getnative@^3.0.0: 5863 +lodash._getnative@^3.0.0:
5878 version "3.9.1" 5864 version "3.9.1"
5879 resolved "http://npm.yohops.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5" 5865 resolved "http://npm.yohops.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
5880 5866
@@ -6033,7 +6019,7 @@ lodash.reject@^4.4.0: @@ -6033,7 +6019,7 @@ lodash.reject@^4.4.0:
6033 version "4.6.0" 6019 version "4.6.0"
6034 resolved "http://npm.yohops.com/lodash.reject/-/lodash.reject-4.6.0.tgz#80d6492dc1470864bbf583533b651f42a9f52415" 6020 resolved "http://npm.yohops.com/lodash.reject/-/lodash.reject-4.6.0.tgz#80d6492dc1470864bbf583533b651f42a9f52415"
6035 6021
6036 -lodash.restparam@*, lodash.restparam@^3.0.0: 6022 +lodash.restparam@^3.0.0:
6037 version "3.6.1" 6023 version "3.6.1"
6038 resolved "http://npm.yohops.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805" 6024 resolved "http://npm.yohops.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
6039 6025
@@ -9159,7 +9145,7 @@ readable-stream@~2.0.0: @@ -9159,7 +9145,7 @@ readable-stream@~2.0.0:
9159 string_decoder "~0.10.x" 9145 string_decoder "~0.10.x"
9160 util-deprecate "~1.0.1" 9146 util-deprecate "~1.0.1"
9161 9147
9162 -readdir-scoped-modules@*, readdir-scoped-modules@^1.0.0: 9148 +readdir-scoped-modules@^1.0.0:
9163 version "1.0.2" 9149 version "1.0.2"
9164 resolved "http://npm.yohops.com/readdir-scoped-modules/-/readdir-scoped-modules-1.0.2.tgz#9fafa37d286be5d92cbaebdee030dc9b5f406747" 9150 resolved "http://npm.yohops.com/readdir-scoped-modules/-/readdir-scoped-modules-1.0.2.tgz#9fafa37d286be5d92cbaebdee030dc9b5f406747"
9165 dependencies: 9151 dependencies:
@@ -12081,7 +12067,7 @@ yoho-mlellipsis@0.0.3: @@ -12081,7 +12067,7 @@ yoho-mlellipsis@0.0.3:
12081 version "0.0.3" 12067 version "0.0.3"
12082 resolved "http://npm.yohops.com/yoho-mlellipsis/-/yoho-mlellipsis-0.0.3.tgz#5f743eeb74ff6f2df58a802cff7cd8ad8e4b21f4" 12068 resolved "http://npm.yohops.com/yoho-mlellipsis/-/yoho-mlellipsis-0.0.3.tgz#5f743eeb74ff6f2df58a802cff7cd8ad8e4b21f4"
12083 12069
12084 -yoho-node-lib@^0.6.23: 12070 +yoho-node-lib@^0.6.33:
12085 version "0.6.34" 12071 version "0.6.34"
12086 resolved "http://npm.yohops.com/yoho-node-lib/-/yoho-node-lib-0.6.34.tgz#c650e06bfc530cf13c79cf4b3e08bc5d15345933" 12072 resolved "http://npm.yohops.com/yoho-node-lib/-/yoho-node-lib-0.6.34.tgz#c650e06bfc530cf13c79cf4b3e08bc5d15345933"
12087 dependencies: 12073 dependencies:
@@ -12115,7 +12101,7 @@ yoho-swiper@^3.3.1: @@ -12115,7 +12101,7 @@ yoho-swiper@^3.3.1:
12115 version "3.3.2" 12101 version "3.3.2"
12116 resolved "http://npm.yohops.com/yoho-swiper/-/yoho-swiper-3.3.2.tgz#a7b0f6e13de38d8ac8bb90badbbc316889a0c214" 12102 resolved "http://npm.yohops.com/yoho-swiper/-/yoho-swiper-3.3.2.tgz#a7b0f6e13de38d8ac8bb90badbbc316889a0c214"
12117 12103
12118 -yoho-zookeeper@^1.0.11: 12104 +yoho-zookeeper@^1.0.8:
12119 version "1.0.11" 12105 version "1.0.11"
12120 resolved "http://npm.yohops.com/yoho-zookeeper/-/yoho-zookeeper-1.0.11.tgz#a4fe2a7176de91971be4f03c9552720b93a9ffe3" 12106 resolved "http://npm.yohops.com/yoho-zookeeper/-/yoho-zookeeper-1.0.11.tgz#a4fe2a7176de91971be4f03c9552720b93a9ffe3"
12121 dependencies: 12107 dependencies: