Authored by huangyi

修改api

1 /* eslint-disable array-callback-return */ 1 /* eslint-disable array-callback-return */
2 const {ActWheelSurfConf, ActWheelSurfPrize, Activity, ActWheelSurfUser} = require('../../../db'); 2 const {ActWheelSurfConf, ActWheelSurfPrize, Activity, ActWheelSurfUser} = require('../../../db');
  3 +const mysqlCli = global.yoho.utils.mysqlCli;
3 4
4 class ActWheelSurfModel extends global.yoho.BaseModel { 5 class ActWheelSurfModel extends global.yoho.BaseModel {
5 constructor(ctx) { 6 constructor(ctx) {
@@ -9,9 +10,11 @@ class ActWheelSurfModel extends global.yoho.BaseModel { @@ -9,9 +10,11 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
9 list() { 10 list() {
10 return Activity.findAll({where: {type: 1}}); 11 return Activity.findAll({where: {type: 1}});
11 } 12 }
  13 +
12 create(data) { 14 create(data) {
13 return Activity.create(data); 15 return Activity.create(data);
14 } 16 }
  17 +
15 async actDelete(id) { 18 async actDelete(id) {
16 await Activity.destroy({where: {id}}); 19 await Activity.destroy({where: {id}});
17 await ActWheelSurfConf.destroy({where: {act_id: id}}); 20 await ActWheelSurfConf.destroy({where: {act_id: id}});
@@ -90,23 +93,41 @@ class ActWheelSurfModel extends global.yoho.BaseModel { @@ -90,23 +93,41 @@ class ActWheelSurfModel extends global.yoho.BaseModel {
90 } 93 }
91 94
92 userFind(obj) { 95 userFind(obj) {
93 - let page = obj.page || 1;  
94 - let size = obj.size || 20;  
95 -  
96 - delete obj.page;  
97 - delete obj.size;  
98 - return ActWheelSurfUser.findAll({  
99 - include: [{  
100 - model: ActWheelSurfPrize,  
101 - as: 'ActWheelSurfPrize',  
102 - }, {  
103 - model: ActWheelSurfConf,  
104 - as: 'ActWheelSurfConf',  
105 - }],  
106 - where: obj,  
107 - offset: (page - 1) * size,  
108 - limit: size 96 + let pageNo = obj.pageNo || 1;
  97 + let pageSize = obj.pageSize || 20;
  98 +
  99 + obj.start = (pageNo - 1) * pageSize;
  100 + obj.page = pageSize;
  101 +
  102 + 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
  103 + 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`;
  104 + let arr = Object.keys(obj);
  105 +
  106 + arr.forEach(function(value, index) {
  107 + switch (obj[value]) {
  108 + case 'type':
  109 + sql += ' and p.type=:type';
  110 + break;
  111 + case 'title':
  112 + sql += ' and a.title=:title';
  113 + break;
  114 + case 'uid':
  115 + sql += ' and u.uid=:uid';
  116 + break;
  117 + case 'name':
  118 + sql += ' and p.name=:name';
  119 + break;
  120 + }
  121 + if (index === arr.length - 1) {
  122 + sql += 'order by u.create_time desc limit :start, :page;';
  123 + }
109 }); 124 });
  125 +
  126 + try {
  127 + return mysqlCli.query(sql, obj);
  128 + } catch (e) {
  129 + return Promise.reject({code: 305, result: false, msg: '服务错误,请稍等'});
  130 + }
110 } 131 }
111 } 132 }
112 133