<?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.ningdatech.carapi.sys.mapper.CompanyMapper">

    <select id="pageVehicleNum" resultType="com.ningdatech.carapi.car.rpt.model.po.RptOtherOnlineVehiclesPO">
        select
            company_id,company_name,region_id,region_name,
            vehicleNums,onlineNums,offlineNums,enterpriseType,enterpriseTypeName,
            IFNULL(round(onlineNums/vehicleNums*100,2),0) onlineRate,
            IFNULL(round(offlineNums/vehicleNums*100,2),0) offlineRate
        from(
                select
                    c.id company_id,
                    c.name company_name,
                    r.id region_id,
                    r.name region_name,
                    c.vehicle_num vehicleNums,
                    c.online_vehicle_num onlineNums,
                    c.offline_vehicle_num offlineNums,
                    GROUP_CONCAT(distinct t.id) enterpriseType,
                    GROUP_CONCAT(distinct t.name) enterpriseTypeName
                from nd_company_info c
                         left join nd_region r on c.region_id = r.id
                         left join nd_company_type_related tr on c.id = tr.company_id
                         left join nd_company_type t on tr.company_type_id = t.id
                where c.deleted = 0
                <if test="param.regionIds != null and param.regionIds.size>0">
                    and c.region_id in
                    <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                </if>
                <if test="param.companyName != null and param.companyName != ''">
                    and c.name like concat('%',#{param.companyName},'%')
                </if>
                <if test="param.enterpriseTypes != null and param.enterpriseTypes.size>0">
                    and t.id in
                    <foreach collection="param.enterpriseTypes" item="item" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                </if>
                ${param.dataScopeSql}
                group by c.id,c.name,r.id,r.name,
                c.vehicle_num,c.online_vehicle_num,c.offline_vehicle_num) ew
        order by IFNULL(round(onlineNums/vehicleNums*100,2),0) desc,
        onlineNums desc,vehicleNums desc,company_name desc
    </select>

    <select id="countByRegion" resultType="com.ningdatech.carapi.company.entity.po.ResCompanyTotalPO">
        select
            <choose>
                <when test="param.regionLevel != null and param.regionLevel == 1">
                    (case when r2.id is not null then r2.id else r.id end) region_id,
                    (case when r2.name is not null then r2.name else r.name end) region_name,
                    (case when r2.region_code is not null then r2.region_code else r.region_code end) region_code,
                </when>
                <otherwise>
                    r.id region_id,r.name region_name,r.region_code,
                </otherwise>
            </choose>
            count(c.id) companyCount
        from nd_company_info c
        left join nd_region r on c.region_id = r.id
        left join nd_region r2 on r.parent_id = r2.id
        -- 过滤掉管理企业
        where c.company_type != 0
        <if test="param.regionIds != null and param.regionIds.size>0">
            and c.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
        group by
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                (case when r2.id is not null then r2.id else r.id end),
                (case when r2.name is not null then r2.name else r.name end),
                (case when r2.region_code is not null then r2.region_code else r.region_code end)
            </when>
            <otherwise>
                r.id,r.name,r.region_code
            </otherwise>
        </choose>
    </select>

    <select id="countEnterpriseType" resultType="com.ningdatech.carapi.company.entity.po.ResCompanyTotalPO">
        select
            t.name,
            count(t.id) count
        from nd_company_info c
            left join nd_region r on c.region_id = r.id
            left join nd_company_type_related tr on c.id = tr.company_id
            right join nd_company_type t on tr.company_type_id = t.id
                and tr.company_type_id != 1
        where 1 = 1
        <if test="param.regionIds != null and param.regionIds.size>0">
            and c.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
        group by t.id
    </select>
</mapper>
