"use strict";

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

/**
 * 保存消息。
 *
 * @param to
 * @param from
 * @param type
 * @param content
 * @param handler
 */
exports.save = function (to, from, type, content, handler) {
    imRepo.execQuery({
        "sql": "INSERT INTO msg_p2p (to_uid,from_uid,type,content) VALUES (?,?,?,?)",
        "args": [to, from, type, content],
        "handler": handler
    });
};

exports.findOneMessage = function (messageId, handler) {
  imRepo.execQuery({
      "sql": "SELECT msg_id, to_uid, from_uid, type, content, timestamp from msg_p2p where msg_id = ?",
      "args": [messageId],
      "handler": handler
  });
};

/**
 * 查找所有消息。
 *
 * @param to
 * @param from
 * @param contentType
 * @param start
 * @param end
 * @param count
 * @param closedInterval
 * @param handler
 */
exports.findAllMessages = function (to, from, contentType, start, end, count, closedInterval, handler) {
    var sql = "SELECT msg_id, to_uid, from_uid, type, content, timestamp from msg_p2p " +
        "WHERE ((to_uid=? AND from_uid=?) OR (to_uid=? AND from_uid=?)) " +
        "   AND type in (" + contentType + ") AND msg_id between ? and ? ORDER BY msg_id DESC LIMIT ?";

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

/**
 * 查找用户聊天过的医生列表。
 *
 * @param userId 指定的用户
 * @param handler
 */
exports.findAllP2PWithDoctor = function (userId, handler) {
    var sql = "SELECT DISTINCT d.code, d.name, d.sex, d.photo, ms3.last_content_type, ms3.last_content, ms3.timestamp, ms3.new_msg_count " +
        "FROM (SELECT DISTINCT CASE WHEN ms1.timestamp > ms2.timestamp THEN ms1.id ELSE ms2.id END id " +
        "      FROM msg_statistic ms1, msg_statistic ms2 " +
        "      WHERE ms1.from_gid IS NULL AND ms2.from_gid IS NULL " +
        "            AND ms1.uid = ms2.peer_uid AND ms1.peer_uid = ms2.uid) x, msg_statistic ms3, wlyy.wlyy_doctor d " +
        "WHERE x.id = ms3.id AND ms3.last_content_type in (1,2,3,5,6) AND " +
        "((ms3.uid = d.code AND ms3.peer_uid = ?) OR (ms3.uid = ? AND ms3.peer_uid = d.code)) GROUP BY d.code, d.name ORDER BY d.code";

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

/**
 * 查找用户聊天过的患者列表。
 *
 * @param userId
 * @param handler
 */
exports.findAllP2PWithPatient = function (userId, handler) {
    var sql = "SELECT p.code, p.name, p.birthday, p.sex, p.photo, ms.last_content_type, ms.last_content, ms.timestamp, ms.new_msg_count " +
        "FROM msg_statistic ms, wlyy.wlyy_patient p " +
        "WHERE ms.msg_type = 1 AND ms.last_content_type in (1,2,3,5,6) AND (ms.from_uid = ? AND ms.uid = p.code) OR (ms.uid = ? AND ms.from_uid = p.code)";

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

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

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

exports.isCurrentSessionFinished = function (userId, peerId, handler) {
    var sql = "SELECT c.consult consult_id, case when c.end_msg_id is not null then 1 else 0 end finished " +
        "FROM msg_p2p pm, wlyy.wlyy_consult_team c " +
        "WHERE ((pm.from_uid = ? AND pm.to_uid = ?) OR (pm.from_uid = ? AND pm.to_uid = ?)) " +
        "AND c.start_msg_id = pm.msg_id ORDER BY start_msg_id desc limit 1";

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