From 35ec3b1744a04a2185cb49740fe4162236f31719 Mon Sep 17 00:00:00 2001 From: p31729568 Date: Sat, 27 Apr 2019 14:47:18 +0800 Subject: [PATCH] fix school data contrast percentage zero issue --- .../school_data_contrast_service.rb | 8 +++--- .../schools/_data_contrast_list.html.erb | 27 ++++++++++--------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/app/services/management/school_data_contrast_service.rb b/app/services/management/school_data_contrast_service.rb index 47c7f117..e6a39e9b 100644 --- a/app/services/management/school_data_contrast_service.rb +++ b/app/services/management/school_data_contrast_service.rb @@ -54,10 +54,8 @@ class Management::SchoolDataContrastService end def select_columns - "school_id, school_name, @total:=0, @other_total:=0,"\ - "(@total:=SUM(IF(date BETWEEN '#{format_date(params[:begin_date])}' AND '#{format_date(params[:end_date])}', #{contrast_column}, 0))) total,"\ - "(@other_total:=SUM(IF(date BETWEEN '#{format_date(params[:other_begin_date])}' AND '#{format_date(params[:other_end_date])}', #{contrast_column}, 0))) other_total,"\ - "(IF(@other_total - @total = 0, 0.0, round((@other_total - @total) / IF(@total = 0, 1, @total), 5))) percentage,"\ - "(@other_total - @total) increase" + "school_id, school_name,"\ + "(SUM(IF(date BETWEEN '#{format_date(params[:begin_date])}' AND '#{format_date(params[:end_date])}', #{contrast_column}, 0))) total,"\ + "(SUM(IF(date BETWEEN '#{format_date(params[:other_begin_date])}' AND '#{format_date(params[:other_end_date])}', #{contrast_column}, 0))) other_total"\ end end diff --git a/app/views/managements/schools/_data_contrast_list.html.erb b/app/views/managements/schools/_data_contrast_list.html.erb index ae2ecfd2..af63aee8 100644 --- a/app/views/managements/schools/_data_contrast_list.html.erb +++ b/app/views/managements/schools/_data_contrast_list.html.erb @@ -14,10 +14,9 @@ 序号 ID 单位名称 - 时段一
<%= "(#{params[:begin_date]}至#{params[:end_date]})" %> - 时段二
<%= "(#{params[:other_begin_date]}至#{params[:other_end_date]})" %> - - <%#= sort_tag('变化情况', name: 'percentage', path: school_data_contrast_managements_path) %> + 时段一
<%= "(#{params[:begin_date]}至#{params[:end_date]})" %> + 时段二
<%= "(#{params[:other_begin_date]}至#{params[:other_end_date]})" %> + 变化情况
( 新 增 数 | 新增百分比) @@ -31,21 +30,25 @@ <%= report.school_name %> <%= report['total'] %> <%= report['other_total'] %> - <% if report['increase'] > 0 %> + <% + increase = report['other_total'] - report['total'] + percentage = report['total'].zero? ? increase.to_f * 100 : (increase / report['total'].to_f) * 100 + %> + <% if increase > 0 %> - +<%= report['increase'] %> + +<%= increase %> - +<%= report['percentage'] %>% - <% elsif report['increase'].zero? %> + +<%= percentage.round(5) %>% + <% elsif increase.zero? %> - <%= report['increase'] %> + <%= increase %> - <%= report['percentage'] %> + <%= percentage.round(5) %>% <% else %> - <%= report['increase'] %> + <%= increase %> - <%= report['percentage'] %>% + <%= percentage.round(5) %>% <% end %> <% end %>