package com.cesgroup.cesmonitor.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.cesgroup.cesmonitor.bean.FastGroupGraph;
import com.cesgroup.cesmonitor.bean.QuickAccess;
import com.cesgroup.cesmonitor.bean.QuickAccessGraph;
import com.cesgroup.framework.base.dao.IBaseDao;

/**
 *
 * 上海中信信息发展股份有限公司;
 *
 * @author user
 * @version 1.0.0 2018年1月10日
 */
public interface QuickAccessMapper extends IBaseDao<Object> {

    /**
     *
     * @return 返回usrgrp
     */
    @Select("select u.name from usrgrp u;")
    List<String> findUsrGroup();

    /**
     * 查询用户群组所对应的主机群组
     *
     * @param usrgrpid
     *
     * @return 返回指定用户群组的主机群组name
     */
    @Select("select g.`name` from usrgrp u,rights r,groups g where u.usrgrpid =#{param1} "
        + "and r.groupid = u.usrgrpid and g.groupid = r.id")
    List<String> findNameByUsrgrpid(String usrgrpid);

    /**
     *
     * @param fastGroupId
     *
     * @return 返回指定快速访问组下的主机的图形
     */
    @Select(" select * from fastgroup_graph fg where fg.fast_groupid = #{param1}")
    List<String> getGraphByGroupId(String fastGroupId);

    /**
     *
     * @param usrgrpid,groupid
     *
     * @return 返回指定主机群组下的主机
     */
    @Select(" select h.`name` from groups g,hosts_groups hg,usrgrp u,hosts h,rights r "
        + " where u.usrgrpid =#{param1} and g.groupid =#{param2} and g.groupid= hg.groupid and "
        + " hg.hostid = h.hostid and g.groupid = r.id and r.groupid = u.usrgrpid and h.status=0")
    List<String> findHostByGroupId(String usrgrpid, String groupid);

    /**
     *
     * @param usrgrpid,groupid,hostId
     *
     * @return 返回指定主机下的图形
     */
    @Select(" select gr.`name`, count(distinct gr.name) from groups g,hosts_groups hg,"
        + "usrgrp u,hosts h,rights r ,items i, graphs_items gi,graphs gr "
        + " where  h.hostid = i.hostid AND gi.itemid = i.itemid and gi.graphid = gr.graphid and "
        + " u.usrgrpid =#{param1} and g.groupid =#{param2} and h.hostid =#{param3} "
        + "and g.groupid= hg.groupid and hg.hostid = h.hostid and "
        + " g.groupid = r.id and r.groupid = u.usrgrpid group by gr.name")
    List<String> getGraphByHostId2(String usrgrpid, String groupid, String hostId);

    /**
     *
     * @param
     *
     * @return 新增fastgroup_graph
     */
    @Insert("insert into fastgroup_graph values(#{param1},#{param2},#{param3},#{param4},#{param5},#{param6})")
    int addFastGroupGraph(String fastGroupGraphId, String fastgroup_id, String name, int group_id, int host_id,
        int graph_id);

    /**
     *
     * @param
     *
     * @return 新增fast_group
     */
    @Insert("insert into fast_group values(#{param1},#{param2})")
    int addFastGroup(String id, String name);

    /**
     *
     * @param
     *
     * @return 新增fastgroup_usrgroup
     */
    @Insert("insert into fastgroup_usrgroup values(#{param1},#{param2})")
    int addFastGroupUsrGroup(String fast_group_id, String usrgroup_id);

    /**
     *
     * @param hostId
     *            主机的ID号
     * @return 返回主机异常数量
     */
    @Select("SELECT COUNT(*) FROM `triggers` t, `events` e,functions f, items i, `hosts` h "
        + "WHERE e.object = 0 AND h.hostid =#{param1} "
        + "AND h.hostid =i.hostid AND e.source=0 AND e.objectid = t.triggerid "
        + "AND f.triggerid = t.triggerid AND f.itemid = i.itemid AND t.`value` = 1 AND t.status = 0")
    Integer findHostStatus(Integer hostId);

    /**
     *
     * @param
     *
     * @return 更新fast_group
     */
    @Update("update fast_group fg SET fg.name = #{param2} where fg.id = #{param1}")
    Integer updateFastGroup(String id, String fastGroupName);

    /**
     *
     * @param   s
     *          fast_group_id
     * @param usrGroupId
     *          usrgroup_id
     * @return 更新fastgroup_usrgroup
     */
    @Update("update fastgroup_usrgroup fu SET (fg.usrgroup_id = #{param2}) where fg.fast_group_id = #{param1}")
    List<String> updateFastGroupUsrGroup(String s, String usrGroupId);

    /**
     *
     * @param
     *
     * @return 更新fastgroup_graph
     */
    @Update("update fastgroup_graph fg SET (fg.name = #{param2}),(fg.group_id = #{param3}),(fg.host_id = #{param4}),"
        + "(fg.graph_id = #{param5}) where fg.fast_groupid = #{param1}")
    List<String> updateFastGroupGraph(String fast_groupid, String name, int group_id, int host_id, int graph_id);

    /**
     *
     * @param
     *
     * @return 删除fast_group
     */
    @Delete("delete from fast_group where id = #{param1}")
    int deleteFastGroup(String id);

    /**
     *
     * @param fast_group_id
     *          fast_group_id
     * @return 删除fastgroup_usrgroup
     */
    @Delete("delete from fastgroup_usrgroup  where fast_group_id = #{param1}")
    int deleteFastGroupUsrGroup(String fast_group_id);

    /**
     *
     * @param
     *
     * @return 删除fastgroup_graph
     */
    @Delete("delete from fastgroup_graph where fast_groupid = #{param1}")
    int deleteFastGroupGraph(String fast_groupid);

    /**
     *
     * @param usrgrpid,groupid,hostid
     *
     * @return 返回指定主机群组下的主机的图形
     */
    @Select(" select h.`name` from groups g,hosts_groups hg,usrgrp u,hosts h,rights r "
        + " where u.usrgrpid =#{param1} and g.groupid =#{param2} and g.groupid= hg.groupid and "
        + " hg.hostid = h.hostid and g.groupid = r.id and r.groupid = u.usrgrpid ")
    List<String> findIpByHostId(String usrgrpid, String groupid, String hostid);

    /**
     *
     * @param fastGroupName
     *
     * @return 根据图形名称返回图形
     */
    @Select("select fg.`name` from fastgroup_graph fg where fg.name =#{param1}")
    String findFastGroupGraphBySelfdefineName(String fastGroupName);

    /**
     *
     * @param fastGroupName
     *
     * @return 根据图形名称返回图形
     */
    @Select("select fg.`name` from fastgroup_graph fg where fg.name =#{param1}")
    List<String> findFastGroupGraphByGraphName(String fastGroupName);

    /**
     * @param fastGroupName
     *          快速访问群组名
     * @return
     *          快速访问群组
     */
    @Select("select fg.id from fast_group fg where fg.name =#{param1}")
    String findFastGroupByName(String fastGroupName);

    /**
     * @param fastGroupGraphId
     *          快速访问群组图形id
     * @return 删除快速访问图形
     */
    @Delete("delete from fastgroup_graph  where id = #{param1}")
    int deleteQuickAccessGraph(String fastGroupGraphId);

    /**
     *
     * @param fastGroupName
     *
     * @return 根据用户Id所在组返回快速访问列表
     */
    @Select("select distinct fg.id groupid,fg.name groupname,fgg.name graphname from users u, users_groups ug, "
        + "usrgrp usrg ,fastgroup_usrgroup fu,fast_group fg,fastgroup_graph fgg, rights r where "
        + "ug.userid = u.userid and ug.usrgrpid = usrg.usrgrpid and fu.usrgroup_id = usrg.usrgrpid "
        + "and fg.id = fu.fast_group_id and fgg.fast_groupid = fg.id and ug.userid = u.userid "
        + "and ug.usrgrpid = usrg.usrgrpid and fu.usrgroup_id = usrg.usrgrpid and fg.id = fu.fast_group_id "
        + "and r.groupid = ug.usrgrpid and ug.userid = u.userid and ug.usrgrpid = usrg.usrgrpid "
        + "and u.userid =#{param1} "
        + "and fgg.fast_groupid = fg.id and fgg.group_id = r.id and r.groupid = ug.usrgrpid order by fg.id")
    List<QuickAccessGraph> findFastGroupGraphByUserId(String UserId);

    /**
     *
     * @param fastGroupName
     *
     * @return 根据用户Id所在组返回快速访问列表
     */
    @Select("select fg.id groupid,fg.name groupname,fgr.name graphname from users u, "
        + "users_groups ug,usrgrp usrg ,fastgroup_usrgroup fu,fast_group fg,fastgroup_graph fgr "
        + " where u.userid = #{param1} and ug.userid = u.userid and ug.usrgrpid = usrg.usrgrpid "
        + " and fg.id = fu.fast_group_id and fgr.fast_groupid = fg.id order by fg.id")
    List<QuickAccessGraph> superAdminfindFastGroupGraphByUserId(String UserId);

    /**
     * @param userId
     *          用户id
     * @return
     *          用户图形
     */
    @Select("select ug.usrgrpid from users_groups ug where ug.userid =#{param1}")
    List<String> findUsrGrpByUserId(String userId);

    /**
     * @param fastGroupId
     *          快速访问群组id
     * @return
     *          图形
     */
    @Select("select t.id, t.name, g.`name` groupname, h.`name` hostname , gr.name graphname "
        + " from (select fg.id, fg.`name`,fg.fast_groupid, fg.group_id, fg.host_id, fg.graph_id "
        + "from fastgroup_graph fg where fast_groupid =#{param1}) t ,"
        + " groups g,hosts h, graphs gr where t.group_id = g.groupid and t.host_id = h.hostid "
        + "and gr.graphid = t.graph_id")
    List<QuickAccess> getGraphsByFastGroupId(String fastGroupId);

    /**
     * @param fast_groupid
     *          快速访问群组id
     * @return
     *          快速访问群组图形
     */
    @Select("select fg.name from fastgroup_graph fg where fg.fast_groupid =#{param1}")
    ArrayList<String> findFastGroupGraphsByFastGroupId(String fast_groupid);

    /**
     * @param groupId
     *          群组id
     * @return
     *          快速访问群组名
     */
    @Select("select fg.name from fast_group fg where fg.id =#{param1}")
    String findFastGroupNameByGroupId(String groupId);

    /**
     * @param groupId
     *          群组id
     * @param userId
     *          用户id
     * @return
     *      快速访问群组图形
     */
    @Select("select distinct fgg.name from users u, users_groups ug, "
        + "usrgrp usrg ,fastgroup_graph fgg, rights r where fgg.fast_groupid = #{param1} "
        + "and ug.userid = u.userid and ug.usrgrpid = usrg.usrgrpid and ug.userid = u.userid "
        + "and r.groupid = ug.usrgrpid and u.userid =#{param2} and fgg.group_id = r.id ")
    ArrayList<String> findFastGroupGraphsByFastGroupIdandUser(String groupId, String userId);

    /**
     * @param groupId
     *          群组id
     * @return
     *           快速访问群组名
     */
    @Select("select fg.name from fast_group fg where fg.id =#{param1}")
    String findFastGroupNameByGroupIdandUser(String groupId);

    /**
     * @param fastGroupId
     * 快速访问群组id
     * @param userId
     *      用户id
     * @return
     * getGraphsByFastGroupIdandUserId
     */
    @Select("select t.id, t.name, g.`name` groupname, h.`name` hostname , gr.name graphname "
        + "from (select distinct fgg.id, fgg.`name`,fgg.fast_groupid, fgg.group_id, fgg.host_id, fgg.graph_id "
        + "from users u, users_groups ug,usrgrp usrg ,fastgroup_graph fgg, rights r where "
        + "ug.userid = u.userid and ug.usrgrpid = usrg.usrgrpid and ug.userid = u.userid and r.groupid = ug.usrgrpid "
        + "and u.userid =#{param2} and fgg.group_id = r.id and fast_groupid =#{param1}) t , "
        + "groups g,hosts h, graphs gr where t.group_id = g.groupid and t.host_id = h.hostid "
        + "and gr.graphid = t.graph_id")
    List<QuickAccess> getGraphsByFastGroupIdandUserId(String fastGroupId, Integer userId);

    /**
     * @param fastGroupGraphId
     *          快速访问群组图形id
     * @return
     *          快速访问群组id
     */
    @Select("select fgg.fast_groupid from fastgroup_graph fgg where fgg.id =#{param1}")
    String findFastGroupIdByFastGroupGraphId(String fastGroupGraphId);

    /**
     * @param groupId
     *          群组id
     * @return
     *      快速访问图形
     */
    @Select("select fgg.id,fgg.name,fgg.group_id groupid,fgg.host_id hostid,fgg.graph_id graphid "
        + "from fastgroup_graph fgg where fgg.fast_groupid =#{param1}")
    List<FastGroupGraph> findFastGraphByFastGroupId(String groupId);

}
