package com.hxzkoa.controller; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.sql.SQLException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.servlet.ModelAndView; import com.hxzkoa.json.tb_gps; import com.hxzkoa.json.tb_system; import com.hxzkoa.json.tb_tag; import com.hxzkoa.services.GpsService; import com.hxzkoa.services.LabelService; import com.hxzkoa.services.SysSettingService; import com.hxzkoa.udp.Dell_GNGGA; import com.hxzkoa.udp.Udp_Out; import com.hxzkoa.util.AddTxt; import com.hxzkoa.util.Config; import com.hxzkoa.util.ExcelUtils; import com.hxzkoa.util.ModifyConfig; import com.hxzkoa.util.PageUtil; import net.sf.json.JSONObject; @Controller public class GpsController { @Autowired private GpsService gpsService; @Autowired private LabelService labelService; @Autowired private SysSettingService sysSettingService; @ResponseBody @RequestMapping(value = "/updateperson.do",method = { RequestMethod.POST, RequestMethod.GET }) public void updateperson(HttpServletRequest request) { String strs = request.getParameter("strs"); Dell_GNGGA.dell_gps(strs); } @ResponseBody @RequestMapping(value = "/xinhuifang.do",method = { RequestMethod.POST, RequestMethod.GET }) public ArrayList xinhuifang(HttpServletRequest request) { ArrayList baowen = AddTxt.txt2String(); System.out.print(baowen.get(0)); return baowen; } @RequestMapping(value = "/huifang.do",method = { RequestMethod.POST, RequestMethod.GET }) public String huifang(HttpServletRequest request) { String toPage = "forward:/hxzk/GPS/gpsHuifang.jsp"; return toPage; } @RequestMapping(value = "/huifangdodo.do",method = { RequestMethod.POST, RequestMethod.GET }) public List huifangdodo(HttpServletRequest request) { List sb = gpsService.txt(); return sb; } @RequestMapping(value = "/gpsManagement.do", method = { RequestMethod.POST, RequestMethod.GET }) public String gpsManagement(HttpServletRequest request) { String toPage = "forward:/hxzk/GPS/gpsManagement.jsp"; List gpsManagementList = gpsService.getGpsManagement(1); request.setAttribute("gpsManagementList", gpsManagementList); List settingList = sysSettingService.getSetting(); request.setAttribute("settingList", settingList); int curPage = 1; int count = gpsService.getGpsManagementCount(); int minPage = PageUtil.getMinPage(count); request.setAttribute("pageList", PageUtil.getPage(minPage)); request.setAttribute("curPage", curPage); return toPage; } @ResponseBody @RequestMapping(value = "/gpsManagement_page.do", method = { RequestMethod.POST, RequestMethod.GET }) public String gpsManagement_page(HttpServletRequest request) { String pageStr = request.getParameter("page"); String curPageStr = request.getParameter("curPage"); int count = gpsService.getGpsManagementCount(); int minPage = PageUtil.getMinPage(count); int curPage = Integer.parseInt(curPageStr); int page = 1; if ("pre".equals(pageStr)) { if (curPage > 1) { page = curPage - 1; } } else if ("next".equals(pageStr)) { if (curPage < minPage) { page = curPage + 1; } } else { page = Integer.parseInt(pageStr); } List gpsManagementList = gpsService.getGpsManagement(page); List pageList = PageUtil.getPage(PageUtil.getLocPage(page, curPage), page, minPage); JSONObject json = new JSONObject(); json.put("dataList", gpsManagementList); json.put("pageList", pageList); json.put("curPage", page); return json.toString(); } @ResponseBody @RequestMapping(value = "/gpsManagement_search.do", method = { RequestMethod.POST, RequestMethod.GET }) public String gpsManagement_search(HttpServletRequest request) { String input = request.getParameter("input"); int curPage = 1; List pageList = new ArrayList(); List gpsManagementList = gpsService.searchGpsManagement(input); pageList.add(1); JSONObject json = new JSONObject(); json.put("dataList", gpsManagementList); json.put("pageList", pageList); json.put("curPage", curPage); return json.toString(); } @ResponseBody @RequestMapping(value = "/gpsManagement_delete.do", method = { RequestMethod.POST, RequestMethod.GET }) public void gpsManagement_delete(HttpServletRequest request) throws ParseException { String checkValStr = request.getParameter("checkVal"); checkValStr = checkValStr.replaceAll("\"", ""); String[] checkVal = checkValStr.split(","); gpsService.gpsManagement_delete(checkVal); for(int i = 0; i < checkVal.length; i++) { String xieyi = "BSTOCS1,DELETEGPS,"+checkVal[i]+",END"; Udp_Out.udp_to_cs(xieyi); } } @ResponseBody @RequestMapping(value = "/gpsManagement_export.do", method = { RequestMethod.POST, RequestMethod.GET }) public String gpsManagement_export(HttpServletRequest request, HttpServletResponse response) { String toPage = "forward:/hxzk/GPS/gpsManagement.jsp"; List gpsManagementList = gpsService.getGpsManagement(); String[] rowName = {"序号", "设备编号","纬度","经度","GPS状态","卫星数","海拔高","差分站ID","电量","更新时间"}; List dataList = objectToArray(gpsManagementList); ExcelUtils excel = new ExcelUtils("gps定位", rowName, dataList); OutputStream out; try { String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls"; String headStr = "attachment; filename=\"" + fileName + "\""; response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", headStr); out = response.getOutputStream(); excel.export(out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } return toPage; } @RequestMapping(value = "/gpsManagement_zeng.do",method = { RequestMethod.POST, RequestMethod.GET }) public ModelAndView zeng(tb_system request) throws Exception{ ModelAndView mv = new ModelAndView(); gpsService.gpszeng(request); mv.setViewName("redirect:/gpsManagement.do"); return mv; } @RequestMapping(value = "/historicalgpsLocation.do", method = { RequestMethod.POST, RequestMethod.GET }) public String historicalgpsLocation(HttpServletRequest request) throws SQLException { String toPage = "forward:/hxzk/GPS/historicalgpsLocation.jsp"; String pagePath = Config.getPageConfig(); Integer perPage = Integer.parseInt(ModifyConfig.readData(pagePath, "perPage")); // List historicalLocationList = // locationService.getRealTimeLocation(1,perPage); Date date = new Date(System.currentTimeMillis()); SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); String currentDate = formatter.format(date); List trackList = gpsService.getTrack(currentDate, 1, perPage); request.setAttribute("historicalLocationList", trackList); int curPage = 1; int count = gpsService.getgpsRealTimeLocationCount(currentDate); int minPage = PageUtil.getMinPage(count); request.setAttribute("pageList", PageUtil.getPage(minPage)); request.setAttribute("curPage", curPage); request.setAttribute("perPage", perPage); // 处理日期查询列表 List trackDateList = gpsService.getgpsTrackDateList(); List trackDateReList = new ArrayList(); trackDateReList.add(currentDate); for (int i = 0; i < trackDateList.size(); i++) { String listdate = trackDateList.get(i); if (!(currentDate).equals(listdate)) { trackDateReList.add(listdate); } } request.setAttribute("dateList", trackDateReList); return toPage; } @ResponseBody @RequestMapping(value = "/historicalgpsLocation_page.do", method = { RequestMethod.POST, RequestMethod.GET }) public String historicalgpsLocation_page(HttpServletRequest request) { String pageStr = request.getParameter("page"); String curPageStr = request.getParameter("curPage"); String date = request.getParameter("date"); int count = gpsService.getgpsRealTimeLocationCount(date); int minPage = PageUtil.getMinPage(count); int curPage = Integer.parseInt(curPageStr); String pagePath = Config.getPageConfig(); Integer perPage = Integer.parseInt(ModifyConfig.readData(pagePath, "perPage")); if (("").equals(pageStr) || pageStr == null) { pageStr = curPageStr; } int page = 1; if ("pre".equals(pageStr)) { if (curPage > 1) { page = curPage - 1; } } else if ("next".equals(pageStr)) { if (curPage < minPage) { page = curPage + 1; } } else { page = Integer.parseInt(pageStr); } // List historicalLocationList = // locationService.getRealTimeLocation(page,perPage); List trackList = gpsService.getTrack(date, page, perPage); List pageList = PageUtil.getPage(PageUtil.getLocPage(page, curPage), page, minPage); JSONObject json = new JSONObject(); json.put("dataList", trackList); json.put("pageList", pageList); json.put("curPage", page); return json.toString(); } @ResponseBody @RequestMapping(value = "/historicalgpsLocation_delete.do", method = { RequestMethod.POST, RequestMethod.GET }) public void historicalgpsLocation_delete(HttpServletRequest request) { String checkValStr = request.getParameter("checkVal"); String datea = request.getParameter("datea"); checkValStr = checkValStr.replaceAll("\"", ""); String[] checkVal = checkValStr.split(","); gpsService.realTimegpsLocation_delete(checkVal,datea); } @ResponseBody @RequestMapping(value = "/historicalgpsLocation_deleteAll.do", method = { RequestMethod.POST, RequestMethod.GET }) public void historicalLocation_deleteAll(HttpServletRequest request) { String date = request.getParameter("aaaa"); gpsService.realTimegpsLocation_deleteAll(date); } @ResponseBody @RequestMapping(value = "/getstatus.do", method = { RequestMethod.POST, RequestMethod.GET }) public String getstatus(HttpServletRequest request) { String tagid = request.getParameter("tagid"); String zhuangtai = gpsService.getpersontagid(tagid); return zhuangtai; } @ResponseBody @RequestMapping(value = "/getstatusss.do", method = { RequestMethod.POST, RequestMethod.GET }) public String getstatusss(HttpServletRequest request) { String tagid = "8888"; System.out.print("执行了"); String zhuangtai = gpsService.getpersontagid(tagid); return zhuangtai; } @ResponseBody @RequestMapping(value = "/historicalgpsLocation_search.do", method = { RequestMethod.POST, RequestMethod.GET }) public String historicalgpsLocation_search(HttpServletRequest request) { String input = request.getParameter("input"); String date = request.getParameter("tttt"); int curPage = 1; List pageList = new ArrayList(); List historicalLocationList = gpsService.searchRealTimegpsLocation(input,date); pageList.add(1); JSONObject json = new JSONObject(); json.put("dataList", historicalLocationList); json.put("pageList", pageList); json.put("curPage", curPage); return json.toString(); } @ResponseBody @RequestMapping(value = "/historicalgpsLocation_export.do", method = { RequestMethod.POST, RequestMethod.GET }) public String gpshistoricalPower_export(HttpServletRequest request, HttpServletResponse response) { String toPage = "forward:/hxzk/label/historicalgpsLocation.jsp"; String tagid = request.getParameter("tagid"); String begin = request.getParameter("begintime"); String end = request.getParameter("endtime"); String date = request.getParameter("date"); List historicalLocationList = gpsService.getRealTimegpsLocation(tagid,begin,end,date); String[] rowName = {"序号", "设备编号","纬度","经度","GPS状态","卫星数","海拔高","差分站ID","电量","更新时间"}; List dataList = objectToArray(historicalLocationList); System.out.print(dataList.get(0)); ExcelUtils excel = new ExcelUtils("GPS数据", rowName, dataList); OutputStream out; try { String fileName = "Excel-" + String.valueOf(System.currentTimeMillis()).substring(4, 13) + ".xls"; String headStr = "attachment; filename=\"" + fileName + "\""; response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", headStr); out = response.getOutputStream(); excel.export(out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } return toPage; } @ResponseBody @RequestMapping(value = "/totxt.do", method = { RequestMethod.POST, RequestMethod.GET }) public String totxt(HttpServletRequest request,HttpServletResponse response) throws IOException { String toPage = "forward:/hxzk/label/historicalgpsLocation.jsp"; String date = request.getParameter("date"); InputStream is; OutputStream os; String filename = date + ".txt"; response.setHeader("Content-Disposition", "attachement;filename=" + filename); is = new FileInputStream("C:\\" +date+ ".txt"); os = response.getOutputStream(); int len = 0; byte[] buffer = new byte[1024]; while ((len = is.read(buffer)) != -1) { os.write(buffer, 0, len); } os.close(); is.close(); return toPage; } // 对象转数组 public static List objectToArray(List sensorManagementList) { List reList = new ArrayList<>(); for (int i = 0; i < sensorManagementList.size(); i++) { List words = new ArrayList(); tb_gps gps = (tb_gps) sensorManagementList.get(i); words.add(String.valueOf(gps.getId())); words.add(gps.getTagid()); words.add(gps.getGps_weidu()); words.add(gps.getGsp_jingdu()); words.add(gps.getGps_state()); words.add(gps.getGps_num()); words.add(gps.getGps_haiba_gao()); words.add(gps.getGps_chafen_id()); words.add(gps.getGps_power()); words.add(gps.getAddtime()); String[] array = words.toArray(new String[0]); reList.add(array); } return reList; } }