/**
 * 群组模型。此数据来源于家庭医生平台数据库的行政团队。
 *
 * 实际团队数据分为两部分：行政团队与临时讨论组，具体的与业务相关。医生在讨论组里，
 * 会出现两种：在行政团队内聊天，也可以在临时讨论组里聊天。
 */
"use strict";

var wlyyRepo = require("./database/wlyy.db.js");
var GROUP_TYPE = require('../include/commons').GROUP_TYPE;

/**
 * 判断是否为团队成员。
 *
 * @param groupId
 * @param groupType
 * @param doctorId
 * @param handler
 */
exports.isGroupMember = function (groupId, groupType, doctorId, handler) {
    if (groupType == GROUP_TYPE.AdminTeam) {
        wlyyRepo.execQuery({
            "sql": "SELECT doctor_code user_id from wlyy_admin_team_member WHERE team_id=? and doctor_code=?",
            "args": [groupId, doctorId],
            "handler": handler
        });
    } else {
        wlyyRepo.execQuery({
            "sql": "SELECT member_code user_id from wlyy_talk_group_member WHERE group_code=? and member_code=?",
            "args": [groupId, doctorId],
            "handler": handler
        });
    }
};

/**
 * 获取团队成员。
 *
 * @param groupId
 * @param groupType
 * @param handler
 */
exports.getMembers = function (groupId, groupType, handler) {
    if (groupType == GROUP_TYPE.AdminTeam) {
        wlyyRepo.execQuery({
            "sql": "SELECT doctor_code user_id from wlyy_admin_team_member WHERE team_id=? AND available = 1",
            "args": [groupId],
            "handler": handler
        });
    } else {
        wlyyRepo.execQuery({
            "sql": "SELECT member_code user_id from wlyy_talk_group_member WHERE group_code=?",
            "args": [groupId],
            "handler": handler
        });
    }
};

exports.getMembersAvatar = function (groups, handler) {
    /*var sql = "SELECT m.member_code g_code, COUNT(m.member_code) member_count, d.code dr_code, d.name dr_name, d.photo dr_photo " +
     "FROM wlyy.wlyy_talk_group_member m, wlyy.wlyy_doctor d " +
     "WHERE m.member_code = d.code AND m.group_code IN (" + groups + ") " +
     "UNION " +
     "SELECT m.team_id g_code, COUNT(m.team_id) member_count, d.code dr_code, d.name dr_name, d.photo dr_photo " +
     "FROM wlyy.wlyy_admin_team_member m, wlyy.wlyy_doctor d " +
     "WHERE m.doctor_code = d.code AND m.team_id IN (" + groups + ") GROUP BY m.team_id";*/

    var sql = "SELECT * FROM(" +
        "SELECT m.group_code g_code, d.code code, d.name name, d.photo photo, 'doctor' type FROM " +
        "wlyy.wlyy_talk_group_member m, wlyy.wlyy_doctor d " +
        "WHERE m.member_code = d.code AND m.group_code IN (" + groups + ") " +
        " UNION " +
        "SELECT m.group_code g_code, p.code code, p.name name, p.photo photo, 'patient' type " +
        "FROM  wlyy.wlyy_talk_group_member m, wlyy.wlyy_patient p " +
        "WHERE m.member_code = p.code AND m.group_code IN (" + groups + ")" +
        " UNION " +
        "SELECT m.team_id g_code, d.code code, d.name name, d.photo photo, 'doctor' type " +
        "FROM wlyy.wlyy_admin_team_member m, wlyy.wlyy_doctor d " +
        "WHERE m.doctor_code = d.code AND m.team_id IN (" + groups + ") " +
        ") x ORDER BY x.g_code";

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