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
7f2b8d27
authored
Aug 24, 2019
by
杨树贤
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
完成商品兑换配置增删改查的redis缓存处理
parent
5aa8c0fb
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
147 additions
and
8 deletions
app/Http/Controllers/ExchangeSettingsController.php
app/Models/ExchangeSetting.php
common
app/Http/Controllers/ExchangeSettingsController.php
View file @
7f2b8d27
...
@@ -41,8 +41,11 @@ class ExchangeSettingsController extends Controller
...
@@ -41,8 +41,11 @@ class ExchangeSettingsController extends Controller
{
{
$page
=
$request
->
has
(
'page'
)
?
$request
->
page
:
self
::
DEFAULT_PAGE
;
$page
=
$request
->
has
(
'page'
)
?
$request
->
page
:
self
::
DEFAULT_PAGE
;
$pageSize
=
$request
->
has
(
'page_size'
)
?
$request
->
page_size
:
self
::
DEFAULT_PAGE_SIZE
;
$pageSize
=
$request
->
has
(
'page_size'
)
?
$request
->
page_size
:
self
::
DEFAULT_PAGE_SIZE
;
if
(
$request
->
has
(
'is_api'
)
&&
$request
->
is_api
)
{
$result
=
$exchangeSetting
->
getExchangeSettingListForApi
();
}
else
{
$result
=
$exchangeSetting
->
getExchangeSettingList
(
$page
,
$pageSize
,
$filter
);
$result
=
$exchangeSetting
->
getExchangeSettingList
(
$page
,
$pageSize
,
$filter
);
}
return
$this
->
Export
(
0
,
'ok'
,
$result
);
return
$this
->
Export
(
0
,
'ok'
,
$result
);
}
}
...
...
app/Models/ExchangeSetting.php
View file @
7f2b8d27
...
@@ -5,6 +5,7 @@ namespace App\Models;
...
@@ -5,6 +5,7 @@ namespace App\Models;
use
App\Http\Filters\QueryFilter
;
use
App\Http\Filters\QueryFilter
;
use
Common\Model\RedisModel
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Support\Facades\DB
;
use
Illuminate\Support\Facades\DB
;
...
@@ -33,20 +34,149 @@ class ExchangeSetting extends Model
...
@@ -33,20 +34,149 @@ class ExchangeSetting extends Model
return
[
'data'
=>
$settings
,
'count'
=>
$count
];
return
[
'data'
=>
$settings
,
'count'
=>
$count
];
}
}
public
function
getExchangeSettingListForApi
()
{
//先从redis里面查询是否有列表,没有的话从MySQL里面取出并且放入redis
$redis
=
new
RedisModel
();
$settings
=
$this
->
getExchangeSettingsFromRedis
();
$count
=
0
;
if
(
!
$settings
)
{
$result
=
$this
->
addExchangeSettingListToRedis
();
if
(
!
$result
)
{
ErrorLog
(
ErrorCode
(
19
,
5
),
'写入商品配置列表到Redis失败'
);
$count
=
ExchangeSetting
::
where
(
'status'
,
1
)
->
count
();
$settings
=
$this
->
getExchangeSettingsFromRedis
();
}
}
else
{
$count
=
$redis
->
hlen
(
'ic_welfare_integrals'
);
}
$settings
=
arraySequence
(
$settings
,
'id'
,
'SORT_ASC'
);
return
[
'data'
=>
array_values
(
$settings
),
'count'
=>
$count
];
}
//从redis查询出商品配置项
public
function
getExchangeSettingsFromRedis
()
{
$redis
=
new
RedisModel
();
$settings
=
$redis
->
hgetall
(
'ic_exchange_settings'
);
$settings
=
array_map
(
function
(
$value
)
{
return
json_decode
(
$value
,
true
);
},
$settings
);
return
$settings
;
}
//添加商品配置项到Redis
public
function
addExchangeSettingListToRedis
()
{
$settings
=
ExchangeSetting
::
where
(
'status'
,
1
)
->
get
()
->
toArray
();
$redis
=
new
RedisModel
();
//转换数据存储到Redis的哈希类型
$data
=
[];
foreach
(
$settings
as
$key
=>
$setting
)
{
$map
=
[
'id'
=>
$setting
[
'id'
],
'name'
=>
$setting
[
'name'
],
'amount'
=>
$setting
[
'amount'
],
'type'
=>
$setting
[
'type'
],
'status'
=>
$setting
[
'status'
],
];
$data
[
$setting
[
'id'
]]
=
json_encode
(
$map
);
}
//这里如果没有数据放到缓存了,直接删除key
if
(
$data
)
{
$redis
->
hmset
(
'ic_exchange_settings'
,
$data
);
}
else
{
$redis
->
del
(
'ic_exchange_settings'
);
}
}
public
function
addExchangeSetting
(
$data
=
[])
public
function
addExchangeSetting
(
$data
=
[])
{
{
$result
=
DB
::
table
(
'exchange_settings'
)
->
insert
(
$data
);
$result
=
DB
::
transaction
(
function
()
use
(
$data
)
{
$id
=
DB
::
table
(
'exchange_settings'
)
->
insertGetId
(
$data
);
$data
[
'id'
]
=
$id
;
if
(
!
$id
)
{
return
false
;
}
$this
->
addIntegralToRedis
(
$id
,
$data
);
return
true
;
},
5
);
return
$result
;
return
$result
;
}
}
//添加单个商品兑换配置到redis
public
function
addIntegralToRedis
(
$id
,
$setting
)
{
$redis
=
new
RedisModel
();
$data
=
[
'id'
=>
$setting
[
'id'
],
'name'
=>
$setting
[
'name'
],
'amount'
=>
$setting
[
'amount'
],
'type'
=>
$setting
[
'type'
],
'status'
=>
$setting
[
'status'
],
];
//添加的时候,状态是可用的才会添加到redis
if
(
$setting
[
'status'
])
{
$redis
->
hset
(
'ic_exchange_settings'
,
$id
,
json_encode
(
$data
));
}
}
public
function
updateExchangeSetting
(
$id
,
$data
=
[])
public
function
updateExchangeSetting
(
$id
,
$data
=
[])
{
{
$result
=
DB
::
table
(
'exchange_settings'
)
->
where
(
'id'
,
$id
)
->
update
(
$data
);
$result
=
DB
::
transaction
(
function
()
use
(
$id
,
$data
)
{
$result
=
DB
::
table
(
'exchange_settings'
)
->
where
(
'id'
,
$id
)
->
update
(
$data
);
if
(
!
$result
)
{
return
false
;
}
if
(
$result
)
{
$this
->
changeExchangeSettingFromRedis
([
$id
]);
}
return
true
;
},
5
);
return
$result
;
return
$result
;
}
}
//因为修改状态会涉及到不少信息的更变,所以直接重新更新有变化的数据(根据ids)
public
function
changeExchangeSettingFromRedis
(
$ids
=
[])
{
if
(
$ids
)
{
$changedSettings
=
DB
::
table
(
'exchange_settings'
)
->
whereIn
(
'id'
,
$ids
)
->
get
()
->
toArray
();
$needDelete
=
$needUpdate
=
[];
foreach
(
$changedSettings
as
$key
=>
$value
)
{
//判断是不是禁用,如果是禁用,就从redis里面删除
if
(
!
$value
->
status
)
{
$needDelete
[]
=
$value
->
id
;
}
else
{
$needUpdate
[
$value
->
id
]
=
json_encode
(
$value
);
}
}
$redis
=
new
RedisModel
();
if
(
$needDelete
)
{
$redis
->
hdel
(
'ic_exchange_settings'
,
$needDelete
);
}
if
(
$needUpdate
)
{
$redis
->
hmset
(
'ic_exchange_settings'
,
$needUpdate
);
}
}
return
true
;
}
public
function
deleteExchangeSetting
(
$ids
=
[])
public
function
deleteExchangeSetting
(
$ids
=
[])
{
{
$res
=
DB
::
table
(
'exchange_settings'
)
->
whereIn
(
'id'
,
$ids
)
->
delete
();
$res
=
DB
::
table
(
'exchange_settings'
)
->
whereIn
(
'id'
,
$ids
)
->
delete
();
...
@@ -63,11 +193,17 @@ class ExchangeSetting extends Model
...
@@ -63,11 +193,17 @@ class ExchangeSetting extends Model
*/
*/
public
function
batchUpdateStatus
(
$status
,
$ids
=
[])
public
function
batchUpdateStatus
(
$status
,
$ids
=
[])
{
{
$res
=
DB
::
table
(
'exchange_settings'
)
$result
=
DB
::
transaction
(
function
()
use
(
$ids
,
$status
)
{
->
whereIn
(
'id'
,
$ids
)
$result
=
DB
::
table
(
'exchange_settings'
)
->
whereIn
(
'id'
,
$ids
)
->
update
([
'status'
=>
$status
]);
->
update
([
'status'
=>
(
int
)
$status
,
'update_time'
=>
time
()]);
if
(
!
$result
)
{
return
false
;
}
$this
->
changeExchangeSettingFromRedis
(
$ids
);
return
$res
;
return
true
;
},
5
);
return
$result
;
}
}
...
...
common
@
6a492b76
Subproject commit
f196a4b6f17e81c0e6df02710896f08973e89391
Subproject commit
6a492b76f3938205ed9d94cd12b81d66feea657d
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