package com.ycwl.basic.pricing.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ycwl.basic.pricing.entity.PriceTierConfig;
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 PriceTierConfigMapper extends BaseMapper<PriceTierConfig> {
    
    /**
     * 根据商品类型、商品ID和数量查询匹配的阶梯价格
     */
    @Select("SELECT * FROM price_tier_config WHERE product_type = #{productType} " +
            "AND product_id = #{productId} " +
            "AND #{quantity} >= min_quantity AND #{quantity} <= max_quantity " +
            "AND is_active = 1 ORDER BY sort_order ASC LIMIT 1")
    PriceTierConfig selectByProductTypeAndQuantity(@Param("productType") String productType,
                                                   @Param("productId") String productId,
                                                   @Param("quantity") Integer quantity);

    /**
     * 根据商品类型、商品ID和数量查询全局匹配的阶梯价格（排除景区级配置）
     */
    @Select("SELECT * FROM price_tier_config WHERE product_type = #{productType} " +
            "AND product_id = #{productId} AND (scenic_id IS NULL OR scenic_id = '') " +
            "AND #{quantity} >= min_quantity AND #{quantity} <= max_quantity " +
            "AND is_active = 1 ORDER BY sort_order ASC LIMIT 1")
    PriceTierConfig selectGlobalByProductTypeAndQuantity(@Param("productType") String productType,
                                                         @Param("productId") String productId,
                                                         @Param("quantity") Integer quantity);

    /**
     * 根据商品类型、商品ID、数量和景区ID查询匹配的阶梯价格（支持景区维度）
     */
    @Select("SELECT * FROM price_tier_config WHERE product_type = #{productType} " +
            "AND product_id = #{productId} AND scenic_id = #{scenicId} " +
            "AND #{quantity} >= min_quantity AND #{quantity} <= max_quantity " +
            "AND is_active = 1 ORDER BY sort_order ASC LIMIT 1")
    PriceTierConfig selectByProductTypeQuantityAndScenic(@Param("productType") String productType,
                                                          @Param("productId") String productId,
                                                          @Param("quantity") Integer quantity,
                                                          @Param("scenicId") String scenicId);
    
    /**
     * 根据商品类型查询所有阶梯配置
     */
    @Select("SELECT * FROM price_tier_config WHERE product_type = #{productType} " +
            "AND is_active = 1 ORDER BY sort_order ASC")
    List<PriceTierConfig> selectByProductType(String productType);
    
    /**
     * 根据商品类型和商品ID查询所有阶梯配置
     */
    @Select("SELECT * FROM price_tier_config WHERE product_type = #{productType} " +
            "AND product_id = #{productId} AND is_active = 1 ORDER BY sort_order ASC")
    List<PriceTierConfig> selectByProductTypeAndId(@Param("productType") String productType,
                                                   @Param("productId") String productId);
    
    
    /**
     * 查询所有启用的阶梯配置
     */
    @Select("SELECT * FROM price_tier_config WHERE is_active = 1 ORDER BY product_type ASC, sort_order ASC")
    List<PriceTierConfig> selectAllActiveConfigs();
    
    /**
     * 检查是否存在default阶梯配置（包含禁用的）
     */
    @Select("SELECT COUNT(*) FROM price_tier_config WHERE product_type = #{productType} AND product_id = 'default'")
    int countDefaultTierConfigsByProductType(@Param("productType") String productType);
    
    // ==================== 管理端接口（包含禁用的配置） ====================
    
    /**
     * 查询所有阶梯配置（包含禁用的）- 管理端使用
     */
    @Select("SELECT * FROM price_tier_config ORDER BY product_type ASC, is_active DESC, sort_order ASC")
    List<PriceTierConfig> selectAllConfigsForAdmin();
    
    /**
     * 根据商品类型查询所有阶梯配置（包含禁用的）- 管理端使用
     */
    @Select("SELECT * FROM price_tier_config WHERE product_type = #{productType} " +
            "ORDER BY is_active DESC, sort_order ASC")
    List<PriceTierConfig> selectByProductTypeForAdmin(@Param("productType") String productType);
    
    /**
     * 根据商品类型和商品ID查询所有阶梯配置（包含禁用的）- 管理端使用
     */
    @Select("SELECT * FROM price_tier_config WHERE product_type = #{productType} " +
            "AND product_id = #{productId} ORDER BY is_active DESC, sort_order ASC")
    List<PriceTierConfig> selectByProductTypeAndIdForAdmin(@Param("productType") String productType,
                                                           @Param("productId") String productId);
    
    /**
     * 插入阶梯定价配置
     */
    @Insert("INSERT INTO price_tier_config (product_type, product_id, scenic_id, min_quantity, max_quantity, price, " +
            "original_price, unit, sort_order, is_active, create_time, update_time) VALUES " +
            "(#{productType}, #{productId}, #{scenicId}, #{minQuantity}, #{maxQuantity}, #{price}, " +
            "#{originalPrice}, #{unit}, #{sortOrder}, #{isActive}, NOW(), NOW())")
    int insertTierConfig(PriceTierConfig config);
    
    /**
     * 更新阶梯定价配置
     */
    @Update("UPDATE price_tier_config SET product_id = #{productId}, scenic_id = #{scenicId}, min_quantity = #{minQuantity}, " +
            "max_quantity = #{maxQuantity}, price = #{price}, original_price = #{originalPrice}, unit = #{unit}, sort_order = #{sortOrder}, " +
            "is_active = #{isActive}, update_time = NOW() WHERE id = #{id}")
    int updateTierConfig(PriceTierConfig config);
    
    /**
     * 更新阶梯配置状态
     */
    @Update("UPDATE price_tier_config SET is_active = #{isActive}, update_time = NOW() WHERE id = #{id}")
    int updateTierConfigStatus(@Param("id") Long id, @Param("isActive") Boolean isActive);
}