package com.zhiwei.manage.servlet;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
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 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(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(Template template, String username, HttpServletRequest request) {
		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(@RequestParam(value = "templateId[]") String[] templateId) {
		boolean isDelete = false;
		String results="";
		for (String l : templateId) {
			System.out.println(l);
			 isDelete = ts.delete(l);
			 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(@RequestParam(value = "pageNo", defaultValue = "1") int pageNo,
			@RequestParam(value = "pageSize", defaultValue = "10") int pageSize, String tempName,String 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(String 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(String 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(@RequestParam(value = "pt", defaultValue = "网媒")String pt){
		FieldBean fieldBean=fb.getFieldsByPt(pt);
		JsonResult result=new JsonResult(true, 200, "获取成功",fieldBean.getField());
		return JSON.toJSONString(result);
	}
	

}
