<?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.newdatasource.NewDataSourceOrderItemDao">

    <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="values">
      #{id}, #{bizType}, #{orderNo}, #{userId}, #{itemId}, #{sourceId}, #{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>

    <insert id="inserts" parameterType="list">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        <foreach collection="itemList" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.bizType}, #{item.orderNo}, #{item.userId}, #{item.itemId},#{item.sourceId},
            #{item.itemName},
            #{item.itemImage}, #{item.itemSpec}, #{item.itemUnit},
            #{item.itemBarcode}, #{item.itemSign}, #{item.brandSign},
            #{item.marketPrice}, #{item.costPrice}, #{item.memberPrice},
            #{item.itemPrice}, #{item.num}, #{item.discountAmount}, #{item.packAmount},
            #{item.fCode}, #{item.status},
            now(), now(), #{item.goodsId}, #{item.isTimeDiscount}, #{item.activityId} ,#{item.orderAt},
            #{item.sellerId}, #{item.productNo}, #{item.isGift}, #{item.features}, 0, #{item.itemType}, #{item.ownerId},
            #{item.batchingInfo}, #{item.paidAmount}
            )
        </foreach>
    </insert>

    <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>

    <insert id="insertList" parameterType="list">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        <foreach collection="itemList" item="item" index="index" separator=",">
            (
            #{item.id}, #{item.bizType}, #{item.orderNo}, #{item.userId}, #{item.itemId},#{item.sourceId},
            #{item.itemName},
            #{item.itemImage}, #{item.itemSpec}, #{item.itemUnit},
            #{item.itemBarcode}, #{item.itemSign}, #{item.brandSign},
            #{item.marketPrice}, #{item.costPrice}, #{item.memberPrice},
            #{item.itemPrice}, #{item.num}, #{item.discountAmount}, #{item.packAmount},
            #{item.fCode}, #{item.status},
            #{item.createdAt}, now(), #{item.goodsId}, #{item.isTimeDiscount}, #{item.activityId} ,#{item.orderAt},
            #{item.sellerId}, #{item.productNo}, #{item.isGift}, #{item.features}, #{item.isDeleted}, #{item.itemType}, #{item.ownerId},
            #{item.batchingInfo}, #{item.paidAmount}
            )
        </foreach>
    </insert>


    <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>
            <choose>
                <when test="userIdList != null and userIdList.size > 0">
                    AND user_id in
                    <foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
                        #{userId}
                    </foreach>
                </when>
                <otherwise>
                    AND user_id = #{userId}
                </otherwise>
            </choose>
            <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="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>

    <update id="update" parameterType="com.zm.order.tool.server.model.OrderItem">
        update
        <include refid="tb"/>
        <set>
            <if test="goodsId != null">
                goods_id = #{goodsId},
            </if>
            <if test="discountAmount != null">
                discount_amount = #{discountAmount},
            </if>
            <if test="features != null and features != ''">
                features = #{features},
            </if>
            <if test="itemSign != null">
                item_sign = #{itemSign},
            </if>
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updateUpdated" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            updated_at = now()
        </set>
        where id = #{id} and user_id = #{userId}
    </update>

    <update id="updateItemPrice" parameterType="com.zm.order.tool.server.model.OrderItem">
        update
        <include refid="tb"/>
        <set>
            <if test="itemName != null">
                item_name = #{itemName},
            </if>
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updateItemSpec" parameterType="com.zm.order.tool.server.model.OrderItem">
        update
        <include refid="tb"/>
        <set>
            <if test="itemSpec != null">
                item_spec = #{itemSpec},
            </if>
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="updateBizType8" parameterType="com.zm.order.tool.server.model.OrderItem">
        update
        <include refid="tb"/>
        set biz_type = 8,
        updated_at = now()
        where order_no = #{orderNo}
        and user_id = #{userId}
    </update>

    <update id="updateItemType" parameterType="com.zm.order.tool.server.model.OrderItem">
        update
        <include refid="tb"/>
        <set>
            <if test="itemType != null">
                item_type = #{itemType},
            </if>
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

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


    <select id="getAdminOrderItemList" parameterType="map" resultMap="OrderItemMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where

        <if test="orderNoList != null  and orderNoList.size > 0 ">
            order_no in
            <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
                #{orderNo}
            </foreach>
        </if>
        and user_id = #{userId}
        and is_deleted = 0
    </select>

    <select id="selectSetMealItem" parameterType="map" resultMap="OrderItemMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where user_id = #{userId} and order_no = #{orderNo} and owner_id = #{ownerId}
    </select>


    <update id="updateUserId" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            user_id = #{newUserId},
            updated_at= now()
        </set>
        where
        id = #{id}
        and user_id = #{userId}
    </update>


    <update id="updateBizType" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            biz_type = #{bizType},
            updated_at= now()
        </set>
        where
        user_id = #{userId}
        and order_no = #{orderNo}
    </update>

    <update id="deleteByOrderNo" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            is_deleted = -1,
            updated_at= now()
        </set>
        where
        user_id = #{userId}
        and order_no = #{orderNo}
    </update>

    <update id="deletes" parameterType="map">
        update
        <include refid="tb"/>
        set is_deleted = #{deleteType},
        updated_at = now()
        where user_id = #{userId}
        and id in
        <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <update id="realDeletes" parameterType="map">
        delete from
        <include refid="tb"/>
        where user_id = #{userId}
        and id in
        <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <update id="updateBizTypeByList" parameterType="map">
        update
        <include refid="tb"/>
        set biz_type = #{bizType},
        updated_at = now()
        where
        user_id in
        <foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
            #{userId}
        </foreach>
        and order_no in
        <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
            #{orderNo}
        </foreach>
    </update>

    <update id="realDeleteByOrderNoUserId" parameterType="map">
        delete from
        <include refid="tb"/>
        where
            user_id in
        <foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
            #{userId}
        </foreach>
        and order_no in
        <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
            #{orderNo}
        </foreach>
    </update>

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

    <update id="updateItemSign" parameterType="com.zm.order.tool.server.model.OrderItem">
        update
        <include refid="tb"/>
        <set>
            <if test="batchingInfo != null">
                batching_info = #{batchingInfo},
            </if>
            <if test="itemSign != null">
                item_sign = #{itemSign},
            </if>
            <if test="isDeleted != null">
                is_deleted = #{isDeleted},
            </if>
            <if test="itemId != null">
                item_id = #{itemId},
            </if>
            <if test="itemName != null">
                item_name = #{itemName},
            </if>
            <if test="itemImage != null">
                item_image = #{itemImage},
            </if>
            <if test="itemBarcode != null">
                item_barcode = #{itemBarcode},
            </if>
            <if test="goodsId != null">
                goods_id = #{goodsId},
            </if>
            <if test="costPrice != null">
                cost_price = #{costPrice},
            </if>
            <if test="itemPrice != null">
                item_price = #{itemPrice},
            </if>
            <if test="paidAmount != null">
                paid_amount = #{paidAmount},
            </if>
            <if test="isGift != null">
                is_gift = #{isGift},
            </if>
            <if test="discountAmount != null">
                discount_amount = #{discountAmount},
            </if>
            <if test="num != null">
                num = #{num},
            </if>
            <if test="features != null and features != ''">
                features = #{features},
            </if>
            <if test="marketPrice != null">
                market_price = #{marketPrice},
            </if>
            <if test="packAmount != null">
                pack_amount = #{packAmount},
            </if>
            <if test="productNo != null">
                product_no = #{productNo},
            </if>
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>

    <update id="deleteByOrderNos" parameterType="com.zm.order.tool.server.model.OrderItem">
        delete
        from
        <include refid="tb"/>
        where
        user_id in
        <foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
            #{userId}
        </foreach>
        AND order_no in
        <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
            #{orderNo}
        </foreach>
    </update>

    <update id="updateByOrderNo" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            user_id = #{newUserId},
            seller_id = #{sellerId},
            updated_at= now()
        </set>
        where
        user_id = #{userId} and order_no = #{orderNo}
    </update>

    <update id="updateSellerUser" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            seller_id = #{sellerId},
            user_id = #{newUserId},
            updated_at= now()
        </set>
        where
        user_id = #{oldUserId}
        and order_no = #{orderNo}
    </update>
</mapper>