<?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="cn.pluss.platform.mapper.MenuInfoMapper">

    <insert id="saveMenuInfo" parameterType="cn.pluss.platform.entity.MenuInfo" keyProperty="id"
            useGeneratedKeys="true">
        insert into tb_pluss_menu_info(name, menuType, link, parentId, menuOrder, iconUrl, visible, creator, version,
                                       description, seq)
        VALUES (#{name}, #{menuType}, #{link}, #{parentId}, #{menuOrder}, #{iconUrl}, #{visible}, #{creator},
                #{version}, #{description}, #{seq})
    </insert>

    <insert id="saveMenuInfoBatch">
        insert into
        tb_pluss_menu_info(name,menuType,link,parentId,menuOrder,iconUrl,visible,creator,version,description,seq) VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{name},#{menuType},#{link},#{parentId},#{menuOrder},#{iconUrl},#{visible},#{creator},#{version},#{description},#{seq}
            )
        </foreach>
    </insert>

    <select id="queryMenuInfo" parameterType="cn.pluss.platform.entity.MenuInfo"
            resultType="cn.pluss.platform.entity.MenuInfo">
        SELECT * from tb_pluss_menu_info
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="name!= null">
                and name = #{name}
            </if>
            <if test="menuType!= null">
                and menuType = #{menuType}
            </if>
            <if test="link!= null">
                and link = #{link}
            </if>
            <if test="parentId!= null">
                and parentId = #{parentId}
            </if>
            <if test="menuOrder!= null">
                and menuOrder = #{menuOrder}
            </if>
            <if test="iconUrl!= null">
                and iconUrl = #{iconUrl}
            </if>
            <if test="visible!= null">
                and visible = #{visible}
            </if>
            <if test="creator!= null">
                and creator = #{creator}
            </if>
            <if test="version!= null">
                and version = #{version}
            </if>
            <if test="description!= null">
                and description = #{description}
            </if>
            <if test="seq!= null">
                and seq = #{seq}
            </if>
        </where>
    </select>

    <select id="queryMenuInfoList" parameterType="cn.pluss.platform.entity.MenuInfo"
            resultType="cn.pluss.platform.entity.MenuInfo">
        SELECT * from tb_pluss_menu_info
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="name!= null">
                and name = #{name}
            </if>
            <if test="menuType!= null">
                and menuType = #{menuType}
            </if>
            <if test="link!= null">
                and link = #{link}
            </if>
            <if test="parentId!= null">
                and parentId = #{parentId}
            </if>
            <if test="menuOrder!= null">
                and menuOrder = #{menuOrder}
            </if>
            <if test="iconUrl!= null">
                and iconUrl = #{iconUrl}
            </if>
            <if test="visible!= null">
                and visible = #{visible}
            </if>
            <if test="creator!= null">
                and creator = #{creator}
            </if>
            <if test="version!= null">
                and version = #{version}
            </if>
            <if test="description!= null">
                and description = #{description}
            </if>
            <if test="seq!= null">
                and seq = #{seq}
            </if>
        </where>
        ORDER BY menuOrder
    </select>

    <select id="queryMenuInfoByRoleId" parameterType="java.util.Map" resultType="cn.pluss.platform.entity.MenuInfo">
        select *
        from tb_pluss_menu_info
        where id in (select menuId from tb_pluss_role_menu_info where roleId = #{roleId})
          and visible = 1
        ORDER BY id
    </select>


    <select id="queryMenuInfoListOrderById" parameterType="cn.pluss.platform.entity.MenuInfo"
            resultType="cn.pluss.platform.entity.MenuInfo">
        SELECT * from tb_pluss_menu_info
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="name!= null">
                and name = #{name}
            </if>
            <if test="menuType!= null">
                and menuType = #{menuType}
            </if>
            <if test="link!= null">
                and link = #{link}
            </if>
            <if test="parentId!= null">
                and parentId = #{parentId}
            </if>
            <if test="menuOrder!= null">
                and menuOrder = #{menuOrder}
            </if>
            <if test="iconUrl!= null">
                and iconUrl = #{iconUrl}
            </if>
            <if test="visible!= null">
                and visible = #{visible}
            </if>
            <if test="creator!= null">
                and creator = #{creator}
            </if>
            <if test="version!= null">
                and version = #{version}
            </if>
            <if test="description!= null">
                and description = #{description}
            </if>
            <if test="seq!= null">
                and seq = #{seq}
            </if>
        </where>
        order by id desc
    </select>

    <update id="updateMenuInfo" parameterType="cn.pluss.platform.entity.MenuInfo">
        update tb_pluss_menu_info
        <set>
            <if test="name!= null">
                name = #{name},
            </if>
            <if test="menuType!= null">
                menuType = #{menuType},
            </if>
            <if test="link!= null">
                link = #{link},
            </if>
            <if test="parentId!= null">
                parentId = #{parentId},
            </if>
            <if test="menuOrder!= null">
                menuOrder = #{menuOrder},
            </if>
            <if test="iconUrl!= null">
                iconUrl = #{iconUrl},
            </if>
            <if test="visible!= null">
                visible = #{visible},
            </if>
            <if test="creator!= null">
                creator = #{creator},
            </if>
            <if test="version!= null">
                version = #{version},
            </if>
            <if test="description!= null">
                description = #{description},
            </if>
            <if test="seq!= null">
                seq = #{seq}
            </if>
        </set>
        where id=#{id}
    </update>

    <delete id="deleteMenuInfo" parameterType="cn.pluss.platform.entity.MenuInfo">
        DELETE
        FROM tb_pluss_menu_info
        where id = #{id}
    </delete>

    <select id="queryMenuInfoPage" parameterType="java.util.Map" resultType="cn.pluss.platform.entity.MenuInfo">
        SELECT * from tb_pluss_menu_info
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="name!= null">
                and name = #{name}
            </if>
            <if test="menuType!= null">
                and menuType = #{menuType}
            </if>
            <if test="link!= null">
                and link = #{link}
            </if>
            <if test="parentId!= null">
                and parentId = #{parentId}
            </if>
            <if test="menuOrder!= null">
                and menuOrder = #{menuOrder}
            </if>
            <if test="iconUrl!= null">
                and iconUrl = #{iconUrl}
            </if>
            <if test="visible!= null">
                and visible = #{visible}
            </if>
            <if test="creator!= null">
                and creator = #{creator}
            </if>
            <if test="version!= null">
                and version = #{version}
            </if>
            <if test="description!= null">
                and description = #{description}
            </if>
            <if test="seq!= null">
                and seq = #{seq}
            </if>
        </where>
        order by id desc limit #{pageSize} offset #{offset}
    </select>

    <select id="queryMenuInfoPageCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT count(*) from tb_pluss_menu_info
        <where>
            <if test="id!= null">
                and id = #{id}
            </if>
            <if test="name!= null">
                and name = #{name}
            </if>
            <if test="menuType!= null">
                and menuType = #{menuType}
            </if>
            <if test="link!= null">
                and link = #{link}
            </if>
            <if test="parentId!= null">
                and parentId = #{parentId}
            </if>
            <if test="menuOrder!= null">
                and menuOrder = #{menuOrder}
            </if>
            <if test="iconUrl!= null">
                and iconUrl = #{iconUrl}
            </if>
            <if test="visible!= null">
                and visible = #{visible}
            </if>
            <if test="creator!= null">
                and creator = #{creator}
            </if>
            <if test="version!= null">
                and version = #{version}
            </if>
            <if test="description!= null">
                and description = #{description}
            </if>
            <if test="seq!= null">
                and seq = #{seq}
            </if>
        </where>
    </select>


    <select id="getPermissionNames" parameterType="java.lang.String" resultType="cn.pluss.platform.entity.MenuInfo">
        SELECT menu.* FROM tb_pluss_menu_info menu
           LEFT JOIN tb_pluss_role_menu_info rmi ON menu.id = rmi.menuId
           LEFT JOIN tb_pluss_role_user_info rui ON rui.roleId = rmi.roleId
           LEFT JOIN tb_pluss_user_info ui ON ui.id = rui.userId
        WHERE ui.loginName = 'admin' AND menu.visible = '1'
        ORDER BY menu.id
    </select>


    <select id="queryPermitMenuInfoById" parameterType="java.util.Map" resultType="cn.pluss.platform.entity.MenuInfo">
        select * from tb_pluss_menu_info mi
        where mi.id in
        (select distinct rmi.menuId from tb_pluss_role_user_info rui,tb_pluss_role_menu_info rmi where
        rui.roleId=rmi.roleId and rui.userId=#{id})
        and visible=1
        <if test="parentId !=null">
            and parentId=#{parentId}
        </if>
        <if test="parentId ==null">
            and parentId is null
        </if>
        ORDER BY id
    </select>

    <select id="queryRoleMenuInfoList" resultType="cn.pluss.platform.entity.MenuInfo">
        SELECT mi.*, IF(t.id IS NULL, 0, 1) checked FROM tb_pluss_menu_info mi LEFT JOIN
        (SELECT rmi.id, rmi.menuId FROM tb_pluss_menu_info mi LEFT JOIN
        tb_pluss_role_menu_info rmi ON mi.id = rmi.menuId
        WHERE rmi.roleId = #{roleId}) t ON  mi.id = t.menuId
        <where>
            <if test="entity.id!= null">
                and mi.id = #{id}
            </if>
            <if test="entity.name!= null">
                and mi.`name` = #{name}
            </if>
            <if test="entity.menuType!= null">
                and mi.menuType = #{menuType}
            </if>
            <if test="entity.link!= null">
                and mi.link = #{link}
            </if>
            <if test="entity.parentId!= null">
                and mi.parentId = #{parentId}
            </if>
            <if test="entity.menuOrder!= null">
                and mi.menuOrder = #{menuOrder}
            </if>
            <if test="entity.iconUrl!= null">
                and mi.iconUrl = #{iconUrl}
            </if>
            <if test="entity.visible!= null">
                and mi.visible = #{visible}
            </if>
            <if test="entity.creator!= null">
                and mi.creator = #{creator}
            </if>
            <if test="entity.version!= null">
                and mi.version = #{version}
            </if>
            <if test="entity.description!= null">
                and mi.description = #{description}
            </if>
            <if test="entity.seq!= null">
                and mi.seq = #{seq}
            </if>
        </where>
    </select>
</mapper>
