<?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.tb.TbOrderDiscountDao">

    <resultMap id="OrderDiscountMap" type="com.zm.order.tool.server.model.OrderDiscount">
        <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="biz_type" jdbcType="INTEGER" property="bizType"/>
        <result column="item_id" jdbcType="VARCHAR" property="itemId"/>
        <result column="discount_level" jdbcType="INTEGER" property="discountLevel"/>
        <result column="discount_id" jdbcType="VARCHAR" property="discountId"/>
        <result column="discount_type" jdbcType="INTEGER" property="discountType"/>
        <result column="discount_name" jdbcType="VARCHAR" property="discountName"/>
        <result column="discount_summary" jdbcType="VARCHAR" property="discountSummary"/>
        <result column="discount_amount" jdbcType="INTEGER" property="discountAmount"/>
        <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="seller_id" jdbcType="INTEGER" property="sellerId"/>
    </resultMap>

    <sql id="tb">
        orderpro.order_discounts
    </sql>

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

    <sql id="cols_exclude_id">
        user_id, order_no, biz_type, item_id, discount_level, discount_id, discount_type,
        discount_name, discount_summary, discount_amount, features,
        created_at, updated_at, is_deleted, seller_id
    </sql>

    <sql id="values">
        #{id}, #{userId}, #{orderNo}, #{bizType}, #{itemId}, #{discountLevel}, #{discountId},
        #{discountType}, #{discountName}, #{discountSummary}, #{discountAmount}, #{features},
        now(), now(), 0, #{sellerId}
    </sql>

    <select id="selectByIdList" parameterType="map" resultMap="OrderDiscountMap">
        select
        id, order_no
        from
        <include refid="tb"/>
        where id in
        <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="selectId" parameterType="map" resultType="long">
        select
        id
        from
        <include refid="tb"/>
        <where>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
            <if test="maxId != null">
                <![CDATA[ AND id < #{maxId} ]]>
            </if>
            <if test="updatedAtStart != null">
                <![CDATA[ AND updated_at >= #{updatedAtStart} ]]>
            </if>
            <if test="updatedAtEnd != null">
                <![CDATA[ AND updated_at <= #{updatedAtEnd} ]]>
            </if>
        </where>
        order by id
        limit #{size}
    </select>

    <select id="selectById" parameterType="map" resultMap="OrderDiscountMap">
        select
        features
        from
        <include refid="tb"/>
        <where>
            id in
            <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
                #{id}
            </foreach>
        </where>
    </select>
</mapper>