<?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="cn.ezhizao.project.tenant.mapper.TenantMapper">

    <resultMap type="Tenant" id="TenantResult">
        <id column="id" property="id"/>
        <result column="industry_category" property="industryCategory"/>
        <result column="industry_specific" property="industrySpecific"/>
        <result column="version_id" property="versionId"/>
        <association property="industryCategoryStr" javaType="java.lang.String" column="industry_category"  select="getIndustryCategoryStr"  />
        <association property="industrySpecificStr" javaType="java.lang.String" column="industry_specific"  select="getIndustrySpecificStr"  />
        <association property="versionName" javaType="java.lang.String" column="version_id" select="getVersionName"/>
    </resultMap>

    <resultMap id="RoleResult" type="cn.ezhizao.project.system.domain.SysRole">
        <id column="role_id" property="roleId" />
    </resultMap>

    <resultMap id="RoleInItResult" type="cn.ezhizao.project.system.domain.SysRole">
        <id column="id" property="id" />
        <result column="init_id" property="initId" />
        <collection property="menuIds" ofType="java.lang.Long" column="init_id" select="getInItMenuIds" />
    </resultMap>

    <resultMap id="SysRoleMenuInitResult" type="cn.ezhizao.project.system.domain.SysRoleMenuInit">

    </resultMap>

    <resultMap id="SourceResult" type="cn.ezhizao.project.setting.domain.BizSource">
        <id column="id" property="id" />
    </resultMap>


    <select id="getIndustryCategoryStr" resultType="java.lang.String">
        SELECT value FROM biz_dict WHERE deleted = 0 AND `key`= 'INDUSTRY_CATEGORY' AND code = #{industry_category};
    </select>

    <select id="getIndustrySpecificStr" resultType="java.lang.String">
        SELECT value FROM biz_dict WHERE deleted = 0 AND `key`= 'INDUSTRY_SPECIFIC' AND code = #{industry_specific};
    </select>

    <select id="getVersionName" resultType="java.lang.String">
        select name from sys_version where deleted = 0 and id = #{version_id}
    </select>

    <select id="getList" parameterType="Tenant" resultMap="TenantResult">
        SELECT *
        <if test="operateType != null  and operateType != ''">
            <choose>
                <when test="operateType == 'join'">
                    ,(SELECT id FROM biz_tenant_apply WHERE deleted = 0 AND tenant_id = biz_tenant.id AND  user_id = #{userId}) AS apply_id,
                     (SELECT status FROM biz_tenant_apply WHERE deleted = 0 AND tenant_id = biz_tenant.id AND  user_id = #{userId}) AS apply_status
                </when>
            </choose>
        </if>
        FROM biz_tenant
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            deleted = 0
            <if test="id != null"> AND id = #{id}</if>
            <if test="orgName != null  and orgName != ''"> AND org_name like concat('%', #{orgName}, '%')</if>
            <if test="companyName != null  and companyName != ''"> AND company_name like concat('%', #{companyName}, '%')</if>
            <if test="userName != null "> AND id IN (SELECT tenant_id FROM biz_user_tenant WHERE deleted = 0 AND user_id = ( SELECT user_id FROM sys_user WHERE del_flag = 0 AND user_name = #{userName}) )</if>
            <if test="operateType != null  and operateType != ''">
                <choose>
                    <when test="operateType == 'join'">
                    AND id NOT IN (SELECT tenant_id FROM biz_user_tenant WHERE deleted = 0  AND user_id = #{userId})
                    </when>
                    <when test="operateType == 'change'">
                    AND id IN (SELECT tenant_id FROM biz_user_tenant WHERE deleted = 0  AND user_id = #{userId})
                    </when>
                </choose>
            </if>
        </trim>
    </select>

    <select id="checkAdminRole" resultType="java.lang.Boolean">
        select count(1) > 0 from sys_role where is_init = '1' and edit_disable = '1' and sys_role.del_flag = 0 and tenant_id = #{tenantId}
    </select>

    <select id="checkAdminUser" resultType="java.lang.Boolean">
        select count(1) > 0 from sys_user_role left join sys_role on sys_role.role_id = sys_user_role.role_id
        where is_init = '1' and edit_disable = '1' and sys_role.del_flag = 0 and sys_role.tenant_id = #{tenantId};
    </select>

    <select id="checkInitDept" resultType="java.lang.Boolean">
        select count(1) > 0 from sys_dept where sys_dept.tenant_id = #{tenantId} and sys_dept.parent_id = 100 and sys_dept.del_flag = 0
    </select>
    <select id="getInitDept" resultType="cn.ezhizao.project.system.domain.SysDept">
        select * from sys_dept where sys_dept.tenant_id = #{tenantId} and sys_dept.parent_id = 100 and sys_dept.del_flag = 0
    </select>

    <select id="checkInitRoles">
        select count(1) > 0 from sys_role where del_flag = 0 and init_id > 0 and sys_role.tenant_id = #{tenantId}
    </select>

    <select id="getInitAdminUser" resultType="cn.ezhizao.project.system.domain.SysUser">
        select * from sys_user
        where del_flag = 0 and exists
        (select 1 from sys_user_role
        left join sys_role on sys_role.role_id = sys_user_role.role_id
        where sys_role.tenant_id = #{tenantId}
        and sys_user_role.user_id = sys_user.user_id
        and is_init = '1' and edit_disable = '1' and is_init_admin = 1)
    </select>

    <select id="getAdminUser" resultType="cn.ezhizao.project.system.domain.SysUser">
        select * from sys_user
        where del_flag = 0 and exists (select 1 from sys_user_role
        left join sys_role on sys_role.role_id = sys_user_role.role_id
        where sys_role.tenant_id = #{tenantId}
        and sys_user_role.user_id = sys_user.user_id
        and is_init = '1' and edit_disable = '1')
        limit 1
    </select>

    <select id="getInitRoles" resultMap="RoleResult">
        select * from sys_role
        where del_flag = 0
        and sys_role.tenant_id = #{tenantId}
        and is_init = '1'
    </select>

    <select id="getInItMenuIds" resultType="java.lang.Long">
        select menu_id from sys_role_menu_init
        WHERE init_role_id =#{roleId}
    </select>

    <select id="initRolesData" resultMap="RoleInItResult">
        select id as init_id, role_name, remark ,role_key,role_sort,menu_check_strictly from sys_role_init
        WHERE deleted = 0
    </select>

    <select id="getMenuIds" resultType="java.lang.Long">
        select sys_module_detail.menu_id from biz_tenant
        left join sys_version_menu on sys_version_menu.version_id = biz_tenant.version_id
        left join sys_module_detail on sys_module_detail.module_id = sys_version_menu.module_id
        where biz_tenant.id = #{tenantId} and sys_version_menu.deleted = 0 and sys_module_detail.deleted = 0
    </select>

<!--    <select id="getAdminMenu" resultMap="java.lang.Long">-->
<!--        select sys_role_menu-->
<!--    </select>-->
    <select id="checkAdminMenu" resultType="java.lang.Long">
        select menu_id from sys_role_menu where role_id = #{roleId}
    </select>
    <select id="checkInitSource" resultType="java.lang.Boolean">
        select count(1) > 0 from biz_source where deleted = 0 and init_id > 0 and biz_source.tenant_id = #{tenantId}
    </select>

    <select id="getInsertSource" resultMap="SourceResult">
        select id as init_id, title, referrer_data_source, remark from sys_source_init where not exists (select 1 from biz_source where deleted = 0 and init_id = sys_source_init.id and tenant_id = #{tenantId})
    </select>
</mapper>
