<?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.ozs.mapper.MsgAlarmMapper">
    <resultMap id="AlarmStatisticDtoResult" type="com.ozs.entity.vo.AlarmStatisticDto">
        <id column="id" property="id"/>
        <result column="alarm_id" property="alarmId"/>
        <result column="railway_name" property="railwayName"/>
        <result column="alarm_time" property="alarmTime"/>
        <result column="line_dir" property="lineDir"/>
        <result column="alarm_mile" property="alarmMile"/>
        <result column="content" property="content"/>
        <result column="alarm_level" property="alarmLevel"/>
        <result column="alarm_confidence" property="alarmConfidence"/>
        <result column="alarm_type" property="alarmType"/>
        <result column="alarm_attr" property="alarmAttr"/>
        <result column="is_release" property="isRelease"/>
        <result column="released_by" property="releasedBy"/>
        <result column="released_time" property="releasedTime"/>
        <result column="release_content" property="releaseContent"/>
        <result column="remark" property="remark"/>
    </resultMap>

    <select id="list" resultMap="AlarmStatisticDtoResult">
        SELECT /*+ GROUP_OPT_FLAG(1)*/
        a.alarm_id ,
        b.railway_name,
        a.alarm_time ,
        a.line_dir,
        a.alarm_mile,
        a.alarm_level,
        a.content ,
        a.alarm_confidence ,
        a.alarm_type,
        a.alarm_attr,
        a.is_release,
        a.released_by ,
        a.released_time,
        a.release_content,
        a.remark
        FROM
        msg_alarm AS a inner join
        base_railway_management AS b on a.railway_code=b.railway_code

        <where>
            <if test="railwayCode != null and railwayCode != ''">
                and UPPER(a.railway_code) like UPPER(CONCAT('%',#{railwayCode},'%'))
            </if>
            <if test="currentYear != null and currentYear != 0">
                and YEAR(a.alarm_time) like UPPER(CONCAT('%',#{currentYear},'%'))
            </if>
            <if test="currentYear == null or currentYear == ''">
                and YEAR(a.alarm_time) = YEAR(SYSDATE())
            </if>
            <if test="currentMonth != null and currentMonth != 0">
                and MONTH(a.alarm_time) =#{currentMonth}
            </if>
        </where>
        group by a.alarm_id order by a.alarm_time desc
    </select>

    <resultMap id="AlarmStatisticMonthDtoResult" type="com.ozs.entity.vo.AlarmStatisticMonthDto">
        <id column="id" property="id"/>
        <result column="monthsta" property="monthsta"/>
        <result column="alarmType" property="alarmType"/>
        <result column="monthstaString" property="monthstaString"/>
        <result column="frequency" property="frequency"/>
    </resultMap>
    <select id="alarmTypeCount" resultMap="AlarmStatisticMonthDtoResult">

        SELECT /*+ GROUP_OPT_FLAG(1)*/ sum(p.fre) as frequency,p.alarmType as alarmType
        from
        (SELECT /*+ GROUP_OPT_FLAG(1)*/
        a.alarm_type as alarmType,
        count(DISTINCT a.alarm_id) as fre
        FROM
        msg_alarm AS a

        <where>
            <if test="railwayCode != null and railwayCode != ''">
                and UPPER(a.railway_code) like UPPER(CONCAT('%',#{railwayCode},'%'))
            </if>
            <if test="currentYear != null and currentYear != 0">
                and YEAR(a.alarm_time) like UPPER(CONCAT('%',#{currentYear},'%'))
            </if>
            <if test="currentYear == null or currentYear == ''">
                and YEAR(a.alarm_time) = YEAR(SYSDATE())
            </if>
            <if test="currentMonth != null and currentMonth != 0">
                and MONTH(a.alarm_time) =#{currentMonth}
            </if>
        </where>
        group by a.alarm_id) AS p group by p.alarmType
    </select>
    <select id="listDtoMonth" resultMap="AlarmStatisticMonthDtoResult">
        SELECT /*+ GROUP_OPT_FLAG(1)*/ p.monthsta as monthsta,sum(p.fre) as frequency,p.monthstaString as monthstaString
        from
        (SELECT /*+ GROUP_OPT_FLAG(1)*/
        translate(to_char(a.alarm_time, 'yyyy-mm#'),'-#','年月')
        /*DATE_FORMAT(a.alarm_time,'%Y年%m月')*/ as monthstaString,
        MONTH(a.alarm_time) as monthsta,
        count(DISTINCT a.alarm_id) as fre
        FROM
        msg_alarm AS a

        <where>
            <if test="railwayCode != null and railwayCode != ''">
                and UPPER(a.railway_code) like UPPER(CONCAT('%',#{railwayCode},'%'))
            </if>
            <if test="currentYear != null and currentYear != 0">
                and YEAR(a.alarm_time) like UPPER(CONCAT('%',#{currentYear},'%'))
            </if>
            <if test="currentYear == null or currentYear == ''">
                and YEAR(a.alarm_time) = YEAR(SYSDATE())
            </if>
            <if test="currentMonth != null and currentMonth != 0">
                and MONTH(a.alarm_time) =#{currentMonth}
            </if>
        </where>
        group by a.alarm_id) AS p group by p.monthsta order by p.monthsta
    </select>

    <select id="listDtoDay" resultMap="AlarmStatisticMonthDtoResult">
        SELECT /*+ GROUP_OPT_FLAG(1)*/ p.daysta as monthsta,sum(p.fre) as frequency,p.daystaString as monthstaString
        from
        (SELECT /*+ GROUP_OPT_FLAG(1)*/
        translate(to_char(a.alarm_time, 'yyyy-mm-dd#'),'-#','年月日')
        /*DATE_FORMAT(a.alarm_time,'%Y年%m月%d日')*/ as daystaString,
        DAY(a.alarm_time) as daysta,
        count(DISTINCT a.alarm_id) as fre
        FROM
        msg_alarm AS a

        <where>
            <if test="railwayCode != null and railwayCode != ''">
                and UPPER(a.railway_code) like UPPER(CONCAT('%',#{railwayCode},'%'))
            </if>
            <if test="currentYear != null and currentYear != 0">
                and YEAR(a.alarm_time) like UPPER(CONCAT('%',#{currentYear},'%'))
            </if>
            <if test="currentYear == null or currentYear == ''">
                and YEAR(a.alarm_time) = YEAR(SYSDATE())
            </if>
            <if test="currentMonth != null and currentMonth != 0">
                and MONTH(a.alarm_time) =#{currentMonth}
            </if>
        </where>
        group by a.alarm_id) AS p group by p.daysta order by p.daysta
    </select>

    <resultMap id="AlarmTypeStatisticResult" type="com.ozs.entity.vo.AlarmFreqArea">
        <id column="id" property="id"/>
        <result column="railwayName" property="railwayName"/>
        <result column="railwayCode" property="railwayCode"/>
        <result column="alarmMile" property="alarmMile"/>
        <result column="frequency" property="frequency"/>
    </resultMap>
    <select id="alarmTypeAreaCount" resultMap="AlarmTypeStatisticResult">
        SELECT /*+ GROUP_OPT_FLAG(1)*/ railwayCode,railwayName,alarmMile,SUM(p.pre) as frequency
        from
        (SELECT /*+ GROUP_OPT_FLAG(1)*/
        a.railway_code as railwayCode,
        b.railway_name as railwayName,
        a.alarm_mile as alarmMile,
        count(a.railway_code) as pre
        FROM
        msg_alarm AS a inner join
        base_railway_management b on a.railway_code=b.railway_code

        <where>
            <if test="railwayCode != null and railwayCode != ''">
                and UPPER(a.railway_code) like UPPER(CONCAT('%',#{railwayCode},'%'))
            </if>
            <if test="currentYear != null and currentYear != 0">
                and YEAR(a.alarm_time) like UPPER(CONCAT('%',#{currentYear},'%'))
            </if>
            <if test="currentYear == null or currentYear == ''">
                and YEAR(a.alarm_time) = YEAR(SYSDATE())
            </if>
            <if test="currentMonth != null and currentMonth != 0">
                and MONTH(a.alarm_time) =#{currentMonth}
            </if>
        </where>
        group by a.alarm_id) AS p group by p.railwayCode order by frequency desc
    </select>

    <select id="getAlarmNum" resultType="java.lang.Long">
        select count(DISTINCT alarm_id) as alarmNum
        from msg_alarm
    </select>

    <select id="selectCameraCode" parameterType="java.lang.String" resultType="java.lang.Integer">
        select is_lock
        from msg_alarm
        where camera_code = #{cameraCode}
        ORDER BY alarm_time DESC
        LIMIT 1
    </select>

    <select id="selectByCameraCode" parameterType="java.lang.String" resultType="com.ozs.entity.MsgAlarm">
        select *
        from msg_alarm
        where camera_code = #{cameraCode}
        order by alarm_time desc
        limit 1
    </select>

    <!-- and date_format(alarm_time,'%Y-%m-%d %H:%i:%S') &gt;= date_format(#{beginAlarmTime},'%Y-%m-%d
             %H:%i:%S')         -->
    <!-- AND date_format(alarm_time,'%Y-%m-%d %H:%i:%S') &lt;= date_format(#{endAlarmTime},'%Y-%m-%d
     %H:%i:%S')-->
    <select id="selectMsgAlarmList" resultType="com.ozs.entity.MsgAlarm" parameterType="com.ozs.entity.vo.MsgAlarmVo">
        SELECT * FROM msg_alarm
        <where>
            <if test="railwayCode != null and railwayCode != ''">
                and railway_code = #{railwayCode}
            </if>
            <if test="alarmType != null and alarmType != 0">
                and alarm_type = #{alarmType}
            </if>
            <if test="alarmMile != null and alarmMile != 0">
                and alarm_mile = #{alarmMile}
            </if>
            <if test="beginMile != null and beginMile != 0">
                and alarm_mile &gt;= #{beginMile}
            </if>
            <if test="endMile != null and endMile != 0">
                and alarm_mile &lt;= #{endMile}
            </if>
            <if test="isRelease != null and isRelease != 0">
                and is_release = #{isRelease}
            </if>
            <if test="lineDir != null and lineDir != 0">
                and line_dir = #{lineDir}
            </if>
            <if test="beginAlarmTime != null and beginAlarmTime != ''">
                and TO_CHAR(alarm_time, 'YYYY-MM-DD HH24:MI:SS') &gt;= TO_CHAR(#{beginAlarmTime}, 'YYYY-MM-DD
                HH24:MI:SS')
            </if>
            <if test="endAlarmTime != null and endAlarmTime != ''">
                and TO_CHAR(alarm_time, 'YYYY-MM-DD HH24:MI:SS') &lt;= TO_CHAR(#{endAlarmTime}, 'YYYY-MM-DD HH24:MI:SS')
            </if>
            <if test="source != null and source != ''">
                and source = #{source}
            </if>
        </where>
        order by  alarm_time desc
    </select>

    <select id="getByAlarmId" resultType="com.ozs.entity.MsgAlarm" parameterType="java.lang.String">
        select * from msg_alarm where alarm_id = #{alarmId}
    </select>

    <select id="countMsg" parameterType="com.ozs.entity.vo.MsgAlarmVo" resultType="java.lang.Integer">
        select count(1) from msg_alarm
    </select>

    <select id="countJC" parameterType="com.ozs.entity.vo.MsgAlarmVo" resultType="java.lang.Integer">
        select count(1) from msg_alarm AS a join base_camera_management AS b on a.railway_code=b.railway_code
        <where>
            a.is_release = 1
            <if test="!dsFlay">
                and
                <trim prefix="(" prefixOverrides="or" suffix=")">
                    <if test="dsUserId != null and dsUserId != ''">
                        or a.create_by = #{dsUserId}
                    </if>
                    <if test="dsDeptId != null and dsDeptId != 0">
                        or b.dept_id = #{dsDeptId}
                    </if>
                    <if test="dsDeptIds != null">
                        or b.dept_id in
                        <foreach item="item" collection="dsDeptIds" separator="," open="(" close=")" index="">
                            #{item}
                        </foreach>
                    </if>
                </trim>
            </if>
        </where>
    </select>

    <select id="countWJC" parameterType="com.ozs.entity.vo.MsgAlarmVo" resultType="java.lang.Integer">
        select count(1) from msg_alarm AS a join base_camera_management AS b on a.railway_code=b.railway_code
        <where>
            a.is_release = 2
            <if test="!dsFlay">
                and
                <trim prefix="(" prefixOverrides="or" suffix=")">
                    <if test="dsUserId != null and dsUserId != ''">
                        or a.create_by = #{dsUserId}
                    </if>
                    <if test="dsDeptId != null and dsDeptId != 0">
                        or b.dept_id = #{dsDeptId}
                    </if>
                    <if test="dsDeptIds != null">
                        or b.dept_id in
                        <foreach item="item" collection="dsDeptIds" separator="," open="(" close=")" index="">
                            #{item}
                        </foreach>
                    </if>
                </trim>
            </if>
        </where>
    </select>
</mapper>
