<?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.datascreen.mapper.VehicleStatisticsMapper">

    <!-- 映射结果 -->
    <resultMap id="TypesResultMap" type="com.ningdatech.carapi.car.datascreen.model.po.VehicleTypeStatisticsPO">
        <result column="vehicle_types_id" jdbcType="INTEGER" property="vehicleTypesId"/>
        <result column="vehicle_num" jdbcType="BIGINT" property="vehicleNum"/>
    </resultMap>

    <!-- 映射结果 -->
    <resultMap id="RegionsResultMap" type="com.ningdatech.carapi.car.datascreen.model.po.RegionDistributionStatisticsPO">
        <result column="region_id" jdbcType="BIGINT" property="regionId"/>
        <result column="vehicle_num" jdbcType="BIGINT" property="vehicleNum"/>
        <result column="region_name" jdbcType="VARCHAR" property="regionName"/>
    </resultMap>

    <!-- 映射结果 -->
    <resultMap id="InsuresResultMap" type="com.ningdatech.carapi.car.datascreen.model.po.InsureDistributionStatisticsPO">
        <result column="insure_company" jdbcType="VARCHAR" property="insureCompany"/>
        <result column="vehicle_num" jdbcType="BIGINT" property="vehicleNum"/>
    </resultMap>

    <!-- 映射结果 -->
    <resultMap id="OperatorResultMap" type="com.ningdatech.carapi.car.datascreen.model.po.OperatorDistributionStatisticsPO">
        <result column="operator_name" jdbcType="VARCHAR" property="operatorName"/>
        <result column="vehicle_num" jdbcType="BIGINT" property="vehicleNum"/>
    </resultMap>

    <!-- 映射结果 -->
    <resultMap id="YearResultMap" type="com.ningdatech.carapi.car.datascreen.model.po.YearVehicleStatisticsPO">
        <result column="year" jdbcType="BIGINT" property="year"/>
        <result column="vehicle_num" jdbcType="BIGINT" property="vehicleNum"/>
    </resultMap>

    <!-- 映射结果 -->
    <resultMap id="UsefulLifeResultMap" type="com.ningdatech.carapi.car.datascreen.model.po.VehicleUsefulLifeStatisticsPO">
        <result column="useful_life" jdbcType="VARCHAR" property="usefulLife"/>
        <result column="vehicle_num" jdbcType="BIGINT" property="vehicleNum"/>
    </resultMap>

    <resultMap id="overspeedResultMap" type="com.ningdatech.carapi.car.datascreen.model.po.OverspeedStatisticsPO">
        <result column="region_name" jdbcType="VARCHAR" property="regionName"/>
        <result column="times" jdbcType="BIGINT" property="times"/>
    </resultMap>

    <select id="getTypesStatistics" resultType="java.util.List" resultMap="TypesResultMap" >
        SELECT
            count(vi.id) vehicle_num,vi.vehicle_type vehicle_types_id
        FROM nd_vehicle_base_info vi
        left join nd_company_info c on vi.company_id = c.id
        left join nd_region r on c.region_id = r.id
        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 vi.vehicle_type
    </select>

    <select id="getZjRegionsStatistics" resultType="java.util.List" resultMap="RegionsResultMap" >
        select
            <choose>
                <when test="param.regionLevel != null and param.regionLevel == 1">
                    (case when r2.name is not null then r2.name else r.name end) region_name,
                    (case when r2.id is not null then r2.id else r.id end) region_id,
                </when>
                <otherwise>
                    r.name region_name,
                </otherwise>
            </choose>
            count(vb.id) vehicle_num
        from nd_vehicle_base_info vb
        left join nd_company_info c on vb.company_id = c.id
        left join nd_region r on c.region_id = r.id
        left join nd_region r2 on r.parent_id = r2.id
        where
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                (case when r2.id is not null then r2.level = 2 else r.level = 2 end)
            </when>
            <otherwise>
                r.level = 3
            </otherwise>
        </choose>
        <if test="param.regionIds != null and param.regionIds.size > 0">
            and vb.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.name is not null then r2.name else r.name end),
                (case when r2.id is not null then r2.id else r.id end)
            </when>
            <otherwise>
                r.name,r.id
            </otherwise>
        </choose>
    </select>

    <select id="getInsureDistribution" resultType="java.util.List"
            resultMap="InsuresResultMap" >
        SELECT
        count(od.id) vehicle_num
        ,ii.insure_company
        sum(case when od.accident_time BETWEEN date_format(concat(year(now())-2,'-',01,'-',01),'%Y-%m-%d') and date_format(concat(year(now())-1,'-',01,'-',01),'%Y-%m-%d') then 1 else 0 end) before_out_danger,
        sum(case when od.accident_time BETWEEN date_format(concat(year(now())-1,'-',01,'-',01),'%Y-%m-%d') and date_format(concat(year(now()),'-',01,'-',01),'%Y-%m-%d') then 1 else 0 end) last_out_danger,
        sum(case when od.accident_time BETWEEN date_format(concat(year(now()),'-',01,'-',01),'%Y-%m-%d') and date_format(concat(year(now())+1,'-',01,'-',01),'%Y-%m-%d') then 1 else 0 end) current_out_danger,
        sum(case when od.accident_time BETWEEN date_format(concat(year(now())-2,'-',01,'-',01),'%Y-%m-%d') and date_format(concat(year(now())+1,'-',01,'-',01),'%Y-%m-%d') then 1 else 0 end) total
        FROM nd_insure_out_danger as od
        left join nd_insure_info as ii on od.car_plate = ii.car_plate
        left join nd_company_info as c on ii.company_id = c.id
        left join nd_region as r on c.region_id = r.id
        where 1 = 1
        <if test="param.regionIds != null and param.regionIds.size>0">
            and ii.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
        group by ii.insure_company
    </select>

    <select id="getOperatorDistribution"  resultType="java.util.List"
            resultMap="OperatorResultMap" >
        SELECT
        count( vb.id ) vehicle_num,
        oi.NAME operator_name
        FROM
        nd_operator_info oi
        LEFT JOIN nd_vehicle_base_info vb ON oi.id = vb.operator_id
        LEFT JOIN nd_region r ON vb.region_id = r.id
        where
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                (case when r2.id is not null then r2.level = 2 else r.level = 2 end)
            </when>
            <otherwise>
                r.level = 3
            </otherwise>
        </choose>
        <if test="param.regionIds != null and param.regionIds.size>0">
            and vb.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
        group by oi.name
    </select>

    <select id="getNewVehicleDistribution" resultType="java.util.List"
            resultMap="YearResultMap" >
        select
            count(vb.id) vehicle_num,year(vb.audit_time) year
        from nd_vehicle_base_info vb
        left join nd_company_info c on vb.company_id = c.id
        left join nd_region r on c.region_id = r.id
        where vb.audit_state = 2
        <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 year(vb.audit_time)
    </select>

    <select id="getVehicleNums" resultType="java.lang.Long">
        select
          count(vb.id) vehicle_num
        from nd_vehicle_base_info vb
        left join nd_company_info c on vb.company_id = c.id
        left join nd_region r on c.region_id = r.id
        where vb.deleted = 0 and vb.audit_state = 2
        <if test="param.companyIds != null and param.companyIds.size>0">
            and c.id in
            <foreach collection="param.companyIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="param.regionIds != null and param.regionIds.size>0">
            and vb.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
    </select>

    <select id="getVehicleNumsGroupByRegion" resultType="com.ningdatech.carapi.common.model.KeyValueDTO">
        select
          count(vb.id) `value`,
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                (case when r2.id is not null then r2.id else r.id end) `key`
            </when>
            <otherwise>
                r.id `key`
            </otherwise>
        </choose>
        from nd_vehicle_base_info vb
        left join nd_company_info c on vb.company_id = c.id
        left join nd_region r on c.region_id = r.id
        left join nd_region r2 on r.parent_id = r2.id
        where vb.deleted = 0 and vb.audit_state = 2
        <if test="param.companyIds != null and param.companyIds.size>0">
            and c.id in
            <foreach collection="param.companyIds" 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)
            </when>
            <otherwise>
                r.id
            </otherwise>
        </choose>
    </select>

    <select id="getOverspeedRegions" resultType="com.ningdatech.carapi.common.model.po.MapDataPO">
        SELECT
            <choose>
                <when test="param.regionLevel != null and param.regionLevel == 1">
                    (case when r2.name is not null then r2.name else r.name end) region_name,
                    (case when r2.id is not null then r2.id else r.id end) region_id,
                    (case when r2.region_code is not null then r2.region_code else r.region_code end) region_code,
                </when>
                <otherwise>
                    r.name region_name,
                    r.id region_id,
                    r.region_code,
                </otherwise>
            </choose>
            ifnull(count(o.id),0) value
        FROM nd_vehicle_overspeed_info o
        left join nd_region r on o.region_id = r.id
        left join nd_region r2 on r.parent_id = r2.id
        left join nd_company_info c on o.company_id = c.id
        where 1 = 1
        <if test="param.startTime != null">
            and o.overspeed_date &gt;= #{param.startTime}
        </if>
        <if test="param.endTime != null">
            and o.overspeed_date &lt;= #{param.endTime}
        </if>
        <if test="param.regionIds != null and param.regionIds.size>0">
            and o.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.name is not null then r2.name else r.name end),
                (case when r2.id is not null then r2.id else r.id end),
                (case when r2.region_code is not null then r2.region_code else r.region_code end)
            </when>
            <otherwise>
                r.name,
                r.id,
                r.region_code
            </otherwise>
        </choose>
    </select>

    <select id="getViolationRegions" resultType="com.ningdatech.carapi.common.model.po.MapDataPO">
        SELECT
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                (case when r2.name is not null then r2.name else r.name end) region_name,
                (case when r2.id is not null then r2.id else r.id end) region_id,
                (case when r2.region_code is not null then r2.region_code else r.region_code end) region_code,
            </when>
            <otherwise>
                r.name region_name,
                r.id region_id,
                r.region_code,
            </otherwise>
        </choose>
        count(o.id) value
        FROM nd_vehicle_violation o
        left join nd_region r on o.region_id = r.id
        left join nd_region r2 on r.parent_id = r2.id
        left join nd_company_info c on o.company_id = c.id
        where 1 = 1
        <if test="param.regionIds != null and param.regionIds.size>0">
            and o.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.name is not null then r2.name else r.name end),
                (case when r2.id is not null then r2.id else r.id end),
                (case when r2.region_code is not null then r2.region_code else r.region_code end)
            </when>
            <otherwise>
                r.name,
                r.id,
                r.region_code
            </otherwise>
        </choose>
    </select>

    <select id="getOutDangerRegions" resultType="com.ningdatech.carapi.common.model.po.MapDataPO">
        SELECT
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                (case when r2.name is not null then r2.name else r.name end) region_name,
                (case when r2.id is not null then r2.id else r.id end) region_id,
                (case when r2.region_code is not null then r2.region_code else r.region_code end) region_code,
            </when>
            <otherwise>
                r.name region_name,
                r.id region_id,
                r.region_code,
            </otherwise>
        </choose>
        count(o.id) value
        FROM nd_insure_out_danger o
        left join nd_region r on o.region_id = r.id
        left join nd_region r2 on r.parent_id = r2.id
        left join nd_company_info c on o.company_id = c.id
        where 1 = 1
        <if test="param.regionIds != null and param.regionIds.size>0">
            and o.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.name is not null then r2.name else r.name end),
                (case when r2.id is not null then r2.id else r.id end),
                (case when r2.region_code is not null then r2.region_code else r.region_code end)
            </when>
            <otherwise>
                r.name,
                r.id,
                r.region_code
            </otherwise>
        </choose>
    </select>

    <select id="getInjuriesRegion" resultType="com.ningdatech.carapi.common.model.po.MapDataPO">
        SELECT
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                (case when r2.name is not null then r2.name else r.name end) region_name,
                (case when r2.id is not null then r2.id else r.id end) region_id,
                (case when r2.region_code is not null then r2.region_code else r.region_code end) region_code,
            </when>
            <otherwise>
                r.name region_name,
                r.id region_id,
                r.region_code,
            </otherwise>
        </choose>
        sum(ifnull(o.injuries,0)) value
        FROM nd_insure_out_danger o
        left join nd_region r on o.region_id = r.id
        left join nd_region r2 on r.parent_id = r2.id
        left join nd_company_info c on o.company_id = c.id
        where 1 = 1
        <if test="param.regionIds != null and param.regionIds.size>0">
            and o.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.name is not null then r2.name else r.name end),
                (case when r2.id is not null then r2.id else r.id end),
                (case when r2.region_code is not null then r2.region_code else r.region_code end)
            </when>
            <otherwise>
                r.name,
                r.id,
                r.region_code
            </otherwise>
        </choose>
    </select>


    <select id="getDeathRegion" resultType="com.ningdatech.carapi.common.model.po.MapDataPO">
        SELECT
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                (case when r2.name is not null then r2.name else r.name end) region_name,
                (case when r2.id is not null then r2.id else r.id end) region_id,
                (case when r2.region_code is not null then r2.region_code else r.region_code end) region_code,
            </when>
            <otherwise>
                r.name region_name,
                r.id region_id,
                r.region_code,
            </otherwise>
        </choose>
        sum(ifnull(o.death_toll,0)) value
        FROM nd_insure_out_danger o
        left join nd_region r on o.region_id = r.id
        left join nd_region r2 on r.parent_id = r2.id
        left join nd_company_info c on o.company_id = c.id
        where 1 = 1
        <if test="param.regionIds != null and param.regionIds.size>0">
            and o.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.name is not null then r2.name else r.name end),
                (case when r2.id is not null then r2.id else r.id end),
                (case when r2.region_code is not null then r2.region_code else r.region_code end)
            </when>
            <otherwise>
                r.name,
                r.id,
                r.region_code
            </otherwise>
        </choose>
    </select>

    <select id="getOnlineRateToday" resultType="com.ningdatech.carapi.car.datascreen.model.po.OnlineVehicleTimesPO">
        select
            h.year,h.month,h.day,h.hour,
            sum(case when h.status = 1 then 1 else 0 end) onlineNum
        from ${tableName} h
                 left join nd_company_info c on h.company_id = c.id
                 left join nd_region r on h.region_id = r.id
        where year = year(now()) and month = month(now()) and day = day(now())
        <if test="param.regionIds != null and param.regionIds.size > 0">
            and h.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
        group by h.year,h.month,h.day,h.hour
    </select>

    <select id="getVehicleOnlineRegions" resultType="com.ningdatech.carapi.car.datascreen.model.po.OnlineVehicleRegionsPO">
        select
            region_name,region_id,region_code,
            vehicleNum,onlineNum,
            IFNULL( round( onlineNum / vehicleNum * 100, 2 ), 0 ) rate
        from(
        SELECT
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                (case when r2.name is not null then r2.name else r.name end) region_name,
                (case when r2.id is not null then r2.id else r.id end) region_id,
                (case when r2.region_code is not null then r2.region_code else r.region_code end) region_code,
            </when>
            <otherwise>
                r.name region_name,
                r.id region_id,
                r.region_code,
            </otherwise>
        </choose>
        count(b.car_plate) vehicleNum,
        sum(case when b.status = 1 then 1 else 0 end) onlineNum
        FROM nd_vehicle_base_info b
        left join nd_region r on b.region_id = r.id
        left join nd_region r2 on r.parent_id = r2.id
        left join nd_company_info c on b.company_id = c.id
        where b.deleted = 0 and b.audit_state = 2
        <if test="param.regionIds != null and param.regionIds.size>0">
            and b.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.name is not null then r2.name else r.name end),
                (case when r2.id is not null then r2.id else r.id end),
                (case when r2.region_code is not null then r2.region_code else r.region_code end)
            </when>
            <otherwise>
                r.name,
                r.id,
                r.region_code
            </otherwise>
        </choose>) online
    </select>

    <select id="getVehicleUsefulLife" resultType="com.ningdatech.carapi.car.datascreen.model.po.VehicleUsefulLifeStatisticsPO">
        select
            count(bb.car_plate) vehicleNum,
            bb.usefulLife
        from(
        SELECT
        b.car_plate,
        (case when b.useful_life &gt;= 0 and b.useful_life &lt; 3 then '3年以下'
            when b.useful_life &gt;= 3 and b.useful_life &lt; 5 then '3-5年'
            when b.useful_life &gt;= 5 and b.useful_life &lt; 10 then '5-10年'
            when b.useful_life &gt; 10  then '10年以上' end) usefulLife
        FROM nd_vehicle_base_info b
        left join nd_region r on b.region_id = r.id
        left join nd_company_info c on b.company_id = c.id
        where b.deleted = 0 and b.audit_state = 2
        <if test="param.regionIds != null and param.regionIds.size>0">
            and b.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}) bb
        where usefulLife is not null
        group by bb.usefulLife
    </select>
</mapper>
