DriverSchedulingExpandSmartServiceImpl.java
6.92 KB
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
package com.ruoyi.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.global.ResultCode;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.domain.DriverSchedulingExpandSmart;
import com.ruoyi.expand.domain.DriverSchedulingExpand;
import com.ruoyi.expand.mapper.DriverSchedulingExpandMapper;
import com.ruoyi.expand.service.IDriverSchedulingExpandService;
import com.ruoyi.pojo.dto.SmartExpandDto;
import com.ruoyi.pojo.vo.SmartExpandVo;
import com.ruoyi.service.DriverSchedulingExpandSmartService;
import com.ruoyi.mapper.DriverSchedulingExpandSmartMapper;
import com.ruoyi.utils.ConstDateUtil;
import com.ruoyi.utils.ToolUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
/**
* @author 20412
* @description 针对表【driver_scheduling_expand_smart(灵活跟班表)】的数据库操作Service实现
* @createDate 2024-03-07 16:49:40
*/
@Service
@Slf4j
public class DriverSchedulingExpandSmartServiceImpl extends ServiceImpl<DriverSchedulingExpandSmartMapper, DriverSchedulingExpandSmart>
implements DriverSchedulingExpandSmartService {
@Autowired
private IDriverSchedulingExpandService expandService;
@Autowired
private DriverSchedulingExpandMapper expandMapper;
@Override
@Transactional(rollbackFor = Exception.class)
public void smartAdd(SmartExpandDto dto) {
List<DriverSchedulingExpandSmart> list = new ArrayList<>(dto.getActivities().size());
for (SmartExpandDto.Activity activity : dto.getActivities()) {
DriverSchedulingExpandSmart smart = new DriverSchedulingExpandSmart();
BeanUtils.copyProperties(activity, smart, "id");
smart.setSlaveJobCode(dto.getSlaveJobCode());
list.add(smart);
}
list.sort(Comparator.comparing(DriverSchedulingExpandSmart::getStartDate));
// 校验跟班时间正确
isExpandCorrect(list);
// 保存记录
DriverSchedulingExpand expand = new DriverSchedulingExpand();
BeanUtils.copyProperties(list.get(0), expand);
expand.setType(dto.getType());
expand.setRemark(dto.getRemark());
expandService.insertDriverSchedulingExpand(expand);
for (DriverSchedulingExpandSmart item : list) {
item.setExpandId(expand.getId().toString());
}
this.saveBatch(list);
}
@Override
public void computedExpand() {
try {
List<SmartExpandVo> vos = baseMapper.querySmartExpandAll();
int sum = 0;
for (SmartExpandVo vo : vos) {
SmartExpandVo.Activity activity = handlerSmartExpand(vo);
DriverSchedulingExpand expand = new DriverSchedulingExpand();
BeanUtils.copyProperties(activity, expand);
expand.setId(vo.getId());
expand.setType(1);
expand.setSlaveJobCode(vo.getSlaveJobCode());
int result = expandMapper.updateDriverSchedulingExpand(expand);
sum += result;
}
log.info("跟班计算完毕,共修改{}数据", sum);
} catch (Exception e) {
log.info("跟班计算出现问题:{}", e.getMessage());
}
}
private SmartExpandVo.Activity handlerSmartExpand(SmartExpandVo vo) {
vo.getActivities().sort(Comparator.comparing(SmartExpandVo.Activity::getStartDate));
Date date = new Date();
for (int i = 0; i < vo.getActivities().size(); i++) {
int compare = StringUtils.compare(ConstDateUtil.formatDate(date), ConstDateUtil.formatDate(vo.getActivities().get(i).getEndDate()));
if (compare > 0 && i < vo.getActivities().size() - 1) {
continue;
}
return vo.getActivities().get(i);
}
log.error("没有找到跟班对象:{}", vo);
throw new ServiceException("未知错误", ResultCode.CODE_500.getCode());
}
@Override
public SmartExpandVo querySmartExpand(Long id) {
return baseMapper.querySmartExpand(id);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateSmartExpand(SmartExpandVo vo) {
List<DriverSchedulingExpandSmart> list = new ArrayList<>(vo.getActivities().size());
// 删除老数据
LambdaQueryWrapper<DriverSchedulingExpandSmart> qw = new LambdaQueryWrapper<>();
qw.eq(DriverSchedulingExpandSmart::getExpandId, vo.getId());
remove(qw);
// COPY
for (SmartExpandVo.Activity activity : vo.getActivities()) {
DriverSchedulingExpandSmart smart = new DriverSchedulingExpandSmart();
BeanUtils.copyProperties(activity, smart, "id");
smart.setSlaveJobCode(vo.getSlaveJobCode());
list.add(smart);
}
list.sort(Comparator.comparing(DriverSchedulingExpandSmart::getStartDate));
for (DriverSchedulingExpandSmart item : list) {
item.setExpandId(vo.getId().toString());
}
this.saveBatch(list);
// 更新当前跟班显示
DriverSchedulingExpandSmart smart = ToolUtils.computedShowSmart(list);
DriverSchedulingExpand expand = new DriverSchedulingExpand();
BeanUtils.copyProperties(expand, smart, "id");
expand.setRemark(vo.getRemark());
expand.setType(vo.getType());
expand.setId(vo.getId());
expandService.updateDriverSchedulingExpand(expand);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteExpand(Long[] ids) {
expandService.deleteDriverSchedulingExpandByIds(ids);
LambdaQueryWrapper<DriverSchedulingExpandSmart> qw = new LambdaQueryWrapper<>();
qw.eq(DriverSchedulingExpandSmart::getExpandId, ids[0]);
remove(qw);
}
private void isExpandCorrect(List<DriverSchedulingExpandSmart> list) {
// 校验跟班人选中的时间日期是否合法
long day = 1000 * 60 * 60 * 24;
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i + 1).getStartDate().getTime() - list.get(i).getEndDate().getTime() < day) {
String errMsg = "时间区间设置不正确,请确保时间是递增的" + ConstDateUtil.formatDate("yyyy-MM-dd", list.get(i).getEndDate()) + "->" + ConstDateUtil.formatDate("yyyy-MM-dd", list.get(i + 1).getStartDate());
throw new ServiceException(errMsg, ResultCode.CODE_400.getCode());
}
}
}
}