SchedulePlanServiceImpl.java
9.77 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
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
package com.bsth.service.schedule;
import com.bsth.entity.Line;
import com.bsth.entity.schedule.*;
import com.bsth.entity.schedule.rule.ScheduleRule1Flat;
import com.bsth.repository.schedule.SchedulePlanInfoRepository;
import com.bsth.repository.schedule.SchedulePlanRepository;
import com.bsth.service.impl.BaseServiceImpl;
import com.bsth.service.schedule.rules.shiftloop.ScheduleCalcuParam_input;
import com.bsth.service.schedule.rules.shiftloop.ScheduleResult_output;
import com.bsth.service.schedule.rules.shiftloop.ScheduleResults_output;
import com.bsth.service.schedule.rules.shiftloop.ScheduleRule_input;
import com.bsth.service.schedule.rules.strategy.IStrategy;
import com.google.common.collect.Multimap;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
/**
* Created by xu on 16/6/16.
*/
@Service
public class SchedulePlanServiceImpl extends BaseServiceImpl<SchedulePlan, Long> implements SchedulePlanService {
@Autowired
private KieBase kieBase;
@Autowired
private IStrategy strategy;
@Autowired
private SchedulePlanRepository schedulePlanRepository;
@Autowired
private SchedulePlanInfoRepository schedulePlanInfoRepository;
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
@Override
public Map<String, Object> save(SchedulePlan schedulePlan) {
// 1-1、查找线路具体信息
Line xl = strategy.getLine(schedulePlan.getXl().getId());
// 1-2、查出指定线路的所有规则
TTInfo ttInfo = strategy.getTTInfo(xl.getId()).get(0); // 时刻表id
schedulePlan.setTtInfo(ttInfo); // TODO:关联的时刻表,之后改掉
// 2-1、构造drools规则输入数据,输出数据
// 全局计算参数
ScheduleCalcuParam_input scheduleCalcuParam_input = new ScheduleCalcuParam_input(schedulePlan);
// 每个规则对应的输入参数
List<ScheduleRule_input> scheduleRule_inputs = new ArrayList<>();
Iterator<ScheduleRule1Flat> scheduleRule1FlatIterator = strategy.getScheduleRule(xl.getId()).iterator();
while (scheduleRule1FlatIterator.hasNext()) {
ScheduleRule1Flat scheduleRule1Flat_temp = scheduleRule1FlatIterator.next();
ScheduleRule_input scheduleRule_input = new ScheduleRule_input(scheduleRule1Flat_temp);
scheduleRule_inputs.add(scheduleRule_input);
}
// 规则输出数据
ScheduleResults_output scheduleResults_output = new ScheduleResults_output();
// 2-2、构造drools session->载入数据->启动规则->计算->销毁session
// 创建session,内部配置的是stateful
KieSession session = kieBase.newKieSession();
// 设置gloable对象,在drl中通过别名使用
session.setGlobal("scheduleResult", scheduleResults_output);
// 载入数据
session.insert(scheduleCalcuParam_input);
for (ScheduleRule_input scheduleRule_input : scheduleRule_inputs) {
session.insert(scheduleRule_input);
}
// 执行rule
session.fireAllRules();
// 执行完毕销毁,有日志的也要关闭
session.dispose();
System.out.println(scheduleResults_output.showGuideboardDesc1());
// 2-3、如果排班的数据之前已经有了,删除之前的数据
schedulePlanInfoRepository.deleteByXlAndScheduleDateGreaterThanEqualAndScheduleDateLessThanEqual(
xl.getId(), schedulePlan.getScheduleFromTime(), schedulePlan.getScheduleToTime()
);
// 3、根据规则返回,组合最后的输出数据
// 3-1、根据注入的策略服务,获取原始数据
Map<Date, Multimap<Long, TTInfoDetail>> gbdTTinfoMaps = strategy.getGuideboardXlTTInfoDetailMaps(
xl.getId(), schedulePlan.getScheduleFromTime(), schedulePlan.getScheduleToTime());
Map<Long, CarConfigInfo> carConfigMaps = strategy.getCarConfigMaps(xl.getId()); // 车辆配置对应车辆信息
Map<Long, EmployeeConfigInfo> employeeConfigMaps = strategy.getEmployeeConfigMaps(xl.getId()); // 人员配置对应的人员信息
// 3-2、循环规则输出
List<SchedulePlanInfo> schedulePlanInfos = new ArrayList<>();
for (ScheduleResult_output scheduleResult_output : scheduleResults_output.getResults()) {
// 车辆配置对应的车辆
CarConfigInfo configInfo = carConfigMaps.get(Long.valueOf(scheduleResult_output.getCarConfigId()));
// 人员配置对应的人员,这里需要分班处理的
List<EmployeeConfigInfo> employeeConfigInfoList = new ArrayList<>();
String[] eids = scheduleResult_output.getEmployeeConfigId().split("-");
for (String eid : eids) {
employeeConfigInfoList.add(employeeConfigMaps.get(Long.valueOf(eid)));
}
// 排班明细(这个要迭代的)
Collection<TTInfoDetail> ttInfoDetails = gbdTTinfoMaps.get(scheduleResult_output.getSd().toDate()).get(
Long.parseLong(scheduleResult_output.getGuideboardId()));
for (TTInfoDetail ttInfoDetail : ttInfoDetails) {
SchedulePlanInfo schedulePlanInfo = new SchedulePlanInfo(
xl,
scheduleResult_output,
ttInfoDetail,
configInfo,
employeeConfigInfoList,
schedulePlan);
schedulePlanInfos.add(schedulePlanInfo);
}
}
// 3-2、保存生成的排班和明细
schedulePlan.getSchedulePlanInfoList().addAll(schedulePlanInfos); // 关联的排班明细信息
return super.save(schedulePlan);
}
@Override
public List<Map<String, Object>> findGroupInfo(Integer xlid, Date scheduleDate) {
List<Object[]> groupInfos = schedulePlanRepository.findGroupInfo(xlid, scheduleDate);
List<Map<String, Object>> ret = new ArrayList<>();
for (Object[] datas : groupInfos) {
// TODO:貌似springdata没有优雅的方式把List<Object[]>转换成List<Map<String, Object>>方法,
// TODO:可能jpa有相关标注,以后找到,此方法就作废
Map<String, Object> map = new HashMap<>();
// 线路名称
map.put("xlName", datas[0]);
// 排班时间
map.put("scheduleDate", datas[1]);
// 路牌名字
map.put("lpName", datas[2]);
// 车辆自编号
map.put("clZbh", datas[3]);
// 出场时间,如果有多个,需要分开
Object ccsj = datas[4];
if (ccsj != null) {
String[] ccsj_array = ((String) ccsj).split(",");
if (ccsj_array.length > 1) {
map.put("ccsj1", ccsj_array[0]);
map.put("ccsj2", ccsj_array[1]);
} else {
map.put("ccsj1", ccsj_array[0]);
}
}
// 驾驶员工号,如果有多个,需要分开
Object jsyGh = datas[5];
if (jsyGh != null) {
String[] jsyGh_array = ((String) jsyGh).split(",");
if (jsyGh_array.length > 1) {
map.put("jsy1Gh", jsyGh_array[0]);
map.put("jsy2Gh", jsyGh_array[1]);
} else {
map.put("jsy1Gh", jsyGh_array[0]);
}
}
// 驾驶员名字,如果有多个,需要分开
Object jsyName = datas[6];
if (jsyName != null) {
String[] jsyName_array = ((String) jsyName).split(",");
if (jsyName_array.length > 1) {
map.put("jsy1Name", jsyName_array[0]);
map.put("jsy2Name", jsyName_array[1]);
} else {
map.put("jsy1Name", jsyName_array[0]);
}
}
// 售票员工号,如果有多个,需要分开
Object spyGh = datas[7];
if (spyGh != null) {
String[] spyGh_array = ((String) spyGh).split(",");
if (spyGh_array.length > 1) {
map.put("spy1Gh", spyGh_array[0]);
map.put("spy2Gh", spyGh_array[1]);
} else {
map.put("spy1Gh", spyGh_array[0]);
}
}
// 售票员名字,如果有多个,需要分开
Object spyName = datas[8];
if (spyName != null) {
String[] spyName_array = ((String) spyName).split(",");
if (spyName_array.length > 1) {
map.put("spy1Name", spyName_array[0]);
map.put("spy2Name", spyName_array[1]);
} else {
map.put("spy1Name", spyName_array[0]);
}
}
// 创建时间
map.put("createDate", datas[9]);
// TODO:可能还有其他字段
ret.add(map);
}
return ret;
}
@Override
public int updateGroupInfo_clinfo(Integer clid, String clZbh, Integer xlid, Date scheduleDate, String lpName) {
return schedulePlanRepository.updateGroupInfo_clinfo(clid, clZbh, xlid, scheduleDate, lpName);
}
}