<?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.AppDao">
    <sql id="app_desc_fields">
        app_id,name,user_id,status,intro,create_time,passed_time,type,officer,
        ver_id,is_test,has_back_store,need_persistence,need_hot_back_up,forecase_qps,
        forecast_obj_num,mem_alert_value,client_machine_room,app_key,client_conn_alert_value
    </sql>
    <!--通过appId查询app的信息-->
    <select id="getAppDescById" resultType="AppDesc" parameterType="long">
        SELECT
            <include refid="app_desc_fields"/>
        FROM app_desc
        WHERE app_id = #{appId};
    </select>
    
    <!-- 根据应用名查询app信息 -->
    <select id="getByAppName" resultType="AppDesc" parameterType="string">
        SELECT
            <include refid="app_desc_fields"/>
        FROM app_desc
        WHERE name = #{appName};
    </select>
    
    
    <insert id="save" parameterType="AppDesc"  keyProperty="appId" useGeneratedKeys="true" >
    	insert into app_desc
    		(<include refid="app_desc_fields"/>)
		values
			(#{appId},#{name},#{userId},#{status},#{intro},#{createTime},#{passedTime},
			#{type},#{officer},#{verId},#{isTest},#{hasBackStore},#{needPersistence},
			#{needHotBackUp},#{forecaseQps},#{forecastObjNum},#{memAlertValue},#{clientMachineRoom},#{appKey},#{clientConnAlertValue})    	
    </insert>
    
    <update id="update" parameterType="AppDesc">
    	update app_desc 
    		set name=#{name}, user_id=#{userId}, status=#{status}, intro=#{intro}, create_time=#{createTime},
    			passed_time=#{passedTime},type=#{type},
    			officer=#{officer},ver_id=#{verId},mem_alert_value=#{memAlertValue},client_conn_alert_value=#{clientConnAlertValue}
    	where app_id=#{appId}
    </update>
    
    <sql id="app_desc_select_column">
    	app_desc.app_id,name,app_desc.user_id,status,intro,create_time,passed_time,type,officer,ver_id,app_key
    </sql>
    <select id="getAppDescList" resultType="AppDesc" parameterType="long">
    	select           
        	<include refid="app_desc_select_column"/>
        from app_desc,app_to_user where app_to_user.user_id=#{userId} and app_to_user.app_id=app_desc.app_id
    </select>
    
    <select id="getUserAppCount" resultType="int" parameterType="long">
    	select count(app_desc.app_id) from app_desc,app_to_user where app_to_user.user_id=#{userId} and app_to_user.app_id=app_desc.app_id
    </select>
    
    <select id="getAllAppCount" resultType="int" parameterType="AppSearch">
    	select count(app_id) from app_desc where 1=1
        <choose>
            <when test="appName != null and appName != ''">
            	and name like '${appName}%'
       	 	</when>
        </choose>
        <choose>
            <when test="appType != null and appType > 0">
            	and type = ${appType}
       	 	</when>
        </choose>
        <choose>
            <when test="appStatus != null and appStatus >= 0">
            	and status = ${appStatus}
       	 	</when>
        </choose>
        <choose>
            <when test="appId != null and appId > 0">
            	and app_id = ${appId}
       	 	</when>
        </choose>
    </select>
    
    <select id="getAllAppDescList" resultType="AppDesc" parameterType="AppSearch">
    	select           
        	<include refid="app_desc_fields"/>
        from app_desc where 1=1
        <choose>
            <when test="appName != null and appName != ''">
            	and name like '${appName}%'
       	 	</when>
        </choose>
        <choose>
            <when test="appType != null and appType > 0">
            	and type = ${appType}
       	 	</when>
        </choose>
        <choose>
            <when test="appStatus != null and appStatus >= 0">
            	and status = ${appStatus}
       	 	</when>
        </choose>
        <choose>
            <when test="appId != null and appId > 0">
            	and app_id = ${appId}
       	 	</when>
        </choose>
        <choose>
        	<when test="page != null">
        		<choose>
		        	<when test="page.totalCount > page.pageSize">
            			limit #{page.start},#{page.pageSize};
		        	</when>
		        	<otherwise>
		        	    limit #{page.totalCount}
		        	</otherwise>
		        </choose>
        	</when>
        </choose>
    </select>
    
    <update id="updateAppKey">
    	update app_desc set app_key=#{appKey} where app_id=#{appId}
    </update>
    
</mapper>


