yoho-activitys.js 3.45 KB
/**
 * 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);

      } else {
        result = await this.client.zrangeAsync(key, start, end);
      }

      for (let i = 0; i < result.length; i++) {
        let score = await this.client.zscoreAsync(key, result[i]); // 获取得分

        finalResult.push(Object.assign({}, {id: result[i]}, {score: score}));
      }
      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;