<?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">
<!-- 不使用namespace的话sql搜索定位会比较方便 -->
<mapper namespace="com.blueland.dao.ParameterDao">
	<resultMap id="RM_ParameterEntity" type="com.blueland.bo.ParameterEntity">
        <result property="pid" column="P_ID"/>
        <result property="description" column="DESCRIPTION"/>
        <result property="pkey" column="P_KEY"/>
        <result property="pvalue" column="P_VALUE"/>
        <result property="appId" column="APP_ID"/>
	</resultMap>

	<sql id="ParameterEntityColumns">
        	P_ID,
        	DESCRIPTION,
        	P_KEY,
        	P_VALUE,
        	APP_ID
	</sql>
	
	<sql id="ParameterEntityQueryColumns">
        	P_ID,
        	DESCRIPTION,
        	P_KEY,
        	P_VALUE,
        	APP_ID
	</sql>
	<!-- useGeneratedKeys="true" keyProperty="xxx" for sqlserver and mysql -->
	<insert id="insert" useGeneratedKeys="false" keyProperty="pid">
        INSERT INTO t_pf_parameter (<include refid="ParameterEntityColumns"/>) VALUES (
        	#{pid,jdbcType=VARCHAR},
        	#{description,jdbcType=VARCHAR},
        	#{pkey,jdbcType=VARCHAR},
        	#{pvalue,jdbcType=VARCHAR},
        	#{appId,jdbcType=VARCHAR}
        )
	</insert>

    
    <select id="get" resultMap="RM_ParameterEntity">
		SELECT <include refid="ParameterEntityQueryColumns"/>
	    FROM t_pf_parameter 
        WHERE 
	        P_ID = #{pid,jdbcType=VARCHAR} 
	</select>
	
	<select id="getObj" resultMap="RM_ParameterEntity">
		SELECT <include refid="ParameterEntityQueryColumns"/>
	    FROM t_pf_parameter 
       <include refid="findList_where"/>
	</select>
	
	 <select id="findPageList" resultMap="RM_ParameterEntity">
		SELECT p_id,
        	description,
        	p_key,
        	p_value
	    FROM t_pf_parameter 
	    where p_key like CONCAT('%',#{bo.pkey,jdbcType=VARCHAR},'%')
	    and p_value like CONCAT('%',#{bo.pvalue,jdbcType=VARCHAR},'%')
	    <if test="@Ognl@isNotEmpty(bo.appId)">
       		and APP_ID = #{bo.appId,jdbcType=VARCHAR}
       	</if>
	</select>

	<select id="findPageListNew" resultMap="RM_ParameterEntity">
		SELECT p_id,
		description,
		p_key,
		p_value
		FROM t_pf_parameter
		where p_key like CONCAT('%',#{bo.pkey,jdbcType=VARCHAR},'%')
		and p_value like CONCAT('%',#{bo.pvalue,jdbcType=VARCHAR},'%')
		<if test="@Ognl@isNotEmpty(bo.appId)">
			and APP_ID = #{bo.appId,jdbcType=VARCHAR}
		</if>
	</select>
	
	<select id="parameterListPage" resultMap="RM_ParameterEntity">
		SELECT p_id,
        	description,
        	p_key,
        	p_value,
        	app_id
	    FROM t_pf_parameter 
	    where app_id =#{bo.appId}
	</select>

	<select id="parameterPage" resultMap="RM_ParameterEntity">
		SELECT p_id,
			   description,
			   p_key,
			   p_value,
			   app_id
		FROM t_pf_parameter
		where app_id =#{bo.appId}
	</select>
    <!--
    	分页查询已经使用Dialect进行分页,也可以不使用Dialect直接编写分页
    	因为分页查询将传 offset,pageSize,lastRows 三个参数,不同的数据库可以根于此三个参数属性应用不同的分页实现
    -->
    <select id="findList" resultMap="RM_ParameterEntity">
    	SELECT <include refid="ParameterEntityQueryColumns"/>
	    FROM t_pf_parameter 
		<include refid="findList_where"/>
		
		<if test="@Ognl@isNotEmpty(bo.sortColumns)">
			ORDER BY ${bo.sortColumns}
		</if>
    </select>

	<select id="findPage" resultMap="RM_ParameterEntity">
		SELECT <include refid="ParameterEntityQueryColumns"/>
		FROM t_pf_parameter
		<include refid="findList_where"/>

		<if test="@Ognl@isNotEmpty(bo.sortColumns)">
			ORDER BY ${bo.sortColumns}
		</if>
	</select>
	
	
	<sql id="findList_where">
		<!-- ognl访问静态方法的表达式 为@class@method(args),以下为调用rapid中的Ognl.isNotEmpty()方法,还有其它方法如isNotBlank()可以使用，具体请查看Ognl类 -->
		<where>	      				
	       <if test="@Ognl@isNotEmpty(bo.pid)">
				AND P_ID = #{bo.pid,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.description)">
				AND DESCRIPTION = #{bo.description,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.pkey)">
				AND P_KEY = #{bo.pkey,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.pvalue)">
				AND P_VALUE = #{bo.pvalue,jdbcType=VARCHAR}
			</if>
			<if test="@Ognl@isNotEmpty(bo.appId)">
				AND APP_ID = #{bo.appId,jdbcType=VARCHAR}
			</if>
		</where>
	</sql>
    
    <select id="getByKey" resultMap="RM_ParameterEntity">
		SELECT <include refid="ParameterEntityQueryColumns"/>
	    FROM t_pf_parameter 
        WHERE 
	        P_KEY = #{key,jdbcType=VARCHAR} 
	    limit 1
	</select>

	<select id="getValueByKey" resultType="string">
		SELECT P_VALUE
		FROM t_pf_parameter
		WHERE
		P_KEY like concat('%', #{key}, '%')
		limit 1
	</select>
	
</mapper>

