杜洪波
2025-05-14 9db1c6c261049fbdcb104e6c0e07fa544bb42013
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package com.product.administration.service;
 
import com.product.administration.config.CmnConst;
import com.product.administration.service.ide.ICarChartservice;
import com.product.common.lang.DateUtils;
import com.product.common.lang.StringUtils;
import com.product.core.dao.BaseDao;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.service.support.AbstractBaseService;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.core.transfer.Transactional;
import com.product.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
 
/**
 * Copyright LX
 * @Title: CarChartservice
 * @Project: product-server
 * @date: 2021-05-07 10:07
 * @author: ZhouJie
 * @Description:
 */
@Component
@Service
public class CarChartservice extends AbstractBaseService implements ICarChartservice {
 
    @Autowired
    public BaseDao baseDao;
    /**
     * @description: 用车看板保存
     * @author: ZhouJie
     * @date: 2021-05-07 11:46
     */
    @Override
    @Transactional
    public String saveCarChart(FieldSetEntity fs) {
        if (StringUtils.isEmpty(fs.getString(CmnConst.UUID))) {
            fs.setValue("created_by", SpringMVCContextHolder.getCurrentUser().getUser_id());
            fs.setValue("created_utc_datetime", new Date());
            return baseDao.add(fs);
        } else {
            fs.setValue("updated_by", SpringMVCContextHolder.getCurrentUser().getUser_id());
            fs.setValue("updated_utc_datetime", new Date());
            baseDao.update(fs);
            return fs.getString(CmnConst.UUID);
        }
    }
    /**
     * @description: 用车看板删除
     * @author: ZhouJie
     * @date: 2021-05-07 11:46
     */
    @Override
    @Transactional
    public boolean deleteCarChart (FieldSetEntity fs){
        String uuid = fs.getUUID();
        String[] uuids = uuid.split(",");
        return baseDao.delete(CmnConst.PRODUCT_OA_CAR_CHART, BaseUtil.buildQuestionMarkFilter(CmnConst.UUID, uuids.length, true), uuids);
    }
    /**
     * @description: 用车看板列表
     * @author: ZhouJie
     * @date: 2021-05-07 11:47
     */
    public DataTableEntity CarChartlist(FieldSetEntity fs) {
        DataTableEntity dt = baseDao.listTable(CmnConst.PRODUCT_OA_CAR_CHART,null,null,null,null,fs.getInteger(CmnConst.PAGESIZE),fs.getInteger(CmnConst.CPAGE));
        baseDao.loadPromptData(dt);
        return dt;
    }
    /**
     * @description: 用车看板详情
     * @author: ZhouJie
     * @date: 2021-05-07 11:47
     */
    public FieldSetEntity CarChartInfo(FieldSetEntity fs) throws BaseException {
        return baseDao.getFieldSetEntity(CmnConst.PRODUCT_OA_CAR_CHART,fs.getUUID(),true);
    }
 
 
    /**
     * 用车看板
     *
     * @return
     * @throws BaseException
     */
    public String useTheKanban(FieldSetEntity fse) throws BaseException {
        String org_level_uuid = SpringMVCContextHolder.getCurrentUser().getOrg_level_uuid();
        if (StringUtils.isEmpty(org_level_uuid)) {
            return null;
        }
        //一周开始时间
        String startTime = fse.getString("start_time");
        //一周结束时间
        String endTime = fse.getString("end_time");
        StringBuilder sql = new StringBuilder();
        //没有还车时间的  车辆一律在使用中。   vehicle_status  是车辆使用状态
        sql.append("  SELECT b.uuid,b.plate_number,a.driving_time,a.return_time,a.scheduled_start_time,a.scheduled_end_time,a.show_name,CASE  WHEN c.return_time is null AND c.driving_time is not null THEN 1 ELSE 0 END vehicle_status FROM product_oa_car_info b ")
                .append(" LEFT JOIN  (SELECT a.vehicle_info_uuid,a.driving_time,a.return_time,a.scheduled_start_time,a.scheduled_end_time,c.show_name FROM product_oa_car_application a ")
                .append(" LEFT JOIN product_sys_staffs c ON a.applicant = c.user_id WHERE a.scheduled_start_time >= ? AND a.scheduled_start_time <= ? and a.flow_flag > 0) ")
                .append(" a ON a.vehicle_info_uuid = b.uuid LEFT JOIN  (SELECT a.vehicle_info_uuid,a.driving_time,a.return_time,a.scheduled_start_time,a.scheduled_end_time,c.show_name FROM product_oa_car_application a ")
                .append(" LEFT JOIN product_sys_staffs c ON a.applicant = c.user_id WHERE a.flow_flag > 0 AND a.scheduled_start_time is not null and a.return_time is null) c on c.vehicle_info_uuid = b.uuid order by b.id ");
        DataTableEntity dt = baseDao.listTable(sql.toString(), new Object[]{startTime, endTime});
        dt.getMeta().addAliasTable(CmnConst.PRODUCT_OA_CAR_INFO, "b");
        //baseDao.loadPromptData(dt);
        sql.setLength(0);
        if (!BaseUtil.dataTableIsEmpty(dt)) {
            List<String> weekRefer = new ArrayList<>();
            weekRefer.add("星期一");
            weekRefer.add("星期二");
            weekRefer.add("星期三");
            weekRefer.add("星期四");
            weekRefer.add("星期五");
            weekRefer.add("星期六");
            weekRefer.add("星期日");
            // key = 会议室 value :  key=星期几上午或下午 value=每次申请会议室的数据
            Map<String, Map<String, List<Map<Object, Object>>>> plate = new HashMap<>();
            Map<String, String[]> plate_number = new LinkedHashMap<>();
            for (int i = 0; i < dt.getRows(); i++) {
                FieldSetEntity fs = dt.getFieldSetEntity(i);
                String uuid = fs.getUUID();
                //uuid 为key
                Map<String, List<Map<Object, Object>>> data = plate.get(uuid);
                if (data == null) {
                    data = new HashMap<>();
                    //车辆名称
                    plate_number.put(uuid,new String[]{fs.getString("plate_number"), fs.getString("vehicle_status"),fs.getString("return_time")});
                    plate.put(uuid, data);
                }
                putMeetingToWeek(data, fs, fse);
            }
            HashMap<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("plate", plate);
            return BaseUtil.success(plate_number, objectObjectHashMap);
        }
        return null;
    }
 
    private void putMeetingToWeek(Map<String, List<Map<Object, Object>>> data, FieldSetEntity fs, FieldSetEntity fse) throws BaseException {
 
        Date scheduledStartTime = fs.getDate("scheduled_start_time");
        // 周几
        if (scheduledStartTime == null) {
            return;
        }
        //时间转换为星期
        String week = BaseUtil.getWeek(scheduledStartTime);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(scheduledStartTime);
        //  0 上午 1 下午
//        System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
//        System.out.println(calendar.get(Calendar.AM_PM));
        int am_pm = calendar.get(Calendar.HOUR_OF_DAY) > 12 ? 1 : 0;
        Date scheduledEndTime = fs.getDate("scheduled_end_time");
        calendar.setTime(scheduledEndTime);
        int end_am_pm = calendar.get(Calendar.HOUR_OF_DAY) > 12 ? 1 : 0;
 
        //是否同一天
        boolean samedate = DateUtils.isSameDay(scheduledStartTime, scheduledEndTime);
        if (end_am_pm == am_pm && samedate) {
            // 开始时间 和结束时间属于同一时间段且是同一天
        } else {
            if (end_am_pm != am_pm && samedate) {
                // 开始时间 和结束时间不属于同一时间段且是同一天
                putMeetingToWeek(data, week, end_am_pm, fs.getValues());
            } else {
                // 不在同一天
                //获取开始到结束的每一天
                List<Object> timePeriodDate = getTimePeriodDate(scheduledStartTime, scheduledEndTime, false);
                putMeetingToWeek(data, timePeriodDate, fse, fs, am_pm);
            }
        }
        //记录的开始时间不能大于每周的开始时间
        //Date1.before(Date2),当Date1小于Date2时,返回TRUE,当大于等于时,返回false;
        if (!scheduledStartTime.before(fse.getDate("start_time"))) {
            //星期几_0   上午   星期几_1  下午
            List<Map<Object, Object>> listMap = data.get(week + "_" + am_pm);
            if (listMap == null) {
                listMap = new ArrayList<>();
                data.put(week + "_" + am_pm, listMap);
            }
            listMap.add(fs.getValues());
        }
    }
 
    private void putMeetingToWeek(Map<String, List<Map<Object, Object>>> data, List<Object> timePeriodDate, FieldSetEntity fse, FieldSetEntity fs, int am_pm) {
        Calendar calendar = Calendar.getInstance();
        //预计开始时间
        Date scheduledStartTime = fs.getDate("scheduled_start_time");
        //预计结束时间
        Date scheduledEndTime = fs.getDate("scheduled_end_time");
        for (int j = 0; j < timePeriodDate.size(); j++) {
            //每天的date
            Date o = (Date) timePeriodDate.get(j);
            //startTime 一周开始时间   endTime 一周结束时间
            if (o.before(fse.getDate("start_time")) || fse.getDate("end_time").before(o)) {
                continue;
            }
            String week_evey = BaseUtil.getWeek(o);
            //是否是同一天
            if (DateUtils.isSameDay(scheduledStartTime, o)) {
                //是否为上午时段
                if (am_pm == 0) {
                    putMeetingToWeek(data, week_evey, 1, fs.getValues());
                }
            } else if ((j + 1) < timePeriodDate.size()) {
                putMeetingToWeek(data, week_evey, 0, fs.getValues());
                putMeetingToWeek(data, week_evey, 1, fs.getValues());
            } else {
                SimpleDateFormat dtf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String date1 = dtf.format(o);
                String date2 = dtf.format(scheduledEndTime);
                if (date1.equals(date2)) {
                    //最后结束日期
                    calendar.setTime(o);
//                    System.out.println(calendar.get(Calendar.HOUR_OF_DAY));
//                    System.out.println(calendar.get(Calendar.AM_PM));
                    int cc = calendar.get(Calendar.HOUR_OF_DAY) > 12 ? 1 : 0;
                    if (cc == 1) {
                        //如果最后一天是下午则加上 上午
                        putMeetingToWeek(data, week_evey, 0, fs.getValues());
                    }
                    putMeetingToWeek(data, week_evey, cc, fs.getValues());
                } else {
 
                }
            }
        }
    }
 
    private void putMeetingToWeek(Map<String, List<Map<Object, Object>>> data, String week, int am_pm, Map<Object, Object> values) {
        List<Map<Object, Object>> list = data.get(week + "_" + am_pm);
        if (list == null) {
            list = new ArrayList<>();
            data.put(week + "_" + am_pm, list);
        }
        Map<Object, Object> clone = new TreeMap<>();
        clone.putAll(values);
        list.add(clone);
    }
 
 
 
    /**
     * 获取时间段内的每一天
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<Object> getTimePeriodDate(Date startDate, Date endDate, boolean returnChar) {
        // 返回的日期集合
        List<Object> days = new ArrayList<>();
 
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(startDate);
        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(endDate);
        while (tempStart.before(tempEnd)) {
            days.add(returnChar ? dateFormat.format(tempStart.getTime()) : tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        if(days.size()>1){
            days.add(endDate);
        }
        return days;
    }
}