排班日历自动生成

This commit is contained in:
JinLu.Yin 2022-06-12 22:49:59 +08:00
parent c521e5311c
commit 76a00d72ee
14 changed files with 773 additions and 4 deletions

View File

@ -3,12 +3,14 @@ package com.ktg;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.scheduling.annotation.EnableAsync;
/** /**
* 启动程序 * 启动程序
* *
* @author ruoyi * @author ruoyi
*/ */
@EnableAsync
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class RuoYiApplication public class RuoYiApplication
{ {

View File

@ -140,6 +140,10 @@ public class UserConstants
public static final String CAL_SHIFT_NAME_DAY="白班"; public static final String CAL_SHIFT_NAME_DAY="白班";
public static final String CAL_SHIFT_NAME_NIGHT="夜班"; public static final String CAL_SHIFT_NAME_NIGHT="夜班";
public static final String CAL_SHIFT_NAME_MID="中班"; public static final String CAL_SHIFT_NAME_MID="中班";
public static final String CAL_SHIFT_METHOD_QUARTER="QUARTER";
public static final String CAL_SHIFT_METHOD_MONTH="MONTH";
public static final String CAL_SHIFT_METHOD_WEEK="WEEK";
public static final String CAL_SHIFT_METHOD_DAY="DAY";
/** /**
* 排班日历的查询方式 * 排班日历的查询方式

View File

@ -1,10 +1,14 @@
package com.ktg.mes.cal.controller; package com.ktg.mes.cal.controller;
import java.util.List; import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.ktg.common.constant.UserConstants;
import com.ktg.mes.cal.service.ICalPlanTeamService; import com.ktg.mes.cal.service.ICalPlanTeamService;
import com.ktg.mes.cal.service.ICalShiftService; import com.ktg.mes.cal.service.ICalShiftService;
import com.ktg.mes.cal.service.ICalTeamshiftService;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -44,6 +48,9 @@ public class CalPlanController extends BaseController
@Autowired @Autowired
private ICalPlanTeamService calPlanTeamService; private ICalPlanTeamService calPlanTeamService;
@Autowired
private ICalTeamshiftService calTeamshiftService;
/** /**
* 查询排班计划列表 * 查询排班计划列表
*/ */
@ -94,14 +101,20 @@ public class CalPlanController extends BaseController
return toAjax(ret); return toAjax(ret);
} }
/** /**
* 修改排班计划 * 修改排班计划
*/ */
@PreAuthorize("@ss.hasPermi('mes:cal:calplan:edit')") @PreAuthorize("@ss.hasPermi('mes:cal:calplan:edit')")
@Log(title = "排班计划", businessType = BusinessType.UPDATE) @Log(title = "排班计划", businessType = BusinessType.UPDATE)
@Transactional
@PutMapping @PutMapping
public AjaxResult edit(@RequestBody CalPlan calPlan) public AjaxResult edit(@RequestBody CalPlan calPlan)
{ {
if(UserConstants.ORDER_STATUS_CONFIRMED.equals(calPlan.getStatus())){
calTeamshiftService.genRecords(calPlan.getPlanId());
}
return toAjax(calPlanService.updateCalPlan(calPlan)); return toAjax(calPlanService.updateCalPlan(calPlan));
} }

View File

@ -30,6 +30,7 @@ public interface CalPlanMapper
/** /**
* 新增排班计划 * 新增排班计划
* *

View File

@ -28,6 +28,8 @@ public interface CalShiftMapper
public List<CalShift> selectCalShiftList(CalShift calShift); public List<CalShift> selectCalShiftList(CalShift calShift);
public int checkShiftCount(Long planId); public int checkShiftCount(Long planId);
/** /**

View File

@ -27,6 +27,8 @@ public interface CalTeamshiftMapper
*/ */
public List<CalTeamshift> selectCalTeamshiftList(CalTeamshift calTeamshift); public List<CalTeamshift> selectCalTeamshiftList(CalTeamshift calTeamshift);
/** /**
* 新增班组排班 * 新增班组排班
* *

View File

@ -27,6 +27,9 @@ public interface ICalPlanTeamService
*/ */
public List<CalPlanTeam> selectCalPlanTeamList(CalPlanTeam calPlanTeam); public List<CalPlanTeam> selectCalPlanTeamList(CalPlanTeam calPlanTeam);
public List<CalPlanTeam> selectCalPlanTeamListByPlanId(Long plandId);
public String checkPlanTeamUnique(CalPlanTeam calPlanTeam); public String checkPlanTeamUnique(CalPlanTeam calPlanTeam);
/** /**

View File

@ -27,6 +27,14 @@ public interface ICalShiftService
*/ */
public List<CalShift> selectCalShiftList(CalShift calShift); public List<CalShift> selectCalShiftList(CalShift calShift);
/**
* 根据计划ID查询班次
* @param planId
* @return
*/
public List<CalShift> selectCalShiftListByPlanId(Long planId);
public int checkShiftCount(Long planId); public int checkShiftCount(Long planId);

View File

@ -58,4 +58,9 @@ public interface ICalTeamshiftService
* @return 结果 * @return 结果
*/ */
public int deleteCalTeamshiftByRecordId(Long recordId); public int deleteCalTeamshiftByRecordId(Long recordId);
public void genRecords(Long plandId);
} }

View File

@ -47,6 +47,13 @@ public class CalPlanTeamServiceImpl implements ICalPlanTeamService
return calPlanTeamMapper.selectCalPlanTeamList(calPlanTeam); return calPlanTeamMapper.selectCalPlanTeamList(calPlanTeam);
} }
@Override
public List<CalPlanTeam> selectCalPlanTeamListByPlanId(Long plandId) {
CalPlanTeam param = new CalPlanTeam();
param.setPlanId(plandId);
return calPlanTeamMapper.selectCalPlanTeamList(param);
}
@Override @Override
public String checkPlanTeamUnique(CalPlanTeam calPlanTeam) { public String checkPlanTeamUnique(CalPlanTeam calPlanTeam) {
CalPlanTeam team = calPlanTeamMapper.checkPlanTeamUnique(calPlanTeam); CalPlanTeam team = calPlanTeamMapper.checkPlanTeamUnique(calPlanTeam);

View File

@ -46,6 +46,13 @@ public class CalShiftServiceImpl implements ICalShiftService
return calShiftMapper.selectCalShiftList(calShift); return calShiftMapper.selectCalShiftList(calShift);
} }
@Override
public List<CalShift> selectCalShiftListByPlanId(Long planId) {
CalShift param = new CalShift();
param.setPlanId(planId);
return calShiftMapper.selectCalShiftList(param);
}
@Override @Override
public int checkShiftCount(Long planId) { public int checkShiftCount(Long planId) {
return calShiftMapper.checkShiftCount(planId); return calShiftMapper.checkShiftCount(planId);

View File

@ -1,8 +1,20 @@
package com.ktg.mes.cal.service.impl; package com.ktg.mes.cal.service.impl;
import java.util.Date;
import java.util.List; import java.util.List;
import cn.hutool.core.collection.CollUtil;
import com.ktg.common.constant.UserConstants;
import com.ktg.common.utils.DateUtils; import com.ktg.common.utils.DateUtils;
import com.ktg.mes.cal.domain.CalPlan;
import com.ktg.mes.cal.domain.CalPlanTeam;
import com.ktg.mes.cal.domain.CalShift;
import com.ktg.mes.cal.mapper.CalPlanMapper;
import com.ktg.mes.cal.mapper.CalPlanTeamMapper;
import com.ktg.mes.cal.mapper.CalShiftMapper;
import com.ktg.mes.cal.utils.CalendarUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.ktg.mes.cal.mapper.CalTeamshiftMapper; import com.ktg.mes.cal.mapper.CalTeamshiftMapper;
import com.ktg.mes.cal.domain.CalTeamshift; import com.ktg.mes.cal.domain.CalTeamshift;
@ -20,6 +32,15 @@ public class CalTeamshiftServiceImpl implements ICalTeamshiftService
@Autowired @Autowired
private CalTeamshiftMapper calTeamshiftMapper; private CalTeamshiftMapper calTeamshiftMapper;
@Autowired
private CalPlanMapper calPlanMapper;
@Autowired
private CalShiftMapper calShiftMapper;
@Autowired
private CalPlanTeamMapper calPlanTeamMapper;
/** /**
* 查询班组排班 * 查询班组排班
* *
@ -93,4 +114,263 @@ public class CalTeamshiftServiceImpl implements ICalTeamshiftService
{ {
return calTeamshiftMapper.deleteCalTeamshiftByRecordId(recordId); return calTeamshiftMapper.deleteCalTeamshiftByRecordId(recordId);
} }
/**
* 根据排班计划生成每个班组的明细排班记录
* 1.查询计划头
* 2.查询计划中的班组
* 3.查询计划中的班次
* 4.计算计划的开始日期和结束日期的差值
* 5.遍历每一天设置每一天的班组与班次的对应关系要结合轮班方式
* @param plandId
*/
@Async
@Override
public void genRecords(Long plandId) {
CalPlan plan =calPlanMapper.selectCalPlanByPlanId(plandId);
CalShift p1 = new CalShift();
p1.setPlanId(plandId);
List<CalShift> shifts = calShiftMapper.selectCalShiftList(p1);
CalPlanTeam p2 = new CalPlanTeam();
p2.setPlanId(plandId);
List<CalPlanTeam> teams = calPlanTeamMapper.selectCalPlanTeamList(p2);
Long days = CalendarUtil.getDateDiff(plan.getStartDate(),plan.getEndDate());
int shiftIndex =0;
Date nowDate =null;
for(int i =0;i<days;i++){
//当前遍历到的日期
nowDate = CalendarUtil.addDays(plan.getStartDate(),i);
//按季度轮班
if(UserConstants.CAL_SHIFT_METHOD_QUARTER.equals(plan.getShiftMethod())){
//获取季度第一天
String quarterStart = CalendarUtil.getQuarterStartStr(nowDate);
//如果到了季度第一天并且不是排班计划的开始季度
if(quarterStart.equals(CalendarUtil.getDateStr(nowDate)) && !quarterStart.equals(CalendarUtil.getQuarterStartStr(plan.getStartDate()))){
shiftIndex ++; //轮班一次
}
}
//按月轮班
else if(UserConstants.CAL_SHIFT_METHOD_MONTH.equals(plan.getShiftMethod())){
//获取月份第一天
String monthStart = CalendarUtil.getMonthStartStr(nowDate);
//如果到了月初并且不是排班计划开始月份
if(monthStart.equals(CalendarUtil.getDateStr(nowDate)) && !monthStart.equals(CalendarUtil.getMonthStartStr(plan.getStartDate()))){
shiftIndex ++; //轮班一次
}
}
//按周轮班
else if(UserConstants.CAL_SHIFT_METHOD_WEEK.equals(plan.getShiftMethod())){
//获取周的第一天
String weekStart = CalendarUtil.getWeekStartTimeStr(nowDate);
//如果是每周一并且不是排班计划开始周
if(weekStart.equals(CalendarUtil.getDateStr(nowDate)) && !weekStart.equals(CalendarUtil.getWeekStartTimeStr(plan.getStartDate()))){
shiftIndex ++;
}
}
//如果是单白班不需要倒班
if(UserConstants.CAL_SHIFT_TYPE_SINGLE.equals(plan.getShiftType())){
CalTeamshift bean = new CalTeamshift();
bean.setPlanId(plandId);
bean.setCalendarType(plan.getCalendarType());
bean.setShiftType(plan.getShiftType());
bean.setTheDay(CalendarUtil.getDateStr(nowDate));
bean.setTeamId(teams.get(0).getTeamId());
bean.setTeamName(teams.get(0).getTeamName());
bean.setShiftId(shifts.get(0).getShiftId());
bean.setShiftName(shifts.get(0).getShiftName());
saveTeamShift(bean);
}
//如果是两班倒
if(UserConstants.CAL_SHIFT_TYPE_TWO.equals(plan.getShiftType())){
if(shiftIndex%2 == 0){
//A组上白班
CalTeamshift beanA = new CalTeamshift();
beanA.setPlanId(plandId);
beanA.setCalendarType(plan.getCalendarType());
beanA.setShiftType(plan.getShiftType());
beanA.setTheDay(CalendarUtil.getDateStr(nowDate));
beanA.setTeamId(teams.get(0).getTeamId());
beanA.setTeamName(teams.get(0).getTeamName());
beanA.setShiftId(shifts.get(0).getShiftId());
beanA.setShiftName(shifts.get(0).getShiftName());
beanA.setOrderNum(1L);
saveTeamShift(beanA);
//B组上夜班
CalTeamshift beanB = new CalTeamshift();
beanB.setPlanId(plandId);
beanB.setCalendarType(plan.getCalendarType());
beanB.setShiftType(plan.getShiftType());
beanB.setTheDay(CalendarUtil.getDateStr(nowDate));
beanB.setTeamId(teams.get(1).getTeamId());
beanB.setTeamName(teams.get(1).getTeamName());
beanB.setShiftId(shifts.get(1).getShiftId());
beanB.setShiftName(shifts.get(1).getShiftName());
beanB.setOrderNum(2L);
saveTeamShift(beanB);
}else{
//A组上夜班
CalTeamshift beanA = new CalTeamshift();
beanA.setPlanId(plandId);
beanA.setCalendarType(plan.getCalendarType());
beanA.setShiftType(plan.getShiftType());
beanA.setTheDay(CalendarUtil.getDateStr(nowDate));
beanA.setTeamId(teams.get(0).getTeamId());
beanA.setTeamName(teams.get(0).getTeamName());
beanA.setShiftId(shifts.get(1).getShiftId());
beanA.setShiftName(shifts.get(1).getShiftName());
beanA.setOrderNum(1L);
saveTeamShift(beanA);
//B组上白班
CalTeamshift beanB = new CalTeamshift();
beanB.setPlanId(plandId);
beanB.setCalendarType(plan.getCalendarType());
beanB.setShiftType(plan.getShiftType());
beanB.setTheDay(CalendarUtil.getDateStr(nowDate));
beanB.setTeamId(teams.get(1).getTeamId());
beanB.setTeamName(teams.get(1).getTeamName());
beanB.setShiftId(shifts.get(0).getShiftId());
beanB.setShiftName(shifts.get(0).getShiftName());
beanB.setOrderNum(2L);
saveTeamShift(beanB);
}
}
//如果是三班倒
if(UserConstants.CAL_SHIFT_TYPE_THREE.equals(plan.getShiftType())){
if(shiftIndex%2 == 0){
//A组上白班
CalTeamshift beanA = new CalTeamshift();
beanA.setPlanId(plandId);
beanA.setCalendarType(plan.getCalendarType());
beanA.setShiftType(plan.getShiftType());
beanA.setTheDay(CalendarUtil.getDateStr(nowDate));
beanA.setTeamId(teams.get(0).getTeamId());
beanA.setTeamName(teams.get(0).getTeamName());
beanA.setShiftId(shifts.get(0).getShiftId());
beanA.setShiftName(shifts.get(0).getShiftName());
beanA.setOrderNum(1L);
saveTeamShift(beanA);
//B组上中班
CalTeamshift beanB = new CalTeamshift();
beanB.setPlanId(plandId);
beanB.setCalendarType(plan.getCalendarType());
beanB.setShiftType(plan.getShiftType());
beanB.setTheDay(CalendarUtil.getDateStr(nowDate));
beanB.setTeamId(teams.get(1).getTeamId());
beanB.setTeamName(teams.get(1).getTeamName());
beanB.setShiftId(shifts.get(1).getShiftId());
beanB.setShiftName(shifts.get(1).getShiftName());
beanB.setOrderNum(2L);
saveTeamShift(beanB);
//C组上夜班
CalTeamshift beanC = new CalTeamshift();
beanC.setPlanId(plandId);
beanC.setCalendarType(plan.getCalendarType());
beanC.setShiftType(plan.getShiftType());
beanC.setTheDay(CalendarUtil.getDateStr(nowDate));
beanC.setTeamId(teams.get(2).getTeamId());
beanC.setTeamName(teams.get(2).getTeamName());
beanC.setShiftId(shifts.get(2).getShiftId());
beanC.setShiftName(shifts.get(2).getShiftName());
beanC.setOrderNum(2L);
saveTeamShift(beanC);
}else if(shiftIndex%2 == 1){
//A组上中班
CalTeamshift beanA = new CalTeamshift();
beanA.setPlanId(plandId);
beanA.setCalendarType(plan.getCalendarType());
beanA.setShiftType(plan.getShiftType());
beanA.setTheDay(CalendarUtil.getDateStr(nowDate));
beanA.setTeamId(teams.get(0).getTeamId());
beanA.setTeamName(teams.get(0).getTeamName());
beanA.setShiftId(shifts.get(1).getShiftId());
beanA.setShiftName(shifts.get(1).getShiftName());
beanA.setOrderNum(1L);
saveTeamShift(beanA);
//B组上夜班
CalTeamshift beanB = new CalTeamshift();
beanB.setPlanId(plandId);
beanB.setCalendarType(plan.getCalendarType());
beanB.setShiftType(plan.getShiftType());
beanB.setTheDay(CalendarUtil.getDateStr(nowDate));
beanB.setTeamId(teams.get(1).getTeamId());
beanB.setTeamName(teams.get(1).getTeamName());
beanB.setShiftId(shifts.get(2).getShiftId());
beanB.setShiftName(shifts.get(2).getShiftName());
beanB.setOrderNum(2L);
saveTeamShift(beanB);
//C组上白班
CalTeamshift beanC = new CalTeamshift();
beanC.setPlanId(plandId);
beanC.setCalendarType(plan.getCalendarType());
beanC.setShiftType(plan.getShiftType());
beanC.setTheDay(CalendarUtil.getDateStr(nowDate));
beanC.setTeamId(teams.get(2).getTeamId());
beanC.setTeamName(teams.get(2).getTeamName());
beanC.setShiftId(shifts.get(0).getShiftId());
beanC.setShiftName(shifts.get(0).getShiftName());
beanC.setOrderNum(2L);
saveTeamShift(beanC);
}else{
//A组上夜班
CalTeamshift beanA = new CalTeamshift();
beanA.setPlanId(plandId);
beanA.setCalendarType(plan.getCalendarType());
beanA.setShiftType(plan.getShiftType());
beanA.setTheDay(CalendarUtil.getDateStr(nowDate));
beanA.setTeamId(teams.get(0).getTeamId());
beanA.setTeamName(teams.get(0).getTeamName());
beanA.setShiftId(shifts.get(2).getShiftId());
beanA.setShiftName(shifts.get(2).getShiftName());
beanA.setOrderNum(1L);
saveTeamShift(beanA);
//B组上白班
CalTeamshift beanB = new CalTeamshift();
beanB.setPlanId(plandId);
beanB.setCalendarType(plan.getCalendarType());
beanB.setShiftType(plan.getShiftType());
beanB.setTheDay(CalendarUtil.getDateStr(nowDate));
beanB.setTeamId(teams.get(1).getTeamId());
beanB.setTeamName(teams.get(1).getTeamName());
beanB.setShiftId(shifts.get(0).getShiftId());
beanB.setShiftName(shifts.get(0).getShiftName());
beanB.setOrderNum(2L);
saveTeamShift(beanB);
//C组上中班
CalTeamshift beanC = new CalTeamshift();
beanC.setPlanId(plandId);
beanC.setCalendarType(plan.getCalendarType());
beanC.setShiftType(plan.getShiftType());
beanC.setTheDay(CalendarUtil.getDateStr(nowDate));
beanC.setTeamId(teams.get(2).getTeamId());
beanC.setTeamName(teams.get(2).getTeamName());
beanC.setShiftId(shifts.get(1).getShiftId());
beanC.setShiftName(shifts.get(1).getShiftName());
beanC.setOrderNum(2L);
saveTeamShift(beanC);
}
}
}
}
private void saveTeamShift(CalTeamshift calTeamshift){
CalTeamshift param = new CalTeamshift();
param.setTheDay(calTeamshift.getTheDay());
param.setTeamId(calTeamshift.getTeamId());
List<CalTeamshift> teamshifts = calTeamshiftMapper.selectCalTeamshiftList(param);
if(CollUtil.isNotEmpty(teamshifts)){
calTeamshift.setRecordId(teamshifts.get(0).getRecordId());
calTeamshiftMapper.updateCalTeamshift(calTeamshift);
}else{
calTeamshiftMapper.insertCalTeamshift(calTeamshift);
}
}
} }

View File

@ -4,6 +4,8 @@ import com.ktg.mes.cal.domain.CalCalendar;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.util.*; import java.util.*;
@ -12,8 +14,335 @@ public class CalendarUtil {
private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
/**
* 星期日;
*/
public static final String SUNDAY = "星期日";
private static Date getMonthStart(Date date) { /**
* 星期一;
*/
public static final String MONDAY = "星期一";
/**
* 星期二;
*/
public static final String TUESDAY = "星期二";
/**
* 星期三;
*/
public static final String WEDNESDAY = "星期三";
/**
* 星期四;
*/
public static final String THURSDAY = "星期四";
/**
* 星期五;
*/
public static final String FRIDAY = "星期五";
/**
* 星期六;
*/
public static final String SATURDAY = "星期六";
/**
* 显示年月日时分秒例如 2015-08-11 09:51:53.
*/
public static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 显示年月日时分例如 2015-08-11 09:51.
*/
public static final String NO_SECOND_DATETIME_PATTERN = "yyyy-MM-dd HH:mm";
/**
* 仅显示年月日例如 2015-08-11.
*/
public static final String DATE_PATTERN = "yyyy-MM-dd";
/**
* 仅显示时分秒例如 09:51:53.
*/
public static final String TIME_PATTERN = "HH:mm:ss";
/**
* 显示年月日时分秒(/分割)例如 2015/08/11 09:51:53.
*/
public static final String DATETIME_PATTERN_WITH_SLASH = "yyyy/MM/dd HH:mm:ss";
/**
* 显示年月日(/分割)例如 2015/08/11.
*/
public static final String DATE_PATTERN_WITH_SLASH = "yyyy/MM/dd";
/**
* 仅显示年(无符号)例如 2015.
*/
private static final String YEAR_PATTERN = "yyyy";
/**
* 仅显示年月例如 2015-08.
*/
private static final String MONTH_PATTERN = "yyyy-MM";
/**
* 获取当前日期和时间字符串.
*
* @return String 日期时间字符串例如 2015-08-11 09:51:53
*/
public static String getDateTimeStr() {
return format(new Date(), DATETIME_PATTERN);
}
/**
* 时间戳转换为日期时间字符串
*
* @param timestamp 时间戳
* @param pattern 日期格式 例如DATETIME_PATTERN
* @return String 日期时间字符串例如 2015-08-11 09:51:53
*/
public static String getDateTimeStr(long timestamp, String pattern) {
return new SimpleDateFormat(pattern).format(timestamp);
}
/**
* 获取当前日期字符串.
*
* @return String 日期字符串例如2015-08-11
*/
public static String getDateStr() {
return format(new Date(), DATE_PATTERN);
}
/**
* 获取指定日期字符串.
*
* @return String 日期字符串例如2015-08-11
*/
public static String getDateStr(Date theday) {
return format(theday, DATE_PATTERN);
}
/**
* 获取当前时间字符串.
*
* @return String 时间字符串例如 09:51:53
*/
public static String getTimeStr() {
return format(new Date(), TIME_PATTERN);
}
/**
* 获取当前年份字符串.
*
* @return String 当前年份字符串例如 2015
*/
public static String getYearStr() {
return format(new Date(), YEAR_PATTERN);
}
/**
* 获取当前月份字符串.
*
* @return String 当前月份字符串例如 08
*/
public static String getMonthStr() {
return format(new Date(), "MM");
}
/**
* 获取当前天数字符串.
*
* @return String 当前天数字符串例如 11
*/
public static String getDayStr() {
return format(new Date(), "dd");
}
/**
* 获取当前星期字符串.
*
* @return String 当前星期字符串例如 星期二
*/
public static String getDayOfWeekStr() {
return format(new Date(), "E");
}
/**
* 获取指定日期是星期几
*
* @param date 日期
* @return String 星期几
*/
public static String getDayOfWeekStr(Date date) {
String[] weekOfDays = {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY};
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int num = calendar.get(Calendar.DAY_OF_WEEK) - 1;
return weekOfDays[num];
}
/**
* 获取日期时间字符串
*
* @param date 需要转化的日期时间
* @param pattern 时间格式
* @return String 日期时间字符串例如 2015-08-11 09:51:53
*/
public static String format(Date date, String pattern) {
return new SimpleDateFormat(pattern).format(date);
}
/**
* 获取指定日期num年数之后的日期.
*
* @param num 间隔年数(负数表示之前)
* @return Date 日期
*/
public static Date addYears(Date date, int num) {
return add(date, num, Calendar.YEAR);
}
/**
* 获取指定日期num月数之后的日期.
*
* @param num 间隔月数(负数表示之前)
* @return Date 日期
*/
public static Date addMonths(Date date, int num) {
return add(date, num, Calendar.MONTH);
}
/**
* 获取指定日期num周数之后的日期.
*
* @param date 日期
* @param num 周数(负数表示之前)
* @return Date 新的日期
*/
public static Date addWeeks(Date date, int num) {
return add(date, num, Calendar.WEEK_OF_YEAR);
}
/**
* 获取指定日期num天数之后的日期.
*
* @param date 日期
* @param num 天数(负数表示之前)
* @return Date 新的日期
*/
public static Date addDays(Date date, int num) {
return add(date, num, Calendar.DAY_OF_MONTH);
}
/**
* 获取指定日期num小时之后的日期.
*
* @param date 日期
* @param num 小时数(负数表示之前)
* @return Date 新的日期
*/
public static Date addHours(Date date, int num) {
return add(date, num, Calendar.HOUR_OF_DAY);
}
/**
* 获取指定日期num分钟之后的日期.
*
* @param date 日期
* @param num 分钟数(负数表示之前)
* @return Date 新的日期
*/
public static Date addMinutes(Date date, int num) {
return add(date, num, Calendar.MINUTE);
}
/**
* 获取指定日期num秒钟之后的日期.
*
* @param date 日期
* @param num 秒钟数(负数表示之前)
* @return Date 新的日期
*/
public static Date addSeconds(Date date, int num) {
return add(date, num, Calendar.SECOND);
}
/**
* 获取当前日期指定数量日期时间单位之后的日期.
*
* @param date 日期
* @param num 数量
* @param unit 日期时间单位
* @return Date 新的日期
*/
public static Date add(Date date, int num, int unit) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(unit, num);
return calendar.getTime();
}
/**
* 获取本周的第一天一个星期的第一天是星期一最后一天是星期天
*
* @return Calendar 日历
*/
public static Date getStartDayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 设置一个星期的第一天按中国的习惯一个星期的第一天是星期一
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
return calendar.getTime();
}
/**
* 获取本周的最后一天一个星期的第一天是星期一最后一天是星期天
*
* @return Calendar 日历
*/
public static Date getEndDayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
// 设置一个星期的第一天按中国的习惯一个星期的第一天是星期一
calendar.setFirstDayOfWeek(Calendar.MONDAY);
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
return calendar.getTime();
}
/**
* 获取指定日期当周第一天的日期字符串
*
* @param date 指定日期
* @return String 格式yyyy-MM-dd
*/
public static String getWeekStartTimeStr(Date date) {
return getDateStr(getStartDayOfWeek(date));
}
/**
* 获取本周最后一天的日期字符串
*
* @return String 格式yyyy-MM-dd
*/
public static String getWeekEndTimeStr() {
return getDateStr(getEndDayOfWeek(new Date()));
}
/**
* 获取指定日期所在月份的开始日期
* @param date
* @return
*/
public static Date getMonthStart(Date date) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTime(date); calendar.setTime(date);
int index = calendar.get(Calendar.DAY_OF_MONTH); int index = calendar.get(Calendar.DAY_OF_MONTH);
@ -21,7 +350,12 @@ public class CalendarUtil {
return calendar.getTime(); return calendar.getTime();
} }
private static Date getMonthEnd(Date date) { /**
* 获取指定日期所在月份的最后一天
* @param date
* @return
*/
public static Date getMonthEnd(Date date) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTime(date); calendar.setTime(date);
calendar.add(Calendar.MONTH, 1); calendar.add(Calendar.MONTH, 1);
@ -30,6 +364,74 @@ public class CalendarUtil {
return calendar.getTime(); return calendar.getTime();
} }
/**
* 获取指定日期对应月份第一天
* @param date
* @return
*/
public static String getMonthStartStr(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int index = calendar.get(Calendar.DAY_OF_MONTH);
calendar.add(Calendar.DATE, (1 - index));
return sdf.format(calendar.getTime());
}
/**
* 获取指定日期对应月份最后一天
* @param date
* @return
*/
public static String getMonthEndStr(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, 1);
int index = calendar.get(Calendar.DAY_OF_MONTH);
calendar.add(Calendar.DATE, (-index));
return sdf.format(calendar.getTime());
}
/**
* 获取指定日期对应季度的第一天
* @param theDay
* @return
*/
public static String getQuarterStartStr(Date theDay){
LocalDate resDate = null;
if (theDay == null) {
resDate = LocalDate.now();
}else{
resDate = LocalDate.parse(sdf.format(theDay),formatter);
}
Month month = resDate.getMonth();
Month firstMonthOfQuarter = month.firstMonthOfQuarter();
resDate = LocalDate.of(resDate.getYear(), firstMonthOfQuarter, 1);
return resDate.toString();
}
/**
* 获取指定日期对应季度的最后一天
* @param theDay
* @return
*/
public static String getQuarterEandStr(Date theDay){
LocalDate resDate = LocalDate.now();
if (theDay == null) {
resDate = LocalDate.now();
}else {
resDate = LocalDate.parse(sdf.format(theDay),formatter);
}
Month month = resDate.getMonth();
Month firstMonthOfQuarter = month.firstMonthOfQuarter();
Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
resDate = LocalDate.of(resDate.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(resDate.isLeapYear()));
return resDate.toString();
}
private static Date getNext(Date date) { private static Date getNext(Date date) {
Calendar calendar = Calendar.getInstance(); Calendar calendar = Calendar.getInstance();
calendar.setTime(date); calendar.setTime(date);
@ -40,7 +442,7 @@ public class CalendarUtil {
public static Long getDateDiff(Date start,Date end){ public static Long getDateDiff(Date start,Date end){
LocalDate sdate = LocalDate.parse(sdf.format(start),formatter); LocalDate sdate = LocalDate.parse(sdf.format(start),formatter);
LocalDate edate = LocalDate.parse(sdf.format(end),formatter); LocalDate edate = LocalDate.parse(sdf.format(end),formatter);
return edate.until(sdate,ChronoUnit.DAYS); return sdate.until(edate,ChronoUnit.DAYS);
} }
public static Long getDateDiff(String start,Date end){ public static Long getDateDiff(String start,Date end){
@ -67,6 +469,13 @@ public class CalendarUtil {
return edate.until(sdate,ChronoUnit.DAYS); return edate.until(sdate,ChronoUnit.DAYS);
} }
public static Date getDatePlus(Date theDay,Integer count){
LocalDate sdate = LocalDate.parse(sdf.format(theDay),formatter);
sdate.plusDays(count);
return Date.from(sdate.atStartOfDay(ZoneId.systemDefault()).toInstant());
}
/** /**
* 获取指定月份的所有日期 * 获取指定月份的所有日期
* @param d * @param d
@ -84,4 +493,30 @@ public class CalendarUtil {
} }
return lst; return lst;
} }
/**
* 获取指定日期前后N天的所有日期
* @param date
* @param num
* @param pattern
* @return
*/
public static List<String> getDateStrList(Date date, int num, String pattern) {
List<String> result = new ArrayList<>();
Calendar c = Calendar.getInstance();
c.setTime(date);
boolean flag = false;
if (num < 0) {
num = Math.abs(num);
flag = true;
}
for (int i = 0; i < num; i++) {
result.add(new SimpleDateFormat(pattern).format(c.getTimeInMillis()));
c.add(Calendar.DATE, flag ? -1 : 1);
}
if (flag) {
Collections.reverse(result);
}
return result;
}
} }

View File

@ -41,7 +41,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="shiftType != null and shiftType != ''"> and shift_type = #{shiftType}</if> <if test="shiftType != null and shiftType != ''"> and shift_type = #{shiftType}</if>
<if test="shiftMethod != null and shiftMethod != ''"> and shift_method = #{shiftMethod}</if> <if test="shiftMethod != null and shiftMethod != ''"> and shift_method = #{shiftMethod}</if>
<if test="shiftCount != null "> and shift_count = #{shiftCount}</if> <if test="shiftCount != null "> and shift_count = #{shiftCount}</if>
<if test="status != and status !='' "> and status = #{status}</if> <if test="status !=null and status !='' "> and status = #{status}</if>
</where> </where>
</select> </select>