Authored by 李奇

api修改

... ... @@ -22,15 +22,16 @@ class ActWheelSurfRedis extends global.yoho.BaseModel {
}
getActPrize(actId, len = 7) {
return this.client.lrangeAsync(`turntable:${actId}:prize`, 0, len)
.then(prizes => {
return prizes.map(prize => {
return JSON.parse(prize);
})
.then(async (prizes) => {
let left = 0;
for (let i = 0; i < prizes.length; i++) {
prizes[i] =JSON.parse(prizes[i]);
left = await this.client.getAsync(`turntable:${actId}:prize:${prizes[i].prize_idx}:stock`);
prizes[i].total_left = +left;
}
return prizes;
});
}
delActPrize(actId) {
return this.client.del(`turntable:${actId}:prize`);
}
createActPrize(actId, prizesList) {
prizesList = prizesList || [];
... ...
/* eslint-disable array-callback-return */
const {ActWheelSurfConf, ActWheelSurfPrize, ActWheelSurfUser, Activity, sequelize} = require('../../../db');
// const md5 = require('yoho-md5');
const {Activity} = require('../../../db');
const _ = require('lodash');
const logger = global.yoho.logger;
const mysqlCli = global.yoho.utils.mysqlCli;
const _sender = global.yoho.sender;
/**
... ... @@ -107,19 +102,14 @@ class ActWheelSurfModelRedis extends global.yoho.BaseModel {
// 每日最大次数
if (data.conf.times_type === 1) {
// 获取当天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());
let startTime = new Date(new Date().toLocaleDateString()).getTime();
let endTime = new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1).getTime();
let usedArr = await this.client.zrangebyscoreAsync(`turntable:${obj.act_id}:user:${obj.uid}:prize`, startTime, endTime);
obj.create_time = {gt: startTime, lte: endTime};
residueCount = data.conf.day_limit_times - await ActWheelSurfUser.count({
where: obj
});
} else {
// 活动最大次数
residueCount = data.conf.act_total_times - await ActWheelSurfUser.count({where: obj});
residueCount = data.conf.day_limit_times - usedArr.length;
}
} else {
residueCount = data.conf.times_type === 3 ? data.conf.act_total_times : data.conf.day_limit_times;
residueCount = 0;
}
data.residueCount = residueCount;
return Promise.resolve(data);
... ... @@ -145,7 +135,16 @@ class ActWheelSurfModelRedis extends global.yoho.BaseModel {
}
// 获取活动信息
let conf = await ActWheelSurfConf.findOne({where: {act_id: obj.act_id}});
let conf = await this.client.hgetallAsync(`turntable:${obj.act_id}`)
.then(conf => {
Object.keys(conf).forEach(key => {
if (conf[key] && !_.isNaN(Number(conf[key]))) {
conf[key] = Number(conf[key])
}
});
return conf;
});
let residueCount;
let uid = {
toString: () => {
... ... @@ -163,22 +162,22 @@ class ActWheelSurfModelRedis extends global.yoho.BaseModel {
// 计算用户剩余次数校验
if (conf.times_type === 1) {
// 获取当天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());
let startTime = new Date(new Date().toLocaleDateString()).getTime();
let endTime = new Date(new Date(new Date().toLocaleDateString()).getTime()+24*60*60*1000-1).getTime();
let usedArr = await this.client.zrangebyscoreAsync(`turntable:${obj.act_id}:user:${obj.uid}:prize`, startTime, endTime);
obj.create_time = {gt: startTime, lte: endTime};
residueCount = conf.day_limit_times - await ActWheelSurfUser.count({
where: obj
});
} else {
// 活动最大次数
residueCount = conf.act_total_times - await ActWheelSurfUser.count({where: obj});
residueCount = conf.day_limit_times - usedArr.length;
}
if (residueCount > 0) {
// 获取抽取到奖品
// 获取奖品信息
let prize = await ActWheelSurfPrize.findAll({where: {act_id: obj.act_id}});
let prize = await this.client.lrangeAsync(`turntable:${obj.act_id}:prize`, 0, 7)
.then(prizes => {
return prizes.map(prize => {
return JSON.parse(prize);
})
});
// 获取每个奖品获取它们的中奖范围
let arr = {}, oldValue;
... ... @@ -213,15 +212,11 @@ class ActWheelSurfModelRedis extends global.yoho.BaseModel {
if (getPrize.type !== 1) {
// 校验剩余库存 减库存
reduce = await ActWheelSurfPrize.update({total_left: sequelize.literal('`total_left`-1')}, {
where: {
total_left: {$gt: 0},
id: getPrize.id
}
});
let left = await this.client.getAsync(`turntable:${obj.act_id}:prize:${getPrize.prize_idx}:stock`);
// 如果库存不够则返回当前type=1的商品
if (reduce[0] !== 1) {
if (left > 0) {
await this.client.decrAsync(`turntable:${obj.act_id}:prize:${getPrize.prize_idx}:stock`);
} else {
for (let p of prize) {
if (p.type === 1) {
getPrize = p;
... ... @@ -231,7 +226,6 @@ class ActWheelSurfModelRedis extends global.yoho.BaseModel {
}
}
getPrize = getPrize.dataValues;
if (getPrize.type === 1) {
sendResult.code = 200;
getPrize.desc = '对不起,您没有中奖';
... ... @@ -299,8 +293,20 @@ class ActWheelSurfModelRedis extends global.yoho.BaseModel {
}
getPrize.desc = '对不起,您没有中奖';
}
result = await ActWheelSurfUser.create({act_id: obj.act_id, uid: obj.uid, prize_id: getPrize.id});
if (result) {
let score = new Date().getTime();
getPrize.createTime = score;
result = await Promise.all([
this.client.zaddAsync(
`turntable:${obj.act_id}:user:${obj.uid}:prize`, score, `${JSON.stringify(getPrize)}:::${score}`),
this.client.zaddAsync(
`turntable:${obj.act_id}:prize:${getPrize.prize_idx}:users`, score, `${JSON.stringify(getPrize)}:::${score},${obj.uid}`)
]);
if (result
&& result[0] === 1
&& result[1] === 1) {
delete getPrize.chance;
delete getPrize.create_time;
delete getPrize.total;
... ... @@ -321,17 +327,14 @@ class ActWheelSurfModelRedis 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, u.create_time createTime, 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});
// return await ActWheelSurfUser.findAll({include: [{
// model: ActWheelSurfPrize,
// as: 'ActWheelSurfPrize',
// }],
// where: obj
// });
return await this.client.zrevrangeAsync(`turntable:${obj.act_id}:user:${obj.uid}:prize`, 0, 300)
.then(prizes => {
return prizes.map(prize => {
return JSON.parse(prize.split(':::')[0]);
}).filter(prize => {
return prize.type !== 1;
});
});
} catch (e) {
logger.error(e);
return Promise.reject({code: 305, result: false, msg: '服务错误,请稍等'});
... ...