Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
jiedao-api-boot-master
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
lanbaoming
jiedao-api-boot-master
Commits
705f33ad
Commit
705f33ad
authored
Feb 23, 2025
by
wanghuazhou
Committed by
wux
Feb 27, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: 货币汇率对外接口替换
parent
5e3bd425
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
147 deletions
+37
-147
CurrecyApiImpl.java
.../iocoder/yudao/module/ecw/service/api/CurrecyApiImpl.java
+37
-147
No files found.
yudao-module-ecw/yudao-module-ecw-impl/src/main/java/cn/iocoder/yudao/module/ecw/service/api/CurrecyApiImpl.java
View file @
705f33ad
package
cn
.
iocoder
.
yudao
.
module
.
ecw
.
service
.
api
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.core.util.StrUtil
;
import
cn.iocoder.yudao.module.ecw.api.currency.CurrencyApi
;
import
cn.iocoder.yudao.module.ecw.api.currency.dto.CurrencyRespDTO
;
import
cn.iocoder.yudao.module.ecw.api.currency.dto.ExchangeRateRespDTO
;
import
cn.iocoder.yudao.module.ecw.convert.currency.CurrencyConvert
;
import
cn.iocoder.yudao.module.ecw.convert.exchangeRate.ExchangeRateConvert
;
import
cn.iocoder.yudao.module.ecw.dal.dataobject.currency.CurrencyDO
;
import
cn.iocoder.yudao.module.ecw.dal.dataobject.exchangeRate.ExchangeRateDO
;
import
cn.iocoder.yudao.module.ecw.service.currency.CurrencyService
;
import
cn.iocoder.yudao.module.ecw.service.
exchangeRate.Exchange
RateService
;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
import
cn.iocoder.yudao.module.ecw.service.
currencyRate.Currency
RateService
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.Resource
;
import
java.math.BigDecimal
;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Optional
;
import
java.util.stream.Collectors
;
import
static
cn
.
iocoder
.
yudao
.
framework
.
common
.
exception
.
util
.
ServiceExceptionUtil
.
exception
;
import
static
cn
.
iocoder
.
yudao
.
module
.
ecw
.
enums
.
ErrorCodeConstants
.
EXCHANGE_RATE_EXPIRE
;
import
static
cn
.
iocoder
.
yudao
.
module
.
ecw
.
enums
.
ErrorCodeConstants
.
EXCHANGE_RATE_NOT_EXISTS_PARAM
;
@Service
public
class
CurrecyApiImpl
implements
CurrencyApi
{
@Resource
CurrencyService
currencyService
;
@
Resource
ExchangeRateService
exchange
RateService
;
@
Autowired
private
CurrencyRateService
currency
RateService
;
@Override
public
Map
<
Integer
,
CurrencyRespDTO
>
getAllCurrency
()
{
...
...
@@ -42,158 +35,55 @@ public class CurrecyApiImpl implements CurrencyApi {
@Override
public
ExchangeRateRespDTO
getCurrencyRate
(
Long
sourceId
,
Long
targetId
)
{
if
(
sourceId
==
targetId
)
{
ExchangeRateRespDTO
dto
=
new
ExchangeRateRespDTO
();
dto
.
setCurrencyRate
(
new
BigDecimal
(
"1"
));
dto
.
setSourceCurrencyId
(
sourceId
);
dto
.
setTargetCurrencyId
(
targetId
);
return
dto
;
}
LambdaQueryWrapper
<
ExchangeRateDO
>
lambdaQueryWrapper
=
new
LambdaQueryWrapper
<>();
lambdaQueryWrapper
.
eq
(
ExchangeRateDO:
:
getSourceCurrencyId
,
sourceId
);
lambdaQueryWrapper
.
eq
(
ExchangeRateDO:
:
getTargetCurrencyId
,
targetId
);
ExchangeRateDO
rateDO
=
exchangeRateService
.
getOne
(
lambdaQueryWrapper
);
CurrencyDO
currencyDO1
=
currencyService
.
getCurrency
(
sourceId
.
intValue
());
CurrencyDO
currencyDO2
=
currencyService
.
getCurrency
(
targetId
.
intValue
());
if
(
null
!=
rateDO
)
{
//判断汇率有没有过期
if
(
null
!=
rateDO
.
getExpireDate
())
{
if
(
DateUtil
.
compare
(
rateDO
.
getExpireDate
(),
DateUtil
.
date
())
==
-
1
)
{
if
(
null
==
currencyDO1
||
null
==
currencyDO2
)
{
throw
exception
(
EXCHANGE_RATE_EXPIRE
,
sourceId
.
toString
(),
targetId
.
toString
());
}
else
{
throw
exception
(
EXCHANGE_RATE_EXPIRE
,
currencyDO1
.
getTitleZh
()
+
currencyDO1
.
getFuhao
(),
currencyDO2
.
getTitleZh
()
+
currencyDO2
.
getFuhao
());
}
}
}
else
{
if
(
null
==
currencyDO1
||
null
==
currencyDO2
)
{
throw
exception
(
EXCHANGE_RATE_EXPIRE
,
sourceId
.
toString
(),
targetId
.
toString
());
}
else
{
throw
exception
(
EXCHANGE_RATE_EXPIRE
,
currencyDO1
.
getTitleZh
()
+
currencyDO1
.
getFuhao
(),
currencyDO2
.
getTitleZh
()
+
currencyDO2
.
getFuhao
());
}
}
return
ExchangeRateConvert
.
INSTANCE
.
convert2
(
exchangeRateService
.
getOne
(
lambdaQueryWrapper
));
ExchangeRateRespDTO
dto
=
new
ExchangeRateRespDTO
();
dto
.
setSourceCurrencyId
(
sourceId
);
dto
.
setTargetCurrencyId
(
targetId
);
if
(
sourceId
==
null
||
targetId
==
null
)
{
dto
.
setCurrencyRate
(
BigDecimal
.
ZERO
);
}
else
{
if
(
null
==
currencyDO1
||
null
==
currencyDO2
)
{
ExchangeRateRespDTO
dto
=
new
ExchangeRateRespDTO
();
dto
.
setCurrencyRate
(
BigDecimal
.
ZERO
);
dto
.
setSourceCurrencyId
(
sourceId
);
dto
.
setTargetCurrencyId
(
targetId
);
return
dto
;
}
else
{
throw
exception
(
EXCHANGE_RATE_NOT_EXISTS_PARAM
,
currencyDO1
.
getTitleZh
()
+
currencyDO1
.
getFuhao
(),
currencyDO2
.
getTitleZh
()
+
currencyDO2
.
getFuhao
());
}
dto
.
setCurrencyRate
(
sourceId
.
equals
(
targetId
)
?
BigDecimal
.
ONE
:
currencyRateService
.
rate
(
sourceId
,
targetId
));
}
return
dto
;
}
@Override
public
List
<
ExchangeRateRespDTO
>
getCurrencyRateList
(
Collection
<
Long
>
sourceIds
,
Long
targetId
)
{
List
<
ExchangeRateRespDTO
>
exchangeRateRespDTOList
=
new
ArrayList
<>();
for
(
Long
sourceId
:
sourceIds
)
{
if
(
sourceId
==
targetId
)
{
ExchangeRateRespDTO
dto
=
new
ExchangeRateRespDTO
();
dto
.
setCurrencyRate
(
new
BigDecimal
(
"1"
));
dto
.
setSourceCurrencyId
(
sourceId
);
dto
.
setTargetCurrencyId
(
targetId
);
exchangeRateRespDTOList
.
add
(
dto
);
continue
;
}
LambdaQueryWrapper
<
ExchangeRateDO
>
lambdaQueryWrapper
=
new
LambdaQueryWrapper
<>();
lambdaQueryWrapper
.
eq
(
ExchangeRateDO:
:
getSourceCurrencyId
,
sourceId
);
lambdaQueryWrapper
.
eq
(
ExchangeRateDO:
:
getTargetCurrencyId
,
targetId
);
ExchangeRateDO
rateDO
=
exchangeRateService
.
getOne
(
lambdaQueryWrapper
);
CurrencyDO
currencyDO1
=
currencyService
.
getCurrency
(
sourceId
.
intValue
());
CurrencyDO
currencyDO2
=
currencyService
.
getCurrency
(
targetId
.
intValue
());
if
(
null
!=
rateDO
)
{
//判断汇率有没有过期
if
(
null
!=
rateDO
.
getExpireDate
())
{
if
(
DateUtil
.
compare
(
rateDO
.
getExpireDate
(),
DateUtil
.
date
())
==
-
1
)
{
if
(
null
==
currencyDO1
||
null
==
currencyDO2
)
{
throw
exception
(
EXCHANGE_RATE_EXPIRE
,
sourceId
.
toString
(),
targetId
.
toString
());
}
else
{
throw
exception
(
EXCHANGE_RATE_EXPIRE
,
currencyDO1
.
getTitleZh
()
+
currencyDO1
.
getFuhao
(),
currencyDO2
.
getTitleZh
()
+
currencyDO2
.
getFuhao
());
}
}
}
else
{
if
(
null
==
currencyDO1
||
null
==
currencyDO2
)
{
throw
exception
(
EXCHANGE_RATE_EXPIRE
,
sourceId
.
toString
(),
targetId
.
toString
());
}
else
{
throw
exception
(
EXCHANGE_RATE_EXPIRE
,
currencyDO1
.
getTitleZh
()
+
currencyDO1
.
getFuhao
(),
currencyDO2
.
getTitleZh
()
+
currencyDO2
.
getFuhao
());
}
}
exchangeRateRespDTOList
.
add
(
ExchangeRateConvert
.
INSTANCE
.
convert2
(
rateDO
));
}
else
{
if
(
null
==
currencyDO1
||
null
==
currencyDO2
)
{
ExchangeRateRespDTO
dto
=
new
ExchangeRateRespDTO
();
dto
.
setCurrencyRate
(
BigDecimal
.
ZERO
);
dto
.
setSourceCurrencyId
(
sourceId
);
dto
.
setTargetCurrencyId
(
targetId
);
exchangeRateRespDTOList
.
add
(
dto
);
}
else
{
throw
exception
(
EXCHANGE_RATE_NOT_EXISTS_PARAM
,
currencyDO1
.
getTitleZh
()
+
currencyDO1
.
getFuhao
(),
currencyDO2
.
getTitleZh
()
+
currencyDO2
.
getFuhao
());
}
}
}
return
exchangeRateRespDTOList
;
return
sourceIds
.
stream
()
.
map
(
sourceId
->
getCurrencyRate
(
sourceId
,
targetId
))
.
collect
(
Collectors
.
toList
());
}
@Override
public
ExchangeRateRespDTO
getCurrencyRateByCode
(
String
sourceCode
,
String
targetCode
)
{
ExchangeRateRespDTO
dto
=
new
ExchangeRateRespDTO
();
dto
.
setSourceCurrencyCode
(
sourceCode
);
dto
.
setTargetCurrencyCode
(
targetCode
);
if
(
StrUtil
.
equals
(
sourceCode
,
targetCode
))
{
ExchangeRateRespDTO
dto
=
new
ExchangeRateRespDTO
();
dto
.
setCurrencyRate
(
new
BigDecimal
(
"1"
));
dto
.
setSourceCurrencyCode
(
sourceCode
);
dto
.
setTargetCurrencyCode
(
targetCode
);
dto
.
setCurrencyRate
(
BigDecimal
.
ONE
);
return
dto
;
}
LambdaQueryWrapper
<
ExchangeRateDO
>
lambdaQueryWrapper
=
new
LambdaQueryWrapper
<>();
lambdaQueryWrapper
.
eq
(
ExchangeRateDO:
:
getSourceCurrencyCode
,
sourceCode
);
lambdaQueryWrapper
.
eq
(
ExchangeRateDO:
:
getTargetCurrencyCode
,
targetCode
);
ExchangeRateDO
rateDO
=
exchangeRateService
.
getOne
(
lambdaQueryWrapper
);
CurrencyDO
currencyDO1
=
currencyService
.
getCurrencyByCode
(
sourceCode
);
CurrencyDO
currencyDO2
=
currencyService
.
getCurrencyByCode
(
targetCode
);
if
(
null
!=
rateDO
)
{
//判断汇率有没有过期
if
(
null
!=
rateDO
.
getExpireDate
())
{
if
(
DateUtil
.
compare
(
rateDO
.
getExpireDate
(),
DateUtil
.
date
())
==
-
1
)
{
if
(
null
==
currencyDO1
||
null
==
currencyDO2
)
{
throw
exception
(
EXCHANGE_RATE_EXPIRE
,
sourceCode
,
targetCode
);
}
else
{
throw
exception
(
EXCHANGE_RATE_EXPIRE
,
currencyDO1
.
getTitleZh
()
+
currencyDO1
.
getFuhao
(),
currencyDO2
.
getTitleZh
()
+
currencyDO2
.
getFuhao
());
}
}
}
else
{
if
(
null
==
currencyDO1
||
null
==
currencyDO2
)
{
throw
exception
(
EXCHANGE_RATE_EXPIRE
,
sourceCode
,
targetCode
);
}
else
{
throw
exception
(
EXCHANGE_RATE_EXPIRE
,
currencyDO1
.
getTitleZh
()
+
currencyDO1
.
getFuhao
(),
currencyDO2
.
getTitleZh
()
+
currencyDO2
.
getFuhao
());
}
}
return
ExchangeRateConvert
.
INSTANCE
.
convert2
(
exchangeRateService
.
getOne
(
lambdaQueryWrapper
));
Integer
sourceId
=
Optional
.
ofNullable
(
currencyService
.
getCurrencyByCode
(
sourceCode
))
.
map
(
CurrencyDO:
:
getId
)
.
orElse
(
null
);
Integer
targetId
=
Optional
.
ofNullable
(
currencyService
.
getCurrencyByCode
(
targetCode
))
.
map
(
CurrencyDO:
:
getId
)
.
orElse
(
null
);
if
(
sourceId
==
null
||
targetId
==
null
)
{
dto
.
setCurrencyRate
(
BigDecimal
.
ZERO
);
}
else
{
if
(
null
==
currencyDO1
||
null
==
currencyDO2
)
{
ExchangeRateRespDTO
dto
=
new
ExchangeRateRespDTO
();
dto
.
setCurrencyRate
(
BigDecimal
.
ZERO
);
dto
.
setSourceCurrencyCode
(
sourceCode
);
dto
.
setTargetCurrencyCode
(
targetCode
);
return
dto
;
}
else
{
throw
exception
(
EXCHANGE_RATE_NOT_EXISTS_PARAM
,
currencyDO1
.
getTitleZh
()
+
currencyDO1
.
getFuhao
(),
currencyDO2
.
getTitleZh
()
+
currencyDO2
.
getFuhao
());
}
dto
.
setSourceCurrencyId
(
sourceId
.
longValue
());
dto
.
setTargetCurrencyId
(
targetId
.
longValue
());
dto
.
setCurrencyRate
(
sourceId
.
equals
(
targetId
)
?
BigDecimal
.
ONE
:
currencyRateService
.
rate
(
sourceId
,
targetId
));
}
return
dto
;
}
@Override
...
...
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