package com.dora.mapper;

import com.dora.entity.MembershipPlan;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 会员套餐Mapper接口
 */
@Mapper
public interface MembershipPlanMapper {

    /**
     * 根据ID查询套餐
     * @param id 套餐ID
     * @return 套餐实体
     */
    @Select("SELECT id, name, description, price, duration_days, target_role, discount_percentage, is_active, create_time, update_time, is_deleted FROM membership_plan WHERE id = #{id} AND is_deleted = 0")
    MembershipPlan selectById(@Param("id") Long id);

    /**
     * 获取所有上架的套餐列表
     * @return 套餐列表
     */
    @Select("SELECT id, name, description, price, duration_days, target_role, discount_percentage, is_active, create_time, update_time, is_deleted FROM membership_plan WHERE is_active = 1 AND is_deleted = 0 ORDER BY target_role ASC, duration_days ASC")
    List<MembershipPlan> selectAllActive();

    /**
     * 根据目标角色获取套餐列表
     * @param targetRole 目标角色
     * @return 套餐列表
     */
    @Select("SELECT id, name, description, price, duration_days, target_role, discount_percentage, is_active, create_time, update_time, is_deleted FROM membership_plan WHERE target_role = #{targetRole} AND is_active = 1 AND is_deleted = 0 ORDER BY duration_days ASC")
    List<MembershipPlan> selectByTargetRole(@Param("targetRole") Integer targetRole);

    /**
     * 获取所有套餐列表（包括已下架的）
     * @return 套餐列表
     */
    @Select("SELECT id, name, description, price, duration_days, target_role, discount_percentage, is_active, create_time, update_time, is_deleted FROM membership_plan WHERE is_deleted = 0 ORDER BY create_time DESC")
    List<MembershipPlan> selectAll();

    /**
     * 插入新套餐
     * @param plan 套餐实体
     * @return 影响行数
     */
    @org.apache.ibatis.annotations.Insert("INSERT INTO membership_plan (name, description, price, duration_days, target_role, discount_percentage, is_active, create_time, update_time, is_deleted) " +
            "VALUES (#{name}, #{description}, #{price}, #{durationDays}, #{targetRole}, #{discountPercentage}, #{isActive}, #{createTime}, #{updateTime}, #{isDeleted})")
    @org.apache.ibatis.annotations.Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(MembershipPlan plan);

    /**
     * 根据ID更新套餐
     * @param plan 套餐实体
     * @return 影响行数
     */
    @org.apache.ibatis.annotations.Update("UPDATE membership_plan SET name = #{name}, description = #{description}, " +
            "price = #{price}, duration_days = #{durationDays}, target_role = #{targetRole}, " +
            "discount_percentage = #{discountPercentage}, is_active = #{isActive}, update_time = #{updateTime}, is_deleted = #{isDeleted} " +
            "WHERE id = #{id}")
    int updateById(MembershipPlan plan);
}
