wheel-surf-redis.js
1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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(prizes => {
return prizes.map(prize => {
return JSON.parse(prize);
})
});
}
delActPrize(actId) {
return this.client.del(`turntable:${actId}:prize`);
}
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);
multi.persist(`turntable:${actId}:prize:${prize.prize_idx}:stock`);
});
multi.persist(`turntable:${actId}:prize`);
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;