李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 *    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;
    }
 
}