StationRouteServiceImpl.java
52.5 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
package com.bsth.service.impl;
import com.bsth.common.ResponseCode;
import com.bsth.entity.*;
import com.bsth.entity.search.CustomerSpecs;
import com.bsth.repository.*;
import com.bsth.service.StationRouteService;
import com.bsth.util.*;
import com.bsth.util.Geo.GeoUtils;
import com.bsth.util.Geo.Point;
import com.bsth.util.db.DBUtils_MS;
import com.google.common.base.Splitter;
import org.apache.commons.lang3.StringUtils;
import org.geolatte.geom.Polygon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.DecimalFormat;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
*
* @ClassName: StationRouteServiceImpl(站点路由service业务层实现类)
*
* @Extends : BaseService
*
* @Description: TODO(站点路由service业务层)
*
* @Author bsth@lq
*
* @Date 2016年5月03日 上午9:21:17
*
* @Version 公交调度系统BS版 0.1
*
*/
@Service
public class StationRouteServiceImpl extends BaseServiceImpl<StationRoute, Integer> implements StationRouteService {
@Value("${path.speech.common}")
private String commonPath;
@Value("${path.speech.line}")
private String linePathPattern;
@Autowired
private StationRouteRepository stationRouteRepository;
@Autowired
private SectionRouteRepository sectionRouteRepository;
@Autowired
private LineRepository lineRepository;
@Autowired
private StationRepository stationRepository;
@Autowired
private BusinessRepository businessRepository;
@Autowired
private LsStationRouteRepository lsStationRouteRepository;
@Autowired
private LsSectionRouteRepository lsSectionRouteRepository;
@Autowired
private LineRegionRepository lineRegionRepository;
@Autowired
private LineVersionsRepository lineVersionsRepository;
@Override
public Iterable<StationRoute> list(Map<String, Object> map) {
List<Sort.Order> orders = new ArrayList<>();
orders.add(new Sort.Order(Direction.ASC, "directions"));
orders.add(new Sort.Order(Direction.ASC, "stationRouteCode"));
return stationRouteRepository.findAll(new CustomerSpecs<>(map), Sort.by(orders));
}
@Override
public Map<String, Object> getSectionRouteExport(Integer id, HttpServletResponse resp) {
Map<String, Object> resultMap = new HashMap<String, Object>();
try {
// List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
Map<String,Object> resultExcel = new HashMap<String,Object>();//导出参数的对象
/* 添加表头*/
List<String> title = new ArrayList<String>();
title.add("线路ID");
title.add("方向");
title.add("站点编码");
title.add("站点顺序号");
title.add("站点备注");
title.add("站点名称");
title.add("站点距离(km)");
title.add("站点时长(min)");
title.add("线路名称");
resultExcel.put("title", title);
/* 添加表单*/
Map<String,List<String>> temp = new HashMap<String,List<String>>();
List<StationRoute> strtionList = stationRouteRepository.findStationExport(id);
if(strtionList == null){
logger.info("没有数据导,出用户信息失败!");
} else {
for (int i = 0; i < strtionList.size(); i++) {
StationRoute station = strtionList.get(i);
List<String> varList = new ArrayList<String>();
varList.add(station.getLine().getId().toString());
varList.add(station.getDirections().toString());
varList.add(station.getStationCode());
varList.add(station.getStationRouteCode().toString());
varList.add(station.getStationMark());
varList.add(station.getStationName());
varList.add(station.getDistances().toString());
varList.add(station.getToTime().toString());
varList.add(station.getLine().getName());
temp.put((i+1)+"", varList);
}
}
resultExcel.put("content", temp);
ExcelUtil excelUtil = new ExcelUtil();
excelUtil.buildExcelDocument(resultExcel, strtionList.get(0).getLine().getName()+"线路站点",resp);
resultMap.put("status", ResponseCode.SUCCESS);
} catch (Exception e) {
resultMap.put("status", ResponseCode.ERROR);
logger.error("save erro.", e);
}
return resultMap;
}
/**
* @Description :TODO(查询树站点与路段数据)
*
* @param map <line.id_eq:线路ID; directions_eq:方向>
*
* @return List<Map<String, Object>>
*/
@Override
public Map<String, Object> findRoutes(Map<String, Object> map) {
Map<String, Object> result = new HashMap<>();
List<StationRoute> stationList = stationRouteRepository.findAll(new CustomerSpecs<>(map), Sort.by(Direction.ASC, "directions", "stationRouteCode"));
List<SectionRoute> sectionList = sectionRouteRepository.findAll(new CustomerSpecs<>(map), Sort.by(Direction.ASC, "directions", "sectionrouteCode"));
result.put("stationRoutes", stationList);
result.put("sectionRoutes", sectionList);
return result;
}
@Override
public Map<String, Object> systemQuote(Map<String, Object> map) {
Map<String, Object> resultmap = new HashMap<>();
try{
StationRoute route = new StationRoute();
Integer lineId = map.get("lineId").equals("") ? null : Integer.parseInt(map.get("lineId").toString());
Integer stationId = map.get("stationId").equals("") ? null : Integer.parseInt(map.get("stationId").toString());
Line line = lineRepository.findById(lineId).get();
Station station = stationRepository.findById(stationId).get();
route.setLine(line);
route.setStation(station);
//baseRepository.save(t);
resultmap.put("status", ResponseCode.SUCCESS);
}catch(Exception e){
resultmap.put("status", ResponseCode.ERROR);
logger.error("save erro.", e);
}
return resultmap;
}
/**
* @Description :TODO(查询线路某方向下的站点序号与类型)
*
* @param map <lineId:线路ID; direction:方向;stationRouteCode:站点编码>
*
* @return List<Map<String, Object>>
*/
@Override
public List<Map<String, Object>> findUpStationRouteCode(Map<String, Object> map) {
Integer lineId = map.get("lineId").equals("") ? null : Integer.parseInt(map.get("lineId").toString());
Integer direction = map.get("direction").equals("") ? null : Integer.parseInt(map.get("direction").toString());
Integer stationRouteCode = map.get("stationRouteCode").equals("") ? null : Integer.parseInt(map.get("stationRouteCode").toString());
List<Object[]> reslutList = stationRouteRepository.findUpStationRouteCode(lineId, direction, stationRouteCode);
List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
if(reslutList.size()>0) {
for(int i = 0 ; i <reslutList.size() ;i++){
Map<String, Object> tempM = new HashMap<String, Object>();
tempM.put("stationRouteCode", reslutList.get(i)[0]);
tempM.put("stationRouteMarke", reslutList.get(i)[1]);
list.add(tempM);
}
}
return list;
}
private void traversalStation(List<LsStationRoute> stationRoutes, List<Map<String, Object>> resultList, int len) {
for(int i = 0 ; i < len; i++) {
LsStationRoute stationRoute = stationRoutes.get(i);
Map<String, Object> tempM = new HashMap<String,Object>();
tempM.put("stationRouteLine", stationRoute.getLine().getId());
tempM.put("stationRouteStation", stationRoute.getStation().getId());
tempM.put("stationRouteCode", stationRoute.getStationRouteCode());
tempM.put("stationRouteLIneCode", stationRoute.getLineCode());
tempM.put("stationRouteStationMark", stationRoute.getStationMark());
tempM.put("stationOutStationNmber", stationRoute.getOutStationNmber());
tempM.put("stationRoutedirections", stationRoute.getDirections());
tempM.put("stationRouteDistances", stationRoute.getDistances());
tempM.put("stationRouteToTime", stationRoute.getToTime());
tempM.put("staitonRouteFirstTime", stationRoute.getFirstTime());
tempM.put("stationRouteEndTime", stationRoute.getEndTime());
tempM.put("stationRouteDescriptions", stationRoute.getDescriptions());
tempM.put("stationRouteDestroy", stationRoute.getDestroy());
tempM.put("stationRouteVersions", stationRoute.getVersions());
tempM.put("stationRouteCreateBy", stationRoute.getCreateBy());
tempM.put("stationRouteCreateDate", stationRoute.getCreateDate());
tempM.put("stationRouteUpdateBy", stationRoute.getUpdateBy());
tempM.put("stationRouteUpdateDate", stationRoute.getUpdateDate());
tempM.put("stationId", stationRoute.getStation().getId());
tempM.put("stationCode", stationRoute.getStation().getStationCode());
tempM.put("stationRouteName", stationRoute.getStationName());
tempM.put("stationRoadCoding", stationRoute.getStation().getRoadCoding());
tempM.put("stationJwpoints", stationRoute.getStation().getCenterPoint().toString());
CoordinateConverter.Location location = CoordinateConverter.LocationMake(stationRoute.getStation().getCenterPointWgs().toString());
tempM.put("stationGlonx", location.getLng());
tempM.put("stationGlaty", location.getLat());
Polygon polygon = stationRoute.getBufferPolygon(), polygonWgs = stationRoute.getBufferPolygonWgs();
tempM.put("stationBPolyonGrid", polygon == null ? "" : polygon.toString());
tempM.put("stationGPloyonGrid", polygonWgs == null ? "" : polygonWgs.toString());
tempM.put("stationDestroy", stationRoute.getStation().getDestroy());
tempM.put("stationRadius", stationRoute.getRadius());
tempM.put("stationShapesType", stationRoute.getShapedType());
tempM.put("stationVersions", stationRoute.getStation().getVersions());
tempM.put("sttationDescriptions", stationRoute.getStation().getDescriptions());
tempM.put("stationCreateBy", stationRoute.getStation().getCreateBy());
tempM.put("stationCreateDate", stationRoute.getStation().getCreateDate());
tempM.put("stationUpdateBy", stationRoute.getStation().getUpdateBy());
tempM.put("stationUpdateDate", stationRoute.getStation().getUpdateDate());
tempM.put("stationRouteId", stationRoute.getId());
tempM.put("zdmc", stationRoute.getStationName());
// 行业编码
tempM.put("industryCode", stationRoute.getIndustryCode());
try {
tempM.put("stationNameEn", stationRoute.getStationNameEn());
} catch (Exception e) {
e.printStackTrace();
}
resultList.add(tempM);
}
}
/**
* @Description :TODO(查询线路某方向下所有站点的中心百度坐标)
*
* @param map <lineId:线路ID; direction:方向>
*
* @return List<Map<String, Object>>
*/
@Override
public List<Map<String, Object>> getStationRouteCenterPoints(Map<String, Object> map) {
List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();
// 线路ID
Integer lineId = map.get("lineId").equals("") ? null : Integer.parseInt(map.get("lineId").toString());
// 方向
Integer direction = map.get("direction").equals("") ? null : Integer.parseInt(map.get("direction").toString());
List<Object[]> list = stationRouteRepository.getSelectStationRouteCenterPoints(lineId, direction);
if(list.size()>0) {
for(int i = 0;i<list.size();i++) {
Map<String, Object> tempM = new HashMap<String,Object>();
tempM.put("bJwpoints", list.get(i)[0]);
tempM.put("stationName", list.get(i)[1]);
resultList.add(tempM);
}
}
return resultList;
}
/**
* @Description :TODO(查询线路某方向下所有站点)
*
* @param map <lineId:线路ID; direction:方向>
*
* @return List<Map<String, Object>>
*/
@Override
public List<Map<String, Object>> getStationRouteList(Map<String, Object> map) {
if (map.get("line.id_eq") == null || map.get("directions_eq") == null || map.get("versions_eq") == null) {
throw new IllegalArgumentException("需正确传入线路、方向、版本参数");
}
map.put("destroy_eq", 0);
List<LsStationRoute> stationRoutes = lsStationRouteRepository.findAll(new CustomerSpecs<>(map), Sort.by(Direction.ASC, "stationRouteCode"));
List<Map<String, Object>> resultList = new ArrayList<>();
int len = stationRoutes.size();
if(len > 0) {
// 遍历站点
traversalStation(stationRoutes, resultList, len);
}
return resultList;
}
/**
* @Description :TODO(撤销站点)
*
* @param map <lineId:线路ID; destroy:是否撤销(0:否;1:是)>
*
* @return Map<String, Object> <SUCCESS ; ERROR>
*/
@Override
public Map<String, Object> stationRouteIsDestroy(Map<String, Object> map) {
Map<String, Object> resultMap = new HashMap<String,Object>();
try {
Integer stationRouteId = map.get("stationRouteId").equals("") ? 0 : Integer.parseInt(map.get("stationRouteId").toString());
Integer destroy = map.get("destroy").equals("") ? 0 : Integer.parseInt(map.get("destroy").toString());
lsStationRouteRepository.deleteById(stationRouteId);
resultMap.put("status", ResponseCode.SUCCESS);
} catch (Exception e) {
resultMap.put("status", ResponseCode.ERROR);
logger.error("save erro.", e);
}
return resultMap;
}
/**
* @Description : TODO(根据线路ID生成行单)
*
* @param map <lineId:线路ID>
*
* @return Map<String, Object> <SUCCESS ; ERROR ; NOTDATA>
*/
@Override
public Map<String, Object> usingSingle(Map<String, Object> map) {
// 返回值map
Map<String, Object> resultMap = new HashMap<String,Object>();
try {
// 获取线路ID
Integer lineId = map.get("lineId").equals("") ? 0 : Integer.parseInt(map.get("lineId").toString());
Integer version = lineVersionsRepository.findCurrentVersion(lineId);
Map<String, Object> param = new HashMap<>();
param.put("line_eq", lineId);
param.put("version_eq", version);
/** 查询线路信息 @param:<lineId:线路ID> */
Line line = lineRepository.findById(lineId).get();
Business company = businessRepository.findByBusinessCode(line.getCompany()).get(0);
Integer fileVersions = lineRepository.findfileVersions(lineId);
if(fileVersions == null) {
lineRepository.addFileVersions(line.getId(), line.getLineCode());
fileVersions = 1;
} else {
fileVersions = fileVersions + 1;
lineRepository.editFileVersions(line.getId(),fileVersions);
}
// Integer fileVersions = map.get("fileVersions").equals("") ? 1 : Integer.parseInt(map.get("fileVersions").toString());// 没有输入就默认1
/** 查询线路信息下的站点路由信息 @param:<lineId:线路ID> */
List<Object[]> objects = stationRouteRepository.usingSingle(lineId);
List<LineRegion> lineRegions = lineRegionRepository.findAll(new CustomerSpecs<>(param));
if (objects.size()>0) {
// 报站音频
Set<String> languages = new HashSet<>();
languages.add("cn");
languages.add("sh");
languages.add("en");
ttsAndZip(objects, line, languages, lineRegions);
/** 获取配置文件里的ftp登录参数 */
Map<String, Object> FTPParamMap = readPropertiesGetFTPParam();
// 压缩文件名
String odlGzFileName = line.getLineCode() + ".txt.gz";
// txt文件名
String textFileName = line.getLineCode() + ".txt";
// 创建一个ftp上传实例
FTPClientUtils clientUtils = new FTPClientUtils();
// IP
String url = FTPParamMap.get("url").toString();
// 端口
int port = Integer.valueOf(FTPParamMap.get("port").toString());
// 用户名
String username = FTPParamMap.get("username").toString();
// 密码
String password = FTPParamMap.get("password").toString();
// 相对路径
String remotePath = FTPParamMap.get("remotePath").toString();
/** 如果已存在相同行单文件名则先删除 */
clientUtils.deleteFtpFile(url, port, username, password, remotePath, odlGzFileName);
clientUtils.deleteFtpFile(url, port, username, password, remotePath, textFileName);
clientUtils.deleteFtpFile(url, port, username, password, remotePath + "/voice/", textFileName);
String textStr = "";
// boolean tempTag = ishxType(objects);
Integer linePlayType = line.getLinePlayType() == null ? -1:line.getLinePlayType();
if(linePlayType == 1)
textStr = hxTextFileToFtp(objects,lineId);// 环线行单文件内容
else if (linePlayType == 0)
textStr = newTextFileToFTP(objects,lineId);/** 双向行单文件内容 @param:<objects:站点路由;lineId:线路ID>*/
else
resultMap.put("status","NOLinePlayType");// 线路无线路规划类型
textStr = line.getName() + " " + fileVersions + "\r\n" + textStr;
InputStream input = new ByteArrayInputStream(textStr.getBytes("gbk"));
/** 生成txt文件,上传ftp */
clientUtils.uploadFile(url, port, username, password, remotePath, textFileName, input);
// 创建打包实例
PackTarGZUtils packTarGZUtils= new PackTarGZUtils();
/** 获取txt文件 */
File textFile = clientUtils.GetFtpFile(url, port, username, password, remotePath, textFileName);
File target = new File(odlGzFileName);
// 将txt文件打包
File targetFile = PackTarGZUtils.compress(textFile, target);
clientUtils.FTPUpLoadFromDisk(targetFile, targetFile.getName(), url, port, username, password, remotePath);
// 删除文件
textFile.delete();
targetFile.delete();
textStr = newTextVoiceFileToFTP(objects,lineId);
String lineName = line.getName();
/*try {
lineName = Integer.parseInt(line.getName().replace("路", "")) + "";
} catch (Exception e) {
}*/
String head = lineName + " " + fileVersions + " " + line.getStartStationFirstTime() + "-" + line.getStartStationEndTime() + " " + line.getEndStationFirstTime() + "-" + line.getEndStationEndTime() + " " + line.getTicketPrice() + " " + company.getBusinessName().replace("公司", "公交") + " " + (company.getPhoneNum()== null ? "-" : company.getPhoneNum());
textStr = head +"\r\n" + textStr;
input = new ByteArrayInputStream(textStr.getBytes("gbk"));
// 线路文件上传(全程)
clientUtils.uploadFile(url, port, username, password, remotePath + "/voice/", textFileName, input);
String linePath = String.format(linePathPattern, lineId), voicePath = String.format("%s%s.zip", linePath, lineId);
// 报站文件上传(全程)
clientUtils.deleteFtpFile(url, port, username, password, remotePath + "/voice/", String.format("%s.zip", line.getLineCode()));
clientUtils.FTPUpLoadFromDisk(new File(voicePath), String.format("%s.zip", line.getLineCode()), url, port, username, password, remotePath + "/voice/");
// 线路区间
if (lineRegions.size() > 0) {
FTPClientUtils.deleteFileByPrefix(String.format("%s-", line.getLineCode()), url, port, username, password, String.format("%s/voice/", remotePath));
for (LineRegion lineRegion : lineRegions) {
voicePath = String.format("%s%s-%d.zip", linePath, lineId, lineRegion.getSeq());
textStr = String.format("%s\r\n%s", head, subLine2Ftp(lineRegion));
input = new ByteArrayInputStream(textStr.getBytes("gbk"));
clientUtils.uploadFile(url, port, username, password, remotePath + "/voice/", String.format("%s-%d.txt", line.getLineCode(), lineRegion.getSeq()), input);
clientUtils.deleteFtpFile(url, port, username, password, remotePath + "/voice/", String.format("%s-%d.zip", line.getLineCode(), lineRegion.getSeq()));
clientUtils.FTPUpLoadFromDisk(new File(voicePath), String.format("%s-%d.zip", line.getLineCode(), lineRegion.getSeq()), url, port, username, password, remotePath + "/voice/");
}
}
resultMap.put("status", ResponseCode.SUCCESS);
}else {
resultMap.put("status","NOTDATA");
}
} catch (Exception e) {
resultMap.put("status", ResponseCode.ERROR);
logger.error("save erro.", e);
} finally {
return resultMap;
}
}
/**
* @Description : TODO(形成行单文件内容)
*
* @param objects :站点路由信息
*
* {[0]:g_lonx(GPS经度);[1]:g_laty(GPS纬度);[2]:b_jwpoints(百度经纬度坐标)
*
* [3]:station_mark(站点类型);[4]:station_route_code(站点序号);[5]:station_cod(站点编码);
*
* [6]:distances(站点距离);[7]:station_name(站点名称);[8]:directions(方向)}
*
* @param lineId :线路ID
*
* @return String
*/
public String newTextFileToFTP(List<Object[]> objects,Integer lineId) {
// 返回值String
String stationRStr = "";
// windows下的文本文件换行符
//String enterStr = "\r\n";
// linux/unix下的文本文件换行符
String enterStr = "\r";
int defaultZdxh = 0;
if(objects.size()>0) {
for(int i = 0; i<objects.size();i++) {
defaultZdxh ++ ;
// 经度
String lng = objects.get(i)[0].equals("") ? "0" : objects.get(i)[0].toString();
// 纬度
String lat = objects.get(i)[1].equals("") ? "0" : objects.get(i)[1].toString();
Point point = new Point(Double.valueOf(lng), Double.valueOf(lat));
lat = "\t" + lat;
// 站点类型
String stationMakeStr = objects.get(i)[3].equals("") ? "" : objects.get(i)[3].toString();
String stationMake = "";
if(stationMakeStr.equals("E")) {
stationMake = "\t2";
}else {
stationMake ="\t1";
}
// 站点序号
// String stationNo = objects.get(i)[4].equals("") ? "" : objects.get(i)[4].toString();
String stationNo = String.valueOf(defaultZdxh);
stationNo = "\t" + stationNo;
// 站点编码
String stationCode = objects.get(i)[5].equals("") ? "" : objects.get(i)[5].toString();
int len = stationCode.length();
if(len<8) {
int dx = 8 - len;
String addStr = "";
for(int p =0;p<dx;p++) {
addStr = addStr + "0";
}
stationCode = addStr + stationCode;
}else if(len>8){
stationCode = stationCode.substring(8);
}
stationCode = "\t" +stationCode;
double dis = objects.get(i)[6]==null ? 0.0 : Double.parseDouble(objects.get(i)[6].toString())*1000;
String tempDistc = String.valueOf((int) dis);
// 站点距离
String staitondistance = "\t" + tempDistc;
// 站点名称
String stationName = objects.get(i)[7].equals("") ? "" : objects.get(i)[7].toString();
stationName = "\t" +stationName;
// 限速
// String sleepStr = " " + "60";
// 限速
String sleepStr = "";
// 方向
int directions = objects.get(i)[8]==null ? null : Integer.valueOf(objects.get(i)[8].toString());
/** 获取路段路由信息 @pararm:<lineId:线路ID;directions:方向> */
List<Object[]> sobje = sectionRouteRepository.sectionRouteVector(lineId,directions);
if(sobje.size()==1) {
double dsleepStrt = sobje.get(0)[2] == null ? 60d : Double.valueOf(sobje.get(0)[2].toString());
sleepStr = "\t" + new DecimalFormat("0").format(dsleepStrt);
// int dsleepStr = sobje.get(0)[2] == null || sobje.get(0)[2].equals("") ? 60 : Integer.valueOf(sobje.get(0)[2].toString());
// sleepStr = "\t" + String.valueOf(dsleepStr);
}else if(sobje.size()>1){
for(int j =0;j<sobje.size();j++) {
double dsleepStrt = sobje.get(j)[2] == null || sobje.get(j)[2].equals("") ? 60d : Double.valueOf(sobje.get(j)[2].toString());
String pointsStr = sobje.get(j)[1]==null || sobje.get(j)[1].equals("") ? null : sobje.get(j)[1].toString();
pointsStr = pointsStr.substring(11, pointsStr.length()-1);
List<Point> ps = new ArrayList<>();
String[] pArray = pointsStr.split(",");
for(int a = 0; a <pArray.length; a++) {
String[] tmepA = pArray[a].split(" ");
Point temp = new Point(Double.valueOf(tmepA[0]), Double.valueOf(tmepA[1]));
ps.add(temp);
}
if(GeoUtils.isInSection(ps, point)) {
sleepStr = "\t" + String.valueOf((int)dsleepStrt);
break;
}
}
}
if(sleepStr.equals(""))
sleepStr = "\t" + "60";
stationRStr = stationRStr + lng + lat + stationMake + stationNo + stationCode + staitondistance + sleepStr + stationName + enterStr;
}
}
return stationRStr;
}
public String newTextVoiceFileToFTP(List<Object[]> objects,Integer lineId) {
// 返回值String
String stationRStr = "";
// windows下的文本文件换行符
String enterStr = "\r\n";
// linux/unix下的文本文件换行符
// String enterStr = "\r";
int defaultZdxh = 0;
if(objects.size()>0) {
for(int i = 0; i<objects.size();i++) {
defaultZdxh ++ ;
// 经度
String lng = objects.get(i)[0].equals("") ? "0" : objects.get(i)[0].toString();
// 纬度
String lat = objects.get(i)[1].equals("") ? "0" : objects.get(i)[1].toString();
Point point = new Point(Double.valueOf(lng), Double.valueOf(lat));
lat = "\t" + lat;
// 站点类型
String stationMakeStr = objects.get(i)[3].equals("") ? "" : objects.get(i)[3].toString();
String stationMake = "";
if(stationMakeStr.equals("E")) {
stationMake = "\t2";
}else {
stationMake ="\t1";
}
// 站点序号
// String stationNo = objects.get(i)[4].equals("") ? "" : objects.get(i)[4].toString();
String stationNo = String.valueOf(defaultZdxh);
stationNo = "\t" + stationNo;
// 站点编码
String stationCode = objects.get(i)[5].equals("") ? "" : objects.get(i)[5].toString();
int len = stationCode.length();
if(len<8) {
int dx = 8 - len;
String addStr = "";
for(int p =0;p<dx;p++) {
addStr = addStr + "0";
}
stationCode = addStr + stationCode;
}else if(len>8){
stationCode = stationCode.substring(8);
}
stationCode = "\t" +stationCode;
double dis = objects.get(i)[6]==null ? 0.0 : Double.parseDouble(objects.get(i)[6].toString())*1000;
String tempDistc = String.valueOf((int) dis);
// 站点距离
String staitondistance = "\t" + tempDistc;
// 站点名称
String stationName = objects.get(i)[7].equals("") ? " " : objects.get(i)[7].toString();
String stationNameEn = " ";
if(objects.get(i)[9] != null){
stationNameEn = objects.get(i)[9].equals("") ? " " : objects.get(i)[9].toString();
}
stationName = "\t" +stationName;
stationNameEn = "\t" +stationNameEn;
// 限速
// String sleepStr = " " + "60";
// 限速
String sleepStr = "";
// 方向
int directions = objects.get(i)[8]==null ? null : Integer.valueOf(objects.get(i)[8].toString());
if (directions == 1) {
stationName = stationName.replaceAll("\\(起点站\\)", "").replaceAll("\\(终点站\\)", "").replaceAll("(起点站)", "").replaceAll("(终点站)", "");
}
/** 获取路段路由信息 @pararm:<lineId:线路ID;directions:方向> */
List<Object[]> sobje = sectionRouteRepository.sectionRouteVector(lineId,directions);
if(sobje.size()==1) {
double dsleepStrt = sobje.get(0)[2] == null ? 60d : Double.valueOf(sobje.get(0)[2].toString());
sleepStr = "\t" + new DecimalFormat("0").format(dsleepStrt);
// int dsleepStr = sobje.get(0)[2] == null || sobje.get(0)[2].equals("") ? 60 : Integer.valueOf(sobje.get(0)[2].toString());
// sleepStr = "\t" + String.valueOf(dsleepStr);
}else if(sobje.size()>1){
for(int j =0;j<sobje.size();j++) {
double dsleepStrt = sobje.get(j)[2] == null || sobje.get(j)[2].equals("") ? 60d : Double.valueOf(sobje.get(j)[2].toString());
String pointsStr = sobje.get(j)[1]==null || sobje.get(j)[1].equals("") ? null : sobje.get(j)[1].toString();
pointsStr = pointsStr.substring(11, pointsStr.length()-1);
List<Point> ps = new ArrayList<>();
String[] pArray = pointsStr.split(",");
for(int a = 0; a <pArray.length; a++) {
String[] tmepA = pArray[a].split(" ");
Point temp = new Point(Double.valueOf(tmepA[0]), Double.valueOf(tmepA[1]));
ps.add(temp);
}
if(GeoUtils.isInSection(ps, point)) {
sleepStr = "\t" + String.valueOf((int)dsleepStrt);
break;
}
}
}
if(sleepStr.equals(""))
sleepStr = "\t" + "60";
stationRStr = stationRStr + lng + lat + stationMake + stationNo + stationCode + staitondistance + sleepStr + stationName + stationNameEn + enterStr;
}
}
return stationRStr;
}
public String hxTextFileToFtp(List<Object[]> objects,Integer lineId) {
String restStr = "";
// windows下的文本文件换行符
//String enterStr = "\r\n";
// linux/unix下的文本文件换行符
String enterStr = "\r";
int xh = 1 ;
for(int x =0;x<2;x++) {
for(int i = 0; i<objects.size();i++) {
if(Integer.valueOf(objects.get(i)[8].toString())==0) {
// 经度
String lng = objects.get(i)[0].equals("") ? "0" : objects.get(i)[0].toString();
// 纬度
String lat = objects.get(i)[1].equals("") ? "0" : objects.get(i)[1].toString();
Point point = new Point(Double.valueOf(lng), Double.valueOf(lat));
lat = "\t" + lat;
// 站点类型
String stationMakeStr = objects.get(i)[3].equals("") ? "" : objects.get(i)[3].toString();
String stationMake = "";
if(stationMakeStr.equals("E")) {
stationMake = "\t2";
}else {
stationMake ="\t1";
}
// 站点序号
// String stationNo = objects.get(i)[4].equals("") ? "" : objects.get(i)[4].toString();
String stationNo = "\t" + xh;
// 站点编码
String stationCode = objects.get(i)[5].equals("") ? "" : objects.get(i)[5].toString();
int len = stationCode.length();
if(len<8) {
int dx = 8 - len;
String addStr = "";
for(int p =0;p<dx;p++) {
addStr = addStr + "0";
}
stationCode = addStr + stationCode;
}else if(len>8){
stationCode = stationCode.substring(8);
}
stationCode = "\t" +stationCode;
double dis = objects.get(i)[6]==null ? 0.0 : Double.parseDouble(objects.get(i)[6].toString())*1000;
String tempDistc = String.valueOf((int) dis);
// 站点距离
String staitondistance = "\t" + tempDistc;
// 站点名称
String stationName = objects.get(i)[7].equals("") ? "" : objects.get(i)[7].toString();
stationName = "\t" +stationName;
// 限速
String sleepStr = "";
// 方向
int directions = objects.get(i)[8]==null ? null : Integer.valueOf(objects.get(i)[8].toString());
if (directions == 1) {
stationName = stationName.replaceAll("\\(起点站\\)", "").replaceAll("\\(终点站\\)", "").replaceAll("(起点站)", "").replaceAll("(终点站)", "");
}
/** 获取路段路由信息 @pararm:<lineId:线路ID;directions:方向> */
List<Object[]> sobje = sectionRouteRepository.sectionRouteVector(lineId,directions);
if(sobje.size()==1) {
// int dsleepStr = sobje.get(0)[2] == null || sobje.get(0)[2].equals("") ? 60 : Integer.valueOf(sobje.get(0)[2].toString());
// sleepStr = "\t" + String.valueOf(dsleepStr);
double dsleepStrt = sobje.get(0)[2] == null ? 60d : Double.valueOf(sobje.get(0)[2].toString());
sleepStr = "\t" + new DecimalFormat("0").format(dsleepStrt);
}else if(sobje.size()>1){
for(int j =0;j<sobje.size();j++) {
double dsleepStrt = sobje.get(j)[2] == null || sobje.get(j)[2].equals("") ? 60d : Double.valueOf(sobje.get(j)[2].toString());
String pointsStr = sobje.get(j)[1]==null || sobje.get(j)[1].equals("") ? null : sobje.get(j)[1].toString();
pointsStr = pointsStr.substring(11, pointsStr.length()-1);
List<Point> ps = new ArrayList<>();
String[] pArray = pointsStr.split(",");
for(int a = 0; a <pArray.length; a++) {
String[] tmepA = pArray[a].split(" ");
Point temp = new Point(Double.valueOf(tmepA[0]), Double.valueOf(tmepA[1]));
ps.add(temp);
}
if(GeoUtils.isInSection(ps, point)) {
sleepStr = "\t" + String.valueOf((int)dsleepStrt);
break;
}
}
}
if(sleepStr.equals(""))
sleepStr = "\t" + "60";
xh++;
restStr = restStr + lng + lat + stationMake + stationNo + stationCode + staitondistance + sleepStr + stationName + enterStr;
}
}
}
System.out.println(restStr);
return restStr;
}
public boolean isPointOnPolyline (Map<String, Object> point, List<Map<String, Object>> listMap ){
boolean success = false;
for(int l = 0; l < listMap.size() - 1; l ++){
Map<String, Object> tempM = listMap.get(l);
Map<String, Object> nextTempM = listMap.get(l+1);
if (Double.valueOf(point.get("lng").toString())>= Math.min(Double.valueOf(tempM.get("lng").toString()), Double.valueOf(nextTempM.get("lng").toString())) && Double.valueOf(point.get("lng").toString()) <= Math.max(Double.valueOf(tempM.get("lng").toString()), Double.valueOf(nextTempM.get("lng").toString())) &&
Double.valueOf(point.get("lat").toString()) >= Math.min(Double.valueOf(tempM.get("lat").toString()), Double.valueOf(nextTempM.get("lat").toString())) && Double.valueOf(point.get("lat").toString()) <= Math.max(Double.valueOf(tempM.get("lat").toString()), Double.valueOf(nextTempM.get("lat").toString()))){
double precision = (Double.valueOf(tempM.get("lng").toString()) - Double.valueOf(point.get("lng").toString())) * (Double.valueOf(nextTempM.get("lat").toString()) - Double.valueOf(point.get("lat").toString())) -
(Double.valueOf(nextTempM.get("lng").toString()) - Double.valueOf(tempM.get("lng").toString())) * (Double.valueOf(tempM.get("lat").toString()) - Double.valueOf(nextTempM.get("lat").toString()));
if(precision < 2e-10 && precision > -2e-10){
//实质判断是否接近0
success = true;
}
}
}
return success;
}
/**
* @Description:TOOD(获取FTP登录参数) 这里暂时只做一个map值返回,以后可以作为ftp登录类提出来
*
* @return : Map<String, Object> <url:IP;port:端口;username:用户名;password:密码;remotePath:相对路径>
*/
public Map<String, Object> readPropertiesGetFTPParam(){
// 返回值map
Map<String, Object> resultMap = new HashMap<String, Object>();
Properties env = new Properties();
try {
env.load(DBUtils_MS.class.getClassLoader().getResourceAsStream("ftp.properties"));
resultMap.put("url", env.getProperty("ftp.url"));
resultMap.put("port", env.getProperty("ftp.port"));
resultMap.put("username", env.getProperty("ftp.username"));
resultMap.put("password", env.getProperty("ftp.password"));
resultMap.put("remotePath", env.getProperty("ftp.path"));
} catch (Exception e) {
e.printStackTrace();
}
return resultMap ;
}
@Override
public Map<String, Object> findByMultiLine(String lineIds) {
Map<String, Object> rs = new HashMap<>();
try{
List<String> idx = Splitter.on(',').splitToList(lineIds);
//路由
List<StationRoute> list = new ArrayList<>();
/**
* in 查询符 无法和 @EntityGraph 同时配合使用,这可能是一个bug
* 暂时只能循环单线路查询
*/
//stationRouteRepository.multiLine(idx)
for(String id : idx){
list.addAll(stationRouteRepository.findByLineCode(id));
}
for(StationRoute sr : list){
sr.setLine(null);
}
//过滤部分字段
/*String jsonStr = JSON.toJSONString(list, new PropertyFilter() {
@Override
public boolean apply(Object object, String name, Object value) {
if(name.equals("line"))
return false;
return true;
}
});*/
rs.put("status", ResponseCode.SUCCESS);
rs.put("list", list);
}catch(Exception e){
logger.error("", e);
rs.put("status", ResponseCode.ERROR);
}
return rs;
}
public void matchCode(List<StationRoute> stationRoutes,List<StationMatchData> stationMatchData, List<StationRoute> listMactah){
int listsSize = stationRoutes.size();
if(listsSize > 0 && stationMatchData.size() > 0){
if(stationMatchData.size() == listsSize){
for (int i=0; i<listsSize; i++) {
if(!StringUtils.isEmpty(stationMatchData.get(i).getStationStandardCode())){
stationRoutes.get(i).setIndustryCode(stationMatchData.get(i).getStationStandardCode());
listMactah.add(stationRoutes.get(i));
}
}
} else {
Map<String,String> smdMap = new HashMap<>();
for (int i=0; i<listsSize; i++) {
String name = stationRoutes.get(i).getStationName();
String names[] = null;
if(name.indexOf("(") != -1){
names = name.split("(");
} else if(name.indexOf("(") != -1){
names = name.split("\\(");
}
for (StationMatchData smd:stationMatchData) {
smdMap.put(smd.getStationName(),smd.getStationStandardCode());
String stationName =smd.getStationName();
String stationName2 =smd.getStationName2();
String industryCode =smd.getStationStandardCode();
if(StringUtils.isEmpty(industryCode)){
continue;
}
if(names != null && names.length > 1){
// if(stationRoutes.get(i).getStationMark().equals(smd.getStationType()) && (stationName.indexOf(names[0]) != -1 || stationName.indexOf(names[1].substring(0,names[1].length()-1)) != -1 || stationName2.indexOf(names[0]) != -1 || stationName2.indexOf(names[1].substring(0,names[1].length()-1)) != -1)){
if((stationName.indexOf(names[0]) != -1 || stationName.indexOf(names[1].substring(0,names[1].length()-1)) != -1 || stationName2.indexOf(names[0]) != -1 || stationName2.indexOf(names[1].substring(0,names[1].length()-1)) != -1)){
stationRoutes.get(i).setIndustryCode(industryCode);
listMactah.add(stationRoutes.get(i));
break;
}
}else {
// if(stationRoutes.get(i).getStationMark().equals(smd.getStationType()) && (stationName.indexOf(name) != -1 || stationName2.indexOf(name) != -1)){
if(stationName.indexOf(name) != -1 || stationName2.indexOf(name) != -1){
// if(stationRoutes.get(i).getStationMark().equals(smd.getStationType()) && (name.equals(stationName) || name.equals(stationName2))){
stationRoutes.get(i).setIndustryCode(industryCode);
listMactah.add(stationRoutes.get(i));
break;
}
}
}
}
}
}
}
private String subLine2Ftp(LineRegion lineRegion) {
StringBuilder builder = new StringBuilder();
int len = lineRegion.getStationRoutes().size();
int idx = 1;
for (int i = 0;i < len;i++) {
LsStationRoute route = lineRegion.getStationRoutes().get(i);
builder.append(route.getCenterPointWgs().getPosition().getCoordinate(0))
.append("\t").append(route.getCenterPointWgs().getPosition().getCoordinate(1))
.append("\t").append(i == len - 1 ? 2 : 1)
.append("\t").append(idx).append("\t");
for (int j = 0;j < 8 - route.getStationCode().length();j++) {
builder.append("0");
}
builder.append(route.getStationCode())
.append("\t").append((int) route.getDistances().doubleValue() * 1000)
.append("\t0")
.append("\t").append(route.getStationName())
.append("\t").append(route.getStationNameEn())
.append("\r\n");
idx++;
}
return builder.toString();
}
/**
* tts合成及打包
* @param objects
* @param line
* @param languages 语言 如:cn、en、sh
*/
private void ttsAndZip(List<Object[]> objects, Line line, Set<String> languages, List<LineRegion> lineRegions) throws Exception {
String lineId = line.getLineCode();
StringBuilder cnBuilder = new StringBuilder(line.getName()).append("[p1000]"), enBuilder = new StringBuilder("Hello[p1000]");
int ups = 0, downs = 0;
for (int i = 0;i < objects.size();i++) {
Object[] objArr = objects.get(i);
int direction = (int) objArr[8];
String stationName = objArr[7] == null ? null : objArr[7].toString(), stationNameEn = objArr[9] == null ? null : objArr[9].toString();
if (StringUtils.isEmpty(stationName)) {
throw new RuntimeException("存在异常的中文站点名称");
}
// 如果要生成英语报站语音
if (languages.contains("en")) {
if (StringUtils.isEmpty(stationNameEn)) {
throw new RuntimeException("存在异常的英文站点名称");
}
enBuilder.append(stationNameEn).append("[p1000]");
}
cnBuilder.append(stationName).append("[p1000]");
if (direction == 0) {
ups++;
} else if (direction == 1) {
// 环线
if (line.getLinePlayType() == 1) {
break;
}
downs++;
}
}
cnBuilder.delete(cnBuilder.length() - 8, cnBuilder.length() - 1);
enBuilder.delete(enBuilder.length() - 8, enBuilder.length() - 1);
// 文本转语音并进行分割
// 音频存放及压缩文件路径
String linePath = String.format(linePathPattern, lineId), voicePath = String.format("%s%s.zip", linePath, lineId);
// 先清理历史生成的文件
cleanHistoryAudio(new File(linePath));
for (String language : languages) {
try {
String path = String.format("%s%s.mp3", linePath, language);
if ("cn".equals(language) || "sh".equals(language)) {
IFlyUtils.textToSpeech(cnBuilder.toString(), language, path);
} else if ("en".equals(language)) {
IFlyUtils.textToSpeech(enBuilder.toString(), language, path);
}
AudioOperationUtils.splitBySilence(path, String.format("%s%s", linePath, language), 500, -40);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// 合并每站起步、到达语音(全程)
int seq = 1;
List<SeqStationLevel> seqs = new ArrayList<>();
for (int i = seq;i <= ups;i++) {
seqs.add(new SeqStationLevel(i, i));
}
seq = merge(lineId, "0", seq, seqs, languages);
if (line.getLinePlayType() != 1) {
seqs.clear();
for (int i = seq;i <= ups + downs;i++) {
seqs.add(new SeqStationLevel(i, i, 1));
}
merge(lineId, "0", seq, seqs, languages);
}
zipAudio(String.format("%s%s/", linePath, "0"), voicePath);
// 线路区间
Map<String, List<SeqStationLevel>> region2map = new HashMap<>();
for (LineRegion lineRegion : lineRegions) {
for (int i = 0;i < objects.size();i++) {
Object[] objArr = objects.get(i);
String stationCode = (String) objArr[5];
int direction = (int) objArr[8];
if (lineRegion.getDirection() == direction) {
for (int j = 0;j < lineRegion.getStationRoutes().size();j++) {
LsStationRoute stationRoute = lineRegion.getStationRoutes().get(j);
if (stationCode.equals(stationRoute.getStationCode())) {
List<SeqStationLevel> map = region2map.get(lineRegion.getSeq().toString());
if (map == null) {
map = new ArrayList<>();
region2map.put(lineRegion.getSeq().toString(), map);
}
map.add(new SeqStationLevel(j + 1, i + 1, direction));
break;
}
}
}
}
}
for (Map.Entry<String, List<SeqStationLevel>> entry : region2map.entrySet()) {
String subLineId = entry.getKey();
List<SeqStationLevel> map = entry.getValue();
seq = 1;
merge(lineId, subLineId, seq, map, languages);
zipAudio(String.format("%s%s/", linePath, subLineId), String.format("%s%s-%s.zip", linePath, lineId, subLineId));
}
}
private void cleanHistoryAudio(File file) throws Exception {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
cleanHistoryAudio(f);
}
} else {
if (file.getName().endsWith(".mp3") || file.getName().endsWith(".zip")) {
file.delete();
}
}
}
/**
* 压缩音频到zip
* @param subLinePath 子线路文件夹
* @param voicePath 语音报站文件压缩包路径
*/
private void zipAudio(String subLinePath, String voicePath) throws Exception {
File file = new File(subLinePath);
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(voicePath));
for (File f : file.listFiles()) {
if (f.isFile() && f.getName().endsWith(".mp3")) {
addFileToZip(zos, f);
}
}
// Key打头音频
file = new File(commonPath);
for (File f : file.listFiles()) {
if (f.isFile() && f.getName().startsWith("Key-")) {
addFileToZip(zos, f);
}
}
zos.flush();
zos.close();
}
private static void addFileToZip(ZipOutputStream zos, File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
ZipEntry zipEntry = new ZipEntry(file.getName());
zos.putNextEntry(zipEntry);
byte[] bytes = new byte[4096];
int length;
while ((length = fis.read(bytes)) >= 0) {
zos.write(bytes, 0, length);
}
zos.closeEntry();
fis.close();
}
/**
*
* @param lineId 线路编码
* @param seq 线路站级序号(全局)
* @param seqs 当前方向站级序号
* @param languages 语种
* @return
* @throws Exception
*/
private int merge(String lineId, String subLineId, int seq, List<SeqStationLevel> seqs, Set<String> languages) throws Exception {
SeqStationLevel startSeq = seqs.get(0), terminal = seqs.get(seqs.size() - 1);
for (int i = 0;i < seqs.size();i++) {
SeqStationLevel current = seqs.get(i);
if (current.getStationLevel() > startSeq.getStationLevel()) {
if (i == 1) {
// 生成首站
mergeOriginStart(lineId, subLineId, seq, current, current.getDirection(), terminal, languages);
mergeNormalArrive(lineId, subLineId, seq, current, current.getDirection(), terminal, languages);
} else if (current.getStationLevel() == terminal.getStationLevel()) {
// 生成终点站
mergeNormalStart(lineId, subLineId, seq, current, current.getDirection(), terminal, languages);
mergeTerminalArrive(lineId, subLineId, seq, current, current.getDirection(), terminal, languages);
} else {
// 生成中途站
mergeNormalStart(lineId, subLineId, seq, current, current.getDirection(), terminal, languages);
mergeNormalArrive(lineId, subLineId, seq, current, current.getDirection(), terminal, languages);
}
seq++;
}
}
return seq;
}
private void mergeOriginStart(String lineId, String subLineId, int seq, SeqStationLevel current, int direction, SeqStationLevel terminal, Set<String> languages) throws Exception {
// 子线路路径
String linePath = String.format(linePathPattern, lineId);
String subLinePath = String.format("%s%s/", linePath, subLineId);
List<String> arr = Arrays.asList(commonPath + "cn_origin_1.mp3", linePath + String.format("cn/%03d.mp3", 0), commonPath + "cn_origin_2.mp3", linePath + String.format("cn/%03d.mp3", terminal.getStationLevel()), commonPath + "sh_origin_1.mp3", linePath + String.format("sh/%03d.mp3", 0), commonPath + "sh_origin_2.mp3", linePath + String.format("cn/%03d.mp3", terminal.getStationLevel()), commonPath + "cn_start_1.mp3", linePath + String.format("cn/%03d.mp3", current.getStationLevel()), commonPath + "cn_start_2.mp3", commonPath + "sh_start_1.mp3", linePath + String.format("sh/%03d.mp3", current.getStationLevel()), commonPath + "sh_start_2.mp3", commonPath + "en_1.mp3", linePath + String.format("en/%03d.mp3", current.getStationLevel()), commonPath + "cn_start_3.mp3", commonPath + "sh_start_3.mp3");
List<String> inputPaths = new ArrayList<>();
for (String path : arr) {
for (String lang : languages) {
if (path.indexOf(lang) > -1) {
inputPaths.add(path);
break;
}
}
}
AudioOperationUtils.merge(inputPaths, String.format("%s%03da%s-%03d-%s-Start.mp3", subLinePath, seq, direction == 0 ? "u" : "d", direction == 0 ? current.getSeq() : terminal.getSeq() - current.getSeq() + 1, direction == 0 ? "Up" : "Dn"));
}
private void mergeNormalStart(String lineId, String subLineId, int seq, SeqStationLevel current, int direction, SeqStationLevel terminal, Set<String> languages) throws Exception {
String linePath = String.format(linePathPattern, lineId);
String subLinePath = String.format("%s%s/", linePath, subLineId);
List<String> arr = Arrays.asList(commonPath + "cn_start.mp3", linePath + String.format("cn/%03d.mp3", current.getStationLevel()), commonPath + "cn_start_2.mp3", commonPath + "sh_start_1.mp3", linePath + String.format("sh/%03d.mp3", current.getStationLevel()), commonPath + "sh_start_2.mp3", commonPath + "en_1.mp3", linePath + String.format("en/%03d.mp3", current.getStationLevel()), commonPath + "cn_start_3.mp3", commonPath + "sh_start_3.mp3");
List<String> inputPaths = new ArrayList<>();
for (String path : arr) {
for (String lang : languages) {
if (path.indexOf(lang) > -1) {
inputPaths.add(path);
break;
}
}
}
AudioOperationUtils.merge(inputPaths, String.format("%s%03da%s-%03d-%s-Start.mp3", subLinePath, seq, direction == 0 ? "u" : "d", direction == 0 ? current.getSeq() : terminal.getSeq() - current.getSeq() + 1, direction == 0 ? "Up" : "Dn"));
}
private void mergeNormalArrive(String lineId, String subLineId, int seq, SeqStationLevel current, int direction, SeqStationLevel terminal, Set<String> languages) throws Exception {
String linePath = String.format(linePathPattern, lineId);
String subLinePath = String.format("%s%s/", linePath, subLineId);
List<String> arr = new ArrayList<>(Arrays.asList(commonPath + "cn_arrive.mp3", linePath + String.format("cn/%03d.mp3", current.getStationLevel()), commonPath + "cn_arrive_1.mp3", linePath + String.format("sh/%03d.mp3", current.getStationLevel()), commonPath + "sh_arrive_1.mp3", commonPath + "en_2.mp3", linePath + String.format("en/%03d.mp3", current.getStationLevel())));
List<String> inputPaths = new ArrayList<>();
if (languages.contains("sh")) {
for (int i = 0;i < 3;i++) {
arr.add(linePath + String.format("cn/%03d.mp3", 0));
arr.add(commonPath + "cn_origin_2.mp3");
arr.add(linePath + String.format("cn/%03d.mp3", terminal.getStationLevel()));
arr.add(linePath + String.format("sh/%03d.mp3", 0));
arr.add(commonPath + "sh_origin_2.mp3");
arr.add(linePath + String.format("sh/%03d.mp3", terminal.getStationLevel()));
}
} else {
arr.add(linePath + String.format("cn/%03d.mp3", 0));
arr.add(commonPath + "cn_origin_2.mp3");
arr.add(linePath + String.format("cn/%03d.mp3", terminal.getStationLevel()));
}
for (String path : arr) {
for (String lang : languages) {
if (path.indexOf(lang) > -1) {
inputPaths.add(path);
break;
}
}
}
AudioOperationUtils.merge(inputPaths, String.format("%s%03db%s-%03d-%s-Arrive.mp3", subLinePath, seq, direction == 0 ? "u" : "d", direction == 0 ? current.getSeq() : terminal.getSeq() - current.getSeq() + 1, direction == 0 ? "Up" : "Dn"));
}
private void mergeTerminalArrive(String lineId, String subLineId, int seq, SeqStationLevel current, int direction, SeqStationLevel terminal, Set<String> languages) throws Exception {
String linePath = String.format(linePathPattern, lineId);
String subLinePath = String.format("%s%s/", linePath, subLineId);
List<String> arr = Arrays.asList(commonPath + "cn_terminal.mp3", linePath + String.format("cn/%03d.mp3", current.getStationLevel()), commonPath + "cn_arrive_1.mp3", commonPath + "sh_terminal.mp3", linePath + String.format("sh/%03d.mp3", current.getStationLevel()), commonPath + "sh_arrive_1.mp3", commonPath + "en_3.mp3", linePath + String.format("en/%03d.mp3", current.getStationLevel()), commonPath + "terminal_music.mp3");
List<String> inputPaths = new ArrayList<>();
for (String path : arr) {
for (String lang : languages) {
if (path.indexOf(lang) > -1 || path.indexOf("terminal_music") > -1) {
inputPaths.add(path);
break;
}
}
}
AudioOperationUtils.merge(inputPaths, String.format("%s%03db%s-%03d-%s-Arrive.mp3", subLinePath, seq, direction == 0 ? "u" : "d", direction == 0 ? current.getSeq() : terminal.getSeq() - current.getSeq() + 1, direction == 0 ? "Up" : "Dn"));
}
private void description() {
// Next stop is (en_1.mp3)
// We are arrival at (en_2.mp3)
// We are arrival at the terminal (en_3.mp3)
// 叮咚+欢迎乘坐 (cn_origin_1.mp3)
// 公交车方向 (cn_origin_2.mp3)
// 叮咚+车辆起步请拉好扶手投币后请配合朝里走下一站 (cn_start.mp3)
// 下一站 (cn_start_1.mp3)
// 请准备从后门下车 (cn_start_2.mp3)
// 乘客们请给需要帮助的乘客让个座谢谢 (cn_start_3.mp3)
// 叮咚+车辆进站请注意安全 (cn_arrive.mp3)
// 到了请配合从后门下车开门请当心 (cn_arrive_1.mp3)
// 叮咚+终点站 (cn_terminal.mp3)
// 欢迎乘坐(沪) (sh_origin_1.mp3)
// 公交车方向(沪) (sh_origin_2.mp3)
// 下一站(沪) (sh_start_1.mp3)
// 请准备从后门下车 (sh_start_2.mp3)
// 乘客们请给需要帮助的乘客让个座谢谢(沪) (sh_start_3.mp3)
// 到了请配合从后门下车开门请当心(沪) (sh_arrive_1.mp3)
// 终点站(沪) (sh_terminal.mp3)
// 终点音乐 (terminal_music.mp3)
}
/**
* 站序和站级的影响,全程和区间时会不一致
*/
private final static class SeqStationLevel {
private Integer seq;
private Integer stationLevel;
private int direction;
private SeqStationLevel(Integer seq, Integer stationLevel) {
this(seq, stationLevel, 0);
}
private SeqStationLevel(Integer seq, Integer stationLevel, int direction) {
this.seq = seq;
this.stationLevel = stationLevel;
this.direction = direction;
}
public Integer getSeq() {
return seq;
}
public void setSeq(Integer seq) {
this.seq = seq;
}
public Integer getStationLevel() {
return stationLevel;
}
public void setStationLevel(Integer stationLevel) {
this.stationLevel = stationLevel;
}
public int getDirection() {
return direction;
}
public void setDirection(int direction) {
this.direction = direction;
}
}
}