<?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.common.mapper.DmNormalizeRuleMapper">

    <resultMap id="BaseResultMap" type="com.common.entity.DmNormalizeRule">
        <id column="id" property="id" />
        <result column="created_at" property="createdAt" />
        <result column="updated_at" property="updatedAt" />
        <result column="deleted_at" property="deletedAt" />
        <result column="name" property="name" />
        <result column="display_name" property="displayName" />
        <result column="description" property="description" />
        <result column="is_built_in" property="isBuiltIn" />
        <result column="is_running" property="isRunning" />
        <result column="first_data_saved_at" property="firstDataSavedAt" />
        <result column="data_type" property="dataType" />
        <result column="field_cate_id" property="fieldCateId" />
        <result column="log_parsed" property="logParsed" />
        <result column="sample_logs" property="sampleLogs" typeHandler="org.apache.ibatis.type.ArrayTypeHandler" />
        <result column="is_data_merge_enabled" property="isDataMergeEnabled" />
        <result column="data_merge_interval" property="dataMergeInterval" />
        <result column="data_merge_time_unit" property="dataMergeTimeUnit" />
        <result column="data_merge_row_limit" property="dataMergeRowLimit" />
        <result column="data_merge_columns" property="dataMergeColumns" />
        <result column="data_storage_medium" property="dataStorageMedium" />
        <result column="created_by_id" property="createdById" />
        <result column="group_id" property="groupId" />
        <result column="rule_content" property="ruleContent" />
        <result column="built_in_version" property="builtInVersion" />
        <result column="tenant_id" property="tenantId" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="create_by" property="createBy" />
        <result column="update_by" property="updateBy" />
        <result column="del_flag" property="delFlag" />
        <result column="create_dept" property="createDept" />
    </resultMap>

    <sql id="Base_Column_List">
        id, created_at, updated_at, deleted_at, name, display_name, description,
        is_built_in, is_running, first_data_saved_at, data_type, field_cate_id,
        log_parsed, sample_logs, is_data_merge_enabled, data_merge_interval,
        data_merge_time_unit, data_merge_row_limit, data_merge_columns,
        data_storage_medium, created_by_id, group_id, rule_content,
        built_in_version, tenant_id, create_time, update_time, create_by,
        update_by, del_flag, create_dept
    </sql>

    <!-- 根据ID查询 -->
    <select id="selectById" parameterType="java.lang.Long" resultMap="BaseResultMap">

        SELECT
        id,
        created_at::timestamp as created_at,  <!-- 转换为 timestamp -->
        updated_at::timestamp as updated_at,
        deleted_at::timestamp as deleted_at,
        first_data_saved_at::timestamp as first_data_saved_at,
        create_time::timestamp as create_time,
        update_time::timestamp as update_time,
        name, display_name, description, is_built_in, is_running,
        data_type, field_cate_id, log_parsed, sample_logs,
        is_data_merge_enabled, data_merge_interval, data_merge_time_unit,
        data_merge_row_limit, data_merge_columns, data_storage_medium,
        created_by_id, group_id, rule_content, built_in_version, tenant_id,
        create_by, update_by, del_flag, create_dept
        FROM dm_normalize_rule
        WHERE id = #{id} AND del_flag = '0'

    </select>


    <!-- 根据ID查询 -->
    <select id="selectByDeviceId" parameterType="java.lang.Long" resultType="java.util.LinkedHashMap">

        SELECT
        id,
        created_at::timestamp as created_at,  <!-- 转换为 timestamp -->
        updated_at::timestamp as updated_at,
        deleted_at::timestamp as deleted_at,
        first_data_saved_at::timestamp as first_data_saved_at,
        create_time::timestamp as create_time,
        update_time::timestamp as update_time,
        name, display_name, description, is_built_in, is_running,
        data_type, field_cate_id, log_parsed, sample_logs,
        is_data_merge_enabled, data_merge_interval, data_merge_time_unit,
        data_merge_row_limit, data_merge_columns, data_storage_medium,
        created_by_id, group_id, rule_content, built_in_version, tenant_id,
        create_by, update_by, del_flag, create_dept
        FROM dm_normalize_rule
        WHERE id  in ( select  normalize_rule_id   from dm_normalize_rule_device  where device_id =#{id})  AND del_flag = '0'
    </select>


    <!-- 根据ID和租户ID查询 -->
    <select id="selectByIdAndTenant" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List" />
        FROM dm_normalize_rule
        WHERE id = #{id} AND tenant_id = #{tenantId} AND del_flag = '0'
    </select>

    <!-- 根据ID更新 -->
    <update id="updateById" parameterType="com.common.entity.DmNormalizeRule">
        UPDATE dm_normalize_rule
        <set>
            <if test="updatedAt != null">updated_at = #{updatedAt},</if>
            <if test="name != null">name = #{name},</if>
            <if test="displayName != null">display_name = #{displayName},</if>
            <if test="description != null">description = #{description},</if>
            <if test="isBuiltIn != null">is_built_in = #{isBuiltIn},</if>
            <if test="isRunning != null">is_running = #{isRunning},</if>
            <if test="firstDataSavedAt != null">first_data_saved_at = #{firstDataSavedAt},</if>
            <if test="dataType != null">data_type = #{dataType},</if>
            <if test="fieldCateId != null">field_cate_id = #{fieldCateId},</if>
            <if test="logParsed != null">log_parsed = #{logParsed},</if>
            <if test="sampleLogs != null">sample_logs = #{sampleLogs, typeHandler=org.apache.ibatis.type.ArrayTypeHandler},</if>
            <if test="isDataMergeEnabled != null">is_data_merge_enabled = #{isDataMergeEnabled},</if>
            <if test="dataMergeInterval != null">data_merge_interval = #{dataMergeInterval},</if>
            <if test="dataMergeTimeUnit != null">data_merge_time_unit = #{dataMergeTimeUnit},</if>
            <if test="dataMergeRowLimit != null">data_merge_row_limit = #{dataMergeRowLimit},</if>
            <if test="dataMergeColumns != null">data_merge_columns = #{dataMergeColumns},</if>
            <if test="dataStorageMedium != null">data_storage_medium = #{dataStorageMedium},</if>
            <if test="ruleContent != null">rule_content = #{ruleContent},</if>
            <if test="builtInVersion != null">built_in_version = #{builtInVersion},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </set>
        WHERE id = #{id} AND del_flag = '0'
    </update>

    <!-- 根据ID和租户ID更新 -->
    <update id="updateByIdAndTenant" parameterType="com.common.entity.DmNormalizeRule">
        UPDATE dm_normalize_rule
        <set>
            <if test="updatedAt != null">updated_at = #{updatedAt},</if>
            <if test="name != null">name = #{name},</if>
            <if test="displayName != null">display_name = #{displayName},</if>
            <if test="description != null">description = #{description},</if>
            <if test="isBuiltIn != null">is_built_in = #{isBuiltIn},</if>
            <if test="isRunning != null">is_running = #{isRunning},</if>
            <if test="firstDataSavedAt != null">first_data_saved_at = #{firstDataSavedAt},</if>
            <if test="dataType != null">data_type = #{dataType},</if>
            <if test="fieldCateId != null">field_cate_id = #{fieldCateId},</if>
            <if test="logParsed != null">log_parsed = #{logParsed},</if>
            <if test="sampleLogs != null">sample_logs = #{sampleLogs, typeHandler=org.apache.ibatis.type.ArrayTypeHandler},</if>
            <if test="isDataMergeEnabled != null">is_data_merge_enabled = #{isDataMergeEnabled},</if>
            <if test="dataMergeInterval != null">data_merge_interval = #{dataMergeInterval},</if>
            <if test="dataMergeTimeUnit != null">data_merge_time_unit = #{dataMergeTimeUnit},</if>
            <if test="dataMergeRowLimit != null">data_merge_row_limit = #{dataMergeRowLimit},</if>
            <if test="dataMergeColumns != null">data_merge_columns = #{dataMergeColumns},</if>
            <if test="dataStorageMedium != null">data_storage_medium = #{dataStorageMedium},</if>
            <if test="ruleContent != null">rule_content = #{ruleContent},</if>
            <if test="builtInVersion != null">built_in_version = #{builtInVersion},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </set>
        WHERE id = #{id} AND tenant_id = #{tenantId} AND del_flag = '0'
    </update>

</mapper>