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



232
233
234
235
236
237
238
239
240
# File 'app/models/educode_sales/user_stat_service.rb', line 232

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



247
248
249
250
# File 'app/models/educode_sales/user_stat_service.rb', line 247

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



206
207
208
209
210
# File 'app/models/educode_sales/user_stat_service.rb', line 206

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



200
201
202
203
204
# File 'app/models/educode_sales/user_stat_service.rb', line 200

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



194
195
196
197
198
# File 'app/models/educode_sales/user_stat_service.rb', line 194

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



222
223
224
225
226
227
228
229
230
# File 'app/models/educode_sales/user_stat_service.rb', line 222

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



242
243
244
245
# File 'app/models/educode_sales/user_stat_service.rb', line 242

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



258
259
260
261
262
263
264
# File 'app/models/educode_sales/user_stat_service.rb', line 258

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



252
253
254
255
256
# File 'app/models/educode_sales/user_stat_service.rb', line 252

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

#table_3Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/models/educode_sales/user_stat_service.rb', line 112

def table_3
  users = {'1' => [0, 0, 0], '2-5' => [0, 0, 0], '6-10' => [0, 0, 0], '11-20' => [0, 0, 0], '21-50' => [0, 0, 0], '51-100' => [0, 0, 0] }
  signed_users = {'1' => [0, 0, 0], '2-5' => [0, 0, 0], '6-10' => [0, 0, 0], '11-20' => [0, 0, 0], '21-50' => [0, 0, 0], '51-100' => [0, 0, 0] }
  schools(0).each do |d|
    if d[1] == 1
      users['1'][0] += 1
     elsif d[1] <= 5
       users['2-5'][0] += 1
     elsif d[1] <= 10
       users['6-10'][0] += 1
     elsif d[1] <= 20
       users['11-20'][0] += 1
     elsif d[1] <= 50
       users['21-50'][0] += 1
     elsif d[1] <= 100
        users['51-100'][0] += 1
     end
  end

  departments(0).each do |d|
    if d[1] == 1
      users['1'][1] += 1
     elsif d[1] <= 5
       users['2-5'][1] += 1
     elsif d[1] <= 10
       users['6-10'][1] += 1
     elsif d[1] <= 20
       users['11-20'][1] += 1
     elsif d[1] <= 50
       users['21-50'][1] += 1
     elsif d[1] <= 100
        users['51-100'][1] += 1
     end
  end

  signed_schools(0).each do |d|
    if d[1] == 1
      signed_users['1'][0] += 1
     elsif d[1] <= 5
      signed_users['2-5'][0] += 1
     elsif d[1] <= 10
      signed_users['6-10'][0] += 1
     elsif d[1] <= 20
       signed_users['11-20'][0] += 1
     elsif d[1] <= 50
      signed_users['21-50'][0] += 1
     elsif d[1] <= 100
      signed_users['51-100'][0] += 1
     end
  end

  signed_departments(0).each do |d|
    if d[1] == 1
      signed_users['1'][1] += 1
     elsif d[1] <= 5
      signed_users['2-5'][1] += 1
     elsif d[1] <= 10
      p "----#{d}"
      signed_users['6-10'][1] += 1
     elsif d[1] <= 20
       signed_users['11-20'][1] += 1
     elsif d[1] <= 50
      signed_users['21-50'][1] += 1
     elsif d[1] <= 100
      signed_users['51-100'][1] += 1
     end
  end


  ['1', '2-5', '6-10', '11-20', '21-50', '51-100'].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



212
213
214
215
216
217
218
219
220
# File 'app/models/educode_sales/user_stat_service.rb', line 212

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