Class: EricWeixin::CustomsServiceRecord
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- EricWeixin::CustomsServiceRecord
- 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
-
.batch_get_customs_records(chat_date) ⇒ Object
批量获取用户与客服的聊天记录保存到数据库.
- .common_query(options) ⇒ Object
- .create_one(options) ⇒ Object
- .exist_one(options) ⇒ Object
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 = ::EricWeixin::MessageLog.where("create_time between ? and ? ", start_time_unix, end_time_unix) = .where(event_name: 'kf_create_session', process_status: 1).group(:openid, :weixin_public_account_id) .each do || = { :weixin_public_account_id=>.weixin_public_account_id, :openid=>.openid, :starttime=>start_time_unix, :endtime=>end_time_unix, :pageindex=>1 } i = 1 has_record = true while has_record [:pageindex] = i result_code, has_record = ::EricWeixin::MultCustomer. BusinessException.raise '获取聊天记录失败' unless result_code == 0 i += 1 BusinessException.raise '此人聊天记录竟然上了5000条!' if i >= 100 end = ::EricWeixin::MessageLog.where("create_time between ? and ? ", start_time_unix, end_time_unix) = .where(event_name: 'kf_create_session', process_status: 1, openid: .openid, weixin_public_account_id: .weixin_public_account_id) .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 records = self.all records = records.where(weixin_public_account_id: [:public_account_id]) unless [:public_account_id].blank? records = records.where(opercode: [:opercode]) unless [:opercode].blank? unless [:chat_date].blank? start_time = [:chat_date].to_time.change(hour:0, min:0, sec:0).to_i end_time = [: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 ?", "%#{[:chat_content]}%") unless [:chat_content].blank? records = records.where("worker like ?", "%#{[:worker]}%") unless [:worker].blank? unless [: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([: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 self.transaction do = , [:weixin_public_account_id, :openid, :opercode, :text, :time, :worker] re = self.new 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 = , [:weixin_public_account_id, :openid, :opercode, :text, :time, :worker] self.where( ).count >= 1 end |
Instance Method Details
#nick_name ⇒ Object
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_user ⇒ Object
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 |