加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
BirthdayJob.java 4.24 KB
一键复制 编辑 原始数据 按行查看 历史
白玉龙 提交于 2020-04-03 22:52 . 代码段
package com.baiyulong.crm.job;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.baiyulong.crm.bean.BirthdayManager;
import com.baiyulong.crm.bean.CustomerContanct;
import com.baiyulong.crm.service.BirthdayManagerService;
import com.baiyulong.crm.service.CustomerContanctService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
/**
* 生日定时任务
* @author byl
*
*/
@Component
public class BirthdayJob {
@Resource
private BirthdayManagerService birthdayManagerService;
@Resource
private CustomerContanctService customerContanctService;
@Scheduled(cron = "0 0 23 * * ?") //每天23点执行一次
//@Scheduled(cron = "0/5 * * * * ? ") //方便测试每五秒执行一次
public void birthdayJob() throws Exception{
System.out.println("开始定时任务");
//1.获取所有联系人,如果七天内过生日则插入生日管理表中
EntityWrapper entityWrapper = new EntityWrapper();
entityWrapper.eq("birthday_flage", 0);
List<CustomerContanct> selectList = customerContanctService.selectList(entityWrapper);
if(selectList != null && selectList.size() > 0) {
for (CustomerContanct contanct : selectList) {
Date birthday = contanct.getBirthday();
int day = getDay(birthday);
//生日未过 并且在七天之内 插入生日管理表
if(day <= 7 && day != -1) {
BirthdayManager manager = new BirthdayManager();
manager.setAddress(contanct.getAddress());
manager.setBirthday(contanct.getBirthday());
manager.setContanctId(contanct.getCustomerContanctId());
manager.setContanctName(contanct.getContanctName());
manager.setCustomerName(contanct.getCustomerName());
manager.setPhone(contanct.getContanctPhone());
manager.setSex(contanct.getSex());
manager.setEmail(contanct.getEmail());
manager.setUserId(contanct.getUserId());
birthdayManagerService.insert(manager);
//将联系人生日提醒置为已提醒
contanct.setBirthdayFlage(1);
customerContanctService.updateById(contanct);
}
}
}
//2.获取生日管理表,如果已经过完生日但是未被移除的 自动移除生日管理表
List<BirthdayManager> managers = birthdayManagerService.selectList(null);
if(managers != null && managers.size() > 0) {
for (BirthdayManager birthdayManager : managers) {
Date birthday = birthdayManager.getBirthday();
int day = getDay(birthday);
//day==-1 说明生日已过 应该移除生日管理表
if(day == -1) {
birthdayManagerService.deleteById(birthdayManager.getId());
//将生日标志置为未提醒
customerContanctService.updateBirthFlage(birthdayManager.getContanctId());
}
}
}
System.out.println("定时任务结束");
}
/**
* 获取生日天数
* @param date
* @return
* @throws ParseException
*/
public static int getDay(Date date) throws ParseException {
SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
String clidate = myFormatter.format(date);
Calendar cToday = Calendar.getInstance(); // 存今天
Calendar cBirth = Calendar.getInstance(); // 存生日
cBirth.setTime(myFormatter.parse(clidate)); // 设置生日
cBirth.set(Calendar.YEAR, cToday.get(Calendar.YEAR)); // 修改为本年
int days;
if (cBirth.get(Calendar.DAY_OF_YEAR) < cToday.get(Calendar.DAY_OF_YEAR)) {
return -1;
// 生日已经过了,要算明年的了
// days = cToday.getActualMaximum(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);
//days += cBirth.get(Calendar.DAY_OF_YEAR);
} else {
// 生日还没过
days = cBirth.get(Calendar.DAY_OF_YEAR) - cToday.get(Calendar.DAY_OF_YEAR);
}
// 返回结果
if (days == 0) {
System.out.println("今天生日");
} else {
System.out.println("距离生日还有:" + days + "天");
}
return days;
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化