Class: EducodeSales::UserStatService

Inherits:
Object
  • Object
show all
Defined in:
app/models/educode_sales/user_stat_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUserStatService

Returns a new instance of UserStatService.



5
6
7
8
9
10
11
# File 'app/models/educode_sales/user_stat_service.rb', line 5

def initialize
  @o_type = EducodeSales::Common.find_by(extras: EducodeSales::Common::OTYPE)&.id

  part_a_ids = EducodeSales::CustomerFollow.all.pluck(:school_id)
  part_b_ids = EducodeSales::Business.pluck(:school_id)
  @school_ids = (part_a_ids + part_b_ids + EducodeSales::CustomerAdd.all.pluck(:school_id)).uniq
end

Instance Attribute Details

#o_typeObject (readonly)

Returns the value of attribute o_type.



4
5
6
# File 'app/models/educode_sales/user_stat_service.rb', line 4

def o_type
  @o_type
end

#school_idsObject (readonly)

Returns the value of attribute school_ids.



4
5
6
# File 'app/models/educode_sales/user_stat_service.rb', line 4

def school_ids
  @school_ids
end

Instance Method Details

#department_users(clazz) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'app/models/educode_sales/user_stat_service.rb', line 151

def department_users(clazz)
  EducodeSales::Business.joins(:last_follow_up).
  joins("join departments ON departments.id = educode_sales_businesses.department_id").
  where("educode_sales_follow_ups.o_business_deployment = #{clazz}").
  where(school_id: @school_ids).
  group("DATE_FORMAT(departments.created_at, '%Y')").
  select("count(*) as count, DATE_FORMAT(departments.created_at, '%Y') as year").
  distinct.count("school_id")
end

#departments(clazz) ⇒ Object



166
167
168
169
# File 'app/models/educode_sales/user_stat_service.rb', line 166

def departments(clazz)
  School.joins("JOIN user_extensions ON schools.id = user_extensions.school_id AND user_extensions.identity = #{clazz}").
  where(id: @school_ids).group("user_extensions.department_id").count
end

#public_departmentObject



125
126
127
128
129
# File 'app/models/educode_sales/user_stat_service.rb', line 125

def public_department
  Department.where(school_id: @school_ids).
  group("DATE_FORMAT(departments.created_at, '%Y')").
  select("count(*) as count, DATE_FORMAT(departments.created_at, '%Y') as year").count("*")
end

#public_schoolObject



119
120
121
122
123
# File 'app/models/educode_sales/user_stat_service.rb', line 119

def public_school
  School.where(id: @school_ids).
  group("DATE_FORMAT(schools.created_at, '%Y')").
  select("count(*) as count, DATE_FORMAT(schools.created_at, '%Y') as year").count("*")
end

#public_userObject



113
114
115
116
117
# File 'app/models/educode_sales/user_stat_service.rb', line 113

def public_user
  UserExtension.where(school_id: @school_ids).
  group("DATE_FORMAT(user_extensions.created_at, '%Y')").
  select("count(*) as count, DATE_FORMAT(user_extensions.created_at, '%Y') as year").count("*")
end

#school_users(clazz) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'app/models/educode_sales/user_stat_service.rb', line 141

def school_users(clazz)
  EducodeSales::Business.joins(:last_follow_up).
  joins("join schools ON schools.id = educode_sales_businesses.school_id").
  where("educode_sales_follow_ups.o_business_deployment = #{clazz}").
  where(school_id: @school_ids).
  group("DATE_FORMAT(schools.created_at, '%Y')").
  select("count(*) as count, DATE_FORMAT(schools.created_at, '%Y') as year").
  distinct.count("school_id")
end

#schools(clazz) ⇒ Object



161
162
163
164
# File 'app/models/educode_sales/user_stat_service.rb', line 161

def schools(clazz)
  School.joins("JOIN user_extensions ON schools.id = user_extensions.school_id AND user_extensions.identity = #{clazz}").
  where(id: @school_ids).group(:id).count
end

#signed_departments(clazz) ⇒ Object



177
178
179
180
181
182
183
# File 'app/models/educode_sales/user_stat_service.rb', line 177

def signed_departments(clazz)
  School.joins("JOIN user_extensions on schools.id = user_extensions.school_id AND user_extensions.identity = #{clazz}
    JOIN educode_sales_businesses ON educode_sales_businesses.clazz_id = #{@o_type} AND educode_sales_businesses.school_id = schools.id
  ").where(id: @school_ids).where("user_extensions.department_id > 0").
  group("user_extensions.department_id").
  distinct.count("user_extensions.id")
end

#signed_schools(clazz) ⇒ Object



171
172
173
174
175
# File 'app/models/educode_sales/user_stat_service.rb', line 171

def signed_schools(clazz)
  School.joins("JOIN user_extensions on schools.id = user_extensions.school_id AND user_extensions.identity = #{clazz}
    JOIN educode_sales_businesses ON educode_sales_businesses.clazz_id = #{@o_type} AND educode_sales_businesses.school_id = schools.id
  ").where(id: @school_ids).group(:id).distinct.count("user_extensions.id")
end

#table_1Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/educode_sales/user_stat_service.rb', line 13

def table_1
  # 公有云
  user_years_1 = public_user
  # 私有云
  user_years_2 = {} #year_users(2)
  school_years_1 = public_school
  school_years_2 = {}# school_users(2)
  department_years_1 = public_department
  department_years_2 = {} #department_users(2)

  years = {}
  (2014..Time.current.year).each do |d|
    years[d] = {
      'year' => d,
      'user_1' => (user_years_1[d.to_s] || 0) + (years[d-1] && years[d-1]['user_1']).to_i,
      'user_2' => user_years_2[d.to_s] || 0,
      'school_1' => (school_years_1[d.to_s] || 0) + (years[d-1] && years[d-1]['school_1']).to_i,
      'school_2' => school_years_2[d.to_s] || 0,
      'department_1' => (department_years_1[d.to_s] || 0) + (years[d-1] && years[d-1]['department_1']).to_i,
      'department_2' => department_years_2[d.to_s] || 0
    }
  end

  years.values
end

#table_2(clazz) ⇒ Object



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
69
70
71
72
73
74
75
76
77
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
103
104
105
106
107
108
109
110
# File 'app/models/educode_sales/user_stat_service.rb', line 39

def table_2(clazz)
  users = {'1-9' => [0, 0, 0], '10-99' => [0, 0, 0], '100-999' => [0, 0, 0], '1000-9999' => [0, 0, 0], '10000-49999' => [0, 0, 0] }
  signed_users = {'1-9' => [0, 0, 0], '10-99' => [0, 0, 0], '100-999' => [0, 0, 0], '1000-9999' => [0, 0, 0], '10000-49999' => [0, 0, 0] }
  schools(clazz).each do |d|
    if d[1] <= 9
      users['1-9'][0] += 1
     elsif d[1] <= 99
       users['10-99'][0] += 1
     elsif d[1] <= 999
       users['100-999'][0] += 1
     elsif d[1] <= 9999
       users['1000-9999'][0] += 1
     elsif d[1] <= 49999
       users['10000-49999'][0] += 1
     end
  end

  departments(clazz).each do |d|
    if d[1] <= 9
     users['1-9'][1] += 1
    elsif d[1] <= 99
      users['10-99'][1] += 1
    elsif d[1] <= 999
      users['100-999'][1] += 1
    elsif d[1] <= 9999
      users['1000-9999'][1] += 1
    elsif d[1] <= 49999
      users['10000-49999'][1] += 1
    end
  end

  signed_schools(clazz).each do |d|
    if d[1] <= 9
      signed_users['1-9'][0] += 1
     elsif d[1] <= 99
      signed_users['10-99'][0] += 1
     elsif d[1] <= 999
      signed_users['100-999'][0] += 1
     elsif d[1] <= 9999
      signed_users['1000-9999'][0] += 1
     elsif d[1] <= 49999
      signed_users['10000-49999'][0] += 1
     end
  end

  signed_departments(clazz).each do |d|
    if d[1] <= 9
      signed_users['1-9'][1] += 1
    elsif d[1] <= 99
      signed_users['10-99'][1] += 1
    elsif d[1] <= 999
      signed_users['100-999'][1] += 1
    elsif d[1] <= 9999
      signed_users['1000-9999'][1] += 1
    elsif d[1] <= 49999
      signed_users['10000-49999'][1] += 1
    end
  end


  ['1-9', '10-99', '100-999', '1000-9999', '10000-49999'].map do |d|
    {
      'num' => d,
      'schools' => users[d][0],
      'departments' => users[d][1],
      'majors' => 0,
      'signed_schools' => signed_users[d][0],
      'signed_departments' => signed_users[d][1],
      'signed_majors' => 0,
    }
  end
end

#year_users(clazz) ⇒ Object



131
132
133
134
135
136
137
138
139
# File 'app/models/educode_sales/user_stat_service.rb', line 131

def year_users(clazz)
  EducodeSales::Business.joins(:last_follow_up).
  joins("join user_extensions ON user_extensions.department_id = educode_sales_businesses.department_id").
  where("educode_sales_follow_ups.o_business_deployment = #{clazz}").
  where(school_id: @school_ids).
  group("DATE_FORMAT(user_extensions.created_at, '%Y')").
  select("count(*) as count, DATE_FORMAT(user_extensions.created_at, '%Y') as year").
  distinct.count("user_extensions.id")
end