<?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="org.dromara.business.mapper.CaseTenantOrganizationMapper">

    <select id="selectVoPageList" resultType="org.dromara.business.domain.vo.CaseTenantOrganizationVo">
        select t.id,
               t.tenant_id,
               t.organization_name,
               t.organization_type,
               t.province_code,
               t.province_name,
               t.city_code,
               t.city_name,
               t.status,
               t.organization_logo_str,
               u.tenantNumber,
               u.caseNumber
        from case_tenant_organization t
                 left join (select tenant_organization,
                                   count(tenant_id)                                                  as tenantNumber,
                                   case when sum(caseNumber) is null then 0 else sum(caseNumber) end as caseNumber
                            from (select a.tenant_id, a.tenant_organization, b.caseNumber
                                  from sys_tenant a
                                           LEFT JOIN (select tenant_id, count(1) caseNumber
                                                      from case_base_info
                                                      where del_flag = '0'
                                                      group by tenant_id) b ON a.tenant_id = b.tenant_id
                                  where a.del_flag = '0') m
                            group by tenant_organization) u on t.id = u.tenant_organization
            ${ew.getCustomSqlSegment}
    </select>
</mapper>
