<?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.AlarmRuleMapper">
    <sql id="mappedCompanyJoin">
        LEFT JOIN (
            SELECT rel.rule_id, MIN(CAST(f.corpinfo_id AS CHAR)) AS corpinfo_id
            FROM alarm_rule_fence_rel rel
            INNER JOIN fence f ON f.id = rel.fence_id
            WHERE (rel.delete_enum IS NULL OR rel.delete_enum = 'FALSE')
              AND (f.delete_enum IS NULL OR f.delete_enum = 'FALSE')
              AND f.corpinfo_id IS NOT NULL AND f.corpinfo_id != '' AND f.corpinfo_id != '0'
            GROUP BY rel.rule_id
            HAVING COUNT(DISTINCT f.corpinfo_id) = 1
        ) rule_fence ON rule_fence.rule_id = ar.id
        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 =
            COALESCE(NULLIF(NULLIF(CAST(ar.corpinfo_id AS CHAR), ''), '0'), rule_fence.corpinfo_id)
        LEFT JOIN corp_info corp_uuid
            ON mapped_org.local_corpinfo_id IS NULL
           AND CAST(corp_uuid.corpinfo_id AS CHAR) =
               COALESCE(NULLIF(NULLIF(CAST(ar.corpinfo_id AS CHAR), ''), '0'), rule_fence.corpinfo_id)
           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) =
               COALESCE(NULLIF(NULLIF(CAST(ar.corpinfo_id AS CHAR), ''), '0'), rule_fence.corpinfo_id)
           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>

    <select id="listRules"
            resultType="com.zcloud.personnel.positioning.persistence.dataobject.AlarmRuleDO">
        SELECT ar.id, ar.rule_code, ar.rule_name, ar.alarm_attr, ar.alarm_code,
               ar.alarm_name, ar.enable, ar.rule_status,
               <include refid="mappedCompanyId"/> AS corpinfo_id,
               <include refid="mappedCompanyName"/> AS corpinfo_name,
               ar.org_code, ar.org_name, ar.fence_effect_strategy, ar.track_types,
               ar.crash_type, ar.finds_rule_id, ar.sync_status, ar.sync_token,
               ar.last_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_rule 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.eqId != null">AND ar.id = #{params.eqId}</if>
        <if test="params.eqRuleCode != null and params.eqRuleCode != ''">
            AND ar.rule_code = #{params.eqRuleCode}
        </if>
        <if test="params.eqCorpinfoId != null and params.eqCorpinfoId != ''">
            AND <include refid="mappedCompanyId"/> = #{params.eqCorpinfoId}
        </if>
        <if test="params.eqCorpinfoName != null and params.eqCorpinfoName != ''">
            AND <include refid="mappedCompanyName"/> = #{params.eqCorpinfoName}
        </if>
        <if test="params.likeCorpinfoName != null and params.likeCorpinfoName != ''">
            AND <include refid="mappedCompanyName"/> LIKE CONCAT('%', #{params.likeCorpinfoName}, '%')
        </if>
        <if test="params.likeRuleName != null and params.likeRuleName != ''">
            AND ar.rule_name LIKE CONCAT('%', #{params.likeRuleName}, '%')
        </if>
        <if test="params.eqAlarmAttr != null and params.eqAlarmAttr != ''">
            AND ar.alarm_attr = #{params.eqAlarmAttr}
        </if>
        <if test="params.eqAlarmCode != null and params.eqAlarmCode != ''">
            AND ar.alarm_code = #{params.eqAlarmCode}
        </if>
        <if test="params.eqRuleStatus != null and params.eqRuleStatus != ''">
            AND ar.rule_status = #{params.eqRuleStatus}
        </if>
        <if test="params.eqEnable != null">
            AND ar.enable = #{params.eqEnable}
        </if>
        <if test="params.eqOrgCode != null and params.eqOrgCode != ''">
            AND ar.org_code = #{params.eqOrgCode}
        </if>
        <if test="params.likeOrgName != null and params.likeOrgName != ''">
            AND ar.org_name LIKE CONCAT('%', #{params.likeOrgName}, '%')
        </if>
        <if test="params.eqFenceEffectStrategy != null and params.eqFenceEffectStrategy != ''">
            AND ar.fence_effect_strategy = #{params.eqFenceEffectStrategy}
        </if>
        <if test="params.eqTrackTypes != null and params.eqTrackTypes != ''">
            AND ar.track_types = #{params.eqTrackTypes}
        </if>
        <if test="params.eqCrashType != null and params.eqCrashType != ''">
            AND ar.crash_type = #{params.eqCrashType}
        </if>
        <if test="params.eqFindsRuleId != null">AND ar.finds_rule_id = #{params.eqFindsRuleId}</if>
        <if test="params.eqSyncStatus != null and params.eqSyncStatus != ''">
            AND ar.sync_status = #{params.eqSyncStatus}
        </if>
        <if test="params.geLastSyncTime != null">AND ar.last_sync_time &gt;= #{params.geLastSyncTime}</if>
        <if test="params.leLastSyncTime != null">AND ar.last_sync_time &lt;= #{params.leLastSyncTime}</if>
        ORDER BY ar.create_time DESC, ar.id DESC
    </select>

    <select id="listCompanyStats"
            resultType="com.zcloud.personnel.positioning.persistence.dataobject.AlarmRuleCompanyStatDO">
        SELECT <include refid="mappedCompanyId"/> AS corpinfo_id,
               <include refid="mappedCompanyName"/> AS corpinfo_name,
               COUNT(1) AS config_count,
               SUM(CASE WHEN ar.enable = 1 OR ar.rule_status = 'ENABLED' THEN 1 ELSE 0 END) AS enabled_count,
               COALESCE(MAX(ar.last_sync_time), MAX(ar.update_time)) AS last_sync_time
        FROM alarm_rule 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.companyGroupOrgCode != null and params.companyGroupOrgCode != ''">
                AND ar.org_code = #{params.companyGroupOrgCode}
            </if>
            <if test="params.likeCorpinfoName != null and params.likeCorpinfoName != ''">
                AND <include refid="mappedCompanyName"/> LIKE CONCAT('%', #{params.likeCorpinfoName}, '%')
            </if>
            <if test="params.likeContactName != null and params.likeContactName != ''">
                AND (COALESCE(corp_uuid.contacts, corp_internal.contacts) LIKE CONCAT('%', #{params.likeContactName}, '%')
                    OR COALESCE(corp_uuid.lr_name, corp_internal.lr_name) LIKE CONCAT('%', #{params.likeContactName}, '%'))
            </if>
            <if test="params.eqRuleStatus != null and params.eqRuleStatus != ''">
                AND ar.rule_status = #{params.eqRuleStatus}
            </if>
            <if test="params.eqEnable != null">
                AND ar.enable = #{params.eqEnable}
            </if>
        GROUP BY <include refid="mappedCompanyId"/>, <include refid="mappedCompanyName"/>
        ORDER BY COALESCE(MAX(ar.last_sync_time), MAX(ar.update_time)) DESC
    </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,
                   NULL AS credit_code, NULL AS contacts, NULL AS legal_representative
            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), code, contacts, lr_name
            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>
        <if test="params.keyword != null and params.keyword != ''">
            AND (company_name LIKE CONCAT('%', #{params.keyword}, '%')
                OR credit_code LIKE CONCAT('%', #{params.keyword}, '%')
                OR contacts LIKE CONCAT('%', #{params.keyword}, '%')
                OR legal_representative LIKE CONCAT('%', #{params.keyword}, '%'))
        </if>
        GROUP BY company_id
        ORDER BY label ASC
        LIMIT 200
    </select>
</mapper>
