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

    <resultMap type="MallCoupon" id="MallCouponResult">
        <result property="id"    column="id"    />
        <result property="type"    column="type"    />
        <result property="name"    column="name"    />
        <result property="integral"    column="integral"    />
        <result property="price"    column="price"    />
        <result property="enoughPrice"    column="enough_price"    />
        <result property="createAt"    column="create_at"    />
        <result property="updateAt"    column="update_at"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="isFiring"    column="is_firing"    />
    </resultMap>

    <sql id="selectMallCouponVo">
        select id, `type`, `name`, integral, price, enough_price, create_at, update_at, del_flag, is_firing from mall_coupon
    </sql>

    <select id="selectMallCouponList" parameterType="MallCoupon" resultMap="MallCouponResult">
        <include refid="selectMallCouponVo"/>
        <where>
            <if test="name != null  and name != ''"> and `name` = #{name}</if>
            <if test="type != null  and type != ''"> and `type` = #{type}</if>
            <if test="integral != null "> and integral = #{integral}</if>
            <if test="price != null "> and price = #{price}</if>
            <if test="enoughPrice != null "> and enough_price = #{enoughPrice}</if>
            <if test="createAt != null  and createAt != ''"> and create_at = #{createAt}</if>
            <if test="isFiring != null  and isFiring != ''"> and is_firing = #{isFiring}</if>
        </where>
        order by create_at desc
    </select>

    <select id="selectMallCouponVOList" parameterType="integer" resultType="map">
        select id, `type`, `name`, price, enough_price enoughPrice, create_at createAt from mall_coupon
        <where>
            <if test="type != null and type != -1">and `type` = #{type}</if>
            and is_firing = '0'
            and del_flag = '0'
        </where>
        order by create_at desc
    </select>

    <select id="selectMallCouponById" parameterType="Long" resultMap="MallCouponResult">
        <include refid="selectMallCouponVo"/>
        where id = #{id}
    </select>

    <insert id="insertMallCoupon" parameterType="MallCoupon">
        insert into mall_coupon
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="type != null">`type`,</if>
            <if test="name != null">`name`,</if>
            <if test="integral != null">integral,</if>
            <if test="price != null">price,</if>
            <if test="enoughPrice != null">enough_price,</if>
            <if test="createAt != null">create_at,</if>
            <if test="updateAt != null">update_at,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="isFiring != null">is_firing,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="type != null">#{type},</if>
            <if test="name != null">#{name},</if>
            <if test="integral != null">#{integral},</if>
            <if test="price != null">#{price},</if>
            <if test="enoughPrice != null">#{enoughPrice},</if>
            <if test="createAt != null">#{createAt},</if>
            <if test="updateAt != null">#{updateAt},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="isFiring != null">#{isFiring},</if>
         </trim>
    </insert>

    <update id="updateMallCoupon" parameterType="MallCoupon">
        update mall_coupon
        <trim prefix="SET" suffixOverrides=",">
            <if test="type != null">type = #{type},</if>
            <if test="name != null">`name` = #{name},</if>
            <if test="integral != null">integral = #{integral},</if>
            <if test="price != null">price = #{price},</if>
            <if test="enoughPrice != null">enough_price = #{enoughPrice},</if>
            <if test="createAt != null">create_at = #{createAt},</if>
            <if test="updateAt != null">update_at = #{updateAt},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="isFiring != null">is_firing = #{isFiring},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMallCouponById" parameterType="Long">
        delete from mall_coupon where id = #{id}
    </delete>

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

    <select id="exchangeCouponList" resultType="map">
        select mc.id couponId, mc.type, mc.integral, mc.name, mc.price,
        CASE mc.type
        WHEN '0'
            THEN
            (
                select CASE
                       WHEN ifnull(count(mmc.coupon_id), 0) = 0
                           THEN 1
                       WHEN ifnull(count(mmc.coupon_id), 0) > 0
                           THEN 0
                       ELSE
                           0
                           END
            from mall_member_coupon mmc
            where mmc.user_id = #{userId}
              and mmc.coupon_id = mc.id
                )
        ELSE
        1
        END as isExchange
        from mall_coupon mc
        where mc.del_flag = '0'
          and mc.is_firing = '0'
        order by mc.create_at desc
    </select>
</mapper>
