package com.zhiwei.manage.servlet;

import java.util.ArrayList;
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.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;

/**
 * 数据库等参数
 * 
 * @author admin
 *
 */
@Controller
public class ServerController {

	@Autowired
	private ServerService ss;

	/**
	 * 分页获取所有库信息
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/getServer", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String getServer(@RequestBody Map<String, Object> map) {
		int pageNo = (int) map.get("pageNo");
		int pageSize = (int) map.get("pageSize");
		String dbName = (String) map.get("dbName");
		PageEty page = ss.findDb(pageNo, pageSize, dbName);
		JsonResult result = new JsonResult(true, 200, "获取成功", page);
		return JSON.toJSONString(result);
	}

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

	/**
	 * 删除库信息
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/deleteServer", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String deleteServer(@RequestBody Map<String, Object> map) {
		List<String> serverid = (List<String>) map.get("serverid");
		boolean b = ss.delete(serverid);
		JsonResult result = new JsonResult(true, 200, serverid + (b ? "删除成功" : "删除失败"));
		return JSON.toJSONString(result);
	}

	/**
	 * 插入库信息
	 * 
	 * @param serverBean
	 * @return
	 */
	@RequestMapping(value = "/insertServer", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String insertServer(@RequestBody ServerBean 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(@RequestBody Map<String, Object> map) {
		int pageNo = (int) map.get("pageNo");
		int pageSize = (int) map.get("pageSize");
		PageEty page = ss.findChannel(pageNo, pageSize);
		JsonResult result = new JsonResult(true, 200, "获取成功", page);
		return JSON.toJSONString(result);
	}

	/**
	 * 更改平台
	 *
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/changePt", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String changePt(@RequestBody Map<String, Object> map) {
		JsonResult result = new JsonResult(true, 200, "源改变成功");
		return JSON.toJSONString(result);
	}

	/**
	 * 插入渠道信息
	 * 
	 * @param channel
	 * @return
	 */
	@RequestMapping(value = "/insertChannel", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String insertChannel(@RequestBody Channel channel) {
		ss.insertChannels(channel);
		JsonResult result = new JsonResult(true, 200, "添加成功");
		return JSON.toJSONString(result);
	}

	/**
	 * 更新渠道信息
	 * 
	 * @param channel
	 * @return
	 */
	@RequestMapping(value = "/updateChannel", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String updateChannel(@RequestBody Channel channel) {
		boolean b = ss.updateChannel(channel);
		JsonResult result = new JsonResult(b, 200, b ? "修改成功" : "修改失败");
		return JSON.toJSONString(result);
	}

	/**
	 * 删除渠道信息
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "/deleteChannel", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String deleteChannel(@RequestBody Map<String, Object> map) {
		List<String> id = (List<String>) map.get("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();
		if (list == null || list.size() == 0) {
			Channel c = new Channel();
			c.setId("1");
			c.setPt("网媒");
			c.setValue("wangmei");
			list.add(c);
		}
		JsonResult result = new JsonResult(true, 200, "获取成功", list);
		System.out.println(result);
		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);
	}

	/**
	 * 获取参数
	 * 
	 * @return
	 */
	@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);
	}

}
