LineController.java 3.72 KB
package com.bsth.controller;

import com.bsth.common.ResponseCode;
import com.bsth.entity.Line;
import com.bsth.entity.LineVersions;
import com.bsth.service.LineService;
import com.bsth.service.LineVersionsService;
import com.bsth.util.GetUIDAndCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @ClassName: LineController(线路控制器)
 * 
 * @Extends : BaseController
 * 
 * @Description: TODO(线路控制层)
 * 
 * @Author bsth@lq
 * 
 * @Date 2016年4月28日 上午9:21:17
 *
 * @Version 公交调度系统BS版 0.1
 * 
 */
@RestController
@RequestMapping("line")
public class LineController extends BaseController<Line, Integer> {
	
	@Autowired
	private LineService service;
	
	@Autowired
	private LineVersionsService lineVersionsService;
	
	/**
	 * 获取线路编码与ID
	 * 
	 * @return int <lineCode:线路编码>
	 */
	@RequestMapping(value = "getLineCode", method = RequestMethod.GET)
	public long getLineCode() {
		return service.selectMaxIdToLineCode() + 1;
	}
	
	/**
	 * 验证线路编码是否存在
	 * 
	 * @return Map < {valid: true }:是否通过验证>
	 */
	@RequestMapping(value = "lineCodeVerification", method = RequestMethod.GET)
	public String lineCodeVerification(@RequestParam(defaultValue = "lineCode") String lineCode) {
		return service.lineCodeVerification(lineCode);
	}
	
	/**
	 * 
	 * 保存
	 * 
	 */
	@RequestMapping(method = RequestMethod.POST)
	public Map<String, Object> save(Line t){
		Map<String, Object> map = new HashMap<>();
		if(t.getId()==null) {
			
			t.setId(Integer.valueOf(t.getLineCode()));
			
		}
		if( (t.getId().toString().length()) > 6 || service.lineCodeVerification(t.getLineCode()).equals("false") ) {
			
			map.put("status", ResponseCode.ERROR); 
			return map;
		}
		if(t.getDestroy() == 0){
			t.setInUse(1);
		} else
			t.setInUse(0);
		// 添加线路版本
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date endDate = simpleDateFormat.parse("2088-08-08 00:00:00");
			LineVersions lineVersions = new LineVersions();
			lineVersions.setName("原始版本");
			lineVersions.setLine(t);
			lineVersions.setLineCode(t.getLineCode());
			lineVersions.setStartDate(new java.sql.Date(new Date().getTime()));
			lineVersions.setEndDate(new java.sql.Date(endDate.getTime()));// 2088-8-8 00:00:00
			lineVersions.setVersions(1);
			lineVersions.setStatus(1);
			// 先添加线路再添加版本
			service.save(t);
			return lineVersionsService.save(lineVersions);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			map.put("status", ResponseCode.ERROR); 
			return map;
		}
	} 
	
	/**
	 * 更改
	 */
	@RequestMapping(value="/update", method = RequestMethod.POST)
	public Map<String, Object> update(Line l){
		Map<String, Object> map = new HashMap<>();
		if((l.getId().toString().length()) > 6 || service.lineCodeVerification(l.getLineCode()).equals("true") ) {
			
			map.put("status", ResponseCode.ERROR); 
			return map;
		}
		return service.update(l);
	} 
	
	@RequestMapping(value ="/findById" , method = RequestMethod.GET)
	Line findByID(@RequestParam(defaultValue = "id") Integer id){
		return service.findById(id);
	}

	/**
	 * 删除线路
	 * @param id
	 * @return
	 */
	@RequestMapping(value ="/remove" , method = RequestMethod.POST)
	public Map<String, Object> remove(Integer id){
		return service.remove(id);
	}
}