<?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.WarnAnalysisCompanyMapper">

    <select id="listCompanyAnalysis" resultType="com.ningdatech.carapi.analysis.entity.WarnAnalysisCompany">
        select
        region_id,company_id,company_name,injured_count,
        dead_count,danger_count,over_speed_count,violation_count,
        total_score,danger_level
        from(select
        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) total_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,
        b.region_id,
        b.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_company_info c on b.company_id = c.id
        left join nd_region r on b.region_id = r.id
        right join
--      统计近6个月的出险数据
        (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
         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
        <if test="param.regionIds != null and param.regionIds.size > 0">
            and b.region_id in
            <foreach collection="param.regionIds" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <if test="param.companyName != null and param.companyName != ''">
            and c.name like concat('%',#{param.companyName},'%')
        </if>
        <if test="param.carNo != null and param.carNo != ''">
            and b.car_plate like concat('%',#{param.carNo},'%')
        </if>
        ${param.dataScopeSql}
        group by b.region_id,b.company_id,c.name
        ) a
        GROUP BY region_id,company_id,company_name) aa
        where company_name is not null
        <if test="param.dangerLevel != null and param.dangerLevel != ''">
            and aa.danger_level = #{param.dangerLevel}
        </if>
    </select>

    <insert id="saveBatchXml" parameterType="java.util.List">
        INSERT INTO nd_warn_analysis_company
        (region_id,
        company_id,
        company_name,
        injured_count,
        dead_count,
        danger_count,
        total_score,
        danger_level,
        violation_count
        )
        values
        <foreach collection="singleList" item="item" separator="," index="index">
            (
            #{item.regionId},
            #{item.companyId},
            #{item.companyName},
            #{item.injuredCount},
            #{item.deadCount},
            #{item.dangerCount},
            #{item.totalScore},
            #{item.dangerLevel},
            #{item.violationCount}
            )
        </foreach>
    </insert>
</mapper>
