StationServiceImpl.java 29.2 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
package com.bsth.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bsth.common.ResponseCode;
import com.bsth.entity.*;
import com.bsth.entity.search.CustomerSpecs;
import com.bsth.repository.*;
import com.bsth.service.StationService;
import com.bsth.util.CoordinateConverter;
import com.bsth.util.CoordinateConverter.Location;
import com.bsth.util.CustomBeanUtils;
import com.bsth.util.Geo.GeoUtils;
import com.bsth.util.Geo.Point;
import com.bsth.util.GeoConverter;
import org.geolatte.geom.LineString;
import org.geolatte.geom.Polygon;
import org.geolatte.geom.codec.Wkt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @ClassName: StationServiceImpl(站点service业务层实现类)
 * 
 * @Extends : BaseService
 * 
 * @Description: TODO(站点service业务层)
 * 
 * @Author bsth@lq
 * 
 * @Date 2016年05月03日 上午9:21:17
 *
 * @Version 公交调度系统BS版 0.1
 * 
 */

@Service
public class StationServiceImpl extends BaseServiceImpl<Station, Integer> implements StationService {

	@Autowired
	private StationRepository repository;

	@Autowired
	private StationRouteRepository routeRepository;

	@Autowired
	private LsStationRouteRepository lsStationRouteRepository;

	@Autowired
	private LineInformationRepository informationRepository;

	@Autowired
	private LineRepository lineRepository;

	@Autowired
	private SectionRepository sectionRepository;

	@Autowired
	private SectionRouteRepository sectionRouteRepository;

	@Autowired
	private LsSectionRouteRepository lsSectionRouteRepository;

	@Autowired
	LineVersionsRepository lineVersionsRepository;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * @Description :TODO(系统规划保存数据)
	 * 
	 * @param map <stationJSON:站点信息; sectionJSON:路段信息;dbType:坐标类型; destroy:是否撤销;
	 *            directions:方向;lineId:线路ID;
	 * 
	 *            radius:圆半径;shapesType:图形类型;speedLimit:限速>
	 * 
	 * @return Map<String, Object> <SUCCESS ; ERROR>
	 */
	@Override
	public Map<String, Object> systemSaveStations(Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			// 站点信息字符传
			String stationJSON = map.get("stationJSON").toString().equals("") ? "" : map.get("stationJSON").toString();
			// 路段信息字符串
			String sectionJSON = map.get("sectionJSON").toString().equals("") ? "" : map.get("sectionJSON").toString();
			// 方向
			int directions = Integer.parseInt(map.get("directions").toString());
			// 是否撤销
			int destroy = map.get("destroy").equals("") ? 0 : Integer.parseInt(map.get("destroy").toString());
			// 版本
			int versions = map.get("versions").equals("") ? 0 : Integer.parseInt(map.get("versions").toString());
			// 坐标类型
			String dbType = map.get("dbType").equals("") ? "" : map.get("dbType").toString();
			// 限速
			String speedLimitStr = map.get("speedLimit").equals("") ? "" : map.get("speedLimit").toString();
			// 线路ID
			int lineId = map.get("lineId").toString().equals("") ? 0 : Integer.parseInt(map.get("lineId").toString());
			// 半径
			int radius = map.get("radius").equals("") ? 0 : Integer.parseInt(map.get("radius").toString());
			// 图形类型
			String shapesType = map.get("shapesType").equals("") ? "" : map.get("shapesType").toString();
			// 路段距离
			Double sectionDistance = 0.0d;
			// 路段时长
			Double sectionDuration = 0.0d;
			// 里程(上或者下)
			double sumUpOrDownMileage = 0.0d;
			// 线路信息
			Line resultLine = lineRepository.findById(lineId).get();
			Map<String, Object> resultSaveMapm = new HashMap<String, Object>();
			String baseRes = map.get("baseRes").equals("") ? "" : map.get("baseRes").toString();
			if (!stationJSON.equals("")) {
				// 保存前先撤销原有站点

				// 保存站点与站点路由信息
				resultSaveMapm = savaStationAndStationRouteInfo(stationJSON, sectionDistance, sectionDuration, dbType,
						radius, shapesType, destroy, versions, sumUpOrDownMileage, directions, resultLine, baseRes);
			}

			// 路段长度
			sectionDistance = Double.valueOf(resultSaveMapm.get("sectionDistance").toString());

			// 路段时长
			sectionDuration = Double.valueOf(resultSaveMapm.get("sectionDuration").toString());

			// 如果路段信息JSON字符串不为空
			if (!sectionJSON.equals("")) {

				// 保存路段与路段路由信息
				saveSectionAndSectionRouteInfo(sectionJSON, directions, resultLine, speedLimitStr, sectionDistance,
						sectionDuration, dbType, versions, destroy);
			}

			// 里程
			sumUpOrDownMileage = Double.valueOf(resultSaveMapm.get("sumUpOrDownMileage").toString());

			BigDecimal s = new BigDecimal(sumUpOrDownMileage);

			sumUpOrDownMileage = s.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

			// 更新里程
			updateLineInfoUpOrDownMileage(resultLine, directions, sumUpOrDownMileage);

			if (directions == 0) {

				lineUpdateStationName(resultLine);

			}
			resultMap.put("status", ResponseCode.SUCCESS);

		} catch (Exception e) {

			resultMap.put("status", ResponseCode.ERROR);

			logger.error("save erro.", e);

		}

		return resultMap;
	}

	public void lineUpdateStationName(Line resultLine) {

		List<StationRoute> stationRoutes = routeRepository.findByLine(resultLine);

		int sizeL = stationRoutes.size();

		if (sizeL < 0) {

			return;

		}

		if (resultLine.getStartStationName().equals("")) {

			for (int k = 0; k < sizeL; k++) {

				StationRoute tempS = stationRoutes.get(k);

				if (tempS.getStationMark().equals("B") && tempS.getDirections() == 0) {

					resultLine.setStartStationName(tempS.getStationName());

					lineRepository.save(resultLine);

				}
			}

		}
		;

		if (resultLine.getEndStationName().equals("")) {

			for (int k = 0; k < sizeL; k++) {

				StationRoute tempS = stationRoutes.get(k);

				if (tempS.getStationMark().equals("E") && tempS.getDirections() == 0) {

					resultLine.setEndStationName(tempS.getStationName());

					lineRepository.save(resultLine);

				}
			}

		}
	}

	/**
	 * @Description :保存站点和站点路由信息(系统规划)
	 * 
	 * @param stationJSON:站点和站点路由json数据
	 * 
	 * @param sectionDistance:            总距离
	 * 
	 * @param sectionDuration:            总时长
	 * 
	 * @param dbType:原坐标类型
	 * 
	 * @param radius:半径
	 * 
	 * @param shapesType:图形类型
	 * 
	 * @param destroy:是否撤销
	 * 
	 * @param versions:版本
	 * 
	 * @param sumUpOrDownMileage:里程(上或者下)
	 * 
	 * @param directions:方向
	 * 
	 * @param resultLine:线路实体类
	 * 
	 * @return
	 * 
	 * @throws Exception
	 */
	public Map<String, Object> savaStationAndStationRouteInfo(String stationJSON, double sectionDistance,
			double sectionDuration, String dbType, int radius, String shapesType, int destroy, int versions,
			double sumUpOrDownMileage, int directions, Line resultLine, String baseRes) throws Exception {
		Map<String, Object> paramsMeleageAndDistncDura = new HashMap<String, Object>();
		JSONArray stationsArray = JSONArray.parseArray(stationJSON);
		if (stationsArray.size() > 0) {
			Station loopStartStation = new Station();
			for (int i = 0; i < stationsArray.size(); i++) {
				// 站点名称
				String stationName = stationsArray.getJSONObject(i).equals("") ? ""
						: stationsArray.getJSONObject(i).get("name").toString();
				Double distance = stationsArray.getJSONObject(i).get("distance").equals("") ? 0.0d
						: Double.parseDouble(stationsArray.getJSONObject(i).get("distance").toString());
				// 转成公里
				distance = distance / 1000;
				BigDecimal d = new BigDecimal(distance);
				distance = d.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				sectionDistance = distance + sectionDistance;
				Double duration = stationsArray.getJSONObject(i).get("duration").equals("") ? 0.0d
						: Double.parseDouble(stationsArray.getJSONObject(i).get("duration").toString());
				// 转成分钟
				duration = duration / 60;
				BigDecimal t = new BigDecimal(duration);
				duration = t.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				sectionDuration = duration + sectionDuration;
				// 百度坐标经度
				String bLonx = JSONObject.parseObject(stationsArray.getJSONObject(i).get("potion").toString())
						.get("lng").toString();
				bLonx = bLonx == null || bLonx.equals("") ? "0" : bLonx;
				// 百度坐标纬度
				String bLaty = JSONObject.parseObject(stationsArray.getJSONObject(i).get("potion").toString())
						.get("lat").toString();
				bLaty = bLaty == null || bLaty.equals("") ? "0" : bLaty;
				List<Object[]> stationNameList = repository.findStationName(directions, stationName);
				Map<String, Object> isHaveMap = isHaveStationname(bLonx, bLaty, stationNameList);
				// 初始化站点对象
				Station tempStation = new Station();
				if (Boolean.parseBoolean(isHaveMap.get("isHave").toString())) {
					/*
					 * Integer stationId = Integer.parseInt(stationNameList.get(0)[1].toString());
					 */
					Integer stationId = Integer.parseInt(isHaveMap.get("id").toString());
					tempStation = repository.findById(stationId).get();

					if (i == stationsArray.size() - 1 && resultLine.getLinePlayType() == 1) {// 环线终点和起点引用同一个站
						tempStation = loopStartStation;
					} else {
						List<Station> list = new ArrayList<>();
						list.add(tempStation);
						List<Station> stationlist = JSONArray.parseArray(JSON.toJSONString(list), Station.class);
						Station station = stationlist.get(0);
						// 站点编码
						int stationCode = repository.stationMaxId() + 1;
						station.setStationCode(String.valueOf(stationCode));
						station.setId(stationCode);
						station.setCreateDate(null);
						station.setUpdateDate(null);
						repository.save(station);
						tempStation = station;
						if (i == 0 && resultLine.getLinePlayType() == 1) {// 环线终点和起点引用同一个站
							loopStartStation = tempStation;
						}
					}
				} else {
					// 站点编码
					int stationCode = repository.stationMaxId() + 1 ;
					tempStation.setStationCode(String.valueOf(stationCode));
					tempStation.setId(stationCode);
					tempStation.setStationName(stationName);
					String gLonxStr = JSONObject.parseObject(stationsArray.getJSONObject(i).get("wgs").toString())
							.get("x").equals("") ? null
									: JSONObject.parseObject(stationsArray.getJSONObject(i).get("wgs").toString())
											.get("x").toString();
					String gLatyStr = JSONObject.parseObject(stationsArray.getJSONObject(i).get("wgs").toString())
							.get("y").equals("") ? null
									: JSONObject.parseObject(stationsArray.getJSONObject(i).get("wgs").toString())
											.get("y").toString();

					// 统一归化为wgs
					Location wgs = null;
					// 中心点wkt
					StringBuilder centerPointWkt = new StringBuilder();
					StringBuilder centerPointWgsWkt = new StringBuilder();
					if (baseRes.equals("No")) {
						/** BD to WGS坐标 */
						wgs = CoordinateConverter.transformFromBDToWGS(CoordinateConverter.LocationMake(Double.parseDouble(bLonx), Double.parseDouble(bLaty)));
						centerPointWkt.append("POINT(").append(bLonx).append(" ").append(bLaty).append(")");
						centerPointWgsWkt.append("POINT(").append(wgs.getLng()).append(" ").append(wgs.getLat()).append(")");
					} else if (baseRes.equals("GCJ02")) {
						Location gcj = CoordinateConverter.LocationMake(Double.valueOf(gLonxStr), Double.valueOf(gLatyStr));
						Location baidu = CoordinateConverter.bd_encrypt(gcj);
						wgs = CoordinateConverter.transformFromGCJToWGS(gcj);
						centerPointWkt.append("POINT(").append(baidu.getLng()).append(" ").append(baidu.getLat()).append(")");
						centerPointWgsWkt.append("POINT(").append(wgs.getLng()).append(" ").append(wgs.getLat()).append(")");
					} else if (baseRes.equals("BD09")) {
						wgs = CoordinateConverter.transformFromBDToWGS(CoordinateConverter.LocationMake(Double.valueOf(gLonxStr), Double.valueOf(gLatyStr)));
						centerPointWkt.append("POINT(").append(gLonxStr).append(" ").append(gLatyStr).append(")");
						centerPointWgsWkt.append("POINT(").append(wgs.getLng()).append(" ").append(wgs.getLat()).append(")");
					} else if (baseRes.equals("WGS84")) {
						wgs = CoordinateConverter.LocationMake(Double.valueOf(gLonxStr), Double.valueOf(gLatyStr));
						Location baidu = CoordinateConverter.transformFromWGSToBD(wgs);
						centerPointWkt.append("POINT(").append(baidu.getLng()).append(" ").append(baidu.getLat()).append(")");
						centerPointWgsWkt.append("POINT(").append(wgs.getLng()).append(" ").append(wgs.getLat()).append(")");
					}

					// 版本号
					tempStation.setVersions(versions);
					tempStation.setCenterPoint((org.geolatte.geom.Point) Wkt.fromWkt(centerPointWkt.toString()));
					tempStation.setCenterPointWgs((org.geolatte.geom.Point) Wkt.fromWkt(centerPointWgsWkt.toString()));
					// 插入站点信息
					if (i == 0 && resultLine.getLinePlayType() == 1) {// 环线终点和起点引用同一个站
						loopStartStation = tempStation;
						repository.save(tempStation);
					} else if (i == stationsArray.size() - 1 && resultLine.getLinePlayType() == 1) {// 环线终点和起点引用同一个站
						tempStation = loopStartStation;
					} else {
						repository.save(tempStation);
					}

				}

				Integer version = lineVersionsRepository.findCurrentVersion(resultLine.getId());

				if (versions == version) {
					// 站点路由对象
					StationRoute route = new StationRoute();
					// 站点名称
					route.setStationName(stationName);
					route.setDistances(distance);
					sumUpOrDownMileage = sumUpOrDownMileage + distance;
					route.setToTime(duration);
					// 站点编码
					route.setStationCode(tempStation.getStationCode());
					// 站点序号
					route.setStationRouteCode((i + 1) * 100);
					// 站点类型
					if (i == 0) {
						// 起始站
						route.setStationMark("B");
					} else if (i == stationsArray.size() - 1) {
						// 终点站
						route.setStationMark("E");
					} else {
						// 中途站
						route.setStationMark("Z");
					}
					// 版本号(获取线路当前版本)
					route.setVersions(versions);
					// 站点ID
					route.setStation(tempStation);
					// 方向
					route.setDirections(directions);
					// 线路ID
					route.setLine(resultLine);
					// 线路编码
					route.setLineCode(resultLine.getLineCode());
					route.setDestroy(destroy);
					route.setShapedType(shapesType);
					route.setRadius(80);
					// 插入站点路由信息
					routeRepository.save(route);
				} else {

					LsStationRoute route = new LsStationRoute();
					// 站点名称
					route.setStationName(stationName);
					route.setDistances(distance);
					sumUpOrDownMileage = sumUpOrDownMileage + distance;
					route.setToTime(duration);
					// 站点编码
					route.setStationCode(tempStation.getStationCode());
					// 站点序号
					route.setStationRouteCode((i + 1) * 100);
					// 站点类型
					if (i == 0) {
						// 起始站
						route.setStationMark("B");
					} else if (i == stationsArray.size() - 1) {
						// 终点站
						route.setStationMark("E");
					} else {
						// 中途站
						route.setStationMark("Z");
					}
					// 版本号(获取线路当前版本)
					route.setVersions(versions);
					// 站点ID
					route.setStation(tempStation);
					// 方向
					route.setDirections(directions);
					// 线路ID
					route.setLine(resultLine);
					// 线路编码
					route.setLineCode(resultLine.getLineCode());
					route.setDestroy(destroy);
					route.setShapedType(shapesType);
					route.setRadius(80);
					// 插入站点路由信息
					lsStationRouteRepository.save(route);
				}

			}
			paramsMeleageAndDistncDura.put("sumUpOrDownMileage", sumUpOrDownMileage);
			paramsMeleageAndDistncDura.put("sectionDistance", sectionDistance);
			paramsMeleageAndDistncDura.put("sectionDuration", sectionDuration);
			paramsMeleageAndDistncDura.put("stationdata", stationsArray);
		}
		return paramsMeleageAndDistncDura;
	}

	/**
	 * 
	 * 
	 * @param resultLine:线路实体类
	 * 
	 * @param directions:方向
	 * 
	 * @param sumUpOrDownMileage:里程(上或者下)
	 */
	public void updateLineInfoUpOrDownMileage(Line resultLine, int directions, double sumUpOrDownMileage) {

		List<LineInformation> listLineInfo = informationRepository.findByLine(resultLine);

		int listLineSize = listLineInfo.size();

		// 不存在线路标准信息则插入
		if (listLineSize == 0) {

			LineInformation paramInfo = new LineInformation();

			// 上行
			if (directions == 0) {

				paramInfo.setUpMileage(sumUpOrDownMileage);

				paramInfo.setDownMileage(0.0d);

				paramInfo.setTotalMileage(sumUpOrDownMileage + 0.0d);

				paramInfo.setType("zc");

				paramInfo.setLine(resultLine);

				// 下行
			} else if (directions == 1) {

				paramInfo.setUpMileage(0.0d);

				paramInfo.setDownMileage(sumUpOrDownMileage);

				paramInfo.setTotalMileage(sumUpOrDownMileage + 0.0d);

				paramInfo.setType("zc");

				paramInfo.setLine(resultLine);

			}

			informationRepository.save(paramInfo);

			// 存在则更新
		} else if (listLineSize > 0) {

			for (int s = 0; s < listLineSize; s++) {

				LineInformation lineInformation = listLineInfo.get(s);

				double tempUp = lineInformation.getUpMileage();

				double tempDown = lineInformation.getDownMileage();

				// 上行
				if (directions == 0) {

					if (tempUp > 0.0d) {

						continue;
					}

					lineInformation.setUpMileage(sumUpOrDownMileage);

					lineInformation.setTotalMileage(sumUpOrDownMileage + tempDown);

				} else if (directions == 1) {

					if (tempDown > 0.0d) {

						continue;
					}

					lineInformation.setDownMileage(sumUpOrDownMileage);

					lineInformation.setTotalMileage(sumUpOrDownMileage + tempUp);

				}

				informationRepository.save(lineInformation);

			}

		}

	}

	/**
	 * @Description 保存路段与路段路由信息
	 * 
	 * @param sectionJSON:路段与路段路由json字符串数据
	 * 
	 * @param directions:方向
	 * 
	 * @param resultLine:线路实体类
	 * 
	 * @param speedLimitStr:限速
	 * 
	 * @param sectionDistance:总距离
	 * 
	 * @param sectionDuration:总时长
	 * 
	 * @param dbType:原中坐标类型
	 * 
	 * @param versions:版本号
	 * 
	 * @param destroy:是否撤销
	 * 
	 */
	public void saveSectionAndSectionRouteInfo(String sectionJSON, int directions, Line resultLine,
			String speedLimitStr,

			double sectionDistance, double sectionDuration, String dbType, int versions, int destroy) throws Exception {

		BigDecimal t = new BigDecimal(sectionDistance);

		sectionDistance = t.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

		BigDecimal d = new BigDecimal(sectionDuration);

		sectionDuration = d.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

		// 转换成JSON数组
		JSONArray sectionsArray = JSONArray.parseArray(sectionJSON);

		// 原始线状图形坐标集合
		String sectionsBpoints = "";

		// WGS线状图形坐标集合
		String sectionsWJPpoints = "";

		// 遍历
		for (int s = 0; s < sectionsArray.size(); s++) {

			String pointsLngStr = sectionsArray.getJSONObject(s).get("lng").toString();

			String pointsLatStr = sectionsArray.getJSONObject(s).get("lat").toString();

			String WGSLngStr = "";

			String WGSLatStr = "";

			Location resultPoint = CoordinateConverter.transformFromBDToWGS(CoordinateConverter.LocationMake(Double.parseDouble(pointsLngStr), Double.parseDouble(pointsLatStr)));

			WGSLngStr = String.valueOf(resultPoint.getLng());

			WGSLatStr = String.valueOf(resultPoint.getLat());
			/** to WGS坐标 */
			/*
			 * double lng = Double.parseDouble(WGSLngStr);
			 * 
			 * double lat = Double.parseDouble(WGSLatStr);
			 * 
			 * Location bdLoc = CoordinateConverter.LocationMake(lng, lat);
			 * 
			 * Location location = CoordinateConverter.bd_decrypt(bdLoc);
			 * 
			 * Location WGSPoint = CoordinateConverter.transformFromGCJToWGS(location);
			 */

			// String WGSLngStr =
			// JSONObject.parseObject(sectionsArray.getJSONObject(s).get("WGSpotion").toString()).get("Lng").toString();

			// String WGSLatStr =
			// JSONObject.parseObject(sectionsArray.getJSONObject(s).get("WGSpotion").toString()).get("Lat").toString();

			if (s == 0) {

				sectionsBpoints = pointsLngStr + " " + pointsLatStr;

				sectionsWJPpoints = WGSLngStr + " " + WGSLatStr;

			} else {

				sectionsBpoints = sectionsBpoints + "," + pointsLngStr + " " + pointsLatStr;

				sectionsWJPpoints = sectionsWJPpoints + "," + WGSLngStr + " " + WGSLatStr;

			}

		}

		long sectionMaxId = sectionRepository.sectionMaxId() + 1;

		String sectionCode = String.valueOf(sectionMaxId);

		int sectionid = (int) sectionMaxId;

		String upOrDownStr = "";

		if (directions == 0) {

			upOrDownStr = "上行路段";

		} else if (directions == 1) {

			upOrDownStr = "下行路段";

		}

		// 路段名称
		String sectionName = resultLine.getName() + upOrDownStr;

		// 交出路
		String crosesRoad = "";

		// 终止节点
		String endNode = "";

		// 开始节点
		String startNode = "";

		// 中间节点
		String middleNode = "";

		// WGS坐标点集合
		String gsectionVector = "LINESTRING(" + sectionsWJPpoints + ")";

		// 原坐标点集合
		String bsectionVector = "LINESTRING(" + sectionsBpoints + ")";

		// 城建坐标点集合
		String csectionVector = null;

		// 路段类型
		String sectionType = "";

		// 道路编码
		String roadCoding = "";

		// 限速
		double speedLimit = Double.parseDouble(speedLimitStr);

		// 说明
		String descriptions = "";

		// 路段保存
		sectionRepository.systemSave(sectionCode, sectionName, crosesRoad, endNode, startNode,

				middleNode, gsectionVector, bsectionVector, sectionType, csectionVector,

				roadCoding, sectionDistance, sectionDuration, dbType, speedLimit,

				descriptions, versions, sectionid);

		Section section = sectionRepository.findById(sectionid).get();
		// 路段路由
		SectionRoute sectionRoute = new SectionRoute();
		// 路段序号
		sectionRoute.setSectionrouteCode(100);
		// 线路编码
		sectionRoute.setLineCode(resultLine.getLineCode());
		// 路段ID
		sectionRoute.setSection(section);
		sectionRoute.setDestroy(destroy);
		// 线路ID
		sectionRoute.setLine(resultLine);
		// 路段编码
		sectionRoute.setSectionCode(sectionCode);
		// 版本
		sectionRoute.setVersions(versions);
		// 方向
		sectionRoute.setDirections(directions);
		// 路段路由保存
		sectionRouteRepository.save(sectionRoute);

	}

	public Map<String, Object> isHaveStationname(String jd, String wd, List<Object[]> stationNameList) {
		Map<String, Object> rsM = new HashMap<String, Object>();
		boolean temp = false;
		if (stationNameList.size() > 0) {
			if (jd.equals("0") || wd.equals("0")) {
				rsM.put("id", stationNameList.get(0)[1]);
				temp = true;
			} else {
				Point p1 = new Point(Double.parseDouble(jd), Double.parseDouble(wd));
				for (int k = 0; k < stationNameList.size(); k++) {
					String bJwpoints[] = stationNameList.get(k)[0].toString().split(" ");
					Point p2 = new Point(Double.parseDouble(bJwpoints[0]), Double.parseDouble(bJwpoints[1]));
					double jl = GeoUtils.getDistance(p1, p2);
					if (jl <= 60d) {
						rsM.put("id", stationNameList.get(k)[1]);
						temp = true;
						break;
					}
				}
			}
		}
		rsM.put("isHave", temp);
		return rsM;
	}

	/**
	 * @Description :TODO(更新站点保存)
	 * 
	 * @param stationRoute
	 * 
	 * @return Map<String, Object> <SUCCESS ; ERROR>
	 */
	@Transactional(rollbackFor = Exception.class)
	@Override
	public void stationUpdate(LsStationRoute stationRoute) {
		Station station = stationRoute.getStation(), station1 = repository.findById(station.getId()).get();
		LsStationRoute stationRoute1 = lsStationRouteRepository.findById(stationRoute.getId()).get();

		// 中心点坐标信息
		String centerPoint = station.getCenterPointWkt();
		Location bd = CoordinateConverter.LocationMake(centerPoint);
		Location wgs = CoordinateConverter.transformFromBDToWGS(bd);
		org.geolatte.geom.Point baiduPoint = (org.geolatte.geom.Point) Wkt.fromWkt(bd.toWkt());
		org.geolatte.geom.Point wgsPoint = (org.geolatte.geom.Point) Wkt.fromWkt(wgs.toWkt());
		station.setCenterPoint(baiduPoint);
		station.setCenterPointWgs(wgsPoint);

		// 多边形缓冲区
		if ("d".equals(stationRoute.getShapedType())) {
			String wkt = stationRoute.getBufferPolygonWkt();

			stationRoute.setBufferPolygon((Polygon) Wkt.fromWkt(wkt));
			stationRoute.setBufferPolygonWgs(GeoConverter.polygonBd2wgs(wkt));
		}

		lsStationRouteRepository.updateStartZ(stationRoute);
		lsStationRouteRepository.updateStatiouRouteCode(stationRoute);
		CustomBeanUtils.copyPropertiesIgnoredNull(station, station1);
		CustomBeanUtils.copyPropertiesIgnoredNull(stationRoute, stationRoute1);
		repository.save(station1);
		lsStationRouteRepository.save(stationRoute1);
		lsStationRouteRepository.updateStartB(stationRoute);
	}

	@Transactional
	@Override
	public Map<String, Object> saveRoutes(Integer lineId, Integer versions, Integer directions, List<LsStationRoute> stationRoutes, List<LsSectionRoute> sectionRoutes) {
		Map<String, Object> result = new HashMap<>();
		Line line = lineRepository.findById(lineId).get();
		if (line == null) {
			throw new RuntimeException(String.format("未找到ID为%d的线路信息", lineId));
		}
		List<Station> stations = stationRoutesHandle(line, versions, directions, stationRoutes);
		List<Section> sections = sectionRoutesHandle(line, versions, directions, sectionRoutes);

		repository.saveAll(stations);
		sectionRepository.saveAll(sections);
		lsStationRouteRepository.saveAll(stationRoutes);
		lsSectionRouteRepository.saveAll(sectionRoutes);

		return result;
	}

	private List<Station> stationRoutesHandle(Line line, Integer versions, Integer directions, List<LsStationRoute> stationRoutes) {
		List<Station> stations = new ArrayList<>();
		int currentId = repository.stationMaxId(), count = 0;
		for (int len = stationRoutes.size();count < len;) {
			LsStationRoute stationRoute = stationRoutes.get(count);
			stationRoute.setStationMark(count == 0 ? "B" : count == len - 1 ? "E" : "Z");
			count++;
			stationRoute.setLine(line);
			stationRoute.setVersions(versions);
			stationRoute.setDirections(directions);
			stationRoute.setLineCode(line.getLineCode());
			stationRoute.setStationRouteCode(100 * count);
			Station station = stationRoute.getStation();
			station.setId(currentId + count);
			station.setStationCode(station.getId().toString());
			stationRoute.setStationCode(station.getStationCode());
			String wkt = stationRoute.getStation().getCenterPointWkt();
			station.setCenterPoint((org.geolatte.geom.Point) Wkt.fromWkt(wkt));
			station.setCenterPointWgs(GeoConverter.pointBd2wgs(wkt));
			stations.add(station);
		}

		return stations;
	}

	private List<Section> sectionRoutesHandle(Line line, Integer versions, Integer directions, List<LsSectionRoute> sectionRoutes) {
		List<Section> sections = new ArrayList<>();
		int currentId = repository.stationMaxId(), count = 0;
		for (int len = sectionRoutes.size();count < len;) {
			LsSectionRoute sectionRoute = sectionRoutes.get(count++);
			sectionRoute.setLine(line);
			sectionRoute.setVersions(versions);
			sectionRoute.setDirections(directions);
			sectionRoute.setLineCode(line.getLineCode());
			sectionRoute.setSectionrouteCode(100 * count);
			String wkt = sectionRoute.getSection().getBsectionVectorWkt();
			Section section = sectionRoute.getSection();
			section.setBsectionVector((LineString) Wkt.fromWkt(wkt));
			section.setGsectionVector(GeoConverter.lineStringBd2wgs(wkt));
			section.setId(currentId + count);
			section.setSectionCode(section.getId().toString());
			sectionRoute.setSectionCode(section.getSectionCode());
			sections.add(section);
		}

		return sections;
	}

	/**
	 * @Description :TODO(根据坐标点匹配数据库中的站点)
	 * 将匹配站点的名称赋值到新的站点
	 */
	@Override
	public void matchStation(List<Station> stations) {
		Map<String, Object> param = new HashMap<>();
		param.put("destroy_eq", 0);
		List<Station> stations1 = repository.findAll(new CustomerSpecs<>(param));
		for (Station s : stations) {
			Location location = CoordinateConverter.LocationMake(s.getCenterPointWkt());
			Point point = new Point(location.getLng(), location.getLat());
			for (Station s1 : stations1) {
				Location location1 = CoordinateConverter.LocationMake(s1.getCenterPoint().toString());
				Point point1 = new Point(location1.getLng(), location1.getLat());
				if (GeoUtils.getDistance(point, point1) <= 60) {
					s.setStationName(s1.getStationName());
					break;
				}
			}
		}
	}

	@Override
	public List<Station> getStationByName(String stationName) {
		String query = "SELECT a.id,a.station_code,a.station_name,ST_AsText(a.center_point) center_point_wkt,GROUP_CONCAT(c.`name`) pass_lines FROM bsth_c_station a JOIN bsth_c_stationroute b ON a.id = b.station JOIN bsth_c_line c ON b.line = c.id WHERE a.station_name LIKE CONCAT(?,'%') AND b.destroy = 0 AND c.destroy = 0 GROUP BY a.id";
		return jdbcTemplate.query(query, new Object[]{ stationName }, BeanPropertyRowMapper.newInstance(Station.class));
	}
}