<?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.hotent.orgext.dao.OrgAuthExtDao">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hotent.orgext.model.OrgAuthExt">
        <id column="ID_" property="id" />
        <result column="ORG_ID_" property="orgId" />
        <result column="MENU_ALIAS_" property="menuAlias" />
        <result column="ROLE_ID_" property="roleId" />
        <result column="PC_APP_ID_" property="pcAppId" />
        <result column="MOBILE_APP_ID_" property="mobileAppId" />
        <result column="AUTH_TYPE_" property="authType" />
        <result column="CREATE_BY_" property="createBy" />
        <result column="CREATE_ORG_ID_" property="createOrgId" />
        <result column="CREATE_TIME_" property="createTime" />
        <result column="UPDATE_BY_" property="updateBy" />
        <result column="UPDATE_TIME_" property="updateTime" />
        <result column="TENANT_ID_" property="tenantId" />
    </resultMap>

    <delete id="removeByOrgIdAndRoleId" parameterType="java.lang.String">
		DELETE FROM uc_org_auth_ext
		WHERE ORG_ID_=#{orgId} and ROLE_ID_=#{roleId} and AUTH_TYPE_='1'
	</delete>

    <delete id="removeByOrgIdAndType" parameterType="java.lang.String">
		DELETE FROM uc_org_auth_ext
		WHERE ORG_ID_=#{orgId} and AUTH_TYPE_=#{authType}
	</delete>

    <delete id="removeByAppIdAndType" parameterType="java.lang.String">
        <if test="authType==2">
            DELETE FROM uc_org_auth_ext
            WHERE AUTH_TYPE_ = #{authType} and PC_APP_ID_ = #{appId}
        </if>
        <if test="authType==3">
            DELETE FROM uc_org_auth_ext
            WHERE AUTH_TYPE_ = #{authType} and MOBILE_APP_ID_ = #{appId}
        </if>
	</delete>


    <resultMap id="RoleResultMap" type="com.hotent.orgext.vo.RoleVo">
    </resultMap>

    <select id="queryRoleAuthExt" parameterType="java.lang.String" resultMap="RoleResultMap" >
        select * from (
            SELECT r.*, ext.ORG_ID_ EXT_ORG_ID_, ext.ID_ EXT_ID_
            FROM UC_ROLE r, uc_org_auth_ext ext
            where r.ID_ = ext.ROLE_ID_  and ext.AUTH_TYPE_ = '1'
            and r.IS_DELE_ = 0 and r.ENABLED_ = 1
        ) as t
        <if test="ew.customSqlSegment!=null">
            ${ew.customSqlSegment}
        </if>
    </select>

    <resultMap id="SysAppResultMap" type="com.hotent.orgext.vo.SysAppVo">
    </resultMap>

    <select id="getSysAppAuthExtPage" parameterType="java.lang.String" resultMap="SysAppResultMap" >
        select * from (
            SELECT app.*, ext.ORG_ID_ EXT_ORG_ID_, ext.ID_ EXT_ID_
            FROM portal_sys_app app, uc_org_auth_ext ext
            where app.ID_ = ext.PC_APP_ID_  and ext.AUTH_TYPE_ = '2'
            and app.IS_PUBLISH_ = 1
        ) as t
        <if test="ew.customSqlSegment!=null">
            ${ew.customSqlSegment}
        </if>
    </select>

    <select id="querySysAppExtListFirst" parameterType="java.lang.String" resultMap="SysAppResultMap" >
        select app.* FROM(
            SELECT app2.*
            FROM portal_sys_app app2
            where app2.IS_PUBLISH_ = 1
            and app2.ID_ not in (
                select ext.PC_APP_ID_ from uc_org_auth_ext ext where ext.AUTH_TYPE_ = '2' and ext.ORG_ID_ = #{extOrgId}
            )
        ) app
        <if test="ew.customSqlSegment!=null">
            ${ew.customSqlSegment}
        </if>
    </select>

    <select id="querySysAppExtListOther" parameterType="java.lang.String" resultMap="SysAppResultMap" >
        select app.* FROM(
            SELECT app2.*
            FROM portal_sys_app app2, uc_org_auth_ext ext
            where app2.ID_ = ext.PC_APP_ID_ and app2.IS_PUBLISH_ = 1 and ext.AUTH_TYPE_ = '2'
            and ext.ORG_ID_ = #{extParentOrgId}
            and app2.ID_ not in (
                select e.PC_APP_ID_ from uc_org_auth_ext e where e.AUTH_TYPE_ = '2' and e.ORG_ID_ = #{extOrgId}
            )
        ) app
        <if test="ew.customSqlSegment!=null">
            ${ew.customSqlSegment}
        </if>
    </select>

    <resultMap id="MobileAppResultMap" type="com.hotent.orgext.vo.AppModelVo">
    </resultMap>

    <select id="getMobileAppAuthExtPage" parameterType="java.lang.String" resultMap="MobileAppResultMap" >
        select * from (
            SELECT mapp.*, ext.ORG_ID_ EXT_ORG_ID_, ext.ID_ EXT_ID_
            FROM portal_app mapp, uc_org_auth_ext ext
            where mapp.ID_ = ext.MOBILE_APP_ID_  and ext.AUTH_TYPE_ = '3'
            and mapp.ENABLE_ = 1
        ) as t
        <if test="ew.customSqlSegment!=null">
            ${ew.customSqlSegment}
        </if>
    </select>

    <select id="queryMobileAppExtListFirst" parameterType="java.lang.String" resultMap="MobileAppResultMap" >
        select mapp.* FROM(
            SELECT mapp2.*
            FROM portal_app mapp2
            where mapp2.ENABLE_ = 1
            and mapp2.ID_ not in (
                select ext.MOBILE_APP_ID_ from uc_org_auth_ext ext where ext.AUTH_TYPE_ = '3' and ext.ORG_ID_ = #{extOrgId}
            )
        ) mapp
        <if test="ew.customSqlSegment!=null">
            ${ew.customSqlSegment}
        </if>
    </select>

    <select id="queryMobileAppExtListOther" parameterType="java.lang.String" resultMap="MobileAppResultMap" >
        select mapp.* FROM(
            SELECT mapp2.*
            FROM portal_app mapp2, uc_org_auth_ext ext
            where mapp2.ID_ = ext.MOBILE_APP_ID_ and mapp2.ENABLE_ = 1 and ext.AUTH_TYPE_ = '3'
            and ext.ORG_ID_ = #{extParentOrgId}
            and mapp2.ID_ not in (
                select e.MOBILE_APP_ID_ from uc_org_auth_ext e where e.AUTH_TYPE_ = '3' and e.ORG_ID_ = #{extOrgId}
            )
        ) mapp
        <if test="ew.customSqlSegment!=null">
            ${ew.customSqlSegment}
        </if>
    </select>
</mapper>
