class CompetitionTeam < ApplicationRecord CODE_CHARS = %W(2 3 4 5 6 7 8 9 a b c f e f g h i j k l m n o p q r s t u v w x y z).freeze belongs_to :user belongs_to :competition has_many :team_members, dependent: :destroy has_many :users, through: :team_members, source: :user has_many :competition_scores, dependent: :destroy has_many :members, -> { without_teachers }, class_name: 'TeamMember' has_many :teachers, -> { only_teachers }, class_name: 'TeamMember' has_many :competition_prize_users, dependent: :destroy has_many :competition_course_records, dependent: :destroy def group_team_type? team_type.zero? end def competition_prize competition_prize_users.take&.competition_prize&.name end def personal_team_type? team_type == 1 end def en_team_type group_team_type? ? 'group' : 'personal' end def generate_invite_code code = CODE_CHARS.sample(6).join while self.class.exists?(invite_code: code) code = CODE_CHARS.sample(6).join end self.invite_code = code code end def teachers_info info = "" teachers.each do |teacher| teacher_info = "#{teacher.user.real_name}/#{teacher.user.school_name}" info += info == "" ? teacher_info : "、#{teacher_info}" end info end def teachers_name teachers.map{|teacher| teacher.user.real_name}.join(",") end def members_name members.map{|member| member.user.real_name}.join(",") end def all_prize_approved? !competition_prize_users.exists?(status: :pending) end def certificate_exists? Util::FileManage.exists?(self, self.class.certificate_suffix) end def certificate_url Util::FileManage.source_disk_file_url(self, self.class.certificate_suffix) end def certificate_path Util::FileManage.source_disk_filename(self, self.class.certificate_suffix) end def self.certificate_suffix '_CERT' end end