package com.ycwl.basic.pricing.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.ycwl.basic.pricing.entity.PriceCouponConfig; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import java.util.List; /** * 优惠券配置Mapper */ @Mapper public interface PriceCouponConfigMapper extends BaseMapper { /** * 查询有效的优惠券配置 */ @Select("SELECT * FROM price_coupon_config WHERE is_active = 1 " + "AND valid_from <= NOW() AND valid_until > NOW() " + "AND used_quantity < total_quantity") List selectValidCoupons(); /** * 根据ID查询优惠券(包括使用数量检查) */ @Select("SELECT * FROM price_coupon_config WHERE id = #{couponId} " + "AND is_active = 1 AND valid_from <= NOW() AND valid_until > NOW() " + "AND used_quantity < total_quantity") PriceCouponConfig selectValidCouponById(Long couponId); /** * 增加优惠券使用数量 */ @Update("UPDATE price_coupon_config SET used_quantity = used_quantity + 1, " + "update_time = NOW() WHERE id = #{couponId} AND used_quantity < total_quantity") int incrementUsedQuantity(Long couponId); /** * 插入优惠券配置 */ @Insert("INSERT INTO price_coupon_config (coupon_name, coupon_type, discount_value, min_amount, " + "max_discount, applicable_products, total_quantity, used_quantity, valid_from, valid_until, " + "is_active, scenic_id, create_time, update_time) VALUES " + "(#{couponName}, #{couponType}, #{discountValue}, #{minAmount}, #{maxDiscount}, " + "#{applicableProducts}, #{totalQuantity}, #{usedQuantity}, #{validFrom}, #{validUntil}, " + "#{isActive}, #{scenicId}, NOW(), NOW())") int insertCoupon(PriceCouponConfig coupon); /** * 更新优惠券配置 */ @Update("UPDATE price_coupon_config SET coupon_name = #{couponName}, coupon_type = #{couponType}, " + "discount_value = #{discountValue}, min_amount = #{minAmount}, max_discount = #{maxDiscount}, " + "applicable_products = #{applicableProducts}, total_quantity = #{totalQuantity}, " + "valid_from = #{validFrom}, valid_until = #{validUntil}, is_active = #{isActive}, " + "scenic_id = #{scenicId}, update_time = NOW() WHERE id = #{id}") int updateCoupon(PriceCouponConfig coupon); // ==================== 管理端接口 ==================== /** * 管理端:查询所有优惠券配置(包含禁用的) */ @Select("SELECT * FROM price_coupon_config ORDER BY create_time DESC") List selectAllForAdmin(); /** * 管理端:根据条件查询优惠券配置(支持分页) */ @Select("") List selectByConditionsForAdmin(@Param("isActive") Boolean isActive, @Param("couponName") String couponName, @Param("scenicId") String scenicId); /** * 管理端:根据状态查询优惠券配置 */ @Select("SELECT * FROM price_coupon_config WHERE is_active = #{isActive} ORDER BY create_time DESC") List selectByStatusForAdmin(@Param("isActive") Boolean isActive); /** * 管理端:更新优惠券状态 */ @Update("UPDATE price_coupon_config SET is_active = #{isActive}, update_time = NOW() WHERE id = #{id}") int updateCouponStatus(@Param("id") Long id, @Param("isActive") Boolean isActive); /** * 管理端:删除优惠券配置 */ @Update("UPDATE price_coupon_config SET deleted = 1, update_time = NOW() WHERE id = #{id}") int deleteCoupon(Long id); /** * 查询指定景区的有效优惠券配置 */ @Select("SELECT * FROM price_coupon_config WHERE is_active = 1 " + "AND valid_from <= NOW() AND valid_until > NOW() " + "AND used_quantity < total_quantity " + "AND (scenic_id IS NULL OR scenic_id = #{scenicId})") List selectValidCouponsByScenicId(@Param("scenicId") String scenicId); /** * 管理端:根据景区ID查询优惠券配置 */ @Select("SELECT * FROM price_coupon_config WHERE scenic_id = #{scenicId} ORDER BY create_time DESC") List selectByScenicIdForAdmin(@Param("scenicId") String scenicId); /** * 统计景区优惠券配置数量 */ @Select("SELECT " + "COUNT(*) as total_coupons, " + "COUNT(CASE WHEN is_active = 1 THEN 1 END) as active_coupons, " + "SUM(total_quantity) as total_quantity, " + "SUM(used_quantity) as used_quantity " + "FROM price_coupon_config WHERE scenic_id = #{scenicId}") java.util.Map selectScenicCouponConfigStats(@Param("scenicId") String scenicId); }