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<PriceCouponConfig> {
    
    /**
     * 查询有效的优惠券配置（可领取的）
     */
    @Select("SELECT * FROM price_coupon_config WHERE is_active = 1 " +
            "AND valid_from <= NOW() AND valid_until > NOW() " +
            "AND (total_quantity IS NULL OR total_quantity <= 0 OR COALESCE(claimed_quantity, 0) < total_quantity)")
    List<PriceCouponConfig> selectValidCoupons();

    /**
     * 根据ID查询优惠券（包括库存检查）
     */
    @Select("SELECT * FROM price_coupon_config WHERE id = #{couponId} " +
            "AND is_active = 1 AND valid_from <= NOW() AND valid_until > NOW() " +
            "AND (total_quantity IS NULL OR total_quantity <= 0 OR COALESCE(claimed_quantity, 0) < total_quantity)")
    PriceCouponConfig selectValidCouponById(Long couponId);
    
    /**
     * 增加优惠券使用数量（支持无限量优惠券）
     * 当 total_quantity 为 NULL 或 <= 0 时表示不限制使用数量
     */
    @Update("UPDATE price_coupon_config SET used_quantity = COALESCE(used_quantity, 0) + 1, " +
            "update_time = NOW() WHERE id = #{couponId} " +
            "AND (total_quantity IS NULL OR total_quantity <= 0 OR COALESCE(used_quantity, 0) < total_quantity)")
    int incrementUsedQuantity(Long couponId);

    /**
     * 原子性增加已领取数量（仅对有限库存的优惠券生效，带库存检查）
     */
    @Update("UPDATE price_coupon_config SET claimed_quantity = COALESCE(claimed_quantity, 0) + 1, " +
            "update_time = NOW() WHERE id = #{couponId} AND total_quantity IS NOT NULL AND total_quantity > 0 " +
            "AND COALESCE(claimed_quantity, 0) < total_quantity")
    int incrementClaimedQuantityIfAvailable(@Param("couponId") Long couponId);

    /**
     * 无条件增加已领取数量（用于无限量优惠券的领取统计）
     */
    @Update("UPDATE price_coupon_config SET claimed_quantity = COALESCE(claimed_quantity, 0) + 1, " +
            "update_time = NOW() WHERE id = #{couponId}")
    int incrementClaimedQuantity(@Param("couponId") Long couponId);
    
    /**
     * 插入优惠券配置
     */
    @Insert("INSERT INTO price_coupon_config (coupon_name, coupon_type, discount_value, min_amount, " +
            "max_discount, applicable_products, required_attribute_keys, total_quantity, used_quantity, " +
            "claimed_quantity, user_claim_limit, valid_from, valid_until, valid_days_after_claim, " +
            "is_active, scenic_id, create_time, update_time) VALUES " +
            "(#{couponName}, #{couponType}, #{discountValue}, #{minAmount}, #{maxDiscount}, " +
            "#{applicableProducts}, #{requiredAttributeKeys}, #{totalQuantity}, #{usedQuantity}, " +
            "#{claimedQuantity}, #{userClaimLimit}, #{validFrom}, #{validUntil}, #{validDaysAfterClaim}, " +
            "#{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}, required_attribute_keys = #{requiredAttributeKeys}, " +
            "total_quantity = #{totalQuantity}, user_claim_limit = #{userClaimLimit}, " +
            "valid_from = #{validFrom}, valid_until = #{validUntil}, valid_days_after_claim = #{validDaysAfterClaim}, " +
            "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<PriceCouponConfig> selectAllForAdmin();
    
    /**
     * 管理端：根据条件查询优惠券配置（支持分页）
     */
    @Select("<script>" +
            "SELECT * FROM price_coupon_config " +
            "<where>" +
            "<if test='isActive != null'>" +
            "AND is_active = #{isActive}" +
            "</if>" +
            "<if test='couponName != null and couponName != \"\"'>" +
            "AND coupon_name LIKE CONCAT('%', #{couponName}, '%')" +
            "</if>" +
            "<if test='scenicId != null and scenicId != \"\"'>" +
            "AND scenic_id = #{scenicId}" +
            "</if>" +
            "</where>" +
            "ORDER BY create_time DESC" +
            "</script>")
    List<PriceCouponConfig> 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<PriceCouponConfig> 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 (total_quantity IS NULL OR total_quantity <= 0 OR COALESCE(claimed_quantity, 0) < total_quantity) " +
            "AND (scenic_id IS NULL OR scenic_id = #{scenicId})")
    List<PriceCouponConfig> selectValidCouponsByScenicId(@Param("scenicId") String scenicId);
    
    /**
     * 管理端：根据景区ID查询优惠券配置
     */
    @Select("SELECT * FROM price_coupon_config WHERE scenic_id = #{scenicId} ORDER BY create_time DESC")
    List<PriceCouponConfig> 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<String, Object> selectScenicCouponConfigStats(@Param("scenicId") String scenicId);
}
