wheel-surf.js 4.08 KB
const _ = require('lodash');
const aes = require('../../../utils/aes');

class ActWheelSurfModel extends global.yoho.BaseModel {
  constructor(ctx) {
    super(ctx);
    this.redis = global.yoho.redis;
    this.client = this.redis.client;
  }

  async list() {
    let len = await this.client.llenAsync('turntable:activity');

    return this.client.lrangeAsync('turntable:activity', 0, len - 1).then(acts => {
      return acts.filter(act => act).map(act => {
        return JSON.parse(act);
      });
    });
  }

  async create(data) {
    let len = await this.client.llenAsync('turntable:activity');

    data.id = len + 1;
    data.encryptedId = aes.encryptUid(len + 1);
    return this.client.rpushAsync('turntable:activity', JSON.stringify(data));
  }

  async actDelete(id) {
    id = parseInt(aes.decryptUid(id), 10);
    return this.client.lsetAsync('turntable:activity', id - 1, '');
  }

  actInfo(act_id) {
    let idx = parseInt(aes.decryptUid(act_id), 10) - 1;

    return this.client.lrangeAsync('turntable:activity', idx, idx).then(act => {
      return JSON.parse(act[0] || {});
    });
  }

  async userFind(obj) {
    let pageNo = obj.pageNo || 1;
    let pageSize = obj.pageSize || 20;
    let actId = parseInt(aes.decryptUid(obj.act_id), 10);
    let len = await this.client.llenAsync(`turntable:${actId}:prize:users`);

    return this.client.lrangeAsync(`turntable:${actId}:prize:users`, (pageNo - 1) * pageSize, pageNo * pageSize - 1).then(prizes => {
      let afters = prizes.map(prize => {
        prize = prize.split(':::');
        return {
          uid: prize[0],
          type: prize[1],
          name: prize[2],
          time: +prize[3]
        };
      });

      return {
        total: len,
        list: afters
      };
    });
  }

  async exportRecords(actId = '') {
    actId = actId.replace(/\s/g, '+');
    actId = parseInt(aes.decryptUid(actId), 10);

    let len = await this.client.llenAsync(`turntable:${actId}:prize:user`);

    return this.client.lrangeAsync(`turntable:${actId}:prize:users`, 0, len - 1).then(prizes => {
      return prizes.map(prize => {
        prize = prize.split(':::');
        return {
          uid: prize[0],
          type: prize[1],
          name: prize[2],
          time: prize[3]
        };
      });
    });
  }

  getActConf(actId) {
    actId = parseInt(aes.decryptUid(actId), 10);
    return this.client.hgetallAsync(`turntable:${actId}`).then(conf => {
      conf = 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;
    actId = parseInt(aes.decryptUid(actId), 10);
    return this.client.HMSET(`turntable:${actId}`, confObj);
  }

  getActPrize(actId, len = 7) {
    actId = parseInt(aes.decryptUid(actId), 10);
    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 || [];
    actId = parseInt(aes.decryptUid(actId), 10);

    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 || [];
    actId = parseInt(aes.decryptUid(actId), 10);

    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;