<?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="net.lab1024.smartadmin.module.system.privilege.dao.PrivilegeDao">

    <resultMap id="PrivilegeEntity"
               type="net.lab1024.smartadmin.module.system.privilege.domain.entity.PrivilegeEntity"></resultMap>

    <sql id="baseSql">
        p.id,
        p.type,
        p.name,
        p.key,
        p.url,
        p.sort,
        p.parent_key,
        p.update_time,
        p.create_time
    </sql>

    <select id="selectByType" resultMap="PrivilegeEntity">
        SELECT
        <include refid="baseSql"></include>
        FROM
        t_privilege p
        WHERE
        p.type = #{type}
    </select>


    <select id="selectByExcludeType" resultMap="PrivilegeEntity">
        SELECT
        <include refid="baseSql"></include>
        FROM
        t_privilege p
        WHERE
        p.type &lt;&gt; #{type}
    </select>


    <delete id="delByKeyList">
        DELETE FROM t_privilege
        WHERE `key` IN
        <foreach collection="keyList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </delete>

    <delete id="delByParentKeyList">
        DELETE FROM t_privilege
        WHERE parent_key IN
        <foreach collection="keyList" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </delete>

    <insert id="batchInsert" useGeneratedKeys="true" keyProperty="id" parameterType="java.util.List">
        INSERT INTO t_privilege (`type`, `name`, `key`,url,sort,parent_key,create_time,update_time) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.type},
            #{item.name},
            #{item.key},
            #{item.url},
            #{item.sort},
            #{item.parentKey},
            now(),
            now()
            )
        </foreach>
    </insert>

    <update id="batchUpdate">
        <foreach collection="updateList" item="item" index="index" separator=";">
            UPDATE t_privilege
            SET `type`=#{item.type},`name`=#{item.name},url=#{item.url},sort=#{item.sort},parent_key=#{item.parentKey},update_time=now()
            WHERE `key` = #{item.key}
        </foreach>
    </update>



    <select id="selectByParentKey" resultMap="PrivilegeEntity">
        SELECT
        <include refid="baseSql"></include>
        FROM
        t_privilege p
        WHERE
        p.parent_key = #{parentKey}
        order by sort asc
    </select>


    <select id="selectByKey" resultMap="PrivilegeEntity">
        SELECT
        <include refid="baseSql"></include>
        FROM
        t_privilege p
        WHERE
        p.key = #{key}
    </select>

    <select id="selectByKeyList" resultMap="PrivilegeEntity">
        SELECT
        <include refid="baseSql"></include>
        FROM
        t_privilege p
        WHERE
        p.key in
        <foreach item="item" collection="keyList" open="(" close=")"
                 separator=",">
            #{item}
        </foreach>
    </select>

    <select id="selectAll" resultMap="PrivilegeEntity">
        SELECT
        <include refid="baseSql"></include>,
        parent.id as parent_id
        FROM
        t_privilege p
        LEFT JOIN t_privilege parent on p.parent_key = parent.`key`
    </select>

</mapper>