<?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.history.HistoryDataRefundDetailsDao">
    <resultMap id="ResultMap" type="com.zm.order.tool.server.model.OrderRefundDetails">
		<id column="id" jdbcType="BIGINT" property="id"/>
		<result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
		<result column="refund_no" jdbcType="VARCHAR" property="refundNo"/>
		<result column="user_id" jdbcType="BIGINT" property="userId"/>
		<result column="seller_id" jdbcType="BIGINT" property="sellerId"/>
		<result column="store_id" jdbcType="BIGINT" property="storeId"/>
		<result column="pay_type" jdbcType="INTEGER" property="payType"/>
		<result column="refund_amount" jdbcType="INTEGER" property="refundAmount"/>
		<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="biz_type" jdbcType="INTEGER" property="bizType"/>
		<result column="features" jdbcType="VARCHAR" property="features"/>
	</resultMap>

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

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

    <sql id="cols_exclude_id">
		order_no,
		refund_no,
		user_id,
		seller_id,
		store_id,
		pay_type,
		refund_amount,
		created_at,
		updated_at,
		is_deleted,
		biz_type,
		features
    </sql>

	<sql id="values">
		#{id,jdbcType=BIGINT},
		#{orderNo,jdbcType=VARCHAR},
		#{refundNo,jdbcType=VARCHAR},
		#{userId,jdbcType=BIGINT},
		#{sellerId,jdbcType=BIGINT},
		#{storeId,jdbcType=BIGINT},
		#{payType,jdbcType=INTEGER},
		#{refundAmount,jdbcType=INTEGER},
		now(),
		now(),
		0,
		#{bizType,jdbcType=INTEGER},
		#{features,jdbcType=VARCHAR}
	</sql>

	<insert id="replaceList" parameterType="list">
		replace into
		<include refid="tb"/>
		(<include refid="cols_all"/>)
		values
		<foreach collection="list" item="refund" index="index" separator=",">
			(
			#{refund.id,jdbcType=BIGINT},
			#{refund.orderNo,jdbcType=VARCHAR},
			#{refund.refundNo,jdbcType=VARCHAR},
			#{refund.userId,jdbcType=BIGINT},
			#{refund.sellerId,jdbcType=BIGINT},
			#{refund.storeId,jdbcType=BIGINT},
			#{refund.payType,jdbcType=INTEGER},
			#{refund.refundAmount,jdbcType=INTEGER},
			#{refund.createdAt},
			#{refund.updatedAt},
			#{refund.isDeleted},
			#{refund.bizType,jdbcType=INTEGER},
			#{refund.features,jdbcType=VARCHAR}
			)
		</foreach>
	</insert>

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

	<insert id="insertList" parameterType="list">
		insert into
		<include refid="tb"/>
		(<include refid="cols_all"/>)
		values
		<foreach collection="list" item="refund" index="index" separator=",">
			(
			#{refund.id,jdbcType=BIGINT},
			#{refund.orderNo,jdbcType=VARCHAR},
			#{refund.refundNo,jdbcType=VARCHAR},
			#{refund.userId,jdbcType=BIGINT},
			#{refund.sellerId,jdbcType=BIGINT},
			#{refund.storeId,jdbcType=BIGINT},
			#{refund.payType,jdbcType=INTEGER},
			#{refund.refundAmount,jdbcType=INTEGER},
			now(),
			now(),
			0,
			#{refund.bizType,jdbcType=INTEGER},
			#{refund.features,jdbcType=VARCHAR}
			)
		</foreach>
	</insert>

	<select id="selectByRefundNo" parameterType="map" resultMap="ResultMap">
		select
		<include refid="cols_all"/>
		from
		<include refid="tb"/>
		where refund_no = #{refundNo}
	</select>

	<select id="selectByRefundNoList" parameterType="map" resultMap="ResultMap">
		select
		<include refid="cols_all"/>
		from
		<include refid="tb"/>
		where refund_no in
		<foreach collection="refundNoList" item="refundNo" index="index" open="(" close=")" separator=",">
			#{refundNo}
		</foreach>
	</select>

    <select id="selectList" resultMap="ResultMap">
        SELECT
        <include refid="cols_all"/>
        FROM
        <include refid="tb"/>
        <where>
			is_deleted = 0
			<if test="orderNo != null">
				and order_no = #{orderNo}
			</if>
			<if test="userId != null">
				and user_id = #{userId}
			</if>
			<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="sellerId != null">
				and seller_id = #{sellerId}
			</if>
			<if test="storeId != null">
				and store_id = #{storeId}
			</if>
			<if test="payTypeList != null and payTypeList.size > 0">
				AND pay_type in
				<foreach collection="payTypeList" item="payType" index="index" open="(" close=")" separator=",">
					#{payType}
				</foreach>
			</if>
        </where>
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
    </select>

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

	<update id="updateBiz" 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="update" parameterType="com.zm.order.tool.server.model.OrderRefundDetails">
        UPDATE
        <include refid="tb"/>
        <set>
			<if test="payType != null">
				pay_type = #{payType,jdbcType=INTEGER},
			</if>
			<if test="refundAmount != null">
				refund_amount = #{refundAmount,jdbcType=INTEGER},
			</if>
			updated_at = now()
        </set>
        where id = #{id} and user_id = #{userId}
    </update>

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

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

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

	<select id="selectListOrder" resultMap="ResultMap">
		SELECT
		<include refid="cols_all"/>
		FROM
		<include refid="tb"/>
		<where>
			is_deleted = 0
			<if test="orderNo != null">
				and order_no = #{orderNo}
			</if>
			<if test="userId != null">
				and user_id = #{userId}
			</if>
		</where>
	</select>
</mapper>