text: import { getUrlParams } from './utils';
  
  // mock tableListDataSource
  let tableListDataSource = [];
  for (let i = 0; i < 46; i += 1) {
    tableListDataSource.push({
      key: i,
      leveId: `${i}`,
      profitRatio: (Math.random() * 1).toFixed(2),
      levePrice: Math.ceil(Math.random() * 100),
      leveName: '黄金会员',
      leveSort: 'bank',
      updateUser: "瓜娃子",
      updateTime: new Date(`2017-07-${Math.floor(i / 2) + 5}`),
      createTime: new Date(`2017-07-${Math.floor(i / 2) + 1}`),
    });
  }
  
  export function selectMemberRank(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
      url = req.url; // eslint-disable-line
    }
  
    const params = getUrlParams(url);
  
    let dataSource = [...tableListDataSource];
  
    if (params.sorter) {
      const s = params.sorter.split('_');
      dataSource = dataSource.sort((prev, next) => {
        if (s[1] === 'descend') {
          return next[s[0]] - prev[s[0]];
        }
        return prev[s[0]] - next[s[0]];
      });
    }
  
    if (params.status) {
      const status = params.status.split(',');
      let filterDataSource = [];
      status.forEach((s) => {
        filterDataSource = filterDataSource.concat(
          [...dataSource].filter(data => parseInt(data.status, 10) === parseInt(s[0], 10))
        );
      });
      dataSource = filterDataSource;
    }
  
    if (params.leveSort) {
      dataSource = dataSource.filter(data => data.leveSort.indexOf(params.leveSort) > -1);
    }
  
    let pageSize = 10;
    if (params.pageSize) {
      pageSize = params.pageSize * 1;
    }
  
    const result = {
      code: 0,
      data: dataSource,
      count: dataSource.length,
    };
  
    if (res && res.json) {
      res.json(result);
    } else {
      return result;
    }
  }
  
  export function saveMemberRank(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
      url = req.url; // eslint-disable-line
    }
  
    const body = (b && b.body) || req.body;
    const { method, id } = body;
      /* eslint no-case-declarations:0 */
      // tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);
      const i = Math.ceil(Math.random() * 10000);
      tableListDataSource.unshift({
        key: i,
        leveId: `${ i}`,
        profitRatio: body.profitRatio/100,
        levePrice: body.levePrice,
        leveName: body.leveName,
        leveSort: body.leveSort,
        updateUser: "瓜娃子",
        updateTime: new Date,
        createTime: new Date,
      });
      const result = {
        code: 0,
        data: tableListDataSource,
        count: tableListDataSource.length,
      };
  
    if (res && res.json) {
      res.json(result);
    } else {
      return result;
    }
  }
  export function deleteMemberRank(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
      url = req.url; // eslint-disable-line
    }
  
    const body = (b && b.body) || req.body;
    const { method, no } = body;
      /* eslint no-case-declarations:0 */
    const idArray = url.split("/")
    const id = idArray[idArray.length - 1]
      tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.leveId) === -1);
    const result = {
      code: 0,
      data: tableListDataSource,
      count: tableListDataSource.length,
    };
  
    if (res && res.json) {
      res.json(result);
    } else {
      return result;
    }
  }
  export function updateMemberRank(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
      url = req.url; // eslint-disable-line
    }
  
    const body = (b && b.body) || req.body;
    const { method, leveId } = body;
      /* eslint no-case-declarations:0 */
      // tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);
      tableListDataSource.map((item,index) => {
        if(item.leveId == leveId){
        tableListDataSource[index]={
            key: body.leveId,
            leveId: `${body.leveId}`,
            profitRatio: body.profitRatio/100,
            levePrice: body.levePrice,
            leveName: body.leveName,
            leveSort: body.leveSort,
            updateUser: "瓜娃子",
            updateTime: new Date,
            createTime: new Date,
          }
        }
      });
    const result = {
      code: 0,
      data: tableListDataSource,
      count: tableListDataSource.length,
    };
  
    if (res && res.json) {
      res.json(result);
    } else {
      return result;
    }
  }
  
  export default { selectMemberRank, deleteMemberRank, updateMemberRank, saveMemberRank};
  