Authored by 李奇

simplify model

... ... @@ -4,7 +4,6 @@
* @date: 26/09/2018
*/
const wheelSurfModel = require('../models/wheel-surf');
const wheelSurfModelRedis = require('../models/wheel-surf-redis');
const valid = require('../../../utils/validator');
const whSurfController = {
... ... @@ -107,7 +106,7 @@ const whSurfController = {
status: {type: 'number', empty: true}
});
let result = await req.ctx(wheelSurfModelRedis).setActConf(data.act_id, data);
let result = await req.ctx(wheelSurfModel).setActConf(data.act_id, data);
return res.json({
code: 200,
... ... @@ -133,7 +132,7 @@ const whSurfController = {
});
}
let actInfo = await req.ctx(wheelSurfModel).actInfo(actId);
let result = await req.ctx(wheelSurfModelRedis).getActConf(actId);
let result = await req.ctx(wheelSurfModel).getActConf(actId);
let actStatus = 2; // 1: 活动未开始 2: 活动进行中 3:活动已过期
let now = parseInt(new Date().getTime() / 1000);
... ... @@ -173,7 +172,7 @@ const whSurfController = {
});
});
let result = await req.ctx(wheelSurfModelRedis).createActPrize(arr[0].act_id, arr);
let result = await req.ctx(wheelSurfModel).createActPrize(arr[0].act_id, arr);
return res.json({
code: 200,
... ... @@ -216,7 +215,7 @@ const whSurfController = {
});
});
let result = await req.ctx(wheelSurfModelRedis).updateActPrize(arr[0].act_id, arr);
let result = await req.ctx(wheelSurfModel).updateActPrize(arr[0].act_id, arr);
return res.json({
code: 200,
... ... @@ -249,7 +248,7 @@ const whSurfController = {
});
}
let result = await req.ctx(wheelSurfModelRedis).getActPrize(actId);
let result = await req.ctx(wheelSurfModel).getActPrize(actId);
return res.json({
code: 200,
... ...
const _ = require('lodash');
class ActWheelSurfRedis extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
this.redis = global.yoho.redis;
this.client = this.redis.client;
}
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 = ActWheelSurfRedis;
/* 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;
... ...