# encoding: utf-8
require 'date' # 包含DateTime
require 'time'
class UserPerfomance < ActiveRecord::Base
  has_paper_trail
  self.table_name = "user_perfomances"
  belongs_to :wx_user, :foreign_key => :wx_user_id
  #validates_presence_of :wx_user_id

  #after_create :user_perfomance
  #after_update :user_perfomance

  validates :wx_user_id,:begin_date,:end_date,presence: true
  def user_perfomance
      #统计订单商品销售数据
      url = "#{CONFIG_FILE["api_host"]}/railsadmin/user/perfomance/#{self.id}"
      p url
      open(url)
  end


  def begin_date_fomat
    if !self.begin_date.nil?
      #return self.begin_date.strftime
      return self.begin_date.strftime("%Y-%m-%d")
    end
  end

  def end_date_fomat
    if !self.end_date.nil?
      #return self.end_date.strftime
      return self.end_date.strftime("%Y-%m-%d")
    end
  end


  rails_admin do
    navigation_label '统计汇总'
    weight -500
    list do
      filters [:wx_user_id,:begin_date,:end_date]
      field :id
      field :begin_date do
        visible false
        filterable true
      end
      field :end_date do
        visible false
        filterable true
      end
      field :begin_date_fomat
      field :end_date_fomat
      field :wx_user_id do
        filterable true
      end
      field :nickname

      field :order_perfomance do
        visible true
        label "普通订单业绩(元)"
        formatted_value do
          value!=0 ? (value/100.0):0
        end
      end
      field :shop_order_perfomance do
        visible true
        label "店长专区订单业绩(元)"
        formatted_value do
          value!=0 ? (value/100.0):0
        end
      end
      field :shop_perfomance do
        visible true
        label "开店业绩(元)"
        formatted_value do
          value!=0 ? (value/100.0):0
        end
      end
      field :balance_perfomance do
        visible true
        label "充值业绩(元)"
        formatted_value do
          value!=0 ? (value/100.0):0
        end
      end
      field :total do
        visible true
        label "合计业绩(元)"
        formatted_value do
          value!=0 ? (value/100.0):0
        end
      end
      field :nums
      field :created_at
      field :updated_at
    end

    show do
      field :id
      field :begin_date_fomat
      field :end_date_fomat
      field :wx_user_id
      field :nickname
      field :order_perfomance
      field :shop_order_perfomance
      field :shop_perfomance
      field :balance_perfomance
      field :total
      field :created_at
      field :updated_at
    end

    edit do
      field :begin_date
      field :end_date
      field :wx_user_id
    end
  end

end
