【JAVA】定时任务之借阅到期自动催还

该篇主要以定时任务为主,通知略为记录

一. 定时任务逻辑代码

  1. 定时任务文件
    在这里插入图片描述
  2. TaskScheduling.java
package org.springblade.modules.task;

import lombok.extern.slf4j.Slf4j;
import org.springblade.modules.archiveAdvantage.service.IArchiveBorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@EnableAsync
@Slf4j
public class TaskScheduling {

    @Autowired
    private IArchiveBorrowService borrowService;

    /**
     * 档案借阅自动催还处理,每天凌晨1点执行一次
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public synchronized void automaticReminder(){
        log.info(new Date() + "...>>cron....档案借阅自动催还处理");
        borrowService.automaticReminderNotice();
        log.info(new Date() + "...>>cron....档案借阅自动催还处理...完成!!!");
    }
}

  1. ArchiveBorrowService.java
void automaticReminderNotice();
  1. ArchiveBorrowServiceImpl.java
    (1)方法一
// 引入通知
import org.springblade.common.utils.NoticeUtil;

@Service
public class ArchiveBorrowServiceImpl extends BaseServiceImpl<ArchiveBorrowMapper, ArchiveBorrowEntity> implements IArchiveBorrowService {
	private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Override
	public void automaticReminderNotice() {
		try{
			// 查询已借阅未归还的实体档案
			LambdaQueryWrapper<ArchiveBorrowEntity> wrapper = new LambdaQueryWrapper<>();
			wrapper.eq(ArchiveBorrowEntity::getBorrowType, "0").eq(ArchiveBorrowEntity::getBorrowState, "23");
			List<ArchiveBorrowEntity> archiveBorrowList = this.list(wrapper);
			if(archiveBorrowList.size() > 0 ){
				for(ArchiveBorrowEntity archiveBorrow:archiveBorrowList){
					// 计算归还日期:借阅时间 + 借阅天数
					Calendar calenDar = Calendar.getInstance();
					calenDar.setTime(archiveBorrow.getBorrowTime());
					calenDar.add(calenDar.DATE,archiveBorrow.getBorrowDays());
					// 间隔差:归还时间-当前时间
					Calendar cal = Calendar.getInstance();
                    cal.setTime(returnDateFomat.parse(sdf.format(new Date())));
					long currentTime = cal.getTimeInMillis();
					cal.setTime(returnDateFomat.parse(sdf.format(calenDar.getTime())));
					long returnTime = cal.getTimeInMillis();
					long days = (returnTime - currentTime) / (1000 * 3600 * 24);
					int endDays = Integer.parseInt(String.valueOf(days));
					// 到期归还通知提醒
					if(endDays == 1){
						NoticeUtil.NoticeReturnMsg(archiveBorrow.getBorrowerUser(),"借阅待归还提醒","您于" + sdf.format(new Date()) + "接收到来自管理员的催还通知,请尽快到[我的借阅]画面确认。",1);
					}
				}
			}
		} catch (Exception e){
			e.printStackTrace();
			System.out.println("档案借阅催还失败");
		}
	}
}

(2)方法二

	@Override
	public R automaticReminderNotice() {
		// 查询已借阅未归还的档案
		LambdaQueryWrapper<ArchiveBorrowEntity> wrapper = new LambdaQueryWrapper<>();
		// 0 代表实体档案
		wrapper.eq(ArchiveBorrowEntity::getBorrowType, "0").eq(ArchiveBorrowEntity::getBorrowState, "23");
		List<ArchiveBorrowEntity> archiveBorrowList = this.list(wrapper);
		if(archiveBorrowList.size() > 0 ){
			for(ArchiveBorrowEntity archiveBorrow:archiveBorrowList){
				// 计算归还日期(1)获取借阅时间并转为毫秒数(2)将借阅天数转为毫秒数(3)归还时间 = 系统时间 + 借阅天数
				long borrowTime = archiveBorrow.getBorrowTime().getTime();
				int borrowDays = archiveBorrow.getBorrowDays() * 24 * 60 * 60 * 1000;
				// String returnTime = sdf.format(borrowTime + borrowDays);
				long returnTime = borrowTime + borrowDays;
				// 归还时间和当前时间相隔的天数(1)获取当前时间(2)当前时间和归还时间像个天数
				long currentTime = new Date().getTime();
				long compare = (currentTime - returnTime) / (24 * 60 * 60 * 1000);
				if(compare == 7){
					// 给用户发通知
					NoticeUtil.notifyMsg(archiveBorrow.getBorrowerUser(),"档案借阅催还提醒","请尽快归还档案",1);
				}
			}
		}
	}

二. 参考资料

【JAVA】定时任务之借阅到期自动催还_第1张图片

三. 站内通知(简单了解)

  1. NoticeUtil.java
  public static void NoticeReturnMsg(Long userId, String title, String content,Integer type) {
        try {
            IMessageService messageClient = SpringUtil.getBean("messageService");
            MessageParams message = MessageParams.builder().title(title)
                    .content(content)
                    .receiveUser((userId))
                    .type(type)
                    .build();

            messageClient.sendMessage(message);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("通知发送失败:" + title);
        }
    }

你可能感兴趣的:(Java,java,开发语言)