"use strict";

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

exports.save = function (from, groupId, at, contentType, content, handler) {
    imRepo.execQuery({
        "sql": "INSERT INTO msg_group (to_gid,from_uid,at_uid,type,content) VALUES (?,?,?,?,?)",
        "args": [groupId, from, at, contentType, content],
        "handler": handler
    });
};

exports.findAllMessages = function (groupId, contentType, start, end, count, handler) {
    var sql = "SELECT to_gid, msg_id, from_uid, at_uid, type, content, timestamp " +
        "FROM msg_group " +
        "WHERE to_gid = ? AND type in(" + contentType + ") AND msg_id BETWEEN ? AND ? GROUP BY timestamp DESC LIMIT ?";

    imRepo.execQuery({
        "sql": sql,
        "args": [groupId, end + 1, start - 1, count],
        "handler": handler
    });
};

/**
 * 查找用户参与的组列表，包括行政与求助。
 *
 * @param userId 指定的用户
 * @param handler
 */
exports.findAllGroupsWithDoctor = function (userId, handler) {
    var sql = "SELECT DISTINCT g.id code, g.name name, ms.last_content_type, ms.last_content, ms.timestamp, ms.new_msg_count, '1' group_type " +
        "FROM wlyy.wlyy_admin_team g, wlyy.wlyy_admin_team_member m, wlyy.wlyy_doctor d, msg_statistic ms " +
        "WHERE d.code = ? AND d.code = m.doctor_code AND m.team_id = g.id AND g.id = ms.from_gid AND ms.last_content_type IN (1,2,3,5,6) GROUP BY g.id , g.name " +
        " UNION " +
        "SELECT m.group_code, m.group_name, ms.last_content_type, ms.last_content, ms.timestamp, ms.new_msg_count, '2' group_type " +
        "FROM wlyy.wlyy_talk_group g, wlyy.wlyy_talk_group_member m, wlyy.wlyy_doctor d, msg_statistic ms " +
        "WHERE d.code = ? AND d.code = m.member_code AND m.group_code = g.code AND g.code = ms.from_gid group by m.group_code, m.group_name";

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

/**
 * 查找用户参与的讨论组，且含有患者的讨论组。直接从wlyy_talk_group中查找即可。
 *
 * @param userId
 * @param handler
 */
exports.findAllGroupsWithPatient = function (userId, handler) {
    var sql = "SELECT g.code, g.name, '', '', '', ms.last_content_type, ms.last_content, ms.timestamp, ms.new_msg_count, g.type group_type " +
        "FROM msg_statistic ms, (" +
        " SELECT g.code code, g.name name, g.type type FROM wlyy.wlyy_talk_group g, wlyy.wlyy_talk_group_member m " +
        " WHERE g.code = m.group_code and m.member_code = ?) g, wlyy.wlyy_patient p " +
        "WHERE ((ms.uid = ? and ms.from_uid = p.code) OR (ms.uid = p.code and ms.from_uid = ?)) " +
        "and ms.from_gid = g.code and ms.msg_type = 2 AND ms.last_content_type in (1,2,3,5,6) ";

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

/**
 * 查找指定的消息。
 *
 * @param messageId
 * @param handler
 */
exports.findOneMessage = function (messageId, handler) {
    var sql = "SELECT to_gid, msg_id, from_uid, at_uid, type, content, timestamp " +
        "FROM msg_group " +
        "WHERE msg_id = ?";

    imRepo.execQuery({
        "sql": sql,
        "args": [messageId],
        "handler": handler
    });
};

/**
 * 查找未读消息。
 *
 * @param groupId
 * @param start
 * @param count
 * @param handler
 */
exports.findUnread = function (groupId, start, count, handler) {
    var sql = "SELECT msg_id, to_gid, from_uid, at_uid, type, content, timestamp " +
        "FROM msg_group " +
        "WHERE to_gid = ? AND msg_id < ? ORDER BY timestamp DESC LIMIT ?";

    imRepo.execQuery({
        "sql": sql,
        "args": [groupId, start, count],
        "handler": handler
    });
};