<?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.ozs.rpm.mapper.MenuMapper">
	<select id="listMenuByName" resultType="com.ozs.rpm.dto.MenuDTO">
		SELECT
		id,
		menu_code,
		menu_name,
		menu_level,
		page_url,
		if_leaf_node,
		report_id,
		update_time,
		update_user_id,
		if(menu_level =
		1,0,substr(menu_code,1,length(menu_code)-4)) pCode
		,
		(select count(0)
		from rpm_rel_report_menu where menu_id = rirm.id)
		reportCount
		FROM
		rpm_info_report_menu rirm
		where
		1=1
		<if test="menuName != null &amp;&amp; menuName != ''">
			and menu_name like concat('%',#{menuName},'%')
		</if>
		order by create_time
	</select>
	<select id="getMenuByCode" resultType="com.ozs.rpm.dto.MenuDTO">
		select
		rirm.id, menu_code,
		menu_name,
		menu_level, page_url, if_leaf_node,
		report_id,
		rirm.update_time,
		rirm.update_user_id,diu.nickname updateUserName,
		if(menu_level =
		1,0,substr(menu_code,1,length(menu_code)-4)) pCode
		from
		rpm_info_report_menu rirm left join dc_info_user diu on
		rirm.update_user_id = diu.id
		where
		menu_code=#{menuCode}
	</select>
	<select id="listLeafMenu" resultType="com.ozs.rpm.dto.MenuDTO">
		select
		id, menu_code,
		menu_name
		rpm_info_report_menu
		where
		if_leaf_node=1
	</select>
	<delete id="removeMenuByCode">
		delete from
		rpm_info_report_menu
		where
		menu_code like
		concat(#{menuCode},'%')
	</delete>
	<insert id="saveMenu" parameterType="com.ozs.rpm.dto.MenuDTO">
		insert into
		rpm_info_report_menu
		(
		id,menu_code,menu_name,
		menu_level,page_url,if_leaf_node,
		report_id,update_time,create_time,update_user_id
		)
		values
		(
		UUID(),#{menuCode},#{menuName},
		#{menuLevel},#{pageUrl},#{ifLeafNode},
		#{reportId},now(),now(),#{updateUserId}
		)
	</insert>
	<update id="updateMenu" parameterType="com.ozs.rpm.dto.MenuDTO">
		update
		rpm_info_report_menu
		set
		menu_name=#{menuName},update_time =
		now(),update_user_id = #{updateUserId}
		where
		menu_code=#{menuCode}
	</update>

	<update id="updateMenuIfLeafNode" parameterType="com.ozs.rpm.dto.MenuDTO">
		update
		rpm_info_report_menu
		set
		if_leaf_node=#{ifLeafNode}
		where
		menu_code=#{menuCode}
	</update>
	<select id="getIfLeafNodeByCode" resultType="java.lang.Integer">
		SELECT
		if_leaf_node
		FROM
		rpm_info_report_menu
		where
		menu_code=#{menuCode}
	</select>
	<select id="getMenuByName" resultType="com.ozs.rpm.dto.MenuDTO">
		SELECT
		id,
		menu_code,
		menu_name,
		menu_level,
		page_url,
		if_leaf_node,
		report_id,
		update_time,
		update_user_id,
		if(menu_level =
		1,0,substr(menu_code,1,length(menu_code)-4)) pCode
		FROM
		rpm_info_report_menu
		where
		menu_name=#{menuName}
	</select>

	<select id="countBroMenuByCode" resultType="java.lang.Integer">
		SELECT
		count(0) FROM
		rpm_info_report_menu
		where
		menu_code like
		concat(substr(#{menuCode},1,length(#{menuCode})-4),'%')
		and menu_code
		!=substr(#{menuCode},1,length(#{menuCode})-4)
		and menu_code
		!=#{menuCode}
	</select>
	<select id="countMenuReportNotNull" resultType="java.lang.Integer">
		SELECT
		count(rrrm.report_id)
		FROM
		rpm_info_report_menu rirm left join
		rpm_rel_report_menu rrrm on rirm.id = rrrm.menu_id
		where
		menu_code like
		concat(#{menuCode},'%')
	</select>
	<select id="listMenuTreeOnlyLeaf" resultType="com.ozs.rpm.dto.MenuDTO">
		SELECT
		id,
		menu_code,
		menu_name,
		menu_level,
		page_url,
		if_leaf_node,
		report_id,
		update_time,
		update_user_id,
		if(menu_level =
		1,0,substr(menu_code,1,length(menu_code)-4)) pCode
		FROM
		rpm_info_report_menu
		where
		1=1
		<if test="menuName != null &amp;&amp; menuName != ''">
			and menu_name like concat('%',#{menuName},'%')
		</if>
		<if test="reportId != null &amp;&amp; reportId != ''">
			or report_id = #{reportId}
		</if>
		order by create_time
	</select>
	<delete id="removeRpmInfoReportMenuByMenuId">
		delete from rpm_rel_report_menu where menu_id = #{id}
	</delete>
	<update id="updateMenusCode" parameterType="java.lang.String">
		update rpm_info_report_menu rirm
		inner join (
		select id,replace(menu_code,#{menuCode},#{newMenuCode})
		menu_code from
		rpm_info_report_menu where menu_code like
		concat(#{menuCode},'%')
		) temp on rirm.id = temp.id set rirm.menu_code
		= temp.menu_code
		,rirm.menu_level = length(temp.menu_code)/4
	</update>
</mapper>