master
Joe 2 years ago
parent 43a1916b83
commit e03a90f456

Binary file not shown.

@ -120,6 +120,12 @@ public class EmployeeController {
return ajaxResult;
}
@PostMapping("/syncAge")
@ApiOperation(value = "更新年龄", notes = "更新年龄")
public void syncAge() {
employeeService.syncAge();
}
/**
*
* @param file

@ -0,0 +1,23 @@
package com.jingcheng.cms.handler;
import com.jingcheng.cms.service.EmployeeService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@AllArgsConstructor
public class EmployeeJobHandler {
private final EmployeeService employeeService;
@Scheduled(cron = "0 0 1 * * ?")
public void syncAge() {
employeeService.syncAge();
}
}

@ -0,0 +1,20 @@
package com.jingcheng.cms.listener;
import com.jingcheng.cms.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class StartListener implements ApplicationRunner {
@Autowired
private EmployeeService employeeService;
@Override
public void run(ApplicationArguments args) throws Exception {
//todo 执行一次
employeeService.syncAge();
}
}

@ -34,4 +34,6 @@ public interface EmployeeService {
AjaxResult dataStatistics(JSONObject jsonObject);
AjaxResult getPostListByDept(JSONObject jsonObject);
void syncAge();
}

@ -15,6 +15,7 @@ import com.jingcheng.cms.model.*;
import com.jingcheng.cms.service.EmployeeService;
import com.jingcheng.cms.service.InfoSettingValueService;
import com.jingcheng.cms.beans.AjaxResult;
import com.jingcheng.cms.util.DateUtils;
import com.jingcheng.cms.util.ExcelUtils;
import com.jingcheng.cms.util.PageUtils;
import com.jingcheng.cms.vo.DataAnalysisVO;
@ -30,10 +31,8 @@ import tk.mybatis.mapper.entity.Example;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;
@Service
@ -463,4 +462,22 @@ public class EmployeeServiceImpl implements EmployeeService {
return ajaxResult;
}
@Override
public void syncAge() {
List<Employee> employeeList = employeeMapper.selectAll();
for (Employee employee : employeeList) {
String birthday = employee.getBirthday();
if (StringUtils.isEmpty(birthday)) continue;
Date birth = null;
try {
birth = DateUtils.parse(birthday);
} catch (ParseException e) {
e.printStackTrace();
}
Integer age = DateUtils.getAge(birth);
employee.setAge(age);
employeeMapper.updateByPrimaryKey(employee);
}
}
}

@ -1,6 +1,7 @@
package com.jingcheng.cms.util;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
@ -100,13 +101,37 @@ public class DateUtils {
return Calendar.getInstance().getTime();
}
public static void main(String[] args) {
public static Date parse(String strDate) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
return sdf.parse(strDate);
}
public static int getAge(Date birthDay) {
Calendar cal = Calendar.getInstance();
if (birthDay == null || cal.before(birthDay)) { //出生日期晚于当前时间,无法计算
return 0;
}
int yearNow = cal.get(Calendar.YEAR); //当前年份
int monthNow = cal.get(Calendar.MONTH); //当前月份
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
cal.setTime(birthDay);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
int age = yearNow - yearBirth; //计算整岁数
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
if (dayOfMonthNow < dayOfMonthBirth) age--;//当前日期在生日之前,年龄减一
}else{
age--;//当前月份在生日之前,年龄减一
}
}
return age;
}
public static void main(String[] args) throws ParseException {
System.out.println(DateUtils.startTime());
System.out.println(DateUtils.startTimeStamp());
System.out.println(DateUtils.endTimeStamp());
System.out.println(DateUtils.endTime());
System.out.println(DateUtils.getAge(DateUtils.parse("1996-08-28")));
}
}

Loading…
Cancel
Save