<?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.ytpm.middle.dao.PermissionMapper">
    <insert id="insertOne">
        insert into yt_middle_permission
        (
         permission_name,
         permission_code,
         type,
         parent_id,
         target,
         icon,
         href,
         route,
         sort,
         create_time,
         create_user_id,
         available
        )
        values
        (
         #{permissionName},
         #{permissionCode},
         #{type},
         #{parentId},
         #{target},
         #{icon},
         #{href},
         #{route},
         #{sort},
         #{createTime},
         #{createUserId},
         #{available}
        )
    </insert>
    <insert id="addRolePermissions">
        insert into yt_middle_role_permission (role_id, permission_id, create_time, create_user_id)
        values
        <foreach collection="permissionIds.split(',')" separator="," item="item">
            (#{roleId},#{item}, now(), #{operatorId})
        </foreach>
    </insert>
    <update id="updateById">
        update yt_middle_permission
        <set>
            <if test="permissionName != null">
                permission_name = #{permissionName},
            </if>
            <if test="permissionCode != null">
                permission_code = #{permissionCode},
            </if>
            <if test="type != null">
                `type` = #{type},
            </if>
            <if test="parentId != null">
                parent_id = #{parentId},
            </if>
            <if test="target != null">
                target = #{target},
            </if>
            <if test="icon != null">
                icon = #{icon},
            </if>
            <if test="href != null">
                href = #{href},
            </if>
            <if test="route != null">
                route = #{route},
            </if>
            <if test="sort != null">
                sort = #{sort},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="updateUserId != null">
                update_user_id = #{updateUserId},
            </if>
            <if test="available != null">
                available = #{available}
            </if>
        </set>
        where permission_id = #{permissionId}
    </update>
    <delete id="delByParentId">
        update yt_middle_permission set available = 0, update_time = now(),update_user_id = #{userId} where parent_id = #{userId}
    </delete>
    <delete id="delByRoleId">
        delete from yt_middle_role_permission where role_id = #{roleId}
    </delete>

    <select id="getFirstMenu" resultType="com.ytpm.middle.view.MiddlePermissionVO">
        select
            permission_id,
            permission_name,
            permission_code,
            type,
            parent_id,
            target,
            icon,
            href,
            route,
            sort,
            create_time,
            create_user_id,
            update_time,
            update_user_id,
            available
        from yt_middle_permission
        where parent_id is null
    </select>
    <select id="queryAll" resultType="com.ytpm.middle.view.MiddlePermissionVO">
        select
            permission_id,
            permission_name,
            permission_code,
            type,
            parent_id,
            target,
            icon,
            href,
            route,
            sort,
            create_time,
            create_user_id,
            update_time,
            update_user_id,
            available
        from yt_middle_permission
        <where>
            <if test="permissionName != null and permissionName!= ''">
                and permission_name like concat('%',#{permissionName},'%')
            </if>
            <if test="route != null and route!= ''">
                and route like concat('%',#{route},'%')
            </if>
            <if test="available != null">
                and available  = #{available}
            </if>
        </where>
        order by sort desc
    </select>
    <select id="getByRoute" resultType="com.ytpm.middle.model.YtMiddlePermission">
        select
            permission_id, permission_name, permission_code, type, parent_id, target, icon, href, route, sort, create_time, create_user_id, update_time, update_user_id, available
        from yt_middle_permission
        where route = #{route}
    </select>
    <select id="getByCode" resultType="com.ytpm.middle.model.YtMiddlePermission">
        select
            permission_id, permission_name, permission_code, type, parent_id, target, icon, href, route, sort, create_time, create_user_id, update_time, update_user_id, available
        from yt_middle_permission
        where permission_code = #{permissionCode}
    </select>
    <select id="selectPrimary" resultType="com.ytpm.middle.model.YtMiddlePermission">
        select
            permission_id, permission_name, permission_code, type, parent_id, target, icon, href, route, sort, create_time, create_user_id, update_time, update_user_id, available
        from yt_middle_permission
        where permission_id = #{permissionId}
    </select>
    <select id="permissionDropList" resultType="com.ytpm.middle.view.DropDownVO">
        select
            permission_id optionVal, permission_name optionName
        from yt_middle_permission
    </select>
    <select id="selectByType" resultType="com.ytpm.middle.view.MiddlePermissionVO">
        select
            permission_id, permission_name, permission_code, type, parent_id, target, icon, href, route, sort, create_time, create_user_id, update_time, update_user_id, available
        from yt_middle_permission
        where available = 1 and type = #{type}
    </select>
    <select id="getFirstMenuByRoleIds" resultType="com.ytpm.middle.view.MiddlePermissionVO">
        select DISTINCT
            mp.permission_id,
            mp.permission_name,
            mp.permission_code,
            mp.type,
            mp.parent_id,
            mp.target,
            mp.icon,
            mp.href,
            mp.route,
            mp.sort,
            mp.create_time,
            mp.create_user_id,
            mp.update_time,
            mp.update_user_id,
            mp.available
        from yt_middle_role_permission mrp
        join yt_middle_permission mp on mrp.permission_id = mp.permission_id and mp.available = 1
        where mrp.role_id in
        <foreach collection="roleIdList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </select>
    <select id="getByParentId" resultType="com.ytpm.middle.view.MiddlePermissionVO">
        select
            mp.permission_id,
            mp.permission_name,
            mp.permission_code,
            mp.type,
            mp.parent_id,
            mp.target,
            mp.icon,
            mp.href,
            mp.route,
            mp.sort,
            mp.create_time,
            mp.create_user_id,
            mp.update_time,
            mp.update_user_id,
            mp.available
        from yt_middle_role_permission mrp
         left join yt_middle_permission mp on mrp.permission_id = mp.permission_id and mp.parent_id = #{permissionId} and mp.available = 1
        <where>
            <if test="roleIds != null and roleIds != ''">
                and role_id in
                <foreach collection="roleIds.split(',')" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="queryAllByRole" resultType="com.ytpm.middle.view.MiddlePermissionVO">
        select
            mp.permission_id,
            mp.permission_name,
            mp.permission_code,
            mp.type,
            mp.parent_id,
            mp.target,
            mp.icon,
            mp.href,
            mp.route,
            mp.sort,
            mp.create_time,
            mp.create_user_id,
            mp.update_time,
            mp.update_user_id,
            mp.available
        from yt_middle_role_permission rp
        join yt_middle_permission mp on rp.permission_id = mp.permission_id
        where rp.role_id = #{roleId}
    </select>
    <select id="queryAllByRoleIds" resultType="com.ytpm.middle.view.MiddlePermissionVO">
        select
            mp.permission_id,
            mp.permission_name,
            mp.permission_code,
            mp.type,
            mp.parent_id,
            mp.target,
            mp.icon,
            mp.href,
            mp.route,
            mp.sort,
            mp.create_time,
            mp.create_user_id,
            mp.update_time,
            mp.update_user_id,
            mp.available
        from yt_middle_role_permission rp
        join yt_middle_permission mp on rp.permission_id = mp.permission_id
        where rp.role_id in
        <foreach collection="roleIdList" separator="," item="item" open="(" close=")">
            #{item}
        </foreach>
    </select>
</mapper>