<?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.hint.HintOrderItemDao">

    <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="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="item_type" jdbcType="INTEGER" property="itemType"/>
        <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="paid_amount" jdbcType="INTEGER" property="paidAmount"/>
        <result column="source_id" jdbcType="VARCHAR" property="sourceId"/>
        <result column="income_amount" jdbcType="INTEGER" property="incomeAmount"/>
        <result column="seller_bear_amount" jdbcType="INTEGER" property="sellerBearAmount"/>
        <result column="commission_amount" jdbcType="INTEGER" property="commissionAmount"/>
    </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="list" parameterType="map" resultMap="OrderItemMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where order_no in
        <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
            #{orderNo}
        </foreach>
    </select>

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

    <select id="scan" parameterType="map" resultType="long">
        select
        id
        from
        <include refid="tb"/>
        <where>
            <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="param" index="index" open="(" close=")" separator=",">
                    #{param}
                </foreach>
            </if>

            <if test="updatedAtStart != null">
                <![CDATA[ AND updated_at >= #{updatedAtStart} ]]>
            </if>
            <if test="updatedAtEnd != null">
                <![CDATA[ AND updated_at < #{updatedAtEnd} ]]>
            </if>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
        </where>
        order by id
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
        <if test="size != null">
            limit ${size}
        </if>
    </select>

    <select id="scanList" parameterType="map" resultMap="OrderItemMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <if test="maxId != null">
                <![CDATA[ AND id < #{maxId} ]]>
            </if>
            <if test="excludeSellerId != null">
                and seller_id != #{excludeSellerId}
            </if>
            <if test="orderNo != null">
                and order_no = #{orderNo}
            </if>
            <if test="updatedAtStart != null">
                <![CDATA[ AND updated_at >= #{updatedAtStart} ]]>
            </if>
            <if test="updatedAtEnd != null">
                <![CDATA[ AND updated_at <= #{updatedAtEnd} ]]>
            </if>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
        </where>
        <choose>
            <when test="sortFlag == 1">
                order by updated_at, id
            </when>
            <otherwise>
                order by id
            </otherwise>
        </choose>
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
        <if test="size != null">
            limit ${size}
        </if>
    </select>

    <update id="update" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            <if test="updateUserId != null">
                user_id = #{updateUserId},
            </if>
            updated_at= now()
        </set>
        where
        order_no =#{orderNo}

    </update>

    <update id="updateItemType" parameterType="list">
        update
        <include refid="tb"/>
        <trim prefix="set" suffixOverrides=",">
            biz_type = 5, seller_id = 49112, updated_at = now(),
            <trim prefix="item_type = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.itemType}
                </foreach>
            </trim>
            <trim prefix="batching_info = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.batchingInfo}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="order" separator="," open="(" close=")">
            #{order.id}
        </foreach>
    </update>

    <update id="updateBizTypeRollBack" parameterType="list">
        update
        <include refid="tb"/>
            set biz_type = 1, updated_at = now()
        where id in
        <foreach collection="list" index="index" item="order" separator="," open="(" close=")">
            #{order.id}
        </foreach>
    </update>

    <update id="updateDeleted" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            is_deleted = 100
        </set>
        where order_no in
        <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
            #{orderNo}
        </foreach>
    </update>

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

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

    <update id="updateBizTypeSellerId" parameterType="map">
        update
        <include refid="tb"/>
        <trim prefix="set" suffixOverrides=",">
            biz_type = #{bizType},
            seller_id = #{sellerId},
            updated_at= now(),
            <trim prefix="item_spec = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.itemSpec}
                </foreach>
            </trim>
            <trim prefix="features = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.features}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="order" separator="," open="(" close=")">
            #{order.id}
        </foreach>
    </update>

    <delete id="physicalDeleted" parameterType="map">
        delete from order_items where order_no in
        <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
            #{orderNo}
        </foreach>
        and seller_id = 207720
    </delete>

    <delete id="physicalDeletedTest" parameterType="map">
        delete from order_items where order_no in
        <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
            #{orderNo}
        </foreach>
    </delete>

    <update id="updateItemTypeByIds" parameterType="map">
        update
        <include refid="tb"/>
        set item_type = #{itemType},
        updated_at = now()
        where id in
        <foreach collection="orderItemIdList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <update id="batchUpdateDate" parameterType="list">
        update
        <include refid="tb"/>
        <set>
            updated_at = now()
        </set>
        where
        id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <select id="checkItem" parameterType="map" resultMap="OrderItemMap">
        select
        income_amount, seller_bear_amount, commission_amount
        from
        <include refid="tb"/>
        limit 1
    </select>
</mapper>