<?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">
    <sql id="mappedCompanyJoin">
        LEFT JOIN (
            SELECT local_corpinfo_id, MAX(local_corpinfo_name) AS local_corpinfo_name
            FROM org_match
            WHERE match_status = 'MATCHED'
              AND (delete_enum IS NULL OR delete_enum = 'FALSE')
              AND local_corpinfo_id IS NOT NULL AND local_corpinfo_id != ''
            GROUP BY local_corpinfo_id
        ) mapped_org ON mapped_org.local_corpinfo_id = CAST(ar.corpinfo_id AS CHAR)
        LEFT JOIN corp_info corp_uuid
            ON mapped_org.local_corpinfo_id IS NULL
           AND CAST(corp_uuid.corpinfo_id AS CHAR) = CAST(ar.corpinfo_id AS CHAR)
           AND (corp_uuid.delete_enum IS NULL OR corp_uuid.delete_enum = 'FALSE')
        LEFT JOIN corp_info corp_internal
            ON mapped_org.local_corpinfo_id IS NULL
           AND corp_uuid.corpinfo_id IS NULL
           AND CAST(corp_internal.id AS CHAR) = CAST(ar.corpinfo_id AS CHAR)
           AND (corp_internal.delete_enum IS NULL OR corp_internal.delete_enum = 'FALSE')
    </sql>

    <sql id="mappedCompanyId">
        COALESCE(mapped_org.local_corpinfo_id, CAST(corp_uuid.corpinfo_id AS CHAR),
                 CAST(corp_internal.corpinfo_id AS CHAR))
    </sql>

    <sql id="mappedCompanyName">
        COALESCE(mapped_org.local_corpinfo_name, corp_uuid.corp_name, corp_internal.corp_name)
    </sql>

    <sql id="alarmRecordColumns">
        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, <include refid="mappedCompanyId"/> AS corpinfo_id,
        <include refid="mappedCompanyName"/> 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
    </sql>

    <select id="listAlarmRecords"
            resultType="com.zcloud.personnel.positioning.persistence.dataobject.AlarmRecordDO">
        SELECT <include refid="alarmRecordColumns"/>
        FROM alarm_record ar
        <include refid="mappedCompanyJoin"/>
        WHERE (ar.delete_enum IS NULL OR ar.delete_enum = 'FALSE')
          AND <include refid="mappedCompanyId"/> IS NOT NULL
        <if test="params.eqCorpinfoId != null and params.eqCorpinfoId != ''">
            AND <include refid="mappedCompanyId"/> = #{params.eqCorpinfoId}
        </if>
        <if test="params.likeCorpinfoName != null and params.likeCorpinfoName != ''">
            AND <include refid="mappedCompanyName"/> LIKE CONCAT('%', #{params.likeCorpinfoName}, '%')
        </if>
        <if test="params.likeFirstStaffNameSnapshot != null and params.likeFirstStaffNameSnapshot != ''">
            AND ar.first_staff_name_snapshot LIKE CONCAT('%', #{params.likeFirstStaffNameSnapshot}, '%')
        </if>
        <if test="params.eqAlarmCode != null and params.eqAlarmCode != ''">
            AND ar.alarm_code = #{params.eqAlarmCode}
        </if>
        <if test="params.geStartTime != null">
            AND ar.start_time &gt;= #{params.geStartTime}
        </if>
        <if test="params.leStartTime != null">
            AND ar.start_time &lt;= #{params.leStartTime}
        </if>
        ORDER BY COALESCE(ar.start_time, ar.create_time) DESC, ar.id DESC
    </select>

    <select id="selectBiAlarmList"
            resultType="com.zcloud.personnel.positioning.persistence.dataobject.AlarmRecordDO">
        SELECT
            <include refid="alarmRecordColumns"/>
        FROM alarm_record ar
        <include refid="mappedCompanyJoin"/>
        WHERE (ar.delete_enum IS NULL OR ar.delete_enum = 'FALSE')
          AND <include refid="mappedCompanyId"/> IS NOT NULL
        <if test="corpinfoId != null and corpinfoId != ''">
          AND <include refid="mappedCompanyId"/> = CAST(#{corpinfoId} AS CHAR)
        </if>
        <if test="portArea != null">
          AND COALESCE(corp_uuid.port_area, corp_internal.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
        <include refid="mappedCompanyJoin"/>
        WHERE (ar.delete_enum IS NULL OR ar.delete_enum = 'FALSE')
          AND <include refid="mappedCompanyId"/> IS NOT NULL
        <if test="corpinfoId != null and corpinfoId != ''">
          AND <include refid="mappedCompanyId"/> = 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 MAX(company_name) AS label, company_id AS value
        FROM (
            SELECT local_corpinfo_name AS company_name, local_corpinfo_id AS company_id
            FROM org_match
            WHERE match_status = 'MATCHED'
              AND (delete_enum IS NULL OR delete_enum = 'FALSE')
              AND local_corpinfo_id IS NOT NULL AND local_corpinfo_id != ''
              AND local_corpinfo_name IS NOT NULL AND local_corpinfo_name != ''
            UNION ALL
            SELECT corp_name, CAST(corpinfo_id AS CHAR)
            FROM corp_info
            WHERE (delete_enum IS NULL OR delete_enum = 'FALSE')
              AND corpinfo_id IS NOT NULL AND corp_name IS NOT NULL
        ) platform_company
        WHERE 1 = 1
        <if test="params.eqCorpinfoId != null and params.eqCorpinfoId != ''">
            AND company_id = #{params.eqCorpinfoId}
        </if>
        GROUP BY company_id
        ORDER BY label ASC
        LIMIT 200
    </select>

</mapper>
