|
|
/* eslint-disable array-callback-return */
|
|
|
const {ActWheelSurfConf, ActWheelSurfPrize, Activity, ActWheelSurfUser} = require('../../../db');
|
|
|
const {Activity} = require('../../../db');
|
|
|
const mysqlCli = global.yoho.utils.mysqlCli;
|
|
|
|
|
|
class ActWheelSurfModel extends global.yoho.BaseModel {
|
|
|
constructor(ctx) {
|
|
|
super(ctx);
|
|
|
this.redis = global.yoho.redis;
|
|
|
this.client = this.redis.client;
|
|
|
}
|
|
|
|
|
|
list() {
|
...
|
...
|
@@ -17,54 +18,13 @@ class ActWheelSurfModel extends global.yoho.BaseModel { |
|
|
|
|
|
async actDelete(id) {
|
|
|
await Activity.destroy({where: {id}});
|
|
|
await ActWheelSurfConf.destroy({where: {act_id: id}});
|
|
|
await ActWheelSurfPrize.destroy({where: {act_id: id}});
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
configModify(obj) {
|
|
|
if (obj.id) {
|
|
|
let where = {id: obj.id};
|
|
|
|
|
|
delete obj.id;
|
|
|
return ActWheelSurfConf.update(obj, {where: where});
|
|
|
} else {
|
|
|
return ActWheelSurfConf.create(obj);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
configFindOne(act_id) {
|
|
|
return ActWheelSurfConf.findOne({where: {act_id}});
|
|
|
}
|
|
|
|
|
|
actInfo(act_id) {
|
|
|
return Activity.findOne({where: {id: act_id}});
|
|
|
}
|
|
|
|
|
|
prizeCreate(arr) {
|
|
|
return ActWheelSurfPrize.bulkCreate(arr);
|
|
|
}
|
|
|
|
|
|
prizeUpdate(arr) {
|
|
|
try {
|
|
|
arr.map((value) => {
|
|
|
let where = {id: value.id};
|
|
|
|
|
|
delete value.id;
|
|
|
ActWheelSurfPrize.update(value, {where: where});
|
|
|
});
|
|
|
return Promise.resolve({code: 200, result: true});
|
|
|
} catch (e) {
|
|
|
return Promise.resolve({code: 201, result: false, msg: e});
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
prizeFindByActId(act_id) {
|
|
|
return ActWheelSurfPrize.findAll({where: {act_id}});
|
|
|
}
|
|
|
|
|
|
userFind(obj) {
|
|
|
let pageNo = obj.pageNo || 1;
|
|
|
let pageSize = obj.pageSize || 20;
|
...
|
...
|
@@ -112,6 +72,58 @@ class ActWheelSurfModel extends global.yoho.BaseModel { |
|
|
return Promise.reject({code: 305, result: false, msg: '服务错误,请稍等'});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
getActConf(actId) {
|
|
|
return this.client.hgetallAsync(`turntable:${actId}`).then(conf => {
|
|
|
Object.keys(conf).forEach(key => {
|
|
|
if (conf[key] && !_.isNaN(Number(conf[key]))) {
|
|
|
conf[key] = Number(conf[key])
|
|
|
}
|
|
|
});
|
|
|
return conf;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
setActConf(actId, confObj) {
|
|
|
confObj.id = 1;
|
|
|
return this.client.HMSET(`turntable:${actId}`, confObj);
|
|
|
}
|
|
|
|
|
|
getActPrize(actId, len = 7) {
|
|
|
return this.client.lrangeAsync(`turntable:${actId}:prize`, 0, len)
|
|
|
.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;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
createActPrize(actId, prizesList) {
|
|
|
prizesList = prizesList || [];
|
|
|
|
|
|
let multi = this.client.multi();
|
|
|
prizesList.map((prize, idx) => {
|
|
|
prize.id = idx + 1;
|
|
|
multi.lpush(`turntable:${actId}:prize`, JSON.stringify(prize));
|
|
|
multi.set(`turntable:${actId}:prize:${prize.prize_idx}:stock`, prize.total_left);
|
|
|
});
|
|
|
return multi.execAsync();
|
|
|
}
|
|
|
|
|
|
updateActPrize(actId, prizesList) {
|
|
|
prizesList = prizesList || [];
|
|
|
|
|
|
let multi = this.client.multi();
|
|
|
prizesList.map((prize, idx) => {
|
|
|
multi.lset(`turntable:${actId}:prize`, idx, JSON.stringify(prize));
|
|
|
multi.set(`turntable:${actId}:prize:${prize.prize_idx}:stock`, prize.total_left);
|
|
|
});
|
|
|
return multi.execAsync();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
module.exports = ActWheelSurfModel; |
...
|
...
|
|