添加统计图标接口
This commit is contained in:
parent
7776ab29ed
commit
c9b15a02d6
|
@ -16,12 +16,12 @@ class SponsorshipsController < ApplicationController
|
||||||
|
|
||||||
def sponsored
|
def sponsored
|
||||||
if User.current.id == Integer(params[:id])
|
if User.current.id == Integer(params[:id])
|
||||||
@sponsorships = Sponsorship.where("developer_id=?", params[:id])
|
@sponsorships = Sponsorship.where('developer_id=?', params[:id])
|
||||||
else
|
else
|
||||||
@sponsorships = Sponsorship.where("developer_id=? AND visible=1", params[:id])
|
@sponsorships = Sponsorship.where('developer_id=? AND visible=1', params[:id])
|
||||||
end
|
end
|
||||||
sort = params[:sort_by] || "created_at"
|
sort = params[:sort_by] || 'created_at'
|
||||||
sort_direction = params[:sort_direction] || "desc"
|
sort_direction = params[:sort_direction] || 'desc'
|
||||||
@sponsorships = @sponsorships.reorder("#{sort} #{sort_direction}")
|
@sponsorships = @sponsorships.reorder("#{sort} #{sort_direction}")
|
||||||
@total = @sponsorships.length
|
@total = @sponsorships.length
|
||||||
@sponsorships = kaminari_paginate(@sponsorships)
|
@sponsorships = kaminari_paginate(@sponsorships)
|
||||||
|
@ -29,12 +29,12 @@ class SponsorshipsController < ApplicationController
|
||||||
|
|
||||||
def sponsoring
|
def sponsoring
|
||||||
if User.current.id == Integer(params[:id])
|
if User.current.id == Integer(params[:id])
|
||||||
@sponsorships = Sponsorship.where("sponsor_id=?", params[:id])
|
@sponsorships = Sponsorship.where('sponsor_id=?', params[:id])
|
||||||
else
|
else
|
||||||
@sponsorships = Sponsorship.where("sponsor_id=? AND visible=1", params[:id])
|
@sponsorships = Sponsorship.where('sponsor_id=? AND visible=1', params[:id])
|
||||||
end
|
end
|
||||||
sort = params[:sort_by] || "created_at"
|
sort = params[:sort_by] || 'created_at'
|
||||||
sort_direction = params[:sort_direction] || "desc"
|
sort_direction = params[:sort_direction] || 'desc'
|
||||||
@sponsorships = @sponsorships.reorder("#{sort} #{sort_direction}")
|
@sponsorships = @sponsorships.reorder("#{sort} #{sort_direction}")
|
||||||
@total = @sponsorships.length
|
@total = @sponsorships.length
|
||||||
@sponsorships = kaminari_paginate(@sponsorships)
|
@sponsorships = kaminari_paginate(@sponsorships)
|
||||||
|
@ -42,12 +42,12 @@ class SponsorshipsController < ApplicationController
|
||||||
|
|
||||||
def stopped_sponsored
|
def stopped_sponsored
|
||||||
if User.current.id == Integer(params[:id])
|
if User.current.id == Integer(params[:id])
|
||||||
@stopped_sponsorships = StoppedSponsorship.where("developer_id=?", params[:id])
|
@stopped_sponsorships = StoppedSponsorship.where('developer_id=?', params[:id])
|
||||||
else
|
else
|
||||||
@stopped_sponsorships = StoppedSponsorship.where("developer_id=? AND visible=1", params[:id])
|
@stopped_sponsorships = StoppedSponsorship.where('developer_id=? AND visible=1', params[:id])
|
||||||
end
|
end
|
||||||
sort = params[:sort_by] || "created_at"
|
sort = params[:sort_by] || 'created_at'
|
||||||
sort_direction = params[:sort_direction] || "desc"
|
sort_direction = params[:sort_direction] || 'desc'
|
||||||
@stopped_sponsorships = @stopped_sponsorships.reorder("#{sort} #{sort_direction}")
|
@stopped_sponsorships = @stopped_sponsorships.reorder("#{sort} #{sort_direction}")
|
||||||
@total = @stopped_sponsorships.length
|
@total = @stopped_sponsorships.length
|
||||||
@stopped_sponsorships = kaminari_paginate(@stopped_sponsorships)
|
@stopped_sponsorships = kaminari_paginate(@stopped_sponsorships)
|
||||||
|
@ -55,12 +55,12 @@ class SponsorshipsController < ApplicationController
|
||||||
|
|
||||||
def stopped_sponsoring
|
def stopped_sponsoring
|
||||||
if User.current.id == Integer(params[:id])
|
if User.current.id == Integer(params[:id])
|
||||||
@stopped_sponsorships = StoppedSponsorship.where("sponsor_id=?", params[:id])
|
@stopped_sponsorships = StoppedSponsorship.where('sponsor_id=?', params[:id])
|
||||||
else
|
else
|
||||||
@stopped_sponsorships = StoppedSponsorship.where("sponsor_id=? AND visible=1", params[:id])
|
@stopped_sponsorships = StoppedSponsorship.where('sponsor_id=? AND visible=1', params[:id])
|
||||||
end
|
end
|
||||||
sort = params[:sort_by] || "created_at"
|
sort = params[:sort_by] || 'created_at'
|
||||||
sort_direction = params[:sort_direction] || "desc"
|
sort_direction = params[:sort_direction] || 'desc'
|
||||||
@stopped_sponsorships = @stopped_sponsorships.reorder("#{sort} #{sort_direction}")
|
@stopped_sponsorships = @stopped_sponsorships.reorder("#{sort} #{sort_direction}")
|
||||||
@total = @stopped_sponsorships.length
|
@total = @stopped_sponsorships.length
|
||||||
@stopped_sponsorships = kaminari_paginate(@stopped_sponsorships)
|
@stopped_sponsorships = kaminari_paginate(@stopped_sponsorships)
|
||||||
|
@ -68,14 +68,13 @@ class SponsorshipsController < ApplicationController
|
||||||
|
|
||||||
# GET /sponsorships/1
|
# GET /sponsorships/1
|
||||||
# GET /sponsorships/1.json
|
# GET /sponsorships/1.json
|
||||||
def show
|
def show; end
|
||||||
end
|
|
||||||
|
|
||||||
# POST /sponsorships
|
# POST /sponsorships
|
||||||
# POST /sponsorships.json
|
# POST /sponsorships.json
|
||||||
def create
|
def create
|
||||||
sponsor_id = User.current.id
|
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}))
|
@sponsorship = Sponsorship.new(sponsorship_params.merge({sponsor_id: sponsor_id}))
|
||||||
|
|
||||||
|
@ -144,13 +143,22 @@ class SponsorshipsController < ApplicationController
|
||||||
developer = @sponsorship.developer
|
developer = @sponsorship.developer
|
||||||
sponsor = @sponsorship.sponsor
|
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
|
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
|
else
|
||||||
render json: {status: -1, message: "失败"}
|
render json: {status: -1, message: '失败'}
|
||||||
end
|
end
|
||||||
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
|
private
|
||||||
|
|
||||||
# Use callbacks to share common setup or constraints between actions.
|
# Use callbacks to share common setup or constraints between actions.
|
||||||
def set_sponsorship
|
def set_sponsorship
|
||||||
@sponsorship = Sponsorship.find(params[:id])
|
@sponsorship = Sponsorship.find(params[:id])
|
||||||
|
@ -160,4 +168,44 @@ class SponsorshipsController < ApplicationController
|
||||||
def sponsorship_params
|
def sponsorship_params
|
||||||
params.require(:sponsorship).permit(:amount, :visible, :sponsor_id, :developer_id, :single, :page, :limit, :sort_by, :search)
|
params.require(:sponsorship).permit(:amount, :visible, :sponsor_id, :developer_id, :single, :page, :limit, :sort_by, :search)
|
||||||
end
|
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
|
||||||
|
|
||||||
|
Array[sponsor, sponsored, date]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
class WalletsController < ApplicationController
|
class WalletsController < ApplicationController
|
||||||
before_action :require_login, except: :balance_chart
|
before_action :require_login, except: :community_data
|
||||||
|
|
||||||
def balance
|
def balance
|
||||||
user = User.find_by_id(params[:id])
|
user = User.find_by_id(params[:id])
|
||||||
|
@ -32,16 +32,23 @@ class WalletsController < ApplicationController
|
||||||
@wallet = user.get_wallet
|
@wallet = user.get_wallet
|
||||||
scope = CoinChange.where('to_wallet_id = ? OR from_wallet_id = ?', @wallet.id, @wallet.id)
|
scope = CoinChange.where('to_wallet_id = ? OR from_wallet_id = ?', @wallet.id, @wallet.id)
|
||||||
t1 = Time.now
|
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_data = scope.where('created_at > ? AND created_at < ?', t2, t1)
|
||||||
@balance_chart_array = to_array(@balance_chart_data, @wallet.id)
|
@balance_chart_array = to_array(@balance_chart_data, @wallet.id)
|
||||||
end
|
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
|
private
|
||||||
def to_array(data, id)
|
def to_array(data, id)
|
||||||
t1 = Time.now
|
t1 = Time.now
|
||||||
start_time = Time.new(t1.year, t1.month, t1.day-6)
|
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)
|
end_time = Time.new(start_time.year, start_time.month, start_time.day + 1)
|
||||||
|
|
||||||
income = Array.new(7, 0) # 收入、支出
|
income = Array.new(7, 0) # 收入、支出
|
||||||
outcome = Array.new(7, 0)
|
outcome = Array.new(7, 0)
|
||||||
|
@ -50,6 +57,7 @@ class WalletsController < ApplicationController
|
||||||
index = 0
|
index = 0
|
||||||
|
|
||||||
data.each do |i|
|
data.each do |i|
|
||||||
|
# 更新日期
|
||||||
until (i.created_at >= start_time) && (i.created_at < end_time)
|
until (i.created_at >= start_time) && (i.created_at < end_time)
|
||||||
index += 1
|
index += 1
|
||||||
start_time = end_time
|
start_time = end_time
|
||||||
|
@ -60,6 +68,7 @@ class WalletsController < ApplicationController
|
||||||
if i.from_wallet_id == id
|
if i.from_wallet_id == id
|
||||||
outcome[index] += i.amount
|
outcome[index] += i.amount
|
||||||
else
|
else
|
||||||
|
next if params[:sponsor] == true && i.from_wallet_id.nil?
|
||||||
income[index] += i.amount
|
income[index] += i.amount
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -74,4 +83,31 @@ class WalletsController < ApplicationController
|
||||||
|
|
||||||
Array[income, outcome, date]
|
Array[income, outcome, date]
|
||||||
end
|
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<end_time[i]
|
||||||
|
nums[i] += 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Array[nums, date]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
json.sponsor do
|
||||||
|
json.array! (0..6).each do |i|
|
||||||
|
json.y @community_data_array[0][i]
|
||||||
|
json.x @community_data_array[2][i].to_date
|
||||||
|
end
|
||||||
|
end
|
||||||
|
json.sponsored do
|
||||||
|
json.array! (0..6).each do |i|
|
||||||
|
json.y @community_data_array[1][i]
|
||||||
|
json.x @community_data_array[2][i].to_date
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,31 +1,12 @@
|
||||||
# json.income do
|
json.income do
|
||||||
# json.array! (0..6).each do |i|
|
|
||||||
# json.amount @balance_chart_array[0][i]
|
|
||||||
# json.date @balance_chart_array[2][i]
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
# json.outcome do
|
|
||||||
# json.array! (0..6).each do |i|
|
|
||||||
# json.amount @balance_chart_array[1][i]
|
|
||||||
# json.date @balance_chart_array[2][i]
|
|
||||||
# end
|
|
||||||
# end
|
|
||||||
json.array! (0..1).each do |index|
|
|
||||||
if index == 0
|
|
||||||
json.label '收入'
|
|
||||||
json.data do
|
|
||||||
json.array! (0..6).each do |i|
|
json.array! (0..6).each do |i|
|
||||||
json.primary @balance_chart_array[2][i]
|
json.y @balance_chart_array[0][i]
|
||||||
json.secondary @balance_chart_array[0][i]
|
json.x @balance_chart_array[2][i].to_date
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
json.outcome do
|
||||||
json.label '支出'
|
json.array! (0..6).each do |i|
|
||||||
json.data do
|
json.y @balance_chart_array[1][i]
|
||||||
json.array! (0..6).each do |i|
|
json.x @balance_chart_array[2][i].to_date
|
||||||
json.primary @balance_chart_array[2][i]
|
|
||||||
json.secondary @balance_chart_array[1][i]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -0,0 +1,4 @@
|
||||||
|
json.array! (0..6).each do |i|
|
||||||
|
json.y @community_data_array[0][i]
|
||||||
|
json.x @community_data_array[1][i].to_date
|
||||||
|
end
|
|
@ -28,6 +28,9 @@ Rails.application.routes.draw do
|
||||||
get 'wallets/coin_changes'
|
get 'wallets/coin_changes'
|
||||||
get 'wallets/balance_chart'
|
get 'wallets/balance_chart'
|
||||||
|
|
||||||
|
get 'sponsorships/community_data'
|
||||||
|
get 'wallets/community_data'
|
||||||
|
|
||||||
get 'log/list', to: 'log#list'
|
get 'log/list', to: 'log#list'
|
||||||
# post 'log/download', to: 'log#download'
|
# post 'log/download', to: 'log#download'
|
||||||
match 'log/download/:filename' => 'log#download', :constraints => { filename: /[0-z\.]+/ }, via:[:get]
|
match 'log/download/:filename' => 'log#download', :constraints => { filename: /[0-z\.]+/ }, via:[:get]
|
||||||
|
|
Loading…
Reference in New Issue