feat: 项目基础框架+配置+Kernel
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
namespace App\DAO\PrizePool;
|
||||
|
||||
use App\PrizePool as PrizePoolModel;
|
||||
use App\Setting;
|
||||
|
||||
class CandyCalculator implements PrizeCalculator
|
||||
{
|
||||
protected $reward_type = PrizePoolModel::REWARD_CANDY;
|
||||
protected $reward_currency = PrizePoolModel::CURRENCY_NONE;
|
||||
protected $currency_type = PrizePoolModel::CURRENCY_NONE;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function calculate($scene, $reward_qty, $to_user, $from_user = null, $memo = '', $attach_data = [])
|
||||
{
|
||||
$fasten_data = [
|
||||
'reward_type' => $this->reward_type,
|
||||
'reward_currency' => $this->reward_currency,
|
||||
'create_time' => time(), //发奖时间
|
||||
];
|
||||
$origin_reward_qty = $reward_qty; //原始奖励数量
|
||||
$candy_tousdt = Setting::getValueByKey('candy_tousdt', 100);
|
||||
$candy_tousdt = bc_div($candy_tousdt, 100);
|
||||
$reward_qty = bc_div($reward_qty, $candy_tousdt, 4);
|
||||
$extra_data = $attach_data['extra_data'];
|
||||
if (is_string($extra_data) && !empty($extra_data)) {
|
||||
$extra_data = unserialize($extra_data);
|
||||
}
|
||||
$extra_data['origin_reward_qty '] = $origin_reward_qty;
|
||||
$extra_data['candy_tousdt'] = $candy_tousdt;
|
||||
$attach_data['extra_data'] = serialize($extra_data);
|
||||
try {
|
||||
PrizePoolModel::unguard();
|
||||
$data = [
|
||||
'scene' => $scene,
|
||||
'reward_qty' => $reward_qty,
|
||||
'to_user_id' => $to_user->id,
|
||||
'from_user_id' => $from_user ? $from_user->id : $to_user->id,
|
||||
'memo' => $memo,
|
||||
];
|
||||
$data = array_merge($data, $attach_data, $fasten_data);
|
||||
$prize_pool = PrizePoolModel::create($data);
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
} finally {
|
||||
PrizePoolModel::reguard();
|
||||
}
|
||||
return isset($prize_pool->id) ? $prize_pool : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
namespace App\DAO\PrizePool;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\PrizePool;
|
||||
use App\{Users, AccountLog};
|
||||
|
||||
class CandySender implements PrizeSender
|
||||
{
|
||||
public function send(\App\PrizePool &$prize) : bool
|
||||
{
|
||||
try {
|
||||
$prize->refresh();
|
||||
if ($prize->status != 0) {
|
||||
throw new \Exception('奖励发放异常');
|
||||
}
|
||||
if ($prize->reward_type != PrizePool::REWARD_CANDY) {
|
||||
throw new \Exception('奖励发放类型不匹配');
|
||||
}
|
||||
DB::transaction(function () use (&$prize) {
|
||||
$user = Users::lockForUpdate()->find($prize->to_user_id);
|
||||
$change_result = change_user_candy($user, $prize->reward_qty, AccountLog::REWARD_CANDY, $prize->memo);
|
||||
if ($change_result !== true) {
|
||||
throw new \Exception($change_result);
|
||||
}
|
||||
$prize->receive_time = time();
|
||||
$prize->status = 1;
|
||||
$result = $prize->save();
|
||||
if (!$result) {
|
||||
throw new \Exception('奖励通证发放失败');
|
||||
}
|
||||
});
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$error_info = serialize([
|
||||
'time' => time(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
'message' => $e->getMessage(),
|
||||
]);
|
||||
$prize->error_info = $error_info;
|
||||
$prize->save();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\DAO\PrizePool;
|
||||
|
||||
use App\PrizePool as PrizePoolModel;
|
||||
|
||||
class CurrencyCalculator implements PrizeCalculator
|
||||
{
|
||||
protected $reward_type = PrizePoolModel::REWARD_CURRENCY;
|
||||
protected $reward_currency;
|
||||
protected $currency_type;
|
||||
|
||||
public function __construct($reward_currency, $currency_type)
|
||||
{
|
||||
$this->reward_currency = $reward_currency;
|
||||
$this->currency_type = $currency_type;
|
||||
}
|
||||
|
||||
public function calculate($scene, $reward_qty, $to_user, $from_user = null, $memo = '', $attach_data = [])
|
||||
{
|
||||
$fasten_data = [
|
||||
'reward_type' => $this->reward_type,
|
||||
'reward_currency' => $this->reward_currency,
|
||||
'create_time' => time(), //发奖时间
|
||||
];
|
||||
try {
|
||||
PrizePoolModel::unguard();
|
||||
$data = [
|
||||
'scene' => $scene,
|
||||
'reward_qty' => $reward_qty,
|
||||
'to_user_id' => $to_user->id,
|
||||
'from_user_id' => $from_user ? $from_user->id : $to_user->id,
|
||||
'memo' => $memo,
|
||||
];
|
||||
$data = array_merge($data, $attach_data, $fasten_data);
|
||||
$prize_pool = PrizePoolModel::create($data);
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
} finally {
|
||||
PrizePoolModel::reguard();
|
||||
}
|
||||
return isset($prize_pool->id) ? $prize_pool : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
namespace App\DAO\PrizePool;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\{Users, AccountLog, UsersWallet, PrizePool};
|
||||
|
||||
class CurrencySender implements PrizeSender
|
||||
{
|
||||
public function send(\App\PrizePool &$prize) : bool
|
||||
{
|
||||
try {
|
||||
$prize->refresh();
|
||||
if ($prize->status != 0) {
|
||||
throw new \Exception('奖励发放异常');
|
||||
}
|
||||
if ($prize->reward_type != PrizePool::REWARD_CURRENCY) {
|
||||
throw new \Exception('奖励发放类型不匹配');
|
||||
}
|
||||
if (!in_array($prize->currency_type, [1, 2, 3])) {
|
||||
throw new \Exception('币种类型不正确');
|
||||
}
|
||||
DB::transaction(function () use (&$prize) {
|
||||
$user_wallet = UsersWallet::where('user_id', $prize->to_user_id)
|
||||
->where('currency', $prize->reward_currency)
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
if (!$user_wallet) {
|
||||
throw new \Exception('用户钱包不存在');
|
||||
}
|
||||
$change_result = change_wallet_balance($user_wallet, $prize->currency_type, $prize->reward_qty, AccountLog::REWARD_CURRENCY, $prize->memo, false, $prize->from_user_id, $prize->sign, $extra_data);
|
||||
if ($change_result !== true) {
|
||||
throw new \Exception($change_result);
|
||||
}
|
||||
$prize->receive_time = time();
|
||||
$prize->status = 1;
|
||||
$result = $prize->save();
|
||||
if (!$result) {
|
||||
throw new \Exception('奖励通证发放失败');
|
||||
}
|
||||
});
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
$error_info = serialize([
|
||||
'time' => time(),
|
||||
'file' => $e->getFile(),
|
||||
'line' => $e->getLine(),
|
||||
'message' => $e->getMessage(),
|
||||
]);
|
||||
$prize->error_info = $error_info;
|
||||
$prize->save();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\DAO\PrizePool;
|
||||
|
||||
interface PrizeCalculator
|
||||
{
|
||||
/**
|
||||
* 计算奖励
|
||||
*
|
||||
* @param integer $scene 奖励场景
|
||||
* @param float $reward_qty 奖励数量
|
||||
* @param \App\Users $to_user 被奖励者
|
||||
* @param \App\Users $from_user 触发用户
|
||||
* @param string $memo 备注
|
||||
* @param array $attach_data 附加数据
|
||||
* @return \App\PrizePool 返回奖池记录
|
||||
*/
|
||||
public function calculate($scene, $reward_qty, $to_user, $from_user = null, $memo = '', $attach_data = []);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
namespace App\DAO\PrizePool;
|
||||
|
||||
interface PrizeSender
|
||||
{
|
||||
/**
|
||||
* 发放奖励
|
||||
*
|
||||
* @param \App\PrizePool $prize
|
||||
* @return boolean
|
||||
*/
|
||||
public function send(\App\PrizePool &$prize) : bool;
|
||||
}
|
||||
Reference in New Issue
Block a user