<?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.FindsSyncStateMapper">
    <sql id="tokenCondition">
        <choose><when test="params.token != null">sync_token = #{params.token}</when><otherwise>sync_token IS NULL</otherwise></choose>
    </sql>
    <sql id="pendingCondition">
        sync_status IN
        <choose>
            <when test="params.deleting">('DELETE_WAIT', 'DELETE_FAIL')</when>
            <otherwise>('WAIT', 'FAIL')</otherwise>
        </choose>
        AND <include refid="tokenCondition"/>
    </sql>
    <update id="complete">
        <choose>
            <when test="entityType == 'FENCE'">
                UPDATE fence SET finds_fence_id = #{params.findsId}, finds_fence_code = #{params.findsCode},
                    sync_status = 'SUCCESS', last_sync_time = #{params.syncTime}, raw_json = #{params.rawJson}
                WHERE id = #{params.id} AND <include refid="pendingCondition"/>
            </when>
            <when test="entityType == 'ALARM_RULE'">
                UPDATE alarm_rule SET finds_rule_id = #{params.findsId}, sync_status = 'SUCCESS',
                    last_sync_time = #{params.syncTime}, raw_json = #{params.rawJson}
                WHERE id = #{params.id} AND <include refid="pendingCondition"/>
            </when>
            <when test="entityType == 'TERMINAL'">
                UPDATE terminal SET finds_terminal_id = #{params.findsId}, sync_status = 'SUCCESS',
                    last_sync_time = #{params.syncTime}, raw_json = #{params.rawJson}
                WHERE id = #{params.id} AND <include refid="pendingCondition"/>
            </when>
            <otherwise>
                UPDATE terminal_bind SET finds_terminal_id = #{params.findsId}, sync_status = 'SUCCESS',
                    last_sync_time = #{params.syncTime}
                WHERE id = #{params.id} AND <include refid="pendingCondition"/>
            </otherwise>
        </choose>
    </update>
    <update id="fail">
        <choose>
            <when test="entityType == 'FENCE'">UPDATE fence</when>
            <when test="entityType == 'ALARM_RULE'">UPDATE alarm_rule</when>
            <when test="entityType == 'TERMINAL'">UPDATE terminal</when>
            <otherwise>UPDATE terminal_bind</otherwise>
        </choose>
        SET sync_status = #{params.status}, last_sync_time = #{params.syncTime}
        <if test="params.rawJson != null">, raw_json = #{params.rawJson}</if>
        WHERE id = #{params.id} AND <include refid="pendingCondition"/>
    </update>
    <update id="recordMapping">
        <choose>
            <when test="entityType == 'FENCE'">
                UPDATE fence SET finds_fence_id = COALESCE(finds_fence_id, #{params.findsId}),
                    finds_fence_code = COALESCE(finds_fence_code, #{params.findsCode}) WHERE id = #{params.id}
            </when>
            <when test="entityType == 'ALARM_RULE'">
                UPDATE alarm_rule SET finds_rule_id = COALESCE(finds_rule_id, #{params.findsId}) WHERE id = #{params.id}
            </when>
            <otherwise>
                UPDATE terminal SET finds_terminal_id = COALESCE(finds_terminal_id, #{params.findsId}) WHERE id = #{params.id}
            </otherwise>
        </choose>
    </update>
    <update id="clearMapping">
        <choose>
            <when test="entityType == 'FENCE'">
                UPDATE fence SET finds_fence_id = NULL, finds_fence_code = NULL
                WHERE id = #{params.id} AND finds_fence_id = #{params.findsId}
            </when>
            <when test="entityType == 'ALARM_RULE'">
                UPDATE alarm_rule SET finds_rule_id = NULL
                WHERE id = #{params.id} AND finds_rule_id = #{params.findsId}
            </when>
            <otherwise>
                UPDATE terminal SET finds_terminal_id = NULL
                WHERE id = #{params.id} AND finds_terminal_id = #{params.findsId}
            </otherwise>
        </choose>
        AND sync_status IN ('WAIT', 'FAIL') AND <include refid="tokenCondition"/>
    </update>
    <select id="countCurrent" resultType="long">
        SELECT COUNT(1) FROM
        <choose>
            <when test="entityType == 'FENCE'">fence</when>
            <when test="entityType == 'ALARM_RULE'">alarm_rule</when>
            <otherwise>terminal</otherwise>
        </choose>
        WHERE id = #{params.id} AND sync_status IN ('WAIT', 'FAIL') AND <include refid="tokenCondition"/>
    </select>
    <delete id="deleteCurrent">
        DELETE FROM
        <choose>
            <when test="entityType == 'FENCE'">fence</when>
            <when test="entityType == 'ALARM_RULE'">alarm_rule</when>
            <otherwise>terminal</otherwise>
        </choose>
        WHERE id = #{params.id} AND sync_status IN ('DELETE_WAIT', 'DELETE_FAIL')
          AND <include refid="tokenCondition"/>
    </delete>
    <update id="recordAreaFenceMapping">
        UPDATE area SET bind_fence_id = #{params.fenceId}, finds_fence_id = #{params.findsFenceId}
        WHERE id = #{params.areaId}
    </update>
</mapper>
