package com.dora.mapper;

import com.dora.entity.PointsPackage;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 积分套餐Mapper
 */
@Mapper
@Repository
public interface PointsPackageMapper {

    /**
     * 根据ID查询套餐
     */
    @Select("SELECT * FROM points_package WHERE id = #{id} AND is_deleted = 0")
    PointsPackage selectById(@Param("id") Long id);

    /**
     * 查询所有上架的套餐（按排序）
     */
    @Select("SELECT * FROM points_package WHERE is_active = 1 AND is_deleted = 0 ORDER BY sort_order ASC, id ASC")
    List<PointsPackage> selectActivePackages();

    /**
     * 查询热门套餐
     */
    @Select("SELECT * FROM points_package WHERE is_active = 1 AND is_hot = 1 AND is_deleted = 0 ORDER BY sort_order ASC LIMIT #{limit}")
    List<PointsPackage> selectHotPackages(@Param("limit") Integer limit);

    /**
     * 查询所有套餐（管理端）
     */
    @Select("SELECT * FROM points_package WHERE is_deleted = 0 ORDER BY sort_order ASC, id DESC")
    List<PointsPackage> selectAll();

    /**
     * 插入套餐
     */
    @Insert("INSERT INTO points_package (name, description, points, bonus_points, total_points, price, original_price, " +
            "points_expire_days, discount_label, sort_order, is_hot, is_active, create_time, update_time) " +
            "VALUES (#{name}, #{description}, #{points}, #{bonusPoints}, #{totalPoints}, #{price}, #{originalPrice}, " +
            "#{pointsExpireDays}, #{discountLabel}, #{sortOrder}, #{isHot}, #{isActive}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(PointsPackage pointsPackage);

    /**
     * 更新套餐
     */
    @Update("UPDATE points_package SET name = #{name}, description = #{description}, points = #{points}, " +
            "bonus_points = #{bonusPoints}, total_points = #{totalPoints}, price = #{price}, original_price = #{originalPrice}, " +
            "points_expire_days = #{pointsExpireDays}, discount_label = #{discountLabel}, sort_order = #{sortOrder}, " +
            "is_hot = #{isHot}, is_active = #{isActive}, update_time = NOW() WHERE id = #{id}")
    int updateById(PointsPackage pointsPackage);

    /**
     * 更新套餐状态
     */
    @Update("UPDATE points_package SET is_active = #{isActive}, update_time = NOW() WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("isActive") Integer isActive);

    /**
     * 逻辑删除套餐
     */
    @Update("UPDATE points_package SET is_deleted = 1, update_time = NOW() WHERE id = #{id}")
    int deleteById(@Param("id") Long id);

    /**
     * 统计套餐数量
     */
    @Select("SELECT COUNT(*) FROM points_package WHERE is_deleted = 0")
    Long countAll();

    /**
     * 统计上架套餐数量
     */
    @Select("SELECT COUNT(*) FROM points_package WHERE is_active = 1 AND is_deleted = 0")
    Long countActive();
}

