<?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.fs.company.mapper.CompanyMenuMapper">

    <resultMap type="CompanyMenu" id="CompanyMenuResult">
        <result property="menuId"    column="menu_id"    />
        <result property="menuName"    column="menu_name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="orderNum"    column="order_num"    />
        <result property="path"    column="path"    />
        <result property="component"    column="component"    />
        <result property="isFrame"    column="is_frame"    />
        <result property="isCache"    column="is_cache"    />
        <result property="menuType"    column="menu_type"    />
        <result property="visible"    column="visible"    />
        <result property="status"    column="status"    />
        <result property="perms"    column="perms"    />
        <result property="icon"    column="icon"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectCompanyMenuVo">
        select menu_id, menu_name, parent_id, order_num, path, component, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, update_by, update_time, remark from company_menu
    </sql>

    <select id="selectCompanyMenuList" parameterType="CompanyMenu" resultMap="CompanyMenuResult">
        <include refid="selectCompanyMenuVo"/>
        <where>
            <if test="menuName != null  and menuName != ''"> and menu_name like concat('%', #{menuName}, '%')</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="orderNum != null "> and order_num = #{orderNum}</if>
            <if test="path != null  and path != ''"> and path = #{path}</if>
            <if test="component != null  and component != ''"> and component = #{component}</if>
            <if test="isFrame != null "> and is_frame = #{isFrame}</if>
            <if test="isCache != null "> and is_cache = #{isCache}</if>
            <if test="menuType != null  and menuType != ''"> and menu_type = #{menuType}</if>
            <if test="visible != null  and visible != ''"> and visible = #{visible}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="perms != null  and perms != ''"> and perms = #{perms}</if>
            <if test="icon != null  and icon != ''"> and icon = #{icon}</if>
        </where>
        order by order_num asc
    </select>

    <select id="selectCompanyMenuListByCompany" parameterType="CompanyMenu" resultMap="CompanyMenuResult">
        <include refid="selectCompanyMenuVo"/>
        <where>
            menu_id != 1386
            <if test="menuName != null  and menuName != ''"> and menu_name like concat('%', #{menuName}, '%')</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="orderNum != null "> and order_num = #{orderNum}</if>
            <if test="path != null  and path != ''"> and path = #{path}</if>
            <if test="component != null  and component != ''"> and component = #{component}</if>
            <if test="isFrame != null "> and is_frame = #{isFrame}</if>
            <if test="isCache != null "> and is_cache = #{isCache}</if>
            <if test="menuType != null  and menuType != ''"> and menu_type = #{menuType}</if>
            <if test="visible != null  and visible != ''"> and visible = #{visible}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
            <if test="perms != null  and perms != ''"> and perms = #{perms}</if>
            <if test="icon != null  and icon != ''"> and icon = #{icon}</if>
        </where>
        order by order_num asc
    </select>

    <select id="selectCompanyMenuById" parameterType="Long" resultMap="CompanyMenuResult">
        <include refid="selectCompanyMenuVo"/>
        where menu_id = #{menuId}
    </select>

    <insert id="insertCompanyMenu" parameterType="CompanyMenu" useGeneratedKeys="true" keyProperty="menuId">
        insert into company_menu
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="menuName != null and menuName != ''">menu_name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="path != null">path,</if>
            <if test="component != null">component,</if>
            <if test="isFrame != null">is_frame,</if>
            <if test="isCache != null">is_cache,</if>
            <if test="menuType != null">menu_type,</if>
            <if test="visible != null">visible,</if>
            <if test="status != null">status,</if>
            <if test="perms != null">perms,</if>
            <if test="icon != null">icon,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="menuName != null and menuName != ''">#{menuName},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="path != null">#{path},</if>
            <if test="component != null">#{component},</if>
            <if test="isFrame != null">#{isFrame},</if>
            <if test="isCache != null">#{isCache},</if>
            <if test="menuType != null">#{menuType},</if>
            <if test="visible != null">#{visible},</if>
            <if test="status != null">#{status},</if>
            <if test="perms != null">#{perms},</if>
            <if test="icon != null">#{icon},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateCompanyMenu" parameterType="CompanyMenu">
        update company_menu
        <trim prefix="SET" suffixOverrides=",">
            <if test="menuName != null and menuName != ''">menu_name = #{menuName},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
            <if test="path != null">path = #{path},</if>
            <if test="component != null">component = #{component},</if>
            <if test="isFrame != null">is_frame = #{isFrame},</if>
            <if test="isCache != null">is_cache = #{isCache},</if>
            <if test="menuType != null">menu_type = #{menuType},</if>
            <if test="visible != null">visible = #{visible},</if>
            <if test="status != null">status = #{status},</if>
            <if test="perms != null">perms = #{perms},</if>
            <if test="icon != null">icon = #{icon},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where menu_id = #{menuId}
    </update>

    <delete id="deleteCompanyMenuById" parameterType="Long">
        delete from company_menu where menu_id = #{menuId}
    </delete>

    <delete id="deleteCompanyMenuByIds" parameterType="String">
        delete from company_menu where menu_id in
        <foreach item="menuId" collection="array" open="(" separator="," close=")">
            #{menuId}
        </foreach>
    </delete>


    <select id="selectMenuPermsByUserId" parameterType="Long" resultType="String">
		select distinct m.perms
		from company_menu m
			 left join company_role_menu rm on m.menu_id = rm.menu_id
			 left join company_user_role ur on rm.role_id = ur.role_id
			 left join company_role r on r.role_id = ur.role_id
		where m.status = '0' and r.status = '0' and ur.user_id = #{userId}
	</select>


    <select id="selectMenuTreeAll" resultMap="CompanyMenuResult">
		select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time
		from company_menu m where m.menu_type in ('M', 'C') and m.status = 0 and m.menu_id!=1386
		order by m.parent_id, m.order_num
	</select>

    <select id="selectMenuTreeAllByCompany" resultMap="CompanyMenuResult">
        select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time
        from company_menu m where m.menu_type in ('M', 'C') and m.status = 0
        order by m.parent_id, m.order_num
    </select>

    <select id="selectMenuTreeByUserId" parameterType="Long" resultMap="CompanyMenuResult">
		select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time
		from company_menu m
			 left join company_role_menu rm on m.menu_id = rm.menu_id
			 left join company_user_role ur on rm.role_id = ur.role_id
			 left join company_role ro on ur.role_id = ro.role_id
			 left join company_user u on ur.user_id = u.user_id
		where u.user_id = #{userId} and m.menu_type in ('M', 'C') and m.status = 0  AND ro.status = 0 and m.menu_id!=1386
		order by m.parent_id, m.order_num
	</select>

    <select id="selectMenuTreeByCompanyUserId" parameterType="Long" resultMap="CompanyMenuResult">
        select distinct m.menu_id, m.parent_id, m.menu_name, m.path, m.component, m.visible, m.status, ifnull(m.perms,'') as perms, m.is_frame, m.is_cache, m.menu_type, m.icon, m.order_num, m.create_time
        from company_menu m
                 left join company_role_menu rm on m.menu_id = rm.menu_id
                 left join company_user_role ur on rm.role_id = ur.role_id
                 left join company_role ro on ur.role_id = ro.role_id
                 left join company_user u on ur.user_id = u.user_id
        where u.user_id = #{userId} and m.menu_type in ('M', 'C') and m.status = 0  AND ro.status = 0
        order by m.parent_id, m.order_num
    </select>

    <select id="selectMenuListByRoleId" resultType="Integer">
        select m.menu_id
        from company_menu m
        left join company_role_menu rm on m.menu_id = rm.menu_id
        where rm.role_id = #{roleId}
        <if test="menuCheckStrictly">
            and m.menu_id not in (select m.parent_id from company_menu m inner join company_role_menu rm on m.menu_id = rm.menu_id and rm.role_id = #{roleId})
        </if>
        order by m.parent_id, m.order_num
    </select>

    <select id="hasChildByMenuId" resultType="Integer">
	    select count(1) from company_menu where parent_id = #{menuId}
	</select>
</mapper>
