rewrite wallet module
This commit is contained in:
parent
b524111ae6
commit
eaf78c4d8f
|
@ -11,6 +11,11 @@
|
|||
# sync_subject :boolean default("0")
|
||||
# sync_shixun :boolean default("0")
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_laboratories_on_identifier (identifier) UNIQUE
|
||||
# index_laboratories_on_school_id (school_id)
|
||||
#
|
||||
|
||||
class Laboratory < ApplicationRecord
|
||||
belongs_to :school, optional: true
|
||||
|
|
|
@ -6,6 +6,10 @@
|
|||
# laboratory_id :integer
|
||||
# config :text(65535)
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_laboratory_settings_on_laboratory_id (laboratory_id)
|
||||
#
|
||||
|
||||
class LaboratorySetting < ApplicationRecord
|
||||
belongs_to :laboratory
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
# content :text(65535)
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# is_secret :boolean default("0")
|
||||
#
|
||||
|
||||
class License < ApplicationRecord
|
||||
|
|
|
@ -11,7 +11,6 @@
|
|||
# course_group_id :integer default("0")
|
||||
# is_collect :integer default("1")
|
||||
# graduation_group_id :integer default("0")
|
||||
# is_apply_signature :boolean default("0")
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
|
|
|
@ -45,7 +45,12 @@
|
|||
# is_shixun_marker :boolean default("0")
|
||||
# is_sync_pwd :boolean default("1")
|
||||
# watchers_count :integer default("0")
|
||||
# sponsor_certification :integer default("0")
|
||||
# sponsor_num :integer default("0")
|
||||
# sponsored_num :integer default("0")
|
||||
# description :text(65535)
|
||||
# devops_step :integer default("0")
|
||||
# award_time :datetime
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
|
|
|
@ -74,6 +74,7 @@
|
|||
|
||||
|
||||
|
||||
|
||||
class Project < ApplicationRecord
|
||||
include Matchable
|
||||
include Publicable
|
||||
|
|
|
@ -26,19 +26,18 @@ class Sponsorship < ApplicationRecord
|
|||
sponsor_wallet = sponsor.get_wallet
|
||||
developer_wallet = developer.get_wallet
|
||||
|
||||
success = false
|
||||
Wallet.transaction do
|
||||
return false if sponsor.wallet.balance < amount
|
||||
|
||||
sponsor_wallet.update(balance: sponsor_wallet.balance -= amount)
|
||||
developer_wallet.update(balance: developer_wallet.balance += amount)
|
||||
success = sponsor_wallet.pay(amount)
|
||||
if success
|
||||
success developer_wallet.receive(amount)
|
||||
update(accumulate: self.accumulate += amount)
|
||||
end
|
||||
reason = "#{sponsor.full_name}向#{developer.full_name}的赞助支付。"
|
||||
coinchange = CoinChange.new(amount: amount, reason: reason, to_wallet_id: developer_wallet.id, from_wallet_id: sponsor_wallet.id)
|
||||
if coinchange.save
|
||||
return true
|
||||
return true if coinchange.save
|
||||
end
|
||||
false
|
||||
end
|
||||
success
|
||||
end
|
||||
|
||||
def self.monthly_payment
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
# tokens_value (value) UNIQUE
|
||||
#
|
||||
|
||||
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
|
|
|
@ -757,11 +757,24 @@ class User < Owner
|
|||
end
|
||||
|
||||
def get_wallet
|
||||
# if wallet.nil?
|
||||
# Wallet.transaction(isolation: :serializable) do
|
||||
# if wallet.nil?
|
||||
# create_wallet(balance: 100)
|
||||
# reason = "系统初始赠送"
|
||||
# CoinChange.create(amount: amount, reason: reason, to_wallet_id: wallet.id)
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
if wallet.nil?
|
||||
Wallet.wallet_lock.lock
|
||||
if wallet.nil?
|
||||
create_wallet(balance: 100)
|
||||
reason = "系统初始赠送"
|
||||
CoinChange.create(amount: amount, reason: reason, to_wallet_id: wallet.id)
|
||||
end
|
||||
Wallet.wallet_lock.unlock
|
||||
end
|
||||
wallet
|
||||
end
|
||||
|
||||
|
@ -773,7 +786,7 @@ class User < Owner
|
|||
self.update_column(:award_time, Time.now)
|
||||
amount = 2
|
||||
user_wallet = get_wallet
|
||||
user_wallet.update(balance: user_wallet.balance += amount)
|
||||
user_wallet.receive(amount)
|
||||
reason = "每日登录奖励"
|
||||
CoinChange.create(amount: amount, reason: reason, to_wallet_id: user_wallet.id)
|
||||
end
|
||||
|
|
|
@ -14,4 +14,29 @@ class Wallet < ApplicationRecord
|
|||
has_many :outcome, class_name: 'CoinChange', foreign_key: 'from_wallet_id', dependent: :destroy
|
||||
has_many :income, class_name: 'CoinChange', foreign_key: 'to_wallet_id', dependent: :destroy
|
||||
validates :balance, presence: true
|
||||
@@wallet_lock = Mutex.new
|
||||
|
||||
def receive(amount)
|
||||
with_lock do
|
||||
self.balance += amount
|
||||
save!
|
||||
end
|
||||
end
|
||||
|
||||
def pay(amount)
|
||||
with_lock do
|
||||
if self.balance < amount
|
||||
reload
|
||||
return false
|
||||
else
|
||||
self.balance -= amount
|
||||
save!
|
||||
end
|
||||
end
|
||||
true
|
||||
end
|
||||
|
||||
def self.wallet_lock
|
||||
@@wallet_lock
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue