Sha256: d49edce84bf05027c0443b44676fa486348461f85212311fc55d2a8d78ddea6e

Contents?: true

Size: 1.9 KB

Versions: 20

Compression:

Stored size: 1.9 KB

Contents

module EducodeSales
  class Staff < ApplicationRecord
    belongs_to :user
    belongs_to :role, optional: true

    has_many :follow_ups, dependent: :destroy
    has_many :operation_plans, dependent: :destroy
    has_many :operation_reports, dependent: :destroy
    has_many :sale_reports, dependent: :destroy
    has_many :teacher_follows, dependent: :destroy
    has_many :teachers, dependent: :destroy
    has_many :places
    has_many :businesses, dependent: :destroy
    has_many :activities, dependent: :destroy
    has_many :sale_plans, dependent: :destroy
    has_many :staff_schools, dependent: :destroy

    has_many :login_histories, dependent: :destroy

    has_many :market_areas, dependent: :destroy
    has_many :areas, through: :market_areas
    validates :user_id, uniqueness: { message: '已存在' }

    # attr_writer :month

    def last_login_key
      "login_user_#{self.id}"
    end

    def check_login_status(request)
      unless Rails.cache.data.get(self.last_login_key)
        # 第二天还在线访问,记用户登录一次
        create_login_history(request)
      end
    end

    def create_login_history(request)
      # 登录状态保存到当天结束时间后+10分钟
      Rails.cache.data.set(self.last_login_key, 1)
      Rails.cache.data.expireat(self.last_login_key, Time.now.end_of_day.to_i + 600)

      last_history = self.login_histories.last
      self.login_histories.create(current_ip: request.remote_ip, last_ip: last_history&.current_ip, last_login_at: last_history&.created_at)
    end

    def self.month_list
      list = []
      24.times.map do |d|
        ["#{d + 1}个月", d + 1]
      end
    end

    def area_ids
      self.market_areas.pluck(:area_id)
    end

    def month
      if expired_at.present? && enabled_at.present?
        (expired_at.year * 12 + expired_at.month) - (enabled_at.year * 12 + enabled_at.month)
      else
        nil
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
educode_sales-0.9.22 app/models/educode_sales/staff.rb
educode_sales-0.9.21 app/models/educode_sales/staff.rb
educode_sales-0.9.20 app/models/educode_sales/staff.rb
educode_sales-0.9.19 app/models/educode_sales/staff.rb
educode_sales-0.9.18 app/models/educode_sales/staff.rb
educode_sales-0.9.17 app/models/educode_sales/staff.rb
educode_sales-0.9.16 app/models/educode_sales/staff.rb
educode_sales-0.9.15 app/models/educode_sales/staff.rb
educode_sales-0.9.14 app/models/educode_sales/staff.rb
educode_sales-0.9.13 app/models/educode_sales/staff.rb
educode_sales-0.9.12 app/models/educode_sales/staff.rb
educode_sales-0.9.11 app/models/educode_sales/staff.rb
educode_sales-0.9.10 app/models/educode_sales/staff.rb
educode_sales-0.9.9 app/models/educode_sales/staff.rb
educode_sales-0.9.8 app/models/educode_sales/staff.rb
educode_sales-0.9.7 app/models/educode_sales/staff.rb
educode_sales-0.9.6 app/models/educode_sales/staff.rb
educode_sales-0.9.5 app/models/educode_sales/staff.rb
educode_sales-0.9.4 app/models/educode_sales/staff.rb
educode_sales-0.9.3 app/models/educode_sales/staff.rb