<?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.OrgMatchMapper">
    <select id="findMatched" resultType="com.zcloud.personnel.positioning.persistence.dataobject.OrgMatchSnapshotDO">
        SELECT local_corpinfo_id, local_corpinfo_name, local_department_id,
               local_department_name, match_type
        FROM org_match
        WHERE source_config_code = #{sourceConfigCode}
          AND finds_corpinfo_name = #{findsCorpinfoName}
          AND finds_department_name = #{findsDepartmentName}
          AND match_status = 'MATCHED'
          AND (delete_enum IS NULL OR delete_enum = 'FALSE')
        LIMIT 1
    </select>

    <select id="listCorpCandidates" resultType="com.zcloud.personnel.positioning.persistence.dataobject.OrgLocalCorpDO">
        SELECT CAST(ci.corpinfo_id AS CHAR) AS corpinfo_id, ci.corp_name AS corpinfo_name,
               ci.code AS credit_code
        FROM corp_info ci
        WHERE (ci.delete_enum IS NULL OR ci.delete_enum = 'FALSE')
        <choose>
            <when test="params.corpinfoId != null and params.corpinfoId != ''">
                AND CAST(ci.corpinfo_id AS CHAR) = #{params.corpinfoId}
            </when>
            <when test="params.creditCode != null and params.creditCode != ''">
                AND ci.code = #{params.creditCode}
            </when>
            <when test="params.corpinfoName != null and params.corpinfoName != ''">
                AND ci.corp_name = #{params.corpinfoName}
            </when>
            <otherwise>
                AND ci.corp_name LIKE CONCAT('%', #{params.corpinfoNameSuffix}) ESCAPE '\\'
            </otherwise>
        </choose>
    </select>

    <select id="listDepartmentCandidates" resultType="com.zcloud.personnel.positioning.persistence.dataobject.OrgLocalDepartmentDO">
        SELECT d.id AS department_id, d.dept_name AS department_name,
               CAST(ci.corpinfo_id AS CHAR) AS root_corpinfo_id,
               ci.corp_name AS root_corpinfo_name
        FROM department d
        INNER JOIN corp_info ci ON ci.id = d.corpinfo_id
        WHERE (d.delete_enum IS NULL OR d.delete_enum = 'FALSE')
          AND (ci.delete_enum IS NULL OR ci.delete_enum = 'FALSE')
          AND d.dept_name = #{departmentName}
    </select>
    <insert id="upsert">
        INSERT INTO org_match (source_system, source_config_code, finds_corpinfo_id,
            finds_corpinfo_name, finds_department_name, local_corpinfo_id, local_corpinfo_name,
            local_department_id, local_department_name, match_status, match_type,
            last_seen_time, last_sync_time, sync_status, sync_error, delete_enum)
        VALUES ('FINDS', #{params.sourceConfigCode}, #{params.findsCorpinfoId},
            #{params.findsCorpinfoName}, #{params.findsDepartmentName},
            #{params.localCorpinfoId}, #{params.localCorpinfoName},
            #{params.localDepartmentId}, #{params.localDepartmentName},
            #{params.matchStatus}, #{params.matchType}, #{params.syncTime},
            #{params.syncTime}, 'SUCCESS', NULL, 'FALSE')
        ON DUPLICATE KEY UPDATE finds_corpinfo_id = VALUES(finds_corpinfo_id),
            local_corpinfo_id = VALUES(local_corpinfo_id), local_corpinfo_name = VALUES(local_corpinfo_name),
            local_department_id = VALUES(local_department_id), local_department_name = VALUES(local_department_name),
            match_status = VALUES(match_status), match_type = VALUES(match_type),
            last_seen_time = VALUES(last_seen_time), last_sync_time = VALUES(last_sync_time),
            sync_status = 'SUCCESS', sync_error = NULL, delete_enum = 'FALSE', version = version + 1
    </insert>
    <update id="touch">
        UPDATE org_match SET last_seen_time = #{seenTime}, version = version + 1
        WHERE source_config_code = #{sourceConfigCode}
          AND finds_corpinfo_name = #{findsCorpinfoName}
          AND finds_department_name = #{findsDepartmentName}
          AND (delete_enum IS NULL OR delete_enum = 'FALSE')
    </update>
    <select id="countAll" resultType="long">SELECT COUNT(1) FROM org_match</select>
    <select id="countMatched" resultType="long">SELECT COUNT(1) FROM org_match WHERE match_status = 'MATCHED'</select>
</mapper>
