package com.zhiwei.manage.servlet;

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

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.service.FieldBeanService;

@Controller
public class FieldController {

	@Autowired
	private FieldBeanService fs;

	/**
	 * 获取所有的信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getFields", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getField(@RequestBody Map<String, Object> map) {
		int pageNo = (int) map.get("pageNo");
		int pageSize = (int) map.get("pageSize");
		String pt = (String) map.get("pt");
		PageEty page = fs.findAllFields(pageNo, pageSize, pt);
		JsonResult result = new JsonResult(true, 200, "获取成功", page);
		return JSON.toJSONString(result);
	}

	@RequestMapping(value = "/updateFields", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String updateField(@RequestBody FieldBean fieldBean) {
		boolean b = fs.update(fieldBean);
		JsonResult result = new JsonResult(true, 200, b ? "修改成功" : "修改失败");
		return JSON.toJSONString(result);
	}

	@RequestMapping(value = "/deleteFields", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String deleteFields(@RequestBody Map<String, Object> map) {
		List<String> id=(List<String>) map.get("id");
		System.out.println(id);
		boolean b = fs.delete(id);
		JsonResult result = new JsonResult(b, 200, b ? "删除成功" : "删除失败");
		return JSON.toJSONString(result);
	}

	@RequestMapping(value = "/insertFields", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String insertField(@RequestBody FieldBean fieldbean) {
		fs.insert(fieldbean);
		JsonResult result = new JsonResult(true, 200, "添加成功");
		return JSON.toJSONString(result);
	}

	@RequestMapping(value = "/getAllFields", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getAllFields(@RequestBody Map<String, Object> map) {
		String pt=(String) map.get("pt");
		FieldBean field = fs.getFieldsByPt(pt);
		String fields[] = null;
		if (field != null) {
			fields = field.getField().split(",");
		}
		JsonResult result = new JsonResult(true, 200, "获取成功", fields);
		return JSON.toJSONString(result);
	}
}
