package com.ycwl.basic.pricing.service.impl;

import com.ycwl.basic.pricing.entity.PriceProductConfig;
import com.ycwl.basic.pricing.entity.PriceTierConfig;
import com.ycwl.basic.pricing.exception.ProductConfigNotFoundException;
import com.ycwl.basic.pricing.mapper.PriceProductConfigMapper;
import com.ycwl.basic.pricing.mapper.PriceTierConfigMapper;
import com.ycwl.basic.pricing.service.IProductConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品配置管理服务实现
 */
@Slf4j
@Service("pricingProductConfigServiceImpl")
@RequiredArgsConstructor
public class ProductConfigServiceImpl implements IProductConfigService {
    
    private final PriceProductConfigMapper productConfigMapper;
    private final PriceTierConfigMapper tierConfigMapper;
    
    @Override
//    @Cacheable(value = "product-config", key = "#productType")
    public List<PriceProductConfig> getProductConfig(String productType) {
        return productConfigMapper.selectByProductType(productType);
    }
    
    @Override
//    @Cacheable(value = "product-config", key = "#productType + '_' + #productId")
    public PriceProductConfig getProductConfig(String productType, String productId) {
        PriceProductConfig config = productConfigMapper.selectByProductTypeAndId(productType, productId);
        if (config == null) {
            throw new ProductConfigNotFoundException("商品配置未找到: " + productType + ", productId: " + productId);
        }
        return config;
    }
    
    @Override
//    @Cacheable(value = "tier-config", key = "#productType + '_' + #productId + '_' + #quantity")
    public PriceTierConfig getTierConfig(String productType, String productId, Integer quantity) {
        PriceTierConfig config = tierConfigMapper.selectByProductTypeAndQuantity(productType, productId, quantity);
        
        // 如果没有找到特定商品的阶梯配置，尝试使用default配置
        if (config == null && !"default".equals(productId)) {
            log.warn("阶梯定价配置未找到: productType={}, productId={}, quantity={}, 尝试使用default配置", 
                    productType, productId, quantity);
            config = tierConfigMapper.selectByProductTypeAndQuantity(productType, "default", quantity);
            if (config != null) {
                log.debug("使用default阶梯配置: productType={}, quantity={}, price={}", 
                        productType, quantity, config.getPrice());
            }
        }
        
        if (config == null) {
            log.warn("阶梯定价配置未找到: productType={}, productId={}, quantity={}", 
                    productType, productId, quantity);
        }
        return config;
    }
    
    @Override
//    @Cacheable(value = "active-product-configs")
    public List<PriceProductConfig> getActiveProductConfigs() {
        return productConfigMapper.selectActiveConfigs();
    }
    
    @Override
//    @Cacheable(value = "all-product-configs")
    public List<PriceProductConfig> getAllProductConfigs() {
        return productConfigMapper.selectActiveConfigs();
    }
    
    @Override
//    @Cacheable(value = "tier-configs", key = "#productType")
    public List<PriceTierConfig> getTierConfigs(String productType) {
        return tierConfigMapper.selectByProductType(productType);
    }
    
    @Override
//    @Cacheable(value = "tier-configs", key = "#productType + '_' + #productId")
    public List<PriceTierConfig> getTierConfigs(String productType, String productId) {
        return tierConfigMapper.selectByProductTypeAndId(productType, productId);
    }
    
    @Override
//    @Cacheable(value = "all-tier-configs")
    public List<PriceTierConfig> getAllTierConfigs() {
        return tierConfigMapper.selectAllActiveConfigs();
    }
    
    // ==================== 管理端接口（包含禁用的配置） ====================
    
    @Override
    public List<PriceProductConfig> getAllProductConfigsForAdmin() {
        return productConfigMapper.selectAllConfigsForAdmin();
    }
    
    @Override
    public List<PriceTierConfig> getAllTierConfigsForAdmin() {
        return tierConfigMapper.selectAllConfigsForAdmin();
    }
    
    @Override
    public List<PriceTierConfig> getTierConfigsForAdmin(String productType) {
        return tierConfigMapper.selectByProductTypeForAdmin(productType);
    }
    
    @Override
    public List<PriceTierConfig> getTierConfigsForAdmin(String productType, String productId) {
        return tierConfigMapper.selectByProductTypeAndIdForAdmin(productType, productId);
    }
}