<?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.safe.mapper.SecurityManageDataScreenMapper">

    <!-- 映射结果 -->
    <resultMap id="outDangerResultMap" type="com.ningdatech.carapi.safe.model.po.OutDangerAnalysisPO">
        <result column="year" jdbcType="VARCHAR" property="year"/>
        <result column="month" jdbcType="VARCHAR" property="month"/>
        <result column="out_danger_times" jdbcType="BIGINT" property="outDangerTimes"/>
        <result column="injuries" jdbcType="BIGINT" property="injuries"/>
        <result column="death_toll" jdbcType="BIGINT" property="deathToll"/>
    </resultMap>

    <!-- 映射结果 -->
    <resultMap id="overspeedResultMap" type="com.ningdatech.carapi.safe.model.po.OverspeedAnalysisPO">
        <result column="year" jdbcType="VARCHAR" property="year"/>
        <result column="month" jdbcType="VARCHAR" property="month"/>
        <result column="times" jdbcType="BIGINT" property="times"/>
    </resultMap>

    <!-- 映射结果 -->
    <resultMap id="outDangerTimesResultMap" type="com.ningdatech.carapi.safe.model.po.OutDangerPeriodTimePO">
        <result column="first_period_time" jdbcType="BIGINT" property="firstPeriodTime"/>
        <result column="second_period_time" jdbcType="BIGINT" property="secondPeriodTime"/>
        <result column="third_period_time" jdbcType="BIGINT" property="thirdPeriodTime"/>
        <result column="last_period_time" jdbcType="BIGINT" property="lastPeriodTime"/>
    </resultMap>

    <resultMap id="dateTimesResultMap" type="com.ningdatech.carapi.safe.model.po.OverspeedTimesPO">
        <result column="month_day" jdbcType="VARCHAR" property="monthDay"/>
        <result column="overspeed_times" jdbcType="BIGINT" property="overspeedTimes"/>
    </resultMap>

    <resultMap id="outDangerCityResultMap" type="com.ningdatech.carapi.safe.model.po.OutDangerCityPO">
        <result column="region_name" jdbcType="VARCHAR" property="regionName"/>
        <result column="region_id" jdbcType="BIGINT" property="regionId"/>
        <result column="region_code" jdbcType="VARCHAR" property="regionCode"/>
        <result column="before_out_danger" jdbcType="BIGINT" property="beforeOutDanger"/>
        <result column="last_out_danger" jdbcType="BIGINT" property="lastOutDanger"/>
        <result column="current_out_danger" jdbcType="BIGINT" property="currentOutDanger"/>
        <result column="total" jdbcType="BIGINT" property="total"/>
    </resultMap>

    <select id="getOutDangerAnalysis"
            resultType="java.util.List" resultMap="outDangerResultMap" >
        select
          year(od.accident_time) year,Right(100 + month(od.accident_time),2) month,count(od.id) out_danger_times,
          sum(od.injuries) injuries,sum(od.death_toll) death_toll
        from nd_insure_out_danger od
        left join nd_region r on od.region_id = r.id
        left join nd_company_info c on od.company_id = c.id
        where od.accident_time BETWEEN year(now()) - 3 and now()
        <if test="param.regionIds != null and param.regionIds.size>0">
            and od.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                 #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
        group by year(od.accident_time),Right(100 + month(od.accident_time),2)
    </select>

    <select id="getOverspeedAnalysis" resultType="java.util.List"
            resultMap="overspeedResultMap" >
        select
            year(o.overspeed_date) year,
            Right(100 + month(o.overspeed_date),2) month,
            count(o.id) times
        from nd_vehicle_overspeed_info o
        left join nd_region r on o.region_id = r.id
        left join nd_company_info c on o.company_id = c.id
        where o.overspeed_date BETWEEN year(now()) - 2 and now()
        <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 year(o.overspeed_date),Right(100 + month(o.overspeed_date),2)
    </select>

    <select id="getOutDangerPeriodTime" resultMap="outDangerTimesResultMap" >
        select
            sum(case when HOUR(accident_time) &gt;= 0 and HOUR(accident_time) &lt; 2 then 1 else 0 end)  first_period_time,
            sum(case when HOUR(accident_time) &gt;= 2 and HOUR(accident_time) &lt; 4 then 1 else 0 end)  second_period_time,
            sum(case when HOUR(accident_time) &gt;= 4 and HOUR(accident_time) &lt; 6 then 1 else 0 end)  third_period_time,
            sum(case when HOUR(accident_time) &gt;= 6 and HOUR(accident_time) &lt;8 then 1 else 0 end)  fourth_period_time,
            sum(case when HOUR(accident_time) &gt;= 8 and HOUR(accident_time) &lt;10 then 1 else 0 end)  fifth_period_time,
            sum(case when HOUR(accident_time) &gt;= 10 and HOUR(accident_time) &lt;12 then 1 else 0 end)  sixth_period_time,
            sum(case when HOUR(accident_time) &gt;= 12 and HOUR(accident_time) &lt; 14 then 1 else 0 end)  seventh_period_time,
            sum(case when HOUR(accident_time) &gt;= 14 and HOUR(accident_time) &lt; 16 then 1 else 0 end)  eighth_period_time,
            sum(case when HOUR(accident_time) &gt;= 16 and HOUR(accident_time) &lt; 18 then 1 else 0 end)  ninth_period_time,
            sum(case when HOUR(accident_time) &gt;= 18 and HOUR(accident_time) &lt; 20 then 1 else 0 end)  tenth_period_time,
            sum(case when HOUR(accident_time) &gt;= 20 and HOUR(accident_time) &lt; 22 then 1 else 0 end)  eleventh_period_time,
            sum(case when HOUR(accident_time) &gt;= 22 and HOUR(accident_time) &lt; 24 then 1 else 0 end)  last_period_time
        from nd_insure_out_danger od
        left join nd_region r on od.region_id = r.id
        left join nd_company_info c on od.company_id = c.id
        where accident_time between #{param.startTime} and #{param.endTime}
        <if test="param.regionIds != null and param.regionIds.size>0">
            and od.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
    </select>

    <select id="getOverSpeedCurrentMonth"  parameterType="java.util.List"
            resultType="java.util.List" resultMap="dateTimesResultMap">
        select
            concat(YEAR(overspeed_date),'-',Right(100 + Month(overspeed_date), 2),'-',Right(100 + DAY(overspeed_date),2)) month_day
            ,count(ov.id) overspeed_times
        from nd_vehicle_overspeed_info ov
        left join nd_region r on ov.region_id = r.id
        left join nd_company_info c on ov.company_id = c.id
        where PERIOD_DIFF( date_format(now(), '%Y%m' ),date_format(overspeed_date, '%Y%m')) &lt; 1
        <if test="param.regionIds != null and param.regionIds.size>0">
            and ov.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
        group by concat(YEAR(overspeed_date),'-',Right(100 + Month(overspeed_date), 2),'-',Right(100 + DAY(overspeed_date),2))
    </select>

    <select id="getOutDangerRegions"
            resultType="java.util.List" resultMap="outDangerCityResultMap">
        SELECT
            <choose>
                <when test="param.regionLevel != null and param.regionLevel == 1">
                    ( CASE WHEN r.level = 2 then r.id ELSE r2.id END ) region_id,
                    ( CASE WHEN r.level = 2 then r.name ELSE r2.NAME END ) region_name,
                    ( CASE WHEN r.level = 2 then r.region_code ELSE r2.region_code END ) region_code,
                </when>
                <otherwise>
                    r.name region_name,
                    r.id region_id,
                    r.region_code,
                </otherwise>
            </choose>
             sum(case when od.accident_time BETWEEN date_format(concat(year(now())-2,'-',01,'-',01,' 00:00:00'),'%Y-%m-%d %H:%M:%S') and date_format(concat(year(now())-2,'-',12,'-',31,' 23:59:59'),'%Y-%m-%d %H:%M:%S') then 1 else 0 end) before_out_danger,
             sum(case when od.accident_time BETWEEN date_format(concat(year(now())-1,'-',01,'-',01,' 00:00:00'),'%Y-%m-%d %H:%M:%S') and date_format(concat(year(now())-1 ,'-',12,'-',31,' 23:59:59'),'%Y-%m-%d %H:%M:%S') then 1 else 0 end) last_out_danger,
             sum(case when od.accident_time BETWEEN date_format(concat(year(now()),'-',01,'-',01,' 00:00:00'),'%Y-%m-%d %H:%M:%S') and date_format(concat(year(now()),'-',12,'-',31,' 23:59:59'),'%Y-%m-%d %H:%M:%S') then 1 else 0 end) current_out_danger,
             sum(case when od.accident_time BETWEEN date_format(concat(year(now())-2,'-',01,'-',01,' 00:00:00'),'%Y-%m-%d %H:%M:%S') and date_format(concat(year(now()),'-',12,'-',31,' 23:59:59'),'%Y-%m-%d %H:%M:%S') then 1 else 0 end) total
        FROM nd_insure_out_danger od
        left join nd_region r on od.region_id = r.id
        left join nd_region r2 on r.parent_id = r2.id
        left join nd_company_info c on od.company_id = c.id
        where 1 = 1
        <if test="param.regionIds != null and param.regionIds.size>0">
            and od.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 r.level = 2 then r.id ELSE r2.id END ) ,
                ( CASE WHEN r.level = 2 then r.name ELSE r2.NAME END ) ,
                ( CASE WHEN r.level = 2 then r.region_code ELSE r2.region_code END )
            </when>
            <otherwise>
                r.name,
                r.id,
                r.region_code
            </otherwise>
        </choose>
    </select>
</mapper>
