# encoding: utf-8
class Employ < ActiveRecord::Base
  self.table_name = "employs"
  validates :mobile,:user_name,presence: true
  validate :employ_validation
  def employ_validation
    usr = User.where("tel = ?", self.mobile).first
    if !usr.blank?
        wu = WxUser.where("user_id = ?", usr.id).first
        if !wu.blank?
            self.wx_user_id = wu.id
        end
    else
      self.errors.add(:mobile,"该手机号尚未绑定微信账号，请员工预先绑定小程序！")
    end
  end
  rails_admin do
    navigation_label '食堂管理'
    weight -500
    list do
      filters [:mobile,:wx_user_id,:user_name]
      field :id
      field :user_name
      field :mobile
      field :wx_user_id
      field :created_at
      # field :updated_at
    end
    show do
      field :id
      field :user_name
      field :mobile
      field :wx_user_id
      field :created_at
      field :updated_at
    end
    edit do
      field :user_name
      field :mobile
    end
  end
end
