module EducodeSales module SaleTrendsHelper def goal_forecast_quarter(labels, selects, staff_id = nil) plan_get = plan_get(staff_id, labels, "quarter") actual_get = actual_get(staff_id, labels, "quarter") datasets = selects.map do |select| if select == "计划投标额" { label: select, data: labels.map do |d| quarter = d.split("-").last.to_i year = d.split("-").first.to_i plan_get[[year, quarter]]&.pluck(:budget_amount)&.reject(&:blank?)&.sum.to_f end, backgroundColor: [ 'rgba(255, 99, 132, 0.2)', ], borderColor: [ 'rgba(255,99,132,1)', ], borderWidth: 1 } else { label: select, data: labels.map do |d| quarter = d.split("-").last.to_i year = d.split("-").first.to_i actual_get[[year, quarter]]&.pluck(:actual_amount)&.reject(&:blank?)&.sum.to_f end, backgroundColor: [ 'rgba(54, 162, 235, 0.2)', ], borderColor: [ 'rgba(54, 162, 235, 1)', ], borderWidth: 1 } end end { labels: labels, datasets: datasets } end def goal_forecast_week(labels, selects, staff_id = nil) plan_get = plan_get(staff_id, labels, "week") actual_get = actual_get(staff_id, labels, "week") datasets = selects.map do |select| if select == "计划投标额" { label: select, data: labels.map do |d| week = d.split("-").last year = d.split("-").first yearweek = "#{year}#{week}".to_i plan_get[yearweek].to_f end, backgroundColor: [ 'rgba(255, 99, 132, 0.2)', ], borderColor: [ 'rgba(255,99,132,1)', ], borderWidth: 1 } else { label: select, data: labels.map do |d| week = d.split("-").last year = d.split("-").first yearweek = "#{year}#{week}".to_i actual_get[yearweek].to_f end, backgroundColor: [ 'rgba(54, 162, 235, 0.2)', ], borderColor: [ 'rgba(54, 162, 235, 1)', ], borderWidth: 1 } end end { labels: labels, datasets: datasets } end def goal_forecast_month(labels, selects, staff_id = nil) plan_get = plan_get(staff_id, labels, "month") actual_get = actual_get(staff_id, labels, "month") datasets = selects.map do |select| if select == "计划投标额" { label: select, data: labels.map do |d| month = d.split("-").last.to_i year = d.split("-").first.to_i plan_get[[year, month]]&.pluck(:budget_amount)&.reject(&:blank?)&.sum.to_f end, backgroundColor: [ 'rgba(255, 99, 132, 0.2)', ], borderColor: [ 'rgba(255,99,132,1)', ], borderWidth: 1 } else { label: select, data: labels.map do |d| month = d.split("-").last.to_i year = d.split("-").first.to_i actual_get[[year, month]]&.pluck(:actual_amount)&.reject(&:blank?)&.sum.to_f end, backgroundColor: [ 'rgba(54, 162, 235, 0.2)', ], borderColor: [ 'rgba(54, 162, 235, 1)', ], borderWidth: 1 } end end { labels: labels, datasets: datasets } end def goal_forecast_year(labels, selects, staff_id = nil) plan_get = plan_get(staff_id, labels, "year") actual_get = actual_get(staff_id, labels, "year") datasets = selects.map do |select| if select == "计划投标额" { label: select, data: labels.map { |d| plan_get[d.to_i] || 0 }, backgroundColor: [ 'rgba(255, 99, 132, 0.2)', ], borderColor: [ 'rgba(255,99,132,1)', ], borderWidth: 1 } else { label: select, data: labels.map { |d| actual_get[d.to_i] || 0 }, backgroundColor: [ 'rgba(54, 162, 235, 0.2)', ], borderColor: [ 'rgba(54, 162, 235, 1)', ], borderWidth: 1 } end end { labels: labels, datasets: datasets } end private def plan_get(staff_id, time_range, type) # budget_amount 预算额 staff_id = staff_id.present? ? Array(staff_id) : nil data = Business.joins(:last_follow_up) .where("educode_sales_follow_ups.invitation_at >= ? and educode_sales_follow_ups.invitation_at <= ? #{staff_id.present? ? "AND educode_sales_follow_ups.staff_id in (#{staff_id.join(",")})" : ""}", time_range.first, time_range.last) case type when "week" data.select("educode_sales_follow_ups.*, yearweek(educode_sales_follow_ups.invitation_at) as week").group("yearweek(educode_sales_follow_ups.invitation_at)").sum(:budget_amount) when "quarter" data = data.select("educode_sales_follow_ups.*, quarter(educode_sales_follow_ups.invitation_at) as quarter, year(educode_sales_follow_ups.invitation_at) as year") data.group_by { |d| [d.year, d.quarter] } when "month" data = data.select("educode_sales_follow_ups.*, year(educode_sales_follow_ups.invitation_at) as year, month(educode_sales_follow_ups.invitation_at) as month") data.group_by { |d| [d.year, d.month] } when "year" data.select("educode_sales_follow_ups.*, year(educode_sales_follow_ups.invitation_at) as year").group("year(educode_sales_follow_ups.invitation_at)").sum(:budget_amount) end end def actual_get(staff_id, time_range, type) # actual_amount 合同额 staff_id = staff_id.present? ? Array(staff_id) : nil data = Business.joins(:last_follow_up) .where("educode_sales_follow_ups.bidded_date >= ? and educode_sales_follow_ups.bidded_date <= ? #{staff_id.present? ? "AND educode_sales_follow_ups.staff_id in (#{staff_id.join(",")})" : ""}", time_range.first, time_range.last) case type when "week" data.select("educode_sales_follow_ups.*, yearweek(educode_sales_follow_ups.bidded_date) as week").group("yearweek(educode_sales_follow_ups.bidded_date)").sum(:actual_amount) when "quarter" data = data.select("educode_sales_follow_ups.*, quarter(educode_sales_follow_ups.bidded_date) as quarter, year(educode_sales_follow_ups.bidded_date) as year") data.group_by { |d| [d.year, d.quarter] } when "month" data = data.select("educode_sales_follow_ups.*, year(educode_sales_follow_ups.bidded_date) as year, month(educode_sales_follow_ups.bidded_date) as month") data.group_by { |d| [d.year, d.month] } when "year" data.select("educode_sales_follow_ups.*, year(educode_sales_follow_ups.bidded_date) as year").group("year(educode_sales_follow_ups.bidded_date)").sum(:actual_amount) end end end end