forked from linlinjava/litemall
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eff4862
commit d6f243d
Showing
36 changed files
with
1,163 additions
and
931 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...ll-admin-api/src/main/java/org/linlinjava/litemall/admin/task/AdminTaskStartupRunner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.linlinjava.litemall.admin.task; | ||
|
||
import org.linlinjava.litemall.core.task.TaskService; | ||
import org.linlinjava.litemall.db.domain.LitemallGrouponRules; | ||
import org.linlinjava.litemall.db.service.LitemallGrouponRulesService; | ||
import org.linlinjava.litemall.db.util.GrouponConstant; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.ApplicationArguments; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.List; | ||
|
||
@Component | ||
public class AdminTaskStartupRunner implements ApplicationRunner { | ||
|
||
@Autowired | ||
private LitemallGrouponRulesService rulesService; | ||
@Autowired | ||
private TaskService taskService; | ||
|
||
@Override | ||
public void run(ApplicationArguments args) throws Exception { | ||
List<LitemallGrouponRules> grouponRulesList = rulesService.queryByStatus(GrouponConstant.RULE_STATUS_ON); | ||
for(LitemallGrouponRules grouponRules : grouponRulesList){ | ||
LocalDateTime now = LocalDateTime.now(); | ||
LocalDateTime expire = grouponRules.getExpireTime(); | ||
if(expire.isBefore(now)) { | ||
// 已经过期,则加入延迟队列 | ||
taskService.addTask(new GrouponRuleExpiredTask(grouponRules.getId(), 0)); | ||
} | ||
else{ | ||
// 还没过期,则加入延迟队列 | ||
long delay = ChronoUnit.MILLIS.between(now, expire); | ||
taskService.addTask(new GrouponRuleExpiredTask(grouponRules.getId(), delay)); | ||
} | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...ll-admin-api/src/main/java/org/linlinjava/litemall/admin/task/GrouponRuleExpiredTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package org.linlinjava.litemall.admin.task; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.linlinjava.litemall.core.task.Task; | ||
import org.linlinjava.litemall.core.util.BeanUtil; | ||
import org.linlinjava.litemall.db.domain.LitemallGroupon; | ||
import org.linlinjava.litemall.db.domain.LitemallGrouponRules; | ||
import org.linlinjava.litemall.db.domain.LitemallOrder; | ||
import org.linlinjava.litemall.db.service.*; | ||
import org.linlinjava.litemall.db.util.GrouponConstant; | ||
import org.linlinjava.litemall.db.util.OrderUtil; | ||
|
||
import java.util.List; | ||
|
||
public class GrouponRuleExpiredTask extends Task { | ||
private final Log logger = LogFactory.getLog(GrouponRuleExpiredTask.class); | ||
private int grouponRuleId = -1; | ||
|
||
public GrouponRuleExpiredTask(Integer grouponRuleId, long delayInMilliseconds){ | ||
super("GrouponRuleExpiredTask-" + grouponRuleId, delayInMilliseconds); | ||
this.grouponRuleId = grouponRuleId; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
logger.info("系统开始处理延时任务---团购规则过期---" + this.grouponRuleId); | ||
|
||
LitemallOrderService orderService = BeanUtil.getBean(LitemallOrderService.class); | ||
LitemallGrouponService grouponService = BeanUtil.getBean(LitemallGrouponService.class); | ||
LitemallGrouponRulesService grouponRulesService = BeanUtil.getBean(LitemallGrouponRulesService.class); | ||
|
||
LitemallGrouponRules grouponRules = grouponRulesService.findById(grouponRuleId); | ||
if(grouponRules == null){ | ||
return; | ||
} | ||
if(!grouponRules.getStatus().equals(GrouponConstant.RULE_STATUS_ON)){ | ||
return; | ||
} | ||
|
||
// 团购活动取消 | ||
grouponRules.setStatus(GrouponConstant.RULE_STATUS_DOWN_EXPIRE); | ||
grouponRulesService.updateById(grouponRules); | ||
|
||
List<LitemallGroupon> grouponList = grouponService.queryByRuleId(grouponRuleId); | ||
// 用户团购处理 | ||
for(LitemallGroupon groupon : grouponList){ | ||
Short status = groupon.getStatus(); | ||
LitemallOrder order = orderService.findById(groupon.getOrderId()); | ||
if(status.equals(GrouponConstant.STATUS_NONE)){ | ||
groupon.setStatus(GrouponConstant.STATUS_FAIL); | ||
grouponService.updateById(groupon); | ||
} | ||
else if(status.equals(GrouponConstant.STATUS_ON)){ | ||
// 如果团购进行中 | ||
// (1) 团购设置团购失败等待退款状态 | ||
groupon.setStatus(GrouponConstant.STATUS_FAIL); | ||
grouponService.updateById(groupon); | ||
// (2) 团购订单申请退款 | ||
if(OrderUtil.isPayStatus(order)) { | ||
order.setOrderStatus(OrderUtil.STATUS_REFUND); | ||
orderService.updateWithOptimisticLocker(order); | ||
} | ||
} | ||
} | ||
logger.info("系统结束处理延时任务---团购规则过期---" + this.grouponRuleId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.