From e20628fcc223264539caf2a1d73b2ab897583fa4 Mon Sep 17 00:00:00 2001 From: jingquan huang Date: Tue, 23 Jul 2019 17:57:14 +0800 Subject: [PATCH] user agents rake --- app/controllers/welcome_controller.rb | 2 +- app/helpers/welcome_helper.rb | 51 ++++++++++++++------- app/models/user_agent.rb | 2 + app/views/welcome/_user_agent_list.html.erb | 2 +- lib/tasks/user_agent.rake | 37 +++++++++++++++ 5 files changed, 76 insertions(+), 18 deletions(-) diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index c0abee20..05f04630 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -42,7 +42,7 @@ class WelcomeController < ApplicationController # 运营引流个人数据展示 def user_agents - @user_agents = UserAgent.where(key: params[:edu]).includes(:user_actionss) + @user_agents = UserAgent.where(key: params[:edu]) @agents_count = @user_agents.count limit = 20 @agents_pages = Paginator.new @agents_count, limit, params['page'] || 1 diff --git a/app/helpers/welcome_helper.rb b/app/helpers/welcome_helper.rb index 395bb07a..1d4eb879 100644 --- a/app/helpers/welcome_helper.rb +++ b/app/helpers/welcome_helper.rb @@ -24,22 +24,41 @@ module WelcomeHelper # 引流注册情况 def register_info user_agent - if user_agent.agent_type.to_i == UserAgent::USER_AD - user_id = user_agent.user_actionss.first.try(:user_id) - if user_id && User.where("id = #{user_id} and created_on < '2019-07-15 00:00:00'").present? - "代理前注册" - else - "未转换" - end - elsif user_agent.agent_type.to_i == UserAgent::USER_REGISTER || UserActions.find_by_ip(user_agent.ip) - "代理后注册" - else - user_id = user_agent.user_actionss.first.try(:user_id) - if user_id && User.where("id = #{user_id} and created_on < '2019-07-15 00:00:00'").present? - "代理前注册" - else - "代理后注册" - end + case user_agent.register_status + when 0 + "改IP未登录" + when 1 + "代理之前注册" + when 2 + "代理之后注册" + end + # if user_agent.agent_type.to_i == UserAgent::USER_AD + # user_id = user_agent.user_actionss.first.try(:user_id) + # if user_id && User.where("id = #{user_id} and created_on < '2019-07-15 00:00:00'").present? + # "代理前注册" + # else + # "未转换" + # end + # elsif user_agent.agent_type.to_i == UserAgent::USER_REGISTER || UserActions.find_by_ip(user_agent.ip) + # "代理后注册" + # else + # user_id = user_agent.user_actionss.first.try(:user_id) + # if user_id && User.where("id = #{user_id} and created_on < '2019-07-15 00:00:00'").present? + # "代理前注册" + # else + # "代理后注册" + # end + # end + end + + def competion_info user_agent + case user_agent.action_status + when 0 + "未报名" + when 1 + "代理之前报名" + when 2 + "代理之后报名" end end diff --git a/app/models/user_agent.rb b/app/models/user_agent.rb index 3bb16582..0fabe469 100644 --- a/app/models/user_agent.rb +++ b/app/models/user_agent.rb @@ -1,3 +1,5 @@ +# register_status: 0:未注册;1: 代理之前注册;2:代理之后注册 +# action_status: 0:未注册;1: 代理之前报名;2:代理之后报名 class UserAgent < ActiveRecord::Base has_many :user_actionss, :foreign_key => "ip", :primary_key => "ip" USER_AD = 1 # 广告宣传的引流 diff --git a/app/views/welcome/_user_agent_list.html.erb b/app/views/welcome/_user_agent_list.html.erb index 34792244..607392bd 100644 --- a/app/views/welcome/_user_agent_list.html.erb +++ b/app/views/welcome/_user_agent_list.html.erb @@ -15,7 +15,7 @@ <%= link_to ua.key, user_path(ua.key), :target => "_blank" %> <%= link_to user.try(:show_real_name), user_path(user), target:'_blank' %> <%= register_info(ua) %> - <%= ua.agent_type.to_i == UserAgent::USER_COMPETITION ? "已报名" : "未报名" %> + <%= competion_info(ua) %> <%= ua.ip %> <% end %> diff --git a/lib/tasks/user_agent.rake b/lib/tasks/user_agent.rake index e69de29b..f3f4dd23 100644 --- a/lib/tasks/user_agent.rake +++ b/lib/tasks/user_agent.rake @@ -0,0 +1,37 @@ +# register_status: 0:未注册;1: 代理之前注册;2:代理之后注册 +# register_action: 0:未报名;1: 代理之前报名;2:代理之后报名 +desc "sync user_agents" +namespace :sync do + task :user_agent => :environment do + UserAgent.find_each do |ua| + puts ua.id + user_action = UserActions.find_by_ip(ua.ip) + params = {} + # 用户注册情况 + if user_action + user = User.find(user_action.try(:user_id)) + + # 即代理之前注册 + if user.created_on < ua.created_at + params = params.merge(:register_status => 1) + else + # 代理之后注册的 + params = params.merge(:register_status => 2) + end + end + + # 用户报名情况 + team_member = TeamMember.find_by_user_id(user_action.try(:user_id)) + if team_member + # 即代理之前报名的 + if team_member.created_on < ua.created_at + params = params.merge(:action_status => 1) + else + # 代理之后报名的 + params = params.merge(:action_status => 2) + end + end + ua.update_attributes(params) + end + end +end \ No newline at end of file