<?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.FenceMapper">
    <select id="listCompanyStats"
            resultType="com.zcloud.personnel.positioning.persistence.dataobject.FenceCompanyStatDO">
        SELECT MIN(corpinfo_id) AS corpinfo_id,
               COALESCE(MAX(NULLIF(corpinfo_name, '')), 'UNKNOWN') AS corpinfo_name,
               COUNT(1) AS fence_count,
               SUM(CASE WHEN status = 'ENABLED' THEN 1 ELSE 0 END) AS enabled_count,
               COALESCE(MAX(last_sync_time), MAX(update_time)) AS last_sync_time
        FROM fence
        WHERE delete_enum = 'FALSE'
          AND fence_scope IN ('ELECTRONIC', 'WORK_AREA')
        <if test="params.eqCorpinfoId != null and params.eqCorpinfoId != ''">
            AND corpinfo_id = #{params.eqCorpinfoId}
        </if>
        <if test="params.likeCorpinfoName != null and params.likeCorpinfoName != ''">
            AND corpinfo_name LIKE CONCAT('%', #{params.likeCorpinfoName}, '%')
        </if>
        GROUP BY COALESCE(CAST(corpinfo_id AS CHAR), NULLIF(corpinfo_name, ''), 'UNKNOWN')
        ORDER BY COALESCE(MAX(last_sync_time), MAX(update_time)) DESC
    </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>
        <if test="params.keyword != null and params.keyword != ''">
            AND (corp_name LIKE CONCAT('%', #{params.keyword}, '%')
                OR code LIKE CONCAT('%', #{params.keyword}, '%')
                OR contacts LIKE CONCAT('%', #{params.keyword}, '%')
                OR lr_name LIKE CONCAT('%', #{params.keyword}, '%'))
        </if>
        ORDER BY corp_name ASC
        LIMIT 200
    </select>

    <update id="clearAreaLink">
        UPDATE fence SET area_id = NULL, area_name = NULL WHERE area_id = #{areaId}
    </update>

    <select id="findCorpInfo" resultType="com.zcloud.personnel.positioning.persistence.dataobject.CorpInfoSnapshotDO">
        SELECT CAST(corpinfo_id AS CHAR) AS corpinfo_id, corp_name, code AS credit_code
        FROM corp_info
        WHERE (delete_enum IS NULL OR delete_enum = 'FALSE')
        <choose>
            <when test="params.corpinfoId != null">AND corpinfo_id = #{params.corpinfoId}</when>
            <when test="params.creditCode != null and params.creditCode != ''">AND code = #{params.creditCode}</when>
            <otherwise>AND corp_name = #{params.corpName}</otherwise>
        </choose>
        LIMIT 1
    </select>
</mapper>
