Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
杨树贤
/
ic_server_welfare
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
a08abd05
authored
Aug 24, 2019
by
杨树贤
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
添加流水的异步任务
parent
7f0c9062
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
87 additions
and
8 deletions
app/Http/Controllers/IntegralBillsController.php
app/Models/IntegralBill.php
app/Models/UserIntegral.php
app/Tasks/IntegralBillTask.php
config/database.php
app/Http/Controllers/IntegralBillsController.php
View file @
a08abd05
...
...
@@ -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
(),
...
...
app/Models/IntegralBill.php
View file @
a08abd05
...
...
@@ -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
,
];
}
...
...
app/Models/UserIntegral.php
View file @
a08abd05
...
...
@@ -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
app/Tasks/IntegralBillTask.php
0 → 100644
View file @
a08abd05
<?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
config/database.php
View file @
a08abd05
...
...
@@ -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
]
],
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment