<?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.ydd.oms.mapper.sys.MenuMapper">

    <resultMap id="MenuMap" type="com.ydd.oms.entity.sys.Menu">
		<id column="id" jdbcType="INTEGER" property="id" />
		<result column="status" property="status" />
		<result column="name" property="name" />
		<result column="en_name" property="enName" />
		<result column="url" property="url" />
		<result column="icon" property="icon" />
		<result column="parent_id" property="parentId" />
		<result column="sort" property="sort" />
		<result column="remark" property="remark" />
		<result column="created_id" property="createdId" />
		<result column="modified_id" property="modifiedId" />
		<result column="created_time" property="createdTime" />
		<result column="modified_time" property="modifiedTime" />
		<result column="deleted" property="deleted" />
    </resultMap>

	<resultMap id="MenuDTOMap" type="com.ydd.oms.dto.sys.MenuDTO">
		<id column="id" jdbcType="INTEGER" property="id" />
		<result column="status" property="status" />
		<result column="name" property="name" />
		<result column="en_name" property="enName" />
		<result column="url" property="url" />
		<result column="icon" property="icon" />
		<result column="parent_id" property="parentId" />
		<result column="sort" property="sort" />
		<result column="remark" property="remark" />
	</resultMap>
	
	<!--表名 -->
	<sql id="tableName">
		sys_menu
	</sql>
	
	<!-- 字段 -->
	<sql id="Field">
		id,
		status,
		name,
		en_name,
		url,
		icon,
		parent_id,
		sort,
		remark,
		created_id,
		modified_id,
		created_time,
		modified_time,
		deleted
	</sql>
	
	<!-- 字段值 -->
	<sql id="FieldValue">
		#{id},
		#{status},
		#{name},
		#{url},
		#{icon},
		#{parentId},
		#{sort},
		#{remark},
		#{createdId},
		#{modifiedId},
		#{createdTime},
		#{modifiedTime},
		#{deleted}
	</sql>

	<!-- 查询全部记录 -->
    <select id="findAll" resultMap="MenuMap">
        SELECT
        	<include refid="Field"></include>
        FROM
        	<include refid="tableName"></include>
		WHERE
			deleted = 0
		<if test="parentId != null">
			and parent_id = #{parentId}
		</if>
		ORDER BY
			sort
		DESC
    </select>

	<!--查询一级菜单-->
	<select id="findFirstLevelList" resultMap="MenuMap">
		SELECT
			id, status, name
		FROM
			<include refid="tableName"></include>
		WHERE
			deleted = 0 AND parent_id = 0
		ORDER BY sort DESC
	</select>
	
	<!-- 新增-->
	<insert id="insert" parameterType="com.ydd.oms.entity.sys.Menu" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO
			<include refid="tableName"></include>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
				id,
            </if>
            <if test="status != null">
				status,
            </if>
            <if test="name != null">
				name,
            </if>
			<if test="enName != null">
				en_name,
			</if>
            <if test="url != null">
				url,
            </if>
            <if test="icon != null">
				icon,
            </if>
            <if test="parentId != null">
				parent_id,
            </if>
            <if test="sort != null">
				sort,
            </if>
            <if test="remark != null">
				remark,
            </if>
            <if test="createdId != null">
				created_id,
            </if>
            <if test="modifiedId != null">
				modified_id,
            </if>
            <if test="createdTime != null">
				created_time,
            </if>
            <if test="modifiedTime != null">
				modified_time,
            </if>
            <if test="deleted != null">
				deleted,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
				#{id},
            </if>
            <if test="status != null">
				#{status},
            </if>
            <if test="name != null">
				#{name},
            </if>
			<if test="enName != null">
				#{enName},
			</if>
            <if test="url != null">
				#{url},
            </if>
            <if test="icon != null">
				#{icon},
            </if>
            <if test="parentId != null">
				#{parentId},
            </if>
            <if test="sort != null">
				#{sort},
            </if>
            <if test="remark != null">
				#{remark},
            </if>
            <if test="createdId != null">
				#{createdId},
            </if>
            <if test="modifiedId != null">
				#{modifiedId},
            </if>
            <if test="createdTime != null">
				#{createdTime},
            </if>
            <if test="modifiedTime != null">
				#{modifiedTime},
            </if>
            <if test="deleted != null">
				#{deleted},
            </if>
        </trim>
	</insert>
	
	<!-- 删除-->
	<delete id="delete" parameterType="java.lang.Integer">
		UPDATE
			<include refid="tableName"></include>
		SET
			deleted = 1
		WHERE
			id = #{id}
	</delete>

    <!-- 批量删除 -->
    <delete id="deleteByIds">
        UPDATE
        	<include refid="tableName"></include>
		SET
			deleted = 1
        WHERE
			id IN
			<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
				#{item}
			</foreach>
    </delete>
	
	<!-- 更新记录 -->
	<update id="update" parameterType="com.ydd.oms.entity.sys.Menu">
		UPDATE
			<include refid="tableName"></include>
        <set>
			<if test="status != null">
				status = #{status},
			</if>
			<if test="name != null">
				name = #{name},
			</if>
			<if test="enName != null">
				en_name = #{enName},
			</if>
			<if test="url != null">
				url = #{url},
			</if>
			<if test="icon != null">
				icon = #{icon},
			</if>
			<if test="parentId != null">
				parent_id = #{parentId},
			</if>
			<if test="sort != null">
				sort = #{sort},
			</if>
			<if test="remark != null">
				remark = #{remark},
			</if>
			<if test="createdId != null">
				created_id = #{createdId},
			</if>
			<if test="modifiedId != null">
				modified_id = #{modifiedId},
			</if>
			<if test="createdTime != null">
				created_time = #{createdTime},
			</if>
			<if test="modifiedTime != null">
				modified_time = #{modifiedTime},
			</if>
			<if test="deleted != null">
				deleted = #{deleted},
			</if>
        </set>
        WHERE id = #{id}
	</update>
	
	<!-- 通过编号查询 -->
	<select id="findById" parameterType="java.lang.Integer" resultMap="MenuMap">
		SELECT
			<include refid="Field"></include>
		FROM
			<include refid="tableName"></include>
		WHERE id = #{id}
	</select>

	<!--更新管理员状态-->
	<update id="updateStatus">
		UPDATE
			<include refid="tableName"></include>
		SET
			status = #{status, jdbcType=TINYINT}
		WHERE
			id
		IN
		<foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
			#{id}
		</foreach>
	</update>

	<!--根绝管理员编号查询菜单-->
	<select id="findListByAdminId" resultMap="MenuDTOMap">
		SELECT * FROM sys_menu WHERE deleted = 0 AND id IN (
			SELECT menu_id FROM sys_role_menu WHERE role_id IN (
				SELECT role_id FROM sys_admin_role WHERE admin_id = #{adminId, jdbcType=INTEGER}
			)
		) order by sort desc
	</select>

</mapper>