<?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.orion.visor.module.infra.dao.DataExtraDAO">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.orion.visor.module.infra.entity.domain.DataExtraDO">
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="rel_id" property="relId"/>
        <result column="type" property="type"/>
        <result column="item" property="item"/>
        <result column="value" property="value"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="creator" property="creator"/>
        <result column="updater" property="updater"/>
        <result column="deleted" property="deleted"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, user_id, rel_id, type, item, value, create_time, update_time, creator, updater, deleted
    </sql>

    <delete id="deleteHostKey">
        UPDATE data_extra
        SET value = JSON_REPLACE(value,
                                 "$.keyId", NULL,
                                 "$.authType",
                                    REPLACE(
                                        IF(JSON_EXTRACT(value, "$.authType") = 'CUSTOM_KEY',
                                           'DEFAULT',
                                            JSON_EXTRACT(value, "$.authType")
                                        ),
                                        '"',
                                        ''
                                    )
                                 )
        WHERE deleted = 0
        AND type = 'HOST'
        AND item = 'ssh'
        <foreach collection="keyIdList" item="item" separator="OR" open="AND (" close=")">
            JSON_CONTAINS(value, JSON_OBJECT('keyId', #{item}))
        </foreach>
    </delete>

    <delete id="deleteHostIdentity">
        UPDATE data_extra
        SET value = JSON_REPLACE(value,
                                 "$.identityId", NULL,
                                 "$.authType",
                                    REPLACE(
                                        IF(JSON_EXTRACT(value, "$.authType") = 'CUSTOM_IDENTITY',
                                           'DEFAULT',
                                            JSON_EXTRACT(value, "$.authType")
                                        ),
                                        '"',
                                        ''
                                    )
                                 )
        WHERE deleted = 0
        AND type = 'HOST'
        AND item = 'ssh'
        <foreach collection="identityIdList" item="item" separator="OR" open="AND (" close=")">
            JSON_CONTAINS(value, JSON_OBJECT('identityId', #{item}))
        </foreach>
    </delete>

</mapper>
