<?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.zcxk.dao.MenuMapper">
    <resultMap type="MenuEntity" id="MenuResult">
        <result property="id" column="id"/>
        <result property="appId" column="app_id"/>
        <result property="thirdMenuId" column="third_menu_id"/>
        <result property="parentMenuId" column="parent_menu_id"/>
        <result property="name" column="name"/>
        <result property="englishName" column="ENGLISH_NAME"/>
        <result property="menuImage" column="menu_image"/>
        <result property="menuDynamicImage" column="MENU_DYNAMIC_IMAGE"/>
        <result property="permissionFlag" column="permission_flag"/>
        <result property="linkPath" column="link_path"/>
        <result property="seq" column="seq"/>
        <result property="showState" column="show_state"/>
        <result property="remark" column="remark"/>
        <result property="isTenant" column="is_tenant"/>
        <result property="isAdmin" column="is_admin"/>
        <result property="dateCreate" column="date_create"/>
        <result property="createBy" column="create_by"/>
        <result property="dateUpdate" column="date_update"/>
        <result property="updateBy" column="update_by"/>
        <result property="status" column="status"/>
    </resultMap>
    <!--  实体栏位  -->
    <sql id="menuColumns">
         a.id as "id" ,
         a.app_id as "appId" ,
         a.third_menu_id as "thirdMenuId" ,
         a.parent_menu_id as "parentMenuId" ,
         a.name as "name" ,
         a.ENGLISH_NAME as "englishName",
         a.MENU_DYNAMIC_IMAGE as "menuDynamicImage",
         a.menu_image as "menuImage" ,
         a.permission_flag as "permissionFlag" ,
         a.link_path as "linkPath" ,
         a.seq as "seq" ,
         a.show_state as "showState" ,
         a.remark as "remark" ,
         a.is_tenant as "isTenant" ,
         a.is_admin as "isAdmin" ,
         a.date_create as "dateCreate" ,
         a.create_by as "createBy" ,
         a.date_update as "dateUpdate" ,
         a.update_by as "updateBy" ,
         a.status as "status" 
     </sql>

    <!--  根据主键获取实体   -->
    <select id="findMenuById" resultType="com.zcxk.entity.MenuEntity">
        SELECT
        <include refid="menuColumns"/>
        FROM uims_menu a
        WHERE a.id = #{id} and a.status=1
    </select>
    <!--  根据主键获取实体   -->
    <select id="findMenuByParentIds" resultType="com.zcxk.entity.MenuEntity">
        SELECT
        <include refid="menuColumns"/>
        FROM uims_menu a
        WHERE a.PARENT_MENU_ID in
        <foreach collection = "Ids" item = "dramaId" open = "(" close = ")" separator = "," >
            #{dramaId}
        </foreach>

        and a.status=1
    </select>

    <!--  根据主键获取租户菜单  -->
    <select id="findTenantMenuByParentIds" resultType="com.zcxk.entity.MenuEntity">
        select a.id
        from uims_tenant_menu a
        inner JOIN uims_menu b on a.menu_id=b.id
        inner join uims_menu c on b.id =c.PARENT_MENU_ID and c.status=1
        inner join uims_tenant_menu d on d.menu_id=c.id and d.status=1
        <if test="tenantId != null  and tenantId != ''">
            and d.TENANT_ID=#{tenantId}
        </if>
        WHERE a.id in
        <foreach collection = "Ids" item = "dramaId" open = "(" close = ")" separator = "," >
            #{dramaId}
        </foreach>
    </select>

    <!--  根据获取实体List   -->
    <select id="findList" resultType="com.zcxk.entity.MenuEntity">
        SELECT
        <include refid="menuColumns"/>
        FROM uims_menu a
        <where>
            a.status=1
            <if test="name != null  and name != ''">
                and a.name LIKE concat('%',#{name},'%')
            </if>
            <if test="parentMenuId != null  and parentMenuId != ''">
                and a.PARENT_MENU_ID = #{parentMenuId}
            </if>
        </where>
        order by a.seq
    </select>
    <!--  根据获取实体List   -->
    <select id="findTenantList" resultType="com.zcxk.entity.MenuEntity">
        select
        b.id as "id" ,
        b.app_id as "appId" ,
        b.third_menu_id as "thirdMenuId" ,
        b.parent_menu_id as "parentMenuId" ,
        a.id as "tenantMenuId",
        a.MENU_NAME as "name" ,
        a.ENGLISH_NAME as "englishName",
        a.MENU_DYNAMIC_IMAGE as "menuDynamicImage",
        a.menu_image as "menuImage" ,
        b.permission_flag as "permissionFlag" ,
        a.link_path as "linkPath" ,
        a.MENU_SEQ as "seq" ,
        b.show_state as "showState" ,
        a.remark as "remark" ,
        b.is_tenant as "isTenant" ,
        b.is_admin as "isAdmin" ,
        a.date_create as "dateCreate" ,
        a.create_by as "createBy" ,
        a.date_update as "dateUpdate" ,
        a.update_by as "updateBy" ,
        a.status as "status"
        from uims_tenant_menu  a
        inner join uims_menu b on a.MENU_ID=b.id and b.STATUS=1
        <where>
            a.status=1   and b.is_tenant=1
            <if test="name != null  and name != ''">
                and a.MENU_NAME LIKE concat('%',#{name},'%')
            </if>
            <if test="tenantId != null  and tenantId != ''">
                and a.tenant_id = #{tenantId}
            </if>
        </where>
        order by a.MENU_SEQ
    </select>
    <!--  根据获取实体List   -->
    <select id="findUserMenuById" resultType="com.zcxk.entity.MenuEntity">
        select
        a.id as "id" ,
        b.app_id as "appId" ,
        b.third_menu_id as "thirdMenuId" ,
        b.parent_menu_id as "parentMenuId" ,
        a.id as "tenantMenuId",
        a.MENU_NAME as "name" ,
        a.menu_image as "menuImage" ,
        a.ENGLISH_NAME as "englishName",
        a.MENU_DYNAMIC_IMAGE as "menuDynamicImage",
        b.permission_flag as "permissionFlag" ,
        a.link_path as "linkPath" ,
        a.MENU_SEQ as "seq" ,
        b.show_state as "showState" ,
        a.remark as "remark" ,
        b.is_tenant as "isTenant" ,
        b.is_admin as "isAdmin" ,
        a.date_create as "dateCreate" ,
        a.create_by as "createBy" ,
        a.date_update as "dateUpdate" ,
        a.update_by as "updateBy" ,
        a.status as "status"
        from uims_tenant_menu  a
        inner join uims_menu b on a.MENU_ID=b.id and b.STATUS=1
        <where>
            a.status=1 and b.id = #{id}
            <if test="tenantId != null  and tenantId != ''">
                and a.tenant_id = #{tenantId}
            </if>
        </where>
    </select>
    <!--  根据获取实体List   -->
    <select id="findUserList" resultType="com.zcxk.entity.MenuEntity">
        select
        b.id as "id" ,
        b.app_id as "appId" ,
        b.third_menu_id as "thirdMenuId" ,
        b.parent_menu_id as "parentMenuId" ,
        a.id as "tenantMenuId",
        a.MENU_NAME as "name" ,
        a.ENGLISH_NAME as "englishName",
        a.MENU_DYNAMIC_IMAGE as "menuDynamicImage",
        a.menu_image as "menuImage" ,
        b.permission_flag as "permissionFlag" ,
        a.link_path as "linkPath" ,
        a.MENU_SEQ as "seq" ,
        b.show_state as "showState" ,
        a.remark as "remark" ,
        b.is_tenant as "isTenant" ,
        b.is_admin as "isAdmin" ,
        a.date_create as "dateCreate" ,
        a.create_by as "createBy" ,
        a.date_update as "dateUpdate" ,
        a.update_by as "updateBy" ,
        a.status as "status"
        from uims_user_role  userrole
        inner join uims_role_menu rolemenu on rolemenu.ROLE_ID=userrole.ROLE_ID and rolemenu.status=1
        inner join uims_menu b on rolemenu.MENU_ID=b.id and b.STATUS=1
        inner join uims_tenant_menu a on a.MENU_ID= b.id and a.status=1
        <if test="tenantId != null  and tenantId != ''">
            and a.tenant_id = #{tenantId}
        </if>
        <where>
            a.status=1  and  userrole.status=1  and b.is_tenant=1  and userrole.user_id=#{userId}
            <if test="name != null  and name != ''">
                and a.MENU_NAME LIKE concat('%',#{name},'%')
            </if>
        </where>
        order by a.MENU_SEQ
    </select>

    <!--  超级管理员查询 一级菜单List   -->
    <select id="findAllFirstMenu" resultType="com.zcxk.entity.MenuEntity">
        SELECT
        a.id as "id" ,
        a.app_id as "appId" ,
        a.third_menu_id as "thirdMenuId" ,
        a.parent_menu_id as "parentMenuId" ,
        a.name as "name" ,
        a.ENGLISH_NAME as "englishName",
        a.MENU_DYNAMIC_IMAGE as "menuDynamicImage",
        a.menu_image as "menuImage" ,
        a.permission_flag as "permissionFlag" ,
        a.link_path as "linkPath" ,
        a.seq as "seq" ,
        case when a.is_admin=1 then 1 else 0 end as "isOwner",
        a.show_state as "showState" ,
        a.remark as "remark" ,
        a.is_tenant as "isTenant" ,
        a.is_admin as "isAdmin" ,
        a.date_create as "dateCreate" ,
        a.create_by as "createBy" ,
        a.date_update as "dateUpdate" ,
        a.update_by as "updateBy" ,
        a.status as "status",
        a.APP_ID as "appId"
        FROM uims_menu a
        <where>
            a.status=1 and a.PARENT_MENU_ID=0 and a.SHOW_STATE=1  and a.app_id != 1004
        </where>
        order by a.IS_ADMIN desc,a.seq
    </select>
    <!--  租户管理员查询租户菜单  -->
    <select id="findTenantByMenuId" resultType="com.zcxk.entity.MenuEntity">
        select b.id
        from uims_tenant_menu b
        <where>
            b.status=1  and b.MENU_ID= #{id}
            <if test="tenantId != null  and tenantId != ''">
                and b.tenant_id = #{tenantId}
            </if>
        </where>
        limit 1
    </select>
    <!--  租户管理员查询租户菜单  -->
    <select id="findTenantFirstMenu" resultType="com.zcxk.entity.MenuEntity">
        select
        a.id as "id" ,
        a.parent_menu_id as "parentMenuId" ,
        ifnull(b.MENU_NAME,a.`NAME`) as "name" ,
        IFNULL(b.MENU_IMAGE,a.MENU_IMAGE) as "menuImage" ,
        IFNULL(b.ENGLISH_NAME,a.ENGLISH_NAME) as "englishName",
        IFNULL(b.MENU_DYNAMIC_IMAGE,a.MENU_DYNAMIC_IMAGE) as "menuDynamicImage",
        a.permission_flag as "permissionFlag" ,
        b.LINK_PATH as "linkPath" ,
        b.MENU_SEQ as "seq" ,
        a.show_state as "showState" ,
        b.remark as "remark" ,
        b.date_create as "dateCreate" ,
        b.create_by as "createBy" ,
        b.date_update as "dateUpdate" ,
        b.update_by as "updateBy" ,
        b.status as "status",
        a.APP_ID as "appId"
        from uims_tenant_menu b
        inner join uims_menu a on a.id= b.MENU_ID and a.status=1
        <where>
            b.status=1 and a.PARENT_MENU_ID=0 and a.show_state=1 and a.is_tenant=1 and a.app_id != 1004
            <if test="tenantId != null  and tenantId != ''">
                and b.tenant_id = #{tenantId}
            </if>
        </where>
        order by b.MENU_SEQ
    </select>
    <!--  普通用户查询租户菜单  -->
    <select id="findUserFirstMenu" resultType="com.zcxk.entity.MenuEntity">
        SELECT
        a.id as "id" ,
        a.parent_menu_id as "parentMenuId" ,
        ifnull(b.MENU_NAME,a.`NAME`) as "name" ,
        IFNULL(b.MENU_IMAGE,a.MENU_IMAGE) as "menuImage" ,
        IFNULL(b.ENGLISH_NAME,a.ENGLISH_NAME) as "englishName",
        IFNULL(b.MENU_DYNAMIC_IMAGE,a.MENU_DYNAMIC_IMAGE) as "menuDynamicImage",
        a.permission_flag as "permissionFlag" ,
        b.LINK_PATH as "linkPath" ,
        b.MENU_SEQ as "seq" ,
        a.show_state as "showState" ,
        b.remark as "remark" ,
        b.date_create as "dateCreate" ,
        b.create_by as "createBy" ,
        b.date_update as "dateUpdate" ,
        b.update_by as "updateBy" ,
        b.status as "status",
        a.APP_ID as "appId"
        from uims_user_role  userrole
        inner join uims_role_menu rolemenu on rolemenu.ROLE_ID=userrole.ROLE_ID and rolemenu.status=1
        inner join uims_menu a on a.id=rolemenu.MENU_ID and a.STATUS=1 and a.app_id != 1004
        inner join uims_tenant_menu b on a.id= b.MENU_ID and b.status=1
        <if test="tenantId != null  and tenantId != ''">
            and b.tenant_id = #{tenantId}
        </if>
        <where>
            userrole.status=1 and a.PARENT_MENU_ID=0 and a.SHOW_STATE=1 and a.is_tenant=1
            <if test="userId != null  and userId != ''">
                and userrole.user_id = #{userId}
            </if>

        </where>
        order by b.MENU_SEQ
    </select>
    <!--  超级管理员获取左侧菜单   -->
    <select id="findSystemUserMenu" resultType="com.zcxk.entity.MenuEntity">
        SELECT
        <include refid="menuColumns"/>
        FROM uims_menu a
        <where>
            a.status=1 and  a.SHOW_STATE=1 and a.is_admin=1
            <if test="appId != null  and appId != ''">
                and a.app_id = #{appId}
            </if>
        </where>
        order by a.seq
    </select>
    <!--  租户管理员获取左侧菜单   -->
    <select id="findTenantUserMenu" resultType="com.zcxk.entity.MenuEntity">
        select
        a.id as "id" ,
        a.parent_menu_id as "parentMenuId" ,
        ifnull(b.MENU_NAME,a.`NAME`) as "name" ,
        IFNULL(b.MENU_IMAGE,a.MENU_IMAGE) as "menuImage" ,
        IFNULL(b.ENGLISH_NAME,a.ENGLISH_NAME) as "englishName",
        IFNULL(b.MENU_DYNAMIC_IMAGE,a.MENU_DYNAMIC_IMAGE) as "menuDynamicImage",
        a.permission_flag as "permissionFlag" ,
        b.LINK_PATH as "linkPath" ,
        b.MENU_SEQ as "seq" ,
        a.show_state as "showState" ,
        b.remark as "remark" ,
        b.date_create as "dateCreate" ,
        b.create_by as "createBy" ,
        b.date_update as "dateUpdate" ,
        b.update_by as "updateBy" ,
        b.status as "status"
        from uims_tenant_menu b
        inner join uims_menu a on a.id= b.MENU_ID and a.status=1
        <where>
            b.status=1  and a.SHOW_STATE=1  and a.is_tenant=1
            <if test="appId != null  and appId != ''">
                and a.app_id = #{appId}
            </if>
            <if test="tenantId != null  and tenantId != ''">
                and b.tenant_id = #{tenantId}
            </if>
        </where>
        order by b.MENU_SEQ
    </select>
    <!--  普通用户获取实体List   -->
    <select id="findUserMenu" resultType="com.zcxk.entity.MenuEntity">
        SELECT
        a.id as "id" ,
        a.parent_menu_id as "parentMenuId" ,
        ifnull(b.MENU_NAME,a.`NAME`) as "name" ,
        IFNULL(b.MENU_IMAGE,a.MENU_IMAGE) as "menuImage" ,
        IFNULL(b.ENGLISH_NAME,a.ENGLISH_NAME) as "englishName",
        IFNULL(b.MENU_DYNAMIC_IMAGE,a.MENU_DYNAMIC_IMAGE) as "menuDynamicImage",
        a.permission_flag as "permissionFlag" ,
        b.LINK_PATH as "linkPath" ,
        b.MENU_SEQ as "seq" ,
        a.show_state as "showState" ,
        b.remark as "remark" ,
        b.date_create as "dateCreate" ,
        b.create_by as "createBy" ,
        b.date_update as "dateUpdate" ,
        b.update_by as "updateBy" ,
        b.status as "status"
        from uims_user_role  userrole
        inner join uims_role_menu rolemenu on rolemenu.ROLE_ID=userrole.ROLE_ID and rolemenu.status=1
        inner join uims_menu a on a.id=rolemenu.MENU_ID and a.STATUS=1
        inner join uims_tenant_menu b on a.id= b.MENU_ID and b.status=1
        <if test="tenantId != null  and tenantId != ''">
            and b.tenant_id = #{tenantId}
        </if>
        <where>
            userrole.status=1  and a.SHOW_STATE=1 and a.is_tenant=1
            <if test="userId != null  and userId != ''">
                and userrole.user_id = #{userId}
            </if>
            <if test="appId != null  and appId != ''">
                and a.app_id = #{appId}
            </if>

        </where>
        order by b.MENU_SEQ
    </select>

    <!--  超级管理员获取租户菜单   -->
    <select id="findSystemTenantMenu" resultType="com.zcxk.entity.MenuEntity">
        SELECT
        <include refid="menuColumns"/>
        FROM uims_menu a
        <where>
            a.status=1  and is_tenant =1
        </where>
        order by a.seq
    </select>
    <!--  根据获取实体 page   -->
    <select id="findPage" resultType="com.zcxk.entity.MenuEntity">
        SELECT
        <include refid="menuColumns"/>
        FROM uims_menu a
        <where>
             a.status=1
            <if test="menu.name != null  and menu.name != ''">
                and a.name LIKE concat('%',#{menu.name},'%')
            </if>
        </where>
    </select>
    <update id="updateState">
         update uims_tenant_menu set status=0 where id=#{Id}
    </update>
</mapper>