wheel-surf-redis.js 1.75 KB
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;