package com.ycwl.basic.pricing.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ycwl.basic.pricing.entity.PriceVoucherBatchConfig;
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 PriceVoucherBatchConfigMapper extends BaseMapper<PriceVoucherBatchConfig> {
    
    /**
     * 根据景区ID和推客ID查询有效的批次列表
     * @param scenicId 景区ID
     * @param brokerId 推客ID
     * @return 批次列表
     */
    @Select("SELECT id, batch_name, scenic_id, broker_id, discount_type, discount_value, applicable_products, " +
            "total_count, used_count, claimed_count, status, create_time, update_time, " +
            "create_by, update_by, deleted, deleted_at " +
            "FROM price_voucher_batch_config WHERE scenic_id = #{scenicId} AND broker_id = #{brokerId} " +
            "AND status = 1 AND deleted = 0 ORDER BY create_time DESC")
    List<PriceVoucherBatchConfig> selectActiveBatchesByScenicAndBroker(@Param("scenicId") Long scenicId,
                                                                        @Param("brokerId") Long brokerId);
    
    /**
     * 更新批次的已领取数量
     * @param batchId 批次ID
     * @param increment 增量（可为负数）
     * @return 影响行数
     */
    @Update("UPDATE price_voucher_batch_config SET claimed_count = claimed_count + #{increment}, " +
            "update_time = NOW() WHERE id = #{batchId} AND deleted = 0")
    int updateClaimedCount(@Param("batchId") Long batchId, @Param("increment") Integer increment);
    
    /**
     * 更新批次的已使用数量
     * @param batchId 批次ID
     * @param increment 增量（可为负数）
     * @return 影响行数
     */
    @Update("UPDATE price_voucher_batch_config SET used_count = used_count + #{increment}, " +
            "update_time = NOW() WHERE id = #{batchId} AND deleted = 0")
    int updateUsedCount(@Param("batchId") Long batchId, @Param("increment") Integer increment);
    
    /**
     * 获取批次统计信息
     * @param batchId 批次ID
     * @return 统计信息
     */
    @Select("SELECT id, batch_name, scenic_id, broker_id, discount_type, discount_value, applicable_products, " +
            "total_count, used_count, claimed_count, status, create_time, update_time, " +
            "create_by, update_by, deleted, deleted_at " +
            "FROM price_voucher_batch_config WHERE id = #{batchId} AND deleted = 0")
    PriceVoucherBatchConfig selectBatchStats(@Param("batchId") Long batchId);

    /**
     * 根据券码查询对应的券码批次配置
     * @param voucherCode 券码
     * @return 券码批次配置
     */
    @Select("SELECT b.id, b.batch_name, b.scenic_id, b.broker_id, b.discount_type, b.discount_value, b.applicable_products, " +
            "b.total_count, b.used_count, b.claimed_count, b.status, b.create_time, b.update_time, " +
            "b.create_by, b.update_by, b.deleted, b.deleted_at " +
            "FROM price_voucher_batch_config b INNER JOIN price_voucher_code c ON b.id = c.batch_id " +
            "WHERE c.code = #{voucherCode} AND b.deleted = 0 AND c.deleted = 0")
    PriceVoucherBatchConfig selectByVoucherCode(@Param("voucherCode") String voucherCode);
}