UserCouponMapper.xml 10.4 KB
<?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.yohoufo.dal.promotion.UserCouponMapper" >
  <resultMap id="BaseResultMap" type="com.yohoufo.dal.promotion.model.UserCoupon" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="uid" property="uid" jdbcType="INTEGER" />
    <result column="coupon_id" property="couponId" jdbcType="INTEGER" />
    <result column="coupon_token" property="couponToken" jdbcType="VARCHAR" />
    <result column="coupon_type" property="couponType" jdbcType="INTEGER" />
    <result column="coupon_code" property="couponCode" jdbcType="VARCHAR" />
    <result column="status" property="status" jdbcType="INTEGER" />
    <result column="order_code" property="orderCode" jdbcType="BIGINT" />
    <result column="use_time" property="useTime" jdbcType="INTEGER" />
    <result column="start_time" property="startTime" jdbcType="INTEGER" />
    <result column="end_time" property="endTime" jdbcType="INTEGER" />
    <result column="create_time" property="createTime" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, uid, coupon_id, coupon_type, coupon_code, status, order_code, use_time, start_time, 
    end_time, create_time, coupon_token
  </sql>

  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from user_coupon
    where id = #{id,jdbcType=INTEGER}
  </select>



  <select id="selectByUidAndCouponCodes" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from user_coupon
    where uid = #{uid}
    AND coupon_code IN
    <foreach collection="couponCodes" item="couponCode" open="(" separator="," close=")">
      #{couponCode,jdbcType=VARCHAR}
    </foreach>
  </select>



  <select id="selectByUidAndToken" resultMap="BaseResultMap"  >
    select
    <include refid="Base_Column_List" />
    from user_coupon
    where uid = #{uid,jdbcType=INTEGER}
    and coupon_token =  #{couponToken,jdbcType=VARCHAR}
    limit 1
  </select>


  <select id="selectUsableCouponByUid" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from user_coupon
    where uid = #{uid,jdbcType=INTEGER}
    <include refid="CouponsLogsQueryUsable" />
    order by end_time
  </select>


  <sql id="CouponsLogsQueryUsable" >
    <!-- 在有效时间范围内 -->
    and <![CDATA[ end_time > #{now, jdbcType=INTEGER} ]]>
    <!-- 未使用-->
    and status = 0
  </sql>

  <select id="selectCntUsableCouponByUid" resultType="int" parameterType="java.lang.Integer" >
    select
    count(1)
    from user_coupon
    where uid = #{uid,jdbcType=INTEGER}
    <include refid="CouponsLogsQueryUsable"/>
  </select>


  <insert id="insert" parameterType="com.yohoufo.dal.promotion.model.UserCoupon" >
    insert into user_coupon (id, uid, coupon_id, 
      coupon_type, coupon_code, status, 
      order_code, use_time, start_time, 
      end_time, create_time,coupon_token)
    values (#{id,jdbcType=INTEGER}, #{uid,jdbcType=INTEGER}, #{couponId,jdbcType=INTEGER}, 
      #{couponType,jdbcType=INTEGER}, #{couponCode,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER},
      #{orderCode,jdbcType=BIGINT}, #{useTime,jdbcType=INTEGER}, #{startTime,jdbcType=INTEGER}, 
      #{endTime,jdbcType=INTEGER}, #{createTime,jdbcType=INTEGER}, #{couponToken,jdbcType=VARCHAR})
  </insert>


  <insert id="insertWhere" parameterType="com.yohoufo.dal.promotion.model.UserCoupon" >
    insert into user_coupon (id, uid, coupon_id,
    coupon_type, coupon_code, status,
    order_code, use_time, start_time,
    end_time, create_time,coupon_token)
    select #{id,jdbcType=INTEGER}, #{uid,jdbcType=INTEGER}, #{couponId,jdbcType=INTEGER},
    #{couponType,jdbcType=INTEGER}, #{couponCode,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER},
    #{orderCode,jdbcType=BIGINT}, #{useTime,jdbcType=INTEGER}, #{startTime,jdbcType=INTEGER},
    #{endTime,jdbcType=INTEGER}, #{createTime,jdbcType=INTEGER}, #{couponToken,jdbcType=VARCHAR} from dual
    where not EXISTS (select 1 from user_coupon where uid=#{uid,jdbcType=INTEGER} and coupon_token=#{couponToken,jdbcType=VARCHAR})
  </insert>

  <insert id="insertSelective" parameterType="com.yohoufo.dal.promotion.model.UserCoupon" >
    insert into user_coupon
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="uid != null" >
        uid,
      </if>
      <if test="couponId != null" >
        coupon_id,
      </if>
      <if test="couponType != null" >
        coupon_type,
      </if>
      <if test="couponCode != null" >
        coupon_code,
      </if>
      <if test="status != null" >
        status,
      </if>
      <if test="orderCode != null" >
        order_code,
      </if>
      <if test="useTime != null" >
        use_time,
      </if>
      <if test="startTime != null" >
        start_time,
      </if>
      <if test="endTime != null" >
        end_time,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="uid != null" >
        #{uid,jdbcType=INTEGER},
      </if>
      <if test="couponId != null" >
        #{couponId,jdbcType=INTEGER},
      </if>
      <if test="couponType != null" >
        #{couponType,jdbcType=TINYINT},
      </if>
      <if test="couponCode != null" >
        #{couponCode,jdbcType=VARCHAR},
      </if>
      <if test="status != null" >
        #{status,jdbcType=TINYINT},
      </if>
      <if test="orderCode != null" >
        #{orderCode,jdbcType=BIGINT},
      </if>
      <if test="useTime != null" >
        #{useTime,jdbcType=INTEGER},
      </if>
      <if test="startTime != null" >
        #{startTime,jdbcType=INTEGER},
      </if>
      <if test="endTime != null" >
        #{endTime,jdbcType=INTEGER},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.yohoufo.dal.promotion.model.UserCoupon" >
    update user_coupon
    <set >
      <if test="uid != null" >
        uid = #{uid,jdbcType=INTEGER},
      </if>
      <if test="couponId != null" >
        coupon_id = #{couponId,jdbcType=INTEGER},
      </if>
      <if test="couponType != null" >
        coupon_type = #{couponType,jdbcType=TINYINT},
      </if>
      <if test="couponCode != null" >
        coupon_code = #{couponCode,jdbcType=VARCHAR},
      </if>
      <if test="status != null" >
        status = #{status,jdbcType=TINYINT},
      </if>
      <if test="orderCode != null" >
        order_code = #{orderCode,jdbcType=BIGINT},
      </if>
      <if test="useTime != null" >
        use_time = #{useTime,jdbcType=INTEGER},
      </if>
      <if test="startTime != null" >
        start_time = #{startTime,jdbcType=INTEGER},
      </if>
      <if test="endTime != null" >
        end_time = #{endTime,jdbcType=INTEGER},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.yohoufo.dal.promotion.model.UserCoupon" >
    update user_coupon
    set uid = #{uid,jdbcType=INTEGER},
      coupon_id = #{couponId,jdbcType=INTEGER},
      coupon_type = #{couponType,jdbcType=TINYINT},
      coupon_code = #{couponCode,jdbcType=VARCHAR},
      status = #{status,jdbcType=TINYINT},
      order_code = #{orderCode,jdbcType=BIGINT},
      use_time = #{useTime,jdbcType=INTEGER},
      start_time = #{startTime,jdbcType=INTEGER},
      end_time = #{endTime,jdbcType=INTEGER},
      create_time = #{createTime,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>


  <update id="updateCouponNotUse" parameterType="com.yohoufo.dal.promotion.model.CouponCancelUseDO">
    update user_coupon
    <set>
      status = 0,use_time = 0, order_code =0
    </set>
    where uid= #{uid}
    and order_code= #{orderCode}
    and status = 1
    and coupon_code in
    <foreach collection="couponCodes" index="index" item="couponCode"
             open="(" separator="," close=")">
      #{couponCode}
    </foreach>
  </update>

  <update id="updateCoupon2Use" parameterType="com.yohoufo.dal.promotion.model.CouponUseDO">
    update user_coupon
    <set>
      <if test="orderCode != null">
        order_code= #{orderCode},
      </if>
      <if test="useTime != null">
        use_time= #{useTime},
      </if>
      status=1
    </set>
    where uid= #{uid}
    and coupon_code in
    <foreach collection="couponCodes" index="index" item="couponCode"
             open="(" separator="," close=")">
      #{couponCode}
    </foreach>
    and status = 0
    and order_code= 0
  </update>

  <!--extract public where condition -->
  <sql id="relationQueryWhereForUserCouponList">
    where a.uid = #{uid}
    <if test="couponType > 0">
      <![CDATA[
			AND a.coupon_type = #{couponType}
        	]]>
    </if>
    <if test="status == 0">
      <![CDATA[
			AND a.status = 0
			AND a.end_time > #{nowTime}
        	]]>
    </if>
    <!-- 已使用 -->
    <if test="status == 1">
      <![CDATA[
			AND a.status = 1
        	]]>
    </if>
    <!-- 已失效或已过期的优惠券,只查询一个月数据 -->
    <if test="status == 2">
      <![CDATA[
			AND (
			a.status = 2
			or (a.status = 0 AND a.end_time < #{nowTime})
			)
			AND a.end_time >(#{nowTime}-2592000)
        	]]>
    </if>
  </sql>

  <select id="selectUserCouponListCount" resultType="java.lang.Integer" parameterType="com.yohoufo.dal.promotion.model.UserCouponListQueryParams">
    select
    count(1)
    from user_coupon a
    <include refid="relationQueryWhereForUserCouponList" />
  </select>

  <select id="selectUserCouponList" resultMap="BaseResultMap" parameterType="com.yohoufo.dal.promotion.model.UserCouponListQueryParams">
    select
      <include refid="Base_Column_List" />
    from user_coupon a
    <include refid="relationQueryWhereForUserCouponList"/>
    <if test="status == 0">
      order by a.create_time desc
    </if>
    <if test="status == 1">
      order by a.use_time desc
    </if>
    <if test="status == 2">
      order by a.end_time desc
    </if>
    <if test="limitStart > 0 and size > 0">
      limit #{limitStart}, #{size}
    </if>
  </select>

</mapper>