<?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.ydd.oms.mapper.sys.PermissionMapper">

    <resultMap id="PermissionMap" type="com.ydd.oms.entity.sys.Permission">
		<id column="id" jdbcType="INTEGER" property="id" />
		<result column="name" property="name" />
		<result column="key" property="key" />
		<result column="menu_id" property="menuId" />
		<result column="created_id" property="createdId" />
		<result column="modified_id" property="modifiedId" />
		<result column="created_time" property="createdTime" />
		<result column="modified_time" property="modifiedTime" />
		<result column="deleted" property="deleted" />
    </resultMap>

	<resultMap id="PermissionDTOMap" type="com.ydd.oms.dto.sys.PermissionDTO">
		<id column="id" jdbcType="INTEGER" property="id" />
		<result column="name" property="name" />
		<result column="key" property="key" />
	</resultMap>
	
	<!--表名 -->
	<sql id="tableName">
		sys_permission
	</sql>
	
	<!-- 字段 -->
	<sql id="Field">
		id,
		`name`,
		`key`,
		menu_id,
		created_id,
		modified_id,
		created_time,
		modified_time,
		deleted
	</sql>
	
	<!-- 字段值 -->
	<sql id="FieldValue">
		#{id},
		#{name},
		#{key},
		#{menuId},
		#{createdId},
		#{modifiedId},
		#{createdTime},
		#{modifiedTime},
		#{deleted}
	</sql>

	<!-- 查询全部记录 -->
    <select id="findAll" resultMap="PermissionMap">
        SELECT
        	<include refid="Field"></include>
        FROM
        	<include refid="tableName"></include>
		WHERE
			deleted = 0
    </select>
	
	<!-- 新增-->
	<insert id="insert" parameterType="com.ydd.oms.entity.sys.Permission" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO
			<include refid="tableName"></include>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
				id,
            </if>
            <if test="name != null">
				name,
            </if>
            <if test="key != null">
				`key`,
            </if>
            <if test="menuId != null">
				menu_id,
            </if>
            <if test="createdId != null">
				created_id,
            </if>
            <if test="modifiedId != null">
				modified_id,
            </if>
            <if test="createdTime != null">
				created_time,
            </if>
            <if test="modifiedTime != null">
				modified_time,
            </if>
            <if test="deleted != null">
				deleted,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
				#{id},
            </if>
            <if test="name != null">
				#{name},
            </if>
            <if test="key != null">
				#{key},
            </if>
            <if test="menuId != null">
				#{menuId},
            </if>
            <if test="createdId != null">
				#{createdId},
            </if>
            <if test="modifiedId != null">
				#{modifiedId},
            </if>
            <if test="createdTime != null">
				#{createdTime},
            </if>
            <if test="modifiedTime != null">
				#{modifiedTime},
            </if>
            <if test="deleted != null">
				#{deleted},
            </if>
        </trim>
	</insert>
	
	<!-- 删除-->
	<delete id="delete" parameterType="java.lang.Integer">
		UPDATE
			<include refid="tableName"></include>
		SET
			deleted = 1
		WHERE
			id = #{id}
	</delete>

    <!-- 批量删除 -->
    <delete id="deleteByIds">
        UPDATE
        	<include refid="tableName"></include>
		SET
			deleted = 1
        WHERE
			id IN
			<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
				#{item}
			</foreach>
    </delete>
	
	<!-- 更新记录 -->
	<update id="update" parameterType="com.ydd.oms.entity.sys.Permission">
		UPDATE
			<include refid="tableName"></include>
        <set>
			<if test="name != null">
				name = #{name},
			</if>
			<if test="key != null">
				`key` = #{key},
			</if>
			<if test="menuId != null">
				menu_id = #{menuId},
			</if>
			<if test="createdId != null">
				created_id = #{createdId},
			</if>
			<if test="modifiedId != null">
				modified_id = #{modifiedId},
			</if>
			<if test="createdTime != null">
				created_time = #{createdTime},
			</if>
			<if test="modifiedTime != null">
				modified_time = #{modifiedTime},
			</if>
			<if test="deleted != null">
				deleted = #{deleted},
			</if>
        </set>
        WHERE id = #{id}
	</update>
	
	<!-- 通过编号查询 -->
	<select id="findById" parameterType="java.lang.Integer" resultMap="PermissionMap">
		SELECT
			<include refid="Field"></include>
		FROM
			<include refid="tableName"></include>
		WHERE id = #{id}
	</select>

	<!--根据管理员编号查询权限点键-->
	<select id="findPermissionKeyListByAdminId" parameterType="Integer" resultType="String">
		SELECT
			p.`key`
		FROM
			<include refid="tableName"></include> p
		LEFT JOIN
			sys_role_permission rp ON p.id = rp.permission_id
		WHERE
			p.deleted = 0
		AND
			rp.role_id IN (
				SELECT
					r.id
				FROM
					sys_role r
				LEFT JOIN
					sys_admin_role ar ON r.id = ar.role_id
				WHERE
				    ar.admin_id = #{adminId, jdbcType=INTEGER} AND r.deleted = 0
			)
	</select>

	<select id="countByKeyAndExcludeId" resultType="Integer">
		SELECT
			COUNT(1)
		FROM
			<include refid="tableName"></include>
		WHERE
			deleted = 0 AND `key` = #{key, jdbcType=VARCHAR}
		<if test="excludeId != null and excludeId > 0">
			AND id != #{excludeId, jdbcType=INTEGER}
		</if>

	</select>

	<!--根据管理员编号查询权限点集合-->
	<select id="findListByAdminId" resultMap="PermissionDTOMap">
		SELECT * FROM sys_permission WHERE deleted = 0 AND id IN (
			SELECT permission_id FROM sys_role_permission WHERE role_id IN (
				SELECT role_id FROM sys_admin_role where admin_id = #{adminId, jdbcType=INTEGER}
			)
		)
	</select>

</mapper>