UserCouponMapper.xml 9.48 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="user_type" property="userType" 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, user_type, coupon_type, coupon_code, status, order_code, use_time, start_time,
    end_time, create_time, coupon_token
  </sql>

  <sql id="A_Base_Column_List" >
    a.id, a.uid, a.coupon_id, a.user_type, a.coupon_type, a.coupon_code, a.status, a.order_code, a.use_time, a.start_time,
    a.end_time, a.create_time, a.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}
    <if test="userType != null">
      and user_type = #{userType,jdbcType=INTEGER}
    </if>
    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}
    <if test="userType != null">
      and user_type = #{userType,jdbcType=INTEGER}
    </if>
    <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">
    select count(*) from user_coupon,coupon where coupon.id = user_coupon.coupon_id
    <if test="businessClient!=null">
      and (FIND_IN_SET(#{businessClient}, coupon.business_client)
            or coupon.business_client is null)
    </if>
    and user_coupon.uid = #{uid,jdbcType=INTEGER}
    <if test="userType != null">
      and user_type = #{userType,jdbcType=INTEGER}
    </if>
    <!-- 在有效时间范围内 -->
    and <![CDATA[ user_coupon.end_time > #{now, jdbcType=INTEGER} ]]>
    <!-- 未使用-->
    and user_coupon.status = 0
  </select>


  <insert id="insert" parameterType="com.yohoufo.dal.promotion.model.UserCoupon" >
    insert into user_coupon (id, uid, coupon_id, user_type,
      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}, #{userType,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>

  <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="updateCoupon2Invalidate">
    update user_coupon
    <set>
      status = 2
    </set>
    where uid= #{uid}
    and coupon_code= #{couponCode}
    and status = 0
  </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
  </update>

  <!--extract public where condition -->
  <sql id="relationQueryWhereForUserCouponList">
    where
    a.coupon_id = b.id
    and a.uid = #{uid}
    <if test="userType != null and userType > 0">
      and a.user_type = #{userType,jdbcType=INTEGER}
    </if>
    <if test="businessClient != null">
      and (FIND_IN_SET(#{businessClient},b.business_client)
            or b.business_client is null)
    </if>
    <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, coupon b
    <include refid="relationQueryWhereForUserCouponList" />
  </select>

  <select id="selectUserCouponList" resultMap="BaseResultMap" parameterType="com.yohoufo.dal.promotion.model.UserCouponListQueryParams">
    select
      <include refid="A_Base_Column_List" />
    from user_coupon a, coupon b
    <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>


  <select id="selectUserCouponListByCouponIds" resultMap="BaseResultMap" >
    select
    <include refid="Base_Column_List" />
    from user_coupon
    where uid = #{uid,jdbcType=INTEGER}
    and coupon_id in
    <foreach collection="couponIds" item="couponId" open="(" close=")" separator=",">
      #{couponId,jdbcType=INTEGER}
    </foreach>
  </select>
</mapper>