Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
cloud-platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
youjj
cloud-platform
Commits
448a9ab0
Commit
448a9ab0
authored
Jun 24, 2019
by
hanfeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改获取区域接口
parent
3a3f4a03
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
120 additions
and
44 deletions
+120
-44
RegionType.java
...n/java/com/xxfc/platform/vehicle/constant/RegionType.java
+4
-3
SysRegionBiz.java
...main/java/com/xxfc/platform/vehicle/biz/SysRegionBiz.java
+59
-40
SysRegionController.java
...a/com/xxfc/platform/vehicle/rest/SysRegionController.java
+17
-0
RegionTest.java
...r/src/test/java/com/xxfc/platform/vehicle/RegionTest.java
+40
-1
No files found.
xx-vehicle/xx-vehicle-api/src/main/java/com/xxfc/platform/vehicle/constant/RegionType.java
View file @
448a9ab0
...
@@ -4,11 +4,12 @@ import java.util.HashMap;
...
@@ -4,11 +4,12 @@ import java.util.HashMap;
import
java.util.Map
;
import
java.util.Map
;
public
enum
RegionType
{
public
enum
RegionType
{
ALL
(-
1
,
"全国
"
),
//这一类型不存在db中,只是用于查询所有国家
COUNTRY
(-
1
,
"国家
"
),
//这一类型不存在db中,只是用于查询所有国家
COUNTRY
(
0
,
"国家
"
),
REGION
(
0
,
"地区
"
),
PROVINCE
(
1
,
"省/直辖市"
),
PROVINCE
(
1
,
"省/直辖市"
),
CITY
(
2
,
"市"
),
CITY
(
2
,
"市"
),
TOWN
(
3
,
"镇/县"
),
PREFECTUR
(
3
,
"县"
),
TOWN
(
4
,
"镇"
),
;
;
/**
/**
...
...
xx-vehicle/xx-vehicle-server/src/main/java/com/xxfc/platform/vehicle/biz/SysRegionBiz.java
View file @
448a9ab0
...
@@ -68,45 +68,53 @@ public class SysRegionBiz extends BaseBiz<SysRegionMapper, SysRegion> {
...
@@ -68,45 +68,53 @@ public class SysRegionBiz extends BaseBiz<SysRegionMapper, SysRegion> {
//获取相应地区类型对应在id的位数,作为对应parent查找其下地区的redis key组成部分
//获取相应地区类型对应在id的位数,作为对应parent查找其下地区的redis key组成部分
private
String
getPrefixOfAgencyId
(
Integer
type
,
Long
parentId
){
private
String
getPrefixOfAgencyId
(
Integer
type
,
Long
parentId
){
switch
(
type
){
// switch(type){
case
-
1
:
// case -1:
return
String
.
valueOf
(-
1
);
// return String.valueOf(0);
case
0
:
// case 0:
return
String
.
valueOf
(
0
);
// return String.valueOf(1);
case
1
:
// case 1:
return
String
.
valueOf
(
parentId
).
substring
(
0
,
2
);
// return String.valueOf(parentId);
case
2
:
// case 2:
return
String
.
valueOf
(
parentId
).
substring
(
0
,
4
);
// return String.valueOf(parentId).substring(0,2);
default
:
// case 3:
throw
new
CustomIllegalParamException
(
"not valid region"
);
// return String.valueOf(parentId).substring(0,4);
}
// case 4:
// return String.valueOf(parentId);
// default:
// throw new CustomIllegalParamException("not valid region");
// }
return
String
.
valueOf
(
parentId
);
}
}
//获取相应地区类型对应在id的位数,作为对应parent查找其下地区的redis key组成部分
//获取相应地区类型对应在id的位数,作为对应parent查找其下地区的redis key组成部分
private
String
getPrefixOfAgencyId
(
Long
parentId
){
//
private String getPrefixOfAgencyId(Long parentId){
Integer
type
=
getTypeFromId
(
parentId
);
//
Integer type = getTypeFromId(parentId);
return
getPrefixOfAgencyId
(
type
,
parentId
);
//
return getPrefixOfAgencyId(type,parentId);
}
//
}
/**
/**
* 根据id获得所属类型
* 根据id获得所属类型
* @param
i
d
* @param
parentI
d
* @return
* @return
*/
*/
private
Integer
getTypeFromId
(
Long
id
){
// private Integer getTypeFromId(Long parentId){
if
(
id
==
0
){
// if(parentId == 0) {//国家type-1
return
RegionType
.
ALL
.
getCode
();
// return RegionType.COUNTRY.getCode();
}
if
(
id
<
10
){
// }if (parentId<2){//地区type 0
return
RegionType
.
COUNTRY
.
getCode
();
// return RegionType.REGION.getCode();
}
else
if
(
id
%
100
>
0
){
//第1、2非零,即3级地区
// }if(parentId < 20){//省type 1
return
RegionType
.
TOWN
.
getCode
();
// return RegionType.PROVINCE.getCode();
}
else
if
(
id
%
10000
>
0
){
//第1、2为零,第3、4位非0,即2级地区
// }else if(parentId%100>0){//第1、2非零,即3级地区 镇一级type 4
return
RegionType
.
CITY
.
getCode
();
// return RegionType.TOWN.getCode();
}
else
{
//第1、2、3、4为零,第5、6位非0,即2级地区
// }else if(parentId%10000>0){//第1、2为零,第3、4位非0,即2级地区 县type 3
return
RegionType
.
PROVINCE
.
getCode
();
// return RegionType.PREFECTUR.getCode();
}
// }else{//第1、2、3、4为零,第5、6位非0,即2级地区 市type 2
}
// return RegionType.CITY.getCode();
// }
// }
/**
/**
* 获取对应redis中的hash的key
* 获取对应redis中的hash的key
...
@@ -115,7 +123,7 @@ public class SysRegionBiz extends BaseBiz<SysRegionMapper, SysRegion> {
...
@@ -115,7 +123,7 @@ public class SysRegionBiz extends BaseBiz<SysRegionMapper, SysRegion> {
*/
*/
private
String
getCacheRedisKey
(
Long
parentId
){
private
String
getCacheRedisKey
(
Long
parentId
){
//获取相应地区类型对应在agencyId的位数
//获取相应地区类型对应在agencyId的位数
return
RedisKey
.
SYS_REGION_SONS_CACHE_PREFIX
+
getPrefixOfAgencyId
(
parentId
)
;
return
RedisKey
.
SYS_REGION_SONS_CACHE_PREFIX
+
parentId
;
}
}
/**
/**
...
@@ -140,10 +148,10 @@ public class SysRegionBiz extends BaseBiz<SysRegionMapper, SysRegion> {
...
@@ -140,10 +148,10 @@ public class SysRegionBiz extends BaseBiz<SysRegionMapper, SysRegion> {
return
getSonRegionNoCache
(
id
);
return
getSonRegionNoCache
(
id
);
}
}
public
List
<
SysRegion
>
getSonRegionNoCache
(
Long
i
d
){
public
List
<
SysRegion
>
getSonRegionNoCache
(
Long
parentI
d
){
//从db中查询
//从db中查询
SysRegion
param
=
new
SysRegion
();
SysRegion
param
=
new
SysRegion
();
param
.
setParentId
(
i
d
);
param
.
setParentId
(
parentI
d
);
return
mapper
.
select
(
param
);
return
mapper
.
select
(
param
);
}
}
...
@@ -158,15 +166,15 @@ public class SysRegionBiz extends BaseBiz<SysRegionMapper, SysRegion> {
...
@@ -158,15 +166,15 @@ public class SysRegionBiz extends BaseBiz<SysRegionMapper, SysRegion> {
/**
/**
* 刷新其孩子节点到缓存,并对其孩子迭代执行此操作
* 刷新其孩子节点到缓存,并对其孩子迭代执行此操作
* @param
i
d
* @param
parentI
d
*/
*/
private
void
refreshCacheRegionAndSon
(
Long
i
d
){
private
void
refreshCacheRegionAndSon
(
Long
parentI
d
){
List
<
SysRegion
>
sonRegions
=
getSonRegionNoCache
(
i
d
);
List
<
SysRegion
>
sonRegions
=
getSonRegionNoCache
(
parentI
d
);
//把当前节点的子节点数据缓存
//把当前节点的子节点数据缓存
String
redisKey
=
getCacheRedisKey
(
i
d
);
String
redisKey
=
getCacheRedisKey
(
parentI
d
);
customRedisTemplate
.
opsForValue
().
set
(
redisKey
,
JSON
.
toJSON
(
sonRegions
).
toString
());
customRedisTemplate
.
opsForValue
().
set
(
redisKey
,
JSON
.
toJSON
(
sonRegions
).
toString
());
log
.
info
(
"完成地区【"
+
i
d
+
"】的子地区数据缓存"
);
log
.
info
(
"完成地区【"
+
parentI
d
+
"】的子地区数据缓存"
);
if
(
CollectionUtils
.
isEmpty
(
sonRegions
)
||
getTypeFromId
(
id
).
equals
(
RegionType
.
CITY
.
getCode
())){
if
(
CollectionUtils
.
isEmpty
(
sonRegions
)
||
sonRegions
.
get
(
0
).
getType
(
).
equals
(
RegionType
.
CITY
.
getCode
())){
return
;
return
;
}
}
for
(
SysRegion
sonRegion:
sonRegions
){
for
(
SysRegion
sonRegion:
sonRegions
){
...
@@ -180,7 +188,7 @@ public class SysRegionBiz extends BaseBiz<SysRegionMapper, SysRegion> {
...
@@ -180,7 +188,7 @@ public class SysRegionBiz extends BaseBiz<SysRegionMapper, SysRegion> {
* 5分钟内刷新数据到缓存
* 5分钟内刷新数据到缓存
*/
*/
@Scheduled
(
cron
=
"0 */5 * * * *"
)
//每5分钟刷新一次数据
@Scheduled
(
cron
=
"0 */5 * * * *"
)
//每5分钟刷新一次数据
public
void
refreshCache
(){
public
void
refreshCache
(){
String
redisLockKey
=
RedisKey
.
SYS_REGION_REFRESH_LOCK
+(
DateTime
.
now
().
getMinuteOfDay
()/
5
);
//同一日每5分钟只刷新一次
String
redisLockKey
=
RedisKey
.
SYS_REGION_REFRESH_LOCK
+(
DateTime
.
now
().
getMinuteOfDay
()/
5
);
//同一日每5分钟只刷新一次
Boolean
suc
=
customRedisTemplate
.
opsForValue
().
setIfAbsent
(
redisLockKey
,
String
.
valueOf
(
DateTime
.
now
().
getMillis
()));
Boolean
suc
=
customRedisTemplate
.
opsForValue
().
setIfAbsent
(
redisLockKey
,
String
.
valueOf
(
DateTime
.
now
().
getMillis
()));
...
@@ -203,4 +211,15 @@ public class SysRegionBiz extends BaseBiz<SysRegionMapper, SysRegion> {
...
@@ -203,4 +211,15 @@ public class SysRegionBiz extends BaseBiz<SysRegionMapper, SysRegion> {
.
build
());
.
build
());
return
selectByWeekendSql
;
return
selectByWeekendSql
;
}
}
/**
* 通过属性获取省
* @param type
* @return
*/
public
List
<
SysRegion
>
getSysRegionByType
(
Integer
type
){
SysRegion
sysRegion
=
new
SysRegion
();
sysRegion
.
setType
(
type
);
return
mapper
.
select
(
sysRegion
);
}
}
}
xx-vehicle/xx-vehicle-server/src/main/java/com/xxfc/platform/vehicle/rest/SysRegionController.java
View file @
448a9ab0
...
@@ -22,12 +22,29 @@ import java.util.List;
...
@@ -22,12 +22,29 @@ import java.util.List;
public
class
SysRegionController
extends
BaseController
<
SysRegionBiz
>
{
public
class
SysRegionController
extends
BaseController
<
SysRegionBiz
>
{
/**
* 父类id获取子类对象
* @param id
* @return
*/
@IgnoreUserToken
@IgnoreUserToken
@RequestMapping
(
value
=
"/sons/{id}"
,
method
=
RequestMethod
.
GET
)
@RequestMapping
(
value
=
"/sons/{id}"
,
method
=
RequestMethod
.
GET
)
public
RestResponse
<
List
<
SysRegion
>>
getSonRegion
(
@PathVariable
Long
id
){
public
RestResponse
<
List
<
SysRegion
>>
getSonRegion
(
@PathVariable
Long
id
){
return
RestResponse
.
data
(
baseBiz
.
getSonRegion
(
id
));
return
RestResponse
.
data
(
baseBiz
.
getSonRegion
(
id
));
}
}
/**
* 通过属性获取省
* @param type
* @return
*/
@IgnoreUserToken
@RequestMapping
(
value
=
"/getSonRegionByType/{type}"
,
method
=
RequestMethod
.
GET
)
public
RestResponse
<
List
<
SysRegion
>>
getSysRegionByType
(
@PathVariable
Integer
type
){
return
RestResponse
.
data
(
baseBiz
.
getSysRegionByType
(
type
));
}
@RequestMapping
(
value
=
""
,
method
=
RequestMethod
.
GET
)
@RequestMapping
(
value
=
""
,
method
=
RequestMethod
.
GET
)
public
RestResponse
<
List
<
SysRegion
>>
getRegion
(
@RequestParam
String
idListJson
){
public
RestResponse
<
List
<
SysRegion
>>
getRegion
(
@RequestParam
String
idListJson
){
try
{
try
{
...
...
xx-vehicle/xx-vehicle-server/src/test/java/com/xxfc/platform/vehicle/RegionTest.java
View file @
448a9ab0
...
@@ -8,21 +8,25 @@ import cn.hutool.core.io.file.FileWriter;
...
@@ -8,21 +8,25 @@ import cn.hutool.core.io.file.FileWriter;
import
cn.hutool.core.util.PinyinUtil
;
import
cn.hutool.core.util.PinyinUtil
;
import
cn.hutool.core.util.StrUtil
;
import
cn.hutool.core.util.StrUtil
;
import
cn.hutool.json.JSONUtil
;
import
cn.hutool.json.JSONUtil
;
import
com.ace.cache.annotation.Cache
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.github.stuxuhai.jpinyin.PinyinHelper
;
import
com.github.stuxuhai.jpinyin.PinyinHelper
;
import
com.github.wxiaoqi.security.common.msg.TableResultResponse
;
import
com.github.wxiaoqi.security.common.msg.TableResultResponse
;
import
com.github.wxiaoqi.security.common.util.Query
;
import
com.github.wxiaoqi.security.common.util.Query
;
import
com.xxfc.platform.vehicle.VehicleApplication
;
import
com.xxfc.platform.vehicle.VehicleApplication
;
import
com.xxfc.platform.vehicle.biz.SysRegionBiz
;
import
com.xxfc.platform.vehicle.biz.SysRegionBiz
;
import
com.xxfc.platform.vehicle.constant.RedisKey
;
import
com.xxfc.platform.vehicle.entity.SysRegion
;
import
com.xxfc.platform.vehicle.entity.SysRegion
;
import
lombok.Builder
;
import
lombok.Builder
;
import
lombok.Data
;
import
lombok.Data
;
import
lombok.Getter
;
import
lombok.Getter
;
import
lombok.Setter
;
import
lombok.Setter
;
import
org.joda.time.DateTime
;
import
org.junit.Test
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
...
@@ -32,10 +36,14 @@ import java.util.List;
...
@@ -32,10 +36,14 @@ import java.util.List;
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringBootTest
(
classes
={
VehicleApplication
.
class
})
@SpringBootTest
(
classes
={
VehicleApplication
.
class
})
public
class
RegionTest
{
public
class
RegionTest
{
@Autowired
private
RedisTemplate
customRedisTemplate
;
@Autowired
@Autowired
SysRegionBiz
sysRegionBiz
;
private
SysRegionBiz
sysRegionBiz
;
// public static final String TEST_REDIS_KEY ="redis:{1}";
private
final
String
TEST_REDIS_KEY
=
"redis:{1}"
;
@Data
@Data
public
class
Title
{
public
class
Title
{
String
title
;
String
title
;
...
@@ -122,4 +130,35 @@ public class RegionTest {
...
@@ -122,4 +130,35 @@ public class RegionTest {
writer
.
write
(
JSONUtil
.
toJsonStr
(
newCity
));
writer
.
write
(
JSONUtil
.
toJsonStr
(
newCity
));
System
.
out
.
println
(
"test"
);
System
.
out
.
println
(
"test"
);
}
}
@Test
public
void
test1
()
{
int
i
=
DateTime
.
now
().
getMinuteOfDay
()
/
5
;
System
.
out
.
println
(
"DateTime.now().getMinuteOfDay() / 5++++++++++++"
+
i
);
String
redisLockKey
=
RedisKey
.
SYS_REGION_REFRESH_LOCK
+
i
;
//同一日每5分钟只刷新一次
System
.
out
.
println
(
"redisLockKey++++++++++++++++"
+
redisLockKey
);
String
s
=
String
.
valueOf
(
DateTime
.
now
().
getMillis
());
System
.
out
.
println
(
"s++++++++++++++++++"
+
s
);
Boolean
suc
=
customRedisTemplate
.
opsForValue
().
setIfAbsent
(
redisLockKey
,
s
);
Boolean
test1
=
customRedisTemplate
.
opsForValue
().
setIfAbsent
(
"1"
,
"2"
);
Boolean
test2
=
customRedisTemplate
.
opsForValue
().
setIfAbsent
(
"1"
,
"3"
);
System
.
out
.
println
(
"test1///////////////////////////////"
+
test1
);
System
.
out
.
println
(
"test2///////////////////////////////"
+
test2
);
System
.
out
.
println
(
"suc++++++++++++++++++++"
+
suc
);
}
@Test
public
void
testRedis
()
{
SysRegion
sysRegion
=
getSysRegion
(
1
);
System
.
out
.
println
(
"sysRegion+++++++++"
+
sysRegion
);
System
.
out
.
println
(
"TEST_REDIS_KEY+++++++++++"
+
TEST_REDIS_KEY
);
}
@Cache
(
key
=
TEST_REDIS_KEY
)
private
SysRegion
getSysRegion
(
int
id
){
SysRegion
sysRegion
=
sysRegionBiz
.
selectById
(
2
);
return
sysRegion
;
}
}
}
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