StationRouteServiceImpl.java 47.8 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
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;

	@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());
			/** 查询线路信息 @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);
			if (objects.size()>0) {
				// 报站音频
				Set<String> languages = new HashSet<>();
				languages.add("cn");
				//languages.add("sh");
				//languages.add("en");
				InputStream tts = ttsAndZip(objects, line, languages);
				/** 获取配置文件里的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);

//				textFile.delete();

				clientUtils.deleteFtpFile(url, port, username, password, remotePath + "/voice/", String.format("%s.zip", line.getLineCode()));
				clientUtils.uploadFile(url, port, username, password, remotePath + "/voice/", String.format("%s.zip", line.getLineCode()), tts);

				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 InputStream ttsAndZip(List<Object[]> objects, Line line, Set<String> languages) 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);
		try {
			String path = String.format("%scn.mp3", linePath);
			IFlyUtils.textToSpeechCn(cnBuilder.toString(), path);
			AudioOperationUtils.splitBySilence(path, String.format("%scn", linePath), 500, -40);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		if (languages.contains("sh")) {
            try {
				String path = String.format("%ssh.mp3", linePath);
                IFlyUtils.textToSpeechSh(cnBuilder.toString(), path);
				AudioOperationUtils.splitBySilence(path, String.format("%ssh", linePath), 500, -40);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
		if (languages.contains("en")) {
			try {
				String path = String.format("%sen.mp3", linePath);
				IFlyUtils.textToSpeechEn(enBuilder.toString(), path);
				AudioOperationUtils.splitBySilence(path, String.format("%sen", linePath), 500, -40);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		// 删除原线路音频
		File file = new File(linePath);
		for (File f : file.listFiles()) {
			if (f.isFile() && f.getName().endsWith(".mp3")) {
				file.delete();
			}
		}

		// 合并每站起步、到达语音
		int seq = 1, startSeq = 1, direction = 0;
		seq = merge(lineId, seq, startSeq, direction, ups, languages);
		startSeq = ups + 1;
		direction = 1;
		merge(lineId, seq, startSeq, direction, ups + downs, languages);

		// 压缩音频到zip
		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();

		return new FileInputStream(voicePath);
	}

	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();
	}

	private int merge(String lineId, int seq, int startSeq, int direction, int terminal, Set<String> languages) throws Exception {
		for (int i = startSeq;i <= terminal;i++) {
			if (i > startSeq) {
				if (i == startSeq + 1) {
					// 生成首站
					mergeOriginStart(lineId, seq, i, direction, terminal, languages);
					mergeNormalArrive(lineId, seq, i, direction, terminal, languages);
				} else if (i == terminal) {
					// 生成终点站
					mergeNormalStart(lineId, seq, i, direction, terminal, languages);
					mergeTerminalArrive(lineId, seq, i, direction, terminal, languages);
				} else {
					// 生成中途站
					mergeNormalStart(lineId, seq, i, direction, terminal, languages);
					mergeNormalArrive(lineId, seq, i, direction, terminal, languages);
				}
				seq++;
			}
		}

		return seq;
	}

	private void mergeOriginStart(String lineId, int seq, int stationLevel, int direction, int terminal, Set<String> languages) throws Exception {
		String linePath = String.format(linePathPattern, lineId);
		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), commonPath + "sh_origin_1.mp3", linePath + String.format("sh/%03d.mp3", 0), commonPath + "sh_origin_2.mp3", linePath + String.format("cn/%03d.mp3", terminal), commonPath + "cn_start_1.mp3", linePath + String.format("cn/%03d.mp3", stationLevel), commonPath + "cn_start_2.mp3", commonPath + "sh_start_1.mp3", linePath + String.format("sh/%03d.mp3", stationLevel), commonPath + "sh_start_2.mp3", commonPath + "en_1.mp3", linePath + String.format("en/%03d.mp3", stationLevel), 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", linePath, seq, direction == 0 ? "u" : "d", direction == 0 ? stationLevel : terminal - stationLevel + 1, direction == 0 ? "Up" : "Dn"));
	}

	private void mergeNormalStart(String lineId, int seq, int stationLevel, int direction, int terminal, Set<String> languages) throws Exception {
		String linePath = String.format(linePathPattern, lineId);
		List<String> arr = Arrays.asList(commonPath + "cn_start.mp3", linePath + String.format("cn/%03d.mp3", stationLevel), commonPath + "cn_start_2.mp3", commonPath + "sh_start_1.mp3", linePath + String.format("sh/%03d.mp3", stationLevel), commonPath + "sh_start_2.mp3", commonPath + "en_1.mp3", linePath + String.format("en/%03d.mp3", stationLevel), 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", linePath, seq, direction == 0 ? "u" : "d", direction == 0 ? stationLevel : terminal - stationLevel + 1, direction == 0 ? "Up" : "Dn"));
	}

	private void mergeNormalArrive(String lineId, int seq, int stationLevel, int direction, int terminal, Set<String> languages) throws Exception {
		String linePath = String.format(linePathPattern, lineId);
		List<String> arr = new ArrayList<>(Arrays.asList(commonPath + "cn_arrive.mp3", linePath + String.format("cn/%03d.mp3", stationLevel), commonPath + "cn_arrive_1.mp3", linePath + String.format("sh/%03d.mp3", stationLevel), commonPath + "sh_arrive_1.mp3", commonPath + "en_2.mp3", linePath + String.format("en/%03d.mp3", stationLevel)));
		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));
				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));
			}
		} 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));
		}
		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", linePath, seq, direction == 0 ? "u" : "d", direction == 0 ? stationLevel : terminal - stationLevel + 1, direction == 0 ? "Up" : "Dn"));
	}

	private void mergeTerminalArrive(String lineId, int seq, int stationLevel, int direction, int terminal, Set<String> languages) throws Exception {
		String linePath = String.format(linePathPattern, lineId);
		List<String> arr = Arrays.asList(commonPath + "cn_terminal.mp3", linePath + String.format("cn/%03d.mp3", stationLevel), commonPath + "cn_arrive_1.mp3", commonPath + "sh_terminal.mp3", linePath + String.format("sh/%03d.mp3", stationLevel), commonPath + "sh_arrive_1.mp3", commonPath + "en_3.mp3", linePath + String.format("en/%03d.mp3", stationLevel), 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", linePath, seq, direction == 0 ? "u" : "d", direction == 0 ? stationLevel : terminal - stationLevel + 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)
	}
}