From 5e242aea7afa9ac33a21c43c29467edc57c691cf Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Wed, 4 Mar 2020 16:20:28 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AD=BE=E5=88=B0=E4=B8=BB=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/application_controller.rb | 2 +- .../weapps/attendances_controller.rb | 10 ++++++---- app/helpers/weapps/attendances_helper.rb | 8 ++++++++ .../weapps/attendances/index.json.jbuilder | 20 ++++++++++++++----- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index b7a74cbc1..90a5e494e 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -321,7 +321,7 @@ class ApplicationController < ActionController::Base end if !User.current.logged? && Rails.env.development? - User.current = User.find 3117 + User.current = User.find 1 end diff --git a/app/controllers/weapps/attendances_controller.rb b/app/controllers/weapps/attendances_controller.rb index 567bfae77..6804f0ce4 100644 --- a/app/controllers/weapps/attendances_controller.rb +++ b/app/controllers/weapps/attendances_controller.rb @@ -27,14 +27,16 @@ class Weapps::AttendancesController < ApplicationController tip_exception(403) if @user_course_identity >= Course::STUDENT current_date = Date.current current_end_time = Time.current.strftime("%H:%M:%S") - @current_attendance = @course.course_attendances.where("attendance_date = '#{current_date}' and start_time <= '#{current_end_time}' and end_time > '#{current_end_time}'").take + @current_attendance = @course.course_attendances.where("attendance_date = '#{current_date}' and end_time > '#{current_end_time}'").take - all_attendances = @course.course_attendances + all_attendances = @course.course_attendances.where("attendance_date < '#{current_date}' or (attendance_date = '#{current_date}' and end_time < '#{current_end_time}')") + @all_member_attendances = CourseMemberAttendance.where(course_attendance_id: all_attendances) if params[:group_id].present? - all_attendances = all_attendances.where(course_group_id: [params[:group_id], 0]) + all_attendances = all_attendances.joins(:course_attendance_groups).where(course_attendance_groups: {course_group_id: [params[:group_id], 0]}) + @all_member_attendances = @all_member_attendances.joins(:course_member).where(course_members: {course_group_id: params[:group_id]}) end - @history_attendances = all_attendances.where("attendance_date < '#{current_date}' or (attendance_date = '#{current_date}' and end_time < '#{current_end_time}')").order("id asc") + @history_attendances = all_attendances.order("id asc") @all_history_count = @history_attendances.size end diff --git a/app/helpers/weapps/attendances_helper.rb b/app/helpers/weapps/attendances_helper.rb index 60a9cfe26..30591c149 100644 --- a/app/helpers/weapps/attendances_helper.rb +++ b/app/helpers/weapps/attendances_helper.rb @@ -9,4 +9,12 @@ module Weapps::AttendancesHelper course_member_ids = group.course_members.pluck(:id) attendances.select{|attendance| course_member_ids.include?(attendance.course_member_id) && attendance.attendance_status == "NORMAL"}.size end + + 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 \ No newline at end of file diff --git a/app/views/weapps/attendances/index.json.jbuilder b/app/views/weapps/attendances/index.json.jbuilder index 9541fe231..9ab10de66 100644 --- a/app/views/weapps/attendances/index.json.jbuilder +++ b/app/views/weapps/attendances/index.json.jbuilder @@ -1,11 +1,21 @@ json.current_attendance do - json.(@current_attendance, :id, :normal_count, :all_count) + json.(@current_attendance, :id, :normal_count, :all_count) if @current_attendance end -json.history_attendances @history_attendances do |attendance| +all_normal_rate = [] +all_absence_rate = [] +json.history_attendances @history_attendances.each_with_index.to_a do |attendance, index| + normal_count = history_member_count(@all_member_attendances, "NORMAL", attendance.id) + absence_count = history_member_count(@all_member_attendances, "ABSENCE", attendance.id) + all_count = @all_member_attendances.select{|member_attendance| member_attendance.course_attendance_id == attendance.id}.size + + json.index index + 1 + json.normal_rate cal_rate(normal_count, all_count) + all_normal_rate << cal_rate(normal_count, all_count) + json.absence_rate cal_rate(absence_count, all_count) + all_absence_rate << cal_rate(absence_count, all_count) end json.all_history_count @all_history_count -json.normal_count @normal_count -json.leave_count @leave_count -json.absence_count @absence_count \ No newline at end of file +json.avg_normal_rate all_normal_rate.sum / @all_history_count +json.avg_absence_rate all_absence_rate.sum / @all_history_count