/**
 * 医生模型。医生真实数据存在于家庭医生平台数据库，即医生的ID，姓名，年龄等详细内容。而IM平台的user表只包含用户当前的在线状态，
 * 即用户的准实时状态，token等内容，不包含用户具体的详细信息。
 */
"use strict";

var log = require('../util/log');
var imRepo = require("./database/im.db.js");
var wlyyRepo = require("./database/wlyy.db.js");

/**
 * 判断用户是否存在。数据从家庭医生平台获取，而不是IM库的user表。
 *
 * @param user
 * @param handler
 */
exports.isExist = function(user, handler) {
    wlyyRepo.execQuery({
        "sql": "SELECT count(*) from wlyy_doctor WHERE code=?",
        "args": [user],
        "handler": handler
    });
};

exports.getUserStatus = function(userId, handler) {
    imRepo.execQuery({
        "sql": "SELECT platform,token,client_id,is_online,status from user WHERE user_id = ?",
        "args": [userId],
        "handler": handler
    });
};

exports.login = function(userId, token, client_id, platform, handler) {
    imRepo.execQuery({
        "sql": "INSERT INTO user (user_id,token,client_id,platform,is_online,status) VALUES (?,?,?,?,1,1) ON" +
        " DUPLICATE KEY UPDATE token=?,client_id=?,platform=?,is_online=1,status=1",
        "args": [userId, token, client_id, platform, token, client_id, platform],
        "handler": handler
    });
};

exports.logout = function(userId, handler) {
    imRepo.execQuery({
        "sql": "UPDATE user SET is_online='0',status='0' WHERE user_id=?",
        "args": [userId],
        "handler": handler
    });
};

exports.deleteToken = function(token, handler) {
    imRepo.execQuery({
        "sql": "DELETE FROM user WHERE token=?",
        "args": [token],
        "handler": handler
    });
};

exports.updateStatus = function(userId, status, handler) {
    imRepo.execQuery({
        "sql": "UPDATE user SET status=? WHERE user_id=?",
        "args": [status, userId],
        "handler": handler
    });
};
