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

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

import javax.persistence.criteria.CriteriaBuilder.In;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bsth.common.ResponseCode;
import com.bsth.entity.Line;
import com.bsth.entity.Section;
import com.bsth.entity.SectionRoute;
import com.bsth.entity.Station;
import com.bsth.entity.StationRoute;
import com.bsth.repository.LineRepository;
import com.bsth.repository.SectionRepository;
import com.bsth.repository.SectionRouteRepository;
import com.bsth.repository.StationRepository;
import com.bsth.repository.StationRouteRepository;
import com.bsth.service.StationService;

/**
 * 
 * @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 LineRepository lineRepository;
	
	@Autowired
	private SectionRepository sectionRepository;
	
	@Autowired
	private SectionRouteRepository sectionRouteRepository;
	
	Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@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());
			
			// 线路信息
			Line resultLine = lineRepository.findOne(lineId);
			
			if(!stationJSON.equals("")) {
				
				JSONArray stationsArray = JSONArray.parseArray(stationJSON);
				
				if(stationsArray.size()>0) {
					
					// 站点信息
					List<Station> paramStationList = new ArrayList<Station>();
					
					// 站点路由信息
					List<StationRoute> paramStationRouteList = new ArrayList<StationRoute>();
					
					// 查询最大站点ID
					int stationMaxId = repository.stationMaxId();
					
					for(int i = 0;i <stationsArray.size();i++) {
						
						// 初始化站点对象
						Station arg0 =  new Station();
						
						// 站点名称
						String stationName = stationsArray.getJSONObject(i).equals("") ? "" : stationsArray.getJSONObject(i).get("name").toString();
						arg0.setStationName(stationName);
						
						arg0.setDbType(dbType);
						
						// 站点地理位置WGS坐标经度
						String gLonx = JSONObject.parseObject(stationsArray.getJSONObject(i).get("WGSpotion").toString()).get("Lng").toString();
						arg0.setgLonx(Float.parseFloat(gLonx));
						
						// 站点地理位置WGS坐标纬度
						String gLaty = JSONObject.parseObject(stationsArray.getJSONObject(i).get("WGSpotion").toString()).get("Lat").toString();
						arg0.setgLaty(Float.parseFloat(gLaty));
						
						// 半径
						int radius =  map.get("radius").equals("") ? 0 : Integer.parseInt(map.get("radius").toString());
						arg0.setRadius(radius);
						
						// 图形类型
						String shapesType = map.get("shapesType").equals("") ? "" : map.get("shapesType").toString();
						arg0.setShapesType(shapesType);
						
						// 站点编码
						arg0.setStationCod(String.valueOf(stationMaxId+1+i));
						
						// 是否想撤销
						arg0.setDestroy(destroy);
						
						// 版本号
						arg0.setVersions(versions);
						
						// 百度经纬度坐标
						String bJwpoints = "";
						
						// 百度坐标经度
						String bLonx = JSONObject.parseObject(stationsArray.getJSONObject(i).get("potion").toString()).get("lng").toString();
						
						// 百度坐标纬度
						String bLatx = JSONObject.parseObject(stationsArray.getJSONObject(i).get("potion").toString()).get("lat").toString();
						
						// 百度经纬度
						bJwpoints = bLonx + " " + bLatx;
						arg0.setbJwpoints(bJwpoints);
						
						paramStationList.add(arg0);
						
						// 站点路由对象
						StationRoute route = new StationRoute();
						
						// 站点名称
						route.setStationName(stationName);
						
						// 站点编码
						route.setStationCode(String.valueOf(stationMaxId+1+i));
						
						// 站点序号
						route.setStationRouteCode(i+1);
						
						// 站点类型
						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(arg0);
						
						// 方向
						route.setDirections(directions);
						
						// 线路ID
						route.setLine(resultLine);
						
						// 线路编码
						route.setLineCode(resultLine.getLineCode());
						
						paramStationRouteList.add(route);
						
					}
					
					// 插入站点信息
					repository.save(paramStationList);
					
					// 插入站点路由信息 
					routeRepository.save(paramStationRouteList);
				}
				
			}
			
			// 如果路段信息JSON字符串不为空
			if(!sectionJSON.equals("")) {
				
				// 转换成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 = 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;
						
					}
					
					
				}
				
				int sectionMaxId = sectionRepository.sectionMaxId();
				
				String sectionCode = String.valueOf(sectionMaxId+1);
				
			    // 路段名称
				String sectionName = resultLine.getName();
				
				// 交出路
				String crosesRoad = "";
				
				// 终止节点
				String endNode = "";
				
				// 开始节点
				String startNode = "";
				
				// 中间节点
				String middleNode = "";
				
				// WGS坐标点集合
				String gsectionVector = "LINESTRING(" + sectionsWJPpoints +")";
				
				// 原坐标点集合
				String bsectionVector = "LINESTRING(" + sectionsBpoints + ")";
				
				// 城建坐标点集合
				String csectionVector = "";
				
				// 路段类型
				String sectionType = "";
				
				// 道路编码
				String roadCoding = "";
				
				// 路段距离
				double sectionDistance = 0;
				
				// 路段时间
				double sectionTime = 0;
				
				// 限速
				double speedLimit = Double.parseDouble(speedLimitStr);
				
				// 说明
				String descriptions = "";
				
				sectionRepository.systemSave(sectionCode, sectionName, crosesRoad, endNode, startNode, 
											
											 middleNode, gsectionVector, bsectionVector, sectionType, csectionVector, 
											 
											 roadCoding, sectionDistance, sectionTime, dbType, speedLimit, 
											 
											 descriptions, versions);
				
				
				Section section = sectionRepository.findOne(Integer.parseInt(sectionCode));
				
				// 路段路由
				SectionRoute sectionRoute = new SectionRoute();
				
				// 路段序号
				sectionRoute.setSectionrouteCode(1);
				
				// 线路编码
				sectionRoute.setLineCode(resultLine.getLineCode());
				
				// 路段ID
				sectionRoute.setSection(section);
				
				// 线路ID
				sectionRoute.setLine(resultLine);
				
				// 路段编码
				sectionRoute.setSectionCode(section.getSectionCode());
				
				// 版本
				sectionRoute.setVersions(versions);
				
				// 方向
				sectionRoute.setDirections(directions);
				
				
				sectionRouteRepository.save(sectionRoute);
			}
			
			resultMap.put("status", ResponseCode.SUCCESS);
			
		} catch (Exception e) {
			
			resultMap.put("status", ResponseCode.ERROR);
			
			logger.error("save erro.", e);
			
		}
		
		return resultMap;
	}

	@Override
	public int getStationCode() {
		
		return repository.stationMaxId();
		
	}

	@Override
	public Map<String, Object> stationSaveMap(Map<String, Object> map) {
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		try {
			
			// 站点编码
			String stationCode = map.get("stationCode").equals("") ? "" : map.get("stationCode").toString();
			
			// 站点名称
			String stationName = map.get("stationName").equals("") ? "" : map.get("stationName").toString();
			
			// 道路编码
			String roadCoding = map.get("roadCoding").equals("") ? "" : map.get("roadCoding").toString();
			
			// 原坐标类型
			String dbType = map.get("dbType").equals("") ? "" : map.get("dbType").toString();
			
			// 原坐标点
			String bJwpoints = map.get("bJwpoints").equals("") ? "" : map.get("bJwpoints").toString();
			
			// WGS经度
			Float gLonx = map.get("gLonx").equals("") ? null : Float.parseFloat(map.get("gLonx").toString());
			
			// WGS纬度
			Float gLaty = map.get("gLaty").equals("") ?  null : Float.parseFloat(map.get("gLaty").toString());
			
			// 城建坐标经度
			Float x = map.get("x").equals("") ?  null : Float.parseFloat(map.get("x").toString());
			
			// 城建坐标纬度
			Float y = map.get("y").equals("") ? null : Float.parseFloat(map.get("y").toString());
			
			// 多边形WGS坐标点集合
			String gPloygonGrid = map.get("gPloygonGrid").equals("") ? "" : map.get("gPloygonGrid").toString();
			
			// 多边形原坐标点集合
			String bPloygonGrid = map.get("bPloygonGrid").equals("") ? "" : map.get("bPloygonGrid").toString();
			
			// 是否撤销
			Integer destroy = map.get("destroy").equals("") ? null : Integer.parseInt(map.get("destroy").toString());
			
			// 圆半径
			Integer radius = map.get("radius").equals("") ? null : Integer.parseInt(map.get("radius").toString());
			
			// 图形类型
			String shapesType = map.get("shapesType").equals("") ? "" : map.get("shapesType").toString();
			
			// 版本
			Integer versions = map.get("versions").equals("") ? null : Integer.parseInt(map.get("versions").toString());
			
			// 说明
			String descriptions = map.get("descriptions").equals("") ? "" : map.get("descriptions").toString();
			
			// 创建人
			Integer createBy = map.get("createBy").equals("") ? null : Integer.parseInt(map.get("createBy").toString());
			
			// 修改人
			Integer updateBy = map.get("updateBy").equals("") ? null : Integer.parseInt(map.get("updateBy").toString());
			
			/*
			repository.stationSave(stationCode, stationName, roadCoding, dbType, bJwpoints, 
					
									gLonx, gLaty, x, y, gPloygonGrid,bPloygonGrid, destroy, radius, 
									
									shapesType, versions, descriptions, createBy, updateBy);
									*/
			
			resultMap.put("status", ResponseCode.SUCCESS);
			
			
		} catch (Exception e) {

			resultMap.put("status", ResponseCode.ERROR);
			
			logger.error("save erro.", e);
			
		}
		
		
		
		
		return resultMap;
	}

}