package com.zhiwei.manage.servlet;

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

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
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.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;

/**
 * 模板控制
 * 
 * @author admin
 *
 */
@Controller
public class TemplateController {

	private static final Log log = org.apache.commons.logging.LogFactory.getLog(TemplateController.class);
	@Autowired
	private TemplateService ts;
	@Autowired
	private FieldBeanService fb;

	/**
	 * 一键启动
	 * 
	 * @return
	 */
	@RequestMapping(value = "/startAll", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String startAll() {
		List<Template> tpList = ts.findAll();
		for (Template t : tpList) {
			MainThread.pushMap(t.getTemplateId());
		}
		JsonResult result = new JsonResult(true, 200, "启动成功");
		return JSON.toJSONString(result);
	}

	/**
	 * 一键停止
	 * 
	 * @return
	 */
	@RequestMapping(value = "/stopAll", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String stopAll() {
		MainThread.removeAll();
		JsonResult result = new JsonResult(true, 200, "停止成功");
		return JSON.toJSONString(result);
	}

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

	/**
	 * 更新模板
	 * 
	 * @param template
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updateTmp", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String updateTmp(@RequestBody Template template, HttpServletRequest request) {
		JsonResult result = null;
		if (MainThread.mainMap.get(template.getTemplateId()) != null) {
			result = new JsonResult(true, 200, "修改失败");
		} else {
			if (ts.update(template)) {
				result = new JsonResult(true, 200, "修改成功");
			} else {
				result = new JsonResult(true, 200, "修改失败");
			}
		}
		return JSON.toJSONString(result);
	}

	/**
	 * 删除模板
	 * 
	 * @param map
	 * @return
	 */
	@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) {
			isDelete = ts.delete(l, pt);
			MainThread.removeMap(l);
			results += l + "\n";
		}
		JsonResult result = new JsonResult(true, 200, results + "删除" + (isDelete == false ? "成功" : "失败"));
		return JSON.toJSONString(result);
	}

	/**
	 * 获取所有的模板信息
	 * 
	 * @param map
	 * @return
	 */
	@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);
			}
		}
	}

	/**
	 * 开启模板根据id
	 * 
	 * @param map
	 * @return
	 */
	@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);
		}
	}

	/**
	 * 停止模板
	 * 
	 * @param map
	 * @return
	 */
	@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);
		}
	}

	/**
	 * 获取字段信息
	 * 
	 * @param map
	 * @return
	 */
	@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);
	}

}
