Commit 68fec1e1 by zhouhaibao

refactor(strategy): 优化策略条件验证和删除逻辑

- 修改SopCustomerQueryService中的条件验证方法,简化查询逻辑并使用数组包装单个分支
- 为SopStrategyActionMapper、SopStrategyActionMediaMapper和SopStrategyConditionMapper添加按策略ID删除的方法
- 更新触发时间计算器逻辑,改为仅当天任务才下发
- 移除未来90天的触发时间检查限制
- 在策略服务中添加对步骤2和步骤3条件组的清理逻辑
- 重构策略更新时的数据清理流程,确保相关条件和动作数据被正确删除
1 parent 7344a21f
......@@ -13,4 +13,6 @@ public interface SopStrategyActionMapper extends OreMapper<SopStrategyAction> {
List<SopStrategyAction> selectByBranchId(@Param("branchId") Integer branchId);
int deleteByBranchId(@Param("branchId") Integer branchId);
int deleteByStrategyId(@Param("strategyId") Integer strategyId);
}
\ No newline at end of file
......@@ -13,4 +13,6 @@ public interface SopStrategyActionMediaMapper extends OreMapper<SopStrategyActio
List<SopStrategyActionMedia> selectByActionId(@Param("actionId") Integer actionId);
int deleteByActionId(@Param("actionId") Integer actionId);
int deleteByStrategyId(@Param("strategyId") Integer strategyId);
}
\ No newline at end of file
......@@ -13,4 +13,6 @@ public interface SopStrategyConditionMapper extends OreMapper<SopStrategyConditi
List<SopStrategyCondition> selectByGroupId(@Param("groupId") Integer groupId);
int deleteByGroupId(@Param("groupId") Integer groupId);
int deleteByStrategyId(@Param("strategyId") Integer strategyId);
}
\ No newline at end of file
......@@ -16,6 +16,7 @@ import cn.com.glsx.dj.strategy.model.sop.SopStrategyMetaDateVariable;
import cn.com.glsx.dj.strategy.model.sop.SopStrategyMetaMarketingStage;
import cn.com.glsx.dj.strategy.model.sop.SopStrategyStoreRel;
import cn.hutool.core.collection.CollUtil;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -140,40 +141,24 @@ public class SopCustomerQueryService {
return getDateValue(customer, dateColumn);
}
public boolean checkConditionSatisfied(SopStrategy strategy, Long customerInstanceId) {
return checkStepConditionSatisfied(strategy, customerInstanceId, null);
}
public boolean checkStepConditionSatisfied(SopStrategy strategy, Long customerInstanceId, Integer step) {
log.info("验证客户条件是否满足, strategyId:{}, customerInstanceId:{}, step:{}", strategy.getId(), customerInstanceId, step);
List<SopStrategyBranch> branches;
if (step != null) {
branches = sopStrategyBranchMapper.selectByStrategyIdAndStep(strategy.getId(), step);
} else {
branches = sopStrategyBranchMapper.selectByStrategyId(strategy.getId());
}
SopStrategyBranch sopStrategyBranch=new SopStrategyBranch();
if (CollUtil.isEmpty(branches)) {
log.info("策略未配置条件分支(step:{})或无匹配分支, strategyId:{}, 默认条件满足", step, strategy.getId());
return true;
}
for (SopStrategyBranch branch : branches) {
List<SopStrategyConditionGroup> conditionGroups = sopStrategyConditionGroupMapper.selectByBranchId(branch.getId());
branch.setConditionGroups(conditionGroups);
List<SopStrategyConditionGroup> conditionGroups = sopStrategyConditionGroupMapper.selectByStrategyIdAndStep(strategy.getId(),step);
sopStrategyBranch.setConditionGroups(conditionGroups);
for (SopStrategyConditionGroup group : conditionGroups) {
List<SopStrategyCondition> conditions = sopStrategyConditionMapper.selectByGroupId(group.getId());
group.setConditions(conditions);
}
for (SopStrategyConditionGroup group : conditionGroups) {
List<SopStrategyCondition> conditions = sopStrategyConditionMapper.selectByGroupId(group.getId());
group.setConditions(conditions);
}
boolean result;
Map<String, Object> customer = getCustomerById(customerInstanceId, strategy);
if (customer != null) {
customer.put("triggerDate", new Date());
result = sopDynamicSqlService.executeConditionValidationFromSnapshot(customer, branches);
result = sopDynamicSqlService.executeConditionValidationFromSnapshot(customer, Arrays.asList(sopStrategyBranch));
log.info("使用快照数据条件验证结果: {}, strategyId:{}, customerInstanceId:{}, step:{}", result, strategy.getId(), customerInstanceId, step);
} else {
throw new BusinessException("客户不存在");
......
......@@ -92,14 +92,8 @@ public class SopStrategyTriggerService {
continue;
}
if (triggerTimeCalculator.isTriggerTimeInFuture(triggerTime, 90)) {
log.info(
"触发时间在未来90天以后, 暂不处理, strategyId:{}, customerInstanceId:{}, triggerTime:{}",
strategy.getId(), customerInstanceId, triggerTime);
continue;
}
if (!triggerTimeCalculator.isTriggerTimeReached(triggerTime)) {
// 如果是当天的任务,才下发
if (triggerTimeCalculator.isTriggerTimeReached(triggerTime)) {
Integer taskId = createPendingTask(strategy, customerInstanceId, triggerTime);
if (taskId != null) {
sopCustomerSnapshotService.saveCustomerSnapshotWithTaskId(strategy, taskId,
......@@ -107,14 +101,6 @@ public class SopStrategyTriggerService {
}
log.info("创建待执行任务, strategyId:{}, customerInstanceId:{}, triggerTime:{}",
strategy.getId(), customerInstanceId, triggerTime);
} else {
Integer taskId = createImmediateTask(strategy, customerInstanceId);
if (taskId != null) {
sopCustomerSnapshotService.saveCustomerSnapshotWithTaskId(strategy, taskId,
customerInstanceId);
}
log.info("条件满足,创建立即执行任务, strategyId:{}, customerInstanceId:{}",
strategy.getId(), customerInstanceId);
}
} catch (Exception e) {
log.error("处理客户ID {} 失败: {}", customerInstanceId, e.getMessage(), e);
......@@ -135,10 +121,6 @@ public class SopStrategyTriggerService {
return sopCustomerQueryService.getCustomerBehaviorDate(customerInstanceId, strategy);
}
private boolean checkConditionSatisfied(SopStrategy strategy, Long customerInstanceId) {
return sopCustomerQueryService.checkConditionSatisfied(strategy, customerInstanceId);
}
private boolean checkStep2ConditionSatisfied(SopStrategy strategy, Long customerInstanceId) {
return sopCustomerQueryService.checkStepConditionSatisfied(strategy, customerInstanceId,
STEP_2);
......
package cn.com.glsx.dj.strategy.service;
import cn.com.glsx.dj.strategy.model.sop.SopStrategy;
import cn.com.glsx.framework.core.util.DateUtils;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
......@@ -63,7 +64,7 @@ public class SopTriggerTimeCalculator {
if (triggerTime == null) {
return false;
}
return !triggerTime.after(new Date());
return DateUtils.formatDate(triggerTime).equals(DateUtils.formatDate(new Date()));
}
public boolean isTriggerTimeInFuture(Date triggerTime, int daysThreshold) {
......
......@@ -75,6 +75,7 @@ public class StrategySopService {
saveStrategyStores(strategyId, request.getStoreIds(), createdBy);
saveStep2ConditionGroups(strategyId, request.getStep2ConditionGroups(), createdBy);
saveStrategyBranches(strategyId, request.getBranches(), createdBy);
sopStrategyEventLogService.recordSubmitChangeEvent(strategyId, null);
......@@ -100,9 +101,12 @@ public class StrategySopService {
sopStrategyStoreRelMapper.deleteByStrategyId(request.getStrategyId());
saveStrategyStores(request.getStrategyId(), request.getStoreIds(), updatedBy);
sopStrategyConditionMapper.deleteByStrategyId(request.getStrategyId());
sopStrategyConditionGroupMapper.deleteByStrategyIdAndStep(request.getStrategyId(), 2);
saveStep2ConditionGroups(request.getStrategyId(), request.getStep2ConditionGroups(), updatedBy);
sopStrategyActionMediaMapper.deleteByStrategyId(request.getStrategyId());
sopStrategyActionMapper.deleteByStrategyId(request.getStrategyId());
sopStrategyBranchMapper.deleteByStrategyId(request.getStrategyId());
saveStrategyBranches(request.getStrategyId(), request.getBranches(), updatedBy);
......@@ -225,6 +229,7 @@ public class StrategySopService {
return;
}
int groupNo = 1;
sopStrategyConditionGroupMapper.deleteByStrategyIdAndStep(strategyId, 2);
for (SopConditionGroupReqDTO groupReq : conditionGroups) {
SopStrategyConditionGroup group = BeanUtil.copyProperties(groupReq, SopStrategyConditionGroup.class);
group.setStrategyId(strategyId);
......@@ -247,6 +252,7 @@ public class StrategySopService {
return;
}
int groupNo = 1;
sopStrategyConditionGroupMapper.deleteByStrategyIdAndStep(strategyId, 3);
for (SopConditionGroupReqDTO groupReq : conditionGroups) {
SopStrategyConditionGroup group = BeanUtil.copyProperties(groupReq, SopStrategyConditionGroup.class);
group.setStrategyId(strategyId);
......
......@@ -38,4 +38,9 @@
UPDATE sop_strategy_action SET deleted_flag = 'D', updated_date = NOW()
WHERE branch_id = #{branchId}
</delete>
<delete id="deleteByStrategyId">
UPDATE sop_strategy_action SET deleted_flag = 'D', updated_date = NOW()
WHERE strategy_id = #{strategyId}
</delete>
</mapper>
\ No newline at end of file
......@@ -24,4 +24,9 @@
UPDATE sop_strategy_action_media SET deleted_flag = 'D', updated_date = NOW()
WHERE action_id = #{actionId}
</delete>
<delete id="deleteByStrategyId">
UPDATE sop_strategy_action_media SET deleted_flag = 'D', updated_date = NOW()
WHERE action_id IN (SELECT id FROM sop_strategy_action WHERE strategy_id = #{strategyId})
</delete>
</mapper>
\ No newline at end of file
......@@ -35,4 +35,9 @@
UPDATE sop_strategy_condition SET deleted_flag = 'D', updated_date = NOW()
WHERE group_id = #{groupId}
</delete>
<delete id="deleteByStrategyId">
UPDATE sop_strategy_condition SET deleted_flag = 'D', updated_date = NOW()
WHERE strategy_id = #{strategyId}
</delete>
</mapper>
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!