commit
be46f75aca
@ -0,0 +1,54 @@
|
||||
class AttendanceStatisticsService < ApplicationService
|
||||
|
||||
attr_reader :attendances, :member_attendances
|
||||
|
||||
def initialize(attendances, member_attendances)
|
||||
@attendances = attendances
|
||||
@member_attendances = member_attendances
|
||||
end
|
||||
|
||||
def call
|
||||
all_normal_rate = []
|
||||
all_absence_rate = []
|
||||
all_leave_rate = []
|
||||
|
||||
history_attendances = []
|
||||
|
||||
attendances.each do |attendance|
|
||||
normal_count = history_member_count(member_attendances, "NORMAL", attendance.id)
|
||||
absence_count = history_member_count(member_attendances, "ABSENCE", attendance.id)
|
||||
leave_count = history_member_count(member_attendances, "LEAVE", attendance.id)
|
||||
all_count = member_attendances.select{|member_attendance| member_attendance.course_attendance_id == attendance.id}.size
|
||||
|
||||
normal_rate = cal_rate(normal_count, all_count)
|
||||
all_normal_rate << normal_rate
|
||||
absence_rate = cal_rate(absence_count, all_count)
|
||||
all_absence_rate << absence_rate
|
||||
leave_rate = cal_rate(leave_count, all_count)
|
||||
all_leave_rate << leave_rate
|
||||
|
||||
history_attendances << {name: attendance.name, attendance_date: attendance.attendance_date.strftime("%Y-%m-%d"),
|
||||
start_time: attendance.start_time.strftime("%H:%M"), end_time: attendance.end_time.strftime("%H:%M"),
|
||||
normal_rate: normal_rate, absence_rate: absence_rate, leave_rate: leave_rate}
|
||||
end
|
||||
|
||||
all_history_count = history_attendances.size
|
||||
history_attendances = history_attendances[0..9].reverse
|
||||
avg_normal_rate = cal_rate(all_normal_rate.sum, all_history_count)
|
||||
avg_absence_rate = cal_rate(all_absence_rate.sum, all_history_count)
|
||||
avg_leave_rate = cal_rate(all_leave_rate.sum, all_history_count)
|
||||
|
||||
{all_history_count: all_history_count, history_attendances: history_attendances, avg_normal_rate: avg_normal_rate,
|
||||
avg_absence_rate: avg_absence_rate, avg_leave_rate: avg_leave_rate}
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def history_member_count member_attendances, status, attendance_id
|
||||
member_attendances.select{|member_attendance| member_attendance.attendance_status == status && member_attendance.course_attendance_id == attendance_id}.size
|
||||
end
|
||||
|
||||
def cal_rate base, sum
|
||||
sum == 0 ? 0 : (base.to_f / sum)
|
||||
end
|
||||
end
|
@ -0,0 +1,10 @@
|
||||
|
||||
json.history_attendances @history_attendances.each_with_index.to_a do |attendance, index|
|
||||
json.(attendance, :name, :attendance_date, :start_time, :end_time, :normal_rate, :absence_rate, :leave_rate)
|
||||
json.index "签到#{index + 1}"
|
||||
end
|
||||
|
||||
json.all_history_count @all_history_count
|
||||
json.avg_normal_rate @avg_normal_rate
|
||||
json.avg_absence_rate @avg_absence_rate
|
||||
json.avg_leave_rate @avg_leave_rate
|
Loading…
Reference in new issue