package com.ycwl.basic.pricing.mapper;

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

import java.util.List;

/**
 * 一口价配置Mapper
 */
@Mapper
public interface PriceOnePriceConfigMapper extends BaseMapper<PriceOnePriceConfig> {
    
    /**
     * 查询启用的一口价配置
     */
    @Select("SELECT * FROM price_one_price_config WHERE is_active = 1 AND deleted = 0 " +
            "ORDER BY scenic_id")
    List<PriceOnePriceConfig> selectActiveConfigs();
    
    /**
     * 根据景区ID查询启用的配置
     */
    @Select("SELECT * FROM price_one_price_config " +
            "WHERE is_active = 1 AND deleted = 0 " +
            "AND scenic_id = #{scenicId} " +
            "LIMIT 1")
    PriceOnePriceConfig selectConfigByScenic(@Param("scenicId") Long scenicId);
    
    /**
     * 查询特定景区的所有配置
     */
    @Select("SELECT * FROM price_one_price_config " +
            "WHERE is_active = 1 AND deleted = 0 " +
            "AND scenic_id = #{scenicId}")
    List<PriceOnePriceConfig> selectConfigsByScenic(@Param("scenicId") Long scenicId);
    
    /**
     * 根据ID查询启用的配置
     */
    @Select("SELECT * FROM price_one_price_config " +
            "WHERE id = #{id} AND is_active = 1 AND deleted = 0")
    PriceOnePriceConfig selectActiveConfigById(Long id);
    
    /**
     * 查询所有配置（包含禁用的）- 管理端使用
     */
    @Select("SELECT * FROM price_one_price_config WHERE deleted = 0 " +
            "ORDER BY is_active DESC, create_time DESC")
    List<PriceOnePriceConfig> selectAllConfigsForAdmin();
    
    /**
     * 插入配置
     */
    @Insert("INSERT INTO price_one_price_config " +
            "(config_name, scenic_id, one_price, original_price, description, is_active, " +
            "start_time, end_time, can_use_coupon, can_use_voucher, " +
            "create_time, update_time, create_by, deleted) " +
            "VALUES " +
            "(#{configName}, #{scenicId}, #{onePrice}, #{originalPrice}, #{description}, #{isActive}, " +
            "#{startTime}, #{endTime}, #{canUseCoupon}, #{canUseVoucher}, " +
            "NOW(), NOW(), #{createBy}, 0)")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertConfig(PriceOnePriceConfig config);
    
    /**
     * 更新配置
     */
    @Update("UPDATE price_one_price_config SET " +
            "config_name = #{configName}, scenic_id = #{scenicId}, " +
            "one_price = #{onePrice}, original_price = #{originalPrice}, " +
            "description = #{description}, is_active = #{isActive}, " +
            "start_time = #{startTime}, end_time = #{endTime}, " +
            "can_use_coupon = #{canUseCoupon}, can_use_voucher = #{canUseVoucher}, " +
            "update_time = NOW(), update_by = #{updateBy} " +
            "WHERE id = #{id} AND deleted = 0")
    int updateConfig(PriceOnePriceConfig config);
    
    /**
     * 更新配置状态
     */
    @Update("UPDATE price_one_price_config SET is_active = #{isActive}, update_time = NOW() " +
            "WHERE id = #{id} AND deleted = 0")
    int updateConfigStatus(@Param("id") Long id, @Param("isActive") Boolean isActive);
    
    /**
     * 逻辑删除配置
     */
    @Update("UPDATE price_one_price_config SET deleted = 1, deleted_at = NOW() " +
            "WHERE id = #{id}")
    int deleteConfig(Long id);
}