/**
 * 搜索功能。
 */
"use strict";

var imRepo = require('./database/im.db.js');

/**
 * 搜索与医生签约过的患者，条件：患者姓名。
 */
module.exports.searchPatients = function (userId, userRole, keyword, handler) {
    var sql = "SELECT p.code, p.name, p.birthday, p.sex, p.photo " +
        "FROM wlyy.wlyy_sign_family f, wlyy.wlyy_patient p " +
        "WHERE f.patient = p.code AND p.name LIKE ? ";

    if(userRole == 1){
        // 全科医生
        sql += "AND f.doctor = ?"
    } else if(userRole == 2){
        // 健康管理师
        sql += "AND f.doctor_health = ?"
    }

    imRepo.execQuery({
        "sql": sql,
        "args": ["%" + keyword + "%", userId],
        "handler": handler
    });
};

/**
 * 搜索与患者的私信。
 */
module.exports.searchPatientPM = function (userId, keyword, handler) {
    var sql = "SELECT p.code, p.name, p.birthday, p.sex, p.photo, m.msg_id, m.content " +
        "FROM (SELECT CASE WHEN msg.from_uid = ? THEN msg.to_uid ELSE msg.from_uid END peer_id, msg.msg_id, msg.content " +
        "      FROM msg_p2p msg " +
        "      WHERE (msg.from_uid = ? OR msg.to_uid = ?) AND msg.content LIKE ? AND type = 1) m, wlyy.wlyy_patient p " +
        "WHERE m.peer_id = p.code ORDER BY p.code";

    imRepo.execQuery({
        "sql": sql,
        "args": [userId, userId, userId, "%" + keyword + "%", userId],
        "handler": handler
    });
};

/**
 * 搜索聊天过的医生及行政团队内成员，条件：医生姓名。
 */
module.exports.searchDoctors = function (userId, keyword, handler) {
    var sql = "SELECT DISTINCT d.code, d.name, d.photo, d.hospital " +
        "FROM msg_statistic ms, wlyy.wlyy_doctor d " +
        "WHERE d.code = ? AND ms.from_gid IS NULL AND (ms.peer_uid = d.code OR ms.uid = d.code) AND d.name like ?" +
        "UNION " +
        "SELECT d.code, d.name, d.photo, d.hospital " +
        "FROM wlyy.wlyy_admin_team_member m1, wlyy.wlyy_admin_team_member m2, wlyy.wlyy_doctor d " +
        "WHERE m1.doctor_code = ? AND m1.team_id = m2.team_id AND m2.doctor_code = d.code AND d.name like ?";

    imRepo.execQuery({
        "sql": sql,
        "args": [userId, "%" + keyword + "%", userId, "%" + keyword + "%"],
        "handler": handler
    });
};

/**
 * 搜索医生消息内容。
 */
module.exports.searchDoctorMessages = function (userId, keyword, handler) {
    var sql = "SELECT d.code, d.name, d.photo, m.msg_id, m.content " +
        "FROM (SELECT CASE WHEN msg.from_uid = ? THEN msg.to_uid ELSE msg.from_uid END peer_id, msg.msg_id, msg.content " +
        "      FROM msg_p2p msg " +
        "      WHERE (msg.from_uid = ? OR msg.to_uid = ?) AND msg.content LIKE ? AND type = 1) m, wlyy.wlyy_doctor d " +
        "WHERE m.peer_id = d.code ORDER BY d.name ASC, m.msg_id DESC";

    imRepo.execQuery({
        "sql": sql,
        "args": [userId, userId, userId, "%" + keyword + "%"],
        "handler": handler
    });
};

/**
 * 搜索医生的讨论组，使用名称。
 */
module.exports.searchGroups = function (userId, keyword, handler) {
    var sql = "SELECT g.code, g.name, m.member_code, m.member_name " +
        "FROM wlyy.wlyy_talk_group g left join wlyy.wlyy_talk_group_member m on g.code = m.group_code " +
        "WHERE g.name like ? OR m.member_name LIKE ? AND m.member_code = ?";

    imRepo.execQuery({
        "sql": sql,
        "args": ["%" + keyword + "%", "%" + keyword + "%", userId],
        "handler": handler
    });
};

/**
 * 搜索医生的讨论组消息，使用消息内容。
 */
module.exports.searchGroupMessages = function (userId, keyword, handler) {
    var sql = "SELECT g.code, g.name, gm.msg_id, gm.content FROM msg_group gm, " +
        "   (SELECT t.id code, t.name name " +
        "    FROM wlyy.wlyy_admin_team t, wlyy.wlyy_admin_team_member m " +
        "    WHERE m.doctor_code = ? AND t.id = m.team_id " +
        "    UNION " +
        "    SELECT m.group_code code, m.group_name name " +
        "    FROM wlyy.wlyy_talk_group_member m " +
        "    WHERE m.member_code = ?) g " +
        "WHERE g.code = gm.to_gid AND gm.type = 1 AND gm.content LIKE ? ORDER BY g.name ASC, gm.msg_id DESC";

    imRepo.execQuery({
        "sql": sql,
        "args": [userId, userId, "%" + keyword + "%"],
        "handler": handler
    });
};





