SectionRouteServiceImpl.java 15.6 KB
package com.bsth.service.impl;

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

import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import com.bsth.common.ResponseCode;
import com.bsth.entity.Line;
import com.bsth.entity.Section;
import com.bsth.entity.SectionRoute;
import com.bsth.entity.search.CustomerSpecs;
import com.bsth.repository.LineRepository;
import com.bsth.repository.SectionRepository;
import com.bsth.repository.SectionRouteCacheRepository;
import com.bsth.repository.SectionRouteRepository;
import com.bsth.service.SectionRouteService;
import com.bsth.util.GetUIDAndCode;

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

@Service
public class SectionRouteServiceImpl extends BaseServiceImpl<SectionRoute, Integer> implements SectionRouteService{
	
	@Autowired
	SectionRouteRepository repository;
	
	@Autowired
	SectionRouteCacheRepository Cacherepository;
	
	@Autowired
	SectionRepository repository2; 
	
	@Autowired
	LineRepository lineRepository;
	
	@Autowired
    private EntityManager entityManager;
	
	@Value("${hibernate.jdbc.batch_size}")
    private int batchSize;
	
	@Override
	public Iterable<SectionRoute> list(Map<String, Object> map) {
		List<Sort.Order> orderList = new ArrayList<>();
		orderList.add(new Sort.Order(Direction.ASC, "directions"));
		orderList.add(new Sort.Order(Direction.ASC, "sectionrouteCode"));
		return repository.findAll(new CustomerSpecs<SectionRoute>(map), new Sort(orderList));
	}
	
	/**
	 * @Description :TODO(查询路段信息)
	 * 
	 * @param map <line.id_eq:线路ID; directions_eq:方向>
	 * 
	 * @return Map<String, Object> 
	 */
	@Override
	public List<Map<String, Object>> getSectionRoute(Map<String, Object> map) {
		int lineId = map.get("line.id_eq").equals("") ? 0 : Integer.parseInt(map.get("line.id_eq").toString());
		int directions = map.get("directions_eq").equals("") ? 0 : Integer.parseInt(map.get("directions_eq").toString());
		List<Object[]> listObjArray = repository.getSectionRoute(lineId, directions);
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>(); 
		if(listObjArray.size()>0) {
			for(int i = 0 ; i<listObjArray.size() ; i++){
				Map<String, Object> tempM = new HashMap<String, Object>();
				tempM.put("sectionrouteId",listObjArray.get(i)[0]);
				tempM.put("sectionrouteLine",listObjArray.get(i)[1]);
				tempM.put("sectionrouteLineCode",listObjArray.get(i)[2]);
				tempM.put("sectionrouteSection",listObjArray.get(i)[3]);
				tempM.put("sectionrouteSectionCode",listObjArray.get(i)[4]);
				tempM.put("sectionrouteCode",listObjArray.get(i)[5]);
				tempM.put("sectionrouteDirections",listObjArray.get(i)[6]);
				tempM.put("sectionId",listObjArray.get(i)[7]);
				tempM.put("sectionCode",listObjArray.get(i)[8]);
				tempM.put("sectionName",listObjArray.get(i)[9]);
				tempM.put("sectionCrosesRoad",listObjArray.get(i)[10]);
				tempM.put("sectionEndNode",listObjArray.get(i)[11]);
				tempM.put("sectionStartNode",listObjArray.get(i)[12]);
				tempM.put("sectionMiddleNode",listObjArray.get(i)[13]);
				tempM.put("sectionType",listObjArray.get(i)[14]);
				tempM.put("sectionCsectionVector",listObjArray.get(i)[15]);
				tempM.put("sectionBsectionVector",listObjArray.get(i)[16]);
				tempM.put("sectionGsectionVector",listObjArray.get(i)[17]);
				tempM.put("sectionRoadCoding",listObjArray.get(i)[18]);
				tempM.put("sectionDistance",listObjArray.get(i)[19]);
				tempM.put("sectionTime",listObjArray.get(i)[20]);
				tempM.put("sectiondbType",listObjArray.get(i)[21]);
				tempM.put("sectionSpeedLimet",listObjArray.get(i)[22]);
				tempM.put("destroy",listObjArray.get(i)[23]);
				tempM.put("versions",listObjArray.get(i)[24]);
				tempM.put("descriptions",listObjArray.get(i)[25]);
				tempM.put("isRoadeSpeed",listObjArray.get(i)[26]);
				resultList.add(tempM);
			}
		}
		return resultList;
	}
	
	/**
	 * @Description :TODO(查询路段信息)
	 * 
	 * @param map <line.id_eq:线路ID; directions_eq:方向>
	 * 
	 * @return Map<String, Object> 
	 */
	@Transactional
	@Override
	public List<Map<String, Object>> getSectionRouteCache(Map<String, Object> map) {
		int lineId = map.get("line.id_eq").equals("") ? 0 : Integer.parseInt(map.get("line.id_eq").toString());
		int directions = map.get("directions_eq").equals("") ? 0 : Integer.parseInt(map.get("directions_eq").toString());
		List<Object[]> listObjArray = Cacherepository.getSectionRouteCache(lineId, directions);
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>(); 
		if(listObjArray.size()>0) {
			for(int i = 0 ; i<listObjArray.size() ; i++){
				Map<String, Object> tempM = new HashMap<String, Object>();
				tempM.put("sectionrouteId",listObjArray.get(i)[0]);
				tempM.put("sectionrouteLine",listObjArray.get(i)[1]);
				tempM.put("sectionrouteLineCode",listObjArray.get(i)[2]);
				tempM.put("sectionrouteSection",listObjArray.get(i)[3]);
				tempM.put("sectionrouteSectionCode",listObjArray.get(i)[4]);
				tempM.put("sectionrouteCode",listObjArray.get(i)[5]);
				tempM.put("sectionrouteDirections",listObjArray.get(i)[6]);
				tempM.put("sectionId",listObjArray.get(i)[7]);
				tempM.put("sectionCode",listObjArray.get(i)[8]);
				tempM.put("sectionName",listObjArray.get(i)[9]);
				tempM.put("sectionCrosesRoad",listObjArray.get(i)[10]);
				tempM.put("sectionEndNode",listObjArray.get(i)[11]);
				tempM.put("sectionStartNode",listObjArray.get(i)[12]);
				tempM.put("sectionMiddleNode",listObjArray.get(i)[13]);
				tempM.put("sectionType",listObjArray.get(i)[14]);
				tempM.put("sectionCsectionVector",listObjArray.get(i)[15]);
				tempM.put("sectionBsectionVector",listObjArray.get(i)[16]);
				tempM.put("sectionGsectionVector",listObjArray.get(i)[17]);
				tempM.put("sectionRoadCoding",listObjArray.get(i)[18]);
				tempM.put("sectionDistance",listObjArray.get(i)[19]);
				tempM.put("sectionTime",listObjArray.get(i)[20]);
				tempM.put("sectiondbType",listObjArray.get(i)[21]);
				tempM.put("sectionSpeedLimet",listObjArray.get(i)[22]);
				tempM.put("destroy",listObjArray.get(i)[23]);
				tempM.put("versions",listObjArray.get(i)[24]);
				tempM.put("descriptions",listObjArray.get(i)[25]);
				tempM.put("isRoadeSpeed",listObjArray.get(i)[26]);
				resultList.add(tempM);
			}
		}
		return resultList;
	}
		
	
	/**
	 * @Description : TODO(根据路段路由Id查询详情)
	 * 
	 * @param  map <id:路段路由ID> 
	 * 
	 * @return List<Map<String, Object>>
	 */
	@Override
	public List<Map<String, Object>> findSectionRouteInfoFormId(Map<String, Object> map) {

		// 获取线路ID
	    Integer id = map.get("id").equals("") ? 0 : Integer.parseInt(map.get("id").toString());
		
	    List<Object[]> objects = repository.findSectionRouteInfoFormId(id);
	    
	    List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();	 
	    
	    int len = objects.size();
	    
	    if(objects.size()>0) {
	    	
	    	for(int i = 0 ; i < len; i++) {
	    		Map<String, Object> tempM = new HashMap<String,Object>();
	    		
	    		tempM.put("sectionRouteId", objects.get(i)[0]);
	    		
	    		tempM.put("sectionRouteLineCode", objects.get(i)[1]);
	    		
	    		tempM.put("sectionRouteCode", objects.get(i)[2]);
	    		
	    		tempM.put("sectionRouteDirections", objects.get(i)[3]);
	    		
	    		tempM.put("sectionRouteLine", objects.get(i)[4]);
	    		
	    		tempM.put("sectionRouteSection", objects.get(i)[5]);
	    		
	    		tempM.put("sectionRouteDescriptions", objects.get(i)[6]);
	    		
	    		tempM.put("sectionRouteCreateBy", objects.get(i)[7]);
	    		
	    		tempM.put("sectionRouteCreateDate", objects.get(i)[8]);
	    		
	    		tempM.put("sectionRouteUpdateBy", objects.get(i)[9]);
	    		
	    		tempM.put("sectionRouteUpdateDate", objects.get(i)[10]);
	    		
	    		tempM.put("sectionRouteVersions", objects.get(i)[11]);
	    		
	    		tempM.put("sectionRouteDestroy", objects.get(i)[12]);
	    		
	    		tempM.put("sectionId", objects.get(i)[13]);
	    		
	    		tempM.put("sectionCode", objects.get(i)[14]);
	    		
	    		tempM.put("sectionName", objects.get(i)[15]);
	    		
	    		tempM.put("sectionRoadCoding", objects.get(i)[16]);
	    		
	    		tempM.put("sectionEndCode", objects.get(i)[17]);
	    		
	    		tempM.put("sectionStartNode", objects.get(i)[18]);
	    		
	    		tempM.put("sectionMiddleNode", objects.get(i)[19]);
	    		
	    		tempM.put("sectionType", objects.get(i)[20]);
	    		
	    		tempM.put("sectionCsectionVector", objects.get(i)[21]);
	    		
	    		tempM.put("sectionBsectionVector", objects.get(i)[22]);
	    		
	    		tempM.put("sectionGsectionVector", objects.get(i)[23]);
	    		
	    		tempM.put("sectionDistance", objects.get(i)[24]);
	    		
	    		tempM.put("sectionTime", objects.get(i)[25]);
	    		
	    		tempM.put("sectionDbtype", objects.get(i)[26]);
	    		
	    		tempM.put("sectionSpeedLimit", objects.get(i)[27]);
	    		
	    		tempM.put("sectionDescriptions", objects.get(i)[28]);
	    		
	    		tempM.put("sectionCreateBy", objects.get(i)[29]);
	    		
	    		tempM.put("sectionCreateDate", objects.get(i)[30]);
	    		
	    		tempM.put("sectionUpdateBy", objects.get(i)[31]);
	    		
	    		tempM.put("sectionUpdateDate", objects.get(i)[32]);
	    		
	    		tempM.put("sectionVersion", objects.get(i)[33]);
	    		
	    		tempM.put("isRoadeSpeed", objects.get(i)[34]);
	    		resultList.add(tempM);
	    	}
	    	
	    }
	    
		return resultList;
	}
	
	
	/**
	 * @Description :TODO(查询线路某方向下的上一个路段序号)
	 * 
	 * @param map <lineId:线路ID; direction:方向;sectionRouteCode:路段编码>
	 * 
	 * @return List<Map<String, Object>> 
	 */
	@Override
	public List<Map<String, Object>> findUpSectionRouteCode(Map<String, Object> map) {
		Integer lineId = map.get("lineId").equals("") ? null : Integer.parseInt(map.get("lineId").toString());
		Integer direction = map.get("direction").equals("") ? null : Integer.parseInt(map.get("direction").toString());
		Integer stationRouteCode = map.get("sectionRouteCode").equals("") ? null : Integer.parseInt(map.get("sectionRouteCode").toString());
		List<Object[]> reslutList = repository.findUpSectionRouteCode(lineId, direction, stationRouteCode);
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		if(reslutList.get(0)!=null) {
			for(int i = 0 ; i <reslutList.size() ;i++){
				Map<String, Object> tempM = new HashMap<String, Object>();
				tempM.put("sectionrouteCode", reslutList.get(i));
				list.add(tempM);
			}
		}
		return list;
	}

	@Override
	public Map<String, Object> quoteSection(Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			Integer line = map.get("lineId") ==null ? null : Integer.parseInt(map.get("lineId").toString());
			Integer dir = map.get("dir")==null ? null : Integer.parseInt(map.get("dir").toString());
			Integer toDir = map.get("toDir")==null ? null : Integer.parseInt(map.get("toDir").toString());
			if(line!=null && dir!=null) {
				List<Object[]> list = repository.getSectionRoute(line, dir);
				int src = repository.sectionRouteCodeMaxId();
				for(int i = 0 ; i<list.size() ; i++) {
					/*int sectionId =  Integer.parseInt(String.valueOf(repository2.sectionMaxId())) + 1 ;*/
					long sectionMaxId =  GetUIDAndCode.getSectionId();
					String sectionCode =  String.valueOf(sectionMaxId);
					int sectionId = (int)sectionMaxId;
					String sectionName = list.get(i)[9] == null ? null : list.get(i)[9].toString(); 
					String crosesRoad = list.get(i)[10] == null ? null : list.get(i)[10].toString(); 
					String endNode = list.get(i)[11] == null ? null : list.get(i)[11].toString(); 
					String startNode = list.get(i)[12] == null ? null : list.get(i)[12].toString(); 
					String middleNode = list.get(i)[13] == null ? null : list.get(i)[13].toString(); 
					String sectionType = list.get(i)[14] == null ? null : list.get(i)[14].toString();
					String csectionVector = list.get(i)[15] == null ? null : list.get(i)[15].toString();
					String bsectionVector = list.get(i)[16] == null ? null : list.get(i)[16].toString();
					String gsectionVector = list.get(i)[17] == null ? null : list.get(i)[17].toString();
					String roadCoding = list.get(i)[18] == null ? null : list.get(i)[18].toString();
					Double sectionDistance = list.get(i)[19] == null ? null : Double.parseDouble(list.get(i)[19].toString());
					Double sectionTime = list.get(i)[20] == null ? null : Double.parseDouble(list.get(i)[20].toString());
					String dbType = list.get(i)[21] == null ? null : list.get(i)[21].toString();
					Double speedLimit = list.get(i)[22] == null ? null : Double.parseDouble(list.get(i)[22].toString());
					Integer destroy = list.get(i)[23] == null ? null : Integer.parseInt(list.get(i)[23].toString());
					Integer versions = list.get(i)[24] == null ? null : Integer.parseInt(list.get(i)[24].toString());
					String descriptions = list.get(i)[25] == null ? null : list.get(i)[25].toString();
					repository2.systemSave(sectionCode, 
							sectionName, crosesRoad, endNode, startNode, middleNode, 
							gsectionVector, bsectionVector, 
							sectionType, csectionVector, roadCoding, 
							sectionDistance, sectionTime, dbType, speedLimit, descriptions, versions, sectionId);
					// int sectionRouteId = Integer.parseInt(String.valueOf(repository.sectionRouteMaxId())) + 1 ;
					SectionRoute sr = new SectionRoute();
					Line lineObject = lineRepository.findOne(line);
					Section section = repository2.findOne(sectionId);
					Integer xh = (src ==  0 ? (toDir ==1? (list.size()-i)*10 : (list.size()+i)*10) : toDir==1? (src + (list.size()-i)*10) : (src + (list.size()+i)*10)) ;
					// sr.setId(sectionRouteId);
					sr.setSectionrouteCode(xh);
					sr.setLineCode(lineObject.getLineCode());
					sr.setSectionCode(sectionCode);
					sr.setDirections(toDir);
					sr.setVersions(versions);
					sr.setDestroy(destroy);
					sr.setLine(lineObject);
					sr.setSection(section);
					sr.setSectionCode(section.getSectionCode());
					sr.setDescriptions(descriptions);
					sr.setIsRoadeSpeed(1);
					repository.save(sr);
				}
			}
			resultMap.put("status", ResponseCode.SUCCESS);
		} catch (Exception e) {
			resultMap.put("status", ResponseCode.ERROR);
			logger.error("save erro.", e);
		}
		return resultMap;
	}
	
	/**
	 * @Description : TODO(根据路段路由Id批量撤销路段)
	 * 
	 * @param  map <id:路段路由ID> 
	 */
	@Override
	@Transactional
	public Map<String, Object> updateSectionRouteInfoFormId(Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			//int i = 0;
			String[] idsStr = map.get("ids").toString().split(",") ;
	        for (String id : idsStr) {
	            repository.sectionRouteIsDestroyUpdBatch(Integer.parseInt(id));
	        }
			resultMap.put("status", ResponseCode.SUCCESS);
		} catch (Exception e) {
			resultMap.put("status", ResponseCode.ERROR);
			logger.error("save erro.", e);
		}
		return resultMap;
	}
}