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