package main.java.com.zte.controller.system;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.codehaus.jackson.map.ObjectMapper;
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.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.Echarts;
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.ObjectExcelView;
import main.java.com.zte.util.PageData;
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 {
// pd = taskService.getTaskTypeList();
List<String> taskTypeList = new ArrayList<String>();
taskTypeList.add("在线任务");
taskTypeList.add("离线任务");
mv.addObject("taskTypeList", taskTypeList);
mv.setViewName("task/task_add");
} catch (Exception e) {
logger.error(e.toString(), e);
}
return mv;
}
/**
* 保存任务信息
*
* @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> resultList = taskService.listResult(page);
mv.addObject("resultList", resultList);
mv.addObject("pd", pd);
if (!resultList.isEmpty()) {
mv.addObject("msg", "success");
}
mv.setViewName("pm/result_chart");
} catch (Exception e) {
logger.error(e.toString(), e);
}
System.out.println("ID_01:" + taskId);
return mv;
}
/**
* 获取任务分析结果
*
* @param
* @return
*/
@RequestMapping(value = "/result_charts")
@ResponseBody
public Object 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");
System.out.println("ID_02:" + taskId);
try {
pd = this.getPageData();
pd.put("taskId", taskId);
page.setPd(pd);
List<PageData> resultList = taskService.listResult(page);
JSONArray jsonArray = JSONArray.fromObject(resultList);
if (!resultList.isEmpty()) {
map.put("msg", "success");
map.put("resultList", jsonArray.toString());
}
} catch (Exception e) {
logger.error(e.toString(), e);
}
System.out.println("taskId=" + taskId);
System.out.println("result:"
+ AppUtil.returnObject(new PageData(), map));
JSONObject jsonObject = JSONObject.fromObject(AppUtil.returnObject(
new PageData(), map));
System.out.println("jsonObject:" + jsonObject);
return AppUtil.returnObject(new PageData(), map);
}
/**
* 获取任务分析结果
*
* @param
* @return
*/
@RequestMapping(value = "/test_charts")
@ResponseBody
public String test_charts(Page page) throws Exception {
JSONArray members = new JSONArray();
for (int i = 1; i < 5; i++) {
// 构建JSON 对象
JSONObject member = new JSONObject();
// 构建series所需参数
member.put("name", "张飞" + i); // 对应series.name
JSONArray list = new JSONArray();// 对应series.data
for (int k = 1; k < 5; k++) {
list.add(k * 100);
}
member.put("year", (2012 + i));// 对应Y轴显示
member.put("list", list);
member.put("color", "#FF0022");// 如需要可以设置柱状图颜色
members.add(member);
}
return members.toString();
}
/**
* 获取单个节点上报信息
*
* @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).getDir()); // 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;
}
}