<?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.ruoyi.mall.mapper.MallMemberCouponMapper">

    <resultMap type="MallMemberCoupon" id="MallMemberCouponResult">
        <result property="memberCouponId"    column="member_coupon_id"    />
        <result property="userId"    column="user_id"    />
        <result property="couponId"    column="coupon_id"    />
        <result property="name"    column="name"    />
        <result property="enoughPrice"    column="enough_price"    />
        <result property="price"    column="price"    />
        <result property="type"    column="type"    />
        <result property="createAt"    column="create_at"    />
        <result property="isUsed"    column="is_used"    />
    </resultMap>

    <sql id="selectMallMemberCouponVo">
        select member_coupon_id, user_id, coupon_id, name, enough_price, price, type, create_at, is_used from mall_member_coupon
    </sql>

    <select id="selectMallMemberCouponList" parameterType="MallMemberCoupon" resultMap="MallMemberCouponResult">
        <include refid="selectMallMemberCouponVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="couponId != null "> and coupon_id = #{couponId}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="enoughPrice != null "> and enough_price = #{enoughPrice}</if>
            <if test="price != null "> and price = #{price}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="createAt != null  and createAt != ''"> and create_at = #{createAt}</if>
            <if test="isUsed != null  and isUsed != ''"> and is_used = #{isUsed}</if>
        </where>
    </select>

    <select id="selectMallMemberCouponByMemberCouponId" parameterType="Long" resultMap="MallMemberCouponResult">
        <include refid="selectMallMemberCouponVo"/>
        where member_coupon_id = #{memberCouponId}
    </select>

    <insert id="insertMallMemberCoupon" parameterType="MallMemberCoupon" useGeneratedKeys="true" keyProperty="memberCouponId">
        insert into mall_member_coupon
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="couponId != null">coupon_id,</if>
            <if test="name != null">name,</if>
            <if test="enoughPrice != null">enough_price,</if>
            <if test="price != null">price,</if>
            <if test="type != null">type,</if>
            <if test="createAt != null">create_at,</if>
            <if test="isUsed != null">is_used,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="couponId != null">#{couponId},</if>
            <if test="name != null">#{name},</if>
            <if test="enoughPrice != null">#{enoughPrice},</if>
            <if test="price != null">#{price},</if>
            <if test="type != null">#{type},</if>
            <if test="createAt != null">#{createAt},</if>
            <if test="isUsed != null">#{isUsed},</if>
         </trim>
    </insert>

    <update id="updateMallMemberCoupon" parameterType="MallMemberCoupon">
        update mall_member_coupon
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="couponId != null">coupon_id = #{couponId},</if>
            <if test="name != null">name = #{name},</if>
            <if test="enoughPrice != null">enough_price = #{enoughPrice},</if>
            <if test="price != null">price = #{price},</if>
            <if test="type != null">type = #{type},</if>
            <if test="createAt != null">create_at = #{createAt},</if>
            <if test="isUsed != null">is_used = #{isUsed},</if>
        </trim>
        where member_coupon_id = #{memberCouponId}
    </update>

    <delete id="deleteMallMemberCouponByMemberCouponId" parameterType="Long">
        delete from mall_member_coupon where member_coupon_id = #{memberCouponId}
    </delete>

    <delete id="deleteMallMemberCouponByMemberCouponIds" parameterType="String">
        delete from mall_member_coupon where member_coupon_id in
        <foreach item="memberCouponId" collection="array" open="(" separator="," close=")">
            #{memberCouponId}
        </foreach>
    </delete>

    <select id="checkCouponUsed" resultMap="MallMemberCouponResult">
        <include refid="selectMallMemberCouponVo"/>
        where user_id = #{userId}
          and coupon_id = #{couponId}
          and is_used = '0'
        order by create_at desc
            limit 1
    </select>
</mapper>
