package com.zhiwei.manage.servlet;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zhiwei.manage.bean.FieldBean;
import com.zhiwei.manage.bean.JsonResult;
import com.zhiwei.manage.bean.PageEty;
import com.zhiwei.manage.bean.Template;
import com.zhiwei.manage.handle.MainThread;
import com.zhiwei.manage.service.FieldBeanService;
import com.zhiwei.manage.service.TemplateService;

@Controller
public class TemplateController {

	@Autowired
	private TemplateService ts;
	@Autowired
	private FieldBeanService fb;

	@RequestMapping(value = "/saveTmp", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String saveTmp(@RequestBody Template template) {
		System.out.println(template);
		if (template != null) {
			if (ts.insert(template)) {
				JsonResult result = new JsonResult(true, 200, "存储成功");
				return JSON.toJSONString(result);
			}
		}
		JsonResult result = new JsonResult(false, 200, "保存失败");
		return JSON.toJSONString(result);
	}

	@RequestMapping(value = "/updateTmp", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String updateTmp(@RequestBody Map<String, Object> map, HttpServletRequest request) {
		Template template = (Template) map.get("template");
		if (template != null) {
			if (ts.update(template)) {
				JsonResult result = new JsonResult(true, 200, "修改成功");
				return JSON.toJSONString(result);
			}
		}
		JsonResult result = new JsonResult(true, 200, "修改失败");
		return JSON.toJSONString(result);
	}

	@RequestMapping(value = "/deleteTmp", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String deleteTmp(@RequestBody Map<String, Object> map) {
		List<String> templateId = (List<String>) map.get("templateId");
		String pt = (String) map.get("pt");
		boolean isDelete = false;
		String results = "";
		for (String l : templateId) {
			System.out.println(l);
			isDelete = ts.delete(l, pt);
			MainThread.delete(l);
			results += l + "\n";
		}
		JsonResult result = new JsonResult(true, 200, results + "删除" + (isDelete == false ? "成功" : "失败"));
		return JSON.toJSONString(result);
	}

	@RequestMapping(value = "/findAllTmp", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String findAll(@RequestBody Map<String, Object> map) {
		int pageNo = (int) map.get("pageNo");
		int pageSize = (int) map.get("pageSize");
		String tempName = (String) map.get("tempName");
		String pt = (String) map.get("pt");
		if (tempName == null || tempName.equals("")) {
			PageEty page = ts.findAll(pageNo, pageSize, pt);
			JsonResult result = new JsonResult(true, 200, "返回成功", page);
			return JSON.toJSONString(result);
		} else {
			PageEty page = ts.findByTmpName(tempName, pageNo, pageSize, pt);
			if (page != null) {
				JsonResult result = new JsonResult(true, 200, "返回成功", page);
				return JSON.toJSONString(result);
			} else {
				JsonResult result = new JsonResult(true, 200, "没有改模板", new PageEty());
				return JSON.toJSONString(result);
			}
		}
	}

	@RequestMapping(value = "/startTmp", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String startTmp(@RequestBody Map<String, Object> map) {
		String templateId = (String) map.get("templateId");
		boolean b = MainThread.pushMap(templateId);
		if (b) {
			JsonResult result = new JsonResult(true, 200, "启动成功");
			return JSON.toJSONString(result);
		} else {
			JsonResult result = new JsonResult(false, 200, "启动失败,已经存在");
			return JSON.toJSONString(result);
		}
	}

	@RequestMapping(value = "/stopTmp", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String stopTmp(@RequestBody Map<String, Object> map) {
		String templateId = (String) map.get("templateId");
		boolean b = MainThread.removeMap(templateId);
		if (b) {
			JsonResult result = new JsonResult(true, 200, "关闭成功");
			return JSON.toJSONString(result);
		} else {
			JsonResult result = new JsonResult(false, 200, "停止失败");
			return JSON.toJSONString(result);
		}
	}

	@RequestMapping(value = "/getField", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getField(@RequestBody Map<String, Object> map) {
		String pt = (String) map.get("pt");
		FieldBean fieldBean = fb.getFieldsByPt(pt);
		JsonResult result = new JsonResult(true, 200, "获取成功", fieldBean.getField());
		return JSON.toJSONString(result);
	}

}
