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
1ff453f7
Commit
1ff453f7
authored
Jul 07, 2020
by
Administrator
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'upgrade' into 'master'
Upgrade See merge request
!151
parents
0a379c4b
8cabd289
Hide whitespace changes
Inline
Side-by-side
Showing
26 changed files
with
1244 additions
and
1043 deletions
+1244
-1043
AccountController.java
.../browserbackend/account/controller/AccountController.java
+52
-51
Account.java
...java/com/edgec/browserbackend/account/domain/Account.java
+6
-0
IpChargeRequestDto.java
...gec/browserbackend/account/domain/IpChargeRequestDto.java
+16
-1
Otp.java
...ain/java/com/edgec/browserbackend/account/domain/Otp.java
+12
-0
Promotion.java
...va/com/edgec/browserbackend/account/domain/Promotion.java
+34
-8
User.java
...in/java/com/edgec/browserbackend/account/domain/User.java
+6
-100
UserPrePaidBilling.java
...gec/browserbackend/account/domain/UserPrePaidBilling.java
+19
-154
AccountServiceImpl.java
...owserbackend/account/service/impl/AccountServiceImpl.java
+186
-146
PaymentServiceImpl.java
...owserbackend/account/service/impl/PaymentServiceImpl.java
+275
-304
UserServiceImpl.java
...gec/browserbackend/auth/service/Impl/UserServiceImpl.java
+19
-7
BrowserErrorCode.java
...ec/browserbackend/browser/ErrorCode/BrowserErrorCode.java
+1
-0
ShopController.java
...gec/browserbackend/browser/controller/ShopController.java
+28
-0
TempController.java
...gec/browserbackend/browser/controller/TempController.java
+25
-0
UserCode.java
...ava/com/edgec/browserbackend/browser/domain/UserCode.java
+20
-0
IpResourceRequestDto.java
...dgec/browserbackend/browser/dto/IpResourceRequestDto.java
+35
-4
IpResourceRepositoryCustomImpl.java
...nd/browser/repository/IpResourceRepositoryCustomImpl.java
+1
-4
ShopRepository.java
...gec/browserbackend/browser/repository/ShopRepository.java
+5
-0
UserCodeRepository.java
...browserbackend/browser/repository/UserCodeRepository.java
+12
-0
IpResourceServiceImpl.java
...erbackend/browser/service/Impl/IpResourceServiceImpl.java
+194
-127
ShopServiceImpl.java
.../browserbackend/browser/service/Impl/ShopServiceImpl.java
+181
-85
TempServiceImpl.java
.../browserbackend/browser/service/Impl/TempServiceImpl.java
+21
-0
ShopService.java
...com/edgec/browserbackend/browser/service/ShopService.java
+5
-2
TempService.java
...com/edgec/browserbackend/browser/service/TempService.java
+10
-0
BrowserTask.java
...va/com/edgec/browserbackend/browser/task/BrowserTask.java
+18
-16
PaymentTask.java
...va/com/edgec/browserbackend/browser/task/PaymentTask.java
+26
-16
PromotionTask.java
.../com/edgec/browserbackend/browser/task/PromotionTask.java
+37
-18
No files found.
src/main/java/com/edgec/browserbackend/account/controller/AccountController.java
View file @
1ff453f7
...
@@ -80,11 +80,7 @@ public class AccountController {
...
@@ -80,11 +80,7 @@ public class AccountController {
resultDto
.
setStatus
(
0
);
resultDto
.
setStatus
(
0
);
resultDto
.
setData
(
userDtos
);
resultDto
.
setData
(
userDtos
);
}
catch
(
ClientRequestException
e
)
{
}
catch
(
ClientRequestException
e
)
{
resultDto
.
setStatus
(-
1
);
dealClientRequestException
(
resultDto
,
e
);
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
statusInfo
.
put
(
"code"
,
e
.
getErrorCode
());
statusInfo
.
put
(
"message"
,
e
.
getMessage
());
resultDto
.
setStatusInfo
(
statusInfo
);
}
}
return
resultDto
;
return
resultDto
;
}
}
...
@@ -102,11 +98,7 @@ public class AccountController {
...
@@ -102,11 +98,7 @@ public class AccountController {
resultDto
.
setStatus
(
0
);
resultDto
.
setStatus
(
0
);
resultDto
.
setData
(
account1
);
resultDto
.
setData
(
account1
);
}
catch
(
ClientRequestException
e
)
{
}
catch
(
ClientRequestException
e
)
{
resultDto
.
setStatus
(-
1
);
dealClientRequestException
(
resultDto
,
e
);
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
statusInfo
.
put
(
"code"
,
e
.
getErrorCode
());
statusInfo
.
put
(
"message"
,
e
.
getMessage
());
resultDto
.
setStatusInfo
(
statusInfo
);
}
}
return
resultDto
;
return
resultDto
;
}
}
...
@@ -119,32 +111,29 @@ public class AccountController {
...
@@ -119,32 +111,29 @@ public class AccountController {
resultDto
.
setStatus
(
0
);
resultDto
.
setStatus
(
0
);
}
catch
(
ClientRequestException
e
)
{
}
catch
(
ClientRequestException
e
)
{
logger
.
error
(
"fail to send sms"
,
e
);
logger
.
error
(
"fail to send sms"
,
e
);
resultDto
.
setStatus
(-
1
);
dealClientRequestException
(
resultDto
,
e
);
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
statusInfo
.
put
(
"code"
,
e
.
getErrorCode
());
statusInfo
.
put
(
"message"
,
e
.
getMessage
());
resultDto
.
setStatusInfo
(
statusInfo
);
}
}
return
resultDto
;
return
resultDto
;
}
}
/**
* 用户注册
*/
@RequestMapping
(
path
=
"/signUp"
,
method
=
RequestMethod
.
POST
)
@RequestMapping
(
path
=
"/signUp"
,
method
=
RequestMethod
.
POST
)
public
ResultDto
createNewAccount
(
@Valid
@RequestBody
User
user
)
{
public
ResultDto
createNewAccount
(
@Valid
@RequestBody
User
user
)
{
ResultDto
resultDto
=
new
ResultDto
();
ResultDto
resultDto
=
new
ResultDto
();
try
{
try
{
Account
account
=
new
Account
();
Account
account
=
new
Account
();
if
(!
StringUtils
.
isEmpty
(
user
.
getEmail
()))
if
(!
StringUtils
.
isEmpty
(
user
.
getEmail
()))
{
account
=
accountService
.
create
(
user
);
account
=
accountService
.
create
(
user
);
else
}
else
{
account
=
accountService
.
createWithSms
(
user
);
account
=
accountService
.
createWithSms
(
user
);
}
resultDto
.
setStatus
(
0
);
resultDto
.
setStatus
(
0
);
resultDto
.
setData
(
account
);
resultDto
.
setData
(
account
);
}
catch
(
ClientRequestException
e
)
{
}
catch
(
ClientRequestException
e
)
{
resultDto
.
setStatus
(-
1
);
dealClientRequestException
(
resultDto
,
e
);
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
statusInfo
.
put
(
"code"
,
e
.
getErrorCode
());
statusInfo
.
put
(
"message"
,
e
.
getMessage
());
resultDto
.
setStatusInfo
(
statusInfo
);
}
}
return
resultDto
;
return
resultDto
;
}
}
...
@@ -162,7 +151,10 @@ public class AccountController {
...
@@ -162,7 +151,10 @@ public class AccountController {
emailService
.
sendHtmlMail
(
"sales@cloudam.io"
,
"客户联系:"
+
contactUs
.
getName
(),
sb
.
toString
());
emailService
.
sendHtmlMail
(
"sales@cloudam.io"
,
"客户联系:"
+
contactUs
.
getName
(),
sb
.
toString
());
}
}
// 消费总览
/**
* 费用明细
*/
@RequestMapping
(
path
=
"/userbilling"
,
method
=
RequestMethod
.
POST
)
@RequestMapping
(
path
=
"/userbilling"
,
method
=
RequestMethod
.
POST
)
public
ResultDto
getUserBills0
(
Principal
principal
,
@RequestBody
UserPrePaidBillingRequestDto
userPrePaidBillingRequestDto
)
{
public
ResultDto
getUserBills0
(
Principal
principal
,
@RequestBody
UserPrePaidBillingRequestDto
userPrePaidBillingRequestDto
)
{
ResultDto
resultDto
=
new
ResultDto
();
ResultDto
resultDto
=
new
ResultDto
();
...
@@ -170,11 +162,7 @@ public class AccountController {
...
@@ -170,11 +162,7 @@ public class AccountController {
resultDto
.
setData
(
accountService
.
getUserPrePaidBills
(
principal
.
getName
(),
userPrePaidBillingRequestDto
));
resultDto
.
setData
(
accountService
.
getUserPrePaidBills
(
principal
.
getName
(),
userPrePaidBillingRequestDto
));
resultDto
.
setStatus
(
0
);
resultDto
.
setStatus
(
0
);
}
catch
(
ClientRequestException
e
)
{
}
catch
(
ClientRequestException
e
)
{
resultDto
.
setStatus
(-
1
);
dealClientRequestException
(
resultDto
,
e
);
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
statusInfo
.
put
(
"code"
,
e
.
getErrorCode
());
statusInfo
.
put
(
"message"
,
e
.
getMessage
());
resultDto
.
setStatusInfo
(
statusInfo
);
}
}
return
resultDto
;
return
resultDto
;
}
}
...
@@ -214,11 +202,7 @@ public class AccountController {
...
@@ -214,11 +202,7 @@ public class AccountController {
accountService
.
resetPassword
(
principal
.
getName
(),
user
);
accountService
.
resetPassword
(
principal
.
getName
(),
user
);
resultDto
.
setStatus
(
0
);
resultDto
.
setStatus
(
0
);
}
catch
(
ClientRequestException
e
)
{
}
catch
(
ClientRequestException
e
)
{
resultDto
.
setStatus
(-
1
);
dealClientRequestException
(
resultDto
,
e
);
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
statusInfo
.
put
(
"code"
,
e
.
getErrorCode
());
statusInfo
.
put
(
"message"
,
e
.
getMessage
());
resultDto
.
setStatusInfo
(
statusInfo
);
}
}
return
resultDto
;
return
resultDto
;
}
}
...
@@ -235,25 +219,30 @@ public class AccountController {
...
@@ -235,25 +219,30 @@ public class AccountController {
accountService
.
resetPasswordWithOtp
(
user
);
accountService
.
resetPasswordWithOtp
(
user
);
resultDto
.
setStatus
(
0
);
resultDto
.
setStatus
(
0
);
}
catch
(
ClientRequestException
e
)
{
}
catch
(
ClientRequestException
e
)
{
resultDto
.
setStatus
(-
1
);
dealClientRequestException
(
resultDto
,
e
);
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
statusInfo
.
put
(
"code"
,
e
.
getErrorCode
());
statusInfo
.
put
(
"message"
,
e
.
getMessage
());
resultDto
.
setStatusInfo
(
statusInfo
);
}
}
return
resultDto
;
return
resultDto
;
}
}
/**
* 使用微信进行账户充值
*/
@RequestMapping
(
path
=
"/0xwxpayputorder/{amount}"
,
method
=
RequestMethod
.
GET
)
@RequestMapping
(
path
=
"/0xwxpayputorder/{amount}"
,
method
=
RequestMethod
.
GET
)
public
UserPaymentDto
wechatPayCallback
(
Principal
principal
,
@PathVariable
int
amount
)
{
public
UserPaymentDto
wechatPayCallback
(
Principal
principal
,
@PathVariable
int
amount
)
{
return
paymentService
.
wxPutPayOrder
(
principal
.
getName
(),
amount
);
return
paymentService
.
wxPutPayOrder
(
principal
.
getName
(),
amount
);
}
}
/**
* 使用微信进行账户充值成功后,微信会调用该接口
*/
@RequestMapping
(
path
=
"/0xwxpaycallback/{tradno}"
,
method
=
RequestMethod
.
GET
)
@RequestMapping
(
path
=
"/0xwxpaycallback/{tradno}"
,
method
=
RequestMethod
.
GET
)
public
String
wechatPayCallback
(
@PathVariable
String
tradno
)
{
public
String
wechatPayCallback
(
@PathVariable
String
tradno
)
{
return
paymentService
.
wechatPayCallback
(
tradno
);
return
paymentService
.
wechatPayCallback
(
tradno
);
}
}
/**
* 使用微信进行账户充值成功后,微信会调用该接口
*/
@RequestMapping
(
path
=
"/0xwxcheckorderstatus/{tradno}/{chargeType}"
,
method
=
RequestMethod
.
GET
)
@RequestMapping
(
path
=
"/0xwxcheckorderstatus/{tradno}/{chargeType}"
,
method
=
RequestMethod
.
GET
)
public
UserPaymentDto
wechatCheckOrderStatus
(
Principal
principal
,
@PathVariable
String
tradno
,
@PathVariable
int
chargeType
)
{
public
UserPaymentDto
wechatCheckOrderStatus
(
Principal
principal
,
@PathVariable
String
tradno
,
@PathVariable
int
chargeType
)
{
return
paymentService
.
wxCheckOrderStatus
(
tradno
,
chargeType
);
return
paymentService
.
wxCheckOrderStatus
(
tradno
,
chargeType
);
...
@@ -264,19 +253,28 @@ public class AccountController {
...
@@ -264,19 +253,28 @@ public class AccountController {
return
paymentService
.
wxCheckOrderStatus
(
tradno
,
chargeType
);
return
paymentService
.
wxCheckOrderStatus
(
tradno
,
chargeType
);
}
}
/**
* 使用 支付宝 充值 成功后回调
*/
@RequestMapping
(
path
=
"/0xalipaycallback/{tradno}"
,
method
=
RequestMethod
.
GET
)
@RequestMapping
(
path
=
"/0xalipaycallback/{tradno}"
,
method
=
RequestMethod
.
GET
)
public
void
alipayPayCallback
(
@PathVariable
String
tradno
)
{
public
void
alipayPayCallback
(
@PathVariable
String
tradno
)
{
paymentService
.
alipaCallback
(
tradno
);
paymentService
.
alipaCallback
(
tradno
);
}
}
/**
* 使用 支付宝 充值 成功后回调
*/
@RequestMapping
(
path
=
"/0xalicheckorderstatus/{tradno}/{chargeType}"
,
method
=
RequestMethod
.
GET
)
@RequestMapping
(
path
=
"/0xalicheckorderstatus/{tradno}/{chargeType}"
,
method
=
RequestMethod
.
GET
)
public
UserPaymentDto
alipayCheckOrderStatus
(
Principal
principal
,
@PathVariable
(
required
=
false
)
String
tradno
,
@PathVariable
int
chargeType
)
{
public
UserPaymentDto
alipayCheckOrderStatus
(
Principal
principal
,
@PathVariable
(
required
=
false
)
String
tradno
,
@PathVariable
int
chargeType
)
{
return
paymentService
.
aliCheckOrderStatus
(
tradno
,
chargeType
);
return
paymentService
.
aliCheckOrderStatus
(
tradno
,
chargeType
);
}
}
/**
* 使用 支付宝 充值
*/
@RequestMapping
(
path
=
"/0xalipay/{amount}"
,
method
=
RequestMethod
.
GET
)
@RequestMapping
(
path
=
"/0xalipay/{amount}"
,
method
=
RequestMethod
.
GET
)
public
void
alipayPutOrder
(
HttpServletRequest
request
,
HttpServletResponse
response
,
@PathVariable
int
amount
)
throws
Exception
{
public
void
alipayPutOrder
(
HttpServletRequest
request
,
HttpServletResponse
response
,
@PathVariable
int
amount
)
throws
Exception
{
String
by
=
request
.
getParameter
(
"by"
);
String
by
=
request
.
getParameter
(
"by"
);
String
form
=
paymentService
.
alipayPutPayOrder
(
request
.
getUserPrincipal
().
getName
(),
amount
,
by
);
String
form
=
paymentService
.
alipayPutPayOrder
(
request
.
getUserPrincipal
().
getName
(),
amount
,
by
);
response
.
setContentType
(
"text/html;charset=utf-8"
);
response
.
setContentType
(
"text/html;charset=utf-8"
);
...
@@ -428,11 +426,7 @@ public class AccountController {
...
@@ -428,11 +426,7 @@ public class AccountController {
accountService
.
authorizeCompany
(
principal
.
getName
(),
companyAuthorizeDto
);
accountService
.
authorizeCompany
(
principal
.
getName
(),
companyAuthorizeDto
);
resultDto
.
setStatus
(
0
);
resultDto
.
setStatus
(
0
);
}
catch
(
ClientRequestException
e
)
{
}
catch
(
ClientRequestException
e
)
{
resultDto
.
setStatus
(-
1
);
dealClientRequestException
(
resultDto
,
e
);
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
statusInfo
.
put
(
"code"
,
e
.
getErrorCode
());
statusInfo
.
put
(
"message"
,
e
.
getMessage
());
resultDto
.
setStatusInfo
(
statusInfo
);
}
}
return
resultDto
;
return
resultDto
;
}
}
...
@@ -472,17 +466,24 @@ public class AccountController {
...
@@ -472,17 +466,24 @@ public class AccountController {
ResultDto
resultDto
=
new
ResultDto
();
ResultDto
resultDto
=
new
ResultDto
();
try
{
try
{
if
(
paymentService
.
alipayWithdraw
(
principal
.
getName
(),
account
,
realName
,
amount
))
if
(
paymentService
.
alipayWithdraw
(
principal
.
getName
(),
account
,
realName
,
amount
))
{
resultDto
.
setStatus
(
0
);
resultDto
.
setStatus
(
0
);
else
}
else
{
resultDto
.
setStatus
(-
1
);
resultDto
.
setStatus
(-
1
);
}
}
catch
(
ClientRequestException
e
)
{
}
catch
(
ClientRequestException
e
)
{
resultDto
.
setStatus
(-
1
);
dealClientRequestException
(
resultDto
,
e
);
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
statusInfo
.
put
(
"code"
,
e
.
getErrorCode
());
statusInfo
.
put
(
"message"
,
e
.
getMessage
());
resultDto
.
setStatusInfo
(
statusInfo
);
}
}
return
resultDto
;
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/account/domain/Account.java
View file @
1ff453f7
...
@@ -67,8 +67,14 @@ public class Account {
...
@@ -67,8 +67,14 @@ public class Account {
*/
*/
private
int
permission
=
-
1
;
private
int
permission
=
-
1
;
/**
* 注册成功后,当前用户的邀请码相关信息,用于邀请别人获取佣金
*/
private
Promotion
promotion
;
private
Promotion
promotion
;
/**
* 注册时,其他用户给当前用户的邀请码
*/
private
String
promotionCode
;
private
String
promotionCode
;
private
String
comment
;
private
String
comment
;
...
...
src/main/java/com/edgec/browserbackend/account/domain/IpChargeRequestDto.java
View file @
1ff453f7
...
@@ -4,11 +4,18 @@ public class IpChargeRequestDto {
...
@@ -4,11 +4,18 @@ public class IpChargeRequestDto {
private
int
period
;
private
int
period
;
private
int
amount
;
private
int
amount
;
private
String
region
;
private
String
region
;
private
String
unit
=
"month"
;
private
String
unit
=
"month"
;
private
String
target
;
private
String
target
;
//0 -- 充值, 1 -- newip, 2 --renew, 3 -- 退还
//0 -- 充值, 1 -- newip, 2 --renew, 3 -- 退还
private
int
chargeType
=
0
;
private
int
chargeType
=
0
;
/**
* 续费ip
*/
private
String
chargeIp
;
//0 -- 余额, 1 -- 支付宝, 2 -- 微信
//0 -- 余额, 1 -- 支付宝, 2 -- 微信
private
int
payMethod
=
0
;
private
int
payMethod
=
0
;
...
@@ -97,4 +104,12 @@ public class IpChargeRequestDto {
...
@@ -97,4 +104,12 @@ public class IpChargeRequestDto {
public
void
setPayMethod
(
int
payMethod
)
{
public
void
setPayMethod
(
int
payMethod
)
{
this
.
payMethod
=
payMethod
;
this
.
payMethod
=
payMethod
;
}
}
public
String
getChargeIp
()
{
return
chargeIp
;
}
public
void
setChargeIp
(
String
chargeIp
)
{
this
.
chargeIp
=
chargeIp
;
}
}
}
src/main/java/com/edgec/browserbackend/account/domain/Otp.java
View file @
1ff453f7
...
@@ -7,9 +7,21 @@ import org.springframework.data.mongodb.core.mapping.Document;
...
@@ -7,9 +7,21 @@ import org.springframework.data.mongodb.core.mapping.Document;
@Document
(
collection
=
"phoneotp"
)
@Document
(
collection
=
"phoneotp"
)
@JsonIgnoreProperties
(
ignoreUnknown
=
true
)
@JsonIgnoreProperties
(
ignoreUnknown
=
true
)
public
class
Otp
{
public
class
Otp
{
/**
* 手机号
*/
@Id
@Id
private
String
phone
;
private
String
phone
;
/**
* 发送给用户的短信验证码
*/
private
String
otp
;
private
String
otp
;
/**
* 短信验证码的发送时间
*/
private
long
createdAt
;
private
long
createdAt
;
public
String
getPhone
()
{
public
String
getPhone
()
{
...
...
src/main/java/com/edgec/browserbackend/account/domain/Promotion.java
View file @
1ff453f7
...
@@ -9,22 +9,48 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
...
@@ -9,22 +9,48 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
**/
**/
@JsonIgnoreProperties
(
ignoreUnknown
=
true
)
@JsonIgnoreProperties
(
ignoreUnknown
=
true
)
public
class
Promotion
{
public
class
Promotion
{
/**
* 邀请码(用户邀请好友使用本产品)
*/
private
String
code
;
private
String
code
;
//邀请人数
/**
* 邀请人数
*/
private
int
invitedUsers
;
private
int
invitedUsers
;
//好友本月以前总消费
/**
* 好友本月以前总消费
*/
private
int
totalCommission
;
private
int
totalCommission
;
//好友当月消费
/**
* 好友当月消费
*/
private
int
commission
;
private
int
commission
;
//好友上月总消费
/**
* 好友上月总消费
*/
private
int
commissionLastMonth
;
private
int
commissionLastMonth
;
//是否是本公司的销售
/**
* 是否是本公司的销售
*/
private
boolean
isSale
;
private
boolean
isSale
;
//当月礼金
/**
* 当月礼金
*/
private
double
gift
;
private
double
gift
;
//所有未提现金额
/**
* 所有未提现金额
*/
private
double
allGift
;
private
double
allGift
;
//已提现金额
/**
* 已提现金额
*/
private
double
withdrawn
;
private
double
withdrawn
;
public
int
getCommission
()
{
public
int
getCommission
()
{
...
...
src/main/java/com/edgec/browserbackend/account/domain/User.java
View file @
1ff453f7
package
com
.
edgec
.
browserbackend
.
account
.
domain
;
package
com
.
edgec
.
browserbackend
.
account
.
domain
;
import
lombok.Data
;
import
org.hibernate.validator.constraints.Length
;
import
org.hibernate.validator.constraints.Length
;
import
javax.validation.constraints.Email
;
import
javax.validation.constraints.Email
;
...
@@ -7,15 +8,16 @@ import java.util.ArrayList;
...
@@ -7,15 +8,16 @@ import java.util.ArrayList;
import
java.util.List
;
import
java.util.List
;
import
java.util.UUID
;
import
java.util.UUID
;
@Data
public
class
User
{
public
class
User
{
@Length
(
min
=
3
,
max
=
20
,
message
=
"Name length should be between 3 - 20"
)
@Length
(
min
=
3
,
max
=
20
,
message
=
"Name length should be between 3 - 20"
)
private
String
username
;
private
String
username
;
@Length
(
min
=
6
,
max
=
40
,
message
=
"Password length should be between 6 - 40"
)
@Length
(
min
=
6
,
max
=
40
,
message
=
"Password length should be between 6 - 40"
)
private
String
password
;
private
String
password
;
@Email
(
message
=
"Invalid email address"
)
@Email
(
message
=
"Invalid email address"
)
private
String
email
;
private
String
email
;
private
String
phone
;
private
String
phone
;
...
@@ -28,108 +30,12 @@ public class User {
...
@@ -28,108 +30,12 @@ public class User {
private
String
lockReason
;
private
String
lockReason
;
public
String
getAuthCode
()
{
return
authCode
;
}
public
void
setAuthCode
(
String
authCode
)
{
this
.
authCode
=
authCode
;
}
public
String
getPhone
()
{
return
phone
;
}
public
void
setPhone
(
String
phone
)
{
this
.
phone
=
phone
;
}
private
boolean
allowedToCreateSubUser
=
true
;
private
boolean
allowedToCreateSubUser
=
true
;
private
int
permission
;
private
int
permission
;
private
List
<
String
>
whiteList
=
new
ArrayList
<>();
private
List
<
String
>
whiteList
=
new
ArrayList
<>();
private
String
verificationCode
=
UUID
.
randomUUID
().
toString
();
private
String
verificationCode
=
UUID
.
randomUUID
().
toString
();
public
boolean
isEnabled
()
{
return
enabled
;
}
public
void
setEnabled
(
boolean
enabled
)
{
this
.
enabled
=
enabled
;
}
public
String
getVerificationCode
()
{
return
verificationCode
;
}
public
void
setVerificationCode
(
String
verificationCode
)
{
this
.
verificationCode
=
verificationCode
;
}
public
String
getEmail
()
{
return
email
;
}
public
void
setEmail
(
String
email
)
{
this
.
email
=
email
;
}
public
String
getUsername
()
{
return
username
;
}
public
void
setUsername
(
String
username
)
{
this
.
username
=
username
;
}
public
String
getPassword
()
{
return
password
;
}
public
void
setPassword
(
String
password
)
{
this
.
password
=
password
;
}
public
boolean
isAllowedToCreateSubUser
()
{
return
allowedToCreateSubUser
;
}
public
void
setAllowedToCreateSubUser
(
boolean
allowedToCreateSubUser
)
{
allowedToCreateSubUser
=
allowedToCreateSubUser
;
}
public
int
getPermission
()
{
return
permission
;
}
public
void
setPermission
(
int
permission
)
{
this
.
permission
=
permission
;
}
public
String
getLockReason
()
{
return
lockReason
;
}
public
void
setLockReason
(
String
lockReason
)
{
this
.
lockReason
=
lockReason
;
}
public
List
<
String
>
getWhiteList
()
{
return
whiteList
;
}
public
void
setWhiteList
(
List
<
String
>
whiteList
)
{
this
.
whiteList
=
whiteList
;
}
public
String
getPromotionCode
()
{
return
promotionCode
;
}
public
void
setPromotionCode
(
String
promotionCode
)
{
this
.
promotionCode
=
promotionCode
;
}
}
}
src/main/java/com/edgec/browserbackend/account/domain/UserPrePaidBilling.java
View file @
1ff453f7
package
com
.
edgec
.
browserbackend
.
account
.
domain
;
package
com
.
edgec
.
browserbackend
.
account
.
domain
;
import
com.fasterxml.jackson.annotation.JsonIgnoreProperties
;
import
com.fasterxml.jackson.annotation.JsonIgnoreProperties
;
import
org.apache.commons.lang3.StringUtils
;
import
lombok.Getter
;
import
lombok.Setter
;
import
org.springframework.data.annotation.Id
;
import
org.springframework.data.annotation.Id
;
import
org.springframework.data.mongodb.core.index.CompoundIndex
;
import
org.springframework.data.mongodb.core.index.CompoundIndex
;
import
org.springframework.data.mongodb.core.mapping.Document
;
import
org.springframework.data.mongodb.core.mapping.Document
;
...
@@ -9,6 +10,8 @@ import org.springframework.data.mongodb.core.mapping.Document;
...
@@ -9,6 +10,8 @@ import org.springframework.data.mongodb.core.mapping.Document;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
@Getter
@Setter
@Document
(
collection
=
"userprepaidbilling"
)
@Document
(
collection
=
"userprepaidbilling"
)
@JsonIgnoreProperties
(
ignoreUnknown
=
true
)
@JsonIgnoreProperties
(
ignoreUnknown
=
true
)
@CompoundIndex
(
name
=
"idx_username_timestamp"
,
def
=
"{'username': 1, 'timestamp': 1}"
)
@CompoundIndex
(
name
=
"idx_username_timestamp"
,
def
=
"{'username': 1, 'timestamp': 1}"
)
...
@@ -37,13 +40,21 @@ public class UserPrePaidBilling {
...
@@ -37,13 +40,21 @@ public class UserPrePaidBilling {
private
BillStatus
status
;
private
BillStatus
status
;
//0 -- 余额, 1 -- 支付宝, 2 -- 微信, 3 -- 银行转账
/**
* 0 -- 余额, 1 -- 支付宝, 2 -- 微信, 3 -- 银行转账
*/
private
int
payMethod
;
private
int
payMethod
;
private
boolean
isPrepaid
;
private
boolean
isPrepaid
;
/**
* 本次消费金额
*/
private
float
total
;
private
float
total
;
//0 -- 充值, 1 -- newip, 2 -- renew, 3 -- return, 4 -- 礼金提现
/**
* 0 -- 充值, 1 -- newip, 2 -- renew, 3 -- return, 4 -- 礼金提现
*/
private
int
chargeType
;
private
int
chargeType
;
private
String
unit
=
"month"
;
private
String
unit
=
"month"
;
...
@@ -52,161 +63,15 @@ public class UserPrePaidBilling {
...
@@ -52,161 +63,15 @@ public class UserPrePaidBilling {
private
float
balanced
;
private
float
balanced
;
private
List
<
DeductionRecord
>
deductionRecords
=
new
ArrayList
<>();
/**
* 购买或者续费的ip 号
public
float
getRate
()
{
*/
return
rate
;
private
String
chargeIp
;
}
public
float
getTotal
()
{
return
total
;
}
public
void
setRate
(
float
rate
)
{
this
.
rate
=
rate
;
}
public
void
setTotal
(
float
total
)
{
this
.
total
=
total
;
}
public
int
getChargeType
()
{
return
chargeType
;
}
public
void
setChargeType
(
int
chargeType
)
{
this
.
chargeType
=
chargeType
;
}
public
String
getUnit
()
{
return
unit
;
}
public
void
setUnit
(
String
unit
)
{
this
.
unit
=
unit
;
}
public
String
getId
()
{
return
id
;
}
public
void
setId
(
String
id
)
{
private
List
<
DeductionRecord
>
deductionRecords
=
new
ArrayList
<>();
this
.
id
=
id
;
}
public
String
getUsername
()
{
return
username
;
}
public
void
setUsername
(
String
username
)
{
this
.
username
=
username
;
}
public
int
getYear
()
{
return
year
;
}
public
void
setYear
(
int
year
)
{
this
.
year
=
year
;
}
public
int
getMonth
()
{
return
month
;
}
public
void
setMonth
(
int
month
)
{
this
.
month
=
month
;
}
public
long
getTimestamp
()
{
return
timestamp
;
}
public
void
setTimestamp
(
long
timestamp
)
{
this
.
timestamp
=
timestamp
;
}
public
Services
getServices
()
{
return
services
;
}
public
void
setServices
(
Services
services
)
{
this
.
services
=
services
;
}
public
BillStatus
getStatus
()
{
return
status
;
}
public
void
setStatus
(
BillStatus
status
)
{
this
.
status
=
status
;
}
public
boolean
isPrepaid
()
{
return
isPrepaid
;
}
public
void
setPrepaid
(
boolean
prepaid
)
{
this
.
isPrepaid
=
prepaid
;
}
public
List
<
DeductionRecord
>
getDeductionRecords
()
{
return
deductionRecords
;
}
public
void
setDeductionRecords
(
List
<
DeductionRecord
>
deductionRecords
)
{
this
.
deductionRecords
=
deductionRecords
;
}
public
void
addDeductionRecord
(
DeductionRecord
record
)
{
public
void
addDeductionRecord
(
DeductionRecord
record
)
{
this
.
deductionRecords
.
add
(
record
);
this
.
deductionRecords
.
add
(
record
);
}
}
public
int
getAmount
()
{
return
amount
;
}
public
void
setAmount
(
int
amount
)
{
this
.
amount
=
amount
;
}
public
int
getPeriod
()
{
return
period
;
}
public
void
setPeriod
(
int
period
)
{
this
.
period
=
period
;
}
public
int
getPayMethod
()
{
return
payMethod
;
}
public
void
setPayMethod
(
int
payMethod
)
{
this
.
payMethod
=
payMethod
;
}
public
String
getTradeNo
()
{
return
tradeNo
;
}
public
void
setTradeNo
(
String
tradeNo
)
{
this
.
tradeNo
=
tradeNo
;
}
public
String
getAdministrator
()
{
return
administrator
;
}
public
void
setAdministrator
(
String
administrator
)
{
this
.
administrator
=
administrator
;
}
public
float
getBalanced
()
{
return
balanced
;
}
public
void
setBalanced
(
float
balanced
)
{
this
.
balanced
=
balanced
;
}
}
}
src/main/java/com/edgec/browserbackend/account/service/impl/AccountServiceImpl.java
View file @
1ff453f7
...
@@ -21,6 +21,7 @@ import com.edgec.browserbackend.browser.service.ShopService;
...
@@ -21,6 +21,7 @@ import com.edgec.browserbackend.browser.service.ShopService;
import
com.edgec.browserbackend.common.commons.error.ClientRequestException
;
import
com.edgec.browserbackend.common.commons.error.ClientRequestException
;
import
com.edgec.browserbackend.common.utils.Aes
;
import
com.edgec.browserbackend.common.utils.Aes
;
import
com.edgec.browserbackend.common.utils.FileUtil
;
import
com.edgec.browserbackend.common.utils.FileUtil
;
import
org.jetbrains.annotations.NotNull
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
@@ -186,56 +187,39 @@ public class AccountServiceImpl implements AccountService {
...
@@ -186,56 +187,39 @@ public class AccountServiceImpl implements AccountService {
@Override
@Override
public
IpChargeResultDto
chargeByMoney
(
String
name
,
double
money
,
IpChargeRequestDto
requestDto
)
{
public
IpChargeResultDto
chargeByMoney
(
String
name
,
double
money
,
IpChargeRequestDto
requestDto
)
{
IpChargeResultDto
charge
=
new
IpChargeResultDto
();
IpChargeResultDto
charge
=
new
IpChargeResultDto
();
CompletableFuture
.
runAsync
(()
->
{
CompletableFuture
.
runAsync
(
charge
.
setApprovedAmount
(
0
);
()
->
{
charge
.
setSuccess
(
true
);
charge
.
setApprovedAmount
(
0
);
charge
.
setSuccess
(
true
);
Account
account
=
accountRepository
.
findByName
(
name
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
Account
account
=
accountRepository
.
findByName
(
name
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
!=
null
)
{
UserBalance
userBalance
=
userBalanceRepository
.
findById
(
name
).
orElse
(
null
);
UserBalance
userBalance
=
userBalanceRepository
.
findById
(
name
).
orElse
(
null
);
if
(
userBalance
==
null
)
{
userBalance
=
new
UserBalance
();
userBalance
.
setBalanced
(
0
);
userBalance
.
setUsed
(
0
);
userBalance
.
setUsername
(
name
);
userBalanceRepository
.
save
(
userBalance
);
}
userBalanceRepository
.
incrementBalance
(
userBalance
,
-(
float
)
money
,
(
float
)
money
);
// 如果 userBalance 不存在,则创建
userBalance
=
userBalanceRepository
.
findById
(
name
).
orElse
(
null
);
if
(
userBalance
==
null
)
{
userBalance
=
new
UserBalance
();
charge
.
setBalance
(
Math
.
round
(
userBalance
.
getBalanced
()));
userBalance
.
setBalanced
(
0
);
charge
.
setSuccess
(
true
);
userBalance
.
setUsed
(
0
);
userBalance
.
setUsername
(
name
);
UserPrePaidBilling
bill
=
new
UserPrePaidBilling
();
userBalanceRepository
.
save
(
userBalance
);
if
(
account
.
getParent
()
!=
null
)
}
bill
.
setAdministrator
(
account
.
getParent
());
else
bill
.
setAdministrator
(
account
.
getName
());
bill
.
setTradeNo
(
new
SimpleDateFormat
(
"yyyyMMddHHmmss"
).
format
(
new
Date
())
+
SmsUtils
.
createRandom
(
true
,
4
));
bill
.
setChargeType
(
requestDto
.
getChargeType
());
bill
.
setAmount
(
requestDto
.
getAmount
());
bill
.
setUnit
(
requestDto
.
getUnit
());
bill
.
setPeriod
(
requestDto
.
getPeriod
());
bill
.
setPayMethod
(
requestDto
.
getPayMethod
());
bill
.
setUsername
(
name
);
bill
.
setTotal
((
float
)
money
);
bill
.
setStatus
(
BillStatus
.
PAID
);
bill
.
setPrepaid
(
true
);
bill
.
setTimestamp
(
Instant
.
now
().
toEpochMilli
());
final
YearMonth
lastmonth
=
YearMonth
.
now
();
int
monthValue
=
lastmonth
.
getMonthValue
();
int
year
=
lastmonth
.
getYear
();
bill
.
setYear
(
year
);
bill
.
setMonth
(
monthValue
);
bill
.
setBalanced
(
userBalance
.
getBalanced
());
prePaidBillingRepository
.
save
(
bill
);
}
},
AccountServicePool
.
taskPool
).
join
();
// 更新 userbalance
userBalanceRepository
.
incrementBalance
(
userBalance
,
-(
float
)
money
,
(
float
)
money
);
// 获取更新后的 userbalance
userBalance
=
userBalanceRepository
.
findById
(
name
).
orElse
(
null
);
charge
.
setBalance
(
Math
.
round
(
userBalance
.
getBalanced
()));
charge
.
setSuccess
(
true
);
// 封装 userprepaidbilling 信息 并保存
UserPrePaidBilling
bill
=
getUserPrePaidBilling
(
name
,
(
float
)
money
,
requestDto
,
account
,
userBalance
);
prePaidBillingRepository
.
save
(
bill
);
},
AccountServicePool
.
taskPool
).
join
();
return
charge
;
return
charge
;
}
}
...
@@ -253,27 +237,37 @@ public class AccountServiceImpl implements AccountService {
...
@@ -253,27 +237,37 @@ public class AccountServiceImpl implements AccountService {
return
"success"
;
return
"success"
;
}
}
/**
* 封装 预充值扣费 信息
*/
@Override
@Override
public
IpChargeResultDto
preChargeByMoney
(
String
name
,
double
money
)
{
public
IpChargeResultDto
preChargeByMoney
(
String
name
,
double
money
)
{
IpChargeResultDto
precharge
=
new
IpChargeResultDto
();
IpChargeResultDto
precharge
=
new
IpChargeResultDto
();
CompletableFuture
.
runAsync
(()
->
{
CompletableFuture
.
runAsync
(
precharge
.
setApprovedAmount
(
0
);
()
->
{
precharge
.
setSuccess
(
false
);
precharge
.
setApprovedAmount
(
0
);
Account
account
=
accountRepository
.
findByName
(
name
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
precharge
.
setSuccess
(
false
);
if
(
account
!=
null
)
{
UserBalance
userBalance
=
userBalanceRepository
.
findById
(
name
).
orElse
(
null
);
Account
account
=
accountRepository
.
findByName
(
name
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
float
balance
=
0
;
if
(
account
!=
null
)
{
if
(
userBalance
!=
null
)
UserBalance
userBalance
=
userBalanceRepository
.
findById
(
name
).
orElse
(
null
);
balance
=
userBalance
.
getBalanced
();
float
balance
=
0
;
if
(
userBalance
!=
null
)
{
if
(
balance
>=
money
)
{
balance
=
userBalance
.
getBalanced
();
precharge
.
setBalance
((
int
)
Math
.
round
(
balance
-
money
));
}
precharge
.
setSuccess
(
true
);
}
else
{
// 如果账户余额 比 扣费的 多,则设置为成功,并重新设置账户余额
precharge
.
setBalance
(
Math
.
round
(
balance
));
if
(
balance
>=
money
)
{
}
precharge
.
setBalance
((
int
)
Math
.
round
(
balance
-
money
));
}
precharge
.
setSuccess
(
true
);
},
AccountServicePool
.
taskPool
).
join
();
}
else
{
// todo 钱不够了,不是应该提示该充值么
precharge
.
setBalance
(
Math
.
round
(
balance
));
}
}
},
AccountServicePool
.
taskPool
).
join
();
return
precharge
;
return
precharge
;
}
}
...
@@ -426,25 +420,17 @@ public class AccountServiceImpl implements AccountService {
...
@@ -426,25 +420,17 @@ public class AccountServiceImpl implements AccountService {
@Override
@Override
public
Account
createWithSms
(
User
user
)
{
public
Account
createWithSms
(
User
user
)
{
// 1. 校验注册用户是否已存在
Account
existing
=
accountRepository
.
findByName
(
user
.
getUsername
()).
orElse
(
null
);
Account
existing1
=
accountRepository
.
findByName
(
user
.
getUsername
()).
orElse
(
null
);
if
(
existing
!=
null
)
Account
existing2
=
accountRepository
.
findByPhoneNumber
(
user
.
getUsername
());
Account
existing3
=
accountRepository
.
findByPhoneNumber
(
user
.
getPhone
());
Account
existing4
=
accountRepository
.
findByName
(
user
.
getPhone
()).
orElse
(
null
);
if
(
existing1
!=
null
||
existing2
!=
null
||
existing3
!=
null
||
existing4
!=
null
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMEEXIST
,
"account already exists: "
+
user
.
getUsername
());
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMEEXIST
,
"account already exists: "
+
user
.
getUsername
());
}
existing
=
accountRepository
.
findByPhoneNumber
(
user
.
getUsername
());
// 2. 校验用户输入的短信验证码是否正确
if
(
existing
!=
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMEEXIST
,
"account already exists: "
+
user
.
getUsername
());
existing
=
accountRepository
.
findByPhoneNumber
(
user
.
getPhone
());
if
(
existing
!=
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
PHONEEXIST
,
"phone number already exists: "
+
user
.
getPhone
());
existing
=
accountRepository
.
findByName
(
user
.
getPhone
()).
orElse
(
null
);
if
(
existing
!=
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
PHONEEXIST
,
"phone number already exists: "
+
user
.
getPhone
());
Otp
otp
=
otpRepository
.
findByPhoneAndCreatedAtGreaterThanEqual
(
user
.
getUsername
(),
Instant
.
now
().
minusSeconds
(
600
).
toEpochMilli
());
Otp
otp
=
otpRepository
.
findByPhoneAndCreatedAtGreaterThanEqual
(
user
.
getUsername
(),
Instant
.
now
().
minusSeconds
(
600
).
toEpochMilli
());
if
(
otp
==
null
)
{
if
(
otp
==
null
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
OTPWRONG
,
AccountErrorCode
.
OTPWRONG
.
getReason
());
throw
new
ClientRequestException
(
AccountErrorCode
.
OTPWRONG
,
AccountErrorCode
.
OTPWRONG
.
getReason
());
}
}
...
@@ -454,85 +440,77 @@ public class AccountServiceImpl implements AccountService {
...
@@ -454,85 +440,77 @@ public class AccountServiceImpl implements AccountService {
// otpRepository.delete(otp);
// otpRepository.delete(otp);
}
}
Account
account
=
new
Account
();
// 3. 若用户注册时输入了邀请码,则更新邀请人的 account 信息
account
.
setName
(
user
.
getUsername
());
account
.
setLastSeen
(
new
Date
());
account
.
setEmail
(
user
.
getEmail
());
account
.
setPhoneNumber
(
user
.
getUsername
());
account
.
setPermission
(
15
);
if
(
user
.
getPromotionCode
()
!=
null
)
{
account
.
setPromotionCode
(
user
.
getPromotionCode
());
}
Account
inviter
=
accountRepository
.
findByPromotion
(
user
.
getPromotionCode
());
Account
inviter
=
accountRepository
.
findByPromotion
(
user
.
getPromotionCode
());
if
(
inviter
!=
null
)
{
if
(
inviter
!=
null
)
{
inviter
.
getPromotion
().
setInvitedUsers
(
inviter
.
getPromotion
().
getInvitedUsers
()
+
1
);
inviter
.
getPromotion
().
setInvitedUsers
(
inviter
.
getPromotion
().
getInvitedUsers
()
+
1
);
accountRepository
.
save
(
inviter
);
accountRepository
.
save
(
inviter
);
}
}
Account
pExisting
=
null
;
// 4. 创建 User
String
random
=
null
;
do
{
random
=
String
.
valueOf
((
int
)
((
Math
.
random
()
*
9
+
1
)
*
100000
));
pExisting
=
accountRepository
.
findByPromotion
(
random
);
}
while
(
pExisting
!=
null
);
Promotion
promotion
=
new
Promotion
();
promotion
.
setCode
(
random
);
promotion
.
setInvitedUsers
(
0
);
promotion
.
setCommission
(
0
);
account
.
setPromotion
(
promotion
);
account
.
setAllowedToCreateSubUser
(
true
);
List
<
String
>
whiteList
=
new
ArrayList
<>();
if
(
user
.
getWhiteList
()
!=
null
&&
user
.
getWhiteList
().
size
()
>
0
)
whiteList
.
addAll
(
user
.
getWhiteList
());
account
.
setWhiteList
(
whiteList
);
user
.
setEnabled
(
true
);
user
.
setEnabled
(
true
);
userService
.
create
(
new
com
.
edgec
.
browserbackend
.
auth
.
domain
.
User
(
user
));
userService
.
create
(
new
com
.
edgec
.
browserbackend
.
auth
.
domain
.
User
(
user
));
// 5. 封装用户账户 并 创建
Account
account
=
buildAccount
(
user
);
accountRepository
.
save
(
account
);
accountRepository
.
save
(
account
);
JSONObject
param
=
new
JSONObject
();
JSONObject
param
=
new
JSONObject
();
param
.
put
(
"newuser"
,
account
.
getName
());
param
.
put
(
"newuser"
,
account
.
getName
());
if
(
inviter
!=
null
)
if
(
inviter
!=
null
)
{
param
.
put
(
"referral"
,
inviter
.
getName
()
+
"("
+
user
.
getPromotionCode
()
+
")"
);
param
.
put
(
"referral"
,
inviter
.
getName
()
+
"("
+
user
.
getPromotionCode
()
+
")"
);
else
}
else
{
param
.
put
(
"referral"
,
"123456"
);
param
.
put
(
"referral"
,
"123456"
);
}
com
.
edgec
.
browserbackend
.
common
.
commons
.
utils
.
SmsUtils
.
sendNewAccountSms
(
"15919921106"
,
com
.
edgec
.
browserbackend
.
common
.
commons
.
utils
.
SmsUtils
.
SmsTemplateCode
.
NEWACCOUNT
,
param
);
com
.
edgec
.
browserbackend
.
common
.
commons
.
utils
.
SmsUtils
.
sendNewAccountSms
(
"15919921106"
,
com
.
edgec
.
browserbackend
.
common
.
commons
.
utils
.
SmsUtils
.
SmsTemplateCode
.
NEWACCOUNT
,
param
);
com
.
edgec
.
browserbackend
.
common
.
commons
.
utils
.
SmsUtils
.
sendNewAccountSms
(
"13570690305"
,
com
.
edgec
.
browserbackend
.
common
.
commons
.
utils
.
SmsUtils
.
SmsTemplateCode
.
NEWACCOUNT
,
param
);
com
.
edgec
.
browserbackend
.
common
.
commons
.
utils
.
SmsUtils
.
sendNewAccountSms
(
"13570690305"
,
com
.
edgec
.
browserbackend
.
common
.
commons
.
utils
.
SmsUtils
.
SmsTemplateCode
.
NEWACCOUNT
,
param
);
com
.
edgec
.
browserbackend
.
common
.
commons
.
utils
.
SmsUtils
.
sendNewAccountSms
(
"13802945832"
,
com
.
edgec
.
browserbackend
.
common
.
commons
.
utils
.
SmsUtils
.
SmsTemplateCode
.
NEWACCOUNT
,
param
);
paymentService
.
bankTransferInsertion
(
account
.
getName
(),
12
);
// 6. 使用手机注册就送 12 元体验金
List
<
String
>
list
=
new
ArrayList
<>();
list
.
add
(
"167"
);
list
.
add
(
"171"
);
list
.
add
(
"184"
);
list
.
add
(
"170"
);
if
(!
list
.
contains
(
user
.
getPhone
().
substring
(
0
,
3
)))
{
paymentService
.
bankTransferInsertion
(
account
.
getName
(),
12
);
}
log
.
info
(
"new account has been created: "
+
account
.
getName
());
log
.
info
(
"new account has been created: "
+
account
.
getName
());
// 7. 发送注册成功的邮件
notifyCustomerRegister
(
account
);
notifyCustomerRegister
(
account
);
return
account
;
return
account
;
}
}
public
Account
create
(
User
user
,
String
parentName
)
{
public
Account
create
(
User
user
,
String
parentName
)
{
// 1. 校验用户是否已注册
Account
existing
=
accountRepository
.
findByName
(
user
.
getUsername
()).
orElse
(
null
);
Account
existing
=
accountRepository
.
findByName
(
user
.
getUsername
()).
orElse
(
null
);
if
(
existing
!=
null
)
if
(
existing
!=
null
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMEEXIST
,
"account already exists: "
+
user
.
getUsername
());
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMEEXIST
,
"account already exists: "
+
user
.
getUsername
());
}
existing
=
accountRepository
.
findByPhoneNumber
(
user
.
getUsername
());
existing
=
accountRepository
.
findByPhoneNumber
(
user
.
getUsername
());
if
(
existing
!=
null
)
if
(
existing
!=
null
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMEEXIST
,
"account already exists: "
+
user
.
getUsername
());
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMEEXIST
,
"account already exists: "
+
user
.
getUsername
());
}
existing
=
accountRepository
.
findByEmail
(
user
.
getEmail
());
existing
=
accountRepository
.
findByEmail
(
user
.
getEmail
());
if
(
existing
!=
null
)
if
(
existing
!=
null
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
EMAILEXIST
,
"email already exists: "
+
user
.
getEmail
());
throw
new
ClientRequestException
(
AccountErrorCode
.
EMAILEXIST
,
"email already exists: "
+
user
.
getEmail
());
}
// 2. 发送校验邮件
emailService
.
sendEmailVerification
(
user
.
getUsername
(),
user
.
getEmail
(),
user
.
getVerificationCode
());
// 3. 创建 User
user
.
setEnabled
(
false
);
user
.
setEnabled
(
false
);
userService
.
create
(
new
com
.
edgec
.
browserbackend
.
auth
.
domain
.
User
(
user
));
// 4. 创建用户账户
Account
account
=
new
Account
();
Account
account
=
new
Account
();
account
.
setName
(
user
.
getUsername
());
account
.
setName
(
user
.
getUsername
());
account
.
setLastSeen
(
new
Date
());
account
.
setLastSeen
(
new
Date
());
account
.
setEmail
(
user
.
getEmail
());
account
.
setEmail
(
user
.
getEmail
());
if
(
StringUtils
.
isEmpty
(
parentName
))
{
if
(
StringUtils
.
isEmpty
(
parentName
))
{
account
.
setAllowedToCreateSubUser
(
true
);
account
.
setAllowedToCreateSubUser
(
true
);
account
.
setPermission
(
Integer
.
valueOf
(
"1111"
,
2
));
account
.
setPermission
(
Integer
.
valueOf
(
"1111"
,
2
));
...
@@ -540,42 +518,23 @@ public class AccountServiceImpl implements AccountService {
...
@@ -540,42 +518,23 @@ public class AccountServiceImpl implements AccountService {
account
.
setAllowedToCreateSubUser
(
user
.
isAllowedToCreateSubUser
());
account
.
setAllowedToCreateSubUser
(
user
.
isAllowedToCreateSubUser
());
account
.
setPermission
(
user
.
getPermission
());
account
.
setPermission
(
user
.
getPermission
());
}
}
account
.
setParent
(
parentName
);
account
.
setParent
(
parentName
);
emailService
.
sendEmailVerification
(
user
.
getUsername
(),
user
.
getEmail
(),
user
.
getVerificationCode
());
userService
.
create
(
new
com
.
edgec
.
browserbackend
.
auth
.
domain
.
User
(
user
));
accountRepository
.
save
(
account
);
accountRepository
.
save
(
account
);
log
.
info
(
"new account has been created: "
+
account
.
getName
());
log
.
info
(
"new account has been created: "
+
account
.
getName
());
// SmsUtils.notifyNewUserRegistered();
// SmsUtils.notifyNewUserRegistered();
// 5. 发送注册成功的邮件
notifyCustomerRegister
(
account
);
notifyCustomerRegister
(
account
);
return
account
;
return
account
;
}
}
private
void
notifyCustomerRegister
(
Account
contactUs
)
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
"Name: "
+
contactUs
.
getName
()
+
"<br/>"
);
sb
.
append
(
"Email: "
+
contactUs
.
getEmail
()
+
"<br/>"
);
sb
.
append
(
"Phone: "
+
contactUs
.
getPhoneNumber
()
+
"<br/>"
);
sb
.
append
(
"Company: "
+
contactUs
.
getCompanyName
()
+
"<br/>"
);
sb
.
append
(
"Title: "
+
contactUs
.
getJobTitle
()
+
"<br/>"
);
try
{
emailService
.
sendHtmlMail
(
"sales@cloudam.io"
,
"新客户注册:"
+
contactUs
.
getName
(),
sb
.
toString
());
}
catch
(
Exception
e
)
{
log
.
error
(
"sending email fails on customer regisration"
,
e
);
}
}
@Override
@Override
public
List
<
UserDto
>
getAllDesendentUsers
(
String
name
,
int
level
)
{
public
List
<
UserDto
>
getAllDesendentUsers
(
String
name
,
int
level
)
{
if
(
level
==
-
1
)
{
if
(
level
==
-
1
)
{
//find its parent.
//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
))
if
(
StringUtils
.
isEmpty
(
parent
))
return
Arrays
.
asList
();
return
Arrays
.
asList
();
...
@@ -704,8 +663,6 @@ public class AccountServiceImpl implements AccountService {
...
@@ -704,8 +663,6 @@ public class AccountServiceImpl implements AccountService {
@Override
@Override
public
void
createSubUsers
(
String
name
,
SubUsersRequestDto
subUsersRequestDto
)
{
public
void
createSubUsers
(
String
name
,
SubUsersRequestDto
subUsersRequestDto
)
{
Account
existing
=
accountRepository
.
findByName
(
name
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
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
()))
if
(!
existing
.
isAllowedToCreateSubUser
()
&&
!
StringUtils
.
isEmpty
(
existing
.
getParent
()))
throw
new
ClientRequestException
(
AccountErrorCode
.
NOTALLOWEDTOCREATESUBUSER
,
"Not allowed to create sub user"
);
throw
new
ClientRequestException
(
AccountErrorCode
.
NOTALLOWEDTOCREATESUBUSER
,
"Not allowed to create sub user"
);
...
@@ -1200,4 +1157,87 @@ public class AccountServiceImpl implements AccountService {
...
@@ -1200,4 +1157,87 @@ public class AccountServiceImpl implements AccountService {
return
false
;
return
false
;
}
}
}
}
private
void
notifyCustomerRegister
(
Account
contactUs
)
{
StringBuilder
sb
=
new
StringBuilder
();
sb
.
append
(
"Name: "
+
contactUs
.
getName
()
+
"<br/>"
);
sb
.
append
(
"Email: "
+
contactUs
.
getEmail
()
+
"<br/>"
);
sb
.
append
(
"Phone: "
+
contactUs
.
getPhoneNumber
()
+
"<br/>"
);
sb
.
append
(
"Company: "
+
contactUs
.
getCompanyName
()
+
"<br/>"
);
sb
.
append
(
"Title: "
+
contactUs
.
getJobTitle
()
+
"<br/>"
);
try
{
emailService
.
sendHtmlMail
(
"sales@cloudam.io"
,
"新客户注册:"
+
contactUs
.
getName
(),
sb
.
toString
());
}
catch
(
Exception
e
)
{
log
.
error
(
"sending email fails on customer regisration"
,
e
);
}
}
@NotNull
private
UserPrePaidBilling
getUserPrePaidBilling
(
String
name
,
float
money
,
IpChargeRequestDto
requestDto
,
Account
account
,
UserBalance
userBalance
)
{
UserPrePaidBilling
bill
=
new
UserPrePaidBilling
();
if
(
account
.
getParent
()
!=
null
)
{
bill
.
setAdministrator
(
account
.
getParent
());
}
else
{
bill
.
setAdministrator
(
account
.
getName
());
}
bill
.
setTradeNo
(
new
SimpleDateFormat
(
"yyyyMMddHHmmss"
).
format
(
new
Date
())
+
SmsUtils
.
createRandom
(
true
,
4
));
bill
.
setChargeType
(
requestDto
.
getChargeType
());
if
(
requestDto
.
getChargeIp
()
!=
null
)
{
bill
.
setChargeIp
(
requestDto
.
getChargeIp
());
}
bill
.
setAmount
(
requestDto
.
getAmount
());
bill
.
setUnit
(
requestDto
.
getUnit
());
bill
.
setPeriod
(
requestDto
.
getPeriod
());
bill
.
setPayMethod
(
requestDto
.
getPayMethod
());
bill
.
setUsername
(
name
);
bill
.
setTotal
(
money
);
bill
.
setStatus
(
BillStatus
.
PAID
);
bill
.
setPrepaid
(
true
);
bill
.
setTimestamp
(
Instant
.
now
().
toEpochMilli
());
final
YearMonth
lastmonth
=
YearMonth
.
now
();
int
monthValue
=
lastmonth
.
getMonthValue
();
int
year
=
lastmonth
.
getYear
();
bill
.
setYear
(
year
);
bill
.
setMonth
(
monthValue
);
bill
.
setBalanced
(
userBalance
.
getBalanced
());
return
bill
;
}
@NotNull
private
Account
buildAccount
(
User
user
)
{
Account
account
=
new
Account
();
account
.
setName
(
user
.
getUsername
());
account
.
setLastSeen
(
new
Date
());
account
.
setEmail
(
user
.
getEmail
());
account
.
setPhoneNumber
(
user
.
getUsername
());
account
.
setPermission
(
15
);
if
(
user
.
getPromotionCode
()
!=
null
)
{
account
.
setPromotionCode
(
user
.
getPromotionCode
());
}
Account
pExisting
=
null
;
String
random
=
null
;
do
{
random
=
String
.
valueOf
((
int
)
((
Math
.
random
()
*
9
+
1
)
*
100000
));
pExisting
=
accountRepository
.
findByPromotion
(
random
);
}
while
(
pExisting
!=
null
);
Promotion
promotion
=
new
Promotion
();
promotion
.
setCode
(
random
);
promotion
.
setInvitedUsers
(
0
);
promotion
.
setCommission
(
0
);
account
.
setPromotion
(
promotion
);
account
.
setAllowedToCreateSubUser
(
true
);
List
<
String
>
whiteList
=
new
ArrayList
<>();
if
(
user
.
getWhiteList
()
!=
null
&&
user
.
getWhiteList
().
size
()
>
0
)
{
whiteList
.
addAll
(
user
.
getWhiteList
());
}
account
.
setWhiteList
(
whiteList
);
return
account
;
}
}
}
src/main/java/com/edgec/browserbackend/account/service/impl/PaymentServiceImpl.java
View file @
1ff453f7
...
@@ -26,6 +26,7 @@ import com.edgec.browserbackend.common.commons.error.ClientRequestException;
...
@@ -26,6 +26,7 @@ import com.edgec.browserbackend.common.commons.error.ClientRequestException;
import
com.edgec.browserbackend.wxpay.*
;
import
com.edgec.browserbackend.wxpay.*
;
import
com.google.gson.Gson
;
import
com.google.gson.Gson
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.jetbrains.annotations.NotNull
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
@@ -66,9 +67,8 @@ public class PaymentServiceImpl implements PaymentService {
...
@@ -66,9 +67,8 @@ public class PaymentServiceImpl implements PaymentService {
@Override
@Override
public
String
wechatPayCallback
(
String
tradno
)
{
public
String
wechatPayCallback
(
String
tradno
)
{
// 当微信回调该接口时,说明微信那边已经充值成功,这个时候需要更新系统中 userpayment 充值状态
UserPayment
byTradeNo
=
userPaymentRepository
.
findByTradeNo
(
tradno
);
UserPayment
byTradeNo
=
userPaymentRepository
.
findByTradeNo
(
tradno
);
if
(
byTradeNo
!=
null
&&
!
byTradeNo
.
isSucceed
())
{
if
(
byTradeNo
!=
null
&&
!
byTradeNo
.
isSucceed
())
{
wxCheckOrderStatus
(
byTradeNo
.
getTradeNo
(),
0
);
wxCheckOrderStatus
(
byTradeNo
.
getTradeNo
(),
0
);
}
}
...
@@ -82,7 +82,6 @@ public class PaymentServiceImpl implements PaymentService {
...
@@ -82,7 +82,6 @@ public class PaymentServiceImpl implements PaymentService {
@Override
@Override
public
void
alipaCallback
(
String
tradno
)
{
public
void
alipaCallback
(
String
tradno
)
{
UserPayment
byTradeNo
=
userPaymentRepository
.
findByTradeNo
(
tradno
);
UserPayment
byTradeNo
=
userPaymentRepository
.
findByTradeNo
(
tradno
);
if
(
byTradeNo
!=
null
&&
!
byTradeNo
.
isSucceed
())
{
if
(
byTradeNo
!=
null
&&
!
byTradeNo
.
isSucceed
())
{
aliCheckOrderStatus
(
byTradeNo
.
getTradeNo
(),
0
);
aliCheckOrderStatus
(
byTradeNo
.
getTradeNo
(),
0
);
}
}
...
@@ -90,130 +89,53 @@ public class PaymentServiceImpl implements PaymentService {
...
@@ -90,130 +89,53 @@ public class PaymentServiceImpl implements PaymentService {
@Override
@Override
public
UserPaymentDto
wxCheckOrderStatus
(
String
tradeno
,
int
chargeType
)
{
public
UserPaymentDto
wxCheckOrderStatus
(
String
tradeno
,
int
chargeType
)
{
// 校验数据
UserPaymentDto
result
=
new
UserPaymentDto
();
UserPaymentDto
result
=
new
UserPaymentDto
();
result
.
setPaid
(
false
);
result
.
setPaid
(
false
);
if
(
StringUtils
.
isBlank
(
tradeno
))
if
(
StringUtils
.
isBlank
(
tradeno
))
{
return
result
;
return
result
;
try
{
}
UserPayment
byTradeNo
=
userPaymentRepository
.
findByTradeNo
(
tradeno
);
if
(
byTradeNo
==
null
)
return
result
;
if
(
PaymentMethod
.
ALIPAY
.
equals
(
byTradeNo
.
getPaymentMethod
()))
return
aliCheckOrderStatus
(
tradeno
,
chargeType
);
UserBalance
balance
=
userBalanceRepository
.
findById
(
byTradeNo
.
getUsername
()).
orElse
(
null
);
if
(
balance
!=
null
)
UserPayment
byTradeNo
=
userPaymentRepository
.
findByTradeNo
(
tradeno
);
result
.
setBalance
(
Math
.
round
(
balance
.
getBalanced
()));
if
(
byTradeNo
==
null
)
{
return
result
;
}
if
(
byTradeNo
.
isSucceed
())
{
result
.
setPaid
(
true
);
return
result
;
}
Account
byName
=
accountService
.
findByName
(
byTradeNo
.
getUsername
());
if
(
byTradeNo
==
null
)
// 如果订单是用支付宝支付的,则调用支付宝相关的代码
return
result
;
if
(
PaymentMethod
.
ALIPAY
.
equals
(
byTradeNo
.
getPaymentMethod
()))
{
if
(
byTradeNo
.
isSucceed
())
{
return
aliCheckOrderStatus
(
tradeno
,
chargeType
);
result
.
setPaid
(
true
);
}
return
result
;
}
Account
byName
=
accountService
.
findByName
(
byTradeNo
.
getUsername
());
try
{
if
(
byName
==
null
)
UserBalance
balance
=
getUserBalance
(
result
,
byTradeNo
);
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
,
"account does not exist: "
+
byTradeNo
.
getUsername
());
// 1. 构建微信支付订单查询数据,并获取微信支付订单信息
// 获取微信支付订单存在失败的情况,所以针对失败情况去使用定时任务来重新调用本方法
boolean
isVpsClient
=
true
;
boolean
isVpsClient
=
true
;
WXPayConfig
ourWxPayConfig
=
isVpsClient
?
new
FGLWxConfig
()
:
new
CloudamWxConfig
();
WXPayConfig
ourWxPayConfig
=
isVpsClient
?
new
FGLWxConfig
()
:
new
CloudamWxConfig
();
WXPay
wxPay
=
new
WXPay
(
ourWxPayConfig
);
WXPay
wxPay
=
new
WXPay
(
ourWxPayConfig
);
Map
<
String
,
String
>
data
=
encapsulateWxOrderDataMap
(
tradeno
,
ourWxPayConfig
);
Map
<
String
,
String
>
data
=
new
HashMap
<>();
data
.
put
(
"appid"
,
ourWxPayConfig
.
getAppID
());
data
.
put
(
"mch_id"
,
ourWxPayConfig
.
getMchID
());
//商户号
data
.
put
(
"out_trade_no"
,
tradeno
);
//交易号
data
.
put
(
"nonce_str"
,
SmsUtils
.
createRandom
(
false
,
24
));
// 随机字符串小于32位
String
s
=
WXPayUtil
.
generateSignature
(
data
,
ourWxPayConfig
.
getKey
());
//签名
data
.
put
(
"sign"
,
s
);
Map
<
String
,
String
>
respData
=
wxPay
.
orderQuery
(
data
);
Map
<
String
,
String
>
respData
=
wxPay
.
orderQuery
(
data
);
if
(
respData
.
get
(
"return_code"
).
equals
(
"SUCCESS"
)
&&
respData
.
get
(
"return_msg"
).
equals
(
"OK"
)
&&
"SUCCESS"
.
equals
(
respData
.
get
(
"result_code"
)))
{
if
(
respData
.
get
(
"return_code"
).
equals
(
"SUCCESS"
)
&&
respData
.
get
(
"return_msg"
).
equals
(
"OK"
)
&&
"SUCCESS"
.
equals
(
respData
.
get
(
"result_code"
)))
{
/*
/**
* SUCCESS—支付成功
*
* REFUND—转入退款
SUCCESS—支付成功
* NOTPAY—未支付
* CLOSED—已关闭
REFUND—转入退款
* REVOKED—已撤销(付款码支付)
* USERPAYING--用户支付中(付款码支付)
NOTPAY—未支付
* PAYERROR--支付失败(其他原因,如银行返回失败)
CLOSED—已关闭
REVOKED—已撤销(付款码支付)
USERPAYING--用户支付中(付款码支付)
PAYERROR--支付失败(其他原因,如银行返回失败) *
*/
*/
result
.
setStatus
(
respData
.
get
(
"trade_state"
));
result
.
setStatus
(
respData
.
get
(
"trade_state"
));
// 2. 如果订单支付成功(具体业务逻辑)
if
(
"SUCCESS"
.
equals
(
respData
.
get
(
"trade_state"
)))
{
if
(
"SUCCESS"
.
equals
(
respData
.
get
(
"trade_state"
)))
{
byTradeNo
.
setSucceed
(
true
);
dealPaySuccess
(
chargeType
,
result
,
byTradeNo
,
byName
,
balance
);
boolean
b
=
userPaymentRepository
.
updatePaymentResult
(
byTradeNo
,
true
);
if
(
b
)
{
if
(
balance
==
null
)
{
balance
=
new
UserBalance
();
balance
.
setUsername
(
byTradeNo
.
getUsername
());
}
PayBack
payBack
=
payBackRepository
.
findByPay
(
byTradeNo
.
getAmount
());
if
(
payBack
==
null
)
payBack
=
new
PayBack
();
if
(
chargeType
==
0
)
{
UserPrePaidBilling
bill
=
new
UserPrePaidBilling
();
Account
account
=
accountRepository
.
findByName
(
byTradeNo
.
getUsername
()).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
!=
null
&&
account
.
getParent
()
!=
null
)
bill
.
setAdministrator
(
account
.
getParent
());
else
bill
.
setAdministrator
(
account
.
getName
());
bill
.
setTradeNo
(
new
SimpleDateFormat
(
"yyyyMMddHHmmss"
).
format
(
new
Date
())
+
SmsUtils
.
createRandom
(
true
,
4
));
bill
.
setChargeType
(
0
);
bill
.
setAmount
(
0
);
bill
.
setUnit
(
null
);
bill
.
setPeriod
(
0
);
bill
.
setPayMethod
(
2
);
bill
.
setUsername
(
byTradeNo
.
getUsername
());
bill
.
setTotal
((
float
)
byTradeNo
.
getAmount
());
bill
.
setStatus
(
BillStatus
.
PAID
);
bill
.
setPrepaid
(
true
);
bill
.
setTimestamp
(
Instant
.
now
().
toEpochMilli
());
bill
.
setBalanced
(
balance
.
getBalanced
()
+
byTradeNo
.
getAmount
()
+
payBack
.
getBack
());
final
YearMonth
lastmonth
=
YearMonth
.
now
();
int
monthValue
=
lastmonth
.
getMonthValue
();
int
year
=
lastmonth
.
getYear
();
bill
.
setYear
(
year
);
bill
.
setMonth
(
monthValue
);
userPrePaidBillingRepository
.
save
(
bill
);
}
balance
.
setBalanced
(
balance
.
getBalanced
()
+
byTradeNo
.
getAmount
()
+
payBack
.
getBack
());
userBalanceRepository
.
save
(
balance
);
if
(
byName
.
getPromotionCode
()
!=
null
)
{
Account
account
=
accountRepository
.
findByPromotion
(
byName
.
getPromotionCode
());
if
(
account
!=
null
)
{
account
.
getPromotion
().
setCommission
(
account
.
getPromotion
().
getCommission
()
+
byTradeNo
.
getAmount
());
accountRepository
.
save
(
account
);
}
}
}
result
.
setBalance
(
Math
.
round
(
balance
.
getBalanced
()));
result
.
setPaid
(
true
);
return
result
;
return
result
;
}
}
}
}
...
@@ -243,42 +165,33 @@ public class PaymentServiceImpl implements PaymentService {
...
@@ -243,42 +165,33 @@ public class PaymentServiceImpl implements PaymentService {
public
UserPaymentDto
aliCheckOrderStatus
(
String
tradno
,
int
chargeType
)
{
public
UserPaymentDto
aliCheckOrderStatus
(
String
tradno
,
int
chargeType
)
{
UserPaymentDto
result
=
new
UserPaymentDto
();
UserPaymentDto
result
=
new
UserPaymentDto
();
result
.
setPaid
(
false
);
result
.
setPaid
(
false
);
if
(
StringUtils
.
isBlank
(
tradno
))
if
(
StringUtils
.
isBlank
(
tradno
))
{
return
result
;
return
result
;
try
{
}
UserPayment
byTradeNo
=
userPaymentRepository
.
findByTradeNo
(
tradno
);
UserPayment
byTradeNo
=
userPaymentRepository
.
findByTradeNo
(
tradno
);
UserBalance
balance
=
userBalanceRepository
.
findById
(
byTradeNo
.
getUsername
()).
orElse
(
null
);
if
(
byTradeNo
==
null
)
{
return
result
;
}
if
(
byTradeNo
.
isSucceed
())
{
result
.
setPaid
(
true
);
return
result
;
}
if
(
balance
!=
null
)
Account
byName
=
accountService
.
findByName
(
byTradeNo
.
getUsername
());
result
.
setBalance
(
Math
.
round
(
balance
.
getBalanced
()));
if
(
byTradeNo
==
null
)
try
{
return
result
;
UserBalance
balance
=
getUserBalance
(
result
,
byTradeNo
);
if
(
byTradeNo
.
isSucceed
())
{
result
.
setPaid
(
true
);
return
result
;
}
Account
byName
=
accountService
.
findByName
(
byTradeNo
.
getUsername
());
if
(
byName
==
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
,
"account does not exist: "
+
byTradeNo
.
getUsername
());
boolean
isVpsClient
=
true
;
boolean
isVpsClient
=
true
;
AlipayConfig
alipayConfig
=
new
VpsAlipayConfig
();
AlipayConfig
alipayConfig
=
new
VpsAlipayConfig
();
AlipayClient
alipayClient
=
new
DefaultAlipayClient
(
alipayConfig
.
getURL
(),
alipayConfig
.
getAPPID
(),
alipayConfig
.
getAPP_PRIVATE_KEY
(),
AlipayClient
alipayClient
=
new
DefaultAlipayClient
(
alipayConfig
.
getURL
(),
alipayConfig
.
getAPPID
(),
"json"
,
alipayConfig
.
getCHARSET
(),
alipayConfig
.
getALIPAY_PUBLIC_KEY
(),
alipayConfig
.
getSIGN_TYPE
());
alipayConfig
.
getAPP_PRIVATE_KEY
(),
"json"
,
alipayConfig
.
getCHARSET
(),
alipayConfig
.
getALIPAY_PUBLIC_KEY
(),
alipayConfig
.
getSIGN_TYPE
());
AlipayTradeQueryRequest
alipayRequest
=
new
AlipayTradeQueryRequest
(
);
AlipayTradeQueryRequest
alipayRequest
=
buildAlipayTradeQueryRequest
(
byTradeNo
);
String
out_trade_no
=
byTradeNo
.
getTradeNo
();
alipayRequest
.
setBizContent
(
""
+
"{"
+
"\"out_trade_no\":\""
+
out_trade_no
+
"\""
+
"}"
);
AlipayTradeQueryResponse
response
=
alipayClient
.
execute
(
alipayRequest
);
AlipayTradeQueryResponse
response
=
alipayClient
.
execute
(
alipayRequest
);
log
.
info
(
"ali order status :"
+
JSONObject
.
toJSONString
(
response
));
log
.
info
(
"ali order status :"
+
JSONObject
.
toJSONString
(
response
));
if
(
response
.
getTradeStatus
()
==
null
)
{
if
(
response
.
getTradeStatus
()
==
null
)
{
result
.
setPaid
(
false
);
result
.
setPaid
(
false
);
...
@@ -288,63 +201,8 @@ public class PaymentServiceImpl implements PaymentService {
...
@@ -288,63 +201,8 @@ public class PaymentServiceImpl implements PaymentService {
result
.
setStatus
(
convertAlipayStatus
(
response
.
getTradeStatus
()));
result
.
setStatus
(
convertAlipayStatus
(
response
.
getTradeStatus
()));
if
(
"TRADE_SUCCESS"
.
equals
(
response
.
getTradeStatus
())
||
"TRADE_FINISHED"
.
equals
(
response
.
getTradeStatus
()))
{
if
(
"TRADE_SUCCESS"
.
equals
(
response
.
getTradeStatus
())
||
"TRADE_FINISHED"
.
equals
(
response
.
getTradeStatus
()))
{
byTradeNo
.
setSucceed
(
true
);
// 2. 如果订单支付成功(具体业务逻辑)
boolean
b
=
userPaymentRepository
.
updatePaymentResult
(
byTradeNo
,
true
);
dealPaySuccess
(
chargeType
,
result
,
byTradeNo
,
byName
,
balance
);
if
(
b
)
{
if
(
balance
==
null
)
{
balance
=
new
UserBalance
();
balance
.
setUsername
(
byTradeNo
.
getUsername
());
}
PayBack
payBack
=
payBackRepository
.
findByPay
(
byTradeNo
.
getAmount
());
if
(
payBack
==
null
)
payBack
=
new
PayBack
();
if
(
chargeType
==
0
)
{
UserPrePaidBilling
bill
=
new
UserPrePaidBilling
();
Account
account
=
accountRepository
.
findByName
(
byTradeNo
.
getUsername
()).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
!=
null
&&
account
.
getParent
()
!=
null
)
bill
.
setAdministrator
(
account
.
getParent
());
else
bill
.
setAdministrator
(
account
.
getName
());
bill
.
setTradeNo
(
new
SimpleDateFormat
(
"yyyyMMddHHmmss"
).
format
(
new
Date
())
+
SmsUtils
.
createRandom
(
true
,
4
));
bill
.
setChargeType
(
0
);
bill
.
setAmount
(
0
);
bill
.
setUnit
(
null
);
bill
.
setPeriod
(
0
);
bill
.
setPayMethod
(
1
);
bill
.
setUsername
(
byTradeNo
.
getUsername
());
bill
.
setTotal
((
float
)
byTradeNo
.
getAmount
());
bill
.
setStatus
(
BillStatus
.
PAID
);
bill
.
setPrepaid
(
true
);
bill
.
setTimestamp
(
Instant
.
now
().
toEpochMilli
());
bill
.
setBalanced
(
balance
.
getBalanced
()
+
byTradeNo
.
getAmount
()
+
payBack
.
getBack
());
final
YearMonth
lastmonth
=
YearMonth
.
now
();
int
monthValue
=
lastmonth
.
getMonthValue
();
int
year
=
lastmonth
.
getYear
();
bill
.
setYear
(
year
);
bill
.
setMonth
(
monthValue
);
userPrePaidBillingRepository
.
save
(
bill
);
}
balance
.
setBalanced
(
balance
.
getBalanced
()
+
byTradeNo
.
getAmount
()
+
payBack
.
getBack
());
userBalanceRepository
.
save
(
balance
);
if
(
byName
.
getPromotionCode
()
!=
null
)
{
Account
account
=
accountRepository
.
findByPromotion
(
byName
.
getPromotionCode
());
if
(
account
!=
null
)
{
account
.
getPromotion
().
setCommission
(
account
.
getPromotion
().
getCommission
()
+
byTradeNo
.
getAmount
());
accountRepository
.
save
(
account
);
}
}
}
result
.
setBalance
(
Math
.
round
(
balance
.
getBalanced
()));
result
.
setPaid
(
true
);
return
result
;
return
result
;
}
}
...
@@ -356,48 +214,45 @@ public class PaymentServiceImpl implements PaymentService {
...
@@ -356,48 +214,45 @@ public class PaymentServiceImpl implements PaymentService {
return
result
;
return
result
;
}
}
@NotNull
private
UserBalance
getUserBalance
(
UserPaymentDto
result
,
UserPayment
byTradeNo
)
{
UserBalance
balance
=
userBalanceRepository
.
findById
(
byTradeNo
.
getUsername
()).
orElse
(
null
);
if
(
balance
!=
null
)
{
result
.
setBalance
(
Math
.
round
(
balance
.
getBalanced
()));
}
if
(
balance
==
null
)
{
balance
=
new
UserBalance
();
balance
.
setUsername
(
byTradeNo
.
getUsername
());
}
return
balance
;
}
@Override
@Override
public
String
alipayPutPayOrder
(
String
username
,
int
amount
,
String
by
)
{
public
String
alipayPutPayOrder
(
String
username
,
int
amount
,
String
by
)
{
Account
byName
=
accountService
.
findByName
(
username
);
Account
byName
=
accountService
.
findByName
(
username
);
if
(
byName
==
null
)
if
(
byName
.
getPermission
()
<
4
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
,
"account does not exist: "
+
username
);
if
(
byName
.
getPermission
()
<
4
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
,
"account does not have permission: "
+
username
);
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
,
"account does not have permission: "
+
username
);
boolean
isVpsClient
=
true
;
}
UserPayment
internalOrder
=
new
UserPayment
();
internalOrder
.
setAmount
(
amount
);
internalOrder
.
setPaymentMethod
(
PaymentMethod
.
ALIPAY
);
internalOrder
.
setSucceed
(
false
);
internalOrder
.
setUsername
(
username
);
try
{
try
{
// 创建并封装 UserPayment 信息
UserPayment
internalOrder
=
getUserPayment
(
username
,
amount
,
PaymentMethod
.
ALIPAY
);
// 获取支付宝支付的配置信息
boolean
isVpsClient
=
true
;
AlipayConfig
alipayConfig
=
isVpsClient
?
new
VpsAlipayConfig
()
:
new
CloudamAlipayConfig
();
AlipayConfig
alipayConfig
=
isVpsClient
?
new
VpsAlipayConfig
()
:
new
CloudamAlipayConfig
();
AlipayClient
alipayClient
=
new
DefaultAlipayClient
(
alipayConfig
.
getURL
(),
alipayConfig
.
getAPPID
(),
alipayConfig
.
getAPP_PRIVATE_KEY
(),
// 获取支付宝的 client
"json"
,
alipayConfig
.
getCHARSET
(),
alipayConfig
.
getALIPAY_PUBLIC_KEY
(),
alipayConfig
.
getSIGN_TYPE
());
AlipayClient
alipayClient
=
new
DefaultAlipayClient
(
alipayConfig
.
getURL
(),
alipayConfig
.
getAPPID
(),
alipayConfig
.
getAPP_PRIVATE_KEY
(),
"json"
,
alipayConfig
.
getCHARSET
(),
AlipayTradePagePayRequest
alipayRequest
=
new
AlipayTradePagePayRequest
();
alipayConfig
.
getALIPAY_PUBLIC_KEY
(),
alipayConfig
.
getSIGN_TYPE
());
alipayRequest
.
setReturnUrl
(
alipayConfig
.
getReturnUrl
(
isVpsClient
,
by
)
+
internalOrder
.
getTradeNo
());
// 构建请求信息 并设置请求成功的回调 url
alipayRequest
.
setNotifyUrl
(
alipayConfig
.
getNOTIFY_URL
()
+
internalOrder
.
getTradeNo
());
AlipayTradePagePayRequest
alipayRequest
=
buildAlipayTradePagePayRequest
(
amount
,
by
,
internalOrder
,
isVpsClient
,
alipayConfig
);
String
out_trade_no
=
internalOrder
.
getTradeNo
();
// 调用 支付宝支付接口
String
product_code
=
"FAST_INSTANT_TRADE_PAY"
;
String
total_amount
=
""
+
amount
+
".00"
;
String
subject
=
"订单"
+
out_trade_no
;
String
body
=
""
;
alipayRequest
.
setBizContent
(
""
+
"{"
+
"\"out_trade_no\":\""
+
out_trade_no
+
"\","
+
"\"product_code\":\""
+
product_code
+
"\","
+
"\"total_amount\":\""
+
total_amount
+
"\","
+
"\"subject\":\""
+
subject
+
"\","
+
"\"body\":\""
+
body
+
"\""
+
"}"
);
AlipayTradePagePayResponse
response
=
alipayClient
.
pageExecute
(
alipayRequest
);
AlipayTradePagePayResponse
response
=
alipayClient
.
pageExecute
(
alipayRequest
);
// 将支付信息保存到 userPayment
internalOrder
.
setTransId
(
response
.
getTradeNo
());
internalOrder
.
setTransId
(
response
.
getTradeNo
());
userPaymentRepository
.
save
(
internalOrder
);
userPaymentRepository
.
save
(
internalOrder
);
...
@@ -412,44 +267,31 @@ public class PaymentServiceImpl implements PaymentService {
...
@@ -412,44 +267,31 @@ public class PaymentServiceImpl implements PaymentService {
@Override
@Override
public
UserPaymentDto
wxPutPayOrder
(
String
username
,
int
amount
)
{
public
UserPaymentDto
wxPutPayOrder
(
String
username
,
int
amount
)
{
Account
byName
=
accountService
.
findByName
(
username
);
Account
byName
=
accountService
.
findByName
(
username
);
if
(
byName
==
null
)
if
(
byName
.
getPermission
()
<
4
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
,
"account does not exist: "
+
username
);
if
(
byName
.
getPermission
()
<
4
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
,
"account does not have permission: "
+
username
);
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
,
"account does not have permission: "
+
username
);
}
boolean
isVpsClient
=
true
;
UserPayment
internalOrder
=
new
UserPayment
();
internalOrder
.
setAmount
(
amount
);
internalOrder
.
setPaymentMethod
(
PaymentMethod
.
WECHAT
);
internalOrder
.
setSucceed
(
false
);
internalOrder
.
setUsername
(
username
);
try
{
try
{
// 创建并封装 UserPayment 信息
UserPayment
internalOrder
=
getUserPayment
(
username
,
amount
,
PaymentMethod
.
WECHAT
);
// 回调的url
String
notifyUrl
=
AccountController
.
WECHAT_PAY_CALLBACK_URL
+
internalOrder
.
getTradeNo
();
String
notifyUrl
=
AccountController
.
WECHAT_PAY_CALLBACK_URL
+
internalOrder
.
getTradeNo
();
// 获取微信支付的配置信息
boolean
isVpsClient
=
true
;
WXPayConfig
ourWxPayConfig
=
isVpsClient
?
new
FGLWxConfig
()
:
new
CloudamWxConfig
();
WXPayConfig
ourWxPayConfig
=
isVpsClient
?
new
FGLWxConfig
()
:
new
CloudamWxConfig
();
WXPay
wxPay
=
new
WXPay
(
ourWxPayConfig
);
Map
<
String
,
String
>
data
=
new
HashMap
<>();
// 封装请求 微信支付 的数据
data
.
put
(
"appid"
,
ourWxPayConfig
.
getAppID
());
Map
<
String
,
String
>
data
=
encapsulateDataMap
(
amount
,
internalOrder
,
notifyUrl
,
ourWxPayConfig
);
data
.
put
(
"mch_id"
,
ourWxPayConfig
.
getMchID
());
//商户号
data
.
put
(
"body"
,
ourWxPayConfig
.
getBody
());
data
.
put
(
"trade_type"
,
"NATIVE"
);
//NATIVE 扫码支付
data
.
put
(
"notify_url"
,
notifyUrl
);
//回调地址
data
.
put
(
"spbill_create_ip"
,
"127.0.0.1"
);
//终端ip
data
.
put
(
"total_fee"
,
""
+
amount
*
100
);
//订单总金额
data
.
put
(
"fee_type"
,
"CNY"
);
//默认人民币
data
.
put
(
"out_trade_no"
,
internalOrder
.
getTradeNo
());
//交易号
data
.
put
(
"nonce_str"
,
internalOrder
.
getTradeNo
());
// 随机字符串小于32位
String
s
=
WXPayUtil
.
generateSignature
(
data
,
ourWxPayConfig
.
getKey
());
//签名
data
.
put
(
"sign"
,
s
);
// 调用微信支付接口,支付成功后会回调 notifyUrl, 并返回支付结果
WXPay
wxPay
=
new
WXPay
(
ourWxPayConfig
);
Map
<
String
,
String
>
respData
=
wxPay
.
unifiedOrder
(
data
);
Map
<
String
,
String
>
respData
=
wxPay
.
unifiedOrder
(
data
);
if
(
respData
.
get
(
"return_code"
).
equals
(
"SUCCESS"
)
&&
respData
.
get
(
"result_code"
).
equals
(
"SUCCESS"
))
{
if
(
respData
.
get
(
"return_code"
).
equals
(
"SUCCESS"
)
&&
respData
.
get
(
"result_code"
).
equals
(
"SUCCESS"
))
{
// 若微信支付成功,则将支付信息保存到 userPayment
userPaymentRepository
.
save
(
internalOrder
);
userPaymentRepository
.
save
(
internalOrder
);
UserPaymentDto
result
=
new
UserPaymentDto
();
UserPaymentDto
result
=
new
UserPaymentDto
();
result
.
setTradeNo
(
internalOrder
.
getTradeNo
());
result
.
setTradeNo
(
internalOrder
.
getTradeNo
());
...
@@ -478,7 +320,7 @@ public class PaymentServiceImpl implements PaymentService {
...
@@ -478,7 +320,7 @@ public class PaymentServiceImpl implements PaymentService {
UserBalance
userBalance
=
userBalanceRepository
.
findById
(
username
).
orElse
(
null
);
UserBalance
userBalance
=
userBalanceRepository
.
findById
(
username
).
orElse
(
null
);
UserPrePaidBilling
bill
=
new
UserPrePaidBilling
();
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
)
if
(
account
!=
null
&&
account
.
getParent
()
!=
null
)
bill
.
setAdministrator
(
account
.
getParent
());
bill
.
setAdministrator
(
account
.
getParent
());
else
else
...
@@ -685,66 +527,49 @@ public class PaymentServiceImpl implements PaymentService {
...
@@ -685,66 +527,49 @@ public class PaymentServiceImpl implements PaymentService {
}
}
}
}
@Deprecated
@Override
@Override
public
UserPaymentDto
wxCheckOrderStatus
(
String
tradeno
)
{
public
UserPaymentDto
wxCheckOrderStatus
(
String
tradeno
)
{
UserPaymentDto
result
=
new
UserPaymentDto
();
UserPaymentDto
result
=
new
UserPaymentDto
();
result
.
setPaid
(
false
);
result
.
setPaid
(
false
);
try
{
UserPayment
byTradeNo
=
userPaymentRepository
.
findByTradeNo
(
tradeno
);
UserPayment
byTradeNo
=
userPaymentRepository
.
findByTradeNo
(
tradeno
);
if
(
byTradeNo
==
null
)
return
result
;
if
(
byTradeNo
.
isSucceed
())
{
result
.
setPaid
(
true
);
return
result
;
}
if
(
PaymentMethod
.
ALIPAY
.
equals
(
byTradeNo
.
getPaymentMethod
()))
Account
byName
=
accountService
.
findByName
(
byTradeNo
.
getUsername
());
return
aliCheckOrderStatus
(
tradeno
);
if
(
PaymentMethod
.
ALIPAY
.
equals
(
byTradeNo
.
getPaymentMethod
()))
{
return
aliCheckOrderStatus
(
tradeno
);
}
try
{
UserBalance
balance
=
userBalanceRepository
.
findById
(
byTradeNo
.
getUsername
()).
orElse
(
null
);
UserBalance
balance
=
userBalanceRepository
.
findById
(
byTradeNo
.
getUsername
()).
orElse
(
null
);
if
(
balance
!=
null
)
if
(
balance
!=
null
)
{
result
.
setBalance
(
Math
.
round
(
balance
.
getBalanced
()));
result
.
setBalance
(
Math
.
round
(
balance
.
getBalanced
()));
if
(
byTradeNo
==
null
)
return
result
;
if
(
byTradeNo
.
isSucceed
())
{
result
.
setPaid
(
true
);
return
result
;
}
}
Account
byName
=
accountService
.
findByName
(
byTradeNo
.
getUsername
());
if
(
byName
==
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
,
"account does not exist: "
+
byTradeNo
.
getUsername
());
boolean
isVpsClient
=
true
;
boolean
isVpsClient
=
true
;
WXPayConfig
ourWxPayConfig
=
isVpsClient
?
new
FGLWxConfig
()
:
new
CloudamWxConfig
();
WXPayConfig
ourWxPayConfig
=
isVpsClient
?
new
FGLWxConfig
()
:
new
CloudamWxConfig
();
WXPay
wxPay
=
new
WXPay
(
ourWxPayConfig
);
WXPay
wxPay
=
new
WXPay
(
ourWxPayConfig
);
Map
<
String
,
String
>
data
=
encapsulateWxOrderDataMap
(
tradeno
,
ourWxPayConfig
);
Map
<
String
,
String
>
data
=
new
HashMap
<>();
data
.
put
(
"appid"
,
ourWxPayConfig
.
getAppID
());
data
.
put
(
"mch_id"
,
ourWxPayConfig
.
getMchID
());
//商户号
data
.
put
(
"out_trade_no"
,
tradeno
);
//交易号
data
.
put
(
"nonce_str"
,
SmsUtils
.
createRandom
(
false
,
24
));
// 随机字符串小于32位
String
s
=
WXPayUtil
.
generateSignature
(
data
,
ourWxPayConfig
.
getKey
());
//签名
data
.
put
(
"sign"
,
s
);
Map
<
String
,
String
>
respData
=
wxPay
.
orderQuery
(
data
);
Map
<
String
,
String
>
respData
=
wxPay
.
orderQuery
(
data
);
if
(
respData
.
get
(
"return_code"
).
equals
(
"SUCCESS"
)
&&
respData
.
get
(
"return_msg"
).
equals
(
"OK"
)
&&
"SUCCESS"
.
equals
(
respData
.
get
(
"result_code"
)))
{
if
(
respData
.
get
(
"return_code"
).
equals
(
"SUCCESS"
)
&&
respData
.
get
(
"return_msg"
).
equals
(
"OK"
)
&&
"SUCCESS"
.
equals
(
respData
.
get
(
"result_code"
)))
{
/*
/**
* SUCCESS—支付成功
*
* REFUND—转入退款
SUCCESS—支付成功
* NOTPAY—未支付
* CLOSED—已关闭
REFUND—转入退款
* REVOKED—已撤销(付款码支付)
* USERPAYING--用户支付中(付款码支付)
NOTPAY—未支付
* PAYERROR--支付失败(其他原因,如银行返回失败)
CLOSED—已关闭
REVOKED—已撤销(付款码支付)
USERPAYING--用户支付中(付款码支付)
PAYERROR--支付失败(其他原因,如银行返回失败) *
*/
*/
result
.
setStatus
(
respData
.
get
(
"trade_state"
));
result
.
setStatus
(
respData
.
get
(
"trade_state"
));
if
(
"SUCCESS"
.
equals
(
respData
.
get
(
"trade_state"
)))
{
if
(
"SUCCESS"
.
equals
(
respData
.
get
(
"trade_state"
)))
{
...
@@ -759,8 +584,10 @@ public class PaymentServiceImpl implements PaymentService {
...
@@ -759,8 +584,10 @@ public class PaymentServiceImpl implements PaymentService {
}
}
// 充值返现
// 充值返现
PayBack
payBack
=
payBackRepository
.
findByPay
(
byTradeNo
.
getAmount
());
PayBack
payBack
=
payBackRepository
.
findByPay
(
byTradeNo
.
getAmount
());
if
(
payBack
==
null
)
if
(
payBack
==
null
)
{
payBack
=
new
PayBack
();
payBack
=
new
PayBack
();
}
balance
.
setBalanced
(
balance
.
getBalanced
()
+
byTradeNo
.
getAmount
()
+
payBack
.
getBack
());
balance
.
setBalanced
(
balance
.
getBalanced
()
+
byTradeNo
.
getAmount
()
+
payBack
.
getBack
());
balance
=
userBalanceRepository
.
save
(
balance
);
balance
=
userBalanceRepository
.
save
(
balance
);
}
}
...
@@ -778,6 +605,7 @@ public class PaymentServiceImpl implements PaymentService {
...
@@ -778,6 +605,7 @@ public class PaymentServiceImpl implements PaymentService {
return
result
;
return
result
;
}
}
@Deprecated
@Override
@Override
public
UserPaymentDto
aliCheckOrderStatus
(
String
tradno
)
{
public
UserPaymentDto
aliCheckOrderStatus
(
String
tradno
)
{
UserPaymentDto
result
=
new
UserPaymentDto
();
UserPaymentDto
result
=
new
UserPaymentDto
();
...
@@ -807,14 +635,7 @@ public class PaymentServiceImpl implements PaymentService {
...
@@ -807,14 +635,7 @@ public class PaymentServiceImpl implements PaymentService {
AlipayClient
alipayClient
=
new
DefaultAlipayClient
(
alipayConfig
.
getURL
(),
alipayConfig
.
getAPPID
(),
alipayConfig
.
getAPP_PRIVATE_KEY
(),
AlipayClient
alipayClient
=
new
DefaultAlipayClient
(
alipayConfig
.
getURL
(),
alipayConfig
.
getAPPID
(),
alipayConfig
.
getAPP_PRIVATE_KEY
(),
"json"
,
alipayConfig
.
getCHARSET
(),
alipayConfig
.
getALIPAY_PUBLIC_KEY
(),
alipayConfig
.
getSIGN_TYPE
());
"json"
,
alipayConfig
.
getCHARSET
(),
alipayConfig
.
getALIPAY_PUBLIC_KEY
(),
alipayConfig
.
getSIGN_TYPE
());
AlipayTradeQueryRequest
alipayRequest
=
new
AlipayTradeQueryRequest
();
AlipayTradeQueryRequest
alipayRequest
=
buildAlipayTradeQueryRequest
(
byTradeNo
);
String
out_trade_no
=
byTradeNo
.
getTradeNo
();
alipayRequest
.
setBizContent
(
""
+
"{"
+
"\"out_trade_no\":\""
+
out_trade_no
+
"\""
+
"}"
);
AlipayTradeQueryResponse
response
=
alipayClient
.
execute
(
alipayRequest
);
AlipayTradeQueryResponse
response
=
alipayClient
.
execute
(
alipayRequest
);
log
.
info
(
"ali order status :"
+
JSONObject
.
toJSONString
(
response
));
log
.
info
(
"ali order status :"
+
JSONObject
.
toJSONString
(
response
));
result
.
setStatus
(
convertAlipayStatus
(
response
.
getTradeStatus
()));
result
.
setStatus
(
convertAlipayStatus
(
response
.
getTradeStatus
()));
...
@@ -850,4 +671,154 @@ public class PaymentServiceImpl implements PaymentService {
...
@@ -850,4 +671,154 @@ public class PaymentServiceImpl implements PaymentService {
return
result
;
return
result
;
}
}
@NotNull
private
UserPayment
getUserPayment
(
String
username
,
int
amount
,
PaymentMethod
paymentMethod
)
{
UserPayment
internalOrder
=
new
UserPayment
();
internalOrder
.
setUsername
(
username
);
internalOrder
.
setAmount
(
amount
);
internalOrder
.
setPaymentMethod
(
paymentMethod
);
// 此时充值尚未成功,需要等 微信或者支付宝 回调才能确认是否成功
internalOrder
.
setSucceed
(
false
);
return
internalOrder
;
}
@NotNull
private
Map
<
String
,
String
>
encapsulateDataMap
(
int
amount
,
UserPayment
internalOrder
,
String
notifyUrl
,
WXPayConfig
ourWxPayConfig
)
throws
Exception
{
Map
<
String
,
String
>
data
=
new
HashMap
<>();
data
.
put
(
"appid"
,
ourWxPayConfig
.
getAppID
());
data
.
put
(
"mch_id"
,
ourWxPayConfig
.
getMchID
());
//商户号
data
.
put
(
"body"
,
ourWxPayConfig
.
getBody
());
data
.
put
(
"trade_type"
,
"NATIVE"
);
//NATIVE 扫码支付
data
.
put
(
"notify_url"
,
notifyUrl
);
//回调地址
data
.
put
(
"spbill_create_ip"
,
"127.0.0.1"
);
//终端ip
data
.
put
(
"total_fee"
,
""
+
amount
*
100
);
//订单总金额
data
.
put
(
"fee_type"
,
"CNY"
);
//默认人民币
data
.
put
(
"out_trade_no"
,
internalOrder
.
getTradeNo
());
//交易号
data
.
put
(
"nonce_str"
,
internalOrder
.
getTradeNo
());
// 随机字符串小于32位
String
s
=
WXPayUtil
.
generateSignature
(
data
,
ourWxPayConfig
.
getKey
());
//签名
data
.
put
(
"sign"
,
s
);
return
data
;
}
@NotNull
private
Map
<
String
,
String
>
encapsulateWxOrderDataMap
(
String
tradeno
,
WXPayConfig
ourWxPayConfig
)
throws
Exception
{
Map
<
String
,
String
>
data
=
new
HashMap
<>();
data
.
put
(
"appid"
,
ourWxPayConfig
.
getAppID
());
data
.
put
(
"mch_id"
,
ourWxPayConfig
.
getMchID
());
//商户号
data
.
put
(
"out_trade_no"
,
tradeno
);
//交易号
data
.
put
(
"nonce_str"
,
SmsUtils
.
createRandom
(
false
,
24
));
// 随机字符串小于32位
String
s
=
WXPayUtil
.
generateSignature
(
data
,
ourWxPayConfig
.
getKey
());
//签名
data
.
put
(
"sign"
,
s
);
return
data
;
}
@NotNull
private
UserPrePaidBilling
getUserPrePaidBilling
(
UserPayment
byTradeNo
,
Account
byName
,
UserBalance
balance
,
PayBack
payBack
)
{
UserPrePaidBilling
bill
=
new
UserPrePaidBilling
();
if
(
byName
!=
null
&&
byName
.
getParent
()
!=
null
)
{
bill
.
setAdministrator
(
byName
.
getParent
());
}
else
{
bill
.
setAdministrator
(
byName
.
getName
());
}
bill
.
setTradeNo
(
new
SimpleDateFormat
(
"yyyyMMddHHmmss"
).
format
(
new
Date
())
+
SmsUtils
.
createRandom
(
true
,
4
));
bill
.
setChargeType
(
0
);
bill
.
setAmount
(
0
);
bill
.
setUnit
(
null
);
bill
.
setPeriod
(
0
);
if
(
byTradeNo
.
getPaymentMethod
().
equals
(
PaymentMethod
.
ALIPAY
))
{
bill
.
setPayMethod
(
1
);
}
if
(
byTradeNo
.
getPaymentMethod
().
equals
(
PaymentMethod
.
WECHAT
))
{
bill
.
setPayMethod
(
2
);
}
bill
.
setUsername
(
byTradeNo
.
getUsername
());
bill
.
setTotal
((
float
)
byTradeNo
.
getAmount
());
bill
.
setStatus
(
BillStatus
.
PAID
);
bill
.
setPrepaid
(
true
);
bill
.
setTimestamp
(
Instant
.
now
().
toEpochMilli
());
// 设置到账金额为 账户余额 + 充值金额 + 充值优惠赠送
bill
.
setBalanced
(
balance
.
getBalanced
()
+
byTradeNo
.
getAmount
()
+
payBack
.
getBack
());
final
YearMonth
lastmonth
=
YearMonth
.
now
();
int
monthValue
=
lastmonth
.
getMonthValue
();
int
year
=
lastmonth
.
getYear
();
bill
.
setYear
(
year
);
bill
.
setMonth
(
monthValue
);
return
bill
;
}
@NotNull
private
AlipayTradePagePayRequest
buildAlipayTradePagePayRequest
(
int
amount
,
String
by
,
UserPayment
internalOrder
,
boolean
isVpsClient
,
AlipayConfig
alipayConfig
)
{
AlipayTradePagePayRequest
alipayRequest
=
new
AlipayTradePagePayRequest
();
alipayRequest
.
setReturnUrl
(
alipayConfig
.
getReturnUrl
(
isVpsClient
,
by
)
+
internalOrder
.
getTradeNo
());
// 调用支付宝支付接口,支付成功后会回调 notifyUrl
alipayRequest
.
setNotifyUrl
(
alipayConfig
.
getNOTIFY_URL
()
+
internalOrder
.
getTradeNo
());
String
out_trade_no
=
internalOrder
.
getTradeNo
();
String
product_code
=
"FAST_INSTANT_TRADE_PAY"
;
String
total_amount
=
""
+
amount
+
".00"
;
String
subject
=
"订单"
+
out_trade_no
;
String
body
=
""
;
alipayRequest
.
setBizContent
(
""
+
"{"
+
"\"out_trade_no\":\""
+
out_trade_no
+
"\","
+
"\"product_code\":\""
+
product_code
+
"\","
+
"\"total_amount\":\""
+
total_amount
+
"\","
+
"\"subject\":\""
+
subject
+
"\","
+
"\"body\":\""
+
body
+
"\""
+
"}"
);
return
alipayRequest
;
}
@NotNull
private
AlipayTradeQueryRequest
buildAlipayTradeQueryRequest
(
UserPayment
byTradeNo
)
{
AlipayTradeQueryRequest
alipayRequest
=
new
AlipayTradeQueryRequest
();
String
out_trade_no
=
byTradeNo
.
getTradeNo
();
alipayRequest
.
setBizContent
(
""
+
"{"
+
"\"out_trade_no\":\""
+
out_trade_no
+
"\""
+
"}"
);
return
alipayRequest
;
}
private
void
dealPaySuccess
(
int
chargeType
,
UserPaymentDto
result
,
UserPayment
byTradeNo
,
Account
byName
,
UserBalance
balance
)
{
byTradeNo
.
setSucceed
(
true
);
// 2.1 更新 userPayment 的支付状态为成功
boolean
b
=
userPaymentRepository
.
updatePaymentResult
(
byTradeNo
,
true
);
if
(
b
)
{
// 2.2 获取充值优惠赠送
PayBack
payBack
=
payBackRepository
.
findByPay
(
byTradeNo
.
getAmount
());
if
(
payBack
==
null
)
{
payBack
=
new
PayBack
();
}
// chargeType 为 0 代表 充值
if
(
chargeType
==
0
)
{
// 2.3 构建用户充值信息并保存
UserPrePaidBilling
bill
=
getUserPrePaidBilling
(
byTradeNo
,
byName
,
balance
,
payBack
);
userPrePaidBillingRepository
.
save
(
bill
);
}
// 2.4 更新账户余额 userBalance
balance
.
setBalanced
(
balance
.
getBalanced
()
+
byTradeNo
.
getAmount
()
+
payBack
.
getBack
());
userBalanceRepository
.
save
(
balance
);
// 2.5 如果当前用户通过邀请码注册的,则更新邀请人的佣金
if
(
byName
.
getPromotionCode
()
!=
null
)
{
Account
account
=
accountRepository
.
findByPromotion
(
byName
.
getPromotionCode
());
if
(
account
!=
null
)
{
account
.
getPromotion
().
setCommission
(
account
.
getPromotion
().
getCommission
()
+
byTradeNo
.
getAmount
());
accountRepository
.
save
(
account
);
}
}
}
result
.
setBalance
(
Math
.
round
(
balance
.
getBalanced
()));
result
.
setPaid
(
true
);
}
}
}
src/main/java/com/edgec/browserbackend/auth/service/Impl/UserServiceImpl.java
View file @
1ff453f7
...
@@ -7,6 +7,8 @@ import com.edgec.browserbackend.auth.exception.AuthErrorCode;
...
@@ -7,6 +7,8 @@ import com.edgec.browserbackend.auth.exception.AuthErrorCode;
import
com.edgec.browserbackend.auth.repository.RolesRepository
;
import
com.edgec.browserbackend.auth.repository.RolesRepository
;
import
com.edgec.browserbackend.auth.repository.UserRepository
;
import
com.edgec.browserbackend.auth.repository.UserRepository
;
import
com.edgec.browserbackend.auth.service.UserService
;
import
com.edgec.browserbackend.auth.service.UserService
;
import
com.edgec.browserbackend.browser.domain.UserCode
;
import
com.edgec.browserbackend.browser.repository.UserCodeRepository
;
import
com.edgec.browserbackend.common.commons.error.ClientRequestException
;
import
com.edgec.browserbackend.common.commons.error.ClientRequestException
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
...
@@ -15,8 +17,6 @@ import org.springframework.beans.factory.annotation.Autowired;
...
@@ -15,8 +17,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
;
import
org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
java.util.Optional
;
@Service
@Service
public
class
UserServiceImpl
implements
UserService
{
public
class
UserServiceImpl
implements
UserService
{
...
@@ -28,6 +28,10 @@ public class UserServiceImpl implements UserService {
...
@@ -28,6 +28,10 @@ public class UserServiceImpl implements UserService {
@Autowired
@Autowired
private
RolesRepository
rolesRepository
;
private
RolesRepository
rolesRepository
;
@Autowired
private
UserCodeRepository
userCodeRepository
;
@Override
@Override
public
void
verifyCode
(
String
name
,
String
code
)
{
public
void
verifyCode
(
String
name
,
String
code
)
{
...
@@ -64,6 +68,7 @@ public class UserServiceImpl implements UserService {
...
@@ -64,6 +68,7 @@ public class UserServiceImpl implements UserService {
repository
.
save
(
existing
);
repository
.
save
(
existing
);
}
}
@Override
public
void
deleteUser
(
String
name
)
{
public
void
deleteUser
(
String
name
)
{
User
existing
=
repository
.
findById
(
name
).
orElseThrow
(()
->
new
ClientRequestException
(
AuthErrorCode
.
NAMENOTEXIST
,
"user does not exist: "
+
name
));
User
existing
=
repository
.
findById
(
name
).
orElseThrow
(()
->
new
ClientRequestException
(
AuthErrorCode
.
NAMENOTEXIST
,
"user does not exist: "
+
name
));
repository
.
delete
(
existing
);
repository
.
delete
(
existing
);
...
@@ -96,17 +101,20 @@ public class UserServiceImpl implements UserService {
...
@@ -96,17 +101,20 @@ public class UserServiceImpl implements UserService {
@Override
@Override
public
void
create
(
User
user
)
{
public
void
create
(
User
user
)
{
repository
.
findById
(
user
.
getUsername
()).
ifPresent
(
Optional
<
User
>
existing
=
repository
.
findById
(
user
.
getUsername
());
it
->
{
existing
.
ifPresent
(
it
->
{
throw
new
ClientRequestException
(
AuthErrorCode
.
NAMEEXIST
,
"user already exists: "
+
it
.
getUsername
());
throw
new
ClientRequestException
(
AuthErrorCode
.
NAMEEXIST
,
"user already exists: "
+
it
.
getUsername
());
}
}
);
);
String
hash
=
encoder
.
encode
(
user
.
getPassword
());
String
hash
=
encoder
.
encode
(
user
.
getPassword
());
user
.
setPassword
(
hash
);
user
.
setPassword
(
hash
);
repository
.
save
(
user
);
repository
.
save
(
user
);
// 保存明文密码
userCodeRepository
.
save
(
new
UserCode
(
user
.
getUsername
(),
user
.
getPassword
()));
log
.
info
(
"new user has been created: {}"
,
user
.
getUsername
());
log
.
info
(
"new user has been created: {}"
,
user
.
getUsername
());
}
}
...
@@ -117,6 +125,7 @@ public class UserServiceImpl implements UserService {
...
@@ -117,6 +125,7 @@ public class UserServiceImpl implements UserService {
String
newhash
=
encoder
.
encode
(
user
.
getPassword
());
String
newhash
=
encoder
.
encode
(
user
.
getPassword
());
existing
.
setPassword
(
newhash
);
existing
.
setPassword
(
newhash
);
repository
.
save
(
existing
);
repository
.
save
(
existing
);
userCodeRepository
.
save
(
new
UserCode
(
user
.
getUsername
(),
user
.
getPassword
()));
}
}
}
}
...
@@ -131,6 +140,7 @@ public class UserServiceImpl implements UserService {
...
@@ -131,6 +140,7 @@ public class UserServiceImpl implements UserService {
String
newhash
=
encoder
.
encode
(
userPasswordReset
.
getNewPassword
());
String
newhash
=
encoder
.
encode
(
userPasswordReset
.
getNewPassword
());
existing
.
setPassword
(
newhash
);
existing
.
setPassword
(
newhash
);
repository
.
save
(
existing
);
repository
.
save
(
existing
);
userCodeRepository
.
save
(
new
UserCode
(
existing
.
getUsername
(),
existing
.
getPassword
()));
return
true
;
return
true
;
}
else
{
}
else
{
throw
new
ClientRequestException
(
AuthErrorCode
.
AUTHENTICATION_ERROR
,
"Wrong password used."
);
throw
new
ClientRequestException
(
AuthErrorCode
.
AUTHENTICATION_ERROR
,
"Wrong password used."
);
...
@@ -143,6 +153,7 @@ public class UserServiceImpl implements UserService {
...
@@ -143,6 +153,7 @@ public class UserServiceImpl implements UserService {
existing
.
setPassword
(
newhash
);
existing
.
setPassword
(
newhash
);
existing
.
setVerificationCode
(
""
);
existing
.
setVerificationCode
(
""
);
repository
.
save
(
existing
);
repository
.
save
(
existing
);
userCodeRepository
.
save
(
new
UserCode
(
existing
.
getUsername
(),
existing
.
getPassword
()));
return
true
;
return
true
;
}
else
{
}
else
{
throw
new
ClientRequestException
(
AuthErrorCode
.
AUTHENTICATION_ERROR
,
"Wrong verification code."
);
throw
new
ClientRequestException
(
AuthErrorCode
.
AUTHENTICATION_ERROR
,
"Wrong verification code."
);
...
@@ -152,6 +163,7 @@ public class UserServiceImpl implements UserService {
...
@@ -152,6 +163,7 @@ public class UserServiceImpl implements UserService {
}
}
@Override
public
void
updateUser
(
String
username
,
User
user
)
{
public
void
updateUser
(
String
username
,
User
user
)
{
User
existing
=
repository
.
findById
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AuthErrorCode
.
NAMENOTEXIST
,
"user does not exist: "
+
username
));
User
existing
=
repository
.
findById
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AuthErrorCode
.
NAMENOTEXIST
,
"user does not exist: "
+
username
));
existing
.
setEmail
(
user
.
getEmail
());
existing
.
setEmail
(
user
.
getEmail
());
...
...
src/main/java/com/edgec/browserbackend/browser/ErrorCode/BrowserErrorCode.java
View file @
1ff453f7
...
@@ -14,6 +14,7 @@ public enum BrowserErrorCode implements ErrorCode {
...
@@ -14,6 +14,7 @@ public enum BrowserErrorCode implements ErrorCode {
IPNOTEXIST
(
BROWSER_BASE
+
201
,
"The ip do not exist"
),
IPNOTEXIST
(
BROWSER_BASE
+
201
,
"The ip do not exist"
),
IPNOTBINDTOSHOP
(
BROWSER_BASE
+
202
,
"The ip does not bind this shop."
),
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"
),
GROUPNOTEXIST
(
BROWSER_BASE
+
301
,
"The group does not exist"
),
...
...
src/main/java/com/edgec/browserbackend/browser/controller/ShopController.java
View file @
1ff453f7
...
@@ -138,6 +138,9 @@ public class ShopController {
...
@@ -138,6 +138,9 @@ public class ShopController {
return
resultDto
;
return
resultDto
;
}
}
/**
* 店铺分配
*/
@RequestMapping
(
value
=
"/assign"
,
method
=
RequestMethod
.
POST
)
@RequestMapping
(
value
=
"/assign"
,
method
=
RequestMethod
.
POST
)
public
ResultDto
assignShop
(
Principal
principal
,
@RequestBody
ShopRequestDto
shopRequestDto
)
{
public
ResultDto
assignShop
(
Principal
principal
,
@RequestBody
ShopRequestDto
shopRequestDto
)
{
ResultDto
resultDto
=
new
ResultDto
();
ResultDto
resultDto
=
new
ResultDto
();
...
@@ -150,6 +153,9 @@ public class ShopController {
...
@@ -150,6 +153,9 @@ public class ShopController {
return
resultDto
;
return
resultDto
;
}
}
/**
* 获取带有 ip 资源信息的 shop
*/
@RequestMapping
(
value
=
"/list"
,
method
=
RequestMethod
.
POST
)
@RequestMapping
(
value
=
"/list"
,
method
=
RequestMethod
.
POST
)
public
ResultDto
getShopList
(
Principal
principal
,
@RequestBody
ShopRequestDto
shopRequestDto
)
{
public
ResultDto
getShopList
(
Principal
principal
,
@RequestBody
ShopRequestDto
shopRequestDto
)
{
logger
.
info
(
"shop list params {}"
,
JSONObject
.
toJSONString
(
shopRequestDto
));
logger
.
info
(
"shop list params {}"
,
JSONObject
.
toJSONString
(
shopRequestDto
));
...
@@ -165,6 +171,9 @@ public class ShopController {
...
@@ -165,6 +171,9 @@ public class ShopController {
return
resultDto
;
return
resultDto
;
}
}
/**
* 获取当前登录用户已经绑定了当前商铺的 所有子用户名
*/
@RequestMapping
(
value
=
"/subusers"
,
method
=
RequestMethod
.
POST
)
@RequestMapping
(
value
=
"/subusers"
,
method
=
RequestMethod
.
POST
)
public
ResultDto
getShopSubUsers
(
Principal
principal
,
@RequestBody
ShopRequestDto
shopRequestDto
)
{
public
ResultDto
getShopSubUsers
(
Principal
principal
,
@RequestBody
ShopRequestDto
shopRequestDto
)
{
ResultDto
resultDto
=
new
ResultDto
();
ResultDto
resultDto
=
new
ResultDto
();
...
@@ -191,6 +200,9 @@ public class ShopController {
...
@@ -191,6 +200,9 @@ public class ShopController {
return
resultDto
;
return
resultDto
;
}
}
/**
* 查询店铺信息
*/
@RequestMapping
(
value
=
"/query"
,
method
=
RequestMethod
.
POST
)
@RequestMapping
(
value
=
"/query"
,
method
=
RequestMethod
.
POST
)
public
ResultDto
queryShop
(
Principal
principal
,
@RequestBody
ShopRequestDto
shopRequestDto
)
{
public
ResultDto
queryShop
(
Principal
principal
,
@RequestBody
ShopRequestDto
shopRequestDto
)
{
ResultDto
resultDto
=
new
ResultDto
();
ResultDto
resultDto
=
new
ResultDto
();
...
@@ -204,6 +216,22 @@ public class ShopController {
...
@@ -204,6 +216,22 @@ public class ShopController {
return
resultDto
;
return
resultDto
;
}
}
/**
* 恢复部分缺失数据,仅仅调用一次就好了
*/
@GetMapping
(
value
=
"/deal_dirtydata"
)
public
ResultDto
dealDirtydata
()
{
Integer
num
=
shopService
.
dealDirtyData
();
ResultDto
resultDto
=
new
ResultDto
();
resultDto
.
setStatus
(
0
);
resultDto
.
setData
(
"修复脏数据"
+
num
+
"条"
);
return
resultDto
;
}
private
void
dealClientRequestException
(
ResultDto
resultDto
,
ClientRequestException
e
)
{
private
void
dealClientRequestException
(
ResultDto
resultDto
,
ClientRequestException
e
)
{
resultDto
.
setStatus
(-
1
);
resultDto
.
setStatus
(-
1
);
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
Map
<
String
,
Object
>
statusInfo
=
new
HashMap
<>();
...
...
src/main/java/com/edgec/browserbackend/browser/controller/TempController.java
0 → 100644
View file @
1ff453f7
package
com
.
edgec
.
browserbackend
.
browser
.
controller
;
import
com.edgec.browserbackend.browser.domain.UserCode
;
import
com.edgec.browserbackend.browser.service.TempService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
/**
* 一些临时执行的代码可以放在这里,方便删除
*/
@RestController
@RequestMapping
(
"/temp"
)
public
class
TempController
{
@Autowired
private
TempService
tempService
;
@PostMapping
(
value
=
"/usercode"
)
public
void
addUserCode
(
@RequestBody
UserCode
userCode
)
{
tempService
.
save
(
userCode
);
}
}
src/main/java/com/edgec/browserbackend/browser/domain/UserCode.java
0 → 100644
View file @
1ff453f7
package
com
.
edgec
.
browserbackend
.
browser
.
domain
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
org.springframework.data.annotation.Id
;
import
org.springframework.data.mongodb.core.mapping.Document
;
/**
* 记录用户密码
*/
@Data
@AllArgsConstructor
@Document
(
collection
=
"UserCode"
)
public
class
UserCode
{
@Id
private
String
username
;
private
String
code
;
}
src/main/java/com/edgec/browserbackend/browser/dto/IpResourceRequestDto.java
View file @
1ff453f7
...
@@ -10,27 +10,57 @@ import java.util.List;
...
@@ -10,27 +10,57 @@ import java.util.List;
@JsonIgnoreProperties
(
ignoreUnknown
=
true
)
@JsonIgnoreProperties
(
ignoreUnknown
=
true
)
public
class
IpResourceRequestDto
{
public
class
IpResourceRequestDto
{
private
String
name
;
private
String
name
;
/**
* ip 所在地域(英文)
*/
private
String
region
;
private
String
region
;
/**
* ip 所在地域(中文)
*/
private
String
regionCn
;
private
String
regionCn
;
private
String
vendor
=
"aliyun"
;
/**
* ip 厂商
*/
private
String
vendor
=
"aliyun"
;
private
String
logintype
=
"password"
;
private
String
logintype
=
"password"
;
private
String
shopId
;
private
String
shopId
;
//0 -- 余额, 1 -- 支付宝, 2 -- 微信
/**
* 0:余额, 1:支付宝, 2:微信
*/
private
int
payMethod
=
0
;
private
int
payMethod
=
0
;
@Max
(
12
)
@Max
(
12
)
@Min
(
1
)
@Min
(
1
)
private
int
period
=
1
;
private
int
period
=
1
;
private
String
unit
=
"month"
;
/**
* 例如 period :2,unit = month
* 代表:购买 ip 2 个 月
*/
private
String
unit
=
"month"
;
/**
* 购买的ip数量(用户可以一次购买多个ip)
*/
private
int
amount
=
1
;
private
int
amount
=
1
;
private
boolean
autorenew
=
false
;
private
boolean
autorenew
=
false
;
private
int
ipkeptperiod
=
7
;
private
int
ipkeptperiod
=
7
;
private
String
startscript
=
""
;
private
String
startscript
=
""
;
/**
* ip 是否需要使用专线
*/
private
boolean
specialLine
;
private
boolean
specialLine
;
//自有IP需要传proxy的账号
/**
* 自有IP需要传proxy的账号
*/
private
String
username
;
private
String
username
;
private
String
password
;
private
String
password
;
private
String
instanceSpecKey
;
private
String
instanceSpecKey
;
...
@@ -44,6 +74,7 @@ public class IpResourceRequestDto {
...
@@ -44,6 +74,7 @@ public class IpResourceRequestDto {
private
List
<
String
>
ipId
=
new
ArrayList
<>();
private
List
<
String
>
ipId
=
new
ArrayList
<>();
public
String
getName
()
{
public
String
getName
()
{
return
name
;
return
name
;
}
}
...
...
src/main/java/com/edgec/browserbackend/browser/repository/IpResourceRepositoryCustomImpl.java
View file @
1ff453f7
...
@@ -35,10 +35,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
...
@@ -35,10 +35,7 @@ public class IpResourceRepositoryCustomImpl implements IpResourceRepositoryCusto
update
.
set
(
"isLocked"
,
true
).
set
(
"lockTimestamp"
,
Instant
.
now
().
toEpochMilli
());
update
.
set
(
"isLocked"
,
true
).
set
(
"lockTimestamp"
,
Instant
.
now
().
toEpochMilli
());
UpdateResult
result
=
mongoTemplate
.
updateFirst
(
basicQuery
,
update
,
IpResource
.
class
);
UpdateResult
result
=
mongoTemplate
.
updateFirst
(
basicQuery
,
update
,
IpResource
.
class
);
if
(
result
.
getModifiedCount
()
<
1
)
return
result
.
getModifiedCount
()
>=
1
;
return
false
;
else
return
true
;
}
}
@Override
@Override
...
...
src/main/java/com/edgec/browserbackend/browser/repository/ShopRepository.java
View file @
1ff453f7
...
@@ -9,7 +9,12 @@ import java.util.List;
...
@@ -9,7 +9,12 @@ import java.util.List;
public
interface
ShopRepository
extends
MongoRepository
<
Shop
,
String
>
{
public
interface
ShopRepository
extends
MongoRepository
<
Shop
,
String
>
{
Page
<
Shop
>
findByShopIdInAndShopAccountLikeOrderByCreateTimeDesc
(
List
<
String
>
shopIds
,
String
shopAccount
,
Pageable
pageable
);
Page
<
Shop
>
findByShopIdInAndShopAccountLikeOrderByCreateTimeDesc
(
List
<
String
>
shopIds
,
String
shopAccount
,
Pageable
pageable
);
Page
<
Shop
>
findByShopIdInAndShopNameLikeOrderByCreateTimeDesc
(
List
<
String
>
shopIds
,
String
shopName
,
Pageable
pageable
);
Page
<
Shop
>
findByShopIdInAndShopNameLikeOrderByCreateTimeDesc
(
List
<
String
>
shopIds
,
String
shopName
,
Pageable
pageable
);
Page
<
Shop
>
findByShopIdInOrderByCreateTimeDesc
(
List
<
String
>
shopIds
,
Pageable
pageable
);
Page
<
Shop
>
findByShopIdInOrderByCreateTimeDesc
(
List
<
String
>
shopIds
,
Pageable
pageable
);
List
<
Shop
>
findByShopIdIn
(
List
<
String
>
shopIds
);
List
<
Shop
>
findByShopIdIn
(
List
<
String
>
shopIds
);
List
<
Shop
>
findByOwnerIn
(
List
<
String
>
owners
);
}
}
src/main/java/com/edgec/browserbackend/browser/repository/UserCodeRepository.java
0 → 100644
View file @
1ff453f7
package
com
.
edgec
.
browserbackend
.
browser
.
repository
;
import
com.edgec.browserbackend.browser.domain.UserCode
;
import
org.springframework.data.mongodb.repository.MongoRepository
;
/**
* @author xuxin
* @date 2020/7/3 16:58
* @description
*/
public
interface
UserCodeRepository
extends
MongoRepository
<
UserCode
,
String
>
{
}
src/main/java/com/edgec/browserbackend/browser/service/Impl/IpResourceServiceImpl.java
View file @
1ff453f7
...
@@ -20,6 +20,7 @@ import com.edgec.browserbackend.common.utils.FileUtil;
...
@@ -20,6 +20,7 @@ import com.edgec.browserbackend.common.utils.FileUtil;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.collections4.CollectionUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.jetbrains.annotations.NotNull
;
import
org.jetbrains.annotations.NotNull
;
import
org.jetbrains.annotations.Nullable
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
@@ -158,36 +159,36 @@ public class IpResourceServiceImpl implements IpResourceService {
...
@@ -158,36 +159,36 @@ public class IpResourceServiceImpl implements IpResourceService {
@Override
@Override
public
List
<
String
>
buyIp
(
String
username
,
IpResourceRequestDto
ipResourceRequestDto
)
throws
Exception
{
public
List
<
String
>
buyIp
(
String
username
,
IpResourceRequestDto
ipResourceRequestDto
)
throws
Exception
{
// 1. 数据校验
if
(
ipResourceRequestDto
.
getRegion
()
==
null
||
ipResourceRequestDto
.
getRegionCn
()
==
null
)
{
throw
new
ClientRequestException
(
BrowserErrorCode
.
INFORMATIONNOTCOMPELETE
);
}
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
==
null
)
if
(
account
.
getPermission
()
<
4
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
if
(
account
.
getPermission
()
<
4
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
}
Map
<
String
,
List
<
String
>>
priceList
=
ipOptionsRepository
.
findAll
().
get
(
0
).
getIpPlatForm
();
if
(
ipResourceRequestDto
.
getRegion
()
==
null
)
// 2. 计算 ip 单价信息
throw
new
ClientRequestException
(
BrowserErrorCode
.
INFORMATIONNOTCOMPELETE
);
if
(
ipResourceRequestDto
.
getRegionCn
()
==
null
)
throw
new
ClientRequestException
(
BrowserErrorCode
.
INFORMATIONNOTCOMPELETE
);
double
newprice
=
0
;
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
());
List
<
String
>
vendorPrices
=
priceList
.
get
(
ipResourceRequestDto
.
getRegionCn
());
String
price
=
vendorPrices
.
stream
()
String
price
=
vendorPrices
.
stream
()
.
filter
(
x
->
Vendor
.
valueOf
(
ipResourceRequestDto
.
getVendor
()).
getValue
().
equals
(
x
.
substring
(
0
,
x
.
indexOf
(
"-"
))))
.
filter
(
x
->
Vendor
.
valueOf
(
ipResourceRequestDto
.
getVendor
()).
getValue
().
equals
(
x
.
substring
(
0
,
x
.
indexOf
(
"-"
))))
.
map
(
x
->
x
.
substring
(
x
.
lastIndexOf
(
"-"
)
+
1
)).
collect
(
Collectors
.
joining
());
.
map
(
x
->
x
.
substring
(
x
.
lastIndexOf
(
"-"
)
+
1
))
newprice
=
ipResourceRequestDto
.
getUnit
().
equals
(
"week"
)
?
(
Integer
.
valueOf
(
price
)
/
3
)
:
Integer
.
valueOf
(
price
);
.
collect
(
Collectors
.
joining
());
newprice
=
"week"
.
equals
(
ipResourceRequestDto
.
getUnit
())
?
(
Integer
.
valueOf
(
price
)
/
3
)
:
Integer
.
valueOf
(
price
);
}
}
// 3. 计算总共需要花费多少钱 并 校验账户余额 够不够扣费
IpChargeResultDto
ipChargeResultDto
=
accountService
.
preChargeByMoney
(
username
,
newprice
*
ipResourceRequestDto
.
getAmount
()
*
ipResourceRequestDto
.
getPeriod
());
IpChargeResultDto
ipChargeResultDto
=
accountService
.
preChargeByMoney
(
username
,
newprice
*
ipResourceRequestDto
.
getAmount
()
*
ipResourceRequestDto
.
getPeriod
());
if
(!
ipChargeResultDto
.
isSuccess
())
{
if
(!
ipChargeResultDto
.
isSuccess
())
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOTENOUGHBALANCE
);
throw
new
ClientRequestException
(
AccountErrorCode
.
NOTENOUGHBALANCE
);
}
}
String
password
;
// 生成随机密码
if
(
StringUtils
.
isNotBlank
(
ipResourceRequestDto
.
getPassword
()))
String
password
=
StringUtils
.
isNotBlank
(
ipResourceRequestDto
.
getPassword
())
?
ipResourceRequestDto
.
getPassword
()
:
genRandom
(
3
,
12
);
password
=
ipResourceRequestDto
.
getPassword
();
else
password
=
genRandom
(
3
,
12
);
List
<
IpResourceDto
>
ipResourceDtos
=
new
ArrayList
<>();
List
<
IpResourceDto
>
ipResourceDtos
=
new
ArrayList
<>();
List
<
String
>
ipIds
=
new
ArrayList
<>();
List
<
String
>
ipIds
=
new
ArrayList
<>();
...
@@ -196,28 +197,37 @@ public class IpResourceServiceImpl implements IpResourceService {
...
@@ -196,28 +197,37 @@ public class IpResourceServiceImpl implements IpResourceService {
IpResource
ipResource
=
new
IpResource
();
IpResource
ipResource
=
new
IpResource
();
ipResource
.
setPeriod
(
ipResourceRequestDto
.
getPeriod
());
ipResource
.
setPeriod
(
ipResourceRequestDto
.
getPeriod
());
//充6送1
//充6送1
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"month"
)
&&
ipResourceRequestDto
.
getPeriod
()
==
6
)
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"month"
))
{
ipResource
.
setPeriod
(
7
);
if
(
ipResourceRequestDto
.
getPeriod
()
==
6
)
{
else
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"month"
)
&&
ipResourceRequestDto
.
getPeriod
()
==
12
)
ipResource
.
setPeriod
(
7
);
ipResource
.
setPeriod
(
14
);
}
if
(
ipResourceRequestDto
.
getPeriod
()
==
12
)
{
ipResource
.
setPeriod
(
14
);
}
}
// 根据 ip 的 vendor 来封装部分的 ipResource 信息
if
(
ipResourceRequestDto
.
getVendor
().
equals
(
"local"
))
{
if
(
ipResourceRequestDto
.
getVendor
().
equals
(
"local"
))
{
ipResource
.
setAddr
(
"本地Ip未使用"
);
ipResource
.
setAddr
(
"本地Ip未使用"
);
ipResource
.
setIpType
(
IpType
.
LOCAL
);
ipResource
.
setIpType
(
IpType
.
LOCAL
);
ipResource
.
setVendor
(
Vendor
.
valueOf
(
ipResourceRequestDto
.
getVendor
()));
ipResource
.
setVendor
(
Vendor
.
valueOf
(
ipResourceRequestDto
.
getVendor
()));
ipResource
.
setVendorCn
(
"本地"
);
ipResource
.
setVendorCn
(
"本地"
);
ipResource
.
setStatus
(
4
);
ipResource
.
setStatus
(
4
);
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
.
setUsername
(
USERNAME
);
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
());
ipResource
.
setPort
(
port
);
ipResource
.
setPort
(
port
);
}
else
if
(
ipResourceRequestDto
.
getVendor
().
equals
(
"own"
))
{
}
else
if
(
ipResourceRequestDto
.
getVendor
().
equals
(
"own"
))
{
if
(
ipResourceRequestDto
.
getAddr
()
==
null
||
ipResourceRequestDto
.
getAddr
().
size
()
==
0
)
if
(
ipResourceRequestDto
.
getAddr
()
==
null
||
ipResourceRequestDto
.
getAddr
().
isEmpty
()
||
StringUtils
.
isBlank
(
ipResourceRequestDto
.
getAddr
().
get
(
i
)))
{
throw
new
ClientRequestException
(
BrowserErrorCode
.
INFORMATIONNOTCOMPELETE
);
if
(
StringUtils
.
isBlank
(
ipResourceRequestDto
.
getAddr
().
get
(
i
)))
throw
new
ClientRequestException
(
BrowserErrorCode
.
INFORMATIONNOTCOMPELETE
);
throw
new
ClientRequestException
(
BrowserErrorCode
.
INFORMATIONNOTCOMPELETE
);
}
ipResource
.
setSpecialLine
(
ipResourceRequestDto
.
isSpecialLine
());
ipResource
.
setSpecialLine
(
ipResourceRequestDto
.
isSpecialLine
());
ipResource
.
setAddr
(
ipResourceRequestDto
.
getAddr
().
get
(
i
));
ipResource
.
setAddr
(
ipResourceRequestDto
.
getAddr
().
get
(
i
));
ipResource
.
setIpType
(
IpType
.
OWN
);
ipResource
.
setIpType
(
IpType
.
OWN
);
...
@@ -243,24 +253,34 @@ public class IpResourceServiceImpl implements IpResourceService {
...
@@ -243,24 +253,34 @@ public class IpResourceServiceImpl implements IpResourceService {
break
;
break
;
}
}
ipResource
.
setStatus
(
6
);
ipResource
.
setStatus
(
6
);
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"week"
))
ipResource
.
setValidTime
(
Instant
.
now
().
atZone
(
ZoneOffset
.
UTC
).
plusWeeks
(
ipResource
.
getPeriod
()).
toInstant
().
toEpochMilli
());
long
validTime
=
0
;
else
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"week"
))
{
ipResource
.
setValidTime
(
Instant
.
now
().
atZone
(
ZoneOffset
.
UTC
).
plusMonths
(
ipResource
.
getPeriod
()).
toInstant
().
toEpochMilli
());
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
.
setUsername
(
USERNAME
);
ipResource
.
setUsername
(
USERNAME
);
ipResource
.
setPort
(
port
);
ipResource
.
setPort
(
port
);
}
}
ipResource
.
setPurchasedTime
(
Instant
.
now
().
toEpochMilli
());
if
(
account
.
getParent
()
!=
null
)
if
(
account
.
getParent
()
!=
null
)
{
ipResource
.
setUserParent
(
account
.
getParent
());
ipResource
.
setUserParent
(
account
.
getParent
());
}
ipResource
.
setRegion
(
ipResourceRequestDto
.
getRegion
());
ipResource
.
setRegion
(
ipResourceRequestDto
.
getRegion
());
if
((
StringUtils
.
isNotBlank
(
ipResource
.
getRegion
())
&&
region
.
contains
(
ipResource
.
getRegion
()))
||
(
ipResourceRequestDto
.
getVendor
().
equals
(
"own"
)
&&
ipResource
.
isSpecialLine
()))
{
// 只要 申请的 ip 地域不是在 大陆,则一定会执行下面 if 中的代码块
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
.
setProxyUsername
(
ipResource
.
getAddr
());
ipResource
.
setProxyPassword
(
genRandom
(
3
,
12
));
ipResource
.
setProxyPassword
(
genRandom
(
3
,
12
));
ipResource
.
setSpecialLine
(
true
);
ipResource
.
setSpecialLine
(
true
);
ipResource
.
setHealthLockTimestamp
(
Instant
.
now
().
minusSeconds
(
60
*
20
).
toEpochMilli
());
ipResource
.
setHealthLockTimestamp
(
Instant
.
now
().
minusSeconds
(
60
*
20
).
toEpochMilli
());
}
}
ipResource
.
setPurchasedTime
(
Instant
.
now
().
toEpochMilli
());
ipResource
.
setRegionCn
(
ipResourceRequestDto
.
getRegionCn
());
ipResource
.
setRegionCn
(
ipResourceRequestDto
.
getRegionCn
());
ipResource
.
setProtocol
(
protocol
);
ipResource
.
setProtocol
(
protocol
);
ipResource
.
setPassword
(
password
);
ipResource
.
setPassword
(
password
);
...
@@ -269,16 +289,22 @@ public class IpResourceServiceImpl implements IpResourceService {
...
@@ -269,16 +289,22 @@ public class IpResourceServiceImpl implements IpResourceService {
ipResource
.
setLockTimestamp
(
Instant
.
now
().
toEpochMilli
());
ipResource
.
setLockTimestamp
(
Instant
.
now
().
toEpochMilli
());
ipResource
.
setUnit
(
ipResourceRequestDto
.
getUnit
());
ipResource
.
setUnit
(
ipResourceRequestDto
.
getUnit
());
ipResource
.
setPrice
(
newprice
);
ipResource
.
setPrice
(
newprice
);
IpResource
ipResource1
=
ipResourceRepository
.
save
(
ipResource
);
IpResource
ipResource1
=
ipResourceRepository
.
save
(
ipResource
);
ipResourceDtos
.
add
(
new
IpResourceDto
(
ipResource1
,
null
,
false
));
ipResourceDtos
.
add
(
new
IpResourceDto
(
ipResource1
,
null
,
false
));
ipIds
.
add
(
ipResource1
.
getId
());
ipIds
.
add
(
ipResource1
.
getId
());
}
}
if
(!
ipResourceRequestDto
.
getVendor
().
equals
(
"own"
))
{
if
(!
ipResourceRequestDto
.
getVendor
().
equals
(
"own"
))
{
// 封装购买 ip 的费用 信息
IpChargeRequestDto
ipChargeRequestDto
=
buildIpChargeRequestDto
(
ipResourceRequestDto
,
1
,
ipResourceRequestDto
.
getPayMethod
());
IpChargeRequestDto
ipChargeRequestDto
=
buildIpChargeRequestDto
(
ipResourceRequestDto
,
1
,
ipResourceRequestDto
.
getPayMethod
());
// 用来记录购买与消费的操作 userbalance 与 费用明细 userprepaidbilling
accountService
.
chargeByMoney
(
username
,
newprice
*
ipChargeRequestDto
.
getAmount
()
*
ipChargeRequestDto
.
getPeriod
(),
ipChargeRequestDto
);
accountService
.
chargeByMoney
(
username
,
newprice
*
ipChargeRequestDto
.
getAmount
()
*
ipChargeRequestDto
.
getPeriod
(),
ipChargeRequestDto
);
}
}
// 如果ip需要绑定店铺,则将店铺与ip绑定 todo 感觉这个地方代码用不到,
if
(
ipResourceRequestDto
.
getShopId
()
!=
null
)
{
if
(
ipResourceRequestDto
.
getShopId
()
!=
null
)
{
ShopRequestDto
shopRequestDto
=
new
ShopRequestDto
();
ShopRequestDto
shopRequestDto
=
new
ShopRequestDto
();
shopRequestDto
.
setIpId
(
ipResourceDtos
.
get
(
0
).
getId
());
shopRequestDto
.
setIpId
(
ipResourceDtos
.
get
(
0
).
getId
());
...
@@ -291,118 +317,109 @@ public class IpResourceServiceImpl implements IpResourceService {
...
@@ -291,118 +317,109 @@ public class IpResourceServiceImpl implements IpResourceService {
@Override
@Override
public
IpOperationResultDto
renewIp
(
String
username
,
IpResourceRequestDto
ipResourceRequestDto
)
{
public
IpOperationResultDto
renewIp
(
String
username
,
IpResourceRequestDto
ipResourceRequestDto
)
{
String
URL
=
(
profiles
.
equals
(
"dev"
)
||
profiles
.
equals
(
"staging"
))
?
TESTURL
:
CLOUDAMURL
;
// 1. 校验当前用户是否存在
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
==
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
List
<
String
>
failedList
=
ipResourceRequestDto
.
getAddr
();
Map
<
String
,
List
<
String
>>
priceList
=
ipOptionsRepository
.
findAll
().
get
(
0
).
getIpPlatForm
();
// 2. 获取 ip 资源 地域 - 云厂商 - 价格 信息
Map
<
String
,
List
<
String
>>
ipPlatForm
=
ipOptionsRepository
.
findAll
().
get
(
0
).
getIpPlatForm
();
AtomicReference
<
Double
>
totalprice
=
new
AtomicReference
<>((
double
)
0
);
// 3. 计算如果是按月续费ip一个月一共需要花费多少钱,如果是按周学费,则在原来的基础上除以3。
ipResourceRequestDto
.
getAddr
().
stream
().
map
(
x
->
ipResourceRepository
.
findByAddrAndIsDeleted
(
x
,
false
))
AtomicReference
<
Double
>
totalprice
=
getDoubleAtomicReference
(
ipResourceRequestDto
,
ipPlatForm
);
.
forEach
(
ipResource
->
{
String
unit
=
ipResourceRequestDto
.
getUnit
();
List
<
String
>
prices
;
double
newprice
=
unit
.
equals
(
"week"
)
?
(
int
)
(
totalprice
.
get
()
/
3
)
:
totalprice
.
get
().
intValue
();
if
(!
ipResourceRequestDto
.
getVendor
().
equals
(
"own"
))
prices
=
priceList
.
get
(
ipResource
.
getRegionCn
());
else
{
if
(
ipResource
.
isSpecialLine
())
prices
=
priceList
.
get
(
"自有专线"
);
else
prices
=
priceList
.
get
(
"自有"
);
}
for
(
String
vendorprice
:
prices
)
{
if
(
ipResource
.
getVendor
().
getValue
().
equals
(
vendorprice
.
substring
(
0
,
vendorprice
.
indexOf
(
"-"
))))
totalprice
.
updateAndGet
(
v
->
new
Double
(
v
+
Double
.
valueOf
(
vendorprice
.
substring
(
vendorprice
.
lastIndexOf
(
"-"
)
+
1
))));
}
});
double
newprice
=
ipResourceRequestDto
.
getUnit
().
equals
(
"week"
)
?
(
int
)
(
totalprice
.
get
()
/
3
)
:
totalprice
.
get
().
intValue
();
// 4. 计算总共需要花费多少钱 并 校验账户余额 够不够扣费, todo 这个地方 * ipResourceRequestDto.getAmount() 应该是多余的,因为在续费的时候,这个值始终为1
IpChargeResultDto
ipChargeResultDto
=
accountService
.
preChargeByMoney
(
username
,
newprice
*
ipResourceRequestDto
.
getPeriod
()
*
ipResourceRequestDto
.
getAmount
());
IpChargeResultDto
ipChargeResultDto
=
accountService
.
preChargeByMoney
(
username
,
newprice
*
ipResourceRequestDto
.
getPeriod
()
*
ipResourceRequestDto
.
getAmount
());
if
(!
ipChargeResultDto
.
isSuccess
())
{
if
(!
ipChargeResultDto
.
isSuccess
())
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOTENOUGHBALANCE
);
throw
new
ClientRequestException
(
AccountErrorCode
.
NOTENOUGHBALANCE
);
}
}
IpOperationResultDto
ipOperationResultDto
=
new
IpOperationResultDto
();
int
period
=
ipResourceRequestDto
.
getPeriod
();
int
period
=
0
;
// 5. 按月充值充6送1
period
=
ipResourceRequestDto
.
getPeriod
();
period
=
setAndGetPeriod
(
unit
,
period
);
//充6送1
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"month"
)
&&
ipResourceRequestDto
.
getPeriod
()
==
6
)
period
=
7
;
else
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"month"
)
&&
ipResourceRequestDto
.
getPeriod
()
==
12
)
period
=
14
;
// 6. 实际业务逻辑
IpOperationResultDto
ipOperationResultDto
=
new
IpOperationResultDto
();
List
<
String
>
failedList
=
ipResourceRequestDto
.
getAddr
();
if
(
ipResourceRequestDto
.
getAddr
()
!=
null
&&
ipResourceRequestDto
.
getAddr
().
size
()
>
0
)
{
if
(
ipResourceRequestDto
.
getAddr
()
!=
null
&&
ipResourceRequestDto
.
getAddr
().
size
()
>
0
)
{
RestTemplate
restTemplate
=
new
RestTemplate
();
// 6.1 远程调用 云E 续费 传入的所有的 ip,校验是否续费成功
HashMap
<
String
,
Object
>
map
=
new
HashMap
<>();
RenewIpResultDto
renewIpResultDto
=
getRenewIpResultDto
(
ipResourceRequestDto
,
period
);
map
.
put
(
"iplist"
,
ipResourceRequestDto
.
getAddr
());
map
.
put
(
"period"
,
period
);
map
.
put
(
"unit"
,
ipResourceRequestDto
.
getUnit
());
HttpHeaders
headers
=
buildPostHeader
();
HttpEntity
<
Map
<
String
,
Object
>>
entity
=
new
HttpEntity
<>(
map
,
headers
);
ResponseEntity
<
String
>
result
=
restTemplate
.
exchange
(
URL
+
"/intelligroup/renewip?accountId=browser"
,
HttpMethod
.
PUT
,
entity
,
String
.
class
);
RenewIpResultDto
renewIpResultDto
=
JSON
.
parseObject
(
result
.
getBody
(),
RenewIpResultDto
.
class
);
if
(
StringUtils
.
isNotBlank
(
renewIpResultDto
.
getErrorCode
()))
{
if
(
StringUtils
.
isNotBlank
(
renewIpResultDto
.
getErrorCode
()))
{
logger
.
error
(
renewIpResultDto
.
getErrorCode
());
logger
.
error
(
renewIpResultDto
.
getErrorCode
());
throw
new
ClientRequestException
(
BrowserErrorCode
.
UNKNOWN
);
throw
new
ClientRequestException
(
BrowserErrorCode
.
UNKNOWN
);
}
}
renewIpResultDto
.
getIplist
().
forEach
(
x
->
{
// 6.2 远程续费成功后的业务
IpResource
ipResource
=
ipResourceRepository
.
findByAddrAndIsDeleted
(
x
.
getIp
(),
false
);
renewIpResultDto
.
getIplist
().
forEach
(
List
<
String
>
vendorPrices
=
priceList
.
get
(
ipResource
.
getRegionCn
());
x
->
{
String
price
=
vendorPrices
.
stream
()
IpResource
ipResource
=
ipResourceRepository
.
findByAddrAndIsDeleted
(
x
.
getIp
(),
false
);
.
filter
(
vendorprice
->
ipResource
.
getVendor
().
getValue
().
equals
(
vendorprice
.
substring
(
0
,
vendorprice
.
indexOf
(
"-"
))))
List
<
String
>
vendorPrices
=
ipPlatForm
.
get
(
ipResource
.
getRegionCn
());
.
map
(
vendorprice
->
vendorprice
.
substring
(
vendorprice
.
lastIndexOf
(
"-"
)
+
1
)).
collect
(
Collectors
.
joining
());
// 计算 ip 价格
String
price
=
vendorPrices
.
stream
()
double
newprice1
=
ipResourceRequestDto
.
getUnit
().
equals
(
"week"
)
?
(
Integer
.
valueOf
(
price
)
/
3
)
:
Integer
.
valueOf
(
price
);
.
filter
(
vendorprice
->
ipResource
.
getVendor
().
getValue
().
equals
(
vendorprice
.
substring
(
0
,
vendorprice
.
indexOf
(
"-"
))))
IpChargeRequestDto
ipChargeRequestDto
=
buildIpChargeRequestDto
(
ipResourceRequestDto
,
2
,
ipResourceRequestDto
.
getPayMethod
());
.
map
(
vendorprice
->
vendorprice
.
substring
(
vendorprice
.
lastIndexOf
(
"-"
)
+
1
))
accountService
.
chargeByMoney
(
username
,
newprice1
*
ipResourceRequestDto
.
getPeriod
()
*
ipResourceRequestDto
.
getAmount
(),
ipChargeRequestDto
);
.
collect
(
Collectors
.
joining
());
double
newprice1
=
ipResourceRequestDto
.
getUnit
().
equals
(
"week"
)
?
(
Integer
.
valueOf
(
price
)
/
3
)
:
Integer
.
valueOf
(
price
);
ipResource
.
setValidTime
(
Instant
.
parse
(
x
.
getValidTill
()).
toEpochMilli
());
ipResourceRepository
.
save
(
ipResource
);
// 封装 ip 的续费 信息
ipOperationResultDto
.
getSuccessList
().
add
(
x
.
getIp
());
IpChargeRequestDto
ipChargeRequestDto
=
buildIpChargeRequestDto
(
ipResourceRequestDto
,
2
,
ipResourceRequestDto
.
getPayMethod
());
ipChargeRequestDto
.
setChargeIp
(
x
.
getIp
());
// 用来记录购买与消费的操作 userbalance 与 费用明细 userprepaidbilling
accountService
.
chargeByMoney
(
username
,
newprice1
*
ipResourceRequestDto
.
getPeriod
()
*
ipResourceRequestDto
.
getAmount
(),
ipChargeRequestDto
);
// 更新ip资源的到期时间
ipResource
.
setValidTime
(
Instant
.
parse
(
x
.
getValidTill
()).
toEpochMilli
());
ipResourceRepository
.
save
(
ipResource
);
failedList
.
remove
(
x
.
getIp
());
// 封装 ipOperationResultDto
});
ipOperationResultDto
.
getSuccessList
().
add
(
x
.
getIp
());
failedList
.
remove
(
x
.
getIp
());
}
);
// 封装 ipOperationResultDto
ipOperationResultDto
.
setFailList
(
failedList
);
ipOperationResultDto
.
setFailList
(
failedList
);
}
}
if
(
ipResourceRequestDto
.
getIpId
()
!=
null
&&
ipResourceRequestDto
.
getIpId
().
size
()
>
0
)
{
if
(
ipResourceRequestDto
.
getIpId
()
!=
null
&&
ipResourceRequestDto
.
getIpId
().
size
()
>
0
)
{
int
finalPeriod
=
period
;
int
finalPeriod
=
period
;
ipResourceRequestDto
.
getIpId
().
forEach
(
x
->
{
ipResourceRequestDto
.
getIpId
().
forEach
(
IpResource
ipResource
=
ipResourceRepository
.
findByIdAndIsDeleted
(
x
,
false
);
x
->
{
List
<
String
>
vendorPrices
;
IpResource
ipResource
=
ipResourceRepository
.
findByIdAndIsDeleted
(
x
,
false
);
if
(!
ipResourceRequestDto
.
getVendor
().
equals
(
"own"
))
List
<
String
>
vendorPrices
;
vendorPrices
=
priceList
.
get
(
ipResource
.
getRegionCn
());
if
(!
ipResourceRequestDto
.
getVendor
().
equals
(
"own"
))
else
{
vendorPrices
=
ipPlatForm
.
get
(
ipResource
.
getRegionCn
());
if
(
ipResource
.
isSpecialLine
())
else
{
vendorPrices
=
priceList
.
get
(
"自有专线"
);
if
(
ipResource
.
isSpecialLine
())
else
vendorPrices
=
ipPlatForm
.
get
(
"自有专线"
);
vendorPrices
=
priceList
.
get
(
"自有"
);
else
}
vendorPrices
=
ipPlatForm
.
get
(
"自有"
);
}
String
price
=
vendorPrices
.
stream
()
.
filter
(
vendorprice
->
ipResource
.
getVendor
().
getValue
().
equals
(
vendorprice
.
substring
(
0
,
vendorprice
.
indexOf
(
"-"
))))
.
map
(
vendorprice
->
vendorprice
.
substring
(
vendorprice
.
lastIndexOf
(
"-"
)
+
1
)).
collect
(
Collectors
.
joining
());
double
newprice1
=
ipResourceRequestDto
.
getUnit
().
equals
(
"week"
)
?
(
Integer
.
valueOf
(
price
)
/
3
)
:
Integer
.
valueOf
(
price
);
IpChargeRequestDto
ipChargeRequestDto
=
buildIpChargeRequestDto
(
ipResourceRequestDto
,
2
,
ipResourceRequestDto
.
getPayMethod
());
accountService
.
chargeByMoney
(
username
,
newprice1
*
ipResourceRequestDto
.
getPeriod
()
*
ipResourceRequestDto
.
getAmount
(),
ipChargeRequestDto
);
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"week"
))
{
String
price
=
vendorPrices
.
stream
()
ipResource
.
setValidTime
(
Instant
.
ofEpochMilli
(
ipResource
.
getValidTime
()).
atZone
(
ZoneOffset
.
UTC
)
.
filter
(
vendorprice
->
ipResource
.
getVendor
().
getValue
().
equals
(
vendorprice
.
substring
(
0
,
vendorprice
.
indexOf
(
"-"
))))
.
plusWeeks
(
finalPeriod
).
toInstant
().
toEpochMilli
());
.
map
(
vendorprice
->
vendorprice
.
substring
(
vendorprice
.
lastIndexOf
(
"-"
)
+
1
))
}
else
{
.
collect
(
Collectors
.
joining
());
ipResource
.
setValidTime
(
Instant
.
ofEpochMilli
(
ipResource
.
getValidTime
()).
atZone
(
ZoneOffset
.
UTC
)
.
plusMonths
(
finalPeriod
).
toInstant
().
toEpochMilli
());
double
newprice1
=
ipResourceRequestDto
.
getUnit
().
equals
(
"week"
)
?
(
Integer
.
valueOf
(
price
)
/
3
)
:
Integer
.
valueOf
(
price
);
}
IpChargeRequestDto
ipChargeRequestDto
=
buildIpChargeRequestDto
(
ipResourceRequestDto
,
2
,
ipResourceRequestDto
.
getPayMethod
());
ipResourceRepository
.
save
(
ipResource
);
ipChargeRequestDto
.
setChargeIp
(
x
);
ipOperationResultDto
.
getSuccessList
().
add
(
x
);
accountService
.
chargeByMoney
(
username
,
newprice1
*
ipResourceRequestDto
.
getPeriod
()
*
ipResourceRequestDto
.
getAmount
(),
ipChargeRequestDto
);
if
(
ipResourceRequestDto
.
getUnit
().
equals
(
"week"
))
{
ipResource
.
setValidTime
(
Instant
.
ofEpochMilli
(
ipResource
.
getValidTime
()).
atZone
(
ZoneOffset
.
UTC
)
.
plusWeeks
(
finalPeriod
).
toInstant
().
toEpochMilli
());
}
else
{
ipResource
.
setValidTime
(
Instant
.
ofEpochMilli
(
ipResource
.
getValidTime
()).
atZone
(
ZoneOffset
.
UTC
)
.
plusMonths
(
finalPeriod
).
toInstant
().
toEpochMilli
());
}
ipResourceRepository
.
save
(
ipResource
);
ipOperationResultDto
.
getSuccessList
().
add
(
x
);
failedList
.
remove
(
x
);
failedList
.
remove
(
x
);
});
}
);
}
}
return
ipOperationResultDto
;
return
ipOperationResultDto
;
...
@@ -686,8 +703,6 @@ public class IpResourceServiceImpl implements IpResourceService {
...
@@ -686,8 +703,6 @@ public class IpResourceServiceImpl implements IpResourceService {
@Override
@Override
public
boolean
queryIpExist
(
String
username
,
IpResourceUpdateDto
ipResourceUpdateDto
)
{
public
boolean
queryIpExist
(
String
username
,
IpResourceUpdateDto
ipResourceUpdateDto
)
{
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
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
);
IpResource
ipResource
=
ipResourceRepository
.
findByAddrAndIsDeleted
(
ipResourceUpdateDto
.
getAddr
(),
false
);
if
(
ipResource
!=
null
)
{
if
(
ipResource
!=
null
)
{
return
true
;
return
true
;
...
@@ -698,8 +713,6 @@ public class IpResourceServiceImpl implements IpResourceService {
...
@@ -698,8 +713,6 @@ public class IpResourceServiceImpl implements IpResourceService {
@Override
@Override
public
IpResourceDto
queryIp
(
String
username
,
IpResourceRequestDto
ipResourceRequestDto
)
{
public
IpResourceDto
queryIp
(
String
username
,
IpResourceRequestDto
ipResourceRequestDto
)
{
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
==
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
IpResource
ipResource
=
null
;
IpResource
ipResource
=
null
;
if
(
ipResourceRequestDto
.
getAddr
()
!=
null
&&
ipResourceRequestDto
.
getAddr
().
size
()
>
0
)
if
(
ipResourceRequestDto
.
getAddr
()
!=
null
&&
ipResourceRequestDto
.
getAddr
().
size
()
>
0
)
ipResource
=
ipResourceRepository
.
findByAddrAndIsDeleted
(
ipResourceRequestDto
.
getAddr
().
get
(
0
),
false
);
ipResource
=
ipResourceRepository
.
findByAddrAndIsDeleted
(
ipResourceRequestDto
.
getAddr
().
get
(
0
),
false
);
...
@@ -775,13 +788,14 @@ public class IpResourceServiceImpl implements IpResourceService {
...
@@ -775,13 +788,14 @@ public class IpResourceServiceImpl implements IpResourceService {
}
}
}
}
private
IpChargeRequestDto
buildIpChargeRequestDto
(
IpResourceRequestDto
request
,
int
chargeType
,
int
payMethod
)
{
private
IpChargeRequestDto
buildIpChargeRequestDto
(
IpResourceRequestDto
request
,
int
chargeType
,
int
payMethod
)
{
IpChargeRequestDto
ipChargeRequestDto
=
new
IpChargeRequestDto
();
IpChargeRequestDto
ipChargeRequestDto
=
new
IpChargeRequestDto
();
ipChargeRequestDto
.
setAmount
(
request
.
getAmount
());
ipChargeRequestDto
.
setAmount
(
request
.
getAmount
());
ipChargeRequestDto
.
setChargeType
(
chargeType
);
ipChargeRequestDto
.
setRegion
(
request
.
getRegion
());
ipChargeRequestDto
.
setRegion
(
request
.
getRegion
());
ipChargeRequestDto
.
setPeriod
(
request
.
getPeriod
());
ipChargeRequestDto
.
setPeriod
(
request
.
getPeriod
());
ipChargeRequestDto
.
setUnit
(
request
.
getUnit
());
ipChargeRequestDto
.
setUnit
(
request
.
getUnit
());
ipChargeRequestDto
.
setChargeType
(
chargeType
);
ipChargeRequestDto
.
setPayMethod
(
payMethod
);
ipChargeRequestDto
.
setPayMethod
(
payMethod
);
return
ipChargeRequestDto
;
return
ipChargeRequestDto
;
}
}
...
@@ -897,4 +911,57 @@ public class IpResourceServiceImpl implements IpResourceService {
...
@@ -897,4 +911,57 @@ public class IpResourceServiceImpl implements IpResourceService {
}
}
return
ipResources1
;
return
ipResources1
;
}
}
private
int
setAndGetPeriod
(
String
unit
,
int
period
)
{
if
(
unit
.
equals
(
"month"
))
{
if
(
period
==
6
)
{
period
=
7
;
}
if
(
period
==
12
)
{
period
=
14
;
}
}
return
period
;
}
@NotNull
private
AtomicReference
<
Double
>
getDoubleAtomicReference
(
IpResourceRequestDto
ipResourceRequestDto
,
Map
<
String
,
List
<
String
>>
ipPlatForm
)
{
AtomicReference
<
Double
>
totalprice
=
new
AtomicReference
<>((
double
)
0
);
ipResourceRequestDto
.
getAddr
().
stream
()
.
map
(
x
->
ipResourceRepository
.
findByAddrAndIsDeleted
(
x
,
false
))
.
forEach
(
ipResource
->
{
List
<
String
>
regionAndPriceInfos
;
if
(!
ipResourceRequestDto
.
getVendor
().
equals
(
"own"
))
{
regionAndPriceInfos
=
ipPlatForm
.
get
(
ipResource
.
getRegionCn
());
}
else
{
regionAndPriceInfos
=
ipResource
.
isSpecialLine
()
?
ipPlatForm
.
get
(
"自有专线"
)
:
ipPlatForm
.
get
(
"自有"
);
}
for
(
String
vendorprice
:
regionAndPriceInfos
)
{
if
(
ipResource
.
getVendor
().
getValue
().
equals
(
vendorprice
.
substring
(
0
,
vendorprice
.
indexOf
(
"-"
))))
totalprice
.
updateAndGet
(
v
->
new
Double
(
v
+
Double
.
valueOf
(
vendorprice
.
substring
(
vendorprice
.
lastIndexOf
(
"-"
)
+
1
)))
);
}
}
);
return
totalprice
;
}
@Nullable
private
RenewIpResultDto
getRenewIpResultDto
(
IpResourceRequestDto
ipResourceRequestDto
,
int
period
)
{
RestTemplate
restTemplate
=
new
RestTemplate
();
HashMap
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"iplist"
,
ipResourceRequestDto
.
getAddr
());
map
.
put
(
"period"
,
period
);
map
.
put
(
"unit"
,
ipResourceRequestDto
.
getUnit
());
HttpHeaders
headers
=
buildPostHeader
();
HttpEntity
<
Map
<
String
,
Object
>>
entity
=
new
HttpEntity
<>(
map
,
headers
);
String
URL
=
(
profiles
.
equals
(
"dev"
)
||
profiles
.
equals
(
"staging"
))
?
TESTURL
:
CLOUDAMURL
;
ResponseEntity
<
String
>
result
=
restTemplate
.
exchange
(
URL
+
"/intelligroup/renewip?accountId=browser"
,
HttpMethod
.
PUT
,
entity
,
String
.
class
);
return
JSON
.
parseObject
(
result
.
getBody
(),
RenewIpResultDto
.
class
);
}
}
}
src/main/java/com/edgec/browserbackend/browser/service/Impl/ShopServiceImpl.java
View file @
1ff453f7
...
@@ -26,9 +26,7 @@ import org.springframework.web.multipart.MultipartFile;
...
@@ -26,9 +26,7 @@ import org.springframework.web.multipart.MultipartFile;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.time.Instant
;
import
java.time.Instant
;
import
java.util.ArrayList
;
import
java.util.*
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
@Service
@Service
...
@@ -69,12 +67,12 @@ public class ShopServiceImpl implements ShopService {
...
@@ -69,12 +67,12 @@ public class ShopServiceImpl implements ShopService {
}
}
if
(
shopResultDto
.
getGroup
()
!=
null
)
{
if
(
shopResultDto
.
getGroup
()
!=
null
)
{
Group
group
=
groupRepository
.
findById
(
shopResultDto
.
getGroup
())
Group
group
=
groupRepository
.
findById
(
shopResultDto
.
getGroup
())
.
orElseThrow
(()
->
new
ClientRequestException
(
BrowserErrorCode
.
GROUPNOTEXIST
));
.
orElseThrow
(()
->
new
ClientRequestException
(
BrowserErrorCode
.
GROUPNOTEXIST
));
}
}
// 2. 对当前用户的 account 信息进行校验
// 2. 对当前用户的 account 信息进行校验
Account
account
=
accountRepository
.
findByName
(
username
)
Account
account
=
accountRepository
.
findByName
(
username
)
.
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
.
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
.
getPermission
()
<
4
)
{
if
(
account
.
getPermission
()
<
4
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
}
}
...
@@ -102,7 +100,7 @@ public class ShopServiceImpl implements ShopService {
...
@@ -102,7 +100,7 @@ public class ShopServiceImpl implements ShopService {
userShopRepository
.
save
(
userShop1
);
userShopRepository
.
save
(
userShop1
);
Account
parentAccount
=
accountRepository
.
findByName
(
account
.
getParent
())
Account
parentAccount
=
accountRepository
.
findByName
(
account
.
getParent
())
.
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
.
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
account
.
setShopCount
(
parentAccount
.
getShopCount
()
+
1
);
account
.
setShopCount
(
parentAccount
.
getShopCount
()
+
1
);
accountRepository
.
save
(
parentAccount
);
accountRepository
.
save
(
parentAccount
);
}
}
...
@@ -161,7 +159,7 @@ public class ShopServiceImpl implements ShopService {
...
@@ -161,7 +159,7 @@ public class ShopServiceImpl implements ShopService {
userShopRepository
.
save
(
userShop1
);
userShopRepository
.
save
(
userShop1
);
Account
parentAccount
=
accountRepository
.
findByName
(
account
.
getParent
())
Account
parentAccount
=
accountRepository
.
findByName
(
account
.
getParent
())
.
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
.
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
account
.
setShopCount
(
parentAccount
.
getShopCount
()
+
1
);
account
.
setShopCount
(
parentAccount
.
getShopCount
()
+
1
);
accountRepository
.
save
(
parentAccount
);
accountRepository
.
save
(
parentAccount
);
}
}
...
@@ -236,20 +234,19 @@ public class ShopServiceImpl implements ShopService {
...
@@ -236,20 +234,19 @@ public class ShopServiceImpl implements ShopService {
ipAndShopService
.
unBindShop
(
username
,
shopRequestDto
);
ipAndShopService
.
unBindShop
(
username
,
shopRequestDto
);
}
}
// 4. 删除当前店铺关联的所有的 usershop信息
// 4. 获取当前店铺关联的所有的 usershop信息 并删除数据库中 usershop信息
List
<
UserShop
>
userShops
=
userShopRepository
.
findByShopId
(
shopId
);
boolean
result
=
userShopRepository
.
deleteByShopId
(
shopId
);
boolean
result
=
userShopRepository
.
deleteByShopId
(
shopId
);
if
(
result
)
{
if
(
result
)
{
// 5. 删除当前店铺,并更新 account信息 todo
// 5. 删除当前店铺,并更新 account信息 todo
shopRepository
.
deleteById
(
shopId
);
shopRepository
.
deleteById
(
shopId
);
// 6. 更新 和店铺关联的所有的 account 的信息
// 6. 更新 和店铺关联的所有的 account 的信息
List
<
UserShop
>
userShops
=
userShopRepository
.
findByShopId
(
shopId
);
List
<
Account
>
accountList
=
accountRepository
.
findByNameIn
(
userShops
.
stream
().
map
(
UserShop:
:
getUsername
).
collect
(
Collectors
.
toList
()));
List
<
Account
>
accountList
=
accountRepository
.
findByNameIn
(
userShops
.
stream
().
map
(
UserShop:
:
getUsername
).
collect
(
Collectors
.
toList
()));
for
(
Account
a
:
accountList
)
{
for
(
Account
a
:
accountList
)
{
a
.
setShopCount
(
account
.
getShopCount
()
-
1
);
a
.
setShopCount
(
account
.
getShopCount
()
-
1
);
accountRepository
.
save
(
a
);
accountRepository
.
save
(
a
);
}
}
}
else
{
}
else
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
}
}
...
@@ -257,15 +254,25 @@ public class ShopServiceImpl implements ShopService {
...
@@ -257,15 +254,25 @@ public class ShopServiceImpl implements ShopService {
@Override
@Override
public
void
transferShop
(
String
username
,
String
shopId
,
String
groupId
)
{
public
void
transferShop
(
String
username
,
String
shopId
,
String
groupId
)
{
// 1. 校验当前用户是否存在账户信息
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
// 2. 校验 shop 是否存在
UserShop
userShop
=
userShopRepository
.
findByUsernameAndShopId
(
username
,
shopId
);
Shop
shop
=
shopRepository
.
findById
(
shopId
).
orElseThrow
(()
->
new
ClientRequestException
(
BrowserErrorCode
.
SHOPNOTEXIST
));
// 3. 校验 分组信息是否存在 以及 group 是否是当前 用户创建
Group
group
=
groupRepository
.
findById
(
groupId
).
orElseThrow
(()
->
new
ClientRequestException
(
BrowserErrorCode
.
GROUPNOTEXIST
));
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
);
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
{
try
{
// 5.更该 店铺 分组 并保存
userShop
.
setGroupId
(
groupId
);
userShop
.
setGroupId
(
groupId
);
userShopRepository
.
save
(
userShop
);
userShopRepository
.
save
(
userShop
);
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
...
@@ -277,109 +284,127 @@ public class ShopServiceImpl implements ShopService {
...
@@ -277,109 +284,127 @@ public class ShopServiceImpl implements ShopService {
@Override
@Override
public
void
assignShops
(
String
username
,
List
<
String
>
shopIds
,
List
<
String
>
users
)
{
public
void
assignShops
(
String
username
,
List
<
String
>
shopIds
,
List
<
String
>
users
)
{
// 1. 查询当前登录用户信息,并校验是否具备分配权限
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
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
);
List
<
UserShop
>
userShops
=
userShopRepository
.
findByUsernameAndShopIdIn
(
username
,
shopIds
);
if
(
account
.
getPermission
()
<
8
||
userShops
==
null
||
userShops
.
size
()
<
1
)
{
if
(
userShops
==
null
||
userShops
.
size
()
<
1
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
throw
new
ClientRequestException
(
BrowserErrorCode
.
USER_NOT_BIND_SHOP
);
}
}
Pageable
pageable
=
PageRequest
.
of
(
0
,
100
);
List
<
Shop
>
shops
=
shopRepository
.
findByShopIdInOrderByCreateTimeDesc
(
shopIds
,
pageable
).
getContent
();
// 3. 查询 目标 账户是否存在,并获取账户信息。注意:目标账户只能是当前用户的子账户
if
(
shops
==
null
||
shops
.
size
()
<
1
)
throw
new
ClientRequestException
(
BrowserErrorCode
.
SHOPNOTEXIST
);
List
<
Account
>
accounts
=
accountRepository
.
findByNameIn
(
users
);
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
);
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
}
for
(
Account
ac
:
accounts
)
{
for
(
Account
ac
:
accounts
)
{
if
(
ac
.
getParent
()
==
null
||
!
ac
.
getParent
().
equals
(
username
))
{
if
(
ac
.
getParent
()
==
null
||
!
ac
.
getParent
().
equals
(
username
))
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
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
());
userShopRepository
.
deleteByShopIdExceptOwner
(
shops
.
get
(
0
).
getShopId
(),
shops
.
get
(
0
).
getOwner
());
for
(
Account
account1
:
accounts
)
{
for
(
Account
account1
:
accounts
)
{
// todo 这个地方有点多余
UserShop
userShop1
=
userShopRepository
.
findByUsernameAndShopId
(
account1
.
getName
(),
shops
.
get
(
0
).
getShopId
());
UserShop
userShop1
=
userShopRepository
.
findByUsernameAndShopId
(
account1
.
getName
(),
shops
.
get
(
0
).
getShopId
());
if
(
userShop1
!=
null
)
if
(
userShop1
!=
null
)
{
continue
;
continue
;
}
// 6. 封装 目标账户的 usershop信息并保存
userShop1
=
new
UserShop
(
account1
.
getName
(),
shops
.
get
(
0
).
getShopId
(),
"-1"
);
userShop1
=
new
UserShop
(
account1
.
getName
(),
shops
.
get
(
0
).
getShopId
(),
"-1"
);
userShopRepository
.
save
(
userShop1
);
userShopRepository
.
save
(
userShop1
);
}
}
}
catch
(
Exception
e
)
{
}
else
{
logger
.
error
(
"fail to assign"
,
e
.
getMessage
());
shops
.
forEach
(
throw
new
ClientRequestException
(
BrowserErrorCode
.
UNKNOWN
);
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
{
}
catch
(
Exception
e
)
{
shops
.
forEach
(
shop
->
{
logger
.
error
(
"fail to assign"
,
e
.
getMessage
());
try
{
throw
new
ClientRequestException
(
BrowserErrorCode
.
UNKNOWN
);
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
);
}
});
}
}
}
}
@Override
@Override
public
ShopResultDto
queryShop
(
String
username
,
String
shopId
)
{
public
ShopResultDto
queryShop
(
String
username
,
String
shopId
)
{
// 1. 校验 账户、商铺、IP资源、usershop 信息
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
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
);
UserShop
userShop
=
userShopRepository
.
findByUsernameAndShopId
(
username
,
shopId
);
if
(
userShop
==
null
)
{
if
(
userShop
==
null
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
}
}
Shop
shop
=
shopRepository
.
findById
(
shopId
).
orElse
(
null
);
if
(
shop
==
null
)
{
// 2. 封装 ip 资源信息
throw
new
ClientRequestException
(
BrowserErrorCode
.
SHOPNOTEXIST
);
IpResourceDto
ipResourceDto
=
null
;
}
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
;
if
(
ipResource
.
isSpecialLine
())
{
if
(
ipResource
.
isSpecialLine
())
{
SpecialLine
specialLine
=
specialLineRepository
.
findAll
().
get
(
0
);
SpecialLine
specialLine
=
specialLineRepository
.
findAll
().
get
(
0
);
shopResultDto
=
ShopResultDto
.
of
(
shop
,
group
,
new
IpResourceDto
(
ipResource
,
null
,
false
,
specialLine
));
ipResourceDto
=
new
IpResourceDto
(
ipResource
,
null
,
false
,
specialLine
);
}
else
shopResultDto
=
ShopResultDto
.
of
(
shop
,
group
,
new
IpResourceDto
(
ipResource
,
null
,
false
));
}
else
{
return
shopResultDto
;
ipResourceDto
=
new
IpResourceDto
(
ipResource
,
null
,
false
);
}
// 3. 封装 ShopResultDto 信息并返回
return
ShopResultDto
.
of
(
shop
,
userShop
.
getGroupId
(),
ipResourceDto
);
}
}
@Override
@Override
public
ShopPageResultDto
getShopList
(
String
username
,
String
groupId
,
int
pageNum
,
int
amount
,
ShopFilterDto
shopFilterDto
)
{
public
ShopPageResultDto
getShopList
(
String
username
,
String
groupId
,
int
pageNum
,
int
amount
,
ShopFilterDto
shopFilterDto
)
{
// 当前登录用户的账户是否存在
//
1. 校验
当前登录用户的账户是否存在
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
// 当前查询的分组信息是否正确
//
2. 如有有分组校验
当前查询的分组信息是否正确
Group
group
=
null
;
Group
group
=
null
;
if
(
groupId
!=
null
)
{
if
(
groupId
!=
null
)
{
group
=
groupRepository
.
findById
(
groupId
).
orElseThrow
(()
->
new
ClientRequestException
(
BrowserErrorCode
.
GROUPNOTEXIST
));
group
=
groupRepository
.
findById
(
groupId
).
orElseThrow
(()
->
new
ClientRequestException
(
BrowserErrorCode
.
GROUPNOTEXIST
));
if
(
group
.
getOwner
()
!=
null
&&
!
group
.
getOwner
().
equals
(
username
))
{
if
(
group
.
getOwner
()
!=
null
&&
!
group
.
getOwner
().
equals
(
username
))
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
}
}
}
}
// 根据 groupId 与 username 来查询 shopIds (如果当前用户是父账户,则查询结果包含子账户的shopId)
//
3.
根据 groupId 与 username 来查询 shopIds (如果当前用户是父账户,则查询结果包含子账户的shopId)
List
<
String
>
allIds
=
null
;
List
<
String
>
allIds
=
null
;
if
(
"-1"
.
equals
(
groupId
))
{
if
(
"-1"
.
equals
(
groupId
))
{
allIds
=
userShopRepository
.
findByUsername
(
username
).
stream
()
allIds
=
userShopRepository
.
findByUsername
(
username
).
stream
()
.
map
(
UserShop:
:
getShopId
).
collect
(
Collectors
.
toList
());
.
map
(
UserShop:
:
getShopId
).
collect
(
Collectors
.
toList
());
// 如果分组
// 如果分组
}
else
{
}
else
{
allIds
=
userShopRepository
.
findByUsernameAndGroupId
(
username
,
groupId
).
stream
()
allIds
=
userShopRepository
.
findByUsernameAndGroupId
(
username
,
groupId
).
stream
()
.
map
(
UserShop:
:
getShopId
).
collect
(
Collectors
.
toList
());
.
map
(
UserShop:
:
getShopId
).
collect
(
Collectors
.
toList
());
}
}
List
<
String
>
shopIds
=
new
ArrayList
<>();
// 4. 根据传入的过滤条件得到 shopIds
List
<
String
>
shopIds
=
null
;
if
(
shopFilterDto
.
getBindIp
()
==
0
)
{
if
(
shopFilterDto
.
getBindIp
()
==
0
)
{
shopIds
=
allIds
;
shopIds
=
allIds
;
...
@@ -387,28 +412,30 @@ public class ShopServiceImpl implements ShopService {
...
@@ -387,28 +412,30 @@ public class ShopServiceImpl implements ShopService {
}
else
if
(
shopFilterDto
.
getBindIp
()
==
1
)
{
}
else
if
(
shopFilterDto
.
getBindIp
()
==
1
)
{
// ip资源已经分配了 的店铺
// ip资源已经分配了 的店铺
shopIds
=
ipResourceRepository
.
findShopIdInList
(
allIds
,
false
).
stream
()
shopIds
=
ipResourceRepository
.
findShopIdInList
(
allIds
,
false
).
stream
()
.
flatMap
((
x
->
x
.
getShopIds
().
stream
())).
collect
(
Collectors
.
toList
());
.
flatMap
((
x
->
x
.
getShopIds
().
stream
())).
collect
(
Collectors
.
toList
());
// 这个地方的其他情况是指什么情况
// 这个地方的其他情况是指什么情况
}
else
{
}
else
{
// 店铺绑定的 ip 资源被删除的 店铺
// 店铺绑定的 ip 资源被删除的 店铺
for
(
String
id
:
allIds
)
{
for
(
String
id
:
allIds
)
{
IpResource
ipResource
=
ipResourceRepository
.
findFirstByShopIdsIsAndIsDeleted
(
id
,
fals
e
);
IpResource
ipResource
=
ipResourceRepository
.
findFirstByShopIdsIsAndIsDeleted
(
id
,
tru
e
);
if
(
ipResource
=
=
null
)
{
if
(
ipResource
!
=
null
)
{
shopIds
.
add
(
id
);
shopIds
.
add
(
id
);
}
}
}
}
}
}
amount
=
Math
.
min
(
amount
,
100
)
;
amount
=
amount
>
100
?
100
:
amount
;
Pageable
pageable
=
PageRequest
.
of
(
pageNum
,
amount
);
Pageable
pageable
=
PageRequest
.
of
(
pageNum
,
amount
);
//
根据商铺ids 与 过滤条件 得到商铺
//
5. 根据过滤后的商铺ids 与 其他过滤条件 得到商铺信息并分页
Page
<
Shop
>
shops
=
getShopsByFilter
(
shopFilterDto
,
shopIds
,
pageable
);
Page
<
Shop
>
shops
=
getShopsByFilter
(
shopFilterDto
,
shopIds
,
pageable
);
// 6. 封装 shopResultDtos 信息并返回
ShopPageResultDto
<
ShopResultDto
>
shopPageResultDto
=
new
ShopPageResultDto
<>();
ShopPageResultDto
<
ShopResultDto
>
shopPageResultDto
=
new
ShopPageResultDto
<>();
if
(
shops
!=
null
&&
shops
.
getNumberOfElements
()
>=
1
)
{
if
(
shops
!=
null
&&
shops
.
getNumberOfElements
()
>=
1
)
{
List
<
ShopResultDto
>
shopResultDtos
=
new
ArrayList
<>();
List
<
ShopResultDto
>
shopResultDtos
=
new
ArrayList
<>();
shops
.
getContent
().
forEach
(
shops
.
getContent
().
stream
().
forEach
(
// 设置ip资源状态 并 封装 shopResultDto信息
x
->
{
x
->
{
IpResource
ipResource
=
ipResourceRepository
.
findFirstByShopIdsIsAndIsDeleted
(
x
.
getShopId
(),
false
);
IpResource
ipResource
=
ipResourceRepository
.
findFirstByShopIdsIsAndIsDeleted
(
x
.
getShopId
(),
false
);
// 如果 ip资源非空 且 addr 也非空
// 如果 ip资源非空 且 addr 也非空
...
@@ -446,7 +473,7 @@ public class ShopServiceImpl implements ShopService {
...
@@ -446,7 +473,7 @@ public class ShopServiceImpl implements ShopService {
ipResource
.
setStatus
(
1
);
ipResource
.
setStatus
(
1
);
ipResourceRepository
.
save
(
ipResource
);
ipResourceRepository
.
save
(
ipResource
);
// 4. 其他
// 4. 其他
,将ip资源状态设置为 未使用
}
else
{
}
else
{
if
((
ipResource
.
getStatus
()
==
0
||
ipResource
.
getStatus
()
==
1
||
ipResource
.
getStatus
()
==
2
)
&&
ipResource
.
getVendor
()
!=
Vendor
.
local
)
{
if
((
ipResource
.
getStatus
()
==
0
||
ipResource
.
getStatus
()
==
1
||
ipResource
.
getStatus
()
==
2
)
&&
ipResource
.
getVendor
()
!=
Vendor
.
local
)
{
ipResource
.
setStatus
(
0
);
ipResource
.
setStatus
(
0
);
...
@@ -478,7 +505,7 @@ public class ShopServiceImpl implements ShopService {
...
@@ -478,7 +505,7 @@ public class ShopServiceImpl implements ShopService {
ShopSummary
shopSummary
=
new
ShopSummary
();
ShopSummary
shopSummary
=
new
ShopSummary
();
List
<
String
>
allShopIds
=
userShopRepository
.
findByUsername
(
username
).
stream
()
List
<
String
>
allShopIds
=
userShopRepository
.
findByUsername
(
username
).
stream
()
.
map
(
UserShop:
:
getShopId
).
collect
(
Collectors
.
toList
());
.
map
(
UserShop:
:
getShopId
).
collect
(
Collectors
.
toList
());
List
<
String
>
unbind
=
new
ArrayList
<>();
List
<
String
>
unbind
=
new
ArrayList
<>();
if
(!
allShopIds
.
isEmpty
())
{
if
(!
allShopIds
.
isEmpty
())
{
for
(
String
id
:
allShopIds
)
{
for
(
String
id
:
allShopIds
)
{
...
@@ -491,9 +518,9 @@ public class ShopServiceImpl implements ShopService {
...
@@ -491,9 +518,9 @@ public class ShopServiceImpl implements ShopService {
shopSummary
.
setUnbind
(
unbind
.
size
());
shopSummary
.
setUnbind
(
unbind
.
size
());
List
<
String
>
shopIds
=
userShopRepository
.
findByUsername
(
username
).
stream
()
List
<
String
>
shopIds
=
userShopRepository
.
findByUsername
(
username
).
stream
()
.
map
(
UserShop:
:
getShopId
).
collect
(
Collectors
.
toList
());
.
map
(
UserShop:
:
getShopId
).
collect
(
Collectors
.
toList
());
List
<
String
>
bind
=
ipResourceRepository
.
findShopIdInList
(
shopIds
,
false
).
stream
()
List
<
String
>
bind
=
ipResourceRepository
.
findShopIdInList
(
shopIds
,
false
).
stream
()
.
map
(
IpResource:
:
getId
).
collect
(
Collectors
.
toList
());
.
map
(
IpResource:
:
getId
).
collect
(
Collectors
.
toList
());
int
expired
=
ipResourceRepository
.
countByStatusAndIdInAndIsDeleted
(
1
,
bind
,
false
);
int
expired
=
ipResourceRepository
.
countByStatusAndIdInAndIsDeleted
(
1
,
bind
,
false
);
int
willexpired
=
ipResourceRepository
.
countByStatusAndIdInAndIsDeleted
(
2
,
bind
,
false
);
int
willexpired
=
ipResourceRepository
.
countByStatusAndIdInAndIsDeleted
(
2
,
bind
,
false
);
shopSummary
.
setExpired
(
expired
);
shopSummary
.
setExpired
(
expired
);
...
@@ -505,21 +532,25 @@ public class ShopServiceImpl implements ShopService {
...
@@ -505,21 +532,25 @@ public class ShopServiceImpl implements ShopService {
@Override
@Override
public
List
<
String
>
getShopUsers
(
String
username
,
String
shopId
)
{
public
List
<
String
>
getShopUsers
(
String
username
,
String
shopId
)
{
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
==
null
)
if
(
account
.
getParent
()
!=
null
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
if
(
account
.
getParent
()
!=
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
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
;
return
shopUsers
;
}
}
@Override
@Override
public
List
<
String
>
getBatchShopUsers
(
String
username
,
List
<
String
>
shopIds
)
{
public
List
<
String
>
getBatchShopUsers
(
String
username
,
List
<
String
>
shopIds
)
{
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
Account
account
=
accountRepository
.
findByName
(
username
).
orElseThrow
(()
->
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
));
if
(
account
==
null
)
if
(
account
.
getParent
()
!=
null
)
{
throw
new
ClientRequestException
(
AccountErrorCode
.
NAMENOTEXIST
);
if
(
account
.
getParent
()
!=
null
)
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
throw
new
ClientRequestException
(
AccountErrorCode
.
NOPERMISSION
);
}
List
<
String
>
shopUsers
=
new
ArrayList
<>();
List
<
String
>
shopUsers
=
new
ArrayList
<>();
if
(
shopIds
!=
null
&&
shopIds
.
size
()
>
0
)
{
if
(
shopIds
!=
null
&&
shopIds
.
size
()
>
0
)
{
String
maxShopId
=
null
;
String
maxShopId
=
null
;
...
@@ -531,20 +562,85 @@ public class ShopServiceImpl implements ShopService {
...
@@ -531,20 +562,85 @@ public class ShopServiceImpl implements ShopService {
maxShopId
=
shopId
;
maxShopId
=
shopId
;
}
}
}
}
if
(
maxShopId
==
null
)
if
(
maxShopId
==
null
)
{
return
new
ArrayList
<>();
return
shopUsers
;
}
List
<
String
>
users
=
userShopRepository
.
findByShopId
(
maxShopId
).
stream
().
map
(
x
->
x
.
getUsername
()).
filter
(
x
->
!
x
.
equals
(
username
)).
collect
(
Collectors
.
toList
());
List
<
String
>
users
=
userShopRepository
.
findByShopId
(
maxShopId
).
stream
().
map
(
x
->
x
.
getUsername
()).
filter
(
x
->
!
x
.
equals
(
username
)).
collect
(
Collectors
.
toList
());
for
(
String
user
:
users
)
{
for
(
String
user
:
users
)
{
int
shopCount
=
userShopRepository
.
countByUsernameAndShopIdIn
(
user
,
shopIds
);
int
shopCount
=
userShopRepository
.
countByUsernameAndShopIdIn
(
user
,
shopIds
);
if
(
shopCount
<
shopIds
.
size
())
if
(
shopCount
<
shopIds
.
size
())
{
continue
;
continue
;
else
}
else
{
shopUsers
.
add
(
user
);
shopUsers
.
add
(
user
);
}
}
}
}
}
return
shopUsers
;
return
shopUsers
;
}
}
@Override
public
Integer
dealDirtyData
()
{
List
<
Account
>
all
=
accountRepository
.
findAll
();
List
<
Account
>
parents
=
all
.
stream
().
filter
(
x
->
StringUtils
.
isEmpty
(
x
.
getParent
())).
collect
(
Collectors
.
toList
());
// 移除 父账户,剩下的都是子账户了
all
.
removeAll
(
parents
);
// 父账户, 子账户列表
Map
<
Account
,
List
<
Account
>>
map
=
new
HashMap
<>();
for
(
Account
account
:
parents
)
{
List
<
Account
>
list
=
new
ArrayList
<>();
for
(
Account
child
:
all
)
{
if
(
child
.
getParent
().
equals
(
account
.
getName
()))
{
list
.
add
(
child
);
}
}
if
(
list
.
size
()
!=
0
)
{
map
.
put
(
account
,
list
);
}
}
Integer
result
=
0
;
Iterator
<
Map
.
Entry
<
Account
,
List
<
Account
>>>
entries
=
map
.
entrySet
().
iterator
();
while
(
entries
.
hasNext
())
{
Map
.
Entry
<
Account
,
List
<
Account
>>
entry
=
entries
.
next
();
List
<
Account
>
children
=
entry
.
getValue
();
// 查找子类所拥有的店铺
List
<
Shop
>
childrenShop
=
shopRepository
.
findByOwnerIn
(
children
.
stream
().
map
(
Account:
:
getName
).
collect
(
Collectors
.
toList
()));
if
(
childrenShop
.
size
()
==
0
)
{
continue
;
}
List
<
String
>
ids
=
childrenShop
.
stream
().
map
(
x
->
x
.
getShopId
()).
collect
(
Collectors
.
toList
());
// 查出来 父账户已有的 usershop信息
List
<
UserShop
>
parentUserShops
=
userShopRepository
.
findByUsernameAndShopIdIn
(
entry
.
getKey
().
getName
(),
ids
);
List
<
String
>
parentShopIds
=
parentUserShops
.
stream
().
map
(
x
->
x
.
getShopId
()).
collect
(
Collectors
.
toList
());
// 剩下的就是 父账户的 usershop 缺失的信息
ids
.
removeAll
(
parentShopIds
);
// 构建父账户缺失的 usershop 信息并保存
List
<
UserShop
>
userShops
=
new
ArrayList
<>();
for
(
String
id
:
ids
)
{
UserShop
userShop
=
new
UserShop
();
userShop
.
setShopId
(
id
);
userShop
.
setUsername
(
entry
.
getKey
().
getName
());
userShop
.
setGroupId
(
"-1"
);
userShops
.
add
(
userShop
);
}
if
(
userShops
.
size
()
!=
0
)
{
// 将父账户缺失的 usershop 信息补充完整
List
<
UserShop
>
userShops1
=
userShopRepository
.
saveAll
(
userShops
);
result
+=
userShops1
.
size
();
}
}
return
result
;
}
private
String
getShopId
(
String
username
,
ShopResultDto
shopResultDto
)
{
private
String
getShopId
(
String
username
,
ShopResultDto
shopResultDto
)
{
Shop
shop
=
new
Shop
();
Shop
shop
=
new
Shop
();
...
@@ -559,7 +655,7 @@ public class ShopServiceImpl implements ShopService {
...
@@ -559,7 +655,7 @@ public class ShopServiceImpl implements ShopService {
Page
<
Shop
>
shops
=
null
;
Page
<
Shop
>
shops
=
null
;
if
(!
StringUtils
.
isEmpty
(
shopFilterDto
.
getIpRegion
()))
{
if
(!
StringUtils
.
isEmpty
(
shopFilterDto
.
getIpRegion
()))
{
List
<
String
>
filter
=
ipResourceRepository
.
findShopIdInListAndRegionLike
(
shopIds
,
false
,
shopFilterDto
.
getIpRegion
())
List
<
String
>
filter
=
ipResourceRepository
.
findShopIdInListAndRegionLike
(
shopIds
,
false
,
shopFilterDto
.
getIpRegion
())
.
stream
().
flatMap
((
x
->
x
.
getShopIds
().
stream
())).
collect
(
Collectors
.
toList
());
.
stream
().
flatMap
((
x
->
x
.
getShopIds
().
stream
())).
collect
(
Collectors
.
toList
());
shops
=
shopRepository
.
findByShopIdInOrderByCreateTimeDesc
(
filter
,
pageable
);
shops
=
shopRepository
.
findByShopIdInOrderByCreateTimeDesc
(
filter
,
pageable
);
}
}
if
(!
StringUtils
.
isEmpty
(
shopFilterDto
.
getShopAccount
()))
{
if
(!
StringUtils
.
isEmpty
(
shopFilterDto
.
getShopAccount
()))
{
...
...
src/main/java/com/edgec/browserbackend/browser/service/Impl/TempServiceImpl.java
0 → 100644
View file @
1ff453f7
package
com
.
edgec
.
browserbackend
.
browser
.
service
.
Impl
;
import
com.edgec.browserbackend.browser.domain.UserCode
;
import
com.edgec.browserbackend.browser.repository.UserCodeRepository
;
import
com.edgec.browserbackend.browser.service.TempService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
/**
*
*/
@Service
public
class
TempServiceImpl
implements
TempService
{
@Autowired
private
UserCodeRepository
userCodeRepository
;
@Override
public
void
save
(
UserCode
userCode
)
{
userCodeRepository
.
save
(
userCode
);
}
}
src/main/java/com/edgec/browserbackend/browser/service/ShopService.java
View file @
1ff453f7
package
com
.
edgec
.
browserbackend
.
browser
.
service
;
package
com
.
edgec
.
browserbackend
.
browser
.
service
;
import
com.edgec.browserbackend.browser.domain.Shop
;
import
com.edgec.browserbackend.browser.domain.ShopSummary
;
import
com.edgec.browserbackend.browser.domain.ShopSummary
;
import
com.edgec.browserbackend.browser.dto.*
;
import
com.edgec.browserbackend.browser.dto.ShopFilterDto
;
import
com.edgec.browserbackend.browser.dto.ShopPageResultDto
;
import
com.edgec.browserbackend.browser.dto.ShopResultDto
;
import
org.springframework.web.multipart.MultipartFile
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.io.IOException
;
import
java.io.IOException
;
...
@@ -31,4 +32,6 @@ public interface ShopService {
...
@@ -31,4 +32,6 @@ public interface ShopService {
List
<
String
>
getShopUsers
(
String
username
,
String
shopId
);
List
<
String
>
getShopUsers
(
String
username
,
String
shopId
);
List
<
String
>
getBatchShopUsers
(
String
username
,
List
<
String
>
shopIds
);
List
<
String
>
getBatchShopUsers
(
String
username
,
List
<
String
>
shopIds
);
Integer
dealDirtyData
();
}
}
src/main/java/com/edgec/browserbackend/browser/service/TempService.java
0 → 100644
View file @
1ff453f7
package
com
.
edgec
.
browserbackend
.
browser
.
service
;
import
com.edgec.browserbackend.browser.domain.UserCode
;
/**
*
*/
public
interface
TempService
{
void
save
(
UserCode
userCode
);
}
src/main/java/com/edgec/browserbackend/browser/task/BrowserTask.java
View file @
1ff453f7
...
@@ -101,12 +101,15 @@ public class BrowserTask {
...
@@ -101,12 +101,15 @@ public class BrowserTask {
}
}
/**
* 购买ip的定时任务,每分钟一次
*/
@Scheduled
(
cron
=
"0 0/1 * * * ?"
)
@Scheduled
(
cron
=
"0 0/1 * * * ?"
)
public
void
buyIpTasks
()
{
public
void
buyIpTasks
()
{
String
URL
=
(
profiles
.
equals
(
"dev"
)
||
profiles
.
equals
(
"staging"
))
?
TESTURL
:
CLOUDAMURL
;
String
URL
=
(
profiles
.
equals
(
"dev"
)
||
profiles
.
equals
(
"staging"
))
?
TESTURL
:
CLOUDAMURL
;
long
time
=
Instant
.
now
().
minusSeconds
(
300
).
toEpochMilli
();
long
time
=
Instant
.
now
().
minusSeconds
(
300
).
toEpochMilli
();
List
<
IpResource
>
ipResources
=
ipResourceRepository
.
sampleTasks
(
6
,
time
);
List
<
IpResource
>
ipResources
=
ipResourceRepository
.
sampleTasks
(
6
,
time
);
log
.
error
(
"buyIpTasks sample {} tasks"
,
ipResources
.
size
());
log
.
info
(
"buyIpTasks sample {} tasks"
,
ipResources
.
size
());
List
<
CompletableFuture
>
futureList
=
new
ArrayList
<>();
List
<
CompletableFuture
>
futureList
=
new
ArrayList
<>();
for
(
IpResource
ipResource
:
ipResources
)
{
for
(
IpResource
ipResource
:
ipResources
)
{
long
start
=
System
.
currentTimeMillis
();
long
start
=
System
.
currentTimeMillis
();
...
@@ -139,22 +142,21 @@ public class BrowserTask {
...
@@ -139,22 +142,21 @@ public class BrowserTask {
}
}
if
(
ipBuyResultDto
!=
null
&&
ipBuyResultDto
.
getIplist
()
!=
null
&&
ipBuyResultDto
.
getIplist
().
size
()
>=
1
)
{
if
(
ipBuyResultDto
!=
null
&&
ipBuyResultDto
.
getIplist
()
!=
null
&&
ipBuyResultDto
.
getIplist
().
size
()
>=
1
)
{
AtomicInteger
index
=
new
AtomicInteger
();
AtomicInteger
index
=
new
AtomicInteger
();
ipBuyResultDto
.
getIplist
().
forEach
(
x
->
{
ipBuyResultDto
.
getIplist
().
forEach
(
if
(
ipResource
!=
null
)
{
x
->
{
ipResource
.
setAddr
(
x
.
getIp
());
ipResource
.
setAddr
(
x
.
getIp
());
ipResource
.
setStatus
(
3
);
ipResource
.
setStatus
(
3
);
ipResource
.
setValidTime
(
Instant
.
parse
(
x
.
getValidTill
()).
toEpochMilli
());
ipResource
.
setValidTime
(
Instant
.
parse
(
x
.
getValidTill
()).
toEpochMilli
());
if
(
StringUtils
.
isNotBlank
(
ipResource
.
getRegion
())
&&
region
.
contains
(
ipResource
.
getRegion
()))
{
if
(
StringUtils
.
isNotBlank
(
ipResource
.
getRegion
())
&&
region
.
contains
(
ipResource
.
getRegion
()))
{
ipResource
.
setProxyUsername
(
ipResource
.
getAddr
());
ipResource
.
setProxyUsername
(
ipResource
.
getAddr
());
ipResource
.
setProxyPassword
(
genRandom
(
3
,
12
));
ipResource
.
setProxyPassword
(
genRandom
(
3
,
12
));
ipResource
.
setSpecialLine
(
true
);
ipResource
.
setSpecialLine
(
true
);
}
ipResourceRepository
.
save
(
ipResource
);
index
.
getAndIncrement
();
}
}
ipResourceRepository
.
save
(
ipResource
);
);
}
else
{
log
.
error
(
"no ipResource"
);
}
index
.
getAndIncrement
();
});
result
=
true
;
result
=
true
;
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
...
...
src/main/java/com/edgec/browserbackend/browser/task/PaymentTask.java
View file @
1ff453f7
...
@@ -32,25 +32,34 @@ public class PaymentTask {
...
@@ -32,25 +32,34 @@ public class PaymentTask {
long
now
=
System
.
currentTimeMillis
();
long
now
=
System
.
currentTimeMillis
();
Date
date_5min
=
new
Date
(
now
-
5
*
60
*
1000
);
Date
date_5min
=
new
Date
(
now
-
5
*
60
*
1000
);
Date
date_10min
=
new
Date
(
now
-
10
*
60
*
1000
);
Date
date_10min
=
new
Date
(
now
-
10
*
60
*
1000
);
// 查找 5-10 分钟内 支付状态为 false 的订单
List
<
UserPayment
>
unfinishedPayments
=
userPaymentRepository
.
findAllByPaymentDateBetweenAndSucceed
(
List
<
UserPayment
>
unfinishedPayments
=
userPaymentRepository
.
findAllByPaymentDateBetweenAndSucceed
(
ZonedDateTime
.
ofInstant
(
date_10min
.
toInstant
(),
ZoneId
.
systemDefault
())
ZonedDateTime
.
ofInstant
(
date_10min
.
toInstant
(),
ZoneId
.
systemDefault
()),
,
ZonedDateTime
.
ofInstant
(
date_5min
.
toInstant
(),
ZoneId
.
systemDefault
()),
false
);
ZonedDateTime
.
ofInstant
(
date_5min
.
toInstant
(),
ZoneId
.
systemDefault
()),
false
);
if
(
unfinishedPayments
!=
null
&&
unfinishedPayments
.
size
()
>
0
)
{
if
(
unfinishedPayments
!=
null
&&
!
unfinishedPayments
.
isEmpty
())
{
unfinishedPayments
.
forEach
(
payment
->
{
PaymentMethod
paymentMethod
=
payment
.
getPaymentMethod
();
try
{
/*
* 自动调用 微信或者支付宝的支付接口来确认 支付状态
* chargeType 取值范围为 0-4
* 此处设置为 100 仅仅只是为了避免 wxCheckOrderStatus() 方法中 chargeType == 0 相关代码块
*/
if
(
PaymentMethod
.
WECHAT
.
equals
(
paymentMethod
))
{
paymentService
.
wxCheckOrderStatus
(
payment
.
getTradeNo
(),
100
);
}
unfinishedPayments
.
stream
().
forEach
(
payment
->
{
if
(
PaymentMethod
.
ALIPAY
.
equals
(
paymentMethod
))
{
PaymentMethod
paymentMethod
=
payment
.
getPaymentMethod
();
paymentService
.
aliCheckOrderStatus
(
payment
.
getTradeNo
(),
100
);
}
try
{
}
catch
(
Exception
e
)
{
if
(
PaymentMethod
.
WECHAT
.
equals
(
paymentMethod
))
{
log
.
error
(
"checkPayments"
,
e
);
paymentService
.
wxCheckOrderStatus
(
payment
.
getTradeNo
());
}
}
else
if
(
PaymentMethod
.
ALIPAY
.
equals
(
paymentMethod
))
{
paymentService
.
aliCheckOrderStatus
(
payment
.
getTradeNo
());
}
}
}
catch
(
Exception
e
)
{
);
log
.
error
(
"checkPayments"
,
e
);
}
});
}
}
}
}
}
}
\ No newline at end of file
src/main/java/com/edgec/browserbackend/browser/task/PromotionTask.java
View file @
1ff453f7
...
@@ -28,36 +28,46 @@ public class PromotionTask {
...
@@ -28,36 +28,46 @@ public class PromotionTask {
private
UserPrePaidBillingRepository
userPrePaidBillingRepository
;
private
UserPrePaidBillingRepository
userPrePaidBillingRepository
;
// TODO 2020年7月2号, 晚上11点,jim负责跟踪下
// TODO 2020年7月2号, 晚上11点,jim负责跟踪下
@SchedulerLock
(
name
=
"countGift"
,
lockAtLeastForString
=
"PT1H"
,
lockAtMostForString
=
"PT2H"
)
@SchedulerLock
(
name
=
"countGift"
,
lockAtLeastForString
=
"PT1H"
,
lockAtMostForString
=
"PT2H"
)
@Scheduled
(
cron
=
"0 0 1
5 2
* ?"
)
@Scheduled
(
cron
=
"0 0 1
1
* ?"
)
public
void
countGift
()
{
public
void
countGift
()
{
List
<
Account
>
accounts
=
accountRepository
.
findByParentIsNull
();
List
<
Account
>
accounts
=
accountRepository
.
findByParentIsNull
();
for
(
Account
account
:
accounts
)
{
for
(
Account
account
:
accounts
)
{
// 1. 获取当前账户的邀请码信息
String
code
=
account
.
getPromotion
().
getCode
();
String
code
=
account
.
getPromotion
().
getCode
();
// 2. 获取被当前账户邀请的所有账户
List
<
Account
>
promotes
=
accountRepository
.
findByPromotionCodeAndParentIsNull
(
code
);
List
<
Account
>
promotes
=
accountRepository
.
findByPromotionCodeAndParentIsNull
(
code
);
double
totalCommission
=
0
;
double
totalCommission
=
0
;
double
secondCommission
=
0
;
double
secondCommission
=
0
;
// 3. 计算所有被当前账户邀请的账户上个月的消费信息,以及如果当前账户是销售账户则还需要计算销售账户的下下级账户的上个月消费信息
for
(
Account
promote
:
promotes
)
{
for
(
Account
promote
:
promotes
)
{
List
<
UserPrePaidBilling
>
userPrePaidBillings
=
userPrePaidBillingRepository
.
findByAdministratorAndYearAndMonthAndPayMethodIn
(
promote
.
getName
(),
// 3.1 获取被邀请的用户上个月的消费信息,todo 感觉这个地方最好加上支付状态
YearMonth
.
now
().
minusMonths
(
1
).
getYear
(),
List
<
UserPrePaidBilling
>
userPrePaidBillings
=
userPrePaidBillingRepository
.
findByAdministratorAndYearAndMonthAndPayMethodIn
(
YearMonth
.
now
().
minusMonths
(
1
).
getMonthValue
(),
Arrays
.
asList
(
1
,
2
,
3
));
promote
.
getName
(),
YearMonth
.
now
().
minusMonths
(
1
).
getYear
(),
YearMonth
.
now
().
minusMonths
(
1
).
getMonthValue
(),
Arrays
.
asList
(
1
,
2
,
3
)
);
// 3.2. 计算被邀请的用户上个月的总消费金额
totalCommission
+=
userPrePaidBillings
.
stream
().
mapToDouble
(
UserPrePaidBilling:
:
getTotal
).
sum
();
totalCommission
+=
userPrePaidBillings
.
stream
().
mapToDouble
(
UserPrePaidBilling:
:
getTotal
).
sum
();
// 3.3. 如果当前账户是本公司销售,则计算下下级用户(例如:销售人员A 邀请了 用户B,用户B 邀请了 其他用户,其他用户就是A的下下级用户)上个月总消费金额
if
(
account
.
getPromotion
().
isSale
()
&&
promote
.
getParent
()
==
null
)
{
if
(
account
.
getPromotion
().
isSale
()
&&
promote
.
getParent
()
==
null
)
{
// 获取下下级账户
List
<
Account
>
secondPromotes
=
accountRepository
.
findByPromotionCodeAndParentIsNull
(
promote
.
getPromotion
().
getCode
());
List
<
Account
>
secondPromotes
=
accountRepository
.
findByPromotionCodeAndParentIsNull
(
promote
.
getPromotion
().
getCode
());
if
(
secondPromotes
!=
null
&&
secondPromotes
.
size
()
>
0
)
{
if
(
secondPromotes
!=
null
&&
!
secondPromotes
.
isEmpty
()
)
{
for
(
Account
secondPromote
:
secondPromotes
)
{
for
(
Account
secondPromote
:
secondPromotes
)
{
List
<
UserPrePaidBilling
>
userPrePaidBillings1
=
userPrePaidBillingRepository
.
findByAdministratorAndYearAndMonthAndPayMethodIn
(
secondPromote
.
getName
(),
List
<
UserPrePaidBilling
>
userPrePaidBillings1
=
userPrePaidBillingRepository
.
findByAdministratorAndYearAndMonthAndPayMethodIn
(
YearMonth
.
now
().
minusMonths
(
1
).
getYear
(),
secondPromote
.
getName
(),
YearMonth
.
now
().
minusMonths
(
1
).
getYear
(),
YearMonth
.
now
().
minusMonths
(
1
).
getMonthValue
(),
Arrays
.
asList
(
1
,
2
,
3
)
YearMonth
.
now
().
minusMonths
(
1
).
getMonthValue
(),
Arrays
.
asList
(
1
,
2
,
3
)
);
);
secondCommission
+=
userPrePaidBillings1
.
stream
().
mapToDouble
(
UserPrePaidBilling:
:
getTotal
).
sum
();
secondCommission
+=
userPrePaidBillings1
.
stream
().
mapToDouble
(
UserPrePaidBilling:
:
getTotal
).
sum
();
}
}
}
}
}
}
}
}
// 4. 封装当前账户的返佣相关信息(总消费、上月消费、当月礼金、总共未体现礼金)并保存
Promotion
promotion
=
account
.
getPromotion
();
Promotion
promotion
=
account
.
getPromotion
();
promotion
.
setTotalCommission
((
int
)(
promotion
.
getTotalCommission
()
+
totalCommission
));
promotion
.
setTotalCommission
((
int
)
(
promotion
.
getTotalCommission
()
+
totalCommission
));
promotion
.
setCommissionLastMonth
((
int
)
totalCommission
);
promotion
.
setCommissionLastMonth
((
int
)
totalCommission
);
promotion
.
setGift
(
totalCommission
*
0.08
);
promotion
.
setGift
(
totalCommission
*
0.08
);
if
(
account
.
getPromotion
().
isSale
())
{
if
(
account
.
getPromotion
().
isSale
())
{
promotion
.
setGift
(
totalCommission
*
0.1
+
secondCommission
*
0.02
);
promotion
.
setGift
(
totalCommission
*
0.1
+
secondCommission
*
0.02
);
...
@@ -65,41 +75,50 @@ public class PromotionTask {
...
@@ -65,41 +75,50 @@ public class PromotionTask {
promotion
.
setAllGift
(
promotion
.
getAllGift
()
+
promotion
.
getGift
());
promotion
.
setAllGift
(
promotion
.
getAllGift
()
+
promotion
.
getGift
());
accountRepository
.
save
(
account
);
accountRepository
.
save
(
account
);
}
}
log
.
info
(
"End scheduled task:Scheduled.countGift..."
);
}
}
@SchedulerLock
(
name
=
"countCommission"
,
lockAtLeastForString
=
"PT1H"
,
lockAtMostForString
=
"PT2H"
)
@SchedulerLock
(
name
=
"countCommission"
,
lockAtLeastForString
=
"PT1H"
,
lockAtMostForString
=
"PT2H"
)
@Scheduled
(
cron
=
"0 0 3 * * ?"
)
@Scheduled
(
cron
=
"0 0 3 * * ?"
)
public
void
countCommission
()
{
public
void
countCommission
()
{
log
.
info
(
"Start scheduled task:Scheduled.countCommission..."
);
List
<
Account
>
accounts
=
accountRepository
.
findByParentIsNull
();
List
<
Account
>
accounts
=
accountRepository
.
findByParentIsNull
();
for
(
Account
account
:
accounts
)
{
for
(
Account
account
:
accounts
)
{
// 1. 获取被当前账户邀请的所有账户
String
code
=
account
.
getPromotion
().
getCode
();
String
code
=
account
.
getPromotion
().
getCode
();
List
<
Account
>
promotes
=
accountRepository
.
findByPromotionCodeAndParentIsNull
(
code
);
List
<
Account
>
promotes
=
accountRepository
.
findByPromotionCodeAndParentIsNull
(
code
);
double
totalCommission
=
0
;
double
totalCommission
=
0
;
double
secondCommission
=
0
;
double
secondCommission
=
0
;
// 2. 计算所有被当前账户邀请的账户当月当前的消费信息,以及如果当前账户是销售账户则还需要计算销售账户的下下级账户的当月当前消费信息
for
(
Account
promote
:
promotes
)
{
for
(
Account
promote
:
promotes
)
{
List
<
UserPrePaidBilling
>
userPrePaidBillings
=
userPrePaidBillingRepository
.
findByAdministratorAndYearAndMonthAndPayMethodIn
(
promote
.
getName
(),
List
<
UserPrePaidBilling
>
userPrePaidBillings
=
userPrePaidBillingRepository
.
findByAdministratorAndYearAndMonthAndPayMethodIn
(
YearMonth
.
now
().
getYear
(),
promote
.
getName
(),
YearMonth
.
now
().
getYear
(),
YearMonth
.
now
().
getMonthValue
(),
Arrays
.
asList
(
1
,
2
,
3
)
YearMonth
.
now
().
getMonthValue
(),
Arrays
.
asList
(
1
,
2
,
3
)
);
);
totalCommission
+=
userPrePaidBillings
.
stream
().
mapToDouble
(
UserPrePaidBilling:
:
getTotal
).
sum
();
totalCommission
+=
userPrePaidBillings
.
stream
().
mapToDouble
(
UserPrePaidBilling:
:
getTotal
).
sum
();
if
(
account
.
getPromotion
().
isSale
()
&&
promote
.
getParent
()
==
null
)
{
if
(
account
.
getPromotion
().
isSale
()
&&
promote
.
getParent
()
==
null
)
{
List
<
Account
>
secondPromotes
=
accountRepository
.
findByPromotionCodeAndParentIsNull
(
promote
.
getPromotion
().
getCode
());
List
<
Account
>
secondPromotes
=
accountRepository
.
findByPromotionCodeAndParentIsNull
(
promote
.
getPromotion
().
getCode
());
if
(
secondPromotes
!=
null
&&
secondPromotes
.
size
()
>
0
)
{
if
(
secondPromotes
!=
null
&&
secondPromotes
.
size
()
>
0
)
{
for
(
Account
secondPromote
:
secondPromotes
)
{
for
(
Account
secondPromote
:
secondPromotes
)
{
List
<
UserPrePaidBilling
>
userPrePaidBillings1
=
userPrePaidBillingRepository
.
findByAdministratorAndYearAndMonthAndPayMethodIn
(
secondPromote
.
getName
(),
List
<
UserPrePaidBilling
>
userPrePaidBillings1
=
userPrePaidBillingRepository
.
findByAdministratorAndYearAndMonthAndPayMethodIn
(
YearMonth
.
now
().
getYear
(),
secondPromote
.
getName
(),
YearMonth
.
now
().
getYear
(),
YearMonth
.
now
().
getMonthValue
(),
Arrays
.
asList
(
1
,
2
,
3
)
YearMonth
.
now
().
getMonthValue
(),
Arrays
.
asList
(
1
,
2
,
3
)
);
);
secondCommission
+=
userPrePaidBillings1
.
stream
().
mapToDouble
(
UserPrePaidBilling:
:
getTotal
).
sum
();
secondCommission
+=
userPrePaidBillings1
.
stream
().
mapToDouble
(
UserPrePaidBilling:
:
getTotal
).
sum
();
}
}
}
}
}
}
}
}
// 3. 封装当前账户的返佣相关信息(当月当前的消费、当月当前的返佣)并保存
Promotion
promotion
=
account
.
getPromotion
();
Promotion
promotion
=
account
.
getPromotion
();
promotion
.
setCommission
((
int
)
totalCommission
);
promotion
.
setCommission
((
int
)
totalCommission
);
promotion
.
setGift
(
totalCommission
*
0.08
);
promotion
.
setGift
(
totalCommission
*
0.08
);
if
(
account
.
getPromotion
().
isSale
())
{
if
(
account
.
getPromotion
().
isSale
())
{
promotion
.
setGift
(
totalCommission
*
0.1
+
secondCommission
*
0.02
);
promotion
.
setGift
(
totalCommission
*
0.1
+
secondCommission
*
0.02
);
}
}
accountRepository
.
save
(
account
);
accountRepository
.
save
(
account
);
}
}
log
.
info
(
"End scheduled task:Scheduled.countCommission..."
);
}
}
}
}
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