代码拉取完成,页面将自动刷新
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;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。