From c9b15a02d6e5e688065246f1057aff38669e849a Mon Sep 17 00:00:00 2001 From: qyzh Date: Wed, 9 Jun 2021 11:06:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BB=9F=E8=AE=A1=E5=9B=BE?= =?UTF-8?q?=E6=A0=87=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/sponsorships_controller.rb | 104 +++++++++++++----- app/controllers/wallets_controller.rb | 44 +++++++- .../sponsorships/community_data.json.jbuilder | 12 ++ app/views/wallets/balance_chart.json.jbuilder | 39 ++----- .../wallets/community_data.json.jbuilder | 4 + config/routes.rb | 3 + 6 files changed, 145 insertions(+), 61 deletions(-) create mode 100644 app/views/sponsorships/community_data.json.jbuilder create mode 100644 app/views/wallets/community_data.json.jbuilder diff --git a/app/controllers/sponsorships_controller.rb b/app/controllers/sponsorships_controller.rb index 2e18a075c..8a21176b1 100644 --- a/app/controllers/sponsorships_controller.rb +++ b/app/controllers/sponsorships_controller.rb @@ -16,12 +16,12 @@ class SponsorshipsController < ApplicationController def sponsored if User.current.id == Integer(params[:id]) - @sponsorships = Sponsorship.where("developer_id=?", params[:id]) + @sponsorships = Sponsorship.where('developer_id=?', params[:id]) else - @sponsorships = Sponsorship.where("developer_id=? AND visible=1", params[:id]) + @sponsorships = Sponsorship.where('developer_id=? AND visible=1', params[:id]) end - sort = params[:sort_by] || "created_at" - sort_direction = params[:sort_direction] || "desc" + sort = params[:sort_by] || 'created_at' + sort_direction = params[:sort_direction] || 'desc' @sponsorships = @sponsorships.reorder("#{sort} #{sort_direction}") @total = @sponsorships.length @sponsorships = kaminari_paginate(@sponsorships) @@ -29,12 +29,12 @@ class SponsorshipsController < ApplicationController def sponsoring if User.current.id == Integer(params[:id]) - @sponsorships = Sponsorship.where("sponsor_id=?", params[:id]) + @sponsorships = Sponsorship.where('sponsor_id=?', params[:id]) else - @sponsorships = Sponsorship.where("sponsor_id=? AND visible=1", params[:id]) + @sponsorships = Sponsorship.where('sponsor_id=? AND visible=1', params[:id]) end - sort = params[:sort_by] || "created_at" - sort_direction = params[:sort_direction] || "desc" + sort = params[:sort_by] || 'created_at' + sort_direction = params[:sort_direction] || 'desc' @sponsorships = @sponsorships.reorder("#{sort} #{sort_direction}") @total = @sponsorships.length @sponsorships = kaminari_paginate(@sponsorships) @@ -42,12 +42,12 @@ class SponsorshipsController < ApplicationController def stopped_sponsored if User.current.id == Integer(params[:id]) - @stopped_sponsorships = StoppedSponsorship.where("developer_id=?", params[:id]) + @stopped_sponsorships = StoppedSponsorship.where('developer_id=?', params[:id]) else - @stopped_sponsorships = StoppedSponsorship.where("developer_id=? AND visible=1", params[:id]) + @stopped_sponsorships = StoppedSponsorship.where('developer_id=? AND visible=1', params[:id]) end - sort = params[:sort_by] || "created_at" - sort_direction = params[:sort_direction] || "desc" + sort = params[:sort_by] || 'created_at' + sort_direction = params[:sort_direction] || 'desc' @stopped_sponsorships = @stopped_sponsorships.reorder("#{sort} #{sort_direction}") @total = @stopped_sponsorships.length @stopped_sponsorships = kaminari_paginate(@stopped_sponsorships) @@ -55,12 +55,12 @@ class SponsorshipsController < ApplicationController def stopped_sponsoring if User.current.id == Integer(params[:id]) - @stopped_sponsorships = StoppedSponsorship.where("sponsor_id=?", params[:id]) + @stopped_sponsorships = StoppedSponsorship.where('sponsor_id=?', params[:id]) else - @stopped_sponsorships = StoppedSponsorship.where("sponsor_id=? AND visible=1", params[:id]) + @stopped_sponsorships = StoppedSponsorship.where('sponsor_id=? AND visible=1', params[:id]) end - sort = params[:sort_by] || "created_at" - sort_direction = params[:sort_direction] || "desc" + sort = params[:sort_by] || 'created_at' + sort_direction = params[:sort_direction] || 'desc' @stopped_sponsorships = @stopped_sponsorships.reorder("#{sort} #{sort_direction}") @total = @stopped_sponsorships.length @stopped_sponsorships = kaminari_paginate(@stopped_sponsorships) @@ -68,14 +68,13 @@ class SponsorshipsController < ApplicationController # GET /sponsorships/1 # GET /sponsorships/1.json - def show - end + def show; end # POST /sponsorships # POST /sponsorships.json def create sponsor_id = User.current.id - check_sponsorship = Sponsorship.where("sponsor_id=? AND developer_id=?", sponsor_id, params[:developer_id]) + check_sponsorship = Sponsorship.where('sponsor_id=? AND developer_id=?', sponsor_id, params[:developer_id]) @sponsorship = Sponsorship.new(sponsorship_params.merge({sponsor_id: sponsor_id})) @@ -144,20 +143,69 @@ class SponsorshipsController < ApplicationController developer = @sponsorship.developer sponsor = @sponsorship.sponsor if (User.current.id == developer.id || User.current.id == sponsor.id) && developer.update(sponsored_num: developer.sponsored_num-1) && sponsor.update(sponsor_num: sponsor.sponsor_num-1) && @sponsorship.stop - render json: {status: 1, message: "终止成功"} + render json: {status: 1, message: '终止成功'} else - render json: {status: -1, message: "失败"} + render json: {status: -1, message: '失败'} end end + def community_data + @sponsorships = Sponsorship.all + t1 = Time.now + t2 = Time.new(t1.year, t1.month, t1.day - 6) + @stopped_sponsorships = StoppedSponsorship.where('created_at >= ?', t2) + @community_data_array = to_array(@sponsorships, @stopped_sponsorships) + end + private - # Use callbacks to share common setup or constraints between actions. - def set_sponsorship - @sponsorship = Sponsorship.find(params[:id]) + + # Use callbacks to share common setup or constraints between actions. + def set_sponsorship + @sponsorship = Sponsorship.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def sponsorship_params + params.require(:sponsorship).permit(:amount, :visible, :sponsor_id, :developer_id, :single, :page, :limit, :sort_by, :search) + end + + def to_array(sponsorships, stopped_sponsorships) + t1 = Time.now + start_time = Time.new(t1.year, t1.month, t1.day - 6) + + sponsor = Array.new(7) + sponsored = Array.new(7) + date = Array.new(7) + # date[0] = Time.new(start_time.year, start_time.month, start_time.day) + index = 0 + + (0..6).each do |i| + # 更新日期,date[i]表示第i天0点 + date[i] = Time.new(start_time.year, start_time.month, start_time.day + i) + end_time = Time.new(start_time.year, start_time.month, start_time.day + i+1) + + sponsor_set = Set.new + sponsored_set = Set.new + + # 所有创建时间早于今天23:59的sponsorship + today_sponsor = sponsorships.where('created_at < ?', end_time) + # 所有结束时间晚于今天0:00的stopped_sponsorship + today_stopped_sponsor = stopped_sponsorships.where('created_at <= ?', date[i]) + today_sponsor.each do |s| + sponsor_set.add s.sponsor_id + sponsored_set.add s.developer_id + end + + today_stopped_sponsor.each do |s| + sponsor_set.add s.sponsor_id + sponsored_set.add s.developer_id + end + + sponsor[i] = sponsor_set.size + sponsored[i] = sponsored_set.size + end - # Only allow a list of trusted parameters through. - def sponsorship_params - params.require(:sponsorship).permit(:amount, :visible, :sponsor_id, :developer_id, :single, :page, :limit, :sort_by, :search) - end + Array[sponsor, sponsored, date] + end end diff --git a/app/controllers/wallets_controller.rb b/app/controllers/wallets_controller.rb index 1b811acc7..7add945c8 100644 --- a/app/controllers/wallets_controller.rb +++ b/app/controllers/wallets_controller.rb @@ -1,5 +1,5 @@ class WalletsController < ApplicationController - before_action :require_login, except: :balance_chart + before_action :require_login, except: :community_data def balance user = User.find_by_id(params[:id]) @@ -32,16 +32,23 @@ class WalletsController < ApplicationController @wallet = user.get_wallet scope = CoinChange.where('to_wallet_id = ? OR from_wallet_id = ?', @wallet.id, @wallet.id) t1 = Time.now - t2 = Time.new(t1.year, t1.month, t1.day-6) + t2 = Time.new(t1.year, t1.month, t1.day - 6) @balance_chart_data = scope.where('created_at > ? AND created_at < ?', t2, t1) @balance_chart_array = to_array(@balance_chart_data, @wallet.id) end + def community_data + t1 = Time.now + t2 = Time.new(t1.year, t1.month, t1.day - 6) + coin_changes = CoinChange.where('created_at >= ?', t2) + @community_data_array = community_data_to_array(coin_changes) + end + private def to_array(data, id) t1 = Time.now - start_time = Time.new(t1.year, t1.month, t1.day-6) - end_time = Time.new(start_time.year, start_time.month, start_time.day+1) + start_time = Time.new(t1.year, t1.month, t1.day - 6) + end_time = Time.new(start_time.year, start_time.month, start_time.day + 1) income = Array.new(7, 0) # 收入、支出 outcome = Array.new(7, 0) @@ -50,6 +57,7 @@ class WalletsController < ApplicationController index = 0 data.each do |i| + # 更新日期 until (i.created_at >= start_time) && (i.created_at < end_time) index += 1 start_time = end_time @@ -60,6 +68,7 @@ class WalletsController < ApplicationController if i.from_wallet_id == id outcome[index] += i.amount else + next if params[:sponsor] == true && i.from_wallet_id.nil? income[index] += i.amount end @@ -74,4 +83,31 @@ class WalletsController < ApplicationController Array[income, outcome, date] end + + def community_data_to_array(coin_changes) + t1 = Time.now + start_time = Time.new(t1.year, t1.month, t1.day - 6) + + nums = Array.new(7, 0) + date = Array.new(7) + end_time = Array.new(7) + # date[0] = Time.new(start_time.year, start_time.month, start_time.day) + index = 0 + + (0..6).each do |i| + # 更新日期,date[i]表示第i天0点 + date[i] = Time.new(start_time.year, start_time.month, start_time.day + i) + end_time[i] = Time.new(start_time.year, start_time.month, start_time.day + i+1) + end + + coin_changes.each do |cc| + (0..6).each do |i| + if !cc.from_wallet_id.nil? && cc.created_at>=date[i] && cc.created_at 'log#download', :constraints => { filename: /[0-z\.]+/ }, via:[:get]