<?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.ApplicationDao">
	<resultMap id="RM_PfApplicationEntity" type="com.blueland.bo.ApplicationEntity">
        <result property="appId" column="app_id"/>
        <result property="appName" column="app_name"/>
        <result property="appStatus" column="app_status"/>
        <result property="frontPort" column="front_port"/>
        <result property="rearPort" column="rear_port"/>
        <result property="appType" column="app_type"/>
        <result property="appDescribe" column="app_describe"/>
        <result property="isDeploy" column="is_deploy"/>
        <result property="isService" column="is_service"/>
        <result property="sort" column="sort"/>
        <result property="remark" column="remark"/>
        <result property="createUserCode" column="create_user_code"/>
        <result property="createUserName" column="create_user_name"/>
        <result property="updateUserCode" column="update_user_code"/>
        <result property="updateUserName" column="update_user_name"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="indexUrl" column="index_url"/>
        <result property="busiType" column="busi_type"/>
	</resultMap>

	<sql id="PfApplicationEntityColumns">
        	app_id,
        	app_name,
        	app_status,
        	front_port,
        	rear_port,
        	app_type,
        	app_describe,
        	is_deploy,
        	is_service,
        	sort,
        	remark,
        	create_user_code,
        	create_user_name,
        	update_user_code,
        	update_user_name,
        	create_time,
        	update_time,
        	index_url,
        	busi_type
	</sql>
	
	<sql id="PfApplicationEntityQueryColumns">
        	app_id,
        	app_name,
        	app_status,
        	front_port,
        	rear_port,
        	app_type,
        	app_describe,
        	is_deploy,
        	is_service,
        	sort,
        	remark,
        	create_user_code,
        	create_user_name,
        	update_user_code,
        	update_user_name,
        	create_time,
        	update_time,
        	index_url,
        	busi_type
	</sql>
	
	<sql id="PfApplicationEntityQueryColumns1">
        	T1.app_id,
        	T1.app_name,
        	T1.app_status,
        	T1.front_port,
        	T1.rear_port,
        	T1.app_type,
        	T1.app_describe,
        	T1.is_deploy,
        	T1.is_service,
        	T1.sort,
        	T1.remark,
        	T1.create_user_code,
        	T1.create_user_name,
        	T1.update_user_code,
        	T1.update_user_name,
        	T1.create_time,
        	T1.update_time,
        	T1.index_url,
        	T1.busi_type
	</sql>
	<!-- useGeneratedKeys="true" keyProperty="xxx" for sqlserver and mysql -->
	<insert id="insert" useGeneratedKeys="false" keyProperty="id">
        INSERT INTO t_pf_application (<include refid="PfApplicationEntityColumns"/>) VALUES (
        	#{appId,jdbcType=VARCHAR},
        	#{appName,jdbcType=VARCHAR},
        	#{appStatus,jdbcType=VARCHAR},
        	#{frontPort,jdbcType=VARCHAR},
        	#{rearPort,jdbcType=VARCHAR},
        	#{appType,jdbcType=VARCHAR},
        	#{appDescribe,jdbcType=VARCHAR},
        	#{isDeploy,jdbcType=VARCHAR},
        	#{isService,jdbcType=VARCHAR},
        	#{sort,jdbcType=INTEGER},
        	#{remark,jdbcType=VARCHAR},
        	#{createUserCode,jdbcType=VARCHAR},
        	#{createUserName,jdbcType=VARCHAR},
        	#{updateUserCode,jdbcType=VARCHAR},
        	#{updateUserName,jdbcType=VARCHAR},
        	#{createTime,jdbcType=TIMESTAMP},
        	#{updateTime,jdbcType=TIMESTAMP},
        	#{indexUrl,jdbcType=VARCHAR},
        	#{busiType,jdbcType=VARCHAR}       	
        )
	</insert>
    
    <select id="get" resultMap="RM_PfApplicationEntity">
		SELECT <include refid="PfApplicationEntityQueryColumns"/>
	    FROM t_pf_application 
        WHERE 
	        app_id = #{appId,jdbcType=VARCHAR} 
	</select>
	
    <!--
    	分页查询已经使用Dialect进行分页,也可以不使用Dialect直接编写分页
    	因为分页查询将传 offset,pageSize,lastRows 三个参数,不同的数据库可以根于此三个参数属性应用不同的分页实现
    -->
    <select id="findList" resultMap="RM_PfApplicationEntity">
    	SELECT <include refid="PfApplicationEntityQueryColumns"/>
	    FROM t_pf_application 
		<include refid="findList_where"/>
		<if test="@Ognl@isNotEmpty(bo.sortColumns)">
			ORDER BY ${bo.sortColumns}
		</if>
    </select>

	<select id="findPage" resultMap="RM_PfApplicationEntity">
		SELECT <include refid="PfApplicationEntityQueryColumns"/>
		FROM t_pf_application
		<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.appId)">
				AND app_id = #{bo.appId,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.appName)">
				AND app_name = #{bo.appName,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.appStatus)">
				AND app_status = #{bo.appStatus,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.frontPort)">
				AND front_port = #{bo.frontPort,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.rearPort)">
				AND rear_port = #{bo.rearPort,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.appType)">
				AND app_type = #{bo.appType,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.appDescribe)">
				AND app_describe = #{bo.appDescribe,jdbcType=VARCHAR}
			</if>
			<if test="@Ognl@isNotEmpty(bo.isDeploy)">
				AND is_deploy = #{bo.isDeploy,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.isService)">
				AND is_service = #{bo.isService,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.sort)">
				AND sort = #{bo.sort,jdbcType=INTEGER}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.remark)">
				AND remark = #{bo.remark,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.createUserCode)">
				AND create_user_code = #{bo.createUserCode,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.createUserName)">
				AND create_user_name = #{bo.createUserName,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.updateUserCode)">
				AND update_user_code = #{bo.updateUserCode,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.updateUserName)">
				AND update_user_name = #{bo.updateUserName,jdbcType=VARCHAR}
			</if>
			<if test="@Ognl@isNotEmpty(bo.indexUrl)">
				AND index_url = #{bo.indexUrl,jdbcType=VARCHAR}
			</if>
			<if test="@Ognl@isNotEmpty(bo.busiType)">
				AND busi_type = #{bo.busiType,jdbcType=VARCHAR}
			</if>
			
		</where>
	</sql>
	
	<sql id="where">
		<where>
	 			<if test="@Ognl@isNotEmpty(bo.appId)">T1.app_Id = #{bo.appId}  </if>
	 			<if test="@Ognl@isNotEmpty(bo.appName)">and T1.app_name like CONCAT('%',#{bo.appName},'%')</if>
	 			<if test="@Ognl@isNotEmpty(bo.appStatus)">and T1.app_status = #{bo.appStatus}  </if>
	 			<if test="@Ognl@isNotEmpty(bo.frontPort)">and T1.front_port = #{bo.frontPort}</if>
	       		<if test="@Ognl@isNotEmpty(bo.rearPort)">and T1.rear_port = #{bo.rearPort}</if>
	 			<if test="@Ognl@isNotEmpty(bo.appType)">and T1.app_type = #{bo.appType}  </if>
	 			<if test="@Ognl@isNotEmpty(bo.appDescribe)">and T1.app_describe = #{bo.appDescribe}  </if>
	 			<if test="@Ognl@isNotEmpty(bo.isDeploy)">and T1.is_deploy = #{bo.isDeploy,jdbcType=VARCHAR}</if>
	       		<if test="@Ognl@isNotEmpty(bo.isService)">and T1.is_service = #{bo.isService,jdbcType=VARCHAR}</if>
	 			<if test="@Ognl@isNotEmpty(bo.sort)">and T1.sort = #{bo.sort}  </if>
	 			<if test="@Ognl@isNotEmpty(bo.remark)">and T1.remark = #{bo.remark}  </if>
	 			<if test="@Ognl@isNotEmpty(bo.createUserCode)">and T1.create_user_code = #{bo.createUserCode}  </if>
	 			<if test="@Ognl@isNotEmpty(bo.createUserName)">and T1.create_user_name = #{bo.createUserName}  </if>
	 			<if test="@Ognl@isNotEmpty(bo.updateUserCode)">and T1.update_user_code = #{bo.updateUserCode}  </if>
	 			<if test="@Ognl@isNotEmpty(bo.updateUserName)">and T1.update_user_name = #{bo.updateUserName}  </if>
	 			<if test="@Ognl@isNotEmpty(bo.createTime)">and T1.create_time = #{bo.createTime}  </if>
	 			<if test="@Ognl@isNotEmpty(bo.updateTime)">and T1.update_time = #{bo.updateTime}  </if>
	 			<if test="@Ognl@isNotEmpty(bo.indexUrl)">and T1.index_url = #{bo.indexUrl}  </if>
	 			<if test="@Ognl@isNotEmpty(bo.busiType)">and T1.busi_type = #{bo.busiType}  </if>
		</where>
	</sql>
	
	<!-- find pfApplication list -->
	<select id="findApplicationListByParams" resultMap="RM_PfApplicationEntity">
		select <include refid="PfApplicationEntityQueryColumns" /> from t_pf_application T1
		<include refid="findList_where"/>
		<if test="@Ognl@isNotEmpty(bo.notAppType)">
			AND app_type != #{bo.notAppType,jdbcType=VARCHAR}
		</if>
        <if test="@Ognl@isNotBlank(bo.orderByColumns)">order by ${bo.orderByColumns}</if>
	</select>

	<select id="findApplicationPageByParams" resultMap="RM_PfApplicationEntity">
		select <include refid="PfApplicationEntityQueryColumns" /> from t_pf_application T1
		<include refid="findList_where"/>
		<if test="@Ognl@isNotEmpty(bo.notAppType)">
			AND app_type != #{bo.notAppType,jdbcType=VARCHAR}
		</if>
		<if test="@Ognl@isNotBlank(bo.orderByColumns)">order by ${bo.orderByColumns}</if>
	</select>
    
    <!-- 批量删除 -->
    <delete id="batchDelete" parameterType="java.lang.String">
        delete from t_pf_application where app_id in
        <foreach item="appId" collection="array" open="(" separator="," close=")">
            #{appId}
        </foreach>
    </delete>
    
    <!-- 应用关联模块所有数据 -->
    <select id="appLinkModule" resultType="java.util.HashMap">
		select <include refid="PfApplicationEntityQueryColumns1" /> from t_pf_application T1
		left join t_pf_module T2 on T1.app_id = T2.app_id
	</select>
	
	<!-- 根据用户获取所有应用 -->
	<select id="findApplicationListByUser" resultMap="RM_PfApplicationEntity">
		select 
			DISTINCT t3.*
		from 
			t_pf_role t1,
		  t_pf_role2user t2,
			t_pf_application t3
		where t1.ROLE_ID = t2.ROLE_ID and t1.APP_ID = t2.APP_ID  and t1.APP_ID = t3.APP_ID
		and t1. STATUS = 'y' and t2. STATUS = 'y'
		and t2.USER_ID = #{userId}
	</select>
	
</mapper>

