Commit a5739cf1 by 杨树贤

完善并且修复添加流水的异步任务

parent 95e814ff
...@@ -38,7 +38,7 @@ class IntegralBillsController extends Controller ...@@ -38,7 +38,7 @@ class IntegralBillsController extends Controller
* @param IntegralBill $integralBill * @param IntegralBill $integralBill
* @return array * @return array
*/ */
public function create(Request $request, IntegralBill $integralBill) public function store(Request $request, IntegralBill $integralBill)
{ {
$data = [ $data = [
'user_id' => $request->user_id, 'user_id' => $request->user_id,
......
...@@ -6,6 +6,8 @@ namespace App\Models; ...@@ -6,6 +6,8 @@ namespace App\Models;
use App\Http\Filters\IntegralBillFilter; use App\Http\Filters\IntegralBillFilter;
use App\Http\Filters\QueryFilter; use App\Http\Filters\QueryFilter;
use App\Tasks\IntegralBillTask;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use http\Env\Request; use http\Env\Request;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
...@@ -47,6 +49,15 @@ class IntegralBill extends Model ...@@ -47,6 +49,15 @@ class IntegralBill extends Model
$res = DB::table('integral_bills')->insert($data); $res = DB::table('integral_bills')->insert($data);
return $res; return $res;
// public function addIntegralBill($userId,$integralId)
// {
// $task = new IntegralBillTask($data);
// $result = Task::deliver($task);
// if (!$result) {
// return false;
// }
// }
} }
//更新用户红包兑换列表 //更新用户红包兑换列表
......
...@@ -22,6 +22,13 @@ class UserIntegral extends Model ...@@ -22,6 +22,13 @@ class UserIntegral extends Model
return $filters->apply($query); return $filters->apply($query);
} }
public function addUserIntegral($data = [])
{
$result = DB::table('user_integrals')->insert($data);
return $result;
}
public function getUserIntegralList($page, $pageSize, $filter) public function getUserIntegralList($page, $pageSize, $filter)
{ {
$settings = UserIntegral::filter($filter) $settings = UserIntegral::filter($filter)
......
...@@ -40,31 +40,45 @@ class IntegralBillTask extends Task ...@@ -40,31 +40,45 @@ class IntegralBillTask extends Task
throw new \Exception("插入用户红包账单失败,用户id是$userId,兑换类型id是$integralId"); throw new \Exception("插入用户红包账单失败,用户id是$userId,兑换类型id是$integralId");
} }
//先判断user_integrals表里面存不存在这条记录,如果不存在的话,要初始化一条记录
$userIntegral = new UserIntegral(); $userIntegral = new UserIntegral();
$data = (array)$userIntegral->getUserIntegral($userId); $data = (array)$userIntegral->getUserIntegral($userId);
if ($data) { if (!$data) {
//将用户的累计可兑换金额写进redis里面的ic_user里面去 $data = [
$integral = new Integral(); 'user_id' => $userId,
$res = $integral->getIntegral($integralId); 'status' => 1,
$amount = $res->amount; 'add_time' => $this->data['add_time'],
];
$result = $userIntegral->addUserIntegral($data);
if (!$result) {
throw new \Exception("初始化用户红包详情失败,用户id是$userId");
}
}
//将用户的累计可兑换金额写进redis里面的ic_user里面去
$integral = new Integral();
$res = $integral->getIntegral($integralId);
if (!$res) {
throw new \Exception("获取红包信息失败,红包id是$integralId");
}
$amount = $res->amount;
$redis = new RedisModel(); $redis = new RedisModel();
$user = json_decode($redis->hget('ic_user', $userId), true); $user = json_decode($redis->hget('ic_user', $userId), true);
$user['integral'] = $data['integral'] + $amount; $data = (array)$userIntegral->getUserIntegral($userId);
$result = $redis->hset('ic_user', $userId, json_encode($user)); $user['integral'] = $data['integral'] + $amount;
//还要写进user_integrals数据库 $result = $redis->hset('ic_user', $userId, json_encode($user));
if ($result !== false) { //还要写进user_integrals数据库
$data = [ if ($result !== false) {
'integral' => $data['integral'] + $amount, $data = [
'update_time' => time(), 'integral' => $data['integral'] + $amount,
]; 'update_time' => time(),
$result = $userIntegral->updateUserIntegralByUserId($userId, $data); ];
if (!$result) { $result = $userIntegral->updateUserIntegralByUserId($userId, $data);
throw new \Exception("更新用户剩余积分失败,用户id是$userId,兑换类型id是$integralId"); if (!$result) {
} throw new \Exception("更新用户剩余积分失败,用户id是$userId,兑换类型id是$integralId");
} else {
throw new \Exception("更新redis里ic_user的integral失败,用户id是$userId,兑换类型id是$integralId");
} }
} else {
throw new \Exception("更新redis里ic_user的integral失败,用户id是$userId,兑换类型id是$integralId");
} }
}); });
} catch (\Exception $e) { } catch (\Exception $e) {
......
...@@ -19,7 +19,7 @@ $router->get('/key', function () { ...@@ -19,7 +19,7 @@ $router->get('/key', function () {
return str_random(32); return str_random(32);
}); });
//积分信息 //红包信息
$router->post('/integrals/list', 'IntegralsController@index'); $router->post('/integrals/list', 'IntegralsController@index');
$router->post('/integrals/info', 'IntegralsController@show'); $router->post('/integrals/info', 'IntegralsController@show');
$router->post('/integrals/add', 'IntegralsController@create'); $router->post('/integrals/add', 'IntegralsController@create');
...@@ -28,20 +28,13 @@ $router->post('/integrals/updateStatus', 'IntegralsController@updateStatus'); ...@@ -28,20 +28,13 @@ $router->post('/integrals/updateStatus', 'IntegralsController@updateStatus');
$router->post('/integrals/delete', 'IntegralsController@destroy'); $router->post('/integrals/delete', 'IntegralsController@destroy');
$router->post('/integrals/batchUpdateStatus', 'IntegralsController@batchUpdateStatus'); $router->post('/integrals/batchUpdateStatus', 'IntegralsController@batchUpdateStatus');
//用户红包流水
//积分类型
$router->post('/integral_types/list', 'IntegralTypesController@index');
$router->post('/integral_types/add', 'IntegralTypesController@create');
$router->post('/integral_types/update', 'IntegralTypesController@update');
$router->post('/integral_types/delete', 'IntegralTypesController@destroy');
//用户积分账单
$router->post('/integral_bills/list', 'IntegralBillsController@index'); $router->post('/integral_bills/list', 'IntegralBillsController@index');
$router->post('/integral_bills/add', 'IntegralBillsController@create'); $router->post('/integral_bills/add', 'IntegralBillsController@store');
$router->post('/integral_bills/update', 'IntegralBillsController@update'); $router->post('/integral_bills/update', 'IntegralBillsController@update');
$router->post('/integral_bills/delete', 'IntegralBillsController@destroy'); $router->post('/integral_bills/delete', 'IntegralBillsController@destroy');
//用户积分信息 //用户红包信息
$router->post('/user_integrals/info', 'UserIntegralsController@show'); $router->post('/user_integrals/info', 'UserIntegralsController@show');
$router->post('/user_integrals/list', 'UserIntegralsController@index'); $router->post('/user_integrals/list', 'UserIntegralsController@index');
$router->post('/user_integrals/updateStatus', 'UserIntegralsController@updateStatus'); $router->post('/user_integrals/updateStatus', 'UserIntegralsController@updateStatus');
...@@ -49,8 +42,6 @@ $router->post('/user_integrals/batchUpdateStatus', 'UserIntegralsController@batc ...@@ -49,8 +42,6 @@ $router->post('/user_integrals/batchUpdateStatus', 'UserIntegralsController@batc
$router->post('/user_integrals/updateExchangedIntegral', 'UserIntegralsController@updateExchangedIntegral'); $router->post('/user_integrals/updateExchangedIntegral', 'UserIntegralsController@updateExchangedIntegral');
$router->post('/user_integrals/statistics', 'UserIntegralsController@statistics'); $router->post('/user_integrals/statistics', 'UserIntegralsController@statistics');
//兑换配置 //兑换配置
$router->post('/exchange_settings/info', 'ExchangeSettingsController@show'); $router->post('/exchange_settings/info', 'ExchangeSettingsController@show');
$router->post('/exchange_settings/list', 'ExchangeSettingsController@index'); $router->post('/exchange_settings/list', 'ExchangeSettingsController@index');
...@@ -74,3 +65,4 @@ $router->post('/rob/exchange/quota', 'ExchangeController@create'); ...@@ -74,3 +65,4 @@ $router->post('/rob/exchange/quota', 'ExchangeController@create');
//用户签到 //用户签到
$router->post('/check_in/list','CheckInController@index'); $router->post('/check_in/list','CheckInController@index');
$router->post('/check_in/add','CheckInController@store'); $router->post('/check_in/add','CheckInController@store');
23914 478
\ No newline at end of file \ No newline at end of file
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