<?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.car.rpt.mapper.RptOtherCrossDomainVehiclesMapper">
    <select id="report" resultType="com.ningdatech.carapi.car.rpt.model.po.RptOtherCrossDomainVehiclesPO">
        select
            company_id,company_name,region_id,region_name,
            vehicleNums,onlineNums,onlineRate,offlineNums,offlineRate,
            bulkCementTruckNums,readyMixedMortarTruckNums,concreteDeliveryTruckNums,mobilePumpTruckNums,
            enterpriseType,enterpriseTypeName,
            IFNULL(round(bulkCementTruckNums/vehicleNums * 100,2),0) bulkCementTruckRate,
            IFNULL(round(readyMixedMortarTruckNums/vehicleNums * 100,2),0) readyMixedMortarTruckRate,
            IFNULL(round(concreteDeliveryTruckNums/vehicleNums * 100,2),0) concreteDeliveryTruckRate,
            IFNULL(round(mobilePumpTruckNums/vehicleNums * 100,2),0) mobilePumpTruckRate
        from(select
                 c.id company_id,
                 c.name company_name,
                 r.id region_id,
                 r.name region_name,
                 IFNULL(c.vehicle_num,0) vehicleNums,
                 IFNULL(c.online_vehicle_num,0) onlineNums,
                 IFNULL(round(c.online_vehicle_num/c.vehicle_num * 100,2),0) onlineRate,
                 IFNULL(c.offline_vehicle_num,0) offlineNums,
                 IFNULL(round(c.offline_vehicle_num/c.vehicle_num * 100,2),0) offlineRate,
                 IFNULL(count(case when b.vehicle_type = 1 then 1 end),0) bulkCementTruckNums,
                 IFNULL(count(case when b.vehicle_type = 2 then 1 end),0) readyMixedMortarTruckNums,
                 IFNULL(count(case when b.vehicle_type = 3 then 1 end),0) concreteDeliveryTruckNums,
                 IFNULL(count(case when b.vehicle_type = 4 then 1 end),0) mobilePumpTruckNums,
                 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
                      left join nd_vehicle_base_info b on c.id = b.company_id
             where b.deleted = 0 and b.audit_state = 2 and b.vehicle_type in (1,2,3,4)
             <if test="param.regionIds != null and param.regionIds.size > 0">
                 and r.id in
                <foreach collection="param.regionIds" separator="," open="(" close=")" item="item">
                    #{item}
                </foreach>
             </if>
             <if test="param.companyName != null and param.companyName != ''">
                 and c.name like concat('%',#{param.companyName},'%')
             </if>
             <if test="param.enterpriseType != null">
                 and t.id = #{param.enterpriseType}
             </if>
             ${param.enterpriseType}
             group by c.id,c.name,r.id,r.name,c.vehicle_num,c.online_vehicle_num,c.offline_vehicle_num
             order by IFNULL(round(c.online_vehicle_num/c.vehicle_num * 100,2),0) desc,
                      IFNULL(round(c.offline_vehicle_num/c.vehicle_num * 100,2),0) desc) ew
    </select>

    <select id="reportPage" resultType="com.ningdatech.carapi.car.rpt.model.po.RptOtherCrossDomainVehiclesPO">
        select
        company_id,company_name,region_id,region_name,
        vehicleNums,onlineNums,onlineRate,offlineNums,offlineRate,
        bulkCementTruckNums,readyMixedMortarTruckNums,concreteDeliveryTruckNums,mobilePumpTruckNums,
        enterpriseType,enterpriseTypeName,
        IFNULL(round(bulkCementTruckNums/vehicleNums * 100,2),0) bulkCementTruckRate,
        IFNULL(round(readyMixedMortarTruckNums/vehicleNums * 100,2),0) readyMixedMortarTruckRate,
        IFNULL(round(concreteDeliveryTruckNums/vehicleNums * 100,2),0) concreteDeliveryTruckRate,
        IFNULL(round(mobilePumpTruckNums/vehicleNums * 100,2),0) mobilePumpTruckRate
        from(select
        c.id company_id,
        c.name company_name,
        r.id region_id,
        r.name region_name,
        IFNULL(c.vehicle_num,0) vehicleNums,
        IFNULL(c.online_vehicle_num,0) onlineNums,
        IFNULL(round(c.online_vehicle_num/c.vehicle_num * 100,2),0) onlineRate,
        IFNULL(c.offline_vehicle_num,0) offlineNums,
        IFNULL(round(c.offline_vehicle_num/c.vehicle_num * 100,2),0) offlineRate,
        IFNULL(count(case when b.vehicle_type = 1 then 1 end),0) bulkCementTruckNums,
        IFNULL(count(case when b.vehicle_type = 2 then 1 end),0) readyMixedMortarTruckNums,
        IFNULL(count(case when b.vehicle_type = 3 then 1 end),0) concreteDeliveryTruckNums,
        IFNULL(count(case when b.vehicle_type = 4 then 1 end),0) mobilePumpTruckNums,
        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
        left join nd_vehicle_base_info b on c.id = b.company_id
        where b.deleted = 0 and b.audit_state = 2 and b.vehicle_type in (1,2,3,4)
        <if test="param.regionIds != null and param.regionIds.size > 0">
            and r.id in
            <foreach collection="param.regionIds" separator="," open="(" close=")" item="item">
                #{item}
            </foreach>
        </if>
        <if test="param.companyName != null and param.companyName != ''">
            and c.name like concat('%',#{param.companyName},'%')
        </if>
        <if test="param.enterpriseType != null">
            and t.id = #{param.enterpriseType}
        </if>
        ${param.enterpriseType}
        group by c.id,c.name,r.id,r.name,c.vehicle_num,c.online_vehicle_num,c.offline_vehicle_num
        order by IFNULL(round(c.online_vehicle_num/c.vehicle_num * 100,2),0) desc,
        IFNULL(round(c.offline_vehicle_num/c.vehicle_num * 100,2),0) desc) ew
    </select>
</mapper>
