<?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.gic.haoban.manage.service.dao.mapper.role.TabHaobanMenuMapper">

    <resultMap type="com.gic.haoban.manage.service.entity.role.TabHaobanMenu" id="TabHaobanMenuMap">
        <result property="menuCode" column="menu_code" jdbcType="VARCHAR"/>
        <result property="menuName" column="menu_name" jdbcType="VARCHAR"/>
        <result property="menuUrl" column="menu_url" jdbcType="VARCHAR"/>
        <result property="iconUrl" column="icon_url" jdbcType="VARCHAR"/>
        <result property="menuType" column="menu_type" jdbcType="INTEGER"/>
        <result property="menuSort" column="menu_sort" jdbcType="INTEGER"/>
        <result property="parentCode" column="parent_code" jdbcType="VARCHAR"/>
        <result property="routePath" column="route_path" jdbcType="VARCHAR"/>
        <result property="ownerShip" column="owner_ship" jdbcType="VARCHAR"/>
        <result property="statusFlag" column="status_flag" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="authControl" column="auth_control"/>
        <result property="btnCodeRel" column="btn_code_rel"/>
    </resultMap>

    <sql id="Base_Column_List">
        menu_code , auth_control , btn_code_rel
        , menu_name, menu_url, icon_url, menu_type, menu_sort, parent_code, status_flag,route_path,owner_ship, create_time, update_time
    </sql>

    <!--查询单个-->
    <select id="queryById" resultMap="TabHaobanMenuMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_menu
        where menu_code = #{menuCode}
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="menuId" useGeneratedKeys="true">
        insert into tab_haoban_menu(menu_code, menu_name, menu_url, icon_url, menu_type, menu_sort, parent_code,
                                    status_flag, route_path, owner_ship, create_time, update_time)
        values (#{menuCode}, #{menuName}, #{menuUrl}, #{iconUrl}, #{menuType}, #{menuSort}, #{parentCode},
                #{statusFlag}, #{routePath}, #{ownerShip}, #{createTime}, #{updateTime})
    </insert>

    <insert id="insertBatch" keyProperty="menuId" useGeneratedKeys="true">
        insert into tab_haoban_menu(menu_code, menu_name, menu_url, icon_url, menu_type,
        menu_sort, parent_code, status_flag,route_path,owner_ship, create_time, update_time)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.menuCode}, #{entity.menuName}, #{entity.menuUrl}, #{entity.iconUrl},
            #{entity.menuType}, #{entity.menuSort}, #{entity.parentCode},
            #{entity.statusFlag},#{entity.routePath},#{entity.ownerShip},
            #{entity.createTime}, #{entity.updateTime})
        </foreach>
    </insert>


    <!--通过主键修改数据-->
    <update id="update">
        update tab_haoban_menu
        <set>
            <if test="menuName != null and menuName != ''">
                menu_name = #{menuName},
            </if>
            <if test="menuUrl != null and menuUrl != ''">
                menu_url = #{menuUrl},
            </if>
            <if test="iconUrl != null and iconUrl != ''">
                icon_url = #{iconUrl},
            </if>
            <if test="menuType != null">
                menu_type = #{menuType},
            </if>
            <if test="menuSort != null">
                menu_sort = #{menuSort},
            </if>
            <if test="parentCode != null and parentCode != ''">
                parent_code = #{parentCode},
            </if>
            <if test="statusFlag != null">
                status_flag = #{statusFlag},
            </if>
            <if test="routePath != null">
                route_path = #{routePath},
            </if>
            <if test="ownerShip != null">
                owner_ship = #{ownerShip},
            </if>
            <if test="createTime != null">
                create_time = #{createTime},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
        </set>
        where menu_code = #{menuCode}
    </update>

    <select id="getAllMenuList" resultMap="TabHaobanMenuMap" >
        select <include refid="Base_Column_List"></include>
        from tab_haoban_menu
        where status_flag = 1
    </select>


    <select id="getPidMenuList" resultMap="TabHaobanMenuMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_menu
        where status_flag = 1 and menu_code = parent_code order by menu_sort
    </select>

    <select id="getNotPidMenuList" resultMap="TabHaobanMenuMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_menu
        where status_flag = 1 and menu_code != parent_code and auth_control = 1  order by menu_sort
    </select>


    <select id="getHaobanPidMenuList" resultMap="TabHaobanMenuMap">
        select
        c.*
        from tab_haoban_role_menu a
        left join tab_haoban_role b on a.role_id = b.role_id
        left join tab_haoban_menu c on c.menu_code = a.menu_code
        where
        b.clerk_type = #{clerkType}
        and b.wx_enterprise_id = #{wxEnterpriseId}
        and c.status_flag = 1
        and c.menu_code = c.parent_code
        <if test="systemFlag != null">
            and b.system_flag = #{systemFlag}
        </if>
        order by c.menu_sort
    </select>

    <select id="getHaobanNotPidMenuList" resultMap="TabHaobanMenuMap">
        select
        c.*
        from tab_haoban_role_menu a
        left join tab_haoban_role b on a.role_id = b.role_id
        left join tab_haoban_menu c on c.menu_code = a.menu_code
        where
        b.clerk_type = #{clerkType}
        and b.wx_enterprise_id = #{wxEnterpriseId}
        and c.status_flag = 1
        and c.menu_code != c.parent_code
        <if test="systemFlag != null">
            and b.system_flag = #{systemFlag}
        </if>
        and c.auth_control = 1 
        order by c.menu_sort
    </select>


    <select id="getHaobanMenuListByClerkType" resultType="java.lang.String">
        select
        c.menu_code
        from tab_haoban_role_menu a
        left join tab_haoban_role b on a.role_id = b.role_id
        left join tab_haoban_menu c on c.menu_code = a.menu_code
        where
        b.clerk_type = #{clerkType}
        and b.wx_enterprise_id = #{wxEnterpriseId}
        and c.status_flag = 1
        <if test="systemFlag != null">
            and b.system_flag = #{systemFlag}
        </if>
        order by c.menu_sort
    </select>

    <select id="getHaobanMenuCodeListByCode" resultType="java.lang.String">
        select
        menu_code
        from tab_haoban_menu
        where status_flag = 1 and menu_code in
        <foreach collection="menuCodes" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
    </select>

    <select id="getHaobanChildMenuCodeByPCode" resultType="java.lang.String">
        select
        menu_code
        from tab_haoban_menu
        where status_flag = 1 and parent_code in
        <foreach collection="menuCodes" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        and menu_code not in
        ('customerList_allSearch','customerList_inheritCustomer','userCenter_addClerk','userCenter_delClerk','storeClerkList_edit');
    </select>

    <select id="getHaobanMenuListByCode" resultMap="TabHaobanMenuMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_menu
        where status_flag = 1 and menu_code in
        <foreach collection="menuCodes" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
    </select>


    <select id="getMenuByCode" resultMap="TabHaobanMenuMap">
        select
        <include refid="Base_Column_List"/>
        from tab_haoban_menu
        where status_flag = 1 and menu_code = #{menuCode}
    </select>

    <delete id="deleteAll">
        delete
        from tab_haoban_menu
    </delete>


    <delete id="deleteByMenuCode" parameterType="java.lang.String">
        update  tab_haoban_menu set status_flag = 0 where menu_code = #{menuCode}
    </delete>

</mapper>

