<?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.iotechn.unimall.data.mapper.CouponMapper">

    <update id="decCoupon">
        update unimall_coupon set surplus = surplus - 1 where id = #{couponId}
    </update>

    <select id="getUserCouponsCount" resultType="com.iotechn.unimall.data.model.KVModel">
        SELECT
            coupon_id AS `key`,
            count( coupon_id ) AS `value`
        FROM
            unimall_user_coupon
        WHERE
            coupon_id IN
        <foreach open="(" close=")" collection="couponIds" separator="," item="cid">
            #{cid}
        </foreach>
            AND user_id = #{userId}
        GROUP BY
            coupon_id;
    </select>


    <select id="getAdminCouponList" resultType="com.iotechn.unimall.data.dto.CouponAdminDTO">
        SELECT
          c.id AS id,
          c.title AS title,
          c.`type` AS `type`,
          c.description AS description,
          c.total AS total,
          c.surplus AS surplus,
          c.`limit` AS `limit`,
          c.discount AS discount,
          c.min AS min,
          c.status AS status,
          g.title as categoryTitle,
          c.category_id as categoryId,
          c.days AS days,
          c.gmt_start as gmtStart,
          c.gmt_end as gmtEnd
        FROM
          unimall_coupon c
        LEFT JOIN unimall_category g on c.category_id = g.id
        WHERE
        1 = 1
        <if test="title != null">
            AND c.title like concat("%",#{title},"%")
        </if>
        <if test="type != null">
            AND c.type = #{type}
        </if>
        <if test="status != null">
          <if test="status >= 0">
              AND c.status = #{status} AND (unix_timestamp(c.gmt_end) &gt; unix_timestamp(#{now}) or c.days is not null)
          </if>
          <if test="status &lt; 0">
              AND unix_timestamp(c.gmt_end) &lt; unix_timestamp(#{now})
          </if>
        </if>
        order BY c.id DESC
        limit #{offset},#{limit}
    </select>

    <select id="getActiveCoupons" resultType="com.iotechn.unimall.data.dto.CouponDTO">
        SELECT
            c.id,
            c.title,
            c.type,
            c.description,
            c.total,
            c.surplus,
            c.`limit`,
            c.discount,
            c.`min`,
            c.`status`,
            c.category_id AS categoryId,
            c.days,
            c.gmt_start AS gmtStart,
            c.gmt_end AS gmtEnd,
            c.gmt_update AS gmtUpdate,
            c.gmt_create AS gmtCreate,
            y.title AS categoryTitle
        FROM
            unimall_coupon AS c
        LEFT JOIN unimall_category AS y ON y.id = c.category_id
        WHERE
            c.`status` = 1
            AND (c.gmt_end IS NULL OR ( unix_timestamp(c.gmt_start) &lt; unix_timestamp(now( )) AND unix_timestamp(c.gmt_end) &gt; unix_timestamp(now( )) ))
    </select>


</mapper>