wheel-surf.js 3.64 KB
const {Activity} = require('../../../db');
const mysqlCli = global.yoho.utils.mysqlCli;

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

  list() {
    return Activity.findAll({where: {type: 1}});
  }

  create(data) {
    return Activity.create(data);
  }

  async actDelete(id) {
    await Activity.destroy({where: {id}});
    return true;
  }

  actInfo(act_id) {
    return Activity.findOne({where: {id: act_id}});
  }

  userFind(obj) {
    let pageNo = obj.pageNo || 1;
    let pageSize = obj.pageSize || 20;

    obj.start = (pageNo - 1) * pageSize;
    obj.page = pageSize;

    let sql = `select u.uid, u.act_id act_id, u.prize_id prize_id, u.create_time create_time,a.title title,p.name name,p.type type ,p.value value,p.img img
            from act_wheel_surf_user u , act_wheel_surf_prize p ,activity a where u.prize_id = p.id and u.act_id =:act_id and a.id=:act_id`;
    let arr = Object.keys(obj);

    let countSql = `select count(1) total
            from act_wheel_surf_user u , act_wheel_surf_prize p where u.prize_id = p.id and u.act_id =:act_id`;

    arr.forEach(function(value, index) {
      if (!obj[value]) {
        return;
      }

      switch (value) {
        case 'type':
          sql += ' and p.type=:type';
          countSql += ' and p.type=:type';
          break;
        case 'title':
          sql += ' and a.title=:title';
          break;
        case 'uid':
          sql += ' and u.uid=:uid';
          countSql += ' and u.uid=:uid';
          break;
        case 'name':
          sql += ' and p.name=:name';
          countSql += ' and p.name=:name';
          break;
      }
      if (index === arr.length - 1) {
        sql += ' order by u.create_time desc limit :start, :page;';
      }
    });

    try {
      return Promise.all([mysqlCli.query(countSql, obj).then(res => {return res[0].total}), mysqlCli.query(sql, obj)]);
    } catch (e) {
      return Promise.reject({code: 305, result: false, msg: '服务错误,请稍等'});
    }
  }

  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(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 || [];

    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 || [];

    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;