LsSectionRouteServiceImpl.java 8.88 KB
package com.bsth.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.bsth.common.ResponseCode;
import com.bsth.entity.Line;
import com.bsth.entity.LsSectionRoute;
import com.bsth.entity.Section;
import com.bsth.entity.geo_data.GeoRoad;
import com.bsth.repository.LineRepository;
import com.bsth.repository.LsSectionRouteRepository;
import com.bsth.repository.SectionRepository;
import com.bsth.service.LsSectionRouteService;
import com.bsth.service.geo_data.impl.GeoDataServiceImpl;
import com.bsth.util.Geo.Point;
import com.bsth.util.GetUIDAndCode;
import com.bsth.util.RoadCutDoubleName;
import com.bsth.util.TransGPS;
import com.bsth.util.TransGPS.Location;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 
 * @ClassName: SectionRouteServiceImpl(路段路由service业务层实现类)
 * 
 * @Extends : BaseService
 * 
 * @Description: TODO(路段路由service业务层)
 * 
 * @Author YRF
 * 
 */

@Service
public class LsSectionRouteServiceImpl extends BaseServiceImpl<LsSectionRoute, Integer> implements LsSectionRouteService {

    @Autowired
    SectionRepository repository;

    @Autowired
    LineRepository lineRepository;

    @Autowired
    LsSectionRouteRepository lsRouteRepository;

    @Autowired
    GeoDataServiceImpl geoDataServiceImpl;

    /**
     * @Description :TODO(生成双路名路段)
     * @param map <lineId:线路ID; route:路段信息>
     * @return
     */
    @Override
    @Transactional
    public Map<String, Object> doubleName(Map<String, Object> map) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            // 路段点List
            List<Point> bPointsList = new ArrayList<>();
            // 截取后的路段
            List<Map<String, String>> sectionArrayList = new ArrayList<>();
//            Integer lineId = Integer.parseInt(map.get("lineId").equals("") ? "" :map.get("lineId").toString());
            String lineCode = map.get("lineCode").equals("") ? "" :map.get("lineCode").toString();
            Integer directions = Integer.parseInt(map.get("directions").equals("") ? "" :map.get("directions").toString());
            String sectionrouteCode = map.get("sectionrouteCode").equals("") ? "" :map.get("sectionrouteCode").toString();
            Integer versions = Integer.parseInt(map.get("versions").equals("") ? "" :map.get("versions").toString());
//            String stationRouteBegin = map.get("stationBegin").equals("") ? "" :map.get("stationBegin").toString();
//            String stationRouteFinish = map.get("stationFinish").equals("") ? "" :map.get("stationFinish").toString();
            String routes = map.get("route").equals("") ? "" :map.get("route").toString();
            if(!routes.equals("")) {
                String sectionStr = "";
                // 转换成JSON数组
                JSONArray sectionsArray = JSONArray.parseArray(routes);
                // 遍历
                for(int s = 0 ;s<sectionsArray.size();s++) {
                    String pointsLngStr = sectionsArray.getJSONObject(s).get("lng").toString();
                    String pointsLatStr = sectionsArray.getJSONObject(s).get("lat").toString();
                    if(s == sectionsArray.size()-1){
                        sectionStr += pointsLngStr+","+pointsLatStr;
                    } else {
                        sectionStr += pointsLngStr+","+pointsLatStr+";";
                    }
                }
                Map<String, Object> roads = RoadCutDoubleName.start(sectionStr);
                if (!roads.isEmpty()) {
                    List bSectionList = (List) roads.get("bSections");
                    List roadName = (List) roads.get("roadName");
                    // 路段序号
                    Integer routeCode = 0;
                    if(!sectionrouteCode.equals("")){
                        String sectionrouteCodeArray[] =  sectionrouteCode.split("_");
                        routeCode = Integer.valueOf(sectionrouteCodeArray[0])+1;
                    }else {
                        routeCode = 1;
                    }
                    // 增加路段序号

                    for(int i = 0; i < bSectionList.size(); i++){
                        List<TransGPS.Location> bSection = new ArrayList<>();
                        bSectionList.get(i);
                        bSection = (List<TransGPS.Location>) bSectionList.get(i);

                        String sectionsBpoints = "";
                        // WGS线状图形坐标集合
                        String sectionsWJPpoints = "";
                        for (int j = 0; j < bSection.size(); j++) {
                            bSection.get(j);
                            TransGPS.Location point =  bSection.get(j);
                            String pointsLngStr = String.valueOf(point.getLng());
                            String pointsLatStr = String.valueOf(point.getLat());
                            /** to WGS坐标 */
                            TransGPS.Location resultPoint = FromBDPointToWGSPoint(pointsLngStr,pointsLatStr);
                            String WGSLngStr = String.valueOf(resultPoint.getLng());
                            String WGSLatStr = String.valueOf(resultPoint.getLat());
                            if(j == 0) {
                                sectionsBpoints = pointsLngStr + " " + pointsLatStr;
                                sectionsWJPpoints = WGSLngStr + " " + WGSLatStr;
                            }else {
                                sectionsBpoints = sectionsBpoints + "," +  pointsLngStr + " " + pointsLatStr;
                                sectionsWJPpoints = sectionsWJPpoints + ","  +   WGSLngStr + " " + WGSLatStr;
                            }
                        }
                        // 获取当前最大Id
                        long sectionMaxId = GetUIDAndCode.getSectionId();
                        // 路段编码
                        String sectionCode = String.valueOf(sectionMaxId);
                        // 路段ID
                        int sectionId = (int)sectionMaxId;
                        String sectionName = roadName.get(i).toString();
                        // WGS坐标点集合
                        String gsectionVector = null;
                        if(!sectionsWJPpoints.equals("")) {
                            gsectionVector = "LINESTRING(" + sectionsWJPpoints +")";
                        }
                        // 原坐标点集合
                        String bsectionVectorS = null;
                        if(!sectionsBpoints.equals("")) {
                            bsectionVectorS = "LINESTRING(" + sectionsBpoints + ")";
                        }
                        repository.systemSave(sectionCode, sectionName, null, "", "", "", gsectionVector, bsectionVectorS, "", null, "", 0, 0, "", 0, "", versions, sectionId);

                        lsRouteRepository.sectionUpdSectionRouteCode(lineCode, directions,routeCode);
                        LsSectionRoute route = new LsSectionRoute();
                        List<Line> line = lineRepository.findLineByCode(lineCode);
                        Section section = repository.findOne(sectionId);
                        route.setSectionrouteCode(routeCode+i*100);
                        route.setLineCode(lineCode);
                        route.setSectionCode(sectionCode);
                        route.setDirections(directions);
                        route.setVersions(versions);
                        route.setDestroy(0);
                        route.setLine(line.get(0));
                        route.setSection(section);
                        route.setIsRoadeSpeed(0);
                        lsRouteRepository.save(route);
                    }
                    resultMap.put("status", ResponseCode.SUCCESS);
                } else {
                    resultMap.put("status", "Failure");
                }
            }
            //返回更新之后的数据
            List<GeoRoad> list = geoDataServiceImpl.findRoadByUpdown(lineCode, directions, versions);
            resultMap.put("list", list);
//            resultMap.put("status", ResponseCode.SUCCESS);
        } catch (Exception e) {
            logger.error("", e);
            resultMap.put("status", ResponseCode.ERROR);
            resultMap.put("msg", "服务器出现异常");
        }
        return resultMap;
    }

    /** 百度坐标转WGS坐标 */
    public TransGPS.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;

    }
}