<?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.history.HistoryDataOrderItemDao">

    <resultMap id="OrderItemMap" type="com.zm.order.tool.server.model.OrderItem">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="biz_type" jdbcType="INTEGER" property="bizType"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="item_id" jdbcType="VARCHAR" property="itemId"/>
        <result column="source_id" jdbcType="VARCHAR" property="sourceId"/>
        <result column="item_name" jdbcType="VARCHAR" property="itemName"/>
        <result column="item_image" jdbcType="VARCHAR" property="itemImage"/>
        <result column="item_spec" jdbcType="VARCHAR" property="itemSpec"/>
        <result column="item_unit" jdbcType="VARCHAR" property="itemUnit"/>
        <result column="item_barcode" jdbcType="VARCHAR" property="itemBarcode"/>
        <result column="item_sign" jdbcType="VARCHAR" property="itemSign"/>
        <result column="brand_sign" jdbcType="VARCHAR" property="brandSign"/>
        <result column="market_price" jdbcType="INTEGER" property="marketPrice"/>
        <result column="cost_price" jdbcType="INTEGER" property="costPrice"/>
        <result column="member_price" jdbcType="INTEGER" property="memberPrice"/>
        <result column="item_price" jdbcType="INTEGER" property="itemPrice"/>
        <result column="num" jdbcType="DECIMAL" property="num"/>
        <result column="discount_amount" jdbcType="INTEGER" property="discountAmount"/>
        <result column="pack_amount" jdbcType="INTEGER" property="packAmount"/>
        <result column="paid_amount" jdbcType="INTEGER" property="paidAmount"/>
        <result column="f_code" jdbcType="VARCHAR" property="fCode"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="goods_id" jdbcType="BIGINT" property="goodsId"/>
        <result column="is_time_discount" jdbcType="INTEGER" property="isTimeDiscount"/>
        <result column="activity_id" jdbcType="BIGINT" property="activityId"/>
        <result column="order_at" jdbcType="TIMESTAMP" property="orderAt"/>
        <result column="features" jdbcType="VARCHAR" property="features"/>
        <result column="seller_id" jdbcType="BIGINT" property="sellerId"/>
        <result column="product_no" jdbcType="VARCHAR" property="productNo"/>
        <result column="is_gift" jdbcType="INTEGER" property="isGift"/>
        <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="item_type" jdbcType="INTEGER" property="itemType"/>
        <result column="owner_id" jdbcType="BIGINT" property="ownerId"/>
        <result column="batching_info" jdbcType="VARCHAR" property="batchingInfo"/>
    </resultMap>

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

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

    <sql id="cols_exclude_id">
        biz_type, order_no, user_id, item_id, source_id, item_name, item_image, item_spec, item_unit, item_barcode, item_sign,
        brand_sign,  market_price, cost_price, member_price,
        item_price, num, discount_amount, pack_amount, f_code, status,  created_at, updated_at,
        goods_id, is_time_discount, activity_id, order_at,  seller_id, product_no, is_gift, features, is_deleted,
        item_type, owner_id, batching_info, paid_amount
    </sql>

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

    <select id="selectById" parameterType="map" resultMap="OrderItemMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where id = #{id}
        and user_id = #{userId}
    </select>

    <select id="selectList" parameterType="map" resultMap="OrderItemMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            user_id = #{userId}
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="orderNo != null">
                and order_no = #{orderNo}
            </if>
            <if test="orderNoList != null and orderNoList.size > 0">
                and order_no in
                <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
                    #{orderNo}
                </foreach>
            </if>
            and is_deleted = 0
            order by id asc
            <if test="start != null">
                limit ${start}, ${pageSize}
            </if>
        </where>
    </select>

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

</mapper>