<?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.routing.RoutingOrderItemDao">

    <resultMap id="OrderItemMap" type="com.zm.order.tool.server.model.RoutingOrderItem">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="biz_type" jdbcType="INTEGER" property="bizType"/>
        <result column="item_id" jdbcType="VARCHAR" property="itemId"/>
        <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="INTEGER" property="num"/>
        <result column="discount_amount" jdbcType="INTEGER" property="discountAmount"/>
        <result column="pack_amount" jdbcType="INTEGER" property="packAmount"/>
        <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"/>
        <result column="db_index" jdbcType="INTEGER" property="dbIndex"/>
        <result column="tb_index" jdbcType="INTEGER" property="tbIndex"/>
        <result column="sync_at" jdbcType="TIMESTAMP" property="syncAt"/>
    </resultMap>

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

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

    <sql id="cols_exclude_id">
        order_no, user_id, biz_type, item_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, db_index, tb_index, sync_at
    </sql>

    <sql id="values">
      #{id}, #{orderNo}, #{userId}, #{bizType}, #{itemId}, #{itemName},
      #{itemImage}, #{itemSpec}, #{itemUnit},
      #{itemBarcode}, #{itemSign}, #{brandSign},
      #{marketPrice}, #{costPrice}, #{memberPrice},
      #{itemPrice}, #{num}, #{discountAmount}, #{packAmount},
      #{fCode}, 0,  now(), now(),  #{goodsId}, #{isTimeDiscount}, #{activityId}, #{orderAt}, #{sellerId}, #{productNo},
      #{isGift}, #{features}, 0, #{itemType}, #{ownerId}, #{batchingInfo}
  </sql>

    <select id="selectList" parameterType="map" resultMap="OrderItemMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            is_deleted = 0
            <if test="userId != null">
                and user_id = #{userId}
            </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>
            <if test="activityIdList != null and activityIdList.size > 0">
                and activity_id in
                <foreach collection="activityIdList" item="activityId" index="index" open="(" close=")" separator=",">
                    #{activityId}
                </foreach>
            </if>
            <if test="goodsId != null">
                and goods_id = #{goodsId}
            </if>
            <if test="activityId != null">
                and activity_id = #{activityId}
            </if>
            <if test="isTimeDiscount != null">
                and is_time_discount = #{isTimeDiscount}
            </if>
            <if test="itemSign != null">
                and item_sign = #{itemSign}
            </if>
            <if test="itemPrice != null">
                and item_price = #{itemPrice}
            </if>
            <if test="itemId != null">
                and item_id = #{itemId}
            </if>
        </where>
    </select>

    <select id="selectByOrderNo" parameterType="map" resultMap="OrderItemMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where
        user_id = #{userId}
        and order_no = #{orderNo}
    </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}
        and is_deleted = 0
    </select>

    <select id="selectByOrderNoList" parameterType="list" resultMap="OrderItemMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where order_no in
        <foreach collection="list" item="orderNo" index="index" open="(" close=")" separator=",">
            #{orderNo}
        </foreach>
    </select>
</mapper>