<?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="listCorpsByNames" resultType="com.zcloud.personnel.positioning.persistence.dataobject.OrgLocalCorpDO">
        SELECT ci.corpinfo_id, ci.corp_name AS corpinfo_name, ci.code AS credit_code,
               td.dept_id AS corp_department_id
        FROM corp_info ci
        LEFT JOIN (
            SELECT dept_name, MAX(id) AS dept_id FROM department
            WHERE dept_type_enum = 'TENANT' AND (delete_enum IS NULL OR delete_enum = 'FALSE')
            GROUP BY dept_name HAVING COUNT(1) = 1
        ) td ON td.dept_name = ci.corp_name
        WHERE (ci.delete_enum IS NULL OR ci.delete_enum = 'FALSE')
          AND ci.corp_name IN
          <foreach collection="values" item="value" open="(" separator="," close=")">#{value}</foreach>
    </select>
    <select id="findChildDepartment" resultType="com.zcloud.personnel.positioning.persistence.dataobject.OrgLocalDepartmentDO">
        SELECT d.id AS department_id, d.dept_name AS department_name,
               SUBSTRING_INDEX(SUBSTRING_INDEX(d.parent_ids, ',', 3), ',', -1) AS root_corpinfo_id,
               ci.corp_name AS root_corpinfo_name
        FROM department d
        LEFT JOIN corp_info ci ON CAST(ci.corpinfo_id AS CHAR) =
            SUBSTRING_INDEX(SUBSTRING_INDEX(d.parent_ids, ',', 3), ',', -1)
        WHERE (d.delete_enum IS NULL OR d.delete_enum = 'FALSE')
          AND CAST(d.parent_id AS CHAR) = #{parentId} AND d.dept_name = #{childName}
        LIMIT 1
    </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', #{sourceConfigCode}, NULL, #{findsCorpinfoName}, #{findsDepartmentName},
            #{localCorpinfoId}, #{localCorpinfoName}, #{localDepartmentId}, #{localDepartmentName},
            #{matchStatus}, #{matchType}, #{syncTime}, #{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="disableMissing">
        UPDATE org_match SET match_status = 'DISABLED', sync_status = 'SUCCESS',
            last_sync_time = #{syncTime}, version = version + 1
        WHERE source_config_code = #{sourceConfigCode}
          AND (delete_enum IS NULL OR delete_enum = 'FALSE')
          AND (last_seen_time IS NULL OR last_seen_time &lt; #{syncTime})
    </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>
