forked from Gitlink/forgeplus
coinchange chart
This commit is contained in:
parent
661c8dda85
commit
7776ab29ed
|
@ -1,5 +1,5 @@
|
|||
class WalletsController < ApplicationController
|
||||
before_action :require_login
|
||||
before_action :require_login, except: :balance_chart
|
||||
|
||||
def balance
|
||||
user = User.find_by_id(params[:id])
|
||||
|
@ -20,9 +20,58 @@ class WalletsController < ApplicationController
|
|||
|
||||
sort = params[:sort_by] || "created_at"
|
||||
sort_direction = params[:sort_direction] || "desc"
|
||||
scope = scope.reorder("#{sort} #{sort_direction}")
|
||||
scope = scope.reorder("#{sort} #{sort_direction}") unless scope.nil?
|
||||
|
||||
@total = scope.length
|
||||
@coin_changes = kaminari_paginate(scope)
|
||||
@total = 0
|
||||
@total = scope.length unless scope.nil?
|
||||
@coin_changes = kaminari_paginate(scope) unless scope.nil?
|
||||
end
|
||||
|
||||
def balance_chart
|
||||
user = User.find_by_id(params[:id])
|
||||
@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)
|
||||
@balance_chart_data = scope.where('created_at > ? AND created_at < ?', t2, t1)
|
||||
@balance_chart_array = to_array(@balance_chart_data, @wallet.id)
|
||||
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)
|
||||
|
||||
income = Array.new(7, 0) # 收入、支出
|
||||
outcome = Array.new(7, 0)
|
||||
date = Array.new(7)
|
||||
date[0] = Time.new(start_time.year, start_time.month, start_time.day)
|
||||
index = 0
|
||||
|
||||
data.each do |i|
|
||||
until (i.created_at >= start_time) && (i.created_at < end_time)
|
||||
index += 1
|
||||
start_time = end_time
|
||||
end_time = Time.new(start_time.year, start_time.month, start_time.day + 1)
|
||||
date[index] = Time.new(start_time.year, start_time.month, start_time.day)
|
||||
end
|
||||
|
||||
if i.from_wallet_id == id
|
||||
outcome[index] += i.amount
|
||||
else
|
||||
income[index] += i.amount
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
until end_time >= Time.now
|
||||
index += 1
|
||||
start_time = end_time
|
||||
end_time = Time.new(start_time.year, start_time.month, start_time.day + 1)
|
||||
date[index] = Time.new(start_time.year, start_time.month, start_time.day)
|
||||
end
|
||||
|
||||
Array[income, outcome, date]
|
||||
end
|
||||
end
|
||||
|
|
|
@ -767,13 +767,13 @@ class User < Owner
|
|||
# end
|
||||
# end
|
||||
if wallet.nil?
|
||||
Wallet.wallet_lock.lock
|
||||
Wallet.transaction(isolation: :serializable) do
|
||||
if wallet.nil?
|
||||
create_wallet(balance: 100)
|
||||
reason = "系统初始赠送"
|
||||
CoinChange.create(amount: amount, reason: reason, to_wallet_id: wallet.id)
|
||||
CoinChange.create(amount: 100, reason: reason, to_wallet_id: wallet.id)
|
||||
end
|
||||
Wallet.wallet_lock.unlock
|
||||
end
|
||||
end
|
||||
wallet
|
||||
end
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
# 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.primary @balance_chart_array[2][i]
|
||||
json.secondary @balance_chart_array[0][i]
|
||||
end
|
||||
end
|
||||
else
|
||||
json.label '支出'
|
||||
json.data do
|
||||
json.array! (0..6).each do |i|
|
||||
json.primary @balance_chart_array[2][i]
|
||||
json.secondary @balance_chart_array[1][i]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -26,6 +26,7 @@ Rails.application.routes.draw do
|
|||
scope '/api' do
|
||||
get 'wallets/balance'
|
||||
get 'wallets/coin_changes'
|
||||
get 'wallets/balance_chart'
|
||||
|
||||
get 'log/list', to: 'log#list'
|
||||
# post 'log/download', to: 'log#download'
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 9.7 KiB |
Loading…
Reference in New Issue