<?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.tempdb.TmpRefundItemDao">

    <resultMap id="RefundItemMap" type="com.zm.order.tool.server.model.RefundItem">
        <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="refund_no" jdbcType="VARCHAR" property="refundNo"/>
        <result column="order_item_id" jdbcType="BIGINT" property="orderItemId"/>
        <result column="goods_id" jdbcType="BIGINT" property="goodsId" />
        <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_price" jdbcType="INTEGER" property="itemPrice"/>
        <result column="num" jdbcType="INTEGER" property="num"/>
        <result column="discount_amount" jdbcType="INTEGER" property="discountAmount"/>
        <result column="refund_amount" jdbcType="INTEGER" property="refundAmount"/>
        <result column="refund_num" jdbcType="INTEGER" property="refundNum"/>
        <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"/>
        <result column="item_type" jdbcType="INTEGER" property="itemType"/>
    </resultMap>

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

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

    <sql id="cols_exclude_id">
        biz_type, user_id, order_no, refund_no, item_type, order_item_id, goods_id, item_id,
        source_id, item_name, item_image, item_spec, item_unit, item_price, num,
        discount_amount, refund_amount, refund_num, refund_way, features, created_at,
        updated_at, is_deleted, seller_id, item_sign
    </sql>

    <sql id="values">
        #{id}, #{userId}, #{orderNo}, #{refundNo}, #{orderItemId}, #{goodsId},
        #{itemId}, #{itemName}, #{itemImage}, #{itemSpec},
        #{itemUnit}, #{itemPrice}, #{num},
        #{discountAmount}, #{refundAmount}, #{refundNum}, #{features},
        now(), now(), 0, #{sellerId}, #{itemType}
  </sql>

    <select id="selectList" parameterType="map" resultMap="RefundItemMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <if test="refundNo != null and refundNo != ''">
                AND refund_no = #{refundNo}
            </if>
            <if test="refundNoList != null and refundNoList.size > 0">
                AND refund_no in
                <foreach collection="refundNoList" item="refundNo" index="index" open="(" close=")" separator=",">
                    #{refundNo}
                </foreach>
            </if>
            <if test="orderNo != null and orderNo != ''">
                AND order_no = #{orderNo}
            </if>
            <if test="lastId != null">
                AND id > #{lastId}
            </if>
            <if test="userId != null">
                AND user_id > #{userId}
            </if>
        </where>
        order by id
        <if test="size != null">
            limit ${size}
        </if>
    </select>

    <select id="selectByRefundNo" parameterType="map" resultMap="RefundItemMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <if test="refundNo != null and refundNo != ''">
                AND refund_no = #{refundNo}
            </if>
            <if test="orderNo != null and orderNo != ''">
                AND order_no = #{orderNo}
            </if>
            AND is_deleted = 0
        </where>
    </select>


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

    <update id="updateSellerId" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            seller_id = case id
            <foreach collection="list" index="index" item="refundItem" separator="" open="" close="">
                when #{refundItem.id} then #{refundItem.sellerId}
            </foreach>
        </set>
        end
        where id in
        <foreach collection="list" index="index" item="refundItem" separator="," open="(" close=")">
            #{refundItem.id}
        </foreach>
    </update>

    <update id="updateBizType" parameterType="map">
        update
        <include refid="tb"/>
        <trim prefix="set" suffixOverrides=",">
            biz_type = 5, 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="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="refundItem" separator="," open="(" close=")">
            #{refundItem.id}
        </foreach>
    </update>

    <update id="updateBatch" parameterType="map">
        update
        <include refid="tb"/>
        <trim prefix="set" suffixOverrides=",">
            biz_type = 4, seller_id = #{sellerId}, 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="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 prefix="is_deleted = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.isDeleted}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="refundItem" separator="," open="(" close=")">
            #{refundItem.id}
        </foreach>
    </update>
</mapper>