add 交易记录

This commit is contained in:
iVampireSP.com 2022-09-15 12:13:37 +08:00
parent 89442913f0
commit 2ae48abae2
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
11 changed files with 269 additions and 105 deletions

View File

@ -7,66 +7,3 @@ function now($timezone = null)
{
return Carbon::now($timezone);
}
function getDrops($user_id)
{
$cache_key = 'user_drops_' . $user_id;
$decimal = config('drops.decimal');
// 计算需要乘以多少
$multiple = 1;
for ($i = 0; $i < $decimal; $i++) {
$multiple *= 10;
}
$drops = Cache::get($cache_key);
// 除以 $multiple
$drops = $drops / $multiple;
return $drops;
}
function reduceDrops($user_id, $amount = 0)
{
$cache_key = 'user_drops_' . $user_id;
$decimal = config('drops.decimal');
// 计算需要乘以多少
$multiple = 1;
for ($i = 0; $i < $decimal; $i++) {
$multiple *= 10;
}
$month = now()->month;
Cache::increment('user_' . $user_id . '_month_' . $month . '_drops', $amount);
$amount = $amount * $multiple;
$drops = Cache::decrement($cache_key, $amount);
return $drops;
}
function addDrops($user_id, $amount = 0)
{
$cache_key = 'user_drops_' . $user_id;
$decimal = config('drops.decimal');
// 计算需要乘以多少
$multiple = 1;
for ($i = 0; $i < $decimal; $i++) {
$multiple *= 10;
}
$amount = $amount * $multiple;
$drops = Cache::increment($cache_key, $amount);
return $drops;
}

View File

@ -11,6 +11,7 @@
use Illuminate\Support\Facades\Cache;
use Alipay\EasySDK\Kernel\Util\ResponseChecker;
use Alipay\EasySDK\Kernel\Factory as AlipayFactory;
use App\Models\Transaction;
class BalanceController extends Controller
{
@ -179,6 +180,13 @@ public function checkAndCharge($out_trade_no, Balance $balance)
// }
public function transactions() {
$transactions = Transaction::thisUser()->simplePaginate(30);
return $this->success($transactions);
}
public function drops()
{
@ -188,8 +196,10 @@ public function drops()
$cache_key = 'user_' . $user_id . '_month_' . $month . '_drops';
$transactions = new Transaction();
$resp = [
'drops' => getDrops($user_id),
'drops' => $transactions->getDrops($user_id),
'monthly_usages' => (float) Cache::get($cache_key, 0),
'rate' => config('drops.rate'),
'decimal' => config('drops.decimal'),

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers;
use App\Models\Transaction;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
@ -12,7 +13,9 @@ public function index(Request $request)
{
$user = $request->user();
$user['drops'] = getDrops($user['id']);
$transaction = new Transaction();
$user['drops'] = $transaction->getDrops($user['id']);
$user['drops_rate'] = config('drops.rate');
return $this->success($user);

View File

@ -1,26 +0,0 @@
<?php
namespace App\Jobs;
class ExampleJob extends Job
{
/**
* Create a new job instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
//
}
}

View File

@ -2,10 +2,11 @@
namespace App\Models;
use App\Models\Transaction;
use App\Models\Module\Module;
use App\Models\WorkOrder\WorkOrder;
use Illuminate\Support\Facades\Cache;
// use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache;
use Illuminate\Database\Eloquent\Model;
use App\Exceptions\User\BalanceNotEnoughException;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@ -89,7 +90,9 @@ public function cost($price = null)
{
$this->load('user');
$drops = getDrops($this->user_id);
$transaction = new Transaction();
$drops = $transaction->getDrops($this->user_id);
if ($price !== null) {
$this->price = $price;
@ -134,7 +137,9 @@ public function cost($price = null)
Cache::put($month_cache_key, $hosts_drops, 604800);
reduceDrops($this->user_id, $this->price);
$description = $this->name . ' 扣费。';
$transaction->reduceDrops($this->user_id, $this->price, $description);
return true;
}

View File

@ -3,7 +3,7 @@
namespace App\Models;
use Jenssegers\Mongodb\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class Transaction extends Model
{
@ -12,14 +12,224 @@ class Transaction extends Model
protected $connection = 'mongodb';
protected $collection = 'transactions';
// 停用 updated_at
const UPDATED_AT = null;
protected $dates = [
'created_at',
'updated_at',
'time',
'created_at'
'paid_at',
];
protected $fillable = [
'name'
// 交易类型
'type',
// 交易渠道
'payment',
// 描述
'description',
// 入账
'income',
// 入账 Drops
'income_drops',
// 出账
'outcome',
// 出账 Drops
'outcome_drops',
// 可用余额
'balance',
// 可用 Drops
'drops',
// 赠送金额
'gift',
// 赠送 Drops
'gift_drops',
'user_id',
];
// scope this user
public function scopeThisUser($query)
{
return $query->where('user_id', auth()->id());
}
public function getDrops($user_id = null)
{
//
if (!$user_id) {
$user_id = auth()->id();
}
$cache_key = 'user_drops_' . $user_id;
$decimal = config('drops.decimal');
// 计算需要乘以多少
$multiple = 1;
for ($i = 0; $i < $decimal; $i++) {
$multiple *= 10;
}
$drops = Cache::get($cache_key);
// 除以 $multiple
$drops = $drops / $multiple;
return $drops;
}
public function reduceCurrentUserDrops($amount = 0)
{
return $this->reduceDrops(auth()->id(), $amount);
}
public function increaseCurrentUserDrops($amount = 0)
{
return $this->increaseDrops(auth()->id(), $amount);
}
public function increaseDrops($user_id, $amount = 0)
{
$cache_key = 'user_drops_' . $user_id;
$decimal = config('drops.decimal');
// 计算需要乘以多少
$multiple = 1;
for ($i = 0; $i < $decimal; $i++) {
$multiple *= 10;
}
$amount = $amount * $multiple;
$drops = Cache::increment($cache_key, $amount);
return $drops;
}
public function reduceDrops($user_id, $amount = 0, $description = null)
{
$cache_key = 'user_drops_' . $user_id;
$decimal = config('drops.decimal');
// 计算需要乘以多少
$multiple = 1;
for ($i = 0; $i < $decimal; $i++) {
$multiple *= 10;
}
$month = now()->month;
Cache::increment('user_' . $user_id . '_month_' . $month . '_drops', $amount);
$amount = $amount * $multiple;
$drops = Cache::decrement($cache_key, $amount);
// (new App\Models\Transaction)->create(['name' => 1]);
$this->addPayoutDrops($user_id, $amount, $description);
return $drops;
}
public function addPayoutDrops($user_id, $amount, $description)
{
$data = [
'type' => 'payout',
'payment' => 'drops',
'description' => $description,
'income' => 0,
'income_drops' => 0,
'outcome' => 0,
'outcome_drops' => $amount,
];
return $this->addLog($user_id, $data);
}
public function addIncomeDrops($user_id, $amount, $description)
{
$data = [
'type' => 'income',
'payment' => 'balance',
'description' => $description,
'income' => 0,
'income_drops' => $amount,
'outcome' => 0,
'outcome_drops' => 0,
];
return $this->addLog($user_id, $data);
}
public function addIncomeBalance($user_id, $payment, $amount, $description)
{
$data = [
'type' => 'income',
'payment' => $payment,
'description' => $description,
'income' => $amount,
'income_drops' => 0,
'outcome' => 0,
'outcome_drops' => 0,
];
return $this->addLog($user_id, $data);
}
public function addPayoutBalance($user_id, $amount, $description)
{
$data = [
'type' => 'payout',
'payment' => 'balance',
'description' => $description,
'income' => 0,
'income_drops' => 0,
'outcome' => $amount,
'outcome_drops' => 0,
];
return $this->addLog($user_id, $data);
}
private function addLog($user_id, $data)
{
$user = User::find($user_id);
$current = [
'balance' => $user->balance,
'drops' => $this->getDrops($user_id),
'user_id' => $user_id,
];
// merge
$data = array_merge($data, $current);
// add expired at
$data['expired_at'] = now()->addSeconds(7);
return $this->create($data);
}
}

View File

@ -2,6 +2,7 @@
namespace App\Models;
use App\Models\Transaction;
use App\Exceptions\CommonException;
use Illuminate\Auth\Authenticatable;
use Laravel\Lumen\Auth\Authorizable;
@ -51,7 +52,9 @@ public function toDrops($amount = 1)
$cache_key = 'user_drops_' . $this->id;
$drops = getDrops($this->id);
$transactions = new Transaction();
$drops = $transactions->getDrops($this->id);
$total = 0;
@ -68,7 +71,11 @@ public function toDrops($amount = 1)
$this->balance -= $amount;
$this->save();
addDrops($this->id, $total);
$transactions->increaseDrops($this->id, $total);
// $transactions
$transactions->addPayoutBalance($this->id, $amount, '自动转换为 Drops');
// if user balance <= 0
if ($this->balance < $amount) {
@ -83,6 +90,14 @@ public function toDrops($amount = 1)
return $this;
}
// when update
// protected static function boot()
// {
// parent::boot();
// // when update
// static::updating(function ($model) {
// });
// }
}

View File

@ -21,6 +21,7 @@
"laravel/lumen-framework": "^9.0",
"league/flysystem": "^3.2",
"mmghv/lumen-route-binding": "^1.0",
"mongodb/mongodb": "^1.13",
"predis/predis": "^2.0"
},
"require-dev": {

2
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "f8052eee8b55fc09ba079f652a00ff9d",
"content-hash": "22a158b343410ac17bf6d2ee94827c2c",
"packages": [
{
"name": "adbario/php-dot-notation",

View File

@ -13,10 +13,13 @@
*/
public function up()
{
Schema::create('transactions', function (Blueprint $table) {
$table->id();
$table->timestamps();
Schema::connection('mongodb')->create('transactions', function (Blueprint $collection) {
$collection->unsignedBigInteger('user_id')->index();
$collection->expire('created_at', now()->addYear());
});
}
/**
@ -26,6 +29,6 @@ public function up()
*/
public function down()
{
Schema::dropIfExists('transactions');
Schema::connection('mongodb')->dropIfExists('transactions');
}
};

View File

@ -30,10 +30,16 @@
$router->get('/', [
'uses' => 'User\BalanceController@index'
]);
$router->post('/', [
'uses' => 'User\BalanceController@store'
]);
$router->get('/transactions', [
'uses' => 'User\BalanceController@transactions'
]);
$router->get('/drops', [
'uses' => 'User\BalanceController@drops'
]);