<?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.ruoyi.excel.wecom.mapper.CustomerDataChangeLogMapper">

    <!-- 根据客户ID查询所有变更日志（按时间倒序） -->
    <select id="selectChangeLogByCustomerId" resultType="com.ruoyi.excel.wecom.domain.CustomerDataChangeLog">
        SELECT *
        FROM customer_data_change_log
        WHERE customer_id = #{customerId}
        ORDER BY change_time DESC
    </select>

    <!-- 根据历史记录ID查询变更日志 -->
    <select id="selectChangeLogByHistoryId" resultType="com.ruoyi.excel.wecom.domain.CustomerDataChangeLog">
        SELECT *
        FROM customer_data_change_log
        WHERE history_id = #{historyId}
        ORDER BY log_id
    </select>

    <!-- 根据客户ID和版本号查询变更日志 -->
    <select id="selectChangeLogByCustomerIdAndVersion" resultType="com.ruoyi.excel.wecom.domain.CustomerDataChangeLog">
        SELECT *
        FROM customer_data_change_log
        WHERE customer_id = #{customerId} AND version = #{version}
        ORDER BY log_id
    </select>

    <!-- 批量插入变更日志 -->
    <insert id="batchInsert" parameterType="java.util.List">
        INSERT INTO customer_data_change_log (
                                              corp_id,
            history_id, customer_id, version, field_name, field_label,
            old_value, new_value, change_time
        ) VALUES
        <foreach collection="changeLogs" item="item" separator=",">
            (#{item.corpId},
                #{item.historyId}, #{item.customerId}, #{item.version},
                #{item.fieldName}, #{item.fieldLabel}, #{item.oldValue},
                #{item.newValue}, #{item.changeTime}
            )
        </foreach>
    </insert>

</mapper>
