<?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.casic.missiles.modular.system.dao.AlarmRecordsMapper">
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.casic.missiles.modular.system.model.AlarmRecords">
        <id column="ID" property="id"/>
        <result column="DEVICE_ID" property="deviceId"/>
        <result column="DEVCODE" property="devcode"/>
        <result column="WELL_CODE" property="wellCode"/>
        <result column="ALARM_TYPE" property="alarmType"/>
        <result column="ALARM_CONTENT" property="alarmContent"/>
        <result column="ALARM_VALUE" property="alarmValue"/>
        <result column="ALARM_LEVEL" property="alarmLevel"/>
        <result column="ALARM_MSG" property="alarmMessage"/>
        <result column="ALARM_TIME" property="alarmTime"/>
        <result column="STATUS" property="status"/>
        <result column="JOB_ID" property="jobId"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        ID AS id, DEVICE_ID AS "deviceId", DEVCODE AS devcode, WELL_CODE AS "wellCode",
         ALARM_TYPE AS "alarmType", ALARM_CONTENT AS "alarmContent", ALARM_VALUE AS "alarmValue",
         ALARM_TIME AS "alarmTime", STATUS AS status, JOB_ID AS "jobId"
    </sql>

    <sql id="char_toDate" databaseId="mysql">
        ${paramStr}
    </sql>

    <sql id="char_toDate" databaseId="pgsql">
       TO_TIMESTAMP(${paramStr},'yyyy-MM-dd hh24:mi:ss')::timestamp without time zone
    </sql>

    <sql id="char_toDate" databaseId="oracle">
          ${paramStr}
    </sql>

    <sql id="keywords_sql" databaseId="oracle">
        CONCAT('%',#{keywords},'%')
    </sql>
    <sql id="keywords_sql" databaseId="mysql">
        CONCAT('%',#{keywords},'%')
    </sql>
    <sql id="keywords_sql" databaseId="pgsql">
        CONCAT('%',#{keywords},'%')
    </sql>

    <sql id="date_toChar" databaseId="mysql">
       DATE_FORMAT(${paramTime},'%Y-%m-%d %H:%i:%s')
    </sql>

    <sql id="date_toChar" databaseId="pgsql">
       TO_CHAR(${paramTime},'yyyy-mm-dd hh24:mi:ss')
    </sql>

    <sql id="date_toChar" databaseId="oracle">
       TO_CHAR(${paramTime},'yyyy-mm-dd hh24:mi:ss')
    </sql>

    <select id="alarmList" resultType="map">
        SELECT
        ar.ID as "id",
        bwi.WELL_CODE as "wellCode",
        ar.ALARM_CONTENT as "alarmContentName",
        ar.DEVICE_ID as "deviceId",
        <include refid="date_toChar">
            <property name="paramTime" value="ar.ALARM_TIME"/>
        </include>
        AS "alarmTime",
        ar.STATUS as "status",
        ar.alarm_type as "alarmType",
        ar.ALARM_VALUE AS "alarmValue",
        ar.ALARM_LEVEL AS "alarmLevel",
        ar.ALARM_MSG AS "alarmMessage",
        ar.DEVCODE as "devcode",
        aj.JOB_STATUS as "jobStatus",
        ar.JOB_ID as "jobId",
        bwi.DEPTID AS "deptid"
        FROM alarm_records ar
        JOIN bus_device_well rdv ON ar.DEVICE_ID = rdv.DEVICE_ID
        LEFT JOIN alarm_job aj ON ar.JOB_ID = aj.ID
        JOIN bus_well_info bwi ON rdv.well_id = bwi.ID
        WHERE rdv.VALID='1'

        <if test="status!='' and status!=null">
            AND ar.STATUS = #{status}
        </if>
        <if test="keywords!='' and keywords!=null">
            and ( bwi.WELL_CODE like
            <include refid="keywords_sql"/>
            or bwi.WELL_NAME like
            <include refid="keywords_sql"/>
            )
        </if>
        <if test="alarmType!='' and alarmType!= null">
            AND ar.ALARM_TYPE = #{alarmType}
        </if>
        <if test="alarmContent!='' and alarmContent!= null">
            AND ar.ALARM_CONTENT = #{alarmContent}
        </if>
        <if test="beginTime!='' and beginTime!=null">
            AND ar.ALARM_TIME <![CDATA[ >= ]]>
            <include refid="char_toDate">
                <property name="paramStr" value="#{beginTime}"/>
            </include>
        </if>
        <if test="endTime!='' and endTime!=null">
            AND ar.ALARM_TIME <![CDATA[ <= ]]>
            <include refid="char_toDate">
                <property name="paramStr" value="#{endTime}"/>
            </include>
        </if>
        ORDER BY ar.ALARM_TIME DESC
    </select>

    <select id="alarmListNoPage" resultType="com.casic.missiles.modular.system.model.AlarmRecords">
        SELECT
        ar.ID AS id,
        ar.DEVICE_ID AS "deviceId",
        ar.DEVCODE AS devcode,
        ar.WELL_CODE AS "wellCode",
        ar.ALARM_TYPE AS "alarmType",
        ar.ALARM_CONTENT AS "alarmContent",
        ar.ALARM_VALUE AS "alarmValue",
        ar.ALARM_TIME AS "alarmTime",
        ar.ALARM_LEVEL AS "alarmLevel",
        ar.ALARM_MSG AS "alarmMessage",
        ar.STATUS AS status,
        ar.JOB_ID AS "jobId",
        bwi.DEPTID AS deptid
        FROM alarm_records ar
        LEFT JOIN bus_well_info bwi ON ar.WELL_CODE = bwi.WELL_CODE
        WHERE STATUS = '1'
        <if test="keywords!='' and keywords!=null">
            and ar.WELL_CODE like
            <include refid="keywords_sql"/>
        </if>
        <if test="alarmType!='' and alarmType!= null">
            AND ar.ALARM_TYPE = #{alarmType}
        </if>
        <if test="alarmContent!='' and alarmContent!= null">
            AND ar.ALARM_CONTENT = #{alarmContent}
        </if>
        <if test="beginTime!='' and beginTime!=null">
            AND ar.ALARM_TIME <![CDATA[  >=  ]]>
            <include refid="char_toDate">
                <property name="paramStr" value="#{beginTime}"/>
            </include>
        </if>
        <if test="endTime!='' and endTime!=null">
            AND ar.ALARM_TIME <![CDATA[  <=  ]]>
            <include refid="char_toDate">
                <property name="paramStr" value="#{endTime}"/>
            </include>
        </if>
    </select>

    <update id="cancelAlarm" flushCache="true">
        UPDATE alarm_records
        SET    status = 2
        WHERE ID = #{id}
    </update>

    <update id="closeJob" flushCache="true">
        update alarm_job
        <set>
            job_status = #{jobStatus} ,
            handle_job_person = #{personId},
            job_belong_to = #{personId},
            handle_job_time = now(),
            handle_message = #{handleMessage},
        </set>
        WHERE ID=(
        SELECT JOB_ID
        FROM alarm_records
        WHERE ID=#{id}
        )
    </update>

</mapper>
