const { exec, escape } = require('../db/mysql')
const { genPassword } = require('../utils/cryp')

const login = (username, password) => {
    username = username

    // 生成加密密码
    password = password


    const sql = `
        select username, realname from users where username=${username} and password=${password}
    `
        // console.log('sql is', sql)
    return exec(sql).then(rows => {
        return rows[0] || {}
    })
}
const register = (username, password, realname, headphto) => {
    username = escape(username)
    realname = escape(realname)
        // 生成加密密码
    password = genPassword(password)
    password = escape(password)
    console.log(username, password, realname)


    const sql = `
        insert into users (username, password, realname ,headphto)
        values (${username}, ${password},${realname} , '${headphto}');
    `
        // console.log('sql is', sql)
    return exec(sql).then(insertData => {
        return {
            id: insertData.insertId
        }
    })
}
const headphto = (username) => {
    let sql = `select * from users where username='${username}' `

    // 返回 promise
    return exec(sql)
}

module.exports = {
    login,
    register,
    headphto
}