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

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bsth.common.ResponseCode;
import com.bsth.entity.Line;
import com.bsth.entity.LineVersions;
import com.bsth.entity.LsSectionRoute;
import com.bsth.entity.LsStationRoute;
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.LineVersionsRepository;
import com.bsth.repository.LsSectionRouteRepository;
import com.bsth.repository.LsStationRouteRepository;
import com.bsth.repository.SectionRepository;
import com.bsth.repository.StationRepository;
import com.bsth.service.LineVersionsService;
import com.bsth.service.SectionRouteService;
import com.bsth.service.StationRouteService;
import com.bsth.util.GetUIDAndCode;

/**
 * 
 * @ClassName: LineVersionsServiceImpl(线路service业务层实现类)
 * 
 * @Extends : BaseService
 * 
 * @Description: TODO(线路service业务层)
 * 
 * @Author bsth@lq
 *
 * @Version 公交调度系统BS版 0.1
 * 
 */

@Service
public class LineVersionsServiceImpl extends BaseServiceImpl<LineVersions, Integer> implements LineVersionsService{
	
	@Autowired
	private LineVersionsRepository repository;
	
	@Autowired
	LineRepository lineRepository;
	
	@Autowired
    SectionRouteService sectionRouteService;
    
    @Autowired
    StationRouteService stationRouteService;

    @Autowired
    StationRepository stationRepository;
    
    @Autowired
    SectionRepository sectionRepository;
    
    
    @Autowired
    com.bsth.repository.SectionRouteRepository  sectionRouteRepository ;
    
    @Autowired
    com.bsth.repository.StationRouteRepository  stationRouteRepository ;
    
    @Autowired
    LsSectionRouteRepository  lsSectionRouteRepository ;
    
    @Autowired
    LsStationRouteRepository  lsStationRouteRepository ;
	
	@Override
	public List<LineVersions> findByLineCode(int lineId) {
		return repository.findBylineId(lineId);
	}

	@Override
	public Map<String, Object> update(Map<String, Object> map) {
		Map<String, Object> resultMap = new HashMap<>();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date startDate = simpleDateFormat.parse(map.get("startDate").toString());
			Date endDate = simpleDateFormat.parse(map.get("endDate").toString());
			Integer id = Integer.valueOf(map.get("Id").toString());
			Integer lineid = Integer.valueOf(map.get("lineId").toString());
			String lineCode = map.get("lineCode").toString();
			Integer versions = Integer.valueOf(map.get("versions").toString());
			Integer status = Integer.valueOf(map.get("status").toString());
			String name = map.get("name").toString();
			String remark = map.get("remark").toString();

			Line line = lineRepository.findOne(lineid);
			
			int statu = repository.update(id,line,lineCode,new java.sql.Date(startDate.getTime()),
					new java.sql.Date(endDate.getTime()),versions,status,remark,name);
			LineVersions upLineVersions = repository.findBylineIdAndVersions(line.getId(),versions);
			if(upLineVersions != null){
				upLineVersions.setEndDate(new java.sql.Date(startDate.getTime()));
				repository.save(upLineVersions);
			}
			if (statu==1) {
				resultMap.put("status", ResponseCode.SUCCESS);
			} else {
				resultMap.put("status", ResponseCode.ERROR);
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resultMap.put("status", ResponseCode.ERROR);
		}
		return resultMap;
	}

	@Override
	public List<LineVersions> findupdated() {
		return repository.findupdated();
	}
	
	@Autowired
	JdbcTemplate jdbcTemplate;
	
	// 线路更新
	@Transactional
	@Override
	public List<LineVersions> lineUpdate() {
		List<LineVersions> list = findupdated();
    	for (LineVersions lineVersions : list) {
    		Integer lineId = lineVersions.getLine().getId();
    		String lineCode = lineVersions.getLineCode();
    		Integer versions = lineVersions.getVersions();
    		// 需要更新线路的路由
    		List<LsSectionRoute> lsSectionRoutes = lsSectionRouteRepository.findupdated(lineId, lineCode, versions);
    		List<LsStationRoute> lsStationRoutes = lsStationRouteRepository.findupdated(lineId, lineCode, versions);
    		if (lsSectionRoutes.size() != 0 && lsStationRoutes.size() != 0) {
    			// 更新
    			final List<SectionRoute> sectionRoutes = JSONArray.parseArray(JSON.toJSONString(lsSectionRoutes), SectionRoute.class);
    			final List<StationRoute> stationRoutes = JSONArray.parseArray(JSON.toJSONString(lsStationRoutes), StationRoute.class);
    			//sectionRouteService.batchUpdate(lineId, lineCode);
    			//sectionRouteRepository.batchDelete(lineId, lineCode);
    			
    			jdbcTemplate.update("delete from bsth_c_stationroute where line = ?  and line_code = ?", lineId, lineCode);
    			
    			String stationSql ="insert into bsth_c_stationroute(id,line,station,station_name,station_route_code,"
    					+ "line_code,station_code,station_mark,directions,distances,to_time,destroy,versions,"
    					+ "create_date,update_date,industry_code,station_name_en) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    			jdbcTemplate.batchUpdate(stationSql, new BatchPreparedStatementSetter() {
					
					@Override
					public void setValues(PreparedStatement ps, int i) throws SQLException {
						StationRoute sRoute = stationRoutes.get(i);
						sRoute.getStationRouteCode();
						ps.setInt(1, sRoute.getId());
						ps.setInt(2, sRoute.getLine().getId());
						ps.setInt(3, sRoute.getStation().getId());
						ps.setString(4, sRoute.getStationName());
						ps.setInt(5, sRoute.getStationRouteCode());
						ps.setString(6, sRoute.getLineCode());
						ps.setString(7, sRoute.getStationCode());
						ps.setString(8, sRoute.getStationMark());
						ps.setInt(9, sRoute.getDirections());
						ps.setDouble(10, sRoute.getDistances());
						ps.setDouble(11, sRoute.getToTime());
						ps.setInt(12, sRoute.getDestroy());
						ps.setInt(13, sRoute.getVersions());
						ps.setDate(14, new java.sql.Date(sRoute.getCreateDate().getTime()));
						ps.setDate(15, new java.sql.Date(sRoute.getUpdateDate().getTime()));	
						ps.setString(16, sRoute.getIndustryCode());		
						ps.setString(17, sRoute.getStationNameEn());		
					}
					
					@Override
					public int getBatchSize() {
						// TODO Auto-generated method stub
						return stationRoutes.size();
					}
				} );
    			jdbcTemplate.update("delete from bsth_c_sectionroute where line = ?  and line_code = ?", lineId, lineCode);
    			
    			String sectionSql ="insert into bsth_c_sectionroute(id,line_code,section_code,sectionroute_code,"
    					+ "directions,line,section,create_date,update_date,versions,"
    					+ "destroy) values(?,?,?,?,?,?,?,?,?,?,?)";
    			jdbcTemplate.batchUpdate(sectionSql, new BatchPreparedStatementSetter() {
					
					@Override
					public void setValues(PreparedStatement ps, int i) throws SQLException {
						SectionRoute sRoute = sectionRoutes.get(i);
						ps.setInt(1, sRoute.getId());
						ps.setString(2, sRoute.getLineCode());
						ps.setString(3, sRoute.getSectionCode());
						ps.setInt(4, sRoute.getSectionrouteCode());
						ps.setInt(5, sRoute.getDirections());
						ps.setInt(6, sRoute.getLine().getId());
						ps.setInt(7, sRoute.getSection().getId());
						ps.setDate(8, new java.sql.Date(sRoute.getCreateDate().getTime()));
						ps.setDate(9, new java.sql.Date(sRoute.getUpdateDate().getTime()));
						ps.setInt(10, sRoute.getVersions());
						ps.setInt(11, sRoute.getDestroy());
					}
					
					@Override
					public int getBatchSize() {
						// TODO Auto-generated method stub
						return sectionRoutes.size();
					}
				} );
    			// 更新线路版本
    			repository.updateOdlVersions(lineId, lineCode);
    			repository.updateNewVersions(lineId,lineCode,versions);
    			// 记录版本更新日志
                jdbcTemplate.update("INSERT into bsth_c_line_versions_enablog(line_code,`line_versions`,`t`) VALUES (?,?,?)",lineCode,versions, lineVersions.getStartDate().getTime());
			}
		}
    	return list;
	}

	/**
	 * 查询线路版本最大值
	 */
	@Override
	public LineVersions findLineVersionsMax(int lineId) {
		return repository.findLineVersionsMax(lineId);
	}
	

	@Override
	public List<LineVersions> findAllHistroyLineVersionsById(int lineId){
		return repository.findAllHistroyLineVersionsById(lineId);
	}

	/**
	 * 线路版本添加
	 */
	/**
	 *
	 */
	@Override
	@Transactional
	public Map<String, Object> add(Map<String, Object> map) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String, Object> resultMap = new HashMap<>();
		
		LineVersions lineVersions = new LineVersions();
		try {
			Date startDate = simpleDateFormat.parse(map.get("startDate").toString());
			Date endDate = simpleDateFormat.parse(map.get("endDate").toString());
			Line line = lineRepository.findOne(Integer.valueOf(map.get("lineId").toString()));
			Integer versions = Integer.valueOf(map.get("versions").toString());
			lineVersions.setName(map.get("name").toString());
			lineVersions.setLine(line);
			lineVersions.setLineCode(map.get("lineCode").toString());
			lineVersions.setStartDate(new java.sql.Date(startDate.getTime()));
			lineVersions.setEndDate(new java.sql.Date(endDate.getTime()));
			lineVersions.setVersions(versions);
			// 添加的线路版本默认为待更新的
			lineVersions.setStatus(2);
			lineVersions.setRemark(map.get("remark").toString());
			repository.save(lineVersions);
			// 更新上一版本时间
			LineVersions upLineVersions = repository.findBylineIdAndVersions(line.getId(),versions);
			upLineVersions.setEndDate(new java.sql.Date(startDate.getTime()));
			repository.save(upLineVersions);
			
			if(map.get("hisVersions") != null && !map.get("hisVersions").toString().isEmpty()) {
				Integer hisVersion = Integer.valueOf(map.get("hisVersions").toString());
				
				List<LsSectionRoute> sectionList = lsSectionRouteRepository.findupdated(Integer.valueOf(map.get("lineId").toString()),map.get("lineCode").toString(),hisVersion);
				
				for(int i =0;i<sectionList.size();i++) {

					LsSectionRoute oldData = sectionList.get(i);
					
					long sectionCode = sectionRepository.sectionMaxId() + 1;
					
					sectionRepository.autoCopy(versions, (int)sectionCode, oldData.getSection().getId());
					Section section = sectionRepository.findOne((int)sectionCode);

					
					LsSectionRoute newData = new LsSectionRoute();
					newData.setIsRoadeSpeed(oldData.getIsRoadeSpeed());
					newData.setDescriptions(oldData.getDescriptions());
					newData.setLine(oldData.getLine());
					newData.setLineCode(oldData.getLineCode());
					newData.setDirections(oldData.getDirections());
					newData.setSection(section);
					newData.setSectionCode(String.valueOf(sectionCode));
					newData.setSectionrouteCode(oldData.getSectionrouteCode());
					newData.setDestroy(0);
					newData.setVersions(versions);
					lsSectionRouteRepository.save(newData);
					
				}
				
				List<LsStationRoute> stationList = lsStationRouteRepository.findupdated(Integer.valueOf(map.get("lineId").toString()),map.get("lineCode").toString(),hisVersion);
				
				for(int i =0;i<stationList.size();i++) {
					

					Station newStation = stationRepository.findOne(stationList.get(i).getStation().getId());
					Station oldStation = new Station();
					long stationCode = stationRepository.stationMaxId() + 1;
					oldStation.setStationCod(String.valueOf(stationCode));
					oldStation.setId((int)stationCode);					
					oldStation.setAddr(newStation.getAddr());
					oldStation.setbJwpoints(newStation.getbJwpoints());
					oldStation.setbPolygonGrid(newStation.getbPolygonGrid());
					oldStation.setgPolygonGrid(newStation.getgPolygonGrid());
					oldStation.setCreateDate(new Date());
					oldStation.setDbType(newStation.getDbType());
					oldStation.setDescriptions(newStation.getDescriptions());
					oldStation.setDestroy(newStation.getDestroy());
					oldStation.setgLaty(newStation.getgLaty());
					oldStation.setgLonx(newStation.getgLonx());
					oldStation.setbPolygonGrid(newStation.getbPolygonGrid());
					oldStation.setIsHarbourStation(newStation.getIsHarbourStation());
					oldStation.setIsHaveLed(newStation.getIsHaveLed());
					oldStation.setIsHaveShelter(newStation.getIsHaveShelter());
					oldStation.setRadius(newStation.getRadius());
					oldStation.setRoadCoding(newStation.getRoadCoding());
					oldStation.setShapesType(newStation.getShapesType());
					oldStation.setStationName(newStation.getStationName());
					oldStation.setX(newStation.getX());
					oldStation.setY(newStation.getY());
					
					stationRepository.save(oldStation);
					
					LsStationRoute oldData = stationList.get(i);
					
					LsStationRoute newData = new LsStationRoute();
					
					newData.setLine(oldData.getLine());
					newData.setStation(oldStation);
					newData.setStationName(oldData.getStationName());
					newData.setStationNameEn(oldData.getStationNameEn());
					newData.setStationRouteCode(oldData.getStationRouteCode());
					newData.setLineCode(oldData.getLineCode());
					newData.setStationCode(String.valueOf(stationCode));
					newData.setStationMark(oldData.getStationMark());
					newData.setOutStationNmber(oldData.getOutStationNmber());
					newData.setDirections(oldData.getDirections());
					newData.setDistances(oldData.getDistances());
					newData.setToTime(oldData.getToTime());
					newData.setFirstTime(oldData.getFirstTime());
					newData.setEndTime(oldData.getEndTime());
					newData.setEndTime(oldData.getEndTime());
					newData.setDescriptions(oldData.getDescriptions());
					newData.setIndustryCode(oldData.getIndustryCode());
					newData.setDestroy(0);
					newData.setVersions(versions);
					
					lsStationRouteRepository.save(newData);
				}
				
			}
			resultMap.put("status", ResponseCode.SUCCESS);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			resultMap.put("status", ResponseCode.ERROR);
		}
		return resultMap;
	}

	/**
	 * 获取当前线路版本号
	 */
	@Override
	public Integer findCurrentVersion(int lineId) {
		return repository.findCurrentVersion(lineId);
	}

	@Override
	public Map<String, Object> issueVersion(int id) {
		Map<String, Object> resultMap = new HashMap<>();
		
			int statu = repository.issueVersion(id);
			if (statu == 1) 
				resultMap.put("status", ResponseCode.SUCCESS);
			else 
				resultMap.put("status", ResponseCode.ERROR);
		return resultMap;
	}

	@Override
	@Transactional
	public Map<String, Object> deleteAllData(int id) {
		LineVersions version = repository.findOne(id);
		
		lsSectionRouteRepository.batchDelete(version.getLine().getId(),0,version.getVersions());
		lsSectionRouteRepository.batchDelete(version.getLine().getId(),1,version.getVersions());
		lsStationRouteRepository.batchDelete(version.getLine().getId(),0,version.getVersions());
		lsStationRouteRepository.batchDelete(version.getLine().getId(),1,version.getVersions());
	
		return delete(id);
	}
}