# encoding:utf-8
class PushTmpl < ActiveRecord::Base
  has_paper_trail
  belongs_to :wx_gongzhonghao
  self.table_name = "push_tmpls"
  MSG_TYPE_ENUM = [
    ["订单待支付提醒","0"],
    ["新项目提醒", "1"],
    ["提货通知","2"],
    ["订购成功通知","3"],
    ["一元购退款通知","4"]
  ]
  def get_push_count
    if self.push_count.to_i >0
      return self.push_count 
    end
    if !self.user_id.blank?
      if self.user_id.start_with?("sql=select * ")
        sql = self.user_id.gsub("sql=select * ","select count(*) as sqlcount ")
        item = WxUser.find_by_sql(sql)
        if !item.blank?
          c = item[0].sqlcount
          if self.push_count == 0 
            self.push_count  = c
            self.save
          end
          return c
        end
      end
    end
  end

  def get_click_count
    if self.click_count.to_i >0
      return self.click_count 
    end
    if self.url.blank?
      return 
    end
    if self.url.start_with?("#{CONFIG_FILE["api_host"]}/v1/cfc/")
      size = "#{CONFIG_FILE["api_host"]}/v1/cfc/".size
      id = self.url[size..-1].to_i
      if id>0 
        item = ClickFromChannel.where("id=?",id).first
        if !item.blank?
          if item.click_times !=self.click_count 
            self.click_count  = item.click_times
            self.save
          end
          return item.click_times
        end
      end
    end
  end

  rails_admin do
    navigation_label '推送管理'
    weight -220

    list do
      items_per_page 10
      filters [:id, :user_id, :first]
      field :id
      field :user_id
      field :get_push_count
      field :push_count
      field :msg_type, :enum do
        enum do
          MSG_TYPE_ENUM
        end
      end
      field :push_force
      field :first
      field :keyword1
      field :keyword2
      field :keyword3
      field :keyword4
      field :keyword5
      field :url
      field :last_updated_at
      field :times
      field :get_click_count
      field :click_count
      field :remark
      field :wx_gongzhonghao
      field :created_at
    end

    show do
      field :id
      field :user_id
      field :msg_type, :enum do
        enum do
          MSG_TYPE_ENUM
        end
      end
      field :push_force
      field :first
      field :keyword1
      field :keyword2
      field :keyword3
      field :keyword4
      field :keyword5
      field :url
      field :last_updated_at
      field :times
      field :remark
      field :wx_gongzhonghao
      field :created_at
      field :updated_at
    end

    edit do
      field :user_id do
        label "微信用户ID(注意：拉比兔公众号的openid记录在wx_user_gongzhonghaos表中的gzh_open_id)"
      end
      field :msg_type, :enum do
        enum do
          MSG_TYPE_ENUM
        end
      end
      field :push_force
      field :push_count
      field :click_count
      field :first
      field :keyword1
      field :keyword2
      field :keyword3
      field :keyword4
      field :keyword5
      field :url
      field :last_updated_at
      field :times
      field :remark
      field :wx_gongzhonghao
    end
  end

end
