<?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.jeeplus.modules.sys.dao.SysRoleActivityDao">
    
	<sql id="sysRoleaAtivityColumns">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.del_type AS "delType",
		a.name AS "name",
		a.enname AS "enname",
		a.company_id AS "companyId",
		a.branch_company AS "branchCompanyId",
		a.type AS "type",
		a.sort AS "sort"
	</sql>
	
	<sql id="sysRoleaAtivityJoins">
		JOIN sys_office o ON o.id = a.company_id
	</sql>
	
    
	<select id="get" resultType="SysRoleActivity" >
		SELECT 
			<include refid="sysRoleaAtivityColumns"/>
		FROM sys_role_activity a
		<include refid="sysRoleaAtivityJoins"/>
		WHERE a.id = #{id}
	</select>

	<select id="isSysRoleActivityRelation" resultType="java.lang.String" >
		SELECT
			count(*)
		FROM sys_role_activity_relation a
		WHERE a.role_activity_id = #{id}
	</select>

	<select id="getByEnname" resultType="SysRoleActivity">
		SELECT
		<include refid="sysRoleaAtivityColumns"/>
		FROM sys_role_activity a
		<include refid="sysRoleaAtivityJoins"/>
		WHERE a.enname = #{enname}
	</select>
	<select id="getByRole" resultType="SysRoleActivity">
		SELECT
		<include refid="sysRoleaAtivityColumns"/>
		FROM sys_role_activity a
		JOIN sys_role_activity_relation r ON r.role_activity_id = a.id
		<include refid="sysRoleaAtivityJoins"/>
		WHERE r.role_id = #{roleId}
	</select>

	<select id="findList" resultType="SysRoleActivity" >
		SELECT 
			<include refid="sysRoleaAtivityColumns"/>
		FROM sys_role_activity a
		<include refid="sysRoleaAtivityJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="name != null and name != ''">
				AND a.name LIKE 
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="enname != null and enname != ''">
				AND a.enname LIKE 
					<if test="dbName == 'oracle'">'%'||#{enname}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{enname}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{enname},'%')</if>
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
			<if test="type != null and type != ''">
				AND a.type = #{type}
			</if>
			<if test="branchCompanyId != null and branchCompanyId != ''">
				AND a.branch_company = #{branchCompanyId}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findListByCompany" resultType="SysRoleActivity" >
		SELECT
			<include refid="sysRoleaAtivityColumns"/>
		FROM sys_role_activity a
		<include refid="sysRoleaAtivityJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="name != null and name != ''">
				AND a.name LIKE
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="enname != null and enname != ''">
				AND a.enname LIKE
					<if test="dbName == 'oracle'">'%'||#{enname}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{enname}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{enname},'%')</if>
			</if>
			<if test="companyId != null and companyId != ''">
				AND a.company_id = #{companyId}
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>

	<select id="findAllList" resultType="SysRoleActivity" >
		SELECT 
			<include refid="sysRoleaAtivityColumns"/>
		FROM sys_role_activity a
		<include refid="sysRoleaAtivityJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO sys_role_activity(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			del_type,
			name,
			enname,
			company_id,
			branch_company,
			type,
			sort
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{delType},
			#{name},
			#{enname},
			#{companyId},
			#{branchCompanyId},
			#{type},
			#{sort}
		)
	</insert>
	
	<update id="update">
		UPDATE sys_role_activity SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			name = #{name},
			company_id = #{companyId},
			branch_company = #{branchCompanyId},
			type = #{type},
			sort = #{sort}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM sys_role_activity
		WHERE id = #{id} AND del_type = '0'
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE sys_role_activity SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="SysRoleActivity" statementType="STATEMENT">
		select * FROM sys_role_activity  where ${propertyName} = '${value}'
	</select>
	
</mapper>