yoho-activitys.js
3.57 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Created by qiujun on 2019/5/16.
*/
const _ = require('lodash');
const PARENT_TABLE_NAME = 'yohoactivitys';
const TABLE_NAME = PARENT_TABLE_NAME + ':activity';
class ActYohoActivitysModel extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
this.redis = global.yoho.redis;
this.client = this.redis.client;
}
/**
* 创建一个活动
* @param data
* @returns {Promise.<*>}
*/
async create(data) {
let len = await this.client.llenAsync(TABLE_NAME);
data.id = len + 1;
return this.client.rpushAsync(TABLE_NAME, JSON.stringify(data));
}
/**
* 取得活动列表或者根据id取得某一个活动的信息
* @returns {Promise.<void>}
*/
async actList(id) {
let len = await this.client.llenAsync(TABLE_NAME);
if (id && parseInt(id, 10) > 0) {
let idx = parseInt(id, 10) - 1;
return this.client.lrangeAsync(TABLE_NAME, idx, idx).then(act => {
return JSON.parse(act[0] || {});
});
} else {
return this.client.lrangeAsync(TABLE_NAME, 0, len - 1).then(acts => {
return acts.filter(act => act).map(act => {
return JSON.parse(act);
});
});
}
}
/**
* 创建活动文章
* @param data
* @returns {Promise.<void>}
*/
async createArticles(data) {
if (data.actId) {
let key = `${PARENT_TABLE_NAME}:${data.actId}:items`;
let len = await this.client.zcardAsync(key); // 获得有序集合的基数
if (data.itemLength) {
let promiseArr = [];
for (let i = 0; i < data.itemLength; i++) {
let member = 'item_' + (len + i + 1);
promiseArr.push(this.client.zadd(key, 0, member));
}
return Promise.all(promiseArr);
} else {
let member = 'item_' + (len + 1);
return this.client.zaddAsync(key, 0, member);
}
}
}
/**
* 获取文章有序列表(score倒序)
* @param data
* @returns {Promise.<*>}
*/
async getArtilceList(data) {
if (data.actId) {
let key = `${PARENT_TABLE_NAME}:${data.actId}:items`;
let finalResult = [];
let result = [];
let start = data.start;
let end = data.end;
if (data.order === 'desc') {
result = await this.client.zrevrangeAsync(key, start, end, 'WITHSCORES');
} else {
result = await this.client.zrangeAsync(key, start, end, 'WITHSCORES');
}
for (let i = 0; i < result.length; i += 2) {
// let score = await this.client.zscoreAsync(key, result[i]); // 获取得分
if (result[i] && (result[i + 1] || result[i + 1] === 0)) {
finalResult.push(Object.assign({}, {id: result[i]}, {score: result[i + 1]}));
}
}
return finalResult;
}
}
/**
* 投票
* @param data
* @returns {Promise.<void>}
*/
async addLike(data) {
if (data.actId && data.ids) {
let key = `${PARENT_TABLE_NAME}:${data.actId}:items`;
let idsArr = data.ids.split(',');
let finalResult = [];
for (let i = 0; i < idsArr.length; i++) {
let member = 'item_' + idsArr[i];
let result = await this.client.zincrby(key, 1, member);
finalResult.push(result);
}
return finalResult;
}
}
/**
* 移除排名区间内的数据
* @param data
* @returns {Promise.<*>}
*/
async removeRangeByRand(data) {
if (data.actId && data.end) {
let start = data.start;
let end = data.end;
let key = `${PARENT_TABLE_NAME}:${data.actId}:items`;
return this.client.zremrangebyrank(key, start, end);
}
}
}
module.exports = ActYohoActivitysModel;