wheel-surf.js 3.42 KB
/* eslint-disable array-callback-return */
const {ActWheelSurfConf, ActWheelSurfPrize, Activity, ActWheelSurfUser} = require('../../../db');
const mysqlCli = global.yoho.utils.mysqlCli;

class ActWheelSurfModel extends global.yoho.BaseModel {
  constructor(ctx) {
    super(ctx);
  }

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

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

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


  configModify(obj) {
    if (obj.id) {
      let where = {id: obj.id};

      delete obj.id;
      return ActWheelSurfConf.update(obj, {where: where});
    } else {
      return ActWheelSurfConf.create(obj);
    }
  }

  configList() {
    return ActWheelSurfConf.findAll();
  }

  configFindOne(act_id) {
    return ActWheelSurfConf.findOne({where: {act_id}});
  }

  prizeCreate(arr) {
    return ActWheelSurfPrize.bulkCreate(arr);
  }

  prizeUpdate(arr) {
    try {
      arr.map((value) => {
        let where = {id: value.id};

        delete value.id;
        ActWheelSurfPrize.update(value, {where: where});
      });
      return Promise.resolve({code: 200, result: true});
    } catch (e) {
      return Promise.resolve({code: 201, result: false, msg: e});
    }

  }

  prizeFindByActId(act_id) {
    return ActWheelSurfPrize.findAll({where: {act_id}});
  }

  prizeDelete(id) {
    return ActWheelSurfPrize.destroy({where: {id}});
  }

  async prizeModify(obj) {
    if (obj.id) {
      let where = {id: obj.id};

      delete obj.id;
      if (obj.total) {
        let prize = await ActWheelSurfPrize.findOne({where: where});
        let count = obj.total - prize.total;

        if (count > 0) {
          await prize.increment({total_left: count});
        } else if (count < 0) {
          return Promise.resolve({result: false, code: 204, msg: '总数不能小于原来的总数'});
        }
      }
      await ActWheelSurfPrize.update(obj, {where: where});
      return Promise.resolve({result: true, code: 200, msg: '修改成功'});
    } else {
      await ActWheelSurfPrize.create(obj);
      return Promise.resolve({result: true, code: 200, msg: '插入成功'});
    }
  }

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

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

    let sql = `select u.id, u.act_id act_id, u.prize_id prize_id, u.create_time createTime, 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);

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

    try {
      return mysqlCli.query(sql, obj);
    } catch (e) {
      return Promise.reject({code: 305, result: false, msg: '服务错误,请稍等'});
    }
  }
}

module.exports = ActWheelSurfModel;