Commit a08abd05 by 杨树贤

添加流水的异步任务

parent 7f0c9062
......@@ -42,7 +42,6 @@ class IntegralBillsController extends Controller
{
$data = [
'user_id' => $request->user_id,
'amount' => $request->integral_amount,
'integral_id' => $request->integral_id,
'status' => 1,
'add_time' => time(),
......
......@@ -44,9 +44,7 @@ class IntegralBill extends Model
public function createIntegralBill($data = [])
{
$res = DB::transaction(function () use ($data) {
DB::table('integral_bills')->insert($data);
});
$res = DB::table('integral_bills')->insert($data);
return $res;
}
......@@ -86,9 +84,9 @@ class IntegralBill extends Model
->where('user_id', $userId)->value('integral');
return [
'total_amount' => $totalAmount,
'total_amount' => $totalAmount,
'exchanged_amount' => $exchangedAmount,
'integral' => $integral,
'integral' => $integral,
];
}
......
......@@ -49,4 +49,9 @@ class UserIntegral extends Model
return $res;
}
public function getUserIntegral($userId)
{
return DB::table('user_integrals')->where('user_id', $userId)->get()->toArray();
}
}
\ No newline at end of file
<?php
namespace App\Tasks;
use App\Models\IntegralBill;
use App\Models\UserIntegral;
use Common\Model\RedisModel;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use Illuminate\Support\Facades\DB;
class IntegralBillTask extends Task
{
private $userId;
private $integralId;
public function __construct($userId, $integralId)
{
$this->userId = $userId;
$this->integralId = $integralId;
}
public function handle()
{
try {
DB::transaction(function () {
//数据库里面插入数据
$data = [
'user_id' => $this->userId,
'integral_id' => $this->integralId,
'status' => 1,
'add_time' => time(),
];
$integralBill = new IntegralBill();
$result = $integralBill->createIntegralBill($data);
if (!$result) {
throw \Exception("插入用户红包账单失败,用户id是$this->userId,兑换类型id是$this->integralId");
}
$userIntegral = new UserIntegral();
$data = $userIntegral->getUserIntegral($this->userId);
if ($data) {
//将用户的累计可兑换金额写进redis里面的ic_user里面去
$amount = $data['amount'];
$redis = new RedisModel();
$user = json_decode($redis->hget('ic_user', $this->userId), true);
$user['integral'] = $data['integral'] + $amount;
$result = $redis->hset('ic_user', $this->userId, json_encode($user));
//还要写进user_integrals数据库
if ($result) {
$data = [
'integral' => $data['integral'] + $amount,
'update_time' => time(),
];
$result = $userIntegral->updateUserIntegral($this->userId, $data);
if (!$result) {
throw \Exception("更新用户剩余积分失败,用户id是$this->userId,兑换类型id是$this->integralId");
}
} else {
throw \Exception("更新redis里ic_user的integral失败,用户id是$this->userId,兑换类型id是$this->integralId");
}
}
});
} catch (\Exception $e) {
//抛出致命错误日志
ErrorLog(ErrorCode(1, 9), $e);
}
}
public function finish()
{
}
}
\ No newline at end of file
......@@ -143,13 +143,13 @@ return [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 2
'database' => 1
],
'read' =>[
'host' => env('REDIS_READ_HOST', ''),
'password' => env('REDIS_READ_PASSWORD', null),
'port' => env('REDIS_READ_PORT', 6379),
'database' => 2
'database' => 1
]
],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment