<?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.TmpRefundDao">

    <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="refund_no" jdbcType="VARCHAR" property="refundNo"/>
        <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="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="refund_detail_info" jdbcType="VARCHAR" property="refundDetailInfo"/>
        <result column="pay_type" jdbcType="INTEGER" property="payType"/>
        <result column="from_system" jdbcType="INTEGER" property="fromSystem"/>
    </resultMap>

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

    <sql id="cols_all">
        id,
        <include refid="cols_exclude_id"/>
    </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, refund_detail_info, pay_type, from_system
    </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}, #{refundDetailInfo}, #{payType}, #{fromSystem}
  </sql>

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

    <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>
            AND is_deleted in (0, 100)
        </where>
        order by id
        <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>



    <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="updateRefundDetail" parameterType="map">
        update
        <include refid="tb"/>
        set
        refund_detail_info = #{refundDetailInfo},
        updated_at =now()
        where
        id = #{id}
    </update>

    <update id="update" parameterType="map">
        update
        <include refid="tb"/>
        set
        <if test="status != null">
            status = #{status},
        </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="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 = 5, 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>
</mapper>