SectionServiceImpl.java 13.2 KB
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 org.springframework.transaction.annotation.Transactional;

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());
			Integer isRoadeSpeed = map.get("isRoadeSpeed").equals("") ? null : Integer.valueOf(map.get("isRoadeSpeed").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="";
			// 更新
			repository.sectionUpdate(sectionId, gsectionVector, bsectionVectorS, sectionCode, sectionName, crosesRoad, endNode, startNode, middleNode, sectionType, 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);
			route.setIsRoadeSpeed(isRoadeSpeed);
			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
	@Transactional
	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("") ? 0.0d : Double.valueOf(map.get("sectionTime").toString());
			// 路段距离.
			Double sectionDistance = map.get("sectionDistance").equals("") ? 0.0d : Double.valueOf(map.get("sectionDistance").toString());
			// 路段限速.
			Double speedLimit = map.get("speedLimit").equals("") ? 0.0d : Double.valueOf(map.get("speedLimit").toString());
			// 版本.
			Integer versions = map.get("versions").equals("") ? 1 : Integer.valueOf(map.get("versions").toString());
			// 是否撤销.
			Integer destroy = map.get("destroy").equals("") ? 0 : Integer.valueOf(map.get("destroy").toString());
			// 路段方向.
			Integer directions = map.get("directions").equals("") ? 0 : 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=null;
			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;
			}	
			routeRepository.sectionUpdSectionRouteCode(lineId, directions,routeCode);
			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);
			sectionRoute.setIsRoadeSpeed(0);
			routeRepository.save(sectionRoute);
			resultMap.put("status", ResponseCode.SUCCESS);
		} catch (Exception e) {
			resultMap.put("status", ResponseCode.ERROR);
			logger.error("save erro.", e);
		}
		return resultMap;
	}
}