Commit fe2de281 authored by 李驰骋's avatar 李驰骋

排班日历BUG修复测试用例添加

parent 7935a565
......@@ -117,6 +117,9 @@ public class CalShift extends BaseEntity {
*/
private Long attr4;
/**
* 1:休息, 0:工作
*/
private Integer operType;
private List<CalShift> breakTimeList = new ArrayList<>();
......
package com.ximai.mes.cal.service;
import com.ximai.mes.cal.domain.CalPlan;
import com.ximai.mes.cal.domain.CalPlanWorkunit;
import com.ximai.mes.cal.domain.CalWorkunit;
import org.springframework.scheduling.annotation.Async;
import java.util.List;
......@@ -60,7 +62,7 @@ public interface ICalPlanService {
int deleteCalPlanByPlanId(Long planId);
@Async
void genRecords(CalPlan calPlan);
List<CalPlanWorkunit> genRecords(CalPlan calPlan, List<CalWorkunit> calWorkunits);
void releasePlan(CalPlan calPlan);
......
......@@ -150,7 +150,10 @@ public class CalPlanServiceImpl implements ICalPlanService {
public void releasePlan(CalPlan calPlan) {
calPlanMapper.updateCalPlan(calPlan);
this.updateRelationship(calPlan);
this.genRecords(calPlan);
List<CalWorkunit> calWorkunits = calWorkunitMapper.selectListByQw(new QueryWrapper<CalWorkunit>().eq("plan_id", calPlan.getPlanId()));
ExceptionUtil.checkTrueThrowException(CollectionUtil.isEmpty(calWorkunits), MessageUtils.message("cal.error.error19"));
List<CalPlanWorkunit> calPlanWorkunits = this.genRecords(calPlan, calWorkunits);
this.saveCalPlanWorkunit(calPlan, calPlanWorkunits, calWorkunits);
}
/**
......@@ -190,6 +193,7 @@ public class CalPlanServiceImpl implements ICalPlanService {
List<CalPlanWorkunit> saveList = new ArrayList<>();
for (String nowDate : list) {
for (CalWorkunit calWorkunit : calWorkunits) {
int diffDay = 0;
for (CalShift shift : shifts) {
CalPlanWorkunit calPlanWorkunit = new CalPlanWorkunit();
calPlanWorkunit.setPlanId(plan.getPlanId());
......@@ -202,13 +206,12 @@ public class CalPlanServiceImpl implements ICalPlanService {
calPlanWorkunit.setWorkunitCode(calWorkunit.getWorkunitCode());
calPlanWorkunit.setWorkunitName(calWorkunit.getWorkunitName());
Date date1 = MesCalendarUtil.dateStrToDate(nowDate + " " + MesCalendarUtil.getDateHourStr(shift.getStartDate()));
calPlanWorkunit.setStartDate(date1);
calPlanWorkunit.setStartDate(addDays(date1, diffDay));
Date date = MesCalendarUtil.dateStrToDate(nowDate + " " + MesCalendarUtil.getDateHourStr(shift.getEndDate()));
if (date1.getTime() > date.getTime()) {
calPlanWorkunit.setEndDate(addDays(date, 1));
} else {
calPlanWorkunit.setEndDate(date);
diffDay++;
}
calPlanWorkunit.setEndDate(addDays(date, diffDay));
calPlanWorkunit.setRestFlag(shift.getRestFlag());
saveList.add(calPlanWorkunit);
}
......@@ -217,32 +220,6 @@ public class CalPlanServiceImpl implements ICalPlanService {
return saveList;
}
/*
* java265.com 示例程序
*/
public static void main(String[] args) throws ParseException {
// Calendar calendar = Calendar.getInstance();
// calendar.add(Calendar.DAY_OF_MONTH, 20); // 在当前日期上加5天
// 输出5天后的时间
// System.out.println(calendar.getTime());
// List<Date> weekendByMonth = getWeekendByMonth(new Date(), calendar.getTime(), Arrays.asList(Calendar.SATURDAY, Calendar.SUNDAY));
// System.out.println(weekendByMonth);
// System.out.println(JSON.toJSONString(weekendByMonth));
// System.out.println(JSON.toJSONString(weekendByMonth));
// getWeekendByMonth(new Date(), Calendar.SUNDAY);
// Long days = CalendarUtil.getDateDiff(new Date(), calendar.getTime());
// System.out.println(days);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String format = sdf.format(new Date());
String dateStr = format + " " + "8:00";
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
Date date = dateFormat.parse(dateStr);
System.out.println(date);
}
/**
* 根据排班计划生成每个班组的明细排班记录
......@@ -256,7 +233,7 @@ public class CalPlanServiceImpl implements ICalPlanService {
*/
@Async
@Override
public void genRecords(CalPlan calPlan) {
public List<CalPlanWorkunit> genRecords(CalPlan calPlan, List<CalWorkunit> calWorkunits) {
List<CalShift> shifts = calPlan.getCalShifts();
List<CalShift> shiftList = new ArrayList<>();
Date startDate = calPlan.getStartDate();
......@@ -282,12 +259,11 @@ public class CalPlanServiceImpl implements ICalPlanService {
shift1.setStartDate(shift1StartDate);
shift1.setEndDate(shift1EndDate);
shift1.setOperType(1);
shift1.setRestFlag(1);
shift1.setShiftName(calShift.getShiftName());
shift1.setShiftId(calShift.getShiftId());
shiftList.add(shift1);
}
Date shift3EndDate = formatShiftDate(startDateFormat, shift.getEndTime());
CalShift shift3 = shift.deepCopyObj();
shift3.setStartDate(shift1EndDate);
......@@ -303,23 +279,24 @@ public class CalPlanServiceImpl implements ICalPlanService {
shiftList.add(shift1);
}
}
List<String> weekendMonthStrList = this.getWeekendList(calPlan.getSaturday(), calPlan.getSunday(), calPlan.getStartDate(), calPlan.getEndDate());
List<CalPlanWorkunit> calPlanWorkunits = generateShiftRecords(shiftList, calPlan, weekendMonthStrList, calWorkunits);
calPlanWorkunits.forEach(x -> {
String updateBy = calPlan.getUpdateBy();
x.setCreateBy(updateBy);
x.setUpdateBy(updateBy);
Date nowDate = DateUtils.getNowDate();
x.setCreateTime(nowDate);
x.setUpdateTime(nowDate);
});
return calPlanWorkunits;
}
List<CalWorkunit> calWorkunits = calWorkunitMapper.selectListByQw(new QueryWrapper<CalWorkunit>().eq("plan_id", calPlan.getPlanId()));
ExceptionUtil.checkTrueThrowException(CollectionUtil.isEmpty(calWorkunits), MessageUtils.message("cal.error.error19"));
List<Integer> weekList = new ArrayList<>();
if (Objects.equals(calPlan.getSaturday(), CalPlanWorkDayEnum.WEEKEND.getType())) {
weekList.add(Calendar.SATURDAY);
}
if (Objects.equals(calPlan.getSunday(), CalPlanWorkDayEnum.WEEKEND.getType())) {
weekList.add(Calendar.SUNDAY);
}
private void saveCalPlanWorkunit(CalPlan calPlan, List<CalPlanWorkunit> calPlanWorkunits, List<CalWorkunit> calWorkunits){
List<String> shiftNames = calPlan.getCalShifts().stream().map(CalShift::getShiftName).collect(Collectors.toList());
List<Date> weekendByMonth = getWeekendByMonth(startDate, endDate, weekList);
List<String> weekendMonthStrList = weekendByMonth.stream().filter(Objects::nonNull).map(MesCalendarUtil::getDateDayStr).collect(Collectors.toList());
List<Long> taskWorkunitIds = calWorkunits.stream().map(CalWorkunit::getWorkunitId).collect(Collectors.toList());
List<String> shiftNames = shifts.stream().map(CalShift::getShiftName).collect(Collectors.toList());
List<String> weekendMonthStrList = this.getWeekendList(calPlan.getSaturday(), calPlan.getSunday(), calPlan.getStartDate(), calPlan.getEndDate());
// 删除同一天在同一个工作单元已经粗在上的旧数据
QueryWrapper<CalPlanWorkunit> calPlanWorkunitQueryWrapper = new QueryWrapper<>();
calPlanWorkunitQueryWrapper.in("t1.the_day", weekendMonthStrList);
......@@ -331,15 +308,6 @@ public class CalPlanServiceImpl implements ICalPlanService {
EXECUTOR.execute(() -> calPlanWorkunitService.deleteCalPlanWorkunitByPlanWorkunitIds(list.toArray(new Long[]{})));
}
List<CalPlanWorkunit> calPlanWorkunits = generateShiftRecords(shiftList, calPlan, weekendMonthStrList, calWorkunits);
calPlanWorkunits.forEach(x -> {
String updateBy = calPlan.getUpdateBy();
x.setCreateBy(updateBy);
x.setUpdateBy(updateBy);
Date nowDate = DateUtils.getNowDate();
x.setCreateTime(nowDate);
x.setUpdateTime(nowDate);
});
List<List<CalPlanWorkunit>> savelists = chunkList(calPlanWorkunits, 500);
for (List<CalPlanWorkunit> list : savelists) {
......@@ -347,6 +315,19 @@ public class CalPlanServiceImpl implements ICalPlanService {
}
}
private List<String> getWeekendList(int saturdayRest, int sundayRest, Date startDate, Date endDate){
List<Integer> weekList = new ArrayList<>();
if (Objects.equals(saturdayRest, CalPlanWorkDayEnum.WEEKEND.getType())) {
weekList.add(Calendar.SATURDAY);
}
if (Objects.equals(sundayRest, CalPlanWorkDayEnum.WEEKEND.getType())) {
weekList.add(Calendar.SUNDAY);
}
List<Date> weekendByMonth = getWeekendByMonth(startDate, endDate, weekList);
List<String> weekendMonthStrList = weekendByMonth.stream().filter(Objects::nonNull).map(MesCalendarUtil::getDateDayStr).collect(Collectors.toList());
return weekendMonthStrList;
}
public static final ExecutorService EXECUTOR = Executors.newFixedThreadPool(4);
......
package com.bs.mes.cal;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.ximai.mes.cal.domain.CalPlan;
import com.ximai.mes.cal.domain.CalPlanWorkunit;
import com.ximai.mes.cal.domain.CalShift;
import com.ximai.mes.cal.domain.CalWorkunit;
import com.ximai.mes.cal.service.impl.CalPlanServiceImpl;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
public class CalPlanServiceImplTest
{
@Test public void genRecords(){
CalPlanServiceImpl calPlanService = new CalPlanServiceImpl();
CalPlan calPlan = this.getCalPlan1();
List<CalWorkunit> calWorkunitList = new ArrayList<>();
List<CalPlanWorkunit> list = calPlanService.genRecords(calPlan, calWorkunitList);
Assert.assertEquals(0, list.size());
list = calPlanService.genRecords(calPlan, this.getCalWorkunitList(5));
//每天分为2个班+1个休息班次,10天中4天为周天
Assert.assertEquals(5*3*(10-4), list.size());
System.out.println(CollectionUtil.join(list,"\r\n"));
Assert.assertEquals("24-11-08 12:00",
DateUtil.format(list.get(list.size()-2).getStartDate(), "yy-MM-dd HH:mm"));
Assert.assertEquals("24-11-08 13:30",
DateUtil.format(list.get(list.size()-2).getEndDate(), "yy-MM-dd HH:mm"));
Assert.assertEquals("24-11-08 13:30",
DateUtil.format(list.get(list.size()-1).getStartDate(),"yy-MM-dd HH:mm"));
Assert.assertEquals("24-11-08 18:00",
DateUtil.format(list.get(list.size()-1).getEndDate(),"yy-MM-dd HH:mm"));
long weekend1 = DateUtil.parse("2024-11-02","yyyy-MM-dd").getTime();
long weekend2 = DateUtil.parse("2024-11-03","yyyy-MM-dd").getTime();
long weekend3 = DateUtil.parse("2024-11-04","yyyy-MM-dd").getTime();
//不存在周未数据
for (CalPlanWorkunit calPlanWorkunit : list) {
//计划开始、结束时间不会同时在周六-周日0点之间
Assert.assertFalse(
calPlanWorkunit.getStartDate().getTime()>=weekend1&&calPlanWorkunit.getStartDate().getTime()<weekend2
&&
calPlanWorkunit.getEndDate().getTime()>=weekend2&&calPlanWorkunit.getEndDate().getTime()<weekend3);
//计划开始、结束时间不会同时在周日-周一0点之间
Assert.assertFalse(calPlanWorkunit.getStartDate().getTime()>=weekend2&&calPlanWorkunit.getStartDate().getTime()<weekend3
&&
calPlanWorkunit.getEndDate().getTime()>=weekend2&&calPlanWorkunit.getEndDate().getTime()<weekend3);
}
// 测试2, 2班次,白/夜班次
calPlan = getCalPlan2();
long weekend4 = DateUtil.parse("2024-11-02 08:00","yyyy-MM-dd HH:mm").getTime();
long weekend5 = DateUtil.parse("2024-11-03 08:00","yyyy-MM-dd HH:mm").getTime();
long weekend6 = DateUtil.parse("2024-11-04 08:00","yyyy-MM-dd HH:mm").getTime();
list = calPlanService.genRecords(calPlan, this.getCalWorkunitList(5));
//每天分为2*2个班+2个休息班次,10天中8天为工作日
Assert.assertEquals(5*6*8, list.size());
//不存在周日数据
for (CalPlanWorkunit calPlanWorkunit : list) {
//计划开始、结束时间不会同时在周日-周一0点之间
Assert.assertFalse(calPlanWorkunit.getStartDate().getTime()>=weekend5&&calPlanWorkunit.getStartDate().getTime()<weekend6
&&
calPlanWorkunit.getEndDate().getTime()>=weekend5&&calPlanWorkunit.getEndDate().getTime()<weekend6);
}
//存在周六数据
Assert.assertTrue(list.stream().anyMatch(s->{
return s.getStartDate().getTime()>=weekend1&&s.getStartDate().getTime()<weekend2
&&
s.getEndDate().getTime()>=weekend2&&s.getEndDate().getTime()<weekend3;
}));
System.out.println(CollectionUtil.join(list,"\r\n"));
Assert.assertEquals(0, list.get(list.size()-1).getRestFlag());
Assert.assertEquals("24-11-10 01:00",
DateUtil.format(list.get(list.size()-1).getStartDate(),"yy-MM-dd HH:mm"));
Assert.assertEquals("24-11-10 08:00",
DateUtil.format(list.get(list.size()-1).getEndDate(),"yy-MM-dd HH:mm"));
Assert.assertEquals(1, list.get(list.size()-2).getRestFlag());
Assert.assertEquals("24-11-10 00:00",
DateUtil.format(list.get(list.size()-2).getStartDate(),"yy-MM-dd HH:mm"));
Assert.assertEquals("24-11-10 01:00",
DateUtil.format(list.get(list.size()-2).getEndDate(),"yy-MM-dd HH:mm"));
// 测试3, 2班次,白/夜班次 无休息
calPlan = getCalPlan3();
list = calPlanService.genRecords(calPlan, this.getCalWorkunitList(5));
//每天分为2个班,10天中8天为工作日
Assert.assertEquals(5*2*8, list.size());
Assert.assertEquals(0, list.get(list.size()-1).getRestFlag());
Assert.assertEquals("24-11-09 20:00",
DateUtil.format(list.get(list.size()-1).getStartDate(),"yy-MM-dd HH:mm"));
Assert.assertEquals("24-11-10 08:00",
DateUtil.format(list.get(list.size()-1).getEndDate(),"yy-MM-dd HH:mm"));
}
/*
单班计划,10天
白班,8:00-18:00,中午休息12:00-13:30
*/
private CalPlan getCalPlan1(){
CalPlan calPlan = new CalPlan();
calPlan.setSaturday(2);
calPlan.setSunday(2);
calPlan.setStartDate(DateUtil.parse("2024-11-01", "yyyy-MM-dd"));
calPlan.setEndDate(DateUtil.parse("2024-11-10", "yyyy-MM-dd"));
List<CalShift> calShifts = new ArrayList<>();
CalShift dayShift = new CalShift();
dayShift.setShiftName("白班");
dayShift.setStartTime("8:00");
dayShift.setEndTime("18:00");
calShifts.add(dayShift);
List<CalShift> calShiftBreakTimeList = new ArrayList<>();
CalShift breakTime = new CalShift();
breakTime.setBreakStartTime("12:00");
breakTime.setBreakEndTime("13:30");
calShiftBreakTimeList.add(breakTime);
dayShift.setBreakTimeList(calShiftBreakTimeList);
calPlan.setCalShifts(calShifts);
return calPlan;
}
/**
二班计划,10天,考虑周六加班
白班,8:00-20:00,中午休息12:00-14:00
夜班,20:00-8:00(隔天),夜间休息00:00-1:00
*/
private CalPlan getCalPlan2(){
CalPlan calPlan = new CalPlan();
calPlan.setSaturday(1);
calPlan.setSunday(2);
calPlan.setStartDate(DateUtil.parse("2024-11-01", "yyyy-MM-dd"));
calPlan.setEndDate(DateUtil.parse("2024-11-10", "yyyy-MM-dd"));
List<CalShift> calShifts = new ArrayList<>();
//白班
CalShift dayShift = new CalShift();
dayShift.setShiftName("白班");
dayShift.setStartTime("8:00");
dayShift.setEndTime("20:00");
calShifts.add(dayShift);
List<CalShift> calShiftBreakTimeList = new ArrayList<>();
CalShift breakTime = new CalShift();
breakTime.setBreakStartTime("12:00");
breakTime.setBreakEndTime("14:00");
calShiftBreakTimeList.add(breakTime);
dayShift.setBreakTimeList(calShiftBreakTimeList);
//夜班
CalShift nightShift = new CalShift();
nightShift.setShiftName("夜班");
nightShift.setStartTime("20:00");
nightShift.setEndTime("8:00");
calShifts.add(nightShift);
List<CalShift> calShiftBreakTimeList2 = new ArrayList<>();
CalShift breakTime2 = new CalShift();
breakTime2.setBreakStartTime("0:00");
breakTime2.setBreakEndTime("1:00");
calShiftBreakTimeList2.add(breakTime2);
nightShift.setBreakTimeList(calShiftBreakTimeList2);
calPlan.setCalShifts(calShifts);
return calPlan;
}
/**
无休息
二班计划,10天,考虑周六、周日加班
白班,8:00-20:00
夜班,20:00-8:00(隔天)
*/
private CalPlan getCalPlan3(){
CalPlan calPlan = new CalPlan();
calPlan.setSaturday(1);
calPlan.setSunday(2);
calPlan.setStartDate(DateUtil.parse("2024-11-01", "yyyy-MM-dd"));
calPlan.setEndDate(DateUtil.parse("2024-11-10", "yyyy-MM-dd"));
List<CalShift> calShifts = new ArrayList<>();
//白班
CalShift dayShift = new CalShift();
dayShift.setShiftName("白班");
dayShift.setStartTime("8:00");
dayShift.setEndTime("20:00");
calShifts.add(dayShift);
//夜班
CalShift nightShift = new CalShift();
nightShift.setShiftName("夜班");
nightShift.setStartTime("20:00");
nightShift.setEndTime("8:00");
calShifts.add(nightShift);
calPlan.setCalShifts(calShifts);
return calPlan;
}
private List<CalWorkunit> getCalWorkunitList(int ct){
List<CalWorkunit> rst = new ArrayList<>();
for(int i=0;i<ct;i++){
CalWorkunit temp = new CalWorkunit();
temp.setWorkunitId(i+0l);
temp.setWorkunitCode(i+"code");
rst.add(temp);
}
return rst;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment