<?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.analysis.mapper.WarnAnalysisDriverMapper">
    <select id="listDriverAnalysis" resultType="com.ningdatech.carapi.analysis.entity.WarnAnalysisDriver">
        select
            driver_id,driver_name,
            region_id,company_id,company_name,injured_count,
            dead_count,danger_count,over_speed_count,violation_count,
            danger_score,danger_level
        from(select
                 driver_id,driver_name,
                 region_id,company_id,company_name,injured_count,
                 dead_count,danger_count,over_speed_count,violation_count,
                 (danger_count / car_num * 40 + injured_count / car_num * 400) danger_score,
                 (case when dead_count > 0 then 1
                    when danger_count / car_num * 40 + injured_count / car_num * 400 &gt;= 60 then 2
                    when danger_count / car_num * 40 + injured_count / car_num * 400 &gt;= 40
                    and danger_count / car_num * 40 + injured_count / car_num * 400 &lt;= 60 then 3
                    when danger_count / car_num * 40 + injured_count / car_num * 400 &gt;= 20
                    and danger_count / car_num * 40 + injured_count / car_num * 400 &lt;= 40 then 4 end) danger_level
             from (SELECT
                       count(DISTINCT b.id) car_num,
                       d.id driver_id,d.driver_name,c.region_id,
                       d.company_id,c.name company_name,
                       ifnull(o.injured_count,0) injured_count,
                       ifnull(o.dead_count,0) dead_count,
                       ifnull(count(DISTINCT o.id),0) danger_count,
                       ifnull(count(DISTINCT oi.id),0) over_speed_count,
                       ifnull(count(DISTINCT vi.id),0) violation_count
                   FROM nd_vehicle_base_info b
                            left join nd_vehicle_get_on g on b.car_plate = g.car_plate
                            right join nd_driver_info d on g.driver_id = d.id
                            left join nd_company_info c on d.company_id = c.id
                            left join nd_region r on c.region_id = r.id
                            right join
                       (select id,car_plate,injuries,death_toll,
                               sum(ifnull(injuries,0)) injured_count,
                               sum(ifnull(death_toll,0)) dead_count
                        from nd_insure_out_danger
                        where deleted = 0
--                      统计近6个月的出险数据
                        and accident_time
                        BETWEEN DATE_FORMAT(DATE_SUB(NOW(),INTERVAL 5 MONTH),'%Y-%m-01 00:00:00')
                        AND LAST_DAY(NOW()) GROUP BY accident_time,car_plate ) o on b.car_plate = o.car_plate
                            left join nd_vehicle_overspeed_info oi on b.car_plate = oi.car_plate
                            left join nd_vehicle_violation vi on b.car_plate = vi.car_plate
                   where b.deleted = 0 and b.audit_state = 2 and d.driver_name is not null and c.region_id is not null
                   ${param.dataScopeSql}
                   group by d.id,d.driver_name,c.region_id,d.company_id,c.name
                  ) a ) aa
        where 1 = 1
    </select>

    <insert id="saveBatchXml" parameterType="java.util.List">
        INSERT INTO nd_warn_analysis_driver
        (region_id,
        driver_id,
        driver_name,
        company_id,
        company_name,
        injured_count,
        dead_count,
        danger_count,
        danger_score,
        danger_level,
        over_speed_count,
        violation_count
        )
        values
        <foreach collection="singleList" item="item" separator="," index="index">
            (
            #{item.regionId},
            #{item.driverId},
            #{item.driverName},
            #{item.companyId},
            #{item.companyName},
            #{item.injuredCount},
            #{item.deadCount},
            #{item.dangerCount},
            #{item.dangerScore},
            #{item.dangerLevel},
            #{item.overSpeedCount},
            #{item.violationCount}
            )
        </foreach>
    </insert>
</mapper>
