<?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.system.mapper.CouponsMapper">

    <resultMap type="Coupons" id="CouponsResult">
        <result property="id"    column="id"    />
        <result property="title"    column="title"    />
        <result property="price"    column="price"    />
        <result property="minPrice"    column="min_price"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="count"    column="count"    />
        <result property="isPermanent"    column="is_permanent"    />
        <result property="status"    column="status"    />
        <result property="couponTime"    column="coupon_time"    />
        <result property="productId"    column="product_id"    />
        <result property="type"    column="type"    />
        <result property="receiveType"    column="receive_type"    />
        <result property="sort"    column="sort"    />
        <result property="cateId"    column="cate_id"    />
        <result property="userIds"    column="user_ids"    />
        <result property="createdAt"    column="created_at"    />
        <result property="updatedAt"    column="updated_at"    />
        <result property="deletedAt"    column="deleted_at"    />
    </resultMap>

    <resultMap id="CouponsCouponUserResult" type="Coupons" extends="CouponsResult">
        <collection property="couponUserList" ofType="CouponUser" column="id" select="selectCouponUserList" />
    </resultMap>

    <resultMap type="CouponUser" id="CouponUserResult">
        <result property="id"    column="id"    />
        <result property="uid"    column="uid"    />
        <result property="couponId"    column="coupon_id"    />
        <result property="couponTitle"    column="coupon_title"    />
        <result property="couponPrice"    column="coupon_price"    />
        <result property="minPrice"    column="min_price"    />
        <result property="addTime"    column="add_time"    />
        <result property="loseTime"    column="lose_time"    />
        <result property="useTime"    column="use_time"    />
        <result property="cateId"    column="cate_id"    />
        <result property="productId"    column="product_id"    />
        <result property="receiveType"    column="receive_type"    />
        <result property="status"    column="status"    />
        <result property="createdAt"    column="created_at"    />
        <result property="updatedAt"    column="updated_at"    />
    </resultMap>

    <sql id="selectCouponsVo">
        select id, title, price, min_price, start_time, end_time, count, is_permanent, status, coupon_time, product_id, type, receive_type, sort, cate_id, user_ids, created_at, updated_at, deleted_at from coupons
    </sql>

    <select id="selectCouponsList" parameterType="Coupons" resultMap="CouponsResult">
        <include refid="selectCouponsVo"/>
        <where>
            <if test="title != null  and title != ''"> and title like concat('%', #{title}, '%')</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="productId != null "> and product_id = #{productId}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="sort != null  and sort != ''"> and sort = #{sort}</if>
            <if test="isPermanent != null  and isPermanent != ''"> and is_permanent = #{isPermanent}</if>

            <if test="priceMin != null and priceMax != null">
                and price BETWEEN #{priceMin} AND #{priceMax}
            </if>
            <if test="minPriceMin != null and minPriceMax != null">
                and min_price BETWEEN #{minPriceMin} AND #{minPriceMax}
            </if>
            <if test="countMin != null and countMax != null">
                and count BETWEEN #{countMin} AND #{countMax}
            </if>
        </where>
    </select>

    <select id="selectCouponsById" parameterType="Long" resultMap="CouponsCouponUserResult">
        select id, title, price, min_price, start_time, end_time, count, is_permanent, status, coupon_time, product_id, type, receive_type, sort, cate_id, user_ids, created_at, updated_at, deleted_at
        from coupons
        where id = #{id}
    </select>

    <select id="selectCouponUserList" resultMap="CouponUserResult">
        select id, uid, coupon_id, coupon_title, coupon_price, min_price, add_time, lose_time, use_time, cate_id, product_id, receive_type, status, created_at, updated_at
        from coupon_user
        where coupon_id = #{coupon_id}
    </select>

    <insert id="insertCoupons" parameterType="Coupons" useGeneratedKeys="true" keyProperty="id">
        insert into coupons
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">title,</if>
            <if test="price != null">price,</if>
            <if test="minPrice != null">min_price,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="count != null">count,</if>
            <if test="isPermanent != null">is_permanent,</if>
            <if test="status != null">status,</if>
            <if test="couponTime != null">coupon_time,</if>
            <if test="productId != null">product_id,</if>
            <if test="type != null">type,</if>
            <if test="receiveType != null">receive_type,</if>
            <if test="sort != null and sort != ''">sort,</if>
            <if test="cateId != null">cate_id,</if>
            <if test="userIds != null">user_ids,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="updatedAt != null">updated_at,</if>
            <if test="deletedAt != null">deleted_at,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null and title != ''">#{title},</if>
            <if test="price != null">#{price},</if>
            <if test="minPrice != null">#{minPrice},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="count != null">#{count},</if>
            <if test="isPermanent != null">#{isPermanent},</if>
            <if test="status != null">#{status},</if>
            <if test="couponTime != null">#{couponTime},</if>
            <if test="productId != null">#{productId},</if>
            <if test="type != null">#{type},</if>
            <if test="receiveType != null">#{receiveType},</if>
            <if test="sort != null and sort != ''">#{sort},</if>
            <if test="cateId != null">#{cateId},</if>
            <if test="userIds != null">#{userIds},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="updatedAt != null">#{updatedAt},</if>
            <if test="deletedAt != null">#{deletedAt},</if>
        </trim>
    </insert>

    <update id="updateCoupons" parameterType="Coupons">
        update coupons
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="price != null">price = #{price},</if>
            <if test="minPrice != null">min_price = #{minPrice},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="count != null">count = #{count},</if>
            <if test="isPermanent != null">is_permanent = #{isPermanent},</if>
            <if test="status != null">status = #{status},</if>
            <if test="couponTime != null">coupon_time = #{couponTime},</if>
            <if test="productId != null">product_id = #{productId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="receiveType != null">receive_type = #{receiveType},</if>
            <if test="sort != null and sort != ''">sort = #{sort},</if>
            <if test="cateId != null">cate_id = #{cateId},</if>
            <if test="userIds != null">user_ids = #{userIds},</if>
            <if test="createdAt != null">created_at = #{createdAt},</if>
            <if test="updatedAt != null">updated_at = #{updatedAt},</if>
            <if test="deletedAt != null">deleted_at = #{deletedAt},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCouponsById" parameterType="Long">
        delete from coupons where id = #{id}
    </delete>

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

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

    <delete id="deleteCouponUserByCouponId" parameterType="Long">
        delete from coupon_user where coupon_id = #{couponId}
    </delete>

    <insert id="batchCouponUser">
        insert into coupon_user( id, uid, coupon_id, coupon_title, coupon_price, min_price, add_time, lose_time, use_time, cate_id, product_id, receive_type, status, created_at, updated_at) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.uid}, #{item.couponId}, #{item.couponTitle}, #{item.couponPrice}, #{item.minPrice}, #{item.addTime}, #{item.loseTime}, #{item.useTime}, #{item.cateId}, #{item.productId}, #{item.receiveType}, #{item.status}, #{item.createdAt}, #{item.updatedAt})
        </foreach>
    </insert>
</mapper>