记得上下班打卡 | git大法好,push需谨慎
Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
liquidnet-bus-v1
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
董敬伟
liquidnet-bus-v1
Commits
379e8fe9
Commit
379e8fe9
authored
May 23, 2022
by
胡佳晨
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
暂时提交
parent
f1c75a9d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
199 additions
and
11 deletions
+199
-11
GoblinListServiceImpl.java
...ice/goblin/service/impl/manage/GoblinListServiceImpl.java
+34
-11
GoblinNftBoxUtils.java
.../com/liquidnet/service/goblin/util/GoblinNftBoxUtils.java
+165
-0
No files found.
liquidnet-bus-service/liquidnet-service-goblin/liquidnet-service-goblin-impl/src/main/java/com/liquidnet/service/goblin/service/impl/manage/GoblinListServiceImpl.java
View file @
379e8fe9
...
...
@@ -14,19 +14,18 @@ import com.liquidnet.service.goblin.param.GoblinListCreateItemParam;
import
com.liquidnet.service.goblin.param.GoblinListCreateParam
;
import
com.liquidnet.service.goblin.param.GoblinListUpdateParam
;
import
com.liquidnet.service.goblin.service.manage.IGoblinListService
;
import
com.liquidnet.service.goblin.util.GoblinMongoUtils
;
import
com.liquidnet.service.goblin.util.GoblinRedisUtils
;
import
com.liquidnet.service.goblin.util.ObjectUtil
;
import
com.liquidnet.service.goblin.util.QueueUtils
;
import
com.liquidnet.service.goblin.util.*
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.math.BigDecimal
;
import
java.text.DateFormat
;
import
java.time.LocalDateTime
;
import
java.time.format.DateTimeFormatter
;
import
java.util.HashMap
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.Map
;
import
static
com
.
liquidnet
.
commons
.
lang
.
util
.
DateUtil
.
DTF_YMD_HMS
;
...
...
@@ -47,6 +46,8 @@ public class GoblinListServiceImpl implements IGoblinListService {
GoblinMongoUtils
mongoUtils
;
@Autowired
QueueUtils
queueUtils
;
@Autowired
GoblinNftBoxUtils
nftBoxUtils
;
@Override
public
ResponseDto
<
PageInfo
<
GoblinListVo
>>
getList
(
String
name
,
Integer
page
)
{
...
...
@@ -199,14 +200,36 @@ public class GoblinListServiceImpl implements IGoblinListService {
itemVo
.
setSkuStock
(
item
.
getSkuStock
());
itemVo
.
setProductId
(
item
.
getProductId
());
voItemList
.
add
(
itemVo
);
//库存
int
skuStock
=
redisUtils
.
decrSkuStock
(
null
,
itemVo
.
getSkuId
(),
itemVo
.
getSkuStock
());
if
(
skuStock
<
0
)
{
redisUtils
.
incrSkuStock
(
null
,
itemVo
.
getSkuId
(),
itemVo
.
getSkuStock
());
return
ResponseDto
.
failure
(
"库存不足"
);
}
else
{
redisUtils
.
setSkuStock
(
vo
.
getListId
(),
itemVo
.
getSkuId
(),
itemVo
.
getSkuStock
());
if
(
skuInfoVo
.
getUnbox
().
equals
(
"1"
))
{
//盲盒逻辑
//计算盲盒对应sku数量
HashMap
<
String
,
BigDecimal
>
map
=
CollectionUtil
.
mapStringBigDecimal
();
GoblinGoodsInfoVo
spuInfoVo
=
redisUtils
.
getGoodsInfoVo
(
item
.
getSpuId
());
for
(
String
itemSkuId
:
spuInfoVo
.
getSkuIdList
())
{
GoblinGoodsSkuInfoVo
skuItemVo
=
redisUtils
.
getGoodsSkuInfoVo
(
itemSkuId
);
if
(
skuItemVo
.
getUnbox
().
equals
(
"0"
))
{
map
.
put
(
skuItemVo
.
getSkuId
(),
skuItemVo
.
getHitRatio
());
}
}
Map
<
String
,
Integer
>
skuMap
=
nftBoxUtils
.
getSkuHitRatio
(
item
.
getSkuStock
(),
map
);
//库存
int
skuStock
=
redisUtils
.
decrSkuStock
(
null
,
itemVo
.
getSkuId
(),
itemVo
.
getSkuStock
());
if
(
skuStock
<
0
)
{
redisUtils
.
incrSkuStock
(
null
,
itemVo
.
getSkuId
(),
itemVo
.
getSkuStock
());
return
ResponseDto
.
failure
(
"库存不足"
);
}
else
{
redisUtils
.
setSkuStock
(
vo
.
getListId
(),
itemVo
.
getSkuId
(),
itemVo
.
getSkuStock
());
}
}
else
{
//非盲盒逻辑
//库存
int
skuStock
=
redisUtils
.
decrSkuStock
(
null
,
itemVo
.
getSkuId
(),
itemVo
.
getSkuStock
());
if
(
skuStock
<
0
)
{
redisUtils
.
incrSkuStock
(
null
,
itemVo
.
getSkuId
(),
itemVo
.
getSkuStock
());
return
ResponseDto
.
failure
(
"库存不足"
);
}
else
{
redisUtils
.
setSkuStock
(
vo
.
getListId
(),
itemVo
.
getSkuId
(),
itemVo
.
getSkuStock
());
}
}
//集合
GoblinListCollectVo
collectVo
=
GoblinListCollectVo
.
getNew
();
collectVo
.
setSpuId
(
itemVo
.
getSpuId
());
...
...
liquidnet-bus-service/liquidnet-service-goblin/liquidnet-service-goblin-impl/src/main/java/com/liquidnet/service/goblin/util/GoblinNftBoxUtils.java
0 → 100644
View file @
379e8fe9
package
com
.
liquidnet
.
service
.
goblin
.
util
;
import
com.liquidnet.commons.lang.util.CollectionUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
java.math.BigDecimal
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.Map
;
@Slf4j
@Component
public
class
GoblinNftBoxUtils
{
@Autowired
GoblinRedisUtils
redisUtils
;
/**
* 获取每个sku的兑换码生成数量
* <p>
* sku兑换码数 = 生码总数 * sku概率/总概率
* 若sku兑换码数 > sku剩余库存
* 用 sku兑换码数 - sku剩余库存 * (其他sku概率/其他总概率)
* <p>
* 例:生成兑换码 120 有sku1,sku2,sku3
* sku1 库存5 概率 10%
* sku2 库存100 概率 20%
* sku3 库存100 概率 30%
* <p>
* sku1兑换码数=120 * (10/60) = 20 >5
* sku2兑换码数=120 * (20/60) = 40 <100
* sku3兑换码数=120 * (30/60) = 60 <100
* <p>
* sku2第二轮兑换码数 = (20-5) * (20/50) = 6 < 60
* sku3第二轮兑换码数 = (20-5) * (30/50) = 9 < 40
* <p>
* 总兑换码 = sku1兑换码数+sku2兑换码数+sku3兑换码数+sku2第二轮兑换码数+sku3第二轮兑换码数
*
* @return
*/
public
Map
<
String
,
Integer
>
getSkuHitRatio
(
Integer
exStock
,
Map
<
String
,
BigDecimal
>
map
)
{
// 获取sku总概率
BigDecimal
hitRatioCount
=
new
BigDecimal
(
0
);
// Map<String, Map<String, Object>> mapMap = new HashMap<>();
// sku --> 概率/库存
int
countStockNumber
=
0
;
HashMap
<
String
,
Map
<
String
,
Object
>>
mapMap
=
CollectionUtil
.
mapHashMap
();
for
(
String
key
:
map
.
keySet
())
{
BigDecimal
skuHitRatio
=
BigDecimal
.
valueOf
(
Double
.
parseDouble
(
map
.
get
(
key
).
toString
()));
HashMap
<
String
,
Object
>
objectMap
=
CollectionUtil
.
mapStringObject
();
if
(
skuHitRatio
==
null
)
{
continue
;
}
hitRatioCount
=
hitRatioCount
.
add
(
skuHitRatio
);
objectMap
.
put
(
"hitRatio"
,
skuHitRatio
);
objectMap
.
put
(
"stock"
,
redisUtils
.
getSkuStock
(
null
,
key
));
countStockNumber
+=
redisUtils
.
getSkuStock
(
null
,
key
);
mapMap
.
put
(
key
,
objectMap
);
}
// sku ---> 库存
HashMap
<
String
,
Integer
>
stockMap
=
CollectionUtil
.
mapStringInteger
();
ArrayList
<
String
>
eliminateSkuIdList
=
CollectionUtil
.
arrayListString
();
getStock
(
exStock
,
hitRatioCount
,
mapMap
,
stockMap
,
countStockNumber
,
eliminateSkuIdList
);
return
stockMap
;
}
/**
* 获取单个sku库存
*
* @param exStock
* @param hitRatioCount
* @param mapMap
* @return
*/
private
static
void
getStock
(
Integer
exStock
,
BigDecimal
hitRatioCount
,
Map
<
String
,
Map
<
String
,
Object
>>
mapMap
,
Map
<
String
,
Integer
>
stockMap
,
Integer
countStockNumber
,
ArrayList
<
String
>
eliminateSkuIdList
)
{
if
(
exStock
<=
0
||
countStockNumber
==
0
)
{
return
;
}
// 单个sku ---> 当前循环权重
HashMap
<
String
,
BigDecimal
>
bigDecimalHashMap
=
CollectionUtil
.
mapStringBigDecimal
();
int
subNumber
=
0
;
for
(
String
key
:
mapMap
.
keySet
())
{
Object
stock
=
mapMap
.
get
(
key
).
get
(
"stock"
);
Object
hitRatio
=
mapMap
.
get
(
key
).
get
(
"hitRatio"
);
if
(
Integer
.
parseInt
(
stock
.
toString
())
<=
0
)
{
continue
;
}
BigDecimal
bigDecimal
=
BigDecimal
.
valueOf
(
exStock
).
multiply
((
new
BigDecimal
(
hitRatio
.
toString
()).
divide
(
hitRatioCount
,
2
,
BigDecimal
.
ROUND_HALF_UP
)));
bigDecimalHashMap
.
put
(
key
,
bigDecimal
);
// 要减去的库存
int
subStock
=
BigDecimal
.
valueOf
(
exStock
).
multiply
((
new
BigDecimal
(
hitRatio
.
toString
()).
divide
(
hitRatioCount
,
2
,
BigDecimal
.
ROUND_HALF_UP
))).
setScale
(
0
,
BigDecimal
.
ROUND_UP
).
intValue
();
if
(
subStock
>
Integer
.
parseInt
(
stock
.
toString
()))
{
subNumber
+=
Integer
.
parseInt
(
stock
.
toString
());
countStockNumber
-=
Integer
.
parseInt
(
stock
.
toString
());
mapMap
.
get
(
key
).
put
(
"stock"
,
0
);
if
(
stockMap
.
get
(
key
)
==
null
)
{
stockMap
.
put
(
key
,
Integer
.
valueOf
(
stock
.
toString
()));
}
else
{
stockMap
.
put
(
key
,
stockMap
.
get
(
key
)
+
Integer
.
parseInt
(
stock
.
toString
()));
}
}
else
{
countStockNumber
-=
subStock
;
mapMap
.
get
(
key
).
put
(
"stock"
,
Integer
.
parseInt
(
stock
.
toString
())
-
subStock
);
subNumber
+=
subStock
;
if
(
stockMap
.
get
(
key
)
==
null
)
{
stockMap
.
put
(
key
,
subStock
);
}
else
{
stockMap
.
put
(
key
,
stockMap
.
get
(
key
)
+
subStock
);
}
}
}
if
(
subNumber
>
exStock
)
{
int
usNumber
=
subNumber
-
exStock
;
ArrayList
<
String
>
skuIdList
=
CollectionUtil
.
arrayListString
();
for
(
int
k
=
0
;
k
<
usNumber
;
k
++)
{
String
subKey
=
""
;
double
maxValue
=
Double
.
MAX_VALUE
;
for
(
String
key
:
bigDecimalHashMap
.
keySet
())
{
if
(
skuIdList
.
contains
(
key
))
{
continue
;
}
double
compare
=
Double
.
parseDouble
(
bigDecimalHashMap
.
get
(
key
).
toString
());
if
(
maxValue
>
compare
)
{
maxValue
=
compare
;
subKey
=
key
;
}
}
Integer
stock
=
stockMap
.
get
(
subKey
);
stockMap
.
put
(
subKey
,
stock
-
1
);
skuIdList
.
add
(
subKey
);
}
return
;
}
exStock
-=
subNumber
;
if
(
exStock
>
0
)
{
for
(
String
key
:
mapMap
.
keySet
())
{
Object
hitRatio
=
mapMap
.
get
(
key
).
get
(
"hitRatio"
);
Object
stock
=
mapMap
.
get
(
key
).
get
(
"stock"
);
if
(
Integer
.
parseInt
(
stock
.
toString
())
<=
0
&&
!
eliminateSkuIdList
.
contains
(
key
))
{
eliminateSkuIdList
.
add
(
key
);
hitRatioCount
=
hitRatioCount
.
subtract
(
new
BigDecimal
(
hitRatio
.
toString
()));
}
}
getStock
(
exStock
,
hitRatioCount
,
mapMap
,
stockMap
,
countStockNumber
,
eliminateSkuIdList
);
}
}
}
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