<?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.xxfc.platform.activity.mapper.CouponMapper">

	<!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.xxfc.platform.activity.vo.ActivityCouponVo" id="couponMap">
        <result property="id" column="id"/>
        <result property="title" column="title"/>
        <result property="icon" column="icon"/>
        <result property="used" column="used"/>
        <result property="channel" column="channel"/>
        <result property="userType" column="user_type"/>
        <result property="type" column="type"/>
        <result property="withSpecial" column="with_special"/>
        <result property="withSn" column="with_sn"/>
        <result property="withAmount" column="with_amount"/>
        <result property="usedAmount" column="used_amount"/>
        <result property="quota" column="quota"/>
        <result property="limitCollar" column="limit_collar"/>
        <result property="takeCount" column="take_count"/>
        <result property="usedCount" column="used_count"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="validType" column="valid_type"/>
        <result property="validStartTime" column="valid_start_time"/>
        <result property="validEndTime" column="valid_end_time"/>
        <result property="validDays" column="valid_days"/>
        <result property="status" column="status"/>
        <result property="url" column="url"/>
        <result property="crtUser" column="crt_user"/>
        <result property="crtName" column="crt_name"/>
        <result property="crtTime" column="crt_time"/>
        <result property="updUser" column="upd_user"/>
        <result property="updName" column="upd_name"/>
        <result property="updTime" column="upd_time"/>
        <result property="isDel" column="is_del"/>
        <result property="isLed" column="isLed"/>
        <result property="ledExpireTime" column="ledExpireTime"/>
    </resultMap>

    <select id="selectTitles" resultType="com.xxfc.platform.activity.vo.CouponTitleVo">
        select `id`,`title` from `coupon` where `is_del`=0
    </select>


    <select id="getCoupons" resultType="com.xxfc.platform.activity.entity.Coupon" >
        select  id ,title,used_amount from coupon
    </select>


  <select id="couponsByTickerNoList" resultType="com.xxfc.platform.activity.entity.Coupon">
      select  c.* from  user_coupon u left join coupon c on u.coupon_id=c.id
      <where>
          u.ticker_no in
          <foreach collection="tickerNoList" item="no" index="index" open="(" separator="," close=")">
               #{no}
          </foreach>
      </where>

  </select>

    <select id="findCouponsByIds" resultType="com.xxfc.platform.activity.dto.CouponDTO">
     select `id`,`title` from  `coupon` where is_del=0
     <if test="ids!=null and ids.size>0">
       and id in <foreach collection="ids" item="id" open="(" close=")" separator=",">
         #{id}
     </foreach>
     </if>
    </select>

    <select id="selectList" resultMap="couponMap" parameterType="com.xxfc.platform.activity.dto.CouponFindDTO">
        select  c.*,IF(COUNT(uc.id) >0 ,1,0) as isLed,IFNULL(uc.expire_time,0) as ledExpireTime  from `coupon` c
        LEFT JOIN  (SELECT * FROM user_coupon
        WHERE 1=1
        <if test="userId != null and userId >= 0">
            and  user_id=#{userId}
        </if>
        ) uc ON c.id=uc.coupon_id
        where c.`is_del`=0
        <if test="isLed != null and isLed >= 0">
            and c.is_led=#{isLed}
        </if>
        <if test="status != null and status >= 0">
            and c.status=#{status}
        </if>
        <if test="ledTime != null and ledTime > 0L" >
            and (c.start_time &lt;= #{ledTime} or c.start_time  = 0) and (c.end_time >= #{ledTime} or c.end_time  = 0)
        </if>
        GROUP BY c.id
    </select>
</mapper>