132 lines
5.2 KiB
XML
132 lines
5.2 KiB
XML
<?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.central.coupon.mapper.CouponMapper">
|
|
|
|
<!-- 结果映射 -->
|
|
<resultMap id="BaseResultMap" type="com.central.coupon.entity.Coupon">
|
|
<id column="id" property="id" jdbcType="BIGINT"/>
|
|
<result column="coupon_name" property="couponName" jdbcType="VARCHAR"/>
|
|
<result column="coupon_type" property="couponType" jdbcType="VARCHAR"/>
|
|
<result column="amount" property="amount" jdbcType="DECIMAL"/>
|
|
<result column="amount_currency" property="amountCurrency" jdbcType="VARCHAR"/>
|
|
<result column="user_phone" property="userPhone" jdbcType="VARCHAR"/>
|
|
<result column="ctrip_coupon_id" property="ctripCouponId" jdbcType="VARCHAR"/>
|
|
<result column="ctrip_order_id" property="ctripOrderId" jdbcType="VARCHAR"/>
|
|
<result column="status" property="status" jdbcType="VARCHAR"/>
|
|
<result column="valid_start_time" property="validStartTime" jdbcType="TIMESTAMP"/>
|
|
<result column="valid_end_time" property="validEndTime" jdbcType="TIMESTAMP"/>
|
|
<result column="used_time" property="usedTime" jdbcType="TIMESTAMP"/>
|
|
<result column="used_order_no" property="usedOrderNo" jdbcType="VARCHAR"/>
|
|
<result column="remark" property="remark" jdbcType="VARCHAR"/>
|
|
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
|
|
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
|
|
<result column="create_by" property="createBy" jdbcType="VARCHAR"/>
|
|
<result column="update_by" property="updateBy" jdbcType="VARCHAR"/>
|
|
<result column="tenant_id" property="tenantId" jdbcType="VARCHAR"/>
|
|
<result column="deleted" property="deleted" jdbcType="TINYINT"/>
|
|
</resultMap>
|
|
|
|
<!-- 基础字段 -->
|
|
<sql id="Base_Column_List">
|
|
id, coupon_name, coupon_type, amount, amount_currency, user_phone, ctrip_coupon_id, ctrip_order_id,
|
|
status, valid_start_time, valid_end_time, used_time, used_order_no, remark,
|
|
create_time, update_time, create_by, update_by, tenant_id, deleted
|
|
</sql>
|
|
|
|
<!-- 分页查询优惠券列表 -->
|
|
<select id="selectCouponPage" resultMap="BaseResultMap">
|
|
SELECT
|
|
<include refid="Base_Column_List"/>
|
|
FROM t_coupon
|
|
<where>
|
|
AND deleted = 0
|
|
<if test="coupon.couponName != null and coupon.couponName != ''">
|
|
AND coupon_name LIKE CONCAT('%', #{coupon.couponName}, '%')
|
|
</if>
|
|
<if test="coupon.userPhone != null and coupon.userPhone != ''">
|
|
AND user_phone = #{coupon.userPhone}
|
|
</if>
|
|
<if test="coupon.status != null and coupon.status != ''">
|
|
AND status = #{coupon.status}
|
|
</if>
|
|
<if test="coupon.couponType != null and coupon.couponType != ''">
|
|
AND coupon_type = #{coupon.couponType}
|
|
</if>
|
|
</where>
|
|
ORDER BY create_time DESC
|
|
</select>
|
|
|
|
<!-- 根据手机号查询用户优惠券 -->
|
|
<select id="selectByUserPhone" resultMap="BaseResultMap">
|
|
SELECT
|
|
<include refid="Base_Column_List"/>
|
|
FROM t_coupon
|
|
WHERE user_phone = #{userPhone}
|
|
AND deleted = 0
|
|
ORDER BY create_time DESC
|
|
</select>
|
|
|
|
<!-- 根据手机号查询可用优惠券 -->
|
|
<select id="selectAvailableByUserPhone" resultMap="BaseResultMap">
|
|
SELECT
|
|
<include refid="Base_Column_List"/>
|
|
FROM t_coupon
|
|
WHERE user_phone = #{userPhone}
|
|
AND status = 'UNUSED'
|
|
AND valid_start_time <= NOW()
|
|
AND valid_end_time >= NOW()
|
|
AND deleted = 0
|
|
ORDER BY create_time DESC
|
|
</select>
|
|
|
|
<!-- 根据携程优惠券ID查询 -->
|
|
<select id="selectByCtripCouponId" resultMap="BaseResultMap">
|
|
SELECT
|
|
<include refid="Base_Column_List"/>
|
|
FROM t_coupon
|
|
WHERE ctrip_coupon_id = #{ctripCouponId}
|
|
AND deleted = 0
|
|
LIMIT 1
|
|
</select>
|
|
|
|
<!-- 根据订单号查询使用的优惠券 -->
|
|
<select id="selectByUsedOrderNo" resultMap="BaseResultMap">
|
|
SELECT
|
|
<include refid="Base_Column_List"/>
|
|
FROM t_coupon
|
|
WHERE used_order_no = #{usedOrderNo}
|
|
AND deleted = 0
|
|
LIMIT 1
|
|
</select>
|
|
|
|
<!-- 更新优惠券状态为已使用 -->
|
|
<update id="updateCouponUsed">
|
|
UPDATE t_coupon
|
|
SET status = 'USED',
|
|
used_time = NOW(),
|
|
used_order_no = #{usedOrderNo},
|
|
update_time = NOW()
|
|
WHERE id = #{id}
|
|
AND deleted = 0
|
|
</update>
|
|
|
|
<!-- 批量更新过期优惠券状态 -->
|
|
<update id="updateExpiredCoupons">
|
|
UPDATE t_coupon
|
|
SET status = 'EXPIRED',
|
|
update_time = NOW()
|
|
WHERE status = 'UNUSED'
|
|
AND valid_end_time < NOW()
|
|
AND deleted = 0
|
|
</update>
|
|
|
|
<!-- 检查用户手机号和携程优惠券ID是否已存在 -->
|
|
<select id="countByUserPhoneAndCtripCouponId" resultType="int">
|
|
SELECT COUNT(*)
|
|
FROM t_coupon
|
|
WHERE user_phone = #{userPhone}
|
|
AND ctrip_coupon_id = #{ctripCouponId}
|
|
AND deleted = 0
|
|
</select>
|
|
|
|
</mapper> |