package org.jeecg.modules.mdc.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import org.apache.commons.lang3.StringUtils;
|
import org.jeecg.modules.mdc.entity.MdcDeviceCalendar;
|
import org.jeecg.modules.mdc.entity.MdcShiftSub;
|
import org.jeecg.modules.mdc.mapper.MdcDeviceCalendarMapper;
|
import org.jeecg.modules.mdc.service.IMdcDeviceCalendarService;
|
import org.jeecg.modules.mdc.service.IMdcEquipmentService;
|
import org.jeecg.modules.mdc.service.IMdcShiftSubService;
|
import org.jeecg.modules.mdc.vo.EquipmentCalendarVo;
|
import org.jeecg.modules.mdc.vo.MdcDeviceCalendarQueryVo;
|
import org.jeecg.modules.mdc.vo.MdcDeviceCalendarVo;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import javax.servlet.http.HttpServletRequest;
|
import java.time.DayOfWeek;
|
import java.time.LocalDate;
|
import java.time.format.DateTimeFormatter;
|
import java.time.temporal.ChronoUnit;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
import java.util.stream.Stream;
|
|
/**
|
* @Description: 设备工作日历表
|
* @Author: jeecg-boot
|
* @Date: 2023-04-10
|
* @Version: V1.0
|
*/
|
@Service
|
public class MdcDeviceCalendarServiceImpl extends ServiceImpl<MdcDeviceCalendarMapper, MdcDeviceCalendar> implements IMdcDeviceCalendarService {
|
|
@Resource
|
private IMdcEquipmentService mdcEquipmentService;
|
@Resource
|
private IMdcShiftSubService mdcShiftSubService;
|
|
/**
|
* 分页列表查询
|
*/
|
@Override
|
public IPage<MdcDeviceCalendarVo> pageList(String userId, MdcDeviceCalendarQueryVo mdcDeviceCalendarQueryVo, Integer pageNo, Integer pageSize, HttpServletRequest req) {
|
IPage<MdcDeviceCalendarVo> pageData = new Page<>(pageNo, pageSize);
|
List<String> equipmentIds = new ArrayList<>();
|
if (StringUtils.isNotEmpty(mdcDeviceCalendarQueryVo.getParentId()) && StringUtils.isEmpty(mdcDeviceCalendarQueryVo.getEquipmentId())) {
|
if ("2".equals(mdcDeviceCalendarQueryVo.getTypeTree())) {
|
//部门层级
|
equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, mdcDeviceCalendarQueryVo.getParentId());
|
} else {
|
//产线层级
|
equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, mdcDeviceCalendarQueryVo.getParentId());
|
}
|
} else if (StringUtils.isNotEmpty(mdcDeviceCalendarQueryVo.getEquipmentId())) {
|
//单台设备信息
|
mdcDeviceCalendarQueryVo.setEquipmentIdList(Collections.singletonList(mdcDeviceCalendarQueryVo.getEquipmentId()));
|
} else {
|
//查询用户拥有的所有设备信息
|
if ("2".equals(mdcDeviceCalendarQueryVo.getTypeTree())) {
|
//部门层级
|
equipmentIds = mdcEquipmentService.getEquipmentIdsByDepart(userId, null);
|
} else {
|
//产线层级
|
equipmentIds = mdcEquipmentService.getEquipmentIdsProduction(userId, null);
|
}
|
}
|
mdcDeviceCalendarQueryVo.setEquipmentIdList(equipmentIds);
|
|
return this.baseMapper.pageList(pageData, mdcDeviceCalendarQueryVo);
|
}
|
|
@Override
|
public boolean saveCalendar(EquipmentCalendarVo calendarVo) {
|
String[] equipmentIdList = calendarVo.getEquipmentId().split(",");
|
//生效时间
|
LocalDate takeEffectDate = calendarVo.getTakeEffectDate();
|
//失效时间
|
LocalDate invalidDate = calendarVo.getInvalidDate();
|
//处理星期
|
Map<String, String> map = new HashMap<>();
|
if (org.apache.commons.lang.StringUtils.isNotBlank(calendarVo.getMonShiftId())) {
|
map.put("1", calendarVo.getMonShiftId());
|
}
|
if (org.apache.commons.lang.StringUtils.isNotBlank(calendarVo.getTueShiftId())) {
|
map.put("2", calendarVo.getTueShiftId());
|
}
|
if (org.apache.commons.lang.StringUtils.isNotBlank(calendarVo.getWedShiftId())) {
|
map.put("3", calendarVo.getWedShiftId());
|
}
|
if (org.apache.commons.lang.StringUtils.isNotBlank(calendarVo.getThuShiftId())) {
|
map.put("4", calendarVo.getThuShiftId());
|
}
|
if (org.apache.commons.lang.StringUtils.isNotBlank(calendarVo.getFriShiftId())) {
|
map.put("5", calendarVo.getFriShiftId());
|
}
|
if (org.apache.commons.lang.StringUtils.isNotBlank(calendarVo.getSatShiftId())) {
|
map.put("6", calendarVo.getSatShiftId());
|
}
|
if (org.apache.commons.lang.StringUtils.isNotBlank(calendarVo.getSunShiftId())) {
|
map.put("7", calendarVo.getSunShiftId());
|
}
|
if (map.isEmpty()) {
|
return false;
|
}
|
List<MdcDeviceCalendar> calendarList = new ArrayList<>();
|
map.forEach((key, value) -> {
|
List<MdcShiftSub> mdcShiftSubList = mdcShiftSubService.list(new LambdaQueryWrapper
|
<MdcShiftSub>().eq(MdcShiftSub::getShiftId, value).eq(MdcShiftSub::getShiftSubStatus, "1"));
|
List<LocalDate> meetDayOfWeekLocalDates = getMeetDayOfWeekLocalDates(takeEffectDate, invalidDate, key);
|
if (!meetDayOfWeekLocalDates.isEmpty()) {
|
List<String> dates = meetDayOfWeekLocalDates.stream().map(localDate -> localDate.format(DateTimeFormatter.BASIC_ISO_DATE)).collect(Collectors.toList());
|
LambdaUpdateChainWrapper<MdcDeviceCalendar> lambdaUpdate = this.lambdaUpdate();
|
lambdaUpdate.in(MdcDeviceCalendar::getEffectiveDate, dates).in(MdcDeviceCalendar::getEqumentId, Arrays.asList(equipmentIdList));
|
lambdaUpdate.remove();
|
List<MdcDeviceCalendar> calendars = handleMdcDeviceCalendar(dates, equipmentIdList, mdcShiftSubList, key);
|
calendarList.addAll(calendars);
|
}
|
});
|
this.saveBatch(calendarList);
|
return true;
|
}
|
|
/**
|
* 获取一段时间范围内符合星期几的日期集合
|
*
|
* @param startDate 开始时间
|
* @param endDate 结束时间
|
* @param workDate 周几 1,2,3,4
|
*/
|
public static List<LocalDate> getMeetDayOfWeekLocalDates(LocalDate startDate, LocalDate endDate, String workDate) {
|
|
List<DayOfWeek> dayOfWeeks = Stream.of(workDate.split(","))
|
.map(Integer::valueOf)
|
.map(DayOfWeek::of)
|
.collect(Collectors.toList());
|
|
long distance = ChronoUnit.DAYS.between(startDate, endDate);
|
|
return Stream.iterate(startDate, d -> d.plusDays(1))
|
.limit(distance + 1)
|
.filter(localDate -> dayOfWeeks.contains(localDate.getDayOfWeek()))
|
.collect(Collectors.toList());
|
}
|
|
public List<MdcDeviceCalendar> handleMdcDeviceCalendar(List<String> dates, String[] equipment, List<MdcShiftSub> mdcShiftSubList, String weekNumber) {
|
List<MdcDeviceCalendar> ds = new ArrayList<>();
|
for (String date : dates) {
|
for (String equipmentId : equipment) {
|
for (MdcShiftSub temp : mdcShiftSubList) {
|
MdcDeviceCalendar mdcDeviceCalendar = new MdcDeviceCalendar();
|
if (org.apache.commons.lang.StringUtils.isNotEmpty(equipmentId)) {
|
mdcDeviceCalendar.setEqumentId(equipmentId);
|
}
|
mdcDeviceCalendar.setShiftId(temp.getShiftId());
|
mdcDeviceCalendar.setShiftSubId(temp.getId());
|
mdcDeviceCalendar.setEffectiveDate(date);
|
mdcDeviceCalendar.setWeekNumber(weekNumber);
|
ds.add(mdcDeviceCalendar);
|
}
|
}
|
}
|
return ds;
|
}
|
}
|