zenglf
2023-09-28 f84d9e69907cb678150eaa6393fd74cf042fcca4
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/service/impl/MdcDeviceCalendarServiceImpl.java
@@ -2,7 +2,6 @@
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;
@@ -16,16 +15,11 @@
import org.jeecg.modules.mdc.vo.MdcDeviceCalendarQueryVo;
import org.jeecg.modules.mdc.vo.MdcDeviceCalendarVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
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: 设备工作日历表
@@ -81,95 +75,61 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    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<String> dateList = calendarVo.getDateList();
        List<MdcShiftSub> mdcShiftSubList = mdcShiftSubService.list(new LambdaQueryWrapper<MdcShiftSub>().eq(MdcShiftSub::getShiftId, calendarVo.getShiftId()).eq(MdcShiftSub::getShiftSubStatus, "1"));
        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);
        for (String equipmentId : equipmentIdList) {
            for (String date : dateList) {
                for (MdcShiftSub mdcShiftSub : mdcShiftSubList) {
                    this.baseMapper.delete(new LambdaQueryWrapper<MdcDeviceCalendar>().eq(MdcDeviceCalendar::getEquipmentId, equipmentId).eq(MdcDeviceCalendar::getEffectiveDate, date));
                    MdcDeviceCalendar mdcDeviceCalendar = new MdcDeviceCalendar();
                    mdcDeviceCalendar.setEffectiveDate(date);
                    mdcDeviceCalendar.setEquipmentId(equipmentId);
                    mdcDeviceCalendar.setShiftId(mdcShiftSub.getShiftId());
                    mdcDeviceCalendar.setShiftSubId(mdcShiftSub.getId());
                    calendarList.add(mdcDeviceCalendar);
            }
        });
            }
        }
        this.saveBatch(calendarList);
        return true;
    }
    /**
     * 获取一段时间范围内符合星期几的日期集合
     * 查询班制数据
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @param workDate  周几 1,2,3,4
     * @param equipmentId
     * @param stringDates
     * @return
     */
    public static List<LocalDate> getMeetDayOfWeekLocalDates(LocalDate startDate, LocalDate endDate, String workDate) {
    @Override
    public List<MdcDeviceCalendarVo> listByEquipmentAndDate(String equipmentId, List<String> stringDates) {
        List<MdcDeviceCalendarVo> result = new ArrayList<>();
        try {
            //查询默认班制
            List<MdcDeviceCalendarVo> acquiesceShift = this.baseMapper.findAcquiesceShift();
        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());
            for (String stringDate : stringDates) {
                List<MdcDeviceCalendarVo> mdcDeviceCalendarVos = this.baseMapper.listByEquipmentAndDate(equipmentId, stringDate);
                if (mdcDeviceCalendarVos != null && !mdcDeviceCalendarVos.isEmpty()) {
                    result.addAll(mdcDeviceCalendarVos);
                } else {
                    acquiesceShift.forEach(mdcDeviceCalendarVo -> {
                        mdcDeviceCalendarVo.setEquipmentId(equipmentId);
                        mdcDeviceCalendarVo.setEffectiveDate(stringDate);
                    });
                    // 设置默认班制
                    result.addAll(acquiesceShift);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
    }
    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);
        return result;
                    }
                    mdcDeviceCalendar.setShiftId(temp.getShiftId());
                    mdcDeviceCalendar.setShiftSubId(temp.getId());
                    mdcDeviceCalendar.setEffectiveDate(date);
                    mdcDeviceCalendar.setWeekNumber(weekNumber);
                    ds.add(mdcDeviceCalendar);
                }
            }
        }
        return ds;
    }
}