Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
browser-backend
Project
Project
Details
Activity
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
Administrator
browser-backend
Commits
284e3a45
Commit
284e3a45
authored
Jun 24, 2020
by
xuxin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
部分代码添加注释与调优
parent
4d35f9a8
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
213 additions
and
155 deletions
+213
-155
AccountServiceImpl.java
...owserbackend/account/service/impl/AccountServiceImpl.java
+2
-4
AdministratorServiceImpl.java
...ackend/account/service/impl/AdministratorServiceImpl.java
+0
-4
PaymentServiceImpl.java
...owserbackend/account/service/impl/PaymentServiceImpl.java
+3
-3
BrowserErrorCode.java
...ec/browserbackend/browser/ErrorCode/BrowserErrorCode.java
+1
-0
GroupController.java
...ec/browserbackend/browser/controller/GroupController.java
+23
-24
ShopController.java
...gec/browserbackend/browser/controller/ShopController.java
+12
-0
GroupServiceImpl.java
...browserbackend/browser/service/Impl/GroupServiceImpl.java
+20
-19
IpResourceServiceImpl.java
...erbackend/browser/service/Impl/IpResourceServiceImpl.java
+49
-35
ShopServiceImpl.java
.../browserbackend/browser/service/Impl/ShopServiceImpl.java
+103
-66
No files found.
src/main/java/com/edgec/browserbackend/account/service/impl/AccountServiceImpl.java
View file @
284e3a45
...
...
@@ -575,7 +575,7 @@ public class AccountServiceImpl implements AccountService {
if
(
level
==
-
1
)
{
//find its parent.
String
parent
=
accountRepository
.
findByName
(
name
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
)
).
getParent
();
String
parent
=
accountRepository
.
findByName
(
name
).
get
(
).
getParent
();
if
(
StringUtils
.
isEmpty
(
parent
))
return
Arrays
.
asList
();
...
...
@@ -704,8 +704,6 @@ public class AccountServiceImpl implements AccountService {
@Override
public
void
createSubUsers
(
String
name
,
SubUsersRequestDto
subUsersRequestDto
)
{
Account
existing
=
accountRepository
.
findByName
(
name
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
existing
==
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
,
"account does not exist: "
+
name
);
if
(!
existing
.
isAllowedToCreateSubUser
()
&&
!
StringUtils
.
isEmpty
(
existing
.
getParent
()))
throw
new
ClientRequestException
(
AccountErrorCode
.
NOTALLOWEDTOCREATESUBUSER
,
"Not allowed to create sub user"
);
...
...
@@ -726,7 +724,7 @@ public class AccountServiceImpl implements AccountService {
else
user
.
setName
(
existing
.
getName
()
+
i
);
Account
child
=
accountRepository
.
findByName
(
user
.
getName
()).
orElse
Throw
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
)
);
Account
child
=
accountRepository
.
findByName
(
user
.
getName
()).
orElse
(
null
);
if
(
child
!=
null
)
{
time
++;
continue
;
...
...
src/main/java/com/edgec/browserbackend/account/service/impl/AdministratorServiceImpl.java
View file @
284e3a45
...
...
@@ -366,8 +366,6 @@ public class AdministratorServiceImpl implements AdministratorService {
@Override
public
void
addPromotionCode
(
String
username
,
String
promotionCode
)
{
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
==
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
account
.
setPromotionCode
(
promotionCode
);
accountRepository
.
save
(
account
);
}
...
...
@@ -497,8 +495,6 @@ public class AdministratorServiceImpl implements AdministratorService {
@Override
public
void
addUserWhiteList
(
String
username
,
String
website
)
{
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
==
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
account
.
getWhiteList
().
add
(
website
);
accountRepository
.
save
(
account
);
}
...
...
src/main/java/com/edgec/browserbackend/account/service/impl/PaymentServiceImpl.java
View file @
284e3a45
...
...
@@ -171,7 +171,7 @@ public class PaymentServiceImpl implements PaymentService {
if
(
chargeType
==
0
)
{
UserPrePaidBilling
bill
=
new
UserPrePaidBilling
();
Account
account
=
accountRepository
.
findByName
(
byTradeNo
.
getUsername
()).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
)
);
Account
account
=
accountRepository
.
findByName
(
byTradeNo
.
getUsername
()).
get
(
);
if
(
account
!=
null
&&
account
.
getParent
()
!=
null
)
bill
.
setAdministrator
(
account
.
getParent
());
else
...
...
@@ -303,7 +303,7 @@ public class PaymentServiceImpl implements PaymentService {
if
(
chargeType
==
0
)
{
UserPrePaidBilling
bill
=
new
UserPrePaidBilling
();
Account
account
=
accountRepository
.
findByName
(
byTradeNo
.
getUsername
()).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
)
);
Account
account
=
accountRepository
.
findByName
(
byTradeNo
.
getUsername
()).
get
(
);
if
(
account
!=
null
&&
account
.
getParent
()
!=
null
)
bill
.
setAdministrator
(
account
.
getParent
());
else
...
...
@@ -478,7 +478,7 @@ public class PaymentServiceImpl implements PaymentService {
UserBalance
userBalance
=
userBalanceRepository
.
findById
(
username
).
orElse
(
null
);
UserPrePaidBilling
bill
=
new
UserPrePaidBilling
();
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
)
);
Account
account
=
accountRepository
.
findByName
(
username
).
get
(
);
if
(
account
!=
null
&&
account
.
getParent
()
!=
null
)
bill
.
setAdministrator
(
account
.
getParent
());
else
...
...
src/main/java/com/edgec/browserbackend/browser/ErrorCode/BrowserErrorCode.java
View file @
284e3a45
...
...
@@ -14,6 +14,7 @@ public enum BrowserErrorCode implements ErrorCode {
IPNOTEXIST
(
BROWSER_BASE
+
201
,
"The ip do not exist"
),
IPNOTBINDTOSHOP
(
BROWSER_BASE
+
202
,
"The ip does not bind this shop."
),
USER_NOT_BIND_SHOP
(
BROWSER_BASE
+
203
,
"The shop does not bind this user."
),
GROUPNOTEXIST
(
BROWSER_BASE
+
301
,
"The group does not exist"
),
...
...
src/main/java/com/edgec/browserbackend/browser/controller/GroupController.java
View file @
284e3a45
...
...
@@ -22,6 +22,9 @@ public class GroupController {
@Autowired
private
GroupService
groupService
;
/**
* 添加分组
*/
@RequestMapping
(
"/add"
)
public
ResultDto
addGroup
(
Principal
principal
,
@RequestBody
GroupDto
groupDto
)
{
ResultDto
resultDto
=
new
ResultDto
();
...
...
@@ -30,12 +33,8 @@ public class GroupController {
groupDto1
.
setId
(
groupService
.
addGroup
(
principal
.
getName
(),
groupDto
.
getName
()));
resultDto
.
setData
(
groupDto1
);
resultDto
.
setStatus
(
0
);
}
catch
(
ClientRequestException
e
)
{
resultDto
.
setStatus
(-
1
);
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
statusInfo
.
put
(
"code"
,
e
.
getErrorCode
());
statusInfo
.
put
(
"message"
,
e
.
getMessage
());
resultDto
.
setStatusInfo
(
statusInfo
);
}
catch
(
ClientRequestException
e
)
{
dealClientRequestException
(
resultDto
,
e
);
}
return
resultDto
;
}
...
...
@@ -46,12 +45,8 @@ public class GroupController {
try
{
groupService
.
updateGroup
(
principal
.
getName
(),
group
);
resultDto
.
setStatus
(
0
);
}
catch
(
ClientRequestException
e
)
{
resultDto
.
setStatus
(-
1
);
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
statusInfo
.
put
(
"code"
,
e
.
getErrorCode
());
statusInfo
.
put
(
"message"
,
e
.
getMessage
());
resultDto
.
setStatusInfo
(
statusInfo
);
}
catch
(
ClientRequestException
e
)
{
dealClientRequestException
(
resultDto
,
e
);
}
return
resultDto
;
}
...
...
@@ -62,16 +57,15 @@ public class GroupController {
try
{
groupService
.
deleteGroup
(
principal
.
getName
(),
groupDto
.
getId
());
resultDto
.
setStatus
(
0
);
}
catch
(
ClientRequestException
e
)
{
resultDto
.
setStatus
(-
1
);
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
statusInfo
.
put
(
"code"
,
e
.
getErrorCode
());
statusInfo
.
put
(
"message"
,
e
.
getMessage
());
resultDto
.
setStatusInfo
(
statusInfo
);
}
catch
(
ClientRequestException
e
)
{
dealClientRequestException
(
resultDto
,
e
);
}
return
resultDto
;
}
/**
* 分组列表
*/
@RequestMapping
(
"/list"
)
public
ResultDto
getGroupList
(
Principal
principal
)
{
ResultDto
resultDto
=
new
ResultDto
();
...
...
@@ -79,14 +73,19 @@ public class GroupController {
List
<
GroupDto
>
groupDtos
=
groupService
.
getGroupList
(
principal
.
getName
());
resultDto
.
setData
(
groupDtos
);
resultDto
.
setStatus
(
0
);
}
catch
(
ClientRequestException
e
)
{
resultDto
.
setStatus
(-
1
);
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
statusInfo
.
put
(
"code"
,
e
.
getErrorCode
());
statusInfo
.
put
(
"message"
,
e
.
getMessage
());
resultDto
.
setStatusInfo
(
statusInfo
);
}
catch
(
ClientRequestException
e
)
{
dealClientRequestException
(
resultDto
,
e
);
}
return
resultDto
;
}
private
void
dealClientRequestException
(
ResultDto
resultDto
,
ClientRequestException
e
)
{
resultDto
.
setStatus
(-
1
);
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
statusInfo
.
put
(
"code"
,
e
.
getErrorCode
());
statusInfo
.
put
(
"message"
,
e
.
getMessage
());
resultDto
.
setStatusInfo
(
statusInfo
);
}
}
src/main/java/com/edgec/browserbackend/browser/controller/ShopController.java
View file @
284e3a45
...
...
@@ -138,6 +138,9 @@ public class ShopController {
return
resultDto
;
}
/**
* 店铺分配
*/
@RequestMapping
(
value
=
"/assign"
,
method
=
RequestMethod
.
POST
)
public
ResultDto
assignShop
(
Principal
principal
,
@RequestBody
ShopRequestDto
shopRequestDto
)
{
ResultDto
resultDto
=
new
ResultDto
();
...
...
@@ -150,6 +153,9 @@ public class ShopController {
return
resultDto
;
}
/**
* 获取带有 ip 资源信息的 shop
*/
@RequestMapping
(
value
=
"/list"
,
method
=
RequestMethod
.
POST
)
public
ResultDto
getShopList
(
Principal
principal
,
@RequestBody
ShopRequestDto
shopRequestDto
)
{
logger
.
info
(
"shop list params {}"
,
JSONObject
.
toJSONString
(
shopRequestDto
));
...
...
@@ -165,6 +171,9 @@ public class ShopController {
return
resultDto
;
}
/**
* 获取当前登录用户已经绑定了当前商铺的 所有子用户名
*/
@RequestMapping
(
value
=
"/subusers"
,
method
=
RequestMethod
.
POST
)
public
ResultDto
getShopSubUsers
(
Principal
principal
,
@RequestBody
ShopRequestDto
shopRequestDto
)
{
ResultDto
resultDto
=
new
ResultDto
();
...
...
@@ -191,6 +200,9 @@ public class ShopController {
return
resultDto
;
}
/**
* 查询店铺信息
*/
@RequestMapping
(
value
=
"/query"
,
method
=
RequestMethod
.
POST
)
public
ResultDto
queryShop
(
Principal
principal
,
@RequestBody
ShopRequestDto
shopRequestDto
)
{
ResultDto
resultDto
=
new
ResultDto
();
...
...
src/main/java/com/edgec/browserbackend/browser/service/Impl/GroupServiceImpl.java
View file @
284e3a45
...
...
@@ -35,20 +35,26 @@ public class GroupServiceImpl implements GroupService {
@Override
public
String
addGroup
(
String
username
,
String
groupName
)
{
if
(
StringUtils
.
isBlank
(
username
)
||
StringUtils
.
isBlank
(
groupName
))
// 1. 校验传参
if
(
StringUtils
.
isBlank
(
username
)
||
StringUtils
.
isBlank
(
groupName
))
{
throw
new
ClientRequestException
(
BrowserErrorCode
.
INFORMATIONNOTCOMPELETE
);
}
// 2. 获取account信息并校验account
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
==
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
if
(
account
.
getGroupCount
()
>=
100
)
if
(
account
.
getGroupCount
()
>=
100
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
GROUPMAX
);
}
Group
group
=
new
Group
();
group
.
setOwner
(
username
);
group
.
setName
(
groupName
);
String
id
;
try
{
// 3. 保存 group信息
Group
group1
=
groupRepository
.
save
(
group
);
//可以优化
// 4. 更新 account 的 分组数
account
.
setGroupCount
(
account
.
getGroupCount
()
+
1
);
accountRepository
.
save
(
account
);
id
=
group1
.
getId
();
...
...
@@ -80,8 +86,6 @@ public class GroupServiceImpl implements GroupService {
if
(
StringUtils
.
isBlank
(
username
)
||
StringUtils
.
isBlank
(
groupId
))
throw
new
ClientRequestException
(
BrowserErrorCode
.
INFORMATIONNOTCOMPELETE
);
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
==
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
try
{
userShopRepository
.
updateGroupId
(
groupId
,
null
);
groupRepository
.
deleteById
(
groupId
);
...
...
@@ -96,20 +100,17 @@ public class GroupServiceImpl implements GroupService {
@Override
public
List
<
GroupDto
>
getGroupList
(
String
username
)
{
if
(
StringUtils
.
isBlank
(
username
))
if
(
StringUtils
.
isBlank
(
username
))
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
List
<
Group
>
groups
=
groupRepository
.
findByOwner
(
username
);
if
(
groups
==
null
)
return
new
ArrayList
<>();
List
<
GroupDto
>
groupDtos
=
new
ArrayList
<>();
try
{
groups
.
stream
().
forEach
(
x
->
{
groupDtos
.
add
(
new
GroupDto
(
x
.
getId
(),
x
.
getName
(),
x
.
getDetails
()));
});
}
catch
(
Exception
e
)
{
logger
.
error
(
"fail to delete group"
,
e
.
getMessage
());
throw
new
ClientRequestException
(
BrowserErrorCode
.
UNKNOWN
);
}
List
<
GroupDto
>
groupDtos
=
new
ArrayList
<>();
List
<
Group
>
groups
=
groupRepository
.
findByOwner
(
username
);
groups
.
forEach
(
x
->
groupDtos
.
add
(
new
GroupDto
(
x
.
getId
(),
x
.
getName
(),
x
.
getDetails
()))
);
return
groupDtos
;
}
}
src/main/java/com/edgec/browserbackend/browser/service/Impl/IpResourceServiceImpl.java
View file @
284e3a45
...
...
@@ -157,24 +157,27 @@ public class IpResourceServiceImpl implements IpResourceService {
@Override
public
List
<
String
>
buyIp
(
String
username
,
IpResourceRequestDto
ipResourceRequestDto
)
throws
Exception
{
if
(
ipResourceRequestDto
.
getRegion
()
==
null
)
{
throw
new
ClientRequestException
(
BrowserErrorCode
.
INFORMATIONNOTCOMPELETE
);
}
if
(
ipResourceRequestDto
.
getRegionCn
()
==
null
)
{
throw
new
ClientRequestException
(
BrowserErrorCode
.
INFORMATIONNOTCOMPELETE
);
}
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
==
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
if
(
account
.
getPermission
()
<
4
)
if
(
account
.
getPermission
()
<
4
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
}
Map
<
String
,
List
<
String
>>
priceList
=
ipOptionsRepository
.
findAll
().
get
(
0
).
getIpPlatForm
();
if
(
ipResourceRequestDto
.
getRegion
()
==
null
)
throw
new
ClientRequestException
(
BrowserErrorCode
.
INFORMATIONNOTCOMPELETE
);
if
(
ipResourceRequestDto
.
getRegionCn
()
==
null
)
throw
new
ClientRequestException
(
BrowserErrorCode
.
INFORMATIONNOTCOMPELETE
);
double
newprice
=
0
;
if
(!
ipResourceRequestDto
.
getVendor
().
equals
(
"own"
))
{
if
(!
"own"
.
equals
(
ipResourceRequestDto
.
getVendor
()))
{
Map
<
String
,
List
<
String
>>
priceList
=
ipOptionsRepository
.
findAll
().
get
(
0
).
getIpPlatForm
();
List
<
String
>
vendorPrices
=
priceList
.
get
(
ipResourceRequestDto
.
getRegionCn
());
String
price
=
vendorPrices
.
stream
()
.
filter
(
x
->
Vendor
.
valueOf
(
ipResourceRequestDto
.
getVendor
()).
getValue
().
equals
(
x
.
substring
(
0
,
x
.
indexOf
(
"-"
))))
.
map
(
x
->
x
.
substring
(
x
.
lastIndexOf
(
"-"
)
+
1
)).
collect
(
Collectors
.
joining
());
newprice
=
ipResourceRequestDto
.
getUnit
().
equals
(
"week"
)
?
(
Integer
.
valueOf
(
price
)
/
3
)
:
Integer
.
valueOf
(
price
);
.
map
(
x
->
x
.
substring
(
x
.
lastIndexOf
(
"-"
)
+
1
))
.
collect
(
Collectors
.
joining
());
newprice
=
"week"
.
equals
(
ipResourceRequestDto
.
getUnit
())
?
(
Integer
.
valueOf
(
price
)
/
3
)
:
Integer
.
valueOf
(
price
);
}
IpChargeResultDto
ipChargeResultDto
=
accountService
.
preChargeByMoney
(
username
,
newprice
*
ipResourceRequestDto
.
getAmount
()
*
ipResourceRequestDto
.
getPeriod
());
...
...
@@ -182,12 +185,7 @@ public class IpResourceServiceImpl implements IpResourceService {
throw
new
ClientRequestException
(
AccountErrorCode
.
NOTENOUGHBALANCE
);
}
String
password
;
if
(
StringUtils
.
isNotBlank
(
ipResourceRequestDto
.
getPassword
()))
password
=
ipResourceRequestDto
.
getPassword
();
else
password
=
genRandom
(
3
,
12
);
String
password
=
StringUtils
.
isNotBlank
(
ipResourceRequestDto
.
getPassword
())
?
ipResourceRequestDto
.
getPassword
()
:
genRandom
(
3
,
12
);
List
<
IpResourceDto
>
ipResourceDtos
=
new
ArrayList
<>();
List
<
String
>
ipIds
=
new
ArrayList
<>();
...
...
@@ -195,10 +193,11 @@ public class IpResourceServiceImpl implements IpResourceService {
IpResource
ipResource
=
new
IpResource
();
ipResource
.
setPeriod
(
ipResourceRequestDto
.
getPeriod
());
//充6送1
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"month"
)
&&
ipResourceRequestDto
.
getPeriod
()
==
6
)
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"month"
)
&&
ipResourceRequestDto
.
getPeriod
()
==
6
)
{
ipResource
.
setPeriod
(
7
);
else
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"month"
)
&&
ipResourceRequestDto
.
getPeriod
()
==
12
)
}
else
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"month"
)
&&
ipResourceRequestDto
.
getPeriod
()
==
12
)
{
ipResource
.
setPeriod
(
14
);
}
if
(
ipResourceRequestDto
.
getVendor
().
equals
(
"local"
))
{
ipResource
.
setAddr
(
"本地Ip未使用"
);
...
...
@@ -207,16 +206,23 @@ public class IpResourceServiceImpl implements IpResourceService {
ipResource
.
setVendorCn
(
"本地"
);
ipResource
.
setStatus
(
4
);
ipResource
.
setUsername
(
USERNAME
);
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"week"
))
ipResource
.
setValidTime
(
Instant
.
now
().
atZone
(
ZoneOffset
.
UTC
).
plusWeeks
(
ipResource
.
getPeriod
()).
toInstant
().
toEpochMilli
());
else
ipResource
.
setValidTime
(
Instant
.
now
().
atZone
(
ZoneOffset
.
UTC
).
plusMonths
(
ipResource
.
getPeriod
()).
toInstant
().
toEpochMilli
());
long
validTime
=
0
;
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"week"
))
{
validTime
=
Instant
.
now
().
atZone
(
ZoneOffset
.
UTC
).
plusWeeks
(
ipResource
.
getPeriod
()).
toInstant
().
toEpochMilli
();
}
else
{
validTime
=
Instant
.
now
().
atZone
(
ZoneOffset
.
UTC
).
plusMonths
(
ipResource
.
getPeriod
()).
toInstant
().
toEpochMilli
();
}
ipResource
.
setValidTime
(
validTime
);
ipResource
.
setPort
(
port
);
}
else
if
(
ipResourceRequestDto
.
getVendor
().
equals
(
"own"
))
{
if
(
ipResourceRequestDto
.
getAddr
()
==
null
||
ipResourceRequestDto
.
getAddr
().
size
()
==
0
)
if
(
ipResourceRequestDto
.
getAddr
()
==
null
||
ipResourceRequestDto
.
getAddr
().
isEmpty
())
{
throw
new
ClientRequestException
(
BrowserErrorCode
.
INFORMATIONNOTCOMPELETE
);
if
(
StringUtils
.
isBlank
(
ipResourceRequestDto
.
getAddr
().
get
(
i
)))
}
if
(
StringUtils
.
isBlank
(
ipResourceRequestDto
.
getAddr
().
get
(
i
)))
{
throw
new
ClientRequestException
(
BrowserErrorCode
.
INFORMATIONNOTCOMPELETE
);
}
ipResource
.
setSpecialLine
(
ipResourceRequestDto
.
isSpecialLine
());
ipResource
.
setAddr
(
ipResourceRequestDto
.
getAddr
().
get
(
i
));
ipResource
.
setIpType
(
IpType
.
OWN
);
...
...
@@ -240,26 +246,37 @@ public class IpResourceServiceImpl implements IpResourceService {
case
"aws"
:
ipResource
.
setVendorCn
(
"亚马逊云"
);
break
;
default
:
break
;
}
ipResource
.
setStatus
(
6
);
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"week"
))
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"week"
))
{
ipResource
.
setValidTime
(
Instant
.
now
().
atZone
(
ZoneOffset
.
UTC
).
plusWeeks
(
ipResource
.
getPeriod
()).
toInstant
().
toEpochMilli
());
else
}
else
{
ipResource
.
setValidTime
(
Instant
.
now
().
atZone
(
ZoneOffset
.
UTC
).
plusMonths
(
ipResource
.
getPeriod
()).
toInstant
().
toEpochMilli
());
}
ipResource
.
setUsername
(
USERNAME
);
ipResource
.
setPort
(
port
);
}
ipResource
.
setPurchasedTime
(
Instant
.
now
().
toEpochMilli
());
if
(
account
.
getParent
()
!=
null
)
if
(
account
.
getParent
()
!=
null
)
{
ipResource
.
setUserParent
(
account
.
getParent
());
}
ipResource
.
setRegion
(
ipResourceRequestDto
.
getRegion
());
if
((
StringUtils
.
isNotBlank
(
ipResource
.
getRegion
())
&&
region
.
contains
(
ipResource
.
getRegion
()))
||
(
ipResourceRequestDto
.
getVendor
().
equals
(
"own"
)
&&
ipResource
.
isSpecialLine
()))
{
boolean
condition1
=
StringUtils
.
isNotBlank
(
ipResource
.
getRegion
())
&&
region
.
contains
(
ipResource
.
getRegion
());
boolean
condition2
=
ipResourceRequestDto
.
getVendor
().
equals
(
"own"
)
&&
ipResource
.
isSpecialLine
();
if
(
condition1
||
condition2
)
{
ipResource
.
setProxyUsername
(
ipResource
.
getAddr
());
ipResource
.
setProxyPassword
(
genRandom
(
3
,
12
));
ipResource
.
setSpecialLine
(
true
);
ipResource
.
setHealthLockTimestamp
(
Instant
.
now
().
minusSeconds
(
60
*
20
).
toEpochMilli
());
}
ipResource
.
setRegionCn
(
ipResourceRequestDto
.
getRegionCn
());
ipResource
.
setProtocol
(
protocol
);
ipResource
.
setPassword
(
password
);
...
...
@@ -268,8 +285,11 @@ public class IpResourceServiceImpl implements IpResourceService {
ipResource
.
setLockTimestamp
(
Instant
.
now
().
toEpochMilli
());
ipResource
.
setUnit
(
ipResourceRequestDto
.
getUnit
());
ipResource
.
setPrice
(
newprice
);
IpResource
ipResource1
=
ipResourceRepository
.
save
(
ipResource
);
ipResourceDtos
.
add
(
new
IpResourceDto
(
ipResource1
,
null
,
false
));
ipIds
.
add
(
ipResource1
.
getId
());
}
...
...
@@ -292,8 +312,6 @@ public class IpResourceServiceImpl implements IpResourceService {
public
IpOperationResultDto
renewIp
(
String
username
,
IpResourceRequestDto
ipResourceRequestDto
)
{
String
URL
=
(
profiles
.
equals
(
"dev"
)
||
profiles
.
equals
(
"staging"
))
?
TESTURL
:
CLOUDAMURL
;
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
==
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
List
<
String
>
failedList
=
ipResourceRequestDto
.
getAddr
();
...
...
@@ -685,8 +703,6 @@ public class IpResourceServiceImpl implements IpResourceService {
@Override
public
boolean
queryIpExist
(
String
username
,
IpResourceUpdateDto
ipResourceUpdateDto
)
{
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
==
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
IpResource
ipResource
=
ipResourceRepository
.
findByAddrAndIsDeleted
(
ipResourceUpdateDto
.
getAddr
(),
false
);
if
(
ipResource
!=
null
)
{
return
true
;
...
...
@@ -697,8 +713,6 @@ public class IpResourceServiceImpl implements IpResourceService {
@Override
public
IpResourceDto
queryIp
(
String
username
,
IpResourceRequestDto
ipResourceRequestDto
)
{
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
==
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
IpResource
ipResource
=
null
;
if
(
ipResourceRequestDto
.
getAddr
()
!=
null
&&
ipResourceRequestDto
.
getAddr
().
size
()
>
0
)
ipResource
=
ipResourceRepository
.
findByAddrAndIsDeleted
(
ipResourceRequestDto
.
getAddr
().
get
(
0
),
false
);
...
...
src/main/java/com/edgec/browserbackend/browser/service/Impl/ShopServiceImpl.java
View file @
284e3a45
...
...
@@ -247,7 +247,6 @@ public class ShopServiceImpl implements ShopService {
a
.
setShopCount
(
account
.
getShopCount
()
-
1
);
accountRepository
.
save
(
a
);
}
}
else
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
}
...
...
@@ -255,15 +254,25 @@ public class ShopServiceImpl implements ShopService {
@Override
public
void
transferShop
(
String
username
,
String
shopId
,
String
groupId
)
{
// 1. 校验当前用户是否存在账户信息
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
UserShop
userShop
=
userShopRepository
.
findByUsernameAndShopId
(
username
,
shopId
);
// 2. 校验 shop 是否存在
Shop
shop
=
shopRepository
.
findById
(
shopId
).
orElseThrow
(()
->
new
ClientRequestException
(
BrowserErrorCode
.
SHOPNOTEXIST
));
// 3. 校验 分组信息是否存在 以及 group 是否是当前 用户创建
Group
group
=
groupRepository
.
findById
(
groupId
).
orElseThrow
(()
->
new
ClientRequestException
(
BrowserErrorCode
.
GROUPNOTEXIST
));
if
(
userShop
==
null
||
(!
group
.
getId
().
equals
(
"-1"
)
&&
group
.
getOwner
()
!=
null
&&
!
group
.
getOwner
().
equals
(
username
)
))
{
if
(
!
group
.
getId
().
equals
(
"-1"
)
&&
group
.
getOwner
()
!=
null
&&
!
group
.
getOwner
().
equals
(
username
))
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
}
Shop
shop
=
shopRepository
.
findById
(
shopId
).
orElseThrow
(()
->
new
ClientRequestException
(
BrowserErrorCode
.
SHOPNOTEXIST
));
// 4. 获取 usershop 信息 并校验
UserShop
userShop
=
userShopRepository
.
findByUsernameAndShopId
(
username
,
shopId
);
if
(
userShop
==
null
)
{
throw
new
ClientRequestException
(
BrowserErrorCode
.
USER_NOT_BIND_SHOP
);
}
try
{
// 5.更该 店铺 分组 并保存
userShop
.
setGroupId
(
groupId
);
userShopRepository
.
save
(
userShop
);
}
catch
(
Exception
e
)
{
...
...
@@ -275,98 +284,115 @@ public class ShopServiceImpl implements ShopService {
@Override
public
void
assignShops
(
String
username
,
List
<
String
>
shopIds
,
List
<
String
>
users
)
{
// 1. 查询当前登录用户信息,并校验是否具备分配权限
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
.
getPermission
()
<
8
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
}
// 2. 校验当前用户是否绑定了对应的 shops,并获取 usershops信息
List
<
UserShop
>
userShops
=
userShopRepository
.
findByUsernameAndShopIdIn
(
username
,
shopIds
);
if
(
account
.
getPermission
()
<
8
||
userShops
==
null
||
userShops
.
size
()
<
1
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
if
(
userShops
==
null
||
userShops
.
size
()
<
1
)
{
throw
new
ClientRequestException
(
BrowserErrorCode
.
USER_NOT_BIND_SHOP
);
}
Pageable
pageable
=
PageRequest
.
of
(
0
,
100
);
List
<
Shop
>
shops
=
shopRepository
.
findByShopIdInOrderByCreateTimeDesc
(
shopIds
,
pageable
).
getContent
();
if
(
shops
==
null
||
shops
.
size
()
<
1
)
throw
new
ClientRequestException
(
BrowserErrorCode
.
SHOPNOTEXIST
);
// 3. 查询 目标 账户是否存在,并获取账户信息。注意:目标账户只能是当前用户的子账户
List
<
Account
>
accounts
=
accountRepository
.
findByNameIn
(
users
);
if
(
accounts
==
null
||
accounts
.
size
()
!=
users
.
size
())
if
(
accounts
==
null
||
accounts
.
size
()
!=
users
.
size
())
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
}
for
(
Account
ac
:
accounts
)
{
if
(
ac
.
getParent
()
==
null
||
!
ac
.
getParent
().
equals
(
username
))
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
}
}
if
(
shops
.
size
()
==
1
)
{
try
{
// 4. 获取 shop 信息 并校验
Pageable
pageable
=
PageRequest
.
of
(
0
,
100
);
List
<
Shop
>
shops
=
shopRepository
.
findByShopIdInOrderByCreateTimeDesc
(
shopIds
,
pageable
).
getContent
();
if
(
shops
==
null
||
shops
.
size
()
<
1
)
{
throw
new
ClientRequestException
(
BrowserErrorCode
.
SHOPNOTEXIST
);
}
try
{
if
(
shops
.
size
()
==
1
)
{
// 5. 删除除当前用户外的所有 usershop 信息
userShopRepository
.
deleteByShopIdExceptOwner
(
shops
.
get
(
0
).
getShopId
(),
shops
.
get
(
0
).
getOwner
());
for
(
Account
account1
:
accounts
)
{
// todo 这个地方有点多余
UserShop
userShop1
=
userShopRepository
.
findByUsernameAndShopId
(
account1
.
getName
(),
shops
.
get
(
0
).
getShopId
());
if
(
userShop1
!=
null
)
if
(
userShop1
!=
null
)
{
continue
;
}
// 6. 封装 目标账户的 usershop信息并保存
userShop1
=
new
UserShop
(
account1
.
getName
(),
shops
.
get
(
0
).
getShopId
(),
"-1"
);
userShopRepository
.
save
(
userShop1
);
}
}
catch
(
Exception
e
)
{
logger
.
error
(
"fail to assign"
,
e
.
getMessage
());
throw
new
ClientRequestException
(
BrowserErrorCode
.
UNKNOWN
);
}
else
{
shops
.
forEach
(
shop
->
{
for
(
Account
account1
:
accounts
)
{
UserShop
userShop1
=
userShopRepository
.
findByUsernameAndShopId
(
account1
.
getName
(),
shop
.
getShopId
());
if
(
userShop1
!=
null
)
{
return
;
}
userShop1
=
new
UserShop
(
account1
.
getName
(),
shop
.
getShopId
(),
"-1"
);
userShopRepository
.
save
(
userShop1
);
}
}
);
}
}
else
{
shops
.
forEach
(
shop
->
{
try
{
for
(
Account
account1
:
accounts
)
{
UserShop
userShop1
=
userShopRepository
.
findByUsernameAndShopId
(
account1
.
getName
(),
shop
.
getShopId
());
if
(
userShop1
!=
null
)
continue
;
userShop1
=
new
UserShop
(
account1
.
getName
(),
shop
.
getShopId
(),
"-1"
);
userShopRepository
.
save
(
userShop1
);
}
}
catch
(
Exception
e
)
{
logger
.
error
(
"fail to assign"
,
e
.
getMessage
());
throw
new
ClientRequestException
(
BrowserErrorCode
.
UNKNOWN
);
}
});
}
catch
(
Exception
e
)
{
logger
.
error
(
"fail to assign"
,
e
.
getMessage
());
throw
new
ClientRequestException
(
BrowserErrorCode
.
UNKNOWN
);
}
}
@Override
public
ShopResultDto
queryShop
(
String
username
,
String
shopId
)
{
// 1. 校验 账户、商铺、IP资源、usershop 信息
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
Shop
shop
=
shopRepository
.
findById
(
shopId
).
orElseThrow
(()
->
new
ClientRequestException
(
BrowserErrorCode
.
SHOPNOTEXIST
));
IpResource
ipResource
=
ipResourceRepository
.
findFirstByShopIdsIsAndIsDeleted
(
shopId
,
false
);
if
(
ipResource
==
null
)
{
throw
new
ClientRequestException
(
BrowserErrorCode
.
IPNOTEXIST
);
}
UserShop
userShop
=
userShopRepository
.
findByUsernameAndShopId
(
username
,
shopId
);
if
(
userShop
==
null
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
}
Shop
shop
=
shopRepository
.
findById
(
shopId
).
orElse
(
null
);
if
(
shop
==
null
)
{
throw
new
ClientRequestException
(
BrowserErrorCode
.
SHOPNOTEXIST
);
}
IpResource
ipResource
=
ipResourceRepository
.
findFirstByShopIdsIsAndIsDeleted
(
shopId
,
false
);
if
(
ipResource
==
null
)
throw
new
ClientRequestException
(
BrowserErrorCode
.
IPNOTEXIST
);
String
group
=
userShopRepository
.
findByUsernameAndShopId
(
username
,
shop
.
getShopId
()).
getGroupId
();
ShopResultDto
shopResultDto
=
null
;
// 2. 封装 ip 资源信息
IpResourceDto
ipResourceDto
=
null
;
if
(
ipResource
.
isSpecialLine
())
{
SpecialLine
specialLine
=
specialLineRepository
.
findAll
().
get
(
0
);
shopResultDto
=
ShopResultDto
.
of
(
shop
,
group
,
new
IpResourceDto
(
ipResource
,
null
,
false
,
specialLine
));
}
else
shopResultDto
=
ShopResultDto
.
of
(
shop
,
group
,
new
IpResourceDto
(
ipResource
,
null
,
false
));
return
shopResultDto
;
ipResourceDto
=
new
IpResourceDto
(
ipResource
,
null
,
false
,
specialLine
);
}
else
{
ipResourceDto
=
new
IpResourceDto
(
ipResource
,
null
,
false
);
}
// 3. 封装 ShopResultDto 信息并返回
return
ShopResultDto
.
of
(
shop
,
userShop
.
getGroupId
(),
ipResourceDto
);
}
@Override
public
ShopPageResultDto
getShopList
(
String
username
,
String
groupId
,
int
pageNum
,
int
amount
,
ShopFilterDto
shopFilterDto
)
{
// 当前登录用户的账户是否存在
//
1. 校验
当前登录用户的账户是否存在
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
// 当前查询的分组信息是否正确
//
2. 如有有分组校验
当前查询的分组信息是否正确
Group
group
=
null
;
if
(
groupId
!=
null
)
{
group
=
groupRepository
.
findById
(
groupId
).
orElseThrow
(()
->
new
ClientRequestException
(
BrowserErrorCode
.
GROUPNOTEXIST
));
if
(
group
.
getOwner
()
!=
null
&&
!
group
.
getOwner
().
equals
(
username
))
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
}
}
// 根据 groupId 与 username 来查询 shopIds (如果当前用户是父账户,则查询结果包含子账户的shopId)
//
3.
根据 groupId 与 username 来查询 shopIds (如果当前用户是父账户,则查询结果包含子账户的shopId)
List
<
String
>
allIds
=
null
;
if
(
"-1"
.
equals
(
groupId
))
{
allIds
=
userShopRepository
.
findByUsername
(
username
).
stream
()
...
...
@@ -377,6 +403,7 @@ public class ShopServiceImpl implements ShopService {
.
map
(
UserShop:
:
getShopId
).
collect
(
Collectors
.
toList
());
}
// 4. 根据传入的过滤条件得到 shopIds
List
<
String
>
shopIds
=
null
;
if
(
shopFilterDto
.
getBindIp
()
==
0
)
{
shopIds
=
allIds
;
...
...
@@ -391,8 +418,8 @@ public class ShopServiceImpl implements ShopService {
}
else
{
// 店铺绑定的 ip 资源被删除的 店铺
for
(
String
id
:
allIds
)
{
IpResource
ipResource
=
ipResourceRepository
.
findFirstByShopIdsIsAndIsDeleted
(
id
,
fals
e
);
if
(
ipResource
=
=
null
)
{
IpResource
ipResource
=
ipResourceRepository
.
findFirstByShopIdsIsAndIsDeleted
(
id
,
tru
e
);
if
(
ipResource
!
=
null
)
{
shopIds
.
add
(
id
);
}
}
...
...
@@ -400,13 +427,15 @@ public class ShopServiceImpl implements ShopService {
amount
=
amount
>
100
?
100
:
amount
;
Pageable
pageable
=
PageRequest
.
of
(
pageNum
,
amount
);
//
根据商铺ids 与 过滤条件 得到商铺
//
5. 根据过滤后的商铺ids 与 其他过滤条件 得到商铺信息并分页
Page
<
Shop
>
shops
=
getShopsByFilter
(
shopFilterDto
,
shopIds
,
pageable
);
// 6. 封装 shopResultDtos 信息并返回
ShopPageResultDto
<
ShopResultDto
>
shopPageResultDto
=
new
ShopPageResultDto
<>();
if
(
shops
!=
null
&&
shops
.
getNumberOfElements
()
>=
1
)
{
List
<
ShopResultDto
>
shopResultDtos
=
new
ArrayList
<>();
shops
.
getContent
().
stream
().
forEach
(
// 设置ip资源状态 并 封装 shopResultDto信息
x
->
{
IpResource
ipResource
=
ipResourceRepository
.
findFirstByShopIdsIsAndIsDeleted
(
x
.
getShopId
(),
false
);
// 如果 ip资源非空 且 addr 也非空
...
...
@@ -444,7 +473,7 @@ public class ShopServiceImpl implements ShopService {
ipResource
.
setStatus
(
1
);
ipResourceRepository
.
save
(
ipResource
);
// 4. 其他
// 4. 其他
,将ip资源状态设置为 未使用
}
else
{
if
((
ipResource
.
getStatus
()
==
0
||
ipResource
.
getStatus
()
==
1
||
ipResource
.
getStatus
()
==
2
)
&&
ipResource
.
getVendor
()
!=
Vendor
.
local
)
{
ipResource
.
setStatus
(
0
);
...
...
@@ -504,21 +533,25 @@ public class ShopServiceImpl implements ShopService {
@Override
public
List
<
String
>
getShopUsers
(
String
username
,
String
shopId
)
{
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
==
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
if
(
account
.
getParent
()
!=
null
)
if
(
account
.
getParent
()
!=
null
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
List
<
String
>
shopUsers
=
userShopRepository
.
findByShopId
(
shopId
).
stream
().
map
(
x
->
x
.
getUsername
()).
filter
(
x
->
!
x
.
equals
(
username
)).
collect
(
Collectors
.
toList
());
}
// 获取当前登录用户绑定了当前商铺的 所有子用户名
List
<
String
>
shopUsers
=
userShopRepository
.
findByShopId
(
shopId
).
stream
()
.
map
(
x
->
x
.
getUsername
())
.
filter
(
x
->
!
x
.
equals
(
username
))
.
collect
(
Collectors
.
toList
());
return
shopUsers
;
}
@Override
public
List
<
String
>
getBatchShopUsers
(
String
username
,
List
<
String
>
shopIds
)
{
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
==
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
if
(
account
.
getParent
()
!=
null
)
if
(
account
.
getParent
()
!=
null
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
}
List
<
String
>
shopUsers
=
new
ArrayList
<>();
if
(
shopIds
!=
null
&&
shopIds
.
size
()
>
0
)
{
String
maxShopId
=
null
;
...
...
@@ -530,15 +563,19 @@ public class ShopServiceImpl implements ShopService {
maxShopId
=
shopId
;
}
}
if
(
maxShopId
==
null
)
return
new
ArrayList
<>();
if
(
maxShopId
==
null
)
{
return
shopUsers
;
}
List
<
String
>
users
=
userShopRepository
.
findByShopId
(
maxShopId
).
stream
().
map
(
x
->
x
.
getUsername
()).
filter
(
x
->
!
x
.
equals
(
username
)).
collect
(
Collectors
.
toList
());
for
(
String
user
:
users
)
{
int
shopCount
=
userShopRepository
.
countByUsernameAndShopIdIn
(
user
,
shopIds
);
if
(
shopCount
<
shopIds
.
size
())
if
(
shopCount
<
shopIds
.
size
())
{
continue
;
else
}
else
{
shopUsers
.
add
(
user
);
}
}
}
return
shopUsers
;
...
...
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