<?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.github.tangyi.user.mapper.MenuMapper">
    <resultMap id="menuResultMap" type="com.github.tangyi.user.api.module.Menu">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="permission" property="permission"/>
        <result column="url" property="url"/>
        <result column="redirect" property="redirect"/>
        <result column="parent_id" property="parentId"/>
        <result column="icon" property="icon"/>
        <result column="sort" property="sort"/>
        <result column="type" property="type"/>
        <result column="component" property="component"/>
        <result column="path" property="path"/>
        <result column="remark" property="remark"/>
        <result column="creator" property="creator"/>
        <result column="create_date" property="createDate"/>
        <result column="modifier" property="modifier"/>
        <result column="modify_date" property="modifyDate"/>
        <result column="del_flag" property="delFlag"/>
        <result column="application_code" property="applicationCode"/>
        <result column="tenant_code" property="tenantCode"/>
    </resultMap>

    <sql id="menuColumns">
        a.id,
        a.name,
        a.permission,
        a.url,
        a.redirect,
        a.parent_id,
        a.icon,
        a.sort,
        a.type,
        a.component,
        a.path,
        a.remark,
        a.creator,
        a.create_date,
        a.modifier,
        a.modify_date,
        a.del_flag,
        a.application_code,
        a.tenant_code
    </sql>

    <!-- where 条件 -->
    <sql id="whereColumnList">
        <if test="name != null and name != ''">
            and a.name like #{name}
        </if>
        <if test="type != null and type != '' ">
            and a.type like #{type}
        </if>
        <if test="permission != null and permission != ''">
            and a.permission like #{permission}
        </if>
        <if test="tenantCode != null and tenantCode != ''">
            and a.tenant_code = #{tenantCode}
        </if>
    </sql>

    <select id="get" resultMap="menuResultMap">
        SELECT
        <include refid="menuColumns"/>
        FROM sys_menu a
        WHERE a.id = #{id} and a.del_flag = 0
    </select>

    <insert id="insert">
        INSERT  INTO sys_menu (
            id,
            name,
            permission,
            url,
            redirect,
            parent_id,
            icon,
            sort,
            type,
            component,
            path,
            remark,
            creator,
            create_date,
            modifier,
            modify_date,
            del_flag,
            application_code,
            tenant_code
        ) VALUES (
            #{id},
            #{name},
            #{permission},
            #{url},
            #{redirect},
            #{parentId},
            #{icon},
            #{sort},
            #{type},
            #{component},
            #{path},
            #{remark},
            #{creator},
            #{createDate},
            #{modifier},
            #{modifyDate},
            #{delFlag},
            #{applicationCode},
            #{tenantCode}
        )
    </insert>

    <select id="findList" resultMap="menuResultMap">
        SELECT
        <include refid="menuColumns"/>
        FROM sys_menu a
        WHERE a.del_flag = 0
        <include refid="whereColumnList"/>
    </select>

    <select id="findAllList" resultMap="menuResultMap">
        SELECT
        <include refid="menuColumns"/>
        FROM sys_menu a
        WHERE a.del_flag = 0
        <include refid="whereColumnList"/>
    </select>

    <select id="findListById" resultMap="menuResultMap">
        SELECT
        <include refid="menuColumns"/>
        FROM sys_menu a
        WHERE a.id IN
        <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="findByRole" resultMap="menuResultMap">
        SELECT
        <include refid="menuColumns"/>
        FROM
        sys_role r
        LEFT JOIN sys_role_menu rm ON rm.role_id = r.id
        LEFT JOIN sys_menu a ON a.id = rm.menu_id
        WHERE
        r.del_flag = 0
        AND a.del_flag = 0
        AND r.role_code = #{role}
        ORDER BY a.sort DESC
    </select>

    <update id="update">
        UPDATE sys_menu  SET
        <if test="name != null">
            name = #{name},
        </if>
        <if test="permission != null">
            permission = #{permission},
        </if>
        <if test="url != null">
            url = #{url},
        </if>
        <if test="redirect != null">
            redirect = #{redirect},
        </if>
        <if test="icon != null">
            icon = #{icon},
        </if>
        <if test="sort != null">
            sort = #{sort},
        </if>
        <if test="type != null">
            type = #{type},
        </if>
        <if test="component != null">
            component = #{component},
        </if>
        <if test="path != null">
            path = #{path},
        </if>
        <if test="remark != null">
            remark = #{remark},
        </if>
        <if test="delFlag != null">
            del_flag = #{delFlag},
        </if>
        modifier = #{modifier},
        modify_date = #{modifyDate}
        WHERE
        <if test="id != null">
            id = #{id}
        </if>
        <if test="parentId != null">
            and parent_id = #{parentId}
        </if>
    </update>

    <delete id="delete">
        UPDATE sys_menu SET
			del_flag = 1
		WHERE id = #{id}
    </delete>
</mapper>
