<?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.PersonnelPositioningBiMapper">
    <sql id="corpInfoSelect">
        SELECT corpinfo_id, corp_name, code, address, address_business, address_office, contacts, lr_name,
               corp_type_name, corp_type2_name, corp_type3_name, corp_type4_name,
               longitude, latitude, port_area
        FROM corp_info WHERE (delete_enum IS NULL OR delete_enum = 'FALSE')
    </sql>
    <sql id="openAlarmWhere">
        (delete_enum IS NULL OR delete_enum = 'FALSE')
        AND (dispose_status IS NULL OR dispose_status &lt;&gt; 'CLOSE')
    </sql>
    <select id="listCorpInfos" resultType="com.zcloud.personnel.positioning.persistence.dataobject.BiCorpInfoDO">
        <include refid="corpInfoSelect"/>
        <if test="portArea != null">AND port_area = #{portArea}</if>
        ORDER BY corp_name, corpinfo_id
    </select>
    <select id="listPersonPositionCompanies" resultType="com.zcloud.personnel.positioning.persistence.dataobject.BiCorpInfoDO">
        <include refid="corpInfoSelect"/>
        <if test="corpinfoId != null and corpinfoId != ''">
            AND CAST(corpinfo_id AS CHAR) = CAST(#{corpinfoId} AS CHAR)
        </if>
        <if test="portArea != null">
            AND port_area = #{portArea}
        </if>
        ORDER BY corp_order ASC, corp_name ASC, corpinfo_id ASC
    </select>
    <select id="listAreaSnapshots" resultType="com.zcloud.personnel.positioning.persistence.dataobject.BiAreaSnapshotDO">
        SELECT id, area_code, area_name, area_type, parent_id, parent_code,
               corpinfo_id, corpinfo_name, center_lon, center_lat, polygon_json
        FROM area WHERE (delete_enum IS NULL OR delete_enum = 'FALSE')
    </select>
    <select id="listCompanyAlarmCounts" resultType="com.zcloud.personnel.positioning.persistence.dataobject.BiCompanyAlarmCountDO">
        SELECT corpinfo_id, corpinfo_name, COUNT(1) AS alarm_count
        FROM alarm_record WHERE <include refid="openAlarmWhere"/>
        GROUP BY corpinfo_id, corpinfo_name
    </select>
    <select id="countOpenAlarms" resultType="int">
        SELECT COUNT(1) FROM alarm_record WHERE <include refid="openAlarmWhere"/>
    </select>
    <select id="countOpenAlarmPersons" resultType="int">
        SELECT COUNT(DISTINCT first_id_card_no) FROM alarm_record
        WHERE <include refid="openAlarmWhere"/>
          AND first_id_card_no IS NOT NULL AND first_id_card_no &lt;&gt; ''
    </select>
    <select id="countOpenAlarmsByPerson" resultType="int">
        SELECT COUNT(1) FROM alarm_record WHERE <include refid="openAlarmWhere"/>
          AND (
            <if test="idCardNo != null and idCardNo != ''">first_id_card_no = #{idCardNo}</if>
            <if test="idCardNo != null and idCardNo != '' and terminalNo != null and terminalNo != ''">OR</if>
            <if test="terminalNo != null and terminalNo != ''">
                CAST(terminal_no_list_json AS CHAR) LIKE CONCAT('%', #{terminalNo}, '%')
            </if>
          )
    </select>
    <select id="findPositionMode" resultType="string">
        SELECT device_type FROM terminal
        WHERE terminal_no = #{terminalNo} AND (delete_enum IS NULL OR delete_enum = 'FALSE')
        ORDER BY update_time DESC LIMIT 1
    </select>
    <select id="findCorpById" resultType="com.zcloud.personnel.positioning.persistence.dataobject.BiCorpInfoDO">
        <include refid="corpInfoSelect"/> AND CAST(corpinfo_id AS CHAR) = CAST(#{corpinfoId} AS CHAR) LIMIT 1
    </select>
    <select id="findCorpByCreditCode" resultType="com.zcloud.personnel.positioning.persistence.dataobject.BiCorpInfoDO">
        <include refid="corpInfoSelect"/> AND code = #{creditCode} LIMIT 1
    </select>
    <select id="findCorpByName" resultType="com.zcloud.personnel.positioning.persistence.dataobject.BiCorpInfoDO">
        <include refid="corpInfoSelect"/> AND corp_name = #{corpName} LIMIT 1
    </select>
    <select id="findCorpFuzzy" resultType="com.zcloud.personnel.positioning.persistence.dataobject.BiCorpInfoDO">
        <include refid="corpInfoSelect"/>
        AND (corp_name LIKE CONCAT('%', #{corpName}, '%') OR #{corpName} LIKE CONCAT('%', corp_name, '%'))
        ORDER BY CHAR_LENGTH(corp_name) DESC LIMIT 1
    </select>
</mapper>
