class Job < ApplicationRecord
  include AASM

  default_scope -> { where.not state: 'deleted'}

  belongs_to :job_classify
  belongs_to :user

  has_one :apply_action, as: :container

  has_many :apply_jobs

  scope :published, -> { where state: 'published' }

  validates :num, presence: true, allow_blank: true, numericality: { greater_than_or_equal_to: 1 }
  validates :name ,:job_category, :job_classify_id, :city, :education, :work_year, :min_salary, :max_salary, :description, presence: { message: '不能为空' }


  HOT_CITY = ['北京', '深圳', '上海', '广州', '长沙']

  enum education: { '学历不限' => 1, '本科' => 2, '硕士' => 3, '博士' => 4, '大专' => 5, '中技' => 6, '中专' => 7, '高中' => 8 }
  enum work_year: { '经验不限' => 1, '1年以下' => 2, '1-3年' => 3, '3-5年' => 4, '5-10年' => 5, '10年以上' => 6 }
  enum job_category: ['全职', '兼职', '实习']


  aasm(:state) do
    state :drafted, initial: true
    state :published
    state :rejected
    state :offline
    state :deleted

    event :publish do
      transitions from: [:drafted], to: :published
    end

    event :off do
      transitions from: [:published], to: :offline
    end

    event :online do
      transitions from: [:offline], to: :published
    end

    event :reject do
      transitions from: [:drafted, :published], to: :rejected
    end

    event :del do
      transitions from: [:drafted, :published, :offline, :rejected], to: :deleted
    end
  end



  def salary_range
    if self.min_salary.present?
      "#{(self.min_salary / 1000) + (self.min_salary % 1000 > 0 ? (self.min_salary % 1000 / 1000.0).round(1) : 0 )}-#{(self.max_salary / 1000) + (self.max_salary % 1000 > 0 ? (self.max_salary % 1000 / 1000.0).round(1) : 0 )}k"
    else
      '面议'
    end
  end
end


