<?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.zm.order.tool.server.dao.CompareRecordDao">

    <resultMap id="CompareRecordMap" type="com.zm.order.tool.server.model.CompareRecord">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="no" jdbcType="INTEGER" property="no"/>
        <result column="start_at" jdbcType="TIMESTAMP" property="startAt"/>
        <result column="end_at" jdbcType="TIMESTAMP" property="endAt"/>
        <result column="execute_at" jdbcType="TIMESTAMP" property="executeAt"/>
        <result column="db_no" jdbcType="INTEGER" property="dbNo"/>
        <result column="tb_no" jdbcType="INTEGER" property="tbNo"/>
        <result column="delay_count" jdbcType="INTEGER" property="delayCount"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="features" jdbcType="VARCHAR" property="features"/>
        <result column="created_at" jdbcType="TIMESTAMP" property="createdAt"/>
        <result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt"/>
        <result column="is_deleted" jdbcType="INTEGER" property="isDeleted"/>
    </resultMap>

    <sql id="tb">
        order_compare_records
    </sql>

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </sql>

    <sql id="cols_exclude_id">
    `no`, start_at, end_at,  execute_at, db_no, tb_no, delay_count, status, features, created_at, updated_at, is_deleted
    </sql>

    <sql id="values">
     #{no}, #{startAt}, #{endAt}, #{executeAt}, #{dbNo}, #{tbNo}, #{delayCount},
      #{status}, #{features}, now(),
      now(), 0
  </sql>


    <select id="selectLast" parameterType="map" resultMap="CompareRecordMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where is_deleted = 0
        and db_no = #{dbNo}
        and tb_no = #{tbNo}
        order by `no` desc
        limit 1
    </select>


    <insert id="insert" parameterType="com.zm.order.tool.server.model.CompareRecord" keyColumn="id" keyProperty="id"
            useGeneratedKeys="true">
        insert into
        <include refid="tb"/>
        (<include refid="cols_exclude_id"/>)
        values
        (<include refid="values"/>)
    </insert>

    <update id="update" parameterType="com.zm.order.tool.server.model.CompareRecord">
        update
        <include refid="tb"/>
        <set>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="executeAt != null">
                execute_at = #{executeAt},
            </if>
            <if test="delayCount != null">
                delay_count = #{delayCount},
            </if>
            <if test="features != null and features != ''">
                features = #{features},
            </if>
            updated_at = now()
        </set>
        where id = #{id}
    </update>
</mapper>