DeviceGpsController.java 4.11 KB
package com.bsth.controller;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
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 org.springframework.web.multipart.MultipartFile;

import com.bsth.data.gpsdata.GpsEntity;
import com.bsth.data.gpsdata.GpsRealData;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@RequestMapping("devicegps")
public class DeviceGpsController {

	@Autowired
	GpsRealData gpsRealData;

	@RequestMapping(value = "/real/line/{lineCode}")
	public List<GpsEntity> findByLineCode(@PathVariable("lineCode") String lineCode) {
		return gpsRealData.getByLine(lineCode);
	}

	@RequestMapping(value = "/real/all")
	public List<GpsEntity> findByLineCodes() {
		return new ArrayList<>(gpsRealData.all());
	}
	
	@RequestMapping(value = "/open", method = RequestMethod.POST)
	public Map<String, Object> open(@RequestParam(value = "_txt_", required = false) MultipartFile file, HttpServletRequest request) {
		Map<String, Object> res = new HashMap<>();
		File rf = new File(request.getServletContext().getRealPath("/temp"), System.currentTimeMillis() + "");
		File rd = rf.getParentFile();
		if (!rd.exists()) {
			rd.mkdirs();
		}
		
		BufferedReader reader = null;
		try {
			file.transferTo(rf);
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(rf), "GBK"));
			String line = null;
			List<Map<String, Object>> data = new ArrayList<>();
			while ((line = reader.readLine()) != null) {
				if (!StringUtils.isEmpty(line)) {
					String temp[] = line.split(",");
					if (temp.length != 4) {
						res.put("errCode", 1);
						res.put("msg", "txt文档格式错误,请检查");
						return res;
					}
					Map<String, Object> info = new HashMap<>();
					info.put("lineName", temp[0]);
					info.put("lineCode", temp[1]);
					info.put("inCode", temp[2]);
					info.put("deviceId", temp[3]);
					info.put("detail", gpsRealData.get(temp[3]));
					
					data.add(info);
				}
			}
			// 删除临时文件
			rf.delete();
			res.put("errCode", 0);
			res.put("data", data);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
				try {
					if (reader != null) reader.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return res;
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/open/filter", method = RequestMethod.POST)
	public Map<String, Object> filter(@RequestParam(value = "json")String json) {
		json = StringEscapeUtils.unescapeHtml(json);
		Map<String, Object> res = new HashMap<>();
		List<Map<String, Object>> data = null;
		try {
			data = new ObjectMapper().readValue(json, List.class);
			for (Map<String, Object> info : data) {
				info.put("detail", gpsRealData.findByDeviceId((String)info.get("deviceId")));
			}
			
			res.put("errCode", 0);
			res.put("data", data);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return res;
	}
}