<?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.zcloud.personnel.positioning.persistence.mapper.AlarmRecordMapper">
    <select id="selectBiAlarmList"
            resultType="com.zcloud.personnel.positioning.persistence.dataobject.AlarmRecordDO">
        SELECT
            ar.id,
            ar.alarm_no,
            ar.finds_alarm_id,
            ar.alarm_code,
            ar.alarm_name,
            ar.level,
            ar.description,
            ar.dispose_status,
            ar.dispose_description,
            ar.duration,
            ar.start_time,
            ar.end_time,
            ar.start_fences_json,
            ar.end_fences_json,
            ar.lon,
            ar.lat,
            ar.alt,
            ar.speed,
            ar.direction,
            ar.gt,
            CAST(ci.id AS CHAR) AS corpinfo_id,
            ci.corp_name AS corpinfo_name,
            ar.first_id_card_no,
            ar.first_staff_no_snapshot,
            ar.first_staff_name_snapshot,
            ar.terminal_no_list_json,
            ar.sync_time,
            ar.raw_json,
            ar.delete_enum,
            ar.remarks,
            ar.create_name,
            ar.update_name,
            ar.tenant_id,
            ar.org_id,
            ar.env,
            ar.version,
            ar.create_time,
            ar.update_time,
            ar.create_id,
            ar.update_id
        FROM alarm_record ar
        INNER JOIN corp_info ci
            ON CAST(ci.id AS CHAR) = CAST(ar.corpinfo_id AS CHAR)
        WHERE (ar.delete_enum IS NULL OR ar.delete_enum = 'FALSE')
          AND (ci.delete_enum IS NULL OR ci.delete_enum = 'FALSE')
        <if test="corpinfoId != null and corpinfoId != ''">
          AND CAST(ci.id AS CHAR) = CAST(#{corpinfoId} AS CHAR)
        </if>
        <if test="portArea != null">
          AND ci.port_area = #{portArea}
        </if>
        ORDER BY COALESCE(ar.start_time, ar.create_time) DESC, ar.id DESC
    </select>

    <select id="selectBiAlarmTypeList"
            resultType="com.zcloud.personnel.positioning.persistence.dataobject.AlarmTypeStatDO">
        SELECT
            ar.alarm_code AS alarmCode,
            MAX(ar.alarm_name) AS alarmName,
            COUNT(1) AS alarmCount
        FROM alarm_record ar
        INNER JOIN corp_info ci
            ON CAST(ci.id AS CHAR) = CAST(ar.corpinfo_id AS CHAR)
        WHERE (ar.delete_enum IS NULL OR ar.delete_enum = 'FALSE')
          AND (ci.delete_enum IS NULL OR ci.delete_enum = 'FALSE')
        <if test="corpinfoId != null and corpinfoId != ''">
          AND CAST(ci.id AS CHAR) = CAST(#{corpinfoId} AS CHAR)
        </if>
        GROUP BY ar.alarm_code
        ORDER BY alarmCount DESC, ar.alarm_code ASC
    </select>

    <select id="listCompanyOptions"
            resultType="com.zcloud.personnel.positioning.persistence.dataobject.CompanyOptionDO">
        SELECT corp_name AS label, CAST(corpinfo_id AS CHAR) AS value
        FROM corp_info
        WHERE (delete_enum IS NULL OR delete_enum = 'FALSE')
          AND corpinfo_id IS NOT NULL
          AND corp_name IS NOT NULL
        <if test="params.eqCorpinfoId != null and params.eqCorpinfoId != ''">
            AND corpinfo_id = #{params.eqCorpinfoId}
        </if>
        ORDER BY corp_name ASC
        LIMIT 200
    </select>

    <select id="findCorpinfoName" resultType="string">
        SELECT corp_name
        FROM corp_info
        WHERE CAST(corpinfo_id AS CHAR) = CAST(#{corpinfoId} AS CHAR)
          AND (delete_enum IS NULL OR delete_enum = 'FALSE')
        LIMIT 1
    </select>
</mapper>
