h5周菜谱

This commit is contained in:
sxu 2025-06-08 09:13:49 +08:00
parent 567b902073
commit 150c494d37
2 changed files with 52 additions and 0 deletions

View File

@ -176,4 +176,12 @@ public class CookRecipeController extends BaseController {
public AjaxResult remove(@PathVariable Long[] recipeIds) {
return toAjax(cookRecipeService.deleteCookRecipeByRecipeIds(recipeIds));
}
@PostMapping({"/generateRecipe"})
@ApiOperation("测试定时任务")
@Transactional
public AjaxResult generateRecipe() {
this.cookRecipeService.generateRecipe();
return AjaxResult.success();
}
}

View File

@ -0,0 +1,44 @@
package com.bonus.canteen.core.cook.task;
import com.bonus.canteen.core.common.utils.RedisUtil;
import com.bonus.canteen.core.cook.service.ICookRecipeService;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.support.TransactionTemplate;
import java.time.LocalDateTime;
@Component
public class CookRecipeTask {
private static final Logger log = LoggerFactory.getLogger(CookRecipeTask.class);
@Autowired
private ICookRecipeService cookRecipeService;
@Autowired
private TransactionTemplate transactionTemplate;
//@XxlJob("cookRecipeHandler")
@Scheduled(fixedDelay = 1*60*60*1000)
public void couponFixedTimeHandler() {
boolean cookRecipeHandler = RedisUtil.setNx("cookRecipeHandler", "1", 3600);
System.err.println("cookRecipeHandler="+cookRecipeHandler);
if (cookRecipeHandler) {
transactionTemplate.execute(status -> {
try {
// 业务代码
log.info("[定时生成菜谱]开始:{},{}", status, LocalDateTime.now());
this.cookRecipeService.generateRecipe();
log.info("[定时生成菜谱]结束:{},{}", status, LocalDateTime.now());
return null;
} catch (Exception e) {
status.setRollbackOnly(); // 标记回滚
throw e;
}
});
}
}
}