package com.zhiwei.manage.servlet;

import java.util.ArrayList;
import java.util.List;

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.Channel;
import com.zhiwei.manage.bean.JsonResult;
import com.zhiwei.manage.bean.PageEty;
import com.zhiwei.manage.bean.ServerBean;
import com.zhiwei.manage.service.ServerService;

@Controller
public class ServerController {

	@Autowired
	private ServerService ss;

	@RequestMapping(value = "/getServer", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getServer(@RequestParam(value = "pageNo", defaultValue = "0") int pageNo,
			@RequestParam(value = "pageSize", defaultValue = "0") int pageSize, String dbName) {
		PageEty page = ss.findDb(pageNo, pageSize, dbName);
		JsonResult result = new JsonResult(true, 200, "获取成功", page);
		return JSON.toJSONString(result);
	}

	@RequestMapping(value = "/updateServer", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String updateServer(ServerBean serverBean) {
		boolean b = ss.update(serverBean);
		JsonResult result = new JsonResult(b, 200, b ? "获取成功" : "获取失败");
		return JSON.toJSONString(result);
	}

	@RequestMapping(value = "/deleteServer", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String deleteServer(@RequestParam(value = "serverid[]")List<String> serverid) {
		boolean b = ss.delete(serverid);
		JsonResult result = new JsonResult(true, 200, serverid+(b ? "删除成功" : "删除失败"));
		return JSON.toJSONString(result);
	}
	
	
	@RequestMapping(value = "/insertServer", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String insertServer(ServerBean serverBean) {
		System.out.println(serverBean);
		ss.insert(serverBean);
		JsonResult result = new JsonResult(true, 200, "添加成功");
		return JSON.toJSONString(result);
	}

	/**
	 * 获取所有的渠道信息
	 * @return
	 */
	@RequestMapping(value = "/getChannel", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getChannel(@RequestParam(value = "pageNo", defaultValue = "1")int pageNo,
			@RequestParam(value = "pageSize", defaultValue = "10")int pageSize){
		PageEty page=ss.findChannel(pageNo, pageSize);
		JsonResult result=new JsonResult(true, 200, "获取成功",page);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/changePt", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String changePt(String pt) {
		ss.findPt(pt);
		JsonResult result = new JsonResult(true, 200, "源改变成功");
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/insertChannel", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String insertChannel(Channel channel){
		ss.insertChannels(channel);
		JsonResult result=new JsonResult(true, 200, "添加成功");
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/updateChannel", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String updateChannel(Channel channel){
		boolean b=ss.updateChannel(channel);
		JsonResult result=new JsonResult(b, 200, b?"修改成功":"修改失败");
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value = "/deleteChannel", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String deleteChannel(@RequestParam(value = "id[]")List<String> id) {
		boolean b=ss.deleteChannel(id);
		JsonResult result=new JsonResult(b, 200, id+(b?"删除成功":"删除失败"));
		return JSON.toJSONString(result);
	}
	
	/**
	 * 用于展示渠道
	 * @return
	 */
	@RequestMapping(value="/getShowChannel",produces="application/json;charset=utf-8")
	@ResponseBody
	public String findAllChannel(){
		List<Channel> list=ss.findDisChannel();
		JsonResult result=new JsonResult(true, 200, "获取成功",list);
		return JSON.toJSONString(result);
	}
	
	/**
	 * 用于给用户选择渠道
	 * @return
	 */
	@RequestMapping(value="/getDisChannel",produces="application/json;charset=utf-8")
	@ResponseBody
	public String findDisChannel(){
		List<String> list=ss.findDisChan();
		JsonResult result=new JsonResult(true, 200, "获取成功",list);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping(value="/getParam",produces="application/json;charset=utf-8")
	@ResponseBody
	public String getParam(){
		List<String> list=new ArrayList<>();
		list.add("msg");
		list.add("temp");
		list.add("data");
		JsonResult result=new JsonResult(true, 200, "获取成功",list);
		return JSON.toJSONString(result);
	}
	
	
	
	
}
