<?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.trips.mapper.VehicleTripsCityMapper">

    <select id="pageCity" resultType="com.ningdatech.carapi.car.trips.entity.VehicleTripsCity">
        select city_name regionName,sum(vehicleTripsNums) vehicleTripsNums,count(car_plate) vehicleNums from (
            select city_name,count(id) vehicleTripsNums,car_plate from (
                select
                    t.id,t.happen_date,t.vehicle_id,t.company_id,t.region_id,t.region_name,t.car_plate,t.city_name
                from nd_vehicle_trips t
                where 1 = 1
                <if test="regionIds != null and regionIds.size > 0">
                    and t.region_id in
                    <foreach collection="regionIds" item="item" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
                <if test="startTime != null and startTime != ''">
                    and t.happen_date &gt;= #{startTime}
                </if>
                <if test="endTime != null and endTime != ''">
                    and t.happen_date &lt;= #{endTime}
                </if>
                ) trips
        group by city_name,car_plate) toll
        group by city_name
    </select>

    <select id="pageCityGetOn" resultType="com.ningdatech.carapi.car.trips.entity.VehicleTripsCity">
        select
            city_name regionName,sum(vehicleTripsNums) vehicleTripsNums,count(car_plate) vehicleNums
        from(select
                 count(0) vehicleTripsNums,region_name city_name,car_plate
             from(select
                      r2.id region_id,
                    <choose>
                        <when test="param.regionLevel != null and param.regionLevel == 1">
                            (case when r2.name is not null then r2.name else g.region_name end) region_name,
                        </when>
                        <when test="param.regionLevel != null and param.regionLevel != 1">
                            r.name region_name,
                        </when>
                    </choose>
                      g.id,g.car_plate
                  from nd_vehicle_get_on g
                           left join nd_vehicle_base_info b on g.vehicle_id = b.id
                           left join nd_region r on g.region_id = r.id
                           left join nd_region r2 on r.parent_id = r2.id
                           left join nd_company_info c on g.company_id = c.id
                    where b.deleted = 0 and b.audit_state = 2
                      <if test="param.vehicleTypes != null and param.vehicleTypes.size > 0">
                          and b.vehicle_type in
                          <foreach collection="param.vehicleTypes" item="item" open="(" separator="," close=")">
                              #{item}
                          </foreach>
                      </if>
                     <if test="param.regionIds != null and param.regionIds.size > 0">
                        and r.id in
                        <foreach collection="param.regionIds" item="item" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </if>
                    <if test="param.startTime != null and param.startTime != ''">
                        and g.create_on &gt;= #{param.startTime}
                    </if>
                    <if test="param.endTime != null and param.endTime != ''">
                        and g.create_on &lt;= #{param.endTime}
                    </if>
                  ${param.dataScopeSql}) trips
             group by region_name,car_plate) tripsOut
        group by city_name
    </select>

    <select id="pageCityGetOnCompany" resultType="com.ningdatech.carapi.car.trips.entity.VehicleTripsCompany">
        select
                company_id,company_name,sum(vehicleTripsNums) vehicleTripsNums,count(car_plate) vehicleNums
            from(select
            count(0) vehicleTripsNums,company_id,company_name,car_plate
            from(select
            c.id company_id,
            c.name company_name,
            g.id,g.car_plate
            from nd_vehicle_get_on g
            left join nd_vehicle_base_info b on g.vehicle_id = b.id
            left join nd_region r on g.region_id = r.id
            left join nd_company_info c on g.company_id = c.id
            where b.deleted = 0 and b.audit_state = 2
            <if test="param.vehicleTypes != null and param.vehicleTypes.size > 0">
                and b.vehicle_type in
                <foreach collection="param.vehicleTypes" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="param.regionIds != null and param.regionIds.size > 0">
                and r.id in
                <foreach collection="param.regionIds" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="param.startTime != null and param.startTime != ''">
                and g.create_on &gt;= #{param.startTime}
            </if>
            <if test="param.endTime != null and param.endTime != ''">
                and g.create_on &lt;= #{param.endTime}
            </if>
            ${param.dataScopeSql}) trips
            group by company_id,company_name,car_plate) tripsOut
        group by company_id,company_name
    </select>
</mapper>
