<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.sohu.cache.dao.InstanceDao">
    <sql id="instance_info_columns">
        id, app_id, host_id, ip, port, status, mem, conn, cmd, type
    </sql>

    <!--通过type查询实例列表-->
    <select id="getInstListByType" resultType="InstanceInfo" parameterType="int">
        SELECT
        <include refid="instance_info_columns"/>
        FROM instance_info
        WHERE type = #{type} and status=1
        ORDER BY id;
    </select>

    <!--通过appId查询实例列表-->
    <select id="getInstListByAppId" resultType="InstanceInfo" parameterType="long">
        SELECT
        <include refid="instance_info_columns"/>
        FROM instance_info
        WHERE app_id = #{appId}
        ORDER BY id;
    </select>

    <select id="getMemoryByHost" resultType="int">
        select ifnull(sum(mem),0)
        from instance_info
        where ip = #{host} and status in (0,1)
    </select>

    <!--通过host和port查询一个实例信息-->
    <select id="getInstByIpAndPort" resultType="InstanceInfo">
        SELECT
        <include refid="instance_info_columns"/>
        FROM instance_info
        WHERE ip = #{ip} AND port = #{port} AND status = 1
    </select>

    <select id="getAllInstByIpAndPort" resultType="InstanceInfo">
        SELECT
        <include refid="instance_info_columns"/>
        FROM instance_info
        WHERE ip = #{ip} AND port = #{port}
    </select>

    <!--通过host和port查询一个实例信息-->
    <select id="getAllInsts" resultType="InstanceInfo">
        SELECT
        <include refid="instance_info_columns"/>
        FROM instance_info
        WHERE status in (0,1)
    </select>

    <!--通过host和port查询一个实例数-->
    <select id="getCountByIpAndPort" resultType="int">
        SELECT count(*)
        FROM instance_info
        WHERE ip = #{ip} AND port = #{port}
    </select>

    <!--通过id查询一个实例信息-->
    <select id="getInstanceInfoById" resultType="InstanceInfo">
        SELECT
        <include refid="instance_info_columns"/>
        FROM instance_info
        WHERE id = #{id}
    </select>

    <!--保存实例-->
    <insert id="saveInstance" parameterType="InstanceInfo" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        INSERT INTO instance_info (app_id, host_id, ip, port, status, mem, conn, cmd, type)
        VALUE(#{appId}, #{hostId}, #{ip}, #{port}, #{status}, #{mem}, #{conn}, #{cmd}, #{type});
    </insert>

    <update id="update" parameterType="InstanceInfo">
        update instance_info
        set
        <if test="appId != null">app_id=#{appId},</if>
        <if test="hostId != null">host_id=#{hostId},</if>
        <if test="ip != null">ip=#{ip},</if>
        <if test="port != null">port=#{port},</if>
        <if test="mem != null">mem=#{mem},</if>
        <if test="conn != null">conn=#{conn},</if>
        <if test="cmd != null">cmd=#{cmd},</if>
        status=#{status}
        where
        id=#{id}
    </update>

    <select id="getAppInstanceInfo" parameterType="long" resultType="InstanceInfo">
        select
        <include refid="instance_info_columns"/>
        from instance_info where app_id=#{appId} and status=1
    </select>

    <select id="getInstanceTypeCount" resultType="int">
        select count(*)
        from instance_info
        where ip = #{ip} and type = #{type}
    </select>
    
    <!--通过机器ip查询实例列表-->
    <select id="getInstListByIp" resultType="InstanceInfo" parameterType="string">
        SELECT
        <include refid="instance_info_columns"/>
        FROM instance_info
        WHERE ip = #{ip} and status != 2
        ORDER BY id;
    </select>


</mapper>


