<?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.driver.mapper.DriverInfoMapper">

    <select id="driverAgeStatistics" resultType="com.ningdatech.carapi.common.model.KeyValueDTO">
        select count(1) `value`, age `key` from (select @age := TIMESTAMPDIFF(YEAR, DATE(birth), CURRENT_DATE()),
         (case ${caseSql} end) age
        from nd_driver_info ndi
        left join nd_company_info c on ndi.company_id = c.id
        left join nd_region r on ndi.region_id = r.id
        where 1 = 1
        <if test="param.regionIds != null and param.regionIds.size>0">
            and ndi.region_id in
            <foreach collection="param.regionIds"  item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
        ) t
        where age != #{undefinedAge}
        group by age;
    </select>

    <select id="drivingExperienceStatistics" resultType="com.ningdatech.carapi.common.model.KeyValueDTO">
        select count(1) `value`, age `key` from (select @age := TIMESTAMPDIFF(YEAR, DATE(initial_license_time), CURRENT_DATE()),
        (case ${caseSql} end) age
        from nd_driver_info ndi
        left join nd_company_info c on ndi.company_id = c.id
        left join nd_region r on ndi.region_id = r.id
        where 1 = 1
        <if test="param.regionIds != null and param.regionIds.size>0">
            and ndi.region_id in
            <foreach collection="param.regionIds"  item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
        ) t
        where age != #{undefinedAge}
        group by age;
    </select>

    <select id="diverCountByRegion" resultType="com.ningdatech.carapi.common.model.po.MapDataPO">
        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(d.id) value
        FROM `nd_driver_info` d
                 left join nd_company_info c on d.company_id = c.id
                 left join nd_region r on d.region_id = r.id
                 left join nd_region r2 on r.parent_id = r2.id
                 where 1 = 1
                 <if test="param.regionIds != null and param.regionIds.size>0">
                     and d.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="nativePlaceStatisticsByRegion" resultType="java.util.Map">
        select
            count(1) `count`, native_place_region_code nativePlaceRegionCode, native_place nativePlace
        from nd_driver_info ndi
        left join nd_company_info c on ndi.company_id = c.id
        left join nd_region r on ndi.region_id = r.id
        where ndi.native_place is not null
        ${param.dataScopeSql}
        group by native_place_region_code, native_place
    </select>
</mapper>
