标签:
package main.java.com.zte.controller.system; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import net.sf.json.JSONArray; import org.apache.shiro.SecurityUtils; import org.apache.shiro.session.Session; import org.apache.shiro.subject.Subject; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import main.java.com.zte.controller.base.BaseController; import main.java.com.zte.entity.Page; import main.java.com.zte.entity.Task; import main.java.com.zte.entity.charts.Option; import main.java.com.zte.entity.charts.Series; import main.java.com.zte.service.TaskService; import main.java.com.zte.util.AppUtil; import main.java.com.zte.util.Const; import main.java.com.zte.util.DateUtil; import main.java.com.zte.util.FileUpload; import main.java.com.zte.util.ObjectExcelView; import main.java.com.zte.util.PageData; import main.java.com.zte.util.PathUtil; import main.java.com.zte.util.Tools; /** * 任务Controller * * @author 10183302 * */ @Controller @RequestMapping(value = "/task") public class TaskController extends BaseController { @Resource(name = "taskService") private TaskService taskService; /** * 显示任务列表 * * @param model * @return */ @RequestMapping(value = "/list") public ModelAndView list(Page page) throws Exception { ModelAndView mv = this.getModelAndView(); PageData pd = new PageData(); try { pd = this.getPageData(); Subject currentUser = SecurityUtils.getSubject(); Session session = currentUser.getSession(); String userName = (String) session .getAttribute(Const.SESSION_USERNAME); // 获取session中的用户名 pd.put(Const.SESSION_USERNAME, userName); String keyw = pd.getString("keyword"); if (Tools.notEmpty(keyw)) { keyw = keyw.trim(); pd.put("keyw", keyw); } page.setPd(pd); List<PageData> taskList = taskService.listTaskPage(page); mv.addObject("taskList", taskList); mv.setViewName("task/task_list"); } catch (Exception e) { logger.error(e.toString(), e); } return mv; } /** * 请求新增任务页面 * * @param model * @return */ @RequestMapping(value = "/toAdd") public ModelAndView toAdd() throws Exception { ModelAndView mv = this.getModelAndView(); try { PageData pd = new PageData(); pd = this.getPageData(); String ffile = DateUtil.getDays(); String filePath = PathUtil.getClasspath() + Const.FILEPATH_LOG + ffile; // 文件上传路径 Subject currentUser = SecurityUtils.getSubject(); Session session = currentUser.getSession(); String userName = (String) session .getAttribute(Const.SESSION_USERNAME); // 获取session中的用户名 String taskTime = DateUtil.getTime().toString(); pd.put("taskId", this.get32UUID()); pd.put("taskNumber", taskTime); pd.put("name", "分析任务" + Tools.date2Str(new Date())); pd.put("owner", userName); pd.put("time", Tools.date2Str(new Date())); pd.put("logPath", filePath); pd.put("logURL", filePath); pd.put("type", "离线任务"); // 状态 pd.put("status", "待启动"); // 状态 taskService.add(pd); mv.addObject("msg", "success"); } catch (Exception e) { logger.error(e.toString(), e); mv.addObject("msg", "failed"); } mv.setViewName("task/task_add"); return mv; } /** * 新增 */ @RequestMapping(value = "/save") @ResponseBody public Object save(@RequestParam(required = false) MultipartFile file) throws Exception { logBefore(logger, "上传日志"); PageData pd = new PageData(); Map<String, String> map = new HashMap<String, String>(); try { Subject currentUser = SecurityUtils.getSubject(); Session session = currentUser.getSession(); String userName = (String) session .getAttribute(Const.SESSION_USERNAME); // 获取session中的用户名 String ffile = DateUtil.getDays(), fileName = "", filePath = ""; if (null != file && !file.isEmpty()) { filePath = PathUtil.getClasspath() + Const.FILEPATH_LOG + ffile; // 文件上传路径 fileName = FileUpload.fileUp(file, filePath, this.get32UUID()); // 执行上传 System.out.println("文件名称:" + file.getName()); System.out.println("文件路径:" + filePath); } else { System.out.println("上传失败"); } pd.put("logId", this.get32UUID()); // 主键 pd.put("taskNumber", getTaskNumber()); // 任务编号 pd.put("userName", userName); // 上传者 pd.put("fileName", fileName); // 文件名 pd.put("filePath", filePath + "/" + fileName); // 路径 pd.put("createTime", Tools.date2Str(new Date())); // 创建时间 pd.put("remarks", ""); // 备注 taskService.saveLogInfo(pd); map.put("result", "ok"); } catch (Exception e) { logger.error(e.toString(), e); } return AppUtil.returnObject(pd, map); } public static String getTaskNumber() { int r1 = (int) (Math.random() * (10));// 产生2个0-9的随机数 int r2 = (int) (Math.random() * (10)); long now = System.currentTimeMillis();// 一个13位的时间戳 String taskId = String.valueOf(now) + String.valueOf(r1) + String.valueOf(r2);// 任务ID return taskId; } public static void main(String[] args) { System.out.println(getTaskNumber()); } /** * 保存任务信息 * * @param menu * @param model * @return */ @RequestMapping(value = "/add") public ModelAndView add(Task task) throws Exception { ModelAndView mv = this.getModelAndView(); PageData pd = new PageData(); pd = this.getPageData(); Subject currentUser = SecurityUtils.getSubject(); Session session = currentUser.getSession(); String userName = (String) session.getAttribute(Const.SESSION_USERNAME); // 获取session中的用户名 String taskName = pd.getString("taskName").trim(); String taskTime = DateUtil.getTime().toString(); try { pd.put("taskTime", taskTime); pd.put("taskStatus", "待启动"); // 状态 // 目录格式:根目录/userName/taskName-时间 pd.put("task_dir", Const.TASK_ROOT_DIR + userName + "/" + taskName + "-" + taskTime); taskService.add(pd); mv.addObject("msg", "success"); } catch (Exception e) { logger.error(e.toString(), e); mv.addObject("msg", "failed"); } mv.setViewName("save_result"); return mv; } /** * 请求编辑页面 * * @param * @return */ @RequestMapping(value = "/toEdit") public ModelAndView toEdit(String taskId) throws Exception { ModelAndView mv = this.getModelAndView(); PageData pd = new PageData(); try { pd = this.getPageData(); pd.put("taskId", taskId); List<PageData> taskList = taskService.getTaskByTaskId(pd); mv.addObject("taskList", taskList); mv.addObject("pd", pd); mv.setViewName("task/task_edit"); } catch (Exception e) { logger.error(e.toString(), e); } return mv; } /** * 保存编辑 * * @param * @return */ @RequestMapping(value = "/edit") public ModelAndView edit() throws Exception { ModelAndView mv = this.getModelAndView(); PageData pd = new PageData(); try { pd = this.getPageData(); pd = taskService.edit(pd); mv.addObject("msg", "success"); } catch (Exception e) { logger.error(e.toString(), e); mv.addObject("msg", "failed"); } mv.setViewName("save_result"); return mv; } /** * 获取任务分析结果 * * @param * @return */ @RequestMapping(value = "/reslut") public ModelAndView reslut(Page page, String taskId) throws Exception { ModelAndView mv = this.getModelAndView(); PageData pd = new PageData(); try { pd = this.getPageData(); pd.put("taskId", taskId); page.setPd(pd); List<PageData> pmResults = taskService.listPmResult(page); mv.addObject("pmResults", pmResults); mv.addObject("pd", pd); if (!pmResults.isEmpty()) { mv.addObject("msg", "success"); } mv.setViewName("pm/result_chart"); } catch (Exception e) { logger.error(e.toString(), e); } return mv; } /** * 获取任务分析结果 * * @param * @return */ @RequestMapping(value = "/result_charts") @ResponseBody public String result_charts(Page page) throws Exception { Map<String, String> map = new HashMap<String, String>(); PageData pd = new PageData(); pd = this.getPageData(); String taskId = pd.getString("taskId"); JSONArray jsonArray = null; try { pd = this.getPageData(); pd.put("taskId", taskId); page.setPd(pd); // List<PageData> pmResults = taskService.listPmResult(page); List<PageData> pmResultsForCharts = taskService .listPmResultForCharts(page); List<Series> seriesList = new ArrayList<Series>(); // 存放类别数据 List<String> categoryList = new ArrayList<String>(); // 存放所有 Option options = new Option(); if (!pmResultsForCharts.isEmpty()) { map.put("msg", "success"); System.out.println("pmResultsForCharts"+pmResultsForCharts); int i=0; for (PageData pmResult : pmResultsForCharts) { Series series = new Series(); System.out.println("pmResultsForCharts["+i+"]"+pmResult); ++i; String[] categorys = pmResult.getString("CATEGORY").trim().split(","); for(String category:categorys){ if(!categoryList.contains(category.trim())){ categoryList.add(category.trim()); } } series.setName(pmResult.getString("NEID").trim()); series.setType("bar"); List<Double> datas = new ArrayList<Double>(); String[] pmDatas = pmResult.getString("DATAS").split(","); for (String pmData : pmDatas) { Double data = Double.valueOf(pmData.trim()); datas.add(data); } series.setData(datas); System.out.println("series["+i+"]"+series); seriesList.add(series); System.out.println("seriesList["+i+"]"+seriesList); } options.setCategorys(categoryList); options.setSeries(seriesList); System.out.println("options:"+options); jsonArray = JSONArray.fromObject(options); } } catch (Exception e) { logger.error(e.toString(), e); } System.out.println("jsonObject:" + jsonArray); return jsonArray.toString(); } /** * 获取任务分析结果 * * @param * @return */ @RequestMapping(value = "/echarts") @ResponseBody public String echarts(Page page) throws Exception { return null; } /** * 获取单个节点上报信息 * * @param * @return */ @RequestMapping(value = "/pm_detail") public ModelAndView pmDetail(Page page, String step) throws Exception { ModelAndView mv = this.getModelAndView(); PageData pd = new PageData(); try { pd = this.getPageData(); String stepName = null; switch (step) { case "1": stepName = Const.RECEIVE_REPORT_MSG_TIME; break; case "2": stepName = Const.PROCESS_GET_FILE_TIME; break; case "3": stepName = Const.PROCESS_PARSE_TIME; break; case "5": stepName = Const.PROCESS_STORE_TIME; break; case "7": stepName = Const.PROCESS_FINISH_TIME; break; } pd.put("stepName", stepName); page.setPd(pd); List<PageData> poResultList = taskService.listPOResult(page); mv.addObject("poResultList", poResultList); mv.addObject("pd", pd); if (!poResultList.isEmpty()) { mv.addObject("msg", "success"); } mv.setViewName("pm/po_charts"); } catch (Exception e) { logger.error(e.toString(), e); } return mv; } /** * 删除任务 * * @param task_id * @param out */ @RequestMapping(value = "/delete") public void delete(@RequestParam String task_id, PrintWriter out) throws Exception { try { taskService.deleteByTaskId(task_id); out.write("success"); out.flush(); out.close(); } catch (Exception e) { logger.error(e.toString(), e); } } /* * 导出到excel * * @return */ @RequestMapping(value = "/excel") public ModelAndView exportExcel() { logBefore(logger, "导出Pictures到excel"); ModelAndView mv = new ModelAndView(); try { Map<String, Object> dataMap = new HashMap<String, Object>(); List<String> titles = new ArrayList<String>(); titles.add("编号"); // 1 titles.add("任务名称"); // 2 titles.add("创建人"); // 3 titles.add("创建时间"); // 4 titles.add("日志路径"); // 5 titles.add("类型"); // 6 titles.add("状态"); // 7 dataMap.put("titles", titles); Subject currentUser = SecurityUtils.getSubject(); Session session = currentUser.getSession(); String userName = (String) session .getAttribute(Const.SESSION_USERNAME); // 获取session中的用户名 List<Task> taskList = taskService.listTaskByUname(userName); List<PageData> varList = new ArrayList<PageData>(); for (int i = 0; i < taskList.size(); i++) { PageData vpd = new PageData(); vpd.put("var1", taskList.get(i).getNumber()); // 1 vpd.put("var2", taskList.get(i).getName()); // 2 vpd.put("var3", taskList.get(i).getOwner()); // 3 vpd.put("var4", taskList.get(i).getTime()); // 4 vpd.put("var5", taskList.get(i).getLogPath()); // 5 vpd.put("var6", taskList.get(i).getType()); // 6 vpd.put("var7", taskList.get(i).getStatus()); // 7 varList.add(vpd); } dataMap.put("varList", varList); ObjectExcelView erv = new ObjectExcelView(); mv = new ModelAndView(erv, dataMap); } catch (Exception e) { logger.error(e.toString(), e); } return mv; } }
标签:
原文地址:http://www.cnblogs.com/onetwo/p/5641485.html