SchedulePlanServiceImpl.java
11.4 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package com.bsth.service.schedule.impl;
import com.bsth.data.gpsdata_v2.entity.StationRoute;
import com.bsth.entity.Line;
import com.bsth.entity.schedule.SchedulePlan;
import com.bsth.entity.schedule.TTInfo;
import com.bsth.repository.BusinessRepository;
import com.bsth.repository.LineRepository;
import com.bsth.repository.schedule.*;
import com.bsth.service.schedule.SchedulePlanService;
import com.bsth.service.schedule.exception.ScheduleException;
import com.bsth.service.schedule.impl.plan.kBase3.validate.rule.ValidateRuleResult;
import com.bsth.service.schedule.impl.plan.kBase3.validate.timetable.CalcuParam;
import com.bsth.service.schedule.impl.plan.kBase3.validate.timetable.Result;
import com.bsth.service.schedule.impl.plan.DroolsSchedulePlan;
import com.bsth.service.schedule.impl.plan.ScheduleRuleService;
import org.joda.time.DateTime;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
/**
* Created by xu on 16/6/16.
*/
@Service
public class SchedulePlanServiceImpl extends BServiceImpl<SchedulePlan, Long> implements SchedulePlanService {
@Autowired
@Qualifier("coreKBase")
private KieBase coreKBase;
@Autowired
@Qualifier("preKBase")
private KieBase preKBase;
@Autowired
@Qualifier("KBase3")
private KieBase validateKBase;
@Autowired
private ScheduleRule1FlatRepository scheduleRule1FlatRepository;
@Autowired
private TTInfoRepository ttInfoRepository;
@Autowired
private TTInfoDetailRepository ttInfoDetailRepository;
@Autowired
private LineRepository lineRepository;
@Autowired
private CarConfigInfoRepository carConfigInfoRepository;
@Autowired
private GuideboardInfoRepository guideboardInfoRepository;
@Autowired
private EmployeeConfigInfoRepository employeeConfigInfoRepository;
@Autowired
private BusinessRepository businessRepository;
@Autowired
private ScheduleRuleService scheduleRuleService;
@Autowired
private RerunRuleRepository rerunRuleRepository;
@Autowired
private JdbcTemplate jdbcTemplate;
/** 日志记录器 */
private Logger logger = LoggerFactory.getLogger(SchedulePlanServiceImpl.class);
/** 线路独占锁 */
private ConcurrentHashMap<Integer, ReentrantLock> lineXLatchMaps = new ConcurrentHashMap<>();
private ReentrantLock getLineXLatch(Integer xlid) {
ReentrantLock lineXLatch = lineXLatchMaps.get(xlid);
if (lineXLatch == null) {
lineXLatch = new ReentrantLock();
ReentrantLock pre = lineXLatchMaps.putIfAbsent(xlid, lineXLatch);
if (pre != null) {
lineXLatch = pre;
}
}
return lineXLatch;
}
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public SchedulePlan save(SchedulePlan schedulePlan) {
ReentrantLock lineXLatch = getLineXLatch(schedulePlan.getXl().getId());
try {
if (lineXLatch.tryLock(1, TimeUnit.SECONDS)) {
// pre、如果排班的数据之前已经有了,删除之前的数据
Date startPre = new Date();
scheduleRuleService.deleteSchedulePlanInfo(
schedulePlan.getXl().getLineCode(),
schedulePlan.getScheduleFromTime(),
schedulePlan.getScheduleToTime());
Date endPre = new Date();
logger.info("删除数据 {} ms --->", endPre.getTime() - startPre.getTime());
// core、生成排班计划
DroolsSchedulePlan droolsSchedulePlan = new DroolsSchedulePlan(
schedulePlan,
lineRepository, scheduleRule1FlatRepository,
ttInfoRepository, ttInfoDetailRepository,
carConfigInfoRepository, employeeConfigInfoRepository,
rerunRuleRepository, businessRepository,
scheduleRuleService,
preKBase, coreKBase,
logger
);
droolsSchedulePlan.generatePlan();
return new SchedulePlan();
} else {
throw new ScheduleException("当前线路正在排班,请稍后再操作...");
}
} catch (Exception exp) {
throw new RuntimeException(exp);
} finally {
try {
lineXLatch.unlock();
} catch (Exception exp2) {
}
}
}
@Override
public void delete(Long aLong) throws ScheduleException {
scheduleRuleService.deleteSchedulePlanAll(aLong);
}
@Override
public SchedulePlan findSchedulePlanTommorw() {
DateTime today = new DateTime(new Date());
DateTime tommorw = new DateTime(
today.getYear(), today.getMonthOfYear(),
today.getDayOfMonth(), 0, 0).plusDays(1);
Map<String, Object> param = new HashMap<>();
param.put("scheduleFromTime_le", tommorw.toDate());
param.put("scheduleToTime_ge", tommorw.toDate());
Iterator<SchedulePlan> schedulePlanIterator = this.list(param).iterator();
if (schedulePlanIterator.hasNext()) {
return schedulePlanIterator.next();
}
return null;
}
@Override
public Result validateTTInfo(Integer xlid, Date from, Date to) {
// 构造drools session->载入数据->启动规则->计算->销毁session
// 创建session,内部配置的是stateful
KieSession session = validateKBase.newKieSession();
// 设置gloable对象,在drl中通过别名使用
session.setGlobal("log", logger);
session.setGlobal("tTInfoDetailRepository", ttInfoDetailRepository);
Result rs = new Result(); // 输出gloable对象
session.setGlobal("rs", rs);
// 载入数据
Line line = lineRepository.findOne(xlid);
session.insert(line);
CalcuParam calcuParam = new CalcuParam(
new DateTime(from), new DateTime(to), xlid);
session.insert(calcuParam);
List<TTInfo> ttInfos = ttInfoRepository.findByXlId(xlid);
for (TTInfo ttInfo: ttInfos) {
session.insert(ttInfo);
}
// 执行rule
session.fireAllRules();
// 执行完毕销毁,有日志的也要关闭
session.dispose();
return rs;
}
@Override
public ValidateRuleResult validateRule(Integer xlId, Date from, Date to) {
KieSession session = validateKBase.newKieSession();
session.setGlobal("LOG", logger);
session.setGlobal("ccRepo", carConfigInfoRepository);
session.setGlobal("lpRepo", guideboardInfoRepository);
session.setGlobal("ecRepo", employeeConfigInfoRepository);
session.setGlobal("ruleRepo", scheduleRule1FlatRepository);
ValidateRuleResult result = new ValidateRuleResult();
session.setGlobal("result", result);
com.bsth.service.schedule.impl.plan.kBase3.validate.rule.CalcuParam calcuParam =
new com.bsth.service.schedule.impl.plan.kBase3.validate.rule.CalcuParam();
calcuParam.setXlId(xlId);
calcuParam.setFromDate(new DateTime(from));
calcuParam.setToDate(new DateTime(to));
session.insert(calcuParam);
session.fireAllRules();
session.dispose();;
return result;
}
public Map<String,Object> listwest(Map map){
SimpleDateFormat sfd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String line = map.get("line").toString();
String page = map.get("page").toString();
Map<String, Object> modelMap = new HashMap<String, Object>();
String fgs="";
if(map.get("fgsdm")!=null){
fgs=map.get("fgsdm").toString();
}
String sql ="SELECT sp.xl as xl,MAX(sp.tt_info_names) as tt_info_name, " +
"MAX(sp.schedule_from_time) as from_time ,MAX(sp.schedule_to_time) as to_time, " +
"MAX(line.name) as name , MAX(us.user_name) as uname, " +
"MAX(sp.update_date) as updata_date,max(bu.business_name) as business_name " +
"from bsth_c_s_sp sp " +
"LEFT JOIN bsth_c_line line on sp.xl = line.line_code " +
"left join bsth_c_sys_user us on sp.update_by = us.id " +
"LEFT JOIN bsth_c_business bu on line.branche_company = bu.business_code " +
"where line.name not like '%测试线路%' and sfyy = '1' " +
"and line.destroy = '0' " ;
if(!fgs.equals(" ") && !fgs.equals("")){
sql +=" and line.branche_company = '"+fgs+"'";
}
if(!line.equals(" ") && !line.equals("")){
sql += "and sp.xl="+line;
}
sql += " GROUP BY sp.xl ORDER BY MAX(sp.schedule_from_time);";
List<Map> list = null;
try {
list = jdbcTemplate.query(sql, new RowMapper<Map>() {
@Override
public Map mapRow(ResultSet rs, int rowNum) throws SQLException {
Map map = new HashMap();
map.put("xl", rs.getString("xl"));
map.put("ttInfoName", rs.getString("tt_info_name"));
map.put("fromTime",rs.getDate("from_time"));
map.put("toTime", rs.getDate("to_time"));
map.put("name", rs.getString("name"));
map.put("uName", rs.getString("uname"));
map.put("updataDate", rs.getString("updata_date"));
map.put("businessName", rs.getString("business_name"));
return map;
}
});
for (Map m : list) {
m.put("updataDate", sfd.format(sfd.parse(m.get("updataDate").toString())));
}
}catch (Exception e){
e.printStackTrace();
}
//分页
if(page.length() != 0 ){
int currPage = Integer.valueOf(page);
modelMap.put("totalPage", list.size()%10>0?(list.size()/10 + 1):list.size()/10);
// modelMap.put("dataList", resList.subList(currPage*10,((currPage+1)*10)>=resList.size()?resList.size():(currPage+1)*10));
modelMap.put("list", list);
} else {
modelMap.put("dataList", list);
}
return modelMap;
}
}