SpringBoot我有同一段代码,定时器和接口都用,我想把这个代码合成一个公共方法,如何才能实现?

小刘
  • 525

SpringBoot我有同一段代码,定时器和接口都用,我想把这个代码合成一个公共方法,如何才能实现?

@Scheduled(fixedRate = 10 * 60 * 1000)
//    @Scheduled(fixedRate = 30 * 1000)
    public void earlyWarningTasks() {
        System.out.println("YYYYYYYYYYYYYYYYYYY执行异常预警" + new Date());
        Map<String, Object> map = new HashMap<>();

        map.put("start", 0);
        map.put("dataLength", 500);
        map.put("sort", "ASC");
        List<OverviewCount> overviewCountList = bulldozerOverviewService.selectBulldozerOverviewDrivingCount(map);

        for(OverviewCount overviewCount: overviewCountList) {

//                System.out.println("licenseNumber == " + overviewCount.getLicenseNumber());
            String warnTimeStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(overviewCount.getWarnTime());
            int countEarlyWarning = earlyWarningService.checkEarlyWarningIsExistedByTime(
                    overviewCount.getLicenseNumber(),
                    warnTimeStr
            );
//                System.out.println("countEarlyWarning == " + countEarlyWarning);

            if(countEarlyWarning >= 1) {
                Map<String, Object> earlyWarningSearchMap = new HashMap<>();
                earlyWarningSearchMap.put("licenseNumber", overviewCount.getLicenseNumber());
                earlyWarningSearchMap.put("warningTime", warnTimeStr);
                earlyWarningSearchMap.put("start", 0);
                earlyWarningSearchMap.put("dataLength", 10);

                List <EarlyWarning> earlyWarningSearchList = earlyWarningService.searchEarlyWarningV1(earlyWarningSearchMap);
                if(earlyWarningSearchList.size() > 0) {
                    int entryCountSearch = earlyWarningSearchList.get(0).getEntryCount();
                    int leaveCountSearch = earlyWarningSearchList.get(0).getLeaveCount();
                    if(entryCountSearch == overviewCount.getEntryCount()&&
                            leaveCountSearch == overviewCount.getLeaveCount()
                    ) {
//                            System.out.println("异常数据没有变化");
                    } else {
//                            System.out.println("UUUUUUUUU更新异常数据变化");
                        Date dateCurrent = new Date();
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
                        SimpleDateFormat dfNoHour = new SimpleDateFormat("yyyy-MM-dd");

                        String dateCurrentStr = df.format(dateCurrent);//获取String类型的时间
                        String dateNoHourCurrentStr = dfNoHour.format(dateCurrent);
                        Timestamp warningTime = new Timestamp(new Date().getTime());

                        String overviewWarnTimeStr = dfNoHour.format(overviewCount.getWarnTime());

                        if(dateNoHourCurrentStr.equals(overviewWarnTimeStr)) {
                            earlyWarningSearchList.get(0).setWarningTime(warningTime);
//                                System.out.println("today");
                        } else {
//                                System.out.println("不是今天的");
                        }

                        if(overviewCount.getEntryCount() == overviewCount.getLeaveCount()) {
                            earlyWarningSearchList.get(0).setWarningType("");
                            earlyWarningSearchList.get(0).setFlagDelete(1);
                        }

                        earlyWarningSearchList.get(0).setLeaveCount(overviewCount.getLeaveCount());
                        earlyWarningSearchList.get(0).setEntryCount(overviewCount.getEntryCount());
//                            System.out.println(earlyWarningSearchList.get(0));
                        earlyWarningService.updateEarlyWarningByIdAndLicenseNumber(
                                earlyWarningSearchList.get(0)
                        );
                    }
                }
            } else { // 记录查询不到,新增

                System.out.println("查询不到数据");
                EarlyWarning earlyWarning = new EarlyWarning();
                earlyWarning.setLicenseNumber(overviewCount.getLicenseNumber());
                earlyWarning.setEntryCount(overviewCount.getEntryCount());
                earlyWarning.setLeaveCount(overviewCount.getLeaveCount());
//                earlyWarning.setWarningTime(overviewCount.getWarnTime());

                String warnTimeFormat = DateUtils.timestampToStringFormat(overviewCount.getWarnTime(), "YMD");
                String dataFormat = DateUtils.dateToStringFormat(new Date(), "YMD");

                if(warnTimeFormat.equals(dataFormat)) {
//                        System.out.println("warnTimeFormat == " + DateUtils.timestampToStringFormat(overviewCount.getWarnTime(), "FULL"));
                    earlyWarning.setWarningTime(new Timestamp(new Date().getTime()));
//                    System.out.println("相等warnTimeFormat == " + warnTimeFormat);
//                    System.out.println("相等dataFormat == " + dataFormat);
                } else {
//                    System.out.println("不等warnTimeFormat == " + warnTimeFormat);
//                    System.out.println("不等dataFormat == " + dataFormat);
                    earlyWarning.setWarningTime(overviewCount.getWarnTime());
                }



                if(overviewCount.getEntryCount() != overviewCount.getLeaveCount()) {
                    earlyWarning.setFlagDelete(0);
                    earlyWarning.setWarningType("出入次数异常");
                } else {
                    earlyWarning.setFlagDelete(1);
                }
                earlyWarningService.insertEarlyWarning(earlyWarning);
            }


        }

        String dateStr = DateUtils.dateToString(new Date());
        stringRedisTemplate.opsForValue().set("timeRefreshWaring", dateStr);
    }
回复
阅读 218
1 个回答

这个应该是最基本的类抽象能力吧。。。

@Component
class SomeThingService {
    public void doSomething() {
        // 你的业务逻辑
    }
}
@Component
class SomeThingTimer {
    
    @Resource
    private SomeThingService someThingService;
    
    @Scheduled(fixedRate = 10 * 60 * 1000)
    public void doSomethingScheduled() {
        // 你的定时任务专有逻辑
        someThingService.doSomething();
        // 你的定时任务专有逻辑
    }
}
@RequestMapping("/api/something")
@RestController
class SomeThingController {
    
    @Resource
    private SomeThingService someThingService;
    
    @GetMapping("/getItDone")
    public 返回类 doSomething() {
        // 你的接口专有逻辑
        someThingService.doSomething();
        // 你的接口专有逻辑
    }
}
你知道吗?

宣传栏