...
|
...
|
@@ -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});
|
|
|
|
...
|
...
|
|