StationController.java 3.73 KB
package com.bsth.controller;

import com.bsth.common.ResponseCode;
import com.bsth.entity.LsSectionRoute;
import com.bsth.entity.LsStationRoute;
import com.bsth.entity.Station;
import com.bsth.repository.StationRepository;
import com.bsth.service.StationService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 
 * @ClassName: StationController(站点控制器)
 * 
 * @Extends : BaseController
 * 
 * @Description: TODO(站点控制层)
 * 
 * @Author bsth@lq
 * 
 * @Date 2016年05月03日 上午9:21:17
 *
 * @Version 公交调度系统BS版 0.1
 * 
 */

@RestController
@RequestMapping("station")
public class StationController extends BaseController<Station, Integer> {
	
	@Autowired
	private StationService stationService;

	@Autowired
	private ObjectMapper mapper;

	private boolean executing = false;

	/** 日志记录器 */
	private static final Logger log = LoggerFactory.getLogger(StationController.class);
	
	/**
	 * @Description :TODO(根据坐标点匹配数据库中的站点)
	 */
	@RequestMapping(value="matchStation")
	public Map<String, Object> matchStation(@RequestBody List<Station> stations) {
		Map<String, Object> result = new HashMap<>();
		try {
			stationService.matchStation(stations);
			result.put("status", ResponseCode.SUCCESS);
			result.put("data", stations);
		} catch (Exception e) {
			result.put("status", ResponseCode.ERROR);
			log.error("", e);
		}

		return result;
	}

	/**
	 * 新增站点
	 * @param station
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Map<String, Object> add(Station station) {
		Map<String, Object> result = new HashMap<>();
		try {
			stationService.add(station);
			result.put("status", ResponseCode.SUCCESS);
		} catch (Exception e) {
			result.put("status", ResponseCode.ERROR);
			result.put("msg", e.getMessage());
			log.error("", e);
		}

		return result;
	}

	/**
	 * 更新站点、站点路由信息
	 * @param station
	 * @return
	 */
	@RequestMapping(value="modify" , method = RequestMethod.POST)
	public Map<String, Object> modify(Station station) {
		Map<String, Object> result = new HashMap<>();
		try {
			stationService.modify(station);
			result.put("status", ResponseCode.SUCCESS);
		} catch (Exception e) {
			result.put("status", ResponseCode.ERROR);
			throw new RuntimeException(e);
		}

		return result;
	}

	/**
	 * @Description :TODO(查询站点编码)
	 *
	 * @return int <stationCode站点编码>
	 */
	@RequestMapping(value="findStationCode" , method = RequestMethod.GET)
	public long findStationCode() {
		return stationService.findLatestStationId() + 1;
	}

	/**
	 * 根据站名模糊搜索站点信息
	 * @param stationName
	 * @return 站点列表
	 */
	@RequestMapping(value="findStationByName" , method = RequestMethod.GET)
	public List<Station> findStationByName(String stationName) {
		return stationService.findStationByName(stationName);
	}

	/**
	 * 更新所有站点途径线路信息
	 * @return
	 */
	@RequestMapping(value="generate-pass-line" , method = RequestMethod.POST)
	public Map<String, Object> generatePassLine() {
		Map<String, Object> result = new HashMap<>();
		if (executing) {
			result.put("status", ResponseCode.ERROR);
			result.put("msg", "计算正在执行中,请勿重复操作");

			return result;
		}
		executing = true;
		try {
			stationService.generatePassLine();
			result.put("status", ResponseCode.SUCCESS);
		} catch (Exception e) {
			result.put("status", ResponseCode.ERROR);
			throw new RuntimeException(e);
		} finally {
			executing = false;
		}

		return result;
	}
}