diff --git a/app/controllers/wallets_controller.rb b/app/controllers/wallets_controller.rb index 064db910..1b811acc 100644 --- a/app/controllers/wallets_controller.rb +++ b/app/controllers/wallets_controller.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 15e47cb7..39d32b0b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/views/wallets/balance_chart.json.jbuilder b/app/views/wallets/balance_chart.json.jbuilder new file mode 100644 index 00000000..ba57d0f7 --- /dev/null +++ b/app/views/wallets/balance_chart.json.jbuilder @@ -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 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index b450c11f..67a3386f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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' diff --git a/public/images/undefined b/public/images/undefined new file mode 100644 index 00000000..b2478a82 Binary files /dev/null and b/public/images/undefined differ