Class: EricWeixin::CustomsServiceRecord

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/eric_weixin/customs_service_record.rb

Constant Summary collapse

OPERCODE =
{
  1000 => "创建未接入会话",
  1001 =>	"接入会话",
  1002 =>	"主动发起会话",
  1004 =>	"关闭会话",
  1005 =>	"抢接会话",
  2001 =>	"公众号收到消息",
  2002 =>	"客服发送消息",
  2003 =>	"客服收到消息"
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.batch_get_customs_records(chat_date) ⇒ Object

批量获取用户与客服的聊天记录保存到数据库.

参数说明

  • chat_date #聊天日期

调用说明

::EricWeixin::CustomsServiceRecord.batch_get_customs_records '2015-6-9'.to_time


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/eric_weixin/customs_service_record.rb', line 39

def self.batch_get_customs_records chat_date
  self.transaction do
    chat_date = chat_date.to_time
    start_time_unix = chat_date.change(hour: 0, min: 0, sec: 0).to_i
    end_time_unix = chat_date.change(hour: 23, min:59, sec: 59).to_i
    message_logs = ::EricWeixin::MessageLog.where("create_time between ? and ? ", start_time_unix, end_time_unix)
    message_logs = message_logs.where(event_name: 'kf_create_session', process_status: 1).group(:openid, :weixin_public_account_id)
    message_logs.each do |message_log|
      options = {
          :weixin_public_account_id=>message_log.,
          :openid=>message_log.openid,
          :starttime=>start_time_unix,
          :endtime=>end_time_unix,
          :pageindex=>1
      }
      i = 1
      has_record = true
      while has_record
        options[:pageindex] = i
        result_code, has_record = ::EricWeixin::MultCustomer.get_customer_service_messages options
        BusinessException.raise '获取聊天记录失败' unless result_code == 0
        i += 1
        BusinessException.raise '此人聊天记录竟然上了5000条!' if i >= 100
      end
      all_message_logs = ::EricWeixin::MessageLog.where("create_time between ? and ? ", start_time_unix, end_time_unix)
      all_message_logs = all_message_logs.where(event_name: 'kf_create_session', process_status: 1, openid: message_log.openid, weixin_public_account_id: message_log.)
      all_message_logs.each{ |ml| ml.update_attribute :process_status, 0 }
    end
  end
end

.common_query(options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/eric_weixin/customs_service_record.rb', line 78

def self.common_query options
  records = self.all

  records = records.where(weixin_public_account_id: options[:public_account_id]) unless options[:public_account_id].blank?

  records = records.where(opercode: options[:opercode]) unless options[:opercode].blank?

  unless options[:chat_date].blank?
    start_time = options[:chat_date].to_time.change(hour:0, min:0, sec:0).to_i
    end_time = options[:chat_date].to_time.change(hour:23, min:59, sec:59).to_i
    records = records.where("time between ? and ?", start_time, end_time)
  end

  #todo 这里的查询方式有可能有问题,建议取消此类查询。
  records = records.where("text like ?", "%#{options[:chat_content]}%") unless options[:chat_content].blank?

  records = records.where("worker like ?", "%#{options[:worker]}%") unless options[:worker].blank?

  unless options[:nick_name].blank?
    records = records.joins('LEFT JOIN weixin_users ON weixin_users.openid = weixin_customs_service_records.openid')
    records = records.where("weixin_users.nickname like ?", "%#{CGI::escape(options[:nick_name])}%")
  end

  records
end

.create_one(options) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'app/models/eric_weixin/customs_service_record.rb', line 17

def self.create_one options
  self.transaction do
    options = get_arguments_options options, [:weixin_public_account_id, :openid, :opercode, :text, :time, :worker]
    re = self.new options
    re.save!
    re.reload
    re
  end

end

.exist_one(options) ⇒ Object



28
29
30
31
# File 'app/models/eric_weixin/customs_service_record.rb', line 28

def self.exist_one options
  options = get_arguments_options options, [:weixin_public_account_id, :openid, :opercode, :text, :time, :worker]
  self.where( options ).count >= 1
end

Instance Method Details

#nick_nameObject



70
71
72
# File 'app/models/eric_weixin/customs_service_record.rb', line 70

def nick_name
  ::EricWeixin::WeixinUser.find_by_openid(self.openid).nickname rescue ''
end

#wixin_userObject



74
75
76
# File 'app/models/eric_weixin/customs_service_record.rb', line 74

def wixin_user
  ::EricWeixin::WeixinUser.find_by_openid(self.openid)
end