<?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.OrderOperationDao">

    <resultMap id="OrderOperationMap" type="com.zm.order.tool.server.model.OrderOperation">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="role" jdbcType="INTEGER" property="role"/>
        <result column="operator" jdbcType="BIGINT" property="operator"/>
        <result column="before_status" jdbcType="INTEGER" property="beforeStatus"/>
        <result column="operation" jdbcType="VARCHAR" property="operation"/>
        <result column="after_status" jdbcType="INTEGER" property="afterStatus"/>
        <result column="operate_at" jdbcType="TIMESTAMP" property="operateAt"/>
        <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"/>
        <result column="data_source_type" jdbcType="INTEGER" property="dataSourceType"/>
    </resultMap>


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

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

    <sql id="cols_exclude_id">
       user_id, order_no, role, operator, before_status, operation, after_status, operate_at,
        features, created_at, updated_at, is_deleted, data_source_type
    </sql>

    <sql id="values">
        #{id}, #{userId}, #{orderNo}, #{role}, #{operator},
        #{beforeStatus}, #{operation}, #{afterStatus}, #{operateAt},
        #{features}, now(), now(), 0, #{dataSourceType}
  </sql>



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

    <insert id="insertList" parameterType="list" >
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        <foreach collection="list" item="record" index="index" separator=",">
            (
            #{record.id}, #{record.userId}, #{record.orderNo}, #{record.role}, #{record.operator},
            #{record.beforeStatus}, #{record.operation}, #{record.afterStatus}, #{record.operateAt},
            #{record.features}, now(), now(), 0, #{record.dataSourceType}
            )
        </foreach>
    </insert>


    <select id="selectList" parameterType="map" resultMap="OrderOperationMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where order_no = #{orderNo} and is_deleted = 0
    </select>

    <select id="selectByOrderNo" parameterType="map" resultMap="OrderOperationMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where
        order_no = #{orderNo}
        and is_deleted = 0
        order by id desc
    </select>

    <select id="selectIdList" parameterType="map" resultType="long">
        select
        id
        from
        <include refid="tb"/>
        <where>
            <![CDATA[ AND updated_at <= #{updatedAtEnd} ]]>
        </where>
        limit ${size}
    </select>

    <update id="realDelete" parameterType="map">
        delete from
        <include refid="tb"/>
        where
            id = #{id} and order_no = #{orderNo}
    </update>
</mapper>