Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
KnowledgePlanet
road-map
xfg-frame-archetype-lite
提交
caf38ce3
xfg-frame-archetype-lite
项目概览
KnowledgePlanet
/
road-map
/
xfg-frame-archetype-lite
通知
1157
Star
58
Fork
71
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
xfg-frame-archetype-lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
caf38ce3
编写于
4月 16, 2024
作者:
H
hudingrong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
rule_model_center
上级
070b7cdd
变更
16
隐藏空白更改
内联
并排
Showing
16 changed file
with
283 addition
and
28 deletion
+283
-28
xfg-frame-archetype-lite-app/src/main/java/cn/bugstack/config/RedisClientConfig.java
...p/src/main/java/cn/bugstack/config/RedisClientConfig.java
+4
-2
xfg-frame-archetype-lite-app/src/main/resources/mybatis/mapper/strategy_award_mapper.xml
...c/main/resources/mybatis/mapper/strategy_award_mapper.xml
+4
-0
xfg-frame-archetype-lite-app/src/test/java/cn/bugstack/test/domain/RaffleStrategyTest.java
...test/java/cn/bugstack/test/domain/RaffleStrategyTest.java
+39
-1
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/model/entity/RaffleFactorEntity.java
...tack/domain/strategy/model/entity/RaffleFactorEntity.java
+2
-0
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/model/entity/StrategyEntity.java
...bugstack/domain/strategy/model/entity/StrategyEntity.java
+6
-1
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/model/valobj/StrategyAwardRuleModelVO.java
...omain/strategy/model/valobj/StrategyAwardRuleModelVO.java
+50
-0
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/repository/IStrategyRepository.java
...stack/domain/strategy/repository/IStrategyRepository.java
+9
-0
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/service/armory/StrategyArmory.java
...gstack/domain/strategy/service/armory/StrategyArmory.java
+2
-2
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/service/raffle/AbstractRaffleStrategy.java
...omain/strategy/service/raffle/AbstractRaffleStrategy.java
+20
-3
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/service/raffle/DefaultRaffleStrategy.java
...domain/strategy/service/raffle/DefaultRaffleStrategy.java
+30
-0
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/service/rule/factory/DefaultLogicFactory.java
...in/strategy/service/rule/factory/DefaultLogicFactory.java
+13
-3
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/service/rule/impl/RuleLockLogicFilter.java
...omain/strategy/service/rule/impl/RuleLockLogicFilter.java
+54
-0
xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/persistent/dao/IStrategyAwardDao.java
...tack/infrastructure/persistent/dao/IStrategyAwardDao.java
+7
-0
xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/persistent/po/Award.java
.../java/cn/bugstack/infrastructure/persistent/po/Award.java
+1
-0
xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/persistent/redis/RedisService.java
...ugstack/infrastructure/persistent/redis/RedisService.java
+1
-0
xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/persistent/repository/StrategyRepository.java
...rastructure/persistent/repository/StrategyRepository.java
+41
-16
未找到文件。
xfg-frame-archetype-lite-app/src/main/java/cn/bugstack/config/RedisClientConfig.java
浏览文件 @
caf38ce3
...
...
@@ -11,6 +11,7 @@ import org.redisson.api.RedissonClient;
import
org.redisson.client.codec.BaseCodec
;
import
org.redisson.client.protocol.Decoder
;
import
org.redisson.client.protocol.Encoder
;
import
org.redisson.codec.JsonJacksonCodec
;
import
org.redisson.config.Config
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.ConfigurableApplicationContext
;
...
...
@@ -32,8 +33,9 @@ public class RedisClientConfig {
public
RedissonClient
redissonClient
(
ConfigurableApplicationContext
applicationContext
,
RedisClientConfigProperties
properties
)
{
Config
config
=
new
Config
();
// 根据需要可以设定编解码器;https://github.com/redisson/redisson/wiki/4.-%E6%95%B0%E6%8D%AE%E5%BA%8F%E5%88%97%E5%8C%96
config
.
setCodec
(
new
RedisCodec
());
// 需要手动从json转pojo
//config.setCodec(new RedisCodec());
config
.
setCodec
(
new
JsonJacksonCodec
());
config
.
useSingleServer
()
.
setAddress
(
"redis://"
+
properties
.
getHost
()
+
":"
+
properties
.
getPort
())
.
setPassword
(
properties
.
getPassword
())
...
...
xfg-frame-archetype-lite-app/src/main/resources/mybatis/mapper/strategy_award_mapper.xml
浏览文件 @
caf38ce3
...
...
@@ -28,5 +28,9 @@
from strategy_award
where strategy_id = #{strategyId}
</select>
<select
id=
"queryStrategyAwardRuleModels"
parameterType=
"cn.bugstack.infrastructure.persistent.po.StrategyAward"
resultType=
"java.lang.String"
>
select rule_models from strategy_award
where strategy_id = #{strategyId} and award_id = #{awardId}
</select>
</mapper>
xfg-frame-archetype-lite-app/src/test/java/cn/bugstack/test/domain/RaffleStrategyTest.java
浏览文件 @
caf38ce3
...
...
@@ -3,6 +3,9 @@ package cn.bugstack.test.domain;
import
cn.bugstack.domain.strategy.model.entity.RaffleAwardEntity
;
import
cn.bugstack.domain.strategy.model.entity.RaffleFactorEntity
;
import
cn.bugstack.domain.strategy.service.IRaffleStrategy
;
import
cn.bugstack.domain.strategy.service.armory.IStrategyArmory
;
import
cn.bugstack.domain.strategy.service.armory.StrategyArmory
;
import
cn.bugstack.domain.strategy.service.rule.impl.RuleLockLogicFilter
;
import
cn.bugstack.domain.strategy.service.rule.impl.RuleWeightLogicFilter
;
import
com.alibaba.fastjson.JSON
;
import
lombok.extern.slf4j.Slf4j
;
...
...
@@ -25,17 +28,35 @@ import javax.annotation.Resource;
@SpringBootTest
public
class
RaffleStrategyTest
{
@Resource
private
IStrategyArmory
strategyArmory
;
@Resource
private
IRaffleStrategy
raffleStrategy
;
@Resource
private
RuleWeightLogicFilter
ruleWeightLogicFilter
;
@Resource
private
RuleLockLogicFilter
ruleLockLogicFilter
;
@Before
public
void
setUp
()
{
// 策略装配 100001、100002、100003
log
.
info
(
"测试结果:{}"
,
strategyArmory
.
assembleLotteryStrategy
(
100001L
));
log
.
info
(
"测试结果:{}"
,
strategyArmory
.
assembleLotteryStrategy
(
100002L
));
log
.
info
(
"测试结果:{}"
,
strategyArmory
.
assembleLotteryStrategy
(
100003L
));
// 通过反射 mock 规则中的值
ReflectionTestUtils
.
setField
(
ruleWeightLogicFilter
,
"userScore"
,
40500L
);
ReflectionTestUtils
.
setField
(
ruleLockLogicFilter
,
"userRaffleCount"
,
10L
);
}
@Test
public
void
test
()
{
log
.
info
(
"测试结果:{}"
,
strategyArmory
.
assembleLotteryStrategy
(
100001L
));
log
.
info
(
"测试结果:{}"
,
strategyArmory
.
assembleLotteryStrategy
(
100002L
));
log
.
info
(
"测试结果:{}"
,
strategyArmory
.
assembleLotteryStrategy
(
100003L
));
}
@Test
public
void
test_performRaffle
()
{
RaffleFactorEntity
raffleFactorEntity
=
RaffleFactorEntity
.
builder
()
...
...
@@ -62,4 +83,21 @@ public class RaffleStrategyTest {
log
.
info
(
"测试结果:{}"
,
JSON
.
toJSONString
(
raffleAwardEntity
));
}
/**
* 次数错校验,抽奖n次后解锁。100003 策略,你可以通过调整 @Before 的 setUp 方法中个人抽奖次数来验证。比如最开始设置0,之后设置10
* ReflectionTestUtils.setField(ruleLockLogicFilter, "userRaffleCount", 10L);
*/
@Test
public
void
test_raffle_center_rule_lock
(){
RaffleFactorEntity
raffleFactorEntity
=
RaffleFactorEntity
.
builder
()
.
userId
(
"xiaofuge"
)
.
strategyId
(
100003L
)
.
build
();
RaffleAwardEntity
raffleAwardEntity
=
raffleStrategy
.
performRaffle
(
raffleFactorEntity
);
log
.
info
(
"请求参数:{}"
,
JSON
.
toJSONString
(
raffleFactorEntity
));
log
.
info
(
"测试结果:{}"
,
JSON
.
toJSONString
(
raffleAwardEntity
));
}
}
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/model/entity/RaffleFactorEntity.java
浏览文件 @
caf38ce3
...
...
@@ -22,4 +22,6 @@ public class RaffleFactorEntity {
private
String
userId
;
/** 策略ID */
private
Long
strategyId
;
/** 奖品ID */
private
Integer
awardId
;
}
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/model/entity/StrategyEntity.java
浏览文件 @
caf38ce3
...
...
@@ -7,6 +7,8 @@ import lombok.Data;
import
lombok.NoArgsConstructor
;
import
org.apache.commons.lang3.StringUtils
;
import
java.io.Serializable
;
/**
* @description: 策略实体
* @author: hdr
...
...
@@ -17,7 +19,7 @@ import org.apache.commons.lang3.StringUtils;
@Builder
@AllArgsConstructor
@NoArgsConstructor
public
class
StrategyEntity
{
public
class
StrategyEntity
implements
Serializable
{
/** 抽奖策略ID */
private
Long
strategyId
;
...
...
@@ -33,6 +35,9 @@ public class StrategyEntity {
public
String
getRuleWeight
()
{
String
[]
ruleModels
=
this
.
ruleModels
();
if
(
ruleModels
==
null
)
{
return
null
;
}
for
(
String
ruleModel
:
ruleModels
)
{
if
(
"rule_weight"
.
equals
(
ruleModel
))
return
ruleModel
;
}
...
...
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/model/valobj/StrategyAwardRuleModelVO.java
0 → 100644
浏览文件 @
caf38ce3
package
cn.bugstack.domain.strategy.model.valobj
;
import
cn.bugstack.domain.strategy.service.rule.factory.DefaultLogicFactory
;
import
cn.bugstack.types.common.Constants
;
import
lombok.AllArgsConstructor
;
import
lombok.Builder
;
import
lombok.Getter
;
import
lombok.NoArgsConstructor
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
/**
* @description: 抽奖策略规则规则值对象;值对象,没有唯一ID,仅限于从数据库查询对象
* @author: hdr
* @PACKAGE_NAME: cn.bugstack.domain.strategy.model.valobj
* @DATE: 2024/4/16
*/
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public
class
StrategyAwardRuleModelVO
{
private
String
ruleModels
;
/**
* 获取抽奖中规则;或者使用 lambda 表达式
* <p>
* List<String> ruleModelList = Arrays.stream(ruleModels.split(Constants.SPLIT))
* .filter(DefaultLogicFactory.LogicModel::isCenter)
* .collect(Collectors.toList());
* return ruleModelList;
* <p>
* List<String> collect = Arrays.stream(ruleModelValues).filter(DefaultLogicFactory.LogicModel::isCenter).collect(Collectors.toList());
*/
public
String
[]
raffleCenterRuleModelList
()
{
List
<
String
>
ruleModelList
=
new
ArrayList
<>();
String
[]
ruleModeValues
=
ruleModels
.
split
(
Constants
.
SPLIT
);
Arrays
.
stream
(
ruleModeValues
).
forEach
(
item
->
{
if
(
DefaultLogicFactory
.
LogicModel
.
isCenter
(
item
))
{
ruleModelList
.
add
(
item
);
}
});
return
ruleModelList
.
toArray
(
new
String
[
0
]);
}
}
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/repository/IStrategyRepository.java
浏览文件 @
caf38ce3
...
...
@@ -3,6 +3,7 @@ package cn.bugstack.domain.strategy.repository;
import
cn.bugstack.domain.strategy.model.entity.StrategyAwardEntity
;
import
cn.bugstack.domain.strategy.model.entity.StrategyEntity
;
import
cn.bugstack.domain.strategy.model.entity.StrategyRuleEntity
;
import
cn.bugstack.domain.strategy.model.valobj.StrategyAwardRuleModelVO
;
import
java.math.BigDecimal
;
import
java.util.HashMap
;
...
...
@@ -76,4 +77,12 @@ public interface IStrategyRepository {
* @return
*/
String
queryStrategyRuleValue
(
Long
strategyId
,
Integer
awardId
,
String
ruleModel
);
/**
* 查询 ruleModel
* @param strategyId
* @param awardId
* @return
*/
StrategyAwardRuleModelVO
queryStrategyAwardRuleModelVO
(
Long
strategyId
,
Integer
awardId
);
}
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/service/armory/StrategyArmory.java
浏览文件 @
caf38ce3
...
...
@@ -8,6 +8,7 @@ import cn.bugstack.types.enums.ResponseCode;
import
cn.bugstack.types.exception.AppException
;
import
com.alibaba.fastjson.JSONObject
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
...
...
@@ -44,8 +45,7 @@ public class StrategyArmory implements IStrategyArmory, IStrategyDispatch {
// 2. 权重策略陪住 - 使用于 rule_weight 权重规则配置
StrategyEntity
strategyEntity
=
repository
.
queryStrategyEntityByStrategyId
(
strategy
);
System
.
out
.
println
(
JSONObject
.
toJSONString
(
strategyEntity
));
if
(
null
==
strategyEntity
.
getRuleWeight
()||
null
==
strategyEntity
)
return
true
;
if
(
StringUtils
.
isEmpty
(
strategyEntity
.
getRuleWeight
()))
return
true
;
String
ruleWeight
=
strategyEntity
.
getRuleWeight
();
// 2.2 查询策略规则
StrategyRuleEntity
strategyRuleEntity
=
repository
.
queryStrategyRule
(
strategy
,
ruleWeight
);
...
...
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/service/raffle/AbstractRaffleStrategy.java
浏览文件 @
caf38ce3
...
...
@@ -5,6 +5,7 @@ import cn.bugstack.domain.strategy.model.entity.RaffleFactorEntity;
import
cn.bugstack.domain.strategy.model.entity.RuleActionEntity
;
import
cn.bugstack.domain.strategy.model.entity.StrategyEntity
;
import
cn.bugstack.domain.strategy.model.valobj.RuleLogicCheckTypeVO
;
import
cn.bugstack.domain.strategy.model.valobj.StrategyAwardRuleModelVO
;
import
cn.bugstack.domain.strategy.repository.IStrategyRepository
;
import
cn.bugstack.domain.strategy.service.IRaffleStrategy
;
import
cn.bugstack.domain.strategy.service.armory.IStrategyDispatch
;
...
...
@@ -53,7 +54,6 @@ public abstract class AbstractRaffleStrategy implements IRaffleStrategy {
.
userId
(
userId
)
.
strategyId
(
strategyId
)
.
build
(),
strategy
.
ruleModels
());
log
.
info
(
"ruleActionEntity:{}"
,
JSON
.
toJSONString
(
ruleActionEntity
));
if
(
RuleLogicCheckTypeVO
.
TAKE_OVER
.
getCode
().
equals
(
ruleActionEntity
.
getCode
()))
{
if
(
DefaultLogicFactory
.
LogicModel
.
RULE_BLACKLIST
.
getCode
().
equals
(
ruleActionEntity
.
getRuleModel
()))
{
...
...
@@ -63,7 +63,6 @@ public abstract class AbstractRaffleStrategy implements IRaffleStrategy {
.
build
();
}
else
if
(
DefaultLogicFactory
.
LogicModel
.
RULE_WIGHT
.
getCode
().
equals
(
ruleActionEntity
.
getRuleModel
()))
{
// 权重根据返回的信息进行抽奖
System
.
out
.
println
(
"权重抽奖"
);
RuleActionEntity
.
RaffleBeforeEntity
raffleBeforeEntity
=
ruleActionEntity
.
getData
();
String
ruleWeightValueKey
=
raffleBeforeEntity
.
getRuleWeightValueKey
();
Integer
awardId
=
strategyDispatch
.
getRandomAwardId
(
strategyId
,
ruleWeightValueKey
);
...
...
@@ -74,13 +73,31 @@ public abstract class AbstractRaffleStrategy implements IRaffleStrategy {
}
// 4. 默认抽奖流程
System
.
out
.
println
(
"默认抽奖"
);
Integer
awardId
=
strategyDispatch
.
getRandomAwardId
(
strategyId
);
// 5. 查询奖品规则「抽奖中(拿到奖品ID时,过滤规则)、抽奖后(扣减完奖品库存后过滤,抽奖中拦截和无库存则走兜底)」
StrategyAwardRuleModelVO
strategyAwardRuleModelVO
=
repository
.
queryStrategyAwardRuleModelVO
(
strategyId
,
awardId
);
// 6. 抽奖中 - 规则过滤
RuleActionEntity
<
RuleActionEntity
.
RaffleCenterEntity
>
ruleActionCenterEntity
=
this
.
doCheckRaffleCenterLogic
(
RaffleFactorEntity
.
builder
()
.
userId
(
userId
)
.
strategyId
(
strategyId
)
.
awardId
(
awardId
)
.
build
(),
strategyAwardRuleModelVO
.
raffleCenterRuleModelList
());
if
(
RuleLogicCheckTypeVO
.
TAKE_OVER
.
getCode
().
equals
(
ruleActionCenterEntity
.
getCode
())){
log
.
info
(
"【临时日志】中奖中规则拦截,通过抽奖后规则 rule_luck_award 走兜底奖励。"
);
return
RaffleAwardEntity
.
builder
()
.
awardDesc
(
"中奖中规则拦截,通过抽奖后规则 rule_luck_award 走兜底奖励。"
)
.
build
();
}
return
RaffleAwardEntity
.
builder
()
.
awardId
(
awardId
)
.
build
();
}
protected
abstract
RuleActionEntity
<
RuleActionEntity
.
RaffleCenterEntity
>
doCheckRaffleCenterLogic
(
RaffleFactorEntity
raffleFactorEntity
,
String
...
logics
);
protected
abstract
RuleActionEntity
<
RuleActionEntity
.
RaffleBeforeEntity
>
doCheckRaffleBeforeLogic
(
RaffleFactorEntity
factorEntity
,
String
...
logics
);
}
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/service/raffle/DefaultRaffleStrategy.java
浏览文件 @
caf38ce3
...
...
@@ -36,9 +36,39 @@ public class DefaultRaffleStrategy extends AbstractRaffleStrategy{
public
DefaultRaffleStrategy
(
IStrategyRepository
repository
,
IStrategyDispatch
strategyDispatch
)
{
super
(
repository
,
strategyDispatch
);
}
@Override
protected
RuleActionEntity
<
RuleActionEntity
.
RaffleCenterEntity
>
doCheckRaffleCenterLogic
(
RaffleFactorEntity
raffleFactorEntity
,
String
...
logics
)
{
if
(
logics
==
null
||
0
==
logics
.
length
)
return
RuleActionEntity
.<
RuleActionEntity
.
RaffleCenterEntity
>
builder
()
.
code
(
RuleLogicCheckTypeVO
.
ALLOW
.
getCode
())
.
info
(
RuleLogicCheckTypeVO
.
ALLOW
.
getInfo
())
.
build
();
Map
<
String
,
ILogicFilter
<
RuleActionEntity
.
RaffleCenterEntity
>>
logicFilterGroup
=
logicFactory
.
openLogicFilter
();
RuleActionEntity
<
RuleActionEntity
.
RaffleCenterEntity
>
ruleActionEntity
=
null
;
for
(
String
ruleModel
:
logics
)
{
ILogicFilter
<
RuleActionEntity
.
RaffleCenterEntity
>
logicFilter
=
logicFilterGroup
.
get
(
ruleModel
);
RuleMatterEntity
ruleMatterEntity
=
new
RuleMatterEntity
();
ruleMatterEntity
.
setUserId
(
raffleFactorEntity
.
getUserId
());
ruleMatterEntity
.
setAwardId
(
raffleFactorEntity
.
getAwardId
());
ruleMatterEntity
.
setStrategyId
(
raffleFactorEntity
.
getStrategyId
());
ruleMatterEntity
.
setRuleModel
(
ruleModel
);
ruleActionEntity
=
logicFilter
.
filter
(
ruleMatterEntity
);
// 非放行结果则顺序过滤
log
.
info
(
"抽奖中规则过滤 userId: {} ruleModel: {} code: {} info: {}"
,
raffleFactorEntity
.
getUserId
(),
ruleModel
,
ruleActionEntity
.
getCode
(),
ruleActionEntity
.
getInfo
());
if
(!
RuleLogicCheckTypeVO
.
ALLOW
.
getCode
().
equals
(
ruleActionEntity
.
getCode
()))
return
ruleActionEntity
;
}
return
ruleActionEntity
;
}
@Override
protected
RuleActionEntity
<
RuleActionEntity
.
RaffleBeforeEntity
>
doCheckRaffleBeforeLogic
(
RaffleFactorEntity
raffleFactorEntity
,
String
...
logics
)
{
if
(
logics
==
null
||
0
==
logics
.
length
)
return
RuleActionEntity
.<
RuleActionEntity
.
RaffleBeforeEntity
>
builder
()
.
code
(
RuleLogicCheckTypeVO
.
ALLOW
.
getCode
())
.
info
(
RuleLogicCheckTypeVO
.
ALLOW
.
getInfo
())
.
build
();
log
.
info
(
"raffleFactorEntity:{},logics:{}"
,
JSON
.
toJSONString
(
raffleFactorEntity
),
logics
);
Map
<
String
,
ILogicFilter
<
RuleActionEntity
.
RaffleBeforeEntity
>>
logicFilterGroup
=
logicFactory
.
openLogicFilter
();
...
...
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/service/rule/factory/DefaultLogicFactory.java
浏览文件 @
caf38ce3
...
...
@@ -40,13 +40,23 @@ public class DefaultLogicFactory {
@AllArgsConstructor
public
enum
LogicModel
{
RULE_WIGHT
(
"rule_weight"
,
"【抽奖前规则】根据抽奖权重返回可抽奖范围KEY"
),
RULE_BLACKLIST
(
"rule_blacklist"
,
"【抽奖前规则】黑名单规则过滤,命中黑名单则直接返回"
),
RULE_WIGHT
(
"rule_weight"
,
"【抽奖前规则】根据抽奖权重返回可抽奖范围KEY"
,
"before"
),
RULE_BLACKLIST
(
"rule_blacklist"
,
"【抽奖前规则】黑名单规则过滤,命中黑名单则直接返回"
,
"before"
),
RULE_LOCK
(
"rule_lock"
,
"【抽奖中规则】抽奖n次后,对应奖品可解锁抽奖"
,
"center"
),
RULE_LUCK_AWARD
(
"rule_luck_award"
,
"【抽奖后规则】抽奖n次后,对应奖品可解锁抽奖"
,
"after"
),
;
private
final
String
code
;
private
final
String
info
;
private
final
String
type
;
public
static
boolean
isCenter
(
String
code
){
return
"center"
.
equals
(
LogicModel
.
valueOf
(
code
.
toUpperCase
()).
type
);
}
public
static
boolean
isAfter
(
String
code
){
return
"after"
.
equals
(
LogicModel
.
valueOf
(
code
.
toUpperCase
()).
type
);
}
}
...
...
xfg-frame-archetype-lite-domain/src/main/java/cn/bugstack/domain/strategy/service/rule/impl/RuleLockLogicFilter.java
0 → 100644
浏览文件 @
caf38ce3
package
cn.bugstack.domain.strategy.service.rule.impl
;
import
cn.bugstack.domain.strategy.model.entity.RuleActionEntity
;
import
cn.bugstack.domain.strategy.model.entity.RuleMatterEntity
;
import
cn.bugstack.domain.strategy.model.valobj.RuleLogicCheckTypeVO
;
import
cn.bugstack.domain.strategy.repository.IStrategyRepository
;
import
cn.bugstack.domain.strategy.service.annotation.LogicStrategy
;
import
cn.bugstack.domain.strategy.service.rule.ILogicFilter
;
import
cn.bugstack.domain.strategy.service.rule.factory.DefaultLogicFactory
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.Resource
;
/**
* @description: 用户抽奖n次后,对应奖品可解锁抽奖
* @author: hdr
* @PACKAGE_NAME: cn.bugstack.domain.strategy.service.raffle
* @DATE: 2024/4/16
*/
@Slf4j
@Component
@LogicStrategy
(
logicMode
=
DefaultLogicFactory
.
LogicModel
.
RULE_LOCK
)
public
class
RuleLockLogicFilter
implements
ILogicFilter
<
RuleActionEntity
.
RaffleCenterEntity
>
{
@Resource
private
IStrategyRepository
repository
;
// 测试数据
private
Long
userRaffleCount
=
0L
;
@Override
public
RuleActionEntity
<
RuleActionEntity
.
RaffleCenterEntity
>
filter
(
RuleMatterEntity
ruleMatterEntity
)
{
log
.
info
(
"规则过滤-次数锁 userId:{} strategyId:{} ruleModel:{}"
,
ruleMatterEntity
.
getUserId
(),
ruleMatterEntity
.
getStrategyId
(),
ruleMatterEntity
.
getRuleModel
());
// 查询规则值配置;当前奖品ID,抽奖中规则对应的校验值。如;1、2、6
String
ruleValue
=
repository
.
queryStrategyRuleValue
(
ruleMatterEntity
.
getStrategyId
(),
ruleMatterEntity
.
getAwardId
(),
ruleMatterEntity
.
getRuleModel
());
long
raffleCount
=
Long
.
parseLong
(
ruleValue
);
// 用户抽奖次数大于规则限定值,规则放行
if
(
userRaffleCount
>=
raffleCount
)
{
return
RuleActionEntity
.<
RuleActionEntity
.
RaffleCenterEntity
>
builder
()
.
code
(
RuleLogicCheckTypeVO
.
ALLOW
.
getCode
())
.
info
(
RuleLogicCheckTypeVO
.
ALLOW
.
getInfo
())
.
build
();
}
// 用户抽奖次数小于规则限定值,规则拦截
return
RuleActionEntity
.<
RuleActionEntity
.
RaffleCenterEntity
>
builder
()
.
code
(
RuleLogicCheckTypeVO
.
TAKE_OVER
.
getCode
())
.
info
(
RuleLogicCheckTypeVO
.
TAKE_OVER
.
getInfo
())
.
build
();
}
}
xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/persistent/dao/IStrategyAwardDao.java
浏览文件 @
caf38ce3
...
...
@@ -22,4 +22,11 @@ public interface IStrategyAwardDao {
* @return
*/
List
<
StrategyAward
>
queryStrategyAwardListByStrategyId
(
@Param
(
"strategyId"
)
Long
strategyId
);
/**
* 查询ruleModels
* @param strategyAward
* @return
*/
String
queryStrategyAwardRuleModels
(
StrategyAward
strategyAward
);
}
xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/persistent/po/Award.java
浏览文件 @
caf38ce3
...
...
@@ -12,6 +12,7 @@ import java.util.Date;
*/
@Data
public
class
Award
{
/** 自增ID */
private
Long
id
;
/** 抽奖奖品ID - 内部流转使用 */
...
...
xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/persistent/redis/RedisService.java
浏览文件 @
caf38ce3
...
...
@@ -102,6 +102,7 @@ public class RedisService implements IRedisService {
return
redissonClient
.
getMap
(
key
);
}
@Override
public
void
addToMap
(
String
key
,
String
field
,
String
value
)
{
RMap
<
String
,
String
>
map
=
redissonClient
.
getMap
(
key
);
map
.
put
(
field
,
value
);
...
...
xfg-frame-archetype-lite-infrastructure/src/main/java/cn/bugstack/infrastructure/persistent/repository/StrategyRepository.java
浏览文件 @
caf38ce3
...
...
@@ -3,6 +3,7 @@ package cn.bugstack.infrastructure.persistent.repository;
import
cn.bugstack.domain.strategy.model.entity.StrategyAwardEntity
;
import
cn.bugstack.domain.strategy.model.entity.StrategyEntity
;
import
cn.bugstack.domain.strategy.model.entity.StrategyRuleEntity
;
import
cn.bugstack.domain.strategy.model.valobj.StrategyAwardRuleModelVO
;
import
cn.bugstack.domain.strategy.repository.IStrategyRepository
;
import
cn.bugstack.infrastructure.persistent.dao.IStrategyAwardDao
;
import
cn.bugstack.infrastructure.persistent.dao.IStrategyDao
;
...
...
@@ -16,6 +17,7 @@ import com.alibaba.fastjson.JSON;
import
com.alibaba.fastjson.JSONObject
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.lang3.ObjectUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.stereotype.Repository
;
...
...
@@ -55,9 +57,10 @@ public class StrategyRepository implements IStrategyRepository {
public
List
<
StrategyAwardEntity
>
queryStrategyAwardList
(
Long
strategy
)
{
// 1. 查询redis中是否有保存策略数据有的话直接返回
String
cacheKey
=
Constants
.
RedisKey
.
STRATEGY_AWARD_KEY
+
strategy
;
Object
json
=
redisService
.
getValue
(
cacheKey
);
List
<
StrategyAwardEntity
>
strategyAwardEntities
=
JSON
.
parseArray
(
json
.
toString
(),
StrategyAwardEntity
.
class
);
if
(
strategyAwardEntities
!=
null
&&
!
strategyAwardEntities
.
isEmpty
())
{
List
<
StrategyAwardEntity
>
strategyAwardEntities
=
redisService
.
getValue
(
cacheKey
);
if
(
strategyAwardEntities
!=
null
&&
strategyAwardEntities
.
size
()
!=
0
)
{
// List<StrategyAwardEntity> strategyAwardEntities = JSON.parseArray(json.toString(), StrategyAwardEntity.class);
return
strategyAwardEntities
;
}
...
...
@@ -75,7 +78,7 @@ public class StrategyRepository implements IStrategyRepository {
// 2.1 保存到redis中
//
redisService.setValue(cacheKey, strategyAwardEntityList);
redisService
.
setValue
(
cacheKey
,
strategyAwardEntityList
);
return
strategyAwardEntityList
;
}
...
...
@@ -103,21 +106,33 @@ public class StrategyRepository implements IStrategyRepository {
public
StrategyEntity
queryStrategyEntityByStrategyId
(
Long
strategyId
)
{
// 1. 优先从缓存获取
String
cacheKey
=
Constants
.
RedisKey
.
STRATEGY_KEY
+
strategyId
;
Map
<
String
,
Object
>
maps
=
redisService
.
getValue
(
cacheKey
);
StrategyEntity
strategyEntity
=
StrategyEntity
.
builder
()
.
strategyId
(
Long
.
parseLong
(
maps
.
get
(
"strategyId"
).
toString
()))
.
strategyDesc
((
String
)
maps
.
get
(
"strategyDesc"
))
.
ruleModels
((
String
)
maps
.
get
(
"ruleModels"
))
.
build
();
if
(
null
!=
strategyEntity
)
return
strategyEntity
;
StrategyEntity
strategyEntity
=
new
StrategyEntity
();
StrategyEntity
maps
=
redisService
.
getValue
(
cacheKey
);
if
(
null
!=
maps
){
// strategyEntity = StrategyEntity.builder()
// .strategyId(Long.parseLong(maps.get("strategyId").toString()))
// .strategyDesc((String) maps.get("strategyDesc"))
// .ruleModels((String) maps.get("ruleModels"))
// .build();
return
maps
;
}
// 2. 没有数据从数据库获取
Strategy
strategy
=
strategyDao
.
queryStrategyByStrategyId
(
strategyId
);
strategyEntity
=
StrategyEntity
.
builder
()
.
strategyId
(
strategy
.
getStrategyId
())
.
strategyDesc
(
strategy
.
getStrategyDesc
())
.
ruleModels
(
strategy
.
getRuleModels
())
.
build
();
strategyEntity
.
setStrategyId
(
strategy
.
getStrategyId
());
if
(
StringUtils
.
isNotBlank
(
strategy
.
getRuleModels
()))
{
strategyEntity
.
setRuleModels
(
strategy
.
getRuleModels
());
}
strategyEntity
.
setStrategyDesc
(
strategy
.
getStrategyDesc
());
// = StrategyEntity.builder()
// .strategyId(strategy.getStrategyId())
// .strategyDesc(strategy.getStrategyDesc())
// .ruleModels(strategy.getRuleModels())
// .build();
// 3. 写入缓存
log
.
info
(
"strategyEntity:{}"
,
strategyEntity
);
redisService
.
setValue
(
cacheKey
,
strategyEntity
);
return
strategyEntity
;
}
...
...
@@ -152,4 +167,14 @@ public class StrategyRepository implements IStrategyRepository {
return
strategyRuleDao
.
queryStrategyRuleValue
(
strategyRule
);
}
@Override
public
StrategyAwardRuleModelVO
queryStrategyAwardRuleModelVO
(
Long
strategyId
,
Integer
awardId
)
{
StrategyAward
strategyAward
=
new
StrategyAward
();
strategyAward
.
setStrategyId
(
strategyId
);
strategyAward
.
setAwardId
(
awardId
);
String
ruleModels
=
strategyAwardDao
.
queryStrategyAwardRuleModels
(
strategyAward
);
return
StrategyAwardRuleModelVO
.
builder
().
ruleModels
(
ruleModels
).
build
();
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录