//*/
var mongoose = require("mongoose");

var dbm = require("../../../config/db/db_manager");
var umDB = dbm.getCfgConnection("usersManages");
var usersSchema = new mongoose.Schema({ account: String, pw: String, name: String });
var usersModel = umDB.model('users', usersSchema);
/*/
var umDB = require('./umDB');
var usersSchema = new umDB.mongoose.Schema({ account: String, pw: String, name: String });
var usersModel = umDB.mongoose.model('users', usersSchema);
//*/

var usersData = function(){};

usersData.prototype.findUser = function(account, pw, controllerFun){
    usersModel.find({"account": account, "pw": pw}, function(err, data){
        if(data.length){
            controllerFun(0, data[0]);
        }
        else{
            controllerFun('用户名或密码错误！', 0);
        }
    })
};

usersData.prototype.checkAccount = function(account, controllerFun){
    usersModel.find({"account": account}, function(err, data){
        if(data.length){
            controllerFun(data[0]);
        }
        else{
            controllerFun(0);
        }
    })
};

usersData.prototype.addUser = function(account, pw, name, controllerFun){
    var user = new usersModel({account: account, pw: pw, name: name});
    user.save(function (err) {
        if (err) {
            controllerFun(err);
        } else {
            controllerFun(0);
        }
    });
};

module.exports = new usersData();
