import { GenericCrudService } from '@/server/utils/generic-crud.service';
import { DataSource } from 'typeorm';
import { MembershipPlan } from './membership-plan.entity';

export class MembershipPlanService extends GenericCrudService<MembershipPlan> {
  constructor(dataSource: DataSource) {
    super(dataSource, MembershipPlan);
  }

  /**
   * 获取启用的套餐列表，按排序升序排列
   */
  async getActivePlans(): Promise<MembershipPlan[]> {
    const [plans] = await this.getList(1, 100, undefined, undefined, { isActive: 1 }, [], { sortOrder: 'ASC' });
    return plans;
  }

  /**
   * 根据类型获取套餐
   */
  async getPlanByType(type: string): Promise<MembershipPlan | null> {
    const plans = await this.repository.findOne({
      where: { type, isActive: 1 },
      order: { sortOrder: 'ASC' }
    });
    return plans;
  }
}