<?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.framework.recovery.mapper.WxCouponsMapper">
    
    <resultMap type="WxCoupons" id="WxCouponsResult">
        <result property="id"    column="id"    />
        <result property="couponsType"    column="coupons_type"    />
        <result property="couponsAmount"    column="coupons_amount"    />
        <result property="createBy"    column="create_by"    />
        <result property="createDate"    column="create_date"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateDate"    column="update_date"    />
        <result property="couponsPrice"    column="coupons_price"    />
        <result property="deadTime"    column="dead_time"    />
    </resultMap>

    <sql id="selectWxCouponsVo">
        select id, coupons_type, coupons_amount, create_by, create_date, update_by, update_date, coupons_price, dead_time from wx_coupons
    </sql>

    <select id="selectWxCouponsList" parameterType="WxCoupons" resultMap="WxCouponsResult">
        <include refid="selectWxCouponsVo"/>
        <where>  
            <if test="couponsType != null  and couponsType != ''"> and coupons_type = #{couponsType}</if>
            <if test="couponsAmount != null  and couponsAmount != ''"> and coupons_amount = #{couponsAmount}</if>
            <if test="createDate != null "> and create_date = #{createDate}</if>
            <if test="updateDate != null "> and update_date = #{updateDate}</if>
            <if test="couponsPrice != null "> and coupons_price = #{couponsPrice}</if>
            <if test="deadTime != null "> and dead_time = #{deadTime}</if>
        </where>
    </select>
    
    <select id="selectWxCouponsById" parameterType="Long" resultMap="WxCouponsResult">
        <include refid="selectWxCouponsVo"/>
        where id = #{id}
    </select>

    <insert id="insertWxCoupons" parameterType="WxCoupons" useGeneratedKeys="true" keyProperty="id">
        insert into wx_coupons
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="couponsType != null">coupons_type,</if>
            <if test="couponsAmount != null">coupons_amount,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createDate != null">create_date,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateDate != null">update_date,</if>
            <if test="couponsPrice != null">coupons_price,</if>
            <if test="deadTime != null">dead_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="couponsType != null">#{couponsType},</if>
            <if test="couponsAmount != null">#{couponsAmount},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createDate != null">#{createDate},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateDate != null">#{updateDate},</if>
            <if test="couponsPrice != null">#{couponsPrice},</if>
            <if test="deadTime != null">#{deadTime},</if>
         </trim>
    </insert>

    <update id="updateWxCoupons" parameterType="WxCoupons">
        update wx_coupons
        <trim prefix="SET" suffixOverrides=",">
            <if test="couponsType != null">coupons_type = #{couponsType},</if>
            <if test="couponsAmount != null">coupons_amount = #{couponsAmount},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createDate != null">create_date = #{createDate},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateDate != null">update_date = #{updateDate},</if>
            <if test="couponsPrice != null">coupons_price = #{couponsPrice},</if>
            <if test="deadTime != null">dead_time = #{deadTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteWxCouponsById" parameterType="Long">
        delete from wx_coupons where id = #{id}
    </delete>

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

    <select id="selectUserCoupons" resultType="com.ruoyi.framework.recovery.domain.vo.UserCouponsVo">
        select wuc.id as id, wuc.coupons_id as couponsId, wuc.open_id as openId, wuc.create_date as createDate,
            wuc.update_date as updateDate, wc.coupons_type as couponsName
        from wx_user_coupons wuc left join wx_coupons wc on wc.id = wuc.coupons_id
        where wuc.open_id = #{dto.openId,jdbcType=VARCHAR}
            and wuc.dead_time > #{dto.findDate,jdbcType=TIMESTAMP}
            and wuc.is_used > -1
    </select>

    <insert id="insertUserCoupons">
        insert into wx_user_coupons (coupons_id, open_id, create_date, is_used, dead_time)
            values (#{dto.couponsId}, #{dto.openId}, #{dto.createTime,jdbcType=TIMESTAMP}, 0, #{dto.findDate})
    </insert>
</mapper>