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

    <select id="pageAnalysis"
            resultType="com.ningdatech.carapi.safe.model.po.NdInsureOutDangerAnalysisPO">
        select
        region_id,region_name,outDangerTimes,accident_time,injuries,death_toll,rank
        from
        (select
        region_id,region_name,outDangerTimes,accident_time,injuries,death_toll,@times:=@times+1 as rank
        from
        (
        select
        od.region_id,od.region_name,count(od.id) outDangerTimes
        ,concat(year(od.accident_time),'-',Right(100 + month(od.accident_time),2)) accident_time,
        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.deleted = 0 -- and od.accident_time is not null
        <if test="po.startTime != null">
            and od.accident_time &gt;= #{po.startTime}
        </if>
        <if test="po.endTime != null">
            and od.accident_time &lt;= #{po.endTime}
        </if>
        <if test="po.regionIds != null and po.regionIds.size > 0">
            and od.region_id in
            <foreach collection="po.regionIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ${po.dataScopeSql}
        group by od.region_id,od.region_name,
        concat(year(od.accident_time),'-',Right(100 + month(od.accident_time),2))
        ) od,(select @times:=0) t
        order by accident_time desc, outDangerTimes desc,death_toll desc,injuries desc) o
        where 1 = 1
        <if test="po.outDangerRank != null">
            and o.rank &lt;= #{po.outDangerRank}
        </if>
    </select>
</mapper>
