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

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.bsth.common.ResponseCode;
import com.bsth.entity.Line;
import com.bsth.entity.Section;
import com.bsth.entity.SectionRoute;
import com.bsth.repository.LineRepository;
import com.bsth.repository.SectionRepository;
import com.bsth.repository.SectionRouteRepository;
import com.bsth.service.SectionService;
import com.bsth.util.TransGPS;
import com.bsth.util.TransGPS.Location;

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

@Service
public class SectionServiceImpl extends BaseServiceImpl<Section, Integer> implements SectionService{
	
	@Autowired
	SectionRepository repository;
	
	@Autowired
	LineRepository lineRepository;
	
	@Autowired
	SectionRouteRepository routeRepository;
	
	/**
	 * @Description :TODO(编辑线路走向)
	 * 
	 * @param map <sectionId:路段ID; sectionJSON:路段信息>
	 * 
	 * @return Map<String, Object> <SUCCESS ; ERROR>
	 */
	@Override
	public Map<String, Object> sectionUpdate(Map<String, Object> map) {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		try {
			
			String bsectionVector = map.get("bsectionVector").equals("") ? "" :map.get("bsectionVector").toString();
			
			// 原始线状图形坐标集合
			String sectionsBpoints = "";
			
			// WGS线状图形坐标集合
			String sectionsWJPpoints = "";
			
			if(!bsectionVector.equals("")) {
				
				// 转换成JSON数组
				JSONArray sectionsArray = JSONArray.parseArray(bsectionVector);
				
				// 遍历
				for(int s = 0 ;s<sectionsArray.size();s++) {
					
					String pointsLngStr = sectionsArray.getJSONObject(s).get("lng").toString();
					
					String pointsLatStr = sectionsArray.getJSONObject(s).get("lat").toString();
					
					/** to WGS坐标 */
					Location resultPoint = FromBDPointToWGSPoint(pointsLngStr,pointsLatStr);
					
					String WGSLngStr = String.valueOf(resultPoint.getLng());
					
					String WGSLatStr = String.valueOf(resultPoint.getLat());
					
					if(s==0) {
						
						sectionsBpoints = pointsLngStr + " " + pointsLatStr;
						
						sectionsWJPpoints = WGSLngStr + " " + WGSLatStr;
						
					}else {
						
						sectionsBpoints = sectionsBpoints + "," +  pointsLngStr + " " + pointsLatStr;
						
						sectionsWJPpoints = sectionsWJPpoints + ","  +   WGSLngStr + " " + WGSLatStr;
						
					}
					
					
				}
				
			}
			
			// 原坐标类型
			String dbType = map.get("dbType").equals("") ? "" : map.get("dbType").toString();
			
			// 说明
			String descriptions = map.get("descriptions").equals("") ? "" : map.get("descriptions").toString();
			
			// 是否撤销
			Integer destroy = map.get("destroy").equals("") ? null : Integer.parseInt(map.get("destroy").toString());
			
			// 方向
			Integer directions = map.get("directions").equals("") ? null : Integer.parseInt(map.get("directions").toString());
			
			// 线路ID
			Integer sectionRouteLine = map.get("sectionRouteLine").equals("") ? null : Integer.parseInt(map.get("sectionRouteLine").toString());
			
			// 道路编码
			String roadCoding = map.get("roadCoding").equals("") ? "" : map.get("roadCoding").toString();
			
			// 路段编码
			String sectionCode = map.get("sectionCode").equals("") ? "" : map.get("sectionCode").toString();
			
			// 路段长度
			Double sectionDistance = map.get("sectionDistance").equals("") ? null : Double.valueOf(map.get("sectionDistance").toString());
			
			// 路段ID
			Integer sectionId = map.get("sectionId").equals("") ? 0 : Integer.parseInt(map.get("sectionId").toString());
			
			// 路段名称
			String sectionName = map.get("sectionName").equals("") ? "" : map.get("sectionName").toString();
			
			// 路段路由Id
			Integer sectionRouteId = map.get("sectionRouteId").equals("") ? null : Integer.valueOf(map.get("sectionRouteId").toString());
			
			// 线路编码
			String lineCode =map.get("lineCode").equals("") ? "" : map.get("lineCode").toString();
			
			// 路段时长
			Double  sectionTime = map.get("sectionTime").equals("") ? null : Double.valueOf(map.get("sectionTime").toString());
			
			// 路段路由
			Integer sectionrouteCode = map.get("sectionrouteCode").equals("") ? null : Integer.valueOf(map.get("sectionrouteCode").toString());
			
			SectionRoute resultS = routeRepository.findOne(sectionRouteId);
			
			int old_code = resultS.getSectionrouteCode();
			
			if(sectionrouteCode!=null) {
				
				int new_code = sectionrouteCode+100;
				
				if(new_code==old_code){
					
					sectionrouteCode = new_code;
					
				}else {
					
					sectionrouteCode = new_code-100+1;
					
				}
				
			 }else {
				 
				 sectionrouteCode = old_code;
				 
			 }
			
			// 限速
			Double speedLimit = map.get("speedLimit").equals("") ? null : Double.valueOf(map.get("speedLimit").toString());
			
			// 版本
			Integer version = map.get("versions").equals("") ? null : Integer.valueOf(map.get("versions").toString());
			
			// WGS坐标点集合
			String gsectionVector = null;
			if(!sectionsWJPpoints.equals(""))
				gsectionVector = "LINESTRING(" + sectionsWJPpoints +")";
			
			// 原坐标点集合
			String bsectionVectorS = null;
			if(!sectionsBpoints.equals(""))
				 bsectionVectorS = "LINESTRING(" + sectionsBpoints + ")";
			
			Integer createBy = map.get("createBy").equals("") ? null : Integer.valueOf(map.get("createBy").toString());
			
			String createDate = map.get("createDate").equals("") ? null : map.get("createDate").toString();
			
			Integer updateBy = map.get("updateBy").equals("") ?null : Integer.valueOf(map.get("updateBy").toString());
			
			SimpleDateFormat formatter = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); 
			
			Date date = new Date();
			
			// 修改日期
			String updateDate = formatter.format(date);
			
			String crosesRoad="";
			
			String endNode="";
			
			String startNode="";
			
			String middleNode="";
			
			String sectionType="";
			
			String csectionVector="";
			
			// 更新
			repository.sectionUpdate(sectionId, gsectionVector, bsectionVectorS, sectionCode, sectionName, crosesRoad, endNode, startNode, middleNode, sectionType, csectionVector, roadCoding, sectionDistance, sectionTime, dbType, speedLimit, descriptions, version, createBy, createDate, updateBy, updateDate);
			
			SectionRoute route = new SectionRoute();
			
			Line line = lineRepository.findOne(sectionRouteLine);
			
			Section section = repository.findOne(sectionId);
			
			route.setId(sectionRouteId);
			
			route.setSectionrouteCode(sectionrouteCode);
			
			route.setLineCode(lineCode);
			
			route.setSectionCode(sectionCode);
			
			route.setDirections(directions);
			
			route.setVersions(version);
			
			route.setDestroy(destroy);
			
			route.setDescriptions(descriptions);
			
			route.setCreateBy(createBy);
			
			route.setUpdateBy(updateBy);
			
			route.setLine(line);
			
			route.setSection(section);
			
			routeRepository.save(route);
			
			resultMap.put("status", ResponseCode.SUCCESS);
			
		} catch (Exception e) {

			resultMap.put("status", ResponseCode.ERROR);
			
			logger.error("save erro.", e);
			
		}
		
		return resultMap;
	}
	
	/** 百度坐标转WGS坐标 */
	public Location FromBDPointToWGSPoint(String bLonx,String bLatx) {
		
		double lng = Double.parseDouble(bLonx);
		
		double lat = Double.parseDouble(bLatx);
		
		Location bdLoc = TransGPS.LocationMake(lng, lat);
		
		Location location = TransGPS.bd_decrypt(bdLoc);
		
		Location WGSPoint = TransGPS.transformFromGCJToWGS(location);
		
		return WGSPoint;
		
	}
	
	
	/**
	 * 新增路段信息
	 * 
	 * @param map:<bsectionVector:折线百度坐标集合;dbType:圆坐标类型;descriptions:描述与说明;destroy:是否撤销;directions:方向;lineId:线路ID
	 * 
	 * 			   lineCode :线路编码;roadCoding:道路编码;sectionCode:路段编码;sectionDistance:路段长度;sectionName:路段名称;sectionTime:路段时长;
	 * 			
	 * 			   sectionrouteCode:路段序号;speedLimit:路段限速>
	 * 
	 * @return map<SUCCESS:成功;ERROR:异常>
	 */
	@Override
	public Map<String, Object> sectionSave(Map<String, Object> map) {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		try {
			
			/** 线路ID */
			Integer lineId = map.get("lineId").equals("") ? null : Integer.valueOf(map.get("lineId").toString());
			
			/** 线路编码 */
			String lineCode = map.get("lineCode").equals("") ? "" : map.get("lineCode").toString();
			
			String sectionName = map.get("sectionName").equals("") ? "" : map.get("sectionName").toString();
			
			String sectionCode = map.get("sectionCode").equals("") ? "" : map.get("sectionCode").toString();
			
			String roadCoding = map.get("roadCoding").equals("") ? "" : map.get("roadCoding").toString();
			
			String dbType = map.get("dbType").equals("") ? "" :map.get("dbType").toString();
			
			String sectionJSON = map.get("bsectionVector").equals("") ? "" : map.get("bsectionVector").toString();
			
			String sectionrouteCode = map.get("sectionrouteCode").equals("") ? "" : map.get("sectionrouteCode").toString();
			
			Double sectionTime = map.get("sectionTime").equals("") ? null : Double.valueOf(map.get("sectionTime").toString());
			
			Double sectionDistance = map.get("sectionDistance").equals("") ? null : Double.valueOf(map.get("sectionDistance").toString());
			
			Double speedLimit = map.get("speedLimit").equals("") ? null : Double.valueOf(map.get("speedLimit").toString());
			
			Integer versions = map.get("versions").equals("") ? null : Integer.valueOf(map.get("versions").toString());
			
			Integer destroy = map.get("destroy").equals("") ? null : Integer.valueOf(map.get("destroy").toString());
			
			Integer directions = map.get("directions").equals("") ? null : Integer.valueOf(map.get("directions").toString());
			
			String descriptions = map.get("descriptions").equals("")? "" : map.get("descriptions").toString();
			
			// 原始线状图形坐标集合
			String sectionsBpoints = "";
			
			// WGS线状图形坐标集合
			String sectionsWJPpoints = "";
			
			if(!sectionJSON.equals("")) {
				
				// 转换成JSON数组
				JSONArray sectionsArray = JSONArray.parseArray(sectionJSON);
				
				// 遍历
				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 = FromBDPointToWGSPoint(pointsLngStr,pointsLatStr);
					
					WGSLngStr = String.valueOf(resultPoint.getLng());
					
					WGSLatStr = String.valueOf(resultPoint.getLat());			
					
					if(s==0) {
						
						sectionsBpoints = pointsLngStr + " " + pointsLatStr;
						
						sectionsWJPpoints = WGSLngStr + " " + WGSLatStr;
						
					}else {
						
						sectionsBpoints = sectionsBpoints + "," +  pointsLngStr + " " + pointsLatStr;
						
						sectionsWJPpoints = sectionsWJPpoints + ","  +   WGSLngStr + " " + WGSLatStr;
						
					}
					
				}
				
			}
			
			// WGS坐标点集合
			String gsectionVector = "LINESTRING(" + sectionsWJPpoints +")";
			
			// 原坐标点集合
			String bsectionVector = "LINESTRING(" + sectionsBpoints + ")";
			
			String crosesRoad="";
			
			String endNode ="";
			
			String startNode="";
			
			String middleNode="";
			
			String sectionType="";
			
			String csectionVector="";
			
			Integer id = Integer.valueOf(sectionCode);
			
			repository.systemSave(sectionCode, sectionName, crosesRoad, endNode, startNode, middleNode, gsectionVector, bsectionVector, sectionType, csectionVector, roadCoding, sectionDistance, sectionTime, dbType, speedLimit, descriptions, versions, id);
			
			Section section = repository.findOne(id);
			
			Line line = lineRepository.findOne(lineId);
			
			// 路段路由
			SectionRoute sectionRoute = new SectionRoute();
			
			Integer routeCode = null;
			
			if(!sectionrouteCode.equals("")){
				
				String sectionrouteCodeArray[] =  sectionrouteCode.split("_");
				
				routeCode = Integer.valueOf(sectionrouteCodeArray[0])+1;
				
			}else {
				
				routeCode = 100;
				
			}	
			
			sectionRoute.setSectionrouteCode(routeCode);
			
			sectionRoute.setLineCode(lineCode);
			
			sectionRoute.setSection(section);
			
			sectionRoute.setSectionCode(sectionCode);
			
			sectionRoute.setDirections(directions);
			
			sectionRoute.setDescriptions(descriptions);
			
			sectionRoute.setDestroy(destroy);
			
			sectionRoute.setVersions(versions);
			
			sectionRoute.setLine(line);
			
			routeRepository.save(sectionRoute);
			
			resultMap.put("status", ResponseCode.SUCCESS);
			
		} catch (Exception e) {
			
			resultMap.put("status", ResponseCode.ERROR);
			
			logger.error("save erro.", e);
			
		}
		
		return resultMap;
	}

}