<?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.dao.RefundDao">

    <resultMap id="RefundMap" type="com.zm.order.tool.server.model.Refund">
        <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="order_type" jdbcType="INTEGER" property="orderType"/>
        <result column="order_sub_type" jdbcType="INTEGER" property="orderSubType"/>
        <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
        <result column="refund_no" jdbcType="VARCHAR" property="refundNo"/>
        <result column="refund_way" jdbcType="INTEGER" property="refundWay"/>
        <result column="refund_level" jdbcType="INTEGER" property="refundLevel"/>
        <result column="seller_id" jdbcType="BIGINT" property="sellerId"/>
        <result column="store_id" jdbcType="BIGINT" property="storeId"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="source" jdbcType="INTEGER" property="source"/>
        <result column="source_no" jdbcType="VARCHAR" property="sourceNo"/>
        <result column="refund_type" jdbcType="INTEGER" property="refundType"/>
        <result column="refund_amount" jdbcType="INTEGER" property="refundAmount"/>
        <result column="amount" jdbcType="INTEGER" property="amount"/>
        <result column="wallet_amount" jdbcType="INTEGER" property="walletAmount"/>
        <result column="refund_amount_details" jdbcType="VARCHAR" property="refundAmountDetails"/>
        <result column="applicant" jdbcType="BIGINT" property="applicant"/>
        <result column="images" jdbcType="VARCHAR" property="images"/>
        <result column="apply_at" jdbcType="TIMESTAMP" property="applyAt"/>
        <result column="apply_reason" jdbcType="VARCHAR" property="applyReason"/>
        <result column="order_at" jdbcType="TIMESTAMP" property="orderAt"/>
        <result column="is_alone" jdbcType="INTEGER" property="isAlone"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="from_system" jdbcType="INTEGER" property="fromSystem"/>
        <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="version" jdbcType="INTEGER" property="version"/>
        <result column="deal_at" jdbcType="TIMESTAMP" property="dealAt"/>
        <result column="pay_type" jdbcType="INTEGER" property="payType"/>
        <result column="review_status" jdbcType="INTEGER" property="reviewStatus"/>
        <result column="gift_card_amount" jdbcType="INTEGER" property="giftCardAmount"/>
        <result column="refund_detail_info" jdbcType="VARCHAR" property="refundDetailInfo"/>
        <result column="mobile_cipher" jdbcType="VARCHAR" property="mobileCipher"/>
        <result column="master_data_source" jdbcType="INTEGER" property="masterDataSource" />
        <result column="bill_at" jdbcType="TIMESTAMP" property="billAt"/>
        <result column="features_cipher" jdbcType="VARCHAR" property="featuresCipher"/>
    </resultMap>

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

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

    <sql id="values">
        #{id}, #{bizType}, #{orderNo}, #{orderType},#{orderSubType}, #{refundNo}, #{refundWay}, #{refundLevel},
        #{sellerId},
        #{storeId}, #{userId}, #{mobile}, #{source}, #{sourceNo}, #{refundType}, #{refundAmount}, #{amount},
        #{walletAmount},
        #{refundAmountDetails}, #{applicant}, #{images}, #{applyAt}, #{orderAt},
        #{applyReason}, #{features}, #{isAlone}, #{status}, #{fromSystem},
        now(), now(), 0, 0, #{dealAt}, #{payType}, #{reviewStatus}, #{giftCardAmount}, #{performanceType},
        #{refundDetailInfo}, #{mobileCipher},#{masterDataSource}, #{billAt},#{featuresCipher}
    </sql>

    <sql id="cols_exclude_id">
        biz_type, order_no, order_type, order_sub_type, refund_no, refund_way, refund_level, seller_id, store_id,
        user_id, mobile,
        source, source_no, refund_type,
        refund_amount, amount, wallet_amount, refund_amount_details, applicant, images, apply_at, order_at,
        apply_reason, features, is_alone,
        status, from_system, created_at, updated_at, is_deleted, version, deal_at, pay_type, review_status,
        gift_card_amount, performance_type, refund_detail_info, mobile_cipher, master_data_source,bill_at,features_cipher
    </sql>

    <insert id="insertList" parameterType="com.zm.order.tool.server.model.Refund" keyColumn="id" keyProperty="id"
            useGeneratedKeys="true">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        <foreach collection="list" item="refundItem" index="index" separator=",">
            (
            #{refundItem.id}, #{refundItem.bizType}, #{refundItem.orderNo}, #{refundItem.orderType},
            #{refundItem.orderSubType}, #{refundItem.refundNo}, #{refundItem.refundWay}, #{refundItem.refundLevel},
            #{refundItem.sellerId}, #{refundItem.storeId}, #{refundItem.userId}, #{refundItem.mobile}, #{refundItem.source},
            #{refundItem.sourceNo}, #{refundItem.refundType}, #{refundItem.refundAmount},
            #{refundItem.amount}, #{refundItem.walletAmount}, #{refundItem.refundAmountDetails}, #{refundItem.applicant},
            #{refundItem.images}, #{refundItem.applyAt}, #{refundItem.orderAt}, #{refundItem.applyReason},
            #{refundItem.features},#{refundItem.isAlone},#{refundItem.status},#{refundItem.fromSystem},#{refundItem.createdAt},
             now(), 0, #{refundItem.version}, #{refundItem.dealAt}, #{refundItem.payType}, #{refundItem.reviewStatus},
            #{refundItem.giftCardAmount},#{refundItem.performanceType},#{refundItem.refundDetailInfo},
            #{refundItem.mobileCipher},#{refundItem.masterDataSource}, #{refundItem.billAt},#{refundItem.featuresCipher}
            )
        </foreach>
    </insert>

    <select id="selectByRefundNo" parameterType="map" resultMap="RefundMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where refund_no = #{refundNo}
        and is_deleted = 0
        limit 1
    </select>

    <insert id="replace" parameterType="com.zm.order.tool.server.model.Refund">
        replace into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        value
        (
        #{id}, #{bizType}, #{orderNo}, #{orderType},
        #{orderSubType}, #{refundNo}, #{refundWay}, #{refundLevel},
        #{sellerId}, #{storeId}, #{userId}, #{mobile},
        #{source},
        #{sourceNo}, #{refundType}, #{refundAmount},
        #{amount}, #{walletAmount}, #{refundAmountDetails},
        #{applicant},
        #{images}, #{applyAt}, #{orderAt}, #{applyReason},
        #{features},#{isAlone},#{status},#{fromSystem},#{createdAt},
        #{updatedAt}, #{isDeleted}, #{version},
        #{dealAt}, #{payType}, #{reviewStatus},
        #{giftCardAmount},#{performanceType},#{refundDetailInfo}, #{mobileCipher},#{masterDataSource}, #{billAt},#{featuresCipher}
        )
    </insert>

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

    <select id="count" parameterType="map" resultType="int">
        select
        count(1)
        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>
            <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="sellerId != null">
                AND seller_id = #{sellerId}
            </if>
            <if test="storeId != null">
                AND store_id = #{storeId}
            </if>
            <if test="userId != null">
                AND user_id = #{userId}
            </if>
            <if test="isAlone != null">
                AND is_alone = #{isAlone}
            </if>
            <if test="source != null">
                AND source = #{source}
            </if>
            <if test="sourceNo != null and sourceNo != ''">
                AND source_no = #{sourceNo}
            </if>
            <if test="applyAtStart != null">
                <![CDATA[ AND apply_at >= #{applyAtStart} ]]>
            </if>
            <if test="applyAtEnd != null">
                <![CDATA[ AND apply_at <= #{applyAtEnd} ]]>
            </if>
            <if test="statusList != null and statusList.size > 0">
                AND status in
                <foreach collection="statusList" item="status" index="index" open="(" close=")" separator=",">
                    #{status}
                </foreach>
            </if>
            AND is_deleted = 0
        </where>
    </select>

    <select id="selectList" parameterType="map" resultMap="RefundMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <choose>
                <when test="refundNoList != null and refundNoList.size > 0">
                    AND refund_no in
                    <foreach collection="refundNoList" item="refundNo" index="index" open="(" close=")" separator=",">
                        #{refundNo}
                    </foreach>
                </when>
                <otherwise>
                    <if test="refundNo != null and refundNo != ''">
                        AND refund_no = #{refundNo}
                    </if>
                </otherwise>
            </choose>
            <if test="orderNo != null and orderNo != ''">
                AND order_no = #{orderNo}
            </if>
            <if test="sellerId != null">
                AND seller_id = #{sellerId}
            </if>
            <if test="bizType != null">
                AND biz_type = #{bizType}
            </if>
            <if test="storeId != null">
                AND store_id = #{storeId}
            </if>
            <if test="excDealAt != null and excDealAt == 1">
                AND deal_at is null
            </if>
            <if test="userId != null">
                AND user_id = #{userId}
            </if>
            <if test="isAlone != null">
                AND is_alone = #{isAlone}
            </if>
            <if test="source != null">
                AND source = #{source}
            </if>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
            <if test="orderAtStart != null">
                <![CDATA[ AND order_at >= #{orderAtStart} ]]>
            </if>
            <if test="orderAtEnd != null">
                <![CDATA[ AND order_at <= #{orderAtEnd} ]]>
            </if>
            <if test="updatedAtStart != null">
                <![CDATA[ AND updated_at >= #{updatedAtStart} ]]>
            </if>
            <if test="updatedAtEnd != null">
                <![CDATA[ AND updated_at <= #{updatedAtEnd} ]]>
            </if>
            <if test="sourceNo != null and sourceNo != ''">
                AND source_no = #{sourceNo}
            </if>
            <if test="applyAtStart != null">
                <![CDATA[ AND apply_at >= #{applyAtStart} ]]>
            </if>
            <if test="applyAtEnd != null">
                <![CDATA[ AND apply_at <= #{applyAtEnd} ]]>
            </if>
            <if test="statusList != null and statusList.size > 0">
                AND status in
                <foreach collection="statusList" item="status" index="index" open="(" close=")" separator=",">
                    #{status}
                </foreach>
            </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>
        </where>
        <choose>
            <when test="sortFlag == 1">
                order by updated_at, id
            </when>
            <otherwise>
                order by id
            </otherwise>
        </choose>
        <if test="start != null">
            limit ${start},
            <choose>
                <when test="fakePageSize != null">
                    ${fakePageSize}
                </when>
                <otherwise>
                    ${pageSize}
                </otherwise>
            </choose>
        </if>
        <if test="size != null">
            limit ${size}
        </if>
    </select>

    <select id="selectIdList" parameterType="map" resultType="long">
        select
        id
        from
        <include refid="tb"/>
        <where>
            <choose>
                <when test="refundNoList != null and refundNoList.size > 0">
                    AND refund_no in
                    <foreach collection="refundNoList" item="refundNo" index="index" open="(" close=")" separator=",">
                        #{refundNo}
                    </foreach>
                </when>
                <otherwise>
                    <if test="refundNo != null and refundNo != ''">
                        AND refund_no = #{refundNo}
                    </if>
                </otherwise>
            </choose>
            <if test="orderNo != null and orderNo != ''">
                AND order_no = #{orderNo}
            </if>
            <if test="sellerId != null">
                AND seller_id = #{sellerId}
            </if>
            <if test="bizType != null">
                AND biz_type = #{bizType}
            </if>
            <if test="storeId != null">
                AND store_id = #{storeId}
            </if>
            <if test="excDealAt != null and excDealAt == 1">
                AND deal_at is null
            </if>
            <if test="userId != null">
                AND user_id = #{userId}
            </if>
            <if test="isAlone != null">
                AND is_alone = #{isAlone}
            </if>
            <if test="source != null">
                AND source = #{source}
            </if>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
            <if test="orderAtStart != null">
                <![CDATA[ AND order_at >= #{orderAtStart} ]]>
            </if>
            <if test="orderAtEnd != null">
                <![CDATA[ AND order_at <= #{orderAtEnd} ]]>
            </if>
            <if test="updatedAtStart != null">
                <![CDATA[ AND updated_at >= #{updatedAtStart} ]]>
            </if>
            <if test="updatedAtEnd != null">
                <![CDATA[ AND updated_at <= #{updatedAtEnd} ]]>
            </if>
            <if test="sourceNo != null and sourceNo != ''">
                AND source_no = #{sourceNo}
            </if>
            <if test="applyAtStart != null">
                <![CDATA[ AND apply_at >= #{applyAtStart} ]]>
            </if>
            <if test="applyAtEnd != null">
                <![CDATA[ AND apply_at <= #{applyAtEnd} ]]>
            </if>
            <if test="statusList != null and statusList.size > 0">
                AND status in
                <foreach collection="statusList" item="status" index="index" open="(" close=")" separator=",">
                    #{status}
                </foreach>
            </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>
        </where>
        <choose>
            <when test="sortFlag == 1">
                order by updated_at, id
            </when>
            <otherwise>
                order by id
            </otherwise>
        </choose>
        <if test="start != null">
            limit ${start},
            <choose>
                <when test="fakePageSize != null">
                    ${fakePageSize}
                </when>
                <otherwise>
                    ${pageSize}
                </otherwise>
            </choose>
        </if>
        <if test="size != null">
            limit ${size}
        </if>
    </select>


    <select id="sumRefundAmount" parameterType="map" resultType="INTEGER">
        select
        sum(${amountField})
        from
        <include refid="tb"/>
        <where>
            <if test="sellerId != null">
                AND seller_id = #{sellerId}
            </if>
            <if test="storeId != null">
                AND store_id = #{storeId}
            </if>
            <if test="isAlone != null">
                AND is_alone = #{isAlone}
            </if>
            <if test="source != null">
                AND source = #{source}
            </if>
            <if test="status != null">
                AND status = #{status}
            </if>
            <if test="statusList != null and statusList.size > 0">
                AND status in
                <foreach collection="statusList" item="status" index="index" open="(" close=")" separator=",">
                    #{status}
                </foreach>
            </if>
            <if test="applyAtStart != null">
                <![CDATA[ AND apply_at >= #{applyAtStart} ]]>
            </if>
            <if test="applyAtEnd != null">
                <![CDATA[ AND apply_at <= #{applyAtEnd} ]]>
            </if>
            AND is_deleted = 0
        </where>

    </select>

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



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

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

    <update id="updateRefundDetail" parameterType="map">
        update
        <include refid="tb"/>
        set
        refund_detail_info = #{refundDetailInfo},
        updated_at =now()
        where
        id = #{id}
    </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="updateBizRefundType" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            biz_type = #{bizType}, refund_type = 1, updated_at = now()
        </set>
        where order_no in
        <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
            #{orderNo}
        </foreach>
    </update>


    <select id="selectByOrderNoList" parameterType="map" resultMap="RefundMap">
        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>
        </where>
    </select>


    <update id="update" parameterType="map">
        update
        <include refid="tb"/>
        set
        <if test="status != null">
            status = #{status},
        </if>
        <if test="refundAmount != null">
            refund_amount = #{refundAmount},
        </if>
        <if test="amount != null">
            amount = #{amount},
        </if>
        <if test="walletAmount != null">
            wallet_amount = #{walletAmount},
        </if>
        <if test="dealAt != null">
            deal_at = #{dealAt},
        </if>
        <if test="features != null and features != ''">
            features = #{features},
        </if>
        updated_at =now()
        where
        id =#{id}
    </update>

    <update id="updateDealAt" parameterType="map">
        update
        <include refid="tb"/>
        set
        deal_at = #{dealAt},
        updated_at =now()
        where
        id =#{id}
    </update>

    <update id="updateDetailInfo" parameterType="map">
        update
        <include refid="tb"/>
        set refund_detail_info = #{refundDetailInfo}, updated_at =now()
        where
        id = #{id}
    </update>

    <update id="batchUpdateDealAt" parameterType="list">
        update
        <include refid="tb"/>
        <set>
            deal_at = case id
            <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                when #{refund.id} then #{refund.dealAt}
            </foreach>
        </set>
        end
        where id in
        <foreach collection="list" index="index" item="refund" separator="," open="(" close=")">
            #{refund.id}
        </foreach>
    </update>

    <update id="updateBatch" parameterType="list">
        update
        <include refid="tb"/>
        <trim prefix="set" suffixOverrides=",">
            biz_type = #{bizType},
            seller_id = #{sellerId},
            updated_at = now(),
            <trim prefix="status = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.status}
                </foreach>
            </trim>
            <trim prefix="refund_way = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.refundWay}
                </foreach>
            </trim>
            <trim prefix="store_id = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.storeId}
                </foreach>
            </trim>
            <trim prefix="order_type = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.orderType}
                </foreach>
            </trim>
            <trim prefix="source = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.source}
                </foreach>
            </trim>
            <trim prefix="order_sub_type = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.orderSubType}
                </foreach>
            </trim>
            <trim prefix="features = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.features}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="refund" separator="," open="(" close=")">
            #{refund.id}
        </foreach>
    </update>

    <update id="updateVersion" parameterType="list">
        update
        <include refid="tb"/>
        set version = version + 1
        where id in
        <foreach collection="list" index="index" item="refund" separator="," open="(" close=")">
            #{refund.id}
        </foreach>
    </update>

    <update id="batchUpdateFromSystem" parameterType="list">
        update
        <include refid="tb"/>
        <set>
            from_system = case id
            <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                when #{refund.id} then #{refund.fromSystem}
            </foreach>
        </set>
        end
        where id in
        <foreach collection="list" index="index" item="refund" separator="," open="(" close=")">
            #{refund.id}
        </foreach>
    </update>

    <update id="updatePayType" parameterType="map">
        update
        <include refid="tb"/>
        set
        pay_type = #{payType},
        updated_at =now()
        where
        id = #{id}
    </update>

    <update id="updateExtraMap" parameterType="map">
        update
        <include refid="tb"/>
        <trim prefix="set" suffixOverrides=",">
            biz_type = 7, seller_id = 217347, updated_at = now(),
            <trim prefix="features = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.features}
                </foreach>
            </trim>
            <trim prefix="order_type = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.orderType}
                </foreach>
            </trim>
            <trim prefix="pay_type = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.payType}
                </foreach>
            </trim>
            <trim prefix="status = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.status}
                </foreach>
            </trim>
            <trim prefix="refund_detail_info = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.refundDetailInfo}
                </foreach>
            </trim>
            <trim prefix="user_id = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.userId}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="refund" separator="," open="(" close=")">
            #{refund.id}
        </foreach>
    </update>

    <update id="delete" parameterType="list">
        update
        <include refid="tb"/>
        <set>
            is_deleted = #{deleteType}, updated_at = now()
        </set>
        where id = #{id}
    </update>

    <insert id="insert" parameterType="com.zm.order.tool.server.model.Refund" keyColumn="id" keyProperty="id"
            useGeneratedKeys="true">
        insert into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        (<include refid="values"/>)
    </insert>

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

    <select id="selectByOrderNo" parameterType="map" resultMap="RefundMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            order_no = #{orderNo} AND is_deleted = 0
        </where>
        limit 200
    </select>

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

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

    <select id="selectListBaseWhere" parameterType="map" resultMap="RefundMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <if test="maxId != null">
                <![CDATA[ AND id < #{maxId} ]]>
            </if>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
        </where>
        order by id desc
        limit ${size}
    </select>

    <select id="selectRespIdList" parameterType="map" resultMap="RefundMap">
        select
        id
        from
        <include refid="tb"/>
        <where>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
            <if test="updatedAtStart != null">
                <![CDATA[ AND updated_at >= #{updatedAtStart} ]]>
            </if>
            <if test="updatedAtEnd != null">
                <![CDATA[ AND updated_at <= #{updatedAtEnd} ]]>
            </if>
        </where>
        order by id
        limit ${size}
    </select>


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

    <update id="updateBatchToBaking" parameterType="map">
        update
        <include refid="tb"/>
        <trim prefix="set" suffixOverrides=",">
            biz_type = 4, updated_at = now(),
            <trim prefix="features = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.features}
                </foreach>
            </trim>
            <trim prefix="order_type = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.orderType}
                </foreach>
            </trim>
            <trim prefix="order_sub_type = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.orderSubType}
                </foreach>
            </trim>
            <trim prefix="pay_type = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.payType}
                </foreach>
            </trim>
            <trim prefix="status = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.status}
                </foreach>
            </trim>
            <trim prefix="refund_detail_info = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.refundDetailInfo}
                </foreach>
            </trim>
            <trim prefix="performance_type = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.performanceType}
                </foreach>
            </trim>
            <trim prefix="source = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.source}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="refund" separator="," open="(" close=")">
            #{refund.id}
        </foreach>
    </update>


    <update id="updateBakingJiFen" parameterType="list">
        update
        <include refid="tb"/>
        <trim prefix="set" suffixOverrides=",">
            biz_type = 8,
            order_type = 20,
            order_sub_type = 29,
            updated_at = now(),
            <trim prefix="status = case id" suffix="end,">
                <foreach collection="list" index="index" item="refund" separator="" open="" close="">
                    when #{refund.id} then #{refund.status}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="refund" separator="," open="(" close=")">
            #{refund.id}
        </foreach>
    </update>

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