/*
|
* Copyright (c) 2018-2025, ztt All rights reserved.
|
*
|
* Redistribution and use in source and binary forms, with or without
|
* modification, are permitted provided that the following conditions are met:
|
*
|
* Redistributions of source code must retain the above copyright notice,
|
* this list of conditions and the following disclaimer.
|
* Redistributions in binary form must reproduce the above copyright
|
* notice, this list of conditions and the following disclaimer in the
|
* documentation and/or other materials provided with the distribution.
|
* Neither the name of the pig4cloud.com developer nor the names of its
|
* contributors may be used to endorse or promote products derived from
|
* this software without specific prior written permission.
|
* Author: ztt
|
*/
|
package com.chinaztt.mes.aps.service.impl;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
import com.alibaba.fastjson.JSONObject;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.chinaztt.mes.aps.entity.TimetableException;
|
import com.chinaztt.mes.aps.entity.WeekTimetable;
|
import com.chinaztt.mes.aps.mapper.TimetableExceptionMapper;
|
import com.chinaztt.mes.aps.mapper.WeekTimetableMapper;
|
import com.chinaztt.mes.aps.service.TimetableService;
|
import com.chinaztt.mes.common.util.JsonUtils;
|
import com.chinaztt.ztt.common.core.util.R;
|
import lombok.AllArgsConstructor;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.time.LocalTime;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* 工作日历
|
*
|
* @author zhangxy
|
* @date 2020-09-21 14:40:55
|
*/
|
@Service
|
@AllArgsConstructor
|
@Transactional(rollbackFor = Exception.class)
|
public class TimetableServiceImpl extends ServiceImpl<WeekTimetableMapper, WeekTimetable> implements TimetableService {
|
|
private JdbcTemplate jdbcTemplate;
|
|
private TimetableExceptionMapper timetableExceptionMapper;
|
|
|
@Override
|
public R<Boolean> save(List<WeekTimetable> list) {
|
jdbcTemplate.execute(" truncate table aps_week_timetable");
|
|
if (CollectionUtil.isNotEmpty(list)) {
|
Iterator<WeekTimetable> it = list.iterator();
|
while (it.hasNext()) {
|
WeekTimetable ce = it.next();
|
if (CollectionUtil.isEmpty(ce.getRange())) {
|
it.remove();
|
continue;
|
}
|
ce.setFromTime(ce.getRange().get(0));
|
ce.setToTime(ce.getRange().get(1));
|
}
|
|
Map<String, List<WeekTimetable>> map = list.stream().collect(Collectors.groupingBy(WeekTimetable::getDayOfWeek));
|
|
map.values().forEach(groupedList -> {
|
List<WeekTimetable> mergedList = merge(groupedList);
|
mergedList.forEach(w -> baseMapper.insert(w));
|
});
|
}
|
return R.ok();
|
}
|
|
@Override
|
public R<Boolean> saveEx(List<TimetableException> list) {
|
if (CollectionUtil.isNotEmpty(list)) {
|
Iterator<TimetableException> it = list.iterator();
|
while (it.hasNext()) {
|
TimetableException ce = it.next();
|
if (CollectionUtil.isEmpty(ce.getRange())) {
|
it.remove();
|
continue;
|
}
|
ce.setFromDate(ce.getRange().get(0));
|
ce.setToDate(ce.getRange().get(1));
|
|
if (ce.getId() == null || ce.getId() == 0) {
|
timetableExceptionMapper.insert(ce);
|
} else {
|
timetableExceptionMapper.updateById(ce);
|
}
|
}
|
}
|
return null;
|
}
|
|
@Override
|
public JSONObject getAll() {
|
List<WeekTimetable> list = baseMapper.selectList(null);
|
if (CollectionUtil.isNotEmpty(list)) {
|
for (WeekTimetable week : list) {
|
week.setRange(Arrays.asList(week.getFromTime(), week.getToTime()));
|
}
|
return JsonUtils.toJsonObject(list.stream().collect(Collectors.groupingBy(WeekTimetable::getDayOfWeek)));
|
}
|
return null;
|
}
|
|
@Override
|
public List<TimetableException> getAllEx() {
|
List<TimetableException> list = timetableExceptionMapper.selectList(null);
|
if (CollectionUtil.isNotEmpty(list)) {
|
for (TimetableException ex : list) {
|
ex.setRange(Arrays.asList(ex.getFromDate(), ex.getToDate()));
|
}
|
return list;
|
}
|
return null;
|
}
|
|
@Override
|
public R<Boolean> removeExById(Long id) {
|
timetableExceptionMapper.deleteById(id);
|
return R.ok();
|
}
|
|
|
/**
|
* 合并重叠时间
|
*
|
* @param input
|
* @return
|
*/
|
public static List<WeekTimetable> merge(List<WeekTimetable> input) {
|
ArrayList<WeekTimetable> result = new ArrayList<>();
|
if (CollectionUtil.isEmpty(input)) {
|
return result;
|
}
|
Collections.sort(input);
|
WeekTimetable prev = input.get(0);
|
for (int i = 1; i < input.size(); i++) {
|
WeekTimetable curr = input.get(i);
|
if (prev.getToTime().compareTo(curr.getFromTime()) == 1) {
|
LocalTime end = prev.getToTime().compareTo(curr.getToTime()) == 1 ? prev.getToTime() : curr.getToTime();
|
WeekTimetable temp = new WeekTimetable();
|
temp.setFromTime(prev.getFromTime());
|
temp.setToTime(end);
|
temp.setDayOfWeek(prev.getDayOfWeek());
|
prev = temp;
|
} else {
|
result.add(prev);
|
prev = curr;
|
}
|
}
|
result.add(prev);
|
return result;
|
}
|
|
}
|