package com.zhongdao.jlr.business.action;
import com.util.RequestUtil;
import com.util.Vo;
import com.zhongdao.jlr.business.service.CasesListService;
import com.zhongdao.jlr.business.vo.LoginUserVo;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * 道路救援控制器
 */
@Controller
@RequestMapping("/casesInfo")
public class CasesAction {

	private final static String timeFormat = "yyyy/MM/dd";

	@Resource(name = "casesListServiceImpl")
	CasesListService casesListService;

	/*
	*  跳转救援工单页面
	* */
	@RequestMapping(value = "/case.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
			RequestMethod.POST})
	public String JumpTocaseInfo(HttpServletRequest request, HttpServletResponse response) {
		//获取参数
		String state = request.getParameter("state");
		//共享数据
		request.setAttribute("state", state);
		if("0".equals(state)){
			return "/page/caseInfo";
		}else {
			return "/page/sue";
		}
	}

	/*
	*  跳转进店工单页面
	* */
	@RequestMapping(value = "/caseEnter.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
			RequestMethod.POST})
	public String caseEnter(HttpServletRequest request, HttpServletResponse response) {
		//获取登录信息
		LoginUserVo loginInfo=(LoginUserVo)request.getSession().getAttribute("logininfo");
		//获取参数
		String state = request.getParameter("state");
		//共享数据
		request.setAttribute("state", state);
		return "/page/caseInfoEnter";
	}

	/*
	* 获取道路救援案件信息
	* */
	@RequestMapping(value = "/caseFirstQuery.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
			RequestMethod.POST})
	public void caseInfoList(HttpServletRequest request, HttpServletResponse response) {
		//获取参数
		String caseCode = request.getParameter("caseCode");// 案件编号
		String frame_code = request.getParameter("frame_code");// VIN码
		String rescueState = request.getParameter("rescueState");// 服务状态
		String createTime = request.getParameter("createTime");//开始时间
		String finishTime = request.getParameter("finishTime");//结束时间
		String companyName = request.getParameter("companyName");//服务商代码
		String state = request.getParameter("state");//服务商代码
		String plateNumber = request.getParameter("plateNumber");//服务商代码
		//获取救援信息
		Vo vo = casesListService.casesListService(state,request,caseCode,
				frame_code, rescueState,createTime, finishTime, companyName,plateNumber);
		//将救援信息响应到客户端
		RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(vo));
	}

	/*
	* 获取进店救援案件信息
	* */
	@RequestMapping(value = "/caseEnterQuery.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
			RequestMethod.POST})
	public void caseEnterQuery(HttpServletRequest request, HttpServletResponse response) {
		//获取参数
		String caseCode = request.getParameter("caseCode");// 案件编号
		String frame_code = request.getParameter("frame_code");// VIN码
		String createTime = request.getParameter("createTime");//开始时间
		String finishTime = request.getParameter("finishTime");//结束时间
		String state = request.getParameter("state");//结束时间
		//获取进店救援案件信息
		Vo vo = casesListService.caseEnterQuery(state,request,caseCode,
				frame_code,createTime, finishTime);
		//将救援信息响应到客户端
		RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(vo));
	}

	/*
	* 救援案件详情信息
	* */
	@RequestMapping(value = "/caseEnterInfo.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
			RequestMethod.POST})
	public String caseEnterInfo(HttpServletRequest request, HttpServletResponse response) {
		//获取参数
		String caseCode = request.getParameter("code");// 案件编号
		String tid = request.getParameter("tid");//任务编号
		String state = request.getParameter("state");//任务编号
		//获取进店救援案件详情信息
		Vo vo = casesListService.caseEnterInfo(request,caseCode,tid);
		//共享数据
		request.setAttribute("info",vo);
		request.setAttribute("state",state);
		//跳转页面
		return "/page/caseDetail";
	}

	/*
	* 故障零件三级检索
	* */
	@RequestMapping(value = "/getTroubleCode.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
			RequestMethod.POST})
	public void getTroubleCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject jsonObject = casesListService.getTroubleCode(request);
		RequestUtil.responseResult4Json(response, jsonObject.toString());
	}

	/*
	* 案件详细信息保存
	* */
	@RequestMapping(value = "/caseDetailSave.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
			RequestMethod.POST})
	public void caseDetailSave(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject jsonObject = casesListService.caseDetailSave(request);
		RequestUtil.responseResult4Json(response, jsonObject.toString());
	}

	/*
	*导出数据EXCEl格式
	* */
	@RequestMapping(value = "/exportData.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
			RequestMethod.POST})
	public void exportData(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject jsonObject = casesListService.exportData(request);
		RequestUtil.responseResult4Json(response, jsonObject.toString());
	}

	/*
	*导出数据CSV
	* */
	@RequestMapping(value = "/exportDataCSV.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
			RequestMethod.POST})
	public void exportDataCSV(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JSONObject jsonObject = casesListService.exportCSVData(request, response);
		RequestUtil.responseResult4Json(response, jsonObject.toString());
	}

	/*
	* 救援完成确认
	* */
	@RequestMapping(value = "/completionOfRescue.do", produces = "text/html;chrset=UTF-8", method = {RequestMethod.GET,
			RequestMethod.POST})
	public void completionOfRescue(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//获取参数
		String code = request.getParameter("code");
		//获取救援详情信息
		JSON jsonObject = casesListService.completionOfRescue(code);
		//将json数据响应到客户端
		RequestUtil.responseResult4Json(response, RequestUtil.ObjecttoJsonString(jsonObject));
	}
}
