text: const hash = require('hash.js');
  const {
    poolQuery
  } = require('../utils/database');
  // const {
  //   querySync
  // } = require('../utils/db');
  // const crypto = require('crypto');
  
  const defaultCoin = 3000;
  const defaultState = 0;
  
  // const md5encode = (str) => {
  //   const md5 = (crypto.createHash('md5'));
  //   md5.update(str);
  //   return md5.digest('hex');
  // };
  
  /**
   * model User
   */
  class User {
    /**
     * set user data
     * @param {User} userData user data
     */
    constructor(userData) {
      this.uid = userData.uid;
      this.name = userData.name;
      this.email = userData.email;
      this.password = userData.password;
      this.coin = userData.coin;
      this.portrait = userData.portrait;
      this.signature = userData.signature;
      this.state = userData.state;
      this.pwd_salt = userData.pwd_salt;
    }
  
    /**
     * update data
     */
    save() {
      const sql = 'update account set coin=? where uid=?';
      const params = [this.coin, this.uid];
      return poolQuery(sql, params).then(res => res.affectedRows === 1);
    }
  
    /**
     * 移除用户
     * @param {number} uid 用户id
     */
    remove() {
      const sql = 'DELETE FROM account WHERE uid=?';
      const params = [this.uid];
      return poolQuery(sql, params).then(res => res.affectedRows === 1);
    }
  
    /**
     * 创建账号
     * @param {object} userInfo 用户信息
     */
    static create(userInfo) {
      const salt = User.getSalt(32);
  
      const sql = 'INSERT INTO account(name, email, password, coin, state, pwd_salt) VALUES(?, ?, ?, ? , ?, ?)';
      const params = [
        userInfo.name,
        userInfo.email,
        User.encryptPwd(salt, userInfo.password),
        defaultCoin,
        defaultState,
        salt
      ];
      return poolQuery(sql, params).then(res => res.affectedRows === 1);
    }
  
    /**
     * 通过邮箱查找用户
     * @param {string} data 用户名
     */
    static find(data) {
      const sql = 'select * from account where email=? limit 1';
      const params = [data.email];
      return poolQuery(sql, params).then((res) => {
        if (res.length === 1) {
          console.log('found user');
          return new User(res[0]);
        }
        return new Error('errMsg:noSpecificUser');
      });
    }
  
    /**
     * 加密密码
     */
    static encryptPwd(salt, password) {
      const mixedPwd = salt + password;
      const encryptPwd = hash.sha256().update(mixedPwd).digest('hex');
      return encryptPwd;
    }
  
    /**
     * 返回随机获得的盐值，用于用户密码的组合加密
     * @param {Number} saltLength 盐值的长度
     * @return {String} 得到的字符串
     * @example 10 => afagdsgdsl
     */
    static getSalt(saltLength = 32) {
      let salt = Math.random().toString(32).substring(2);
      while (salt.length !== saltLength) {
        if (salt.length > saltLength) {
          salt = salt.slice(salt.length - saltLength);
        } else {
          salt += Math.random().toString(32).substring(2);
        }
      }
      return salt;
    }
  }
  module.exports = User;
  