package com.ycwl.basic.pricing.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ycwl.basic.pricing.entity.PriceBundleConfig;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 一口价配置Mapper
 */
@Mapper
public interface PriceBundleConfigMapper extends BaseMapper<PriceBundleConfig> {
    
    /**
     * 查询启用的一口价配置
     */
    @Select("SELECT id, bundle_name, scenic_id, bundle_price, " +
            "included_products, excluded_products, " +
            "description, is_active, can_use_coupon, can_use_voucher, create_time, update_time " +
            "FROM price_bundle_config WHERE is_active = 1")
    @Results({
        @Result(column = "included_products", property = "includedProducts", 
                typeHandler = com.ycwl.basic.pricing.handler.BundleProductListTypeHandler.class),
        @Result(column = "excluded_products", property = "excludedProducts", 
                typeHandler = com.ycwl.basic.pricing.handler.BundleProductListTypeHandler.class)
    })
    List<PriceBundleConfig> selectActiveBundles();
    
    /**
     * 根据ID查询启用的配置
     */
    @Select("SELECT id, bundle_name, scenic_id, bundle_price, " +
            "included_products, excluded_products, " +
            "description, is_active, can_use_coupon, can_use_voucher, create_time, update_time " +
            "FROM price_bundle_config WHERE id = #{id} AND is_active = 1")
    @Results({
        @Result(column = "included_products", property = "includedProducts", 
                typeHandler = com.ycwl.basic.pricing.handler.BundleProductListTypeHandler.class),
        @Result(column = "excluded_products", property = "excludedProducts", 
                typeHandler = com.ycwl.basic.pricing.handler.BundleProductListTypeHandler.class)
    })
    PriceBundleConfig selectActiveBundleById(Long id);
    
    // ==================== 管理端接口（包含禁用的配置） ====================
    
    /**
     * 查询所有一口价配置（包含禁用的）- 管理端使用
     */
    @Select("SELECT id, bundle_name, scenic_id, bundle_price, " +
            "included_products, excluded_products, " +
            "description, is_active, can_use_coupon, can_use_voucher, create_time, update_time " +
            "FROM price_bundle_config ORDER BY is_active DESC, bundle_name ASC")
    @Results({
        @Result(column = "included_products", property = "includedProducts", 
                typeHandler = com.ycwl.basic.pricing.handler.BundleProductListTypeHandler.class),
        @Result(column = "excluded_products", property = "excludedProducts", 
                typeHandler = com.ycwl.basic.pricing.handler.BundleProductListTypeHandler.class)
    })
    List<PriceBundleConfig> selectAllBundlesForAdmin();
    
    /**
     * 插入一口价配置
     */
    @Insert("INSERT INTO price_bundle_config (bundle_name, scenic_id, bundle_price, included_products, excluded_products, " +
            "description, is_active, can_use_coupon, can_use_voucher, create_time, update_time) VALUES " +
            "(#{bundleName}, #{scenicId}, #{bundlePrice}, #{includedProducts,typeHandler=com.ycwl.basic.pricing.handler.BundleProductListTypeHandler}, #{excludedProducts,typeHandler=com.ycwl.basic.pricing.handler.BundleProductListTypeHandler}, " +
            "#{description}, #{isActive}, #{canUseCoupon}, #{canUseVoucher}, NOW(), NOW())")
    int insertBundleConfig(PriceBundleConfig config);
    
    /**
     * 更新一口价配置
     */
    @Update("UPDATE price_bundle_config SET bundle_name = #{bundleName}, scenic_id = #{scenicId}, bundle_price = #{bundlePrice}, " +
            "included_products = #{includedProducts,typeHandler=com.ycwl.basic.pricing.handler.BundleProductListTypeHandler}, excluded_products = #{excludedProducts,typeHandler=com.ycwl.basic.pricing.handler.BundleProductListTypeHandler}, " +
            "description = #{description}, is_active = #{isActive}, can_use_coupon = #{canUseCoupon}, can_use_voucher = #{canUseVoucher}, update_time = NOW() WHERE id = #{id}")
    int updateBundleConfig(PriceBundleConfig config);
    
    /**
     * 更新一口价配置状态
     */
    @Update("UPDATE price_bundle_config SET is_active = #{isActive}, update_time = NOW() WHERE id = #{id}")
    int updateBundleConfigStatus(@Param("id") Long id, @Param("isActive") Boolean isActive);
}