package com.pqs9900.controller.DataInfo;

import com.njcn.mapper.user.UserMapper;
import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.utils.PubUtils;
import com.njcn.utils.UserUtil;
import com.njcn.utils.XssFilterUtil;
import com.pqs9900.controller.business.DeviceController;
import com.pqs9900.pojo.DataInfo.MiddRun;
import com.pqs9900.pojo.DataInfo.Server;
import com.pqs9900.pojo.DataInfo.ServerState;
import com.pqs9900.pojo.DataInfo.ServerTree;
import com.pqs9900.service.DataInfo.ServerService;
import com.pqs9900.service.DataInfo.ServerStateService;
import org.apache.kafka.common.protocol.types.Field;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author denghuajun
 * @Description //TODO 服务器表controller
 * @Date 2019/3/15 14:14
 **/
@Controller
@RequestMapping("serverstate")
public class ServerController {
    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(ServerController.class);

    @Autowired
    UserLogDetailService logDetailService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ServerService serverService;
    @Autowired
    ServerStateService serverStateService;
    @Autowired
    UserUtil userUtil;
    @Autowired
    UserMapper u;

    /**
     * 查询服务器信息
     */
    @RequestMapping(value = "getShow", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getShow(HttpServletRequest request, HttpSession session, String sevIndex, String startTime,
                              String endTime) {
        HttpResult result;
        DatePojo datePojo;
        List<ServerState> serverStates = new ArrayList<>();
        datePojo = PubUtils.validateDate(startTime, endTime, "getShow");
        if (!datePojo.getValidity()) {
            result = PubUtils.assignmentResult(null, 500, "解析前台传递的时间有误");
            return result;
        }
        try {
            serverStates = serverStateService.getServerState(sevIndex, datePojo.getStartTime(), datePojo.getEndTime());
            result = PubUtils.assignmentResult(serverStates, 200, "获取服务器信息成功");
        } catch (Exception e) {
            logger.error("查询所有服务器信息异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "获取服务器信息错误");
        }

        return result;
    }

    @RequestMapping(value = "getServerDetail", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getServerDetail(HttpServletRequest request, HttpSession session, String sevIndex) {
        HttpResult result;
        List<Server> server=new ArrayList<>();
        try {
            server = serverService.getServer(sevIndex);
            result = PubUtils.assignmentResult(server, 200, "获取服务器信息成功");
        } catch (Exception e) {
            logger.error("查询所有服务器信息异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "获取服务器信息错误");
        }

        return result;
    }

    @RequestMapping(value = "getTree", method = RequestMethod.POST)
    @ResponseBody
    public List<ServerTree> getTree(HttpServletRequest request, HttpSession session) {
        List<ServerTree> result = new ArrayList();
        try {
            result = serverService.getTreeType();
        } catch (Exception e) {
            logger.error("查询所有服务器信息异常，异常是" + e.toString());
        }

        return result;
    }

    @RequestMapping(value = "getServerDetails", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getServerDetails(HttpServletRequest request, HttpSession session) {
        HttpResult result;
        List<Server> server=new ArrayList<>();
        try {
            server = serverService.getServerDetails();
            result = PubUtils.assignmentResult(server, 200, "获取服务器信息成功");
        } catch (Exception e) {
            logger.error("查询所有服务器信息异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "获取服务器信息错误");
        }

        return result;
    }

    /**
     * 新增
     */
    @RequestMapping(value = "saveServer", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult saveServer(String serverName, String type, String version,String hardware,
                                 String userName,String password,String ip  ) {
        HttpResult result;
        serverName = XssFilterUtil.dealString(serverName);
        type = XssFilterUtil.dealString(type);
        version = XssFilterUtil.dealString(version);
        hardware = XssFilterUtil.dealString(hardware);
        userName = XssFilterUtil.dealString(userName);
        password = XssFilterUtil.dealString(password);
        ip = XssFilterUtil.dealString(ip);
        try {
            String middRunMessge = serverService.saveServer(serverName,type,version,hardware,userName,password,ip);
            if(middRunMessge.equals("新增成功")){
                result = PubUtils.assignmentResult("success", 200, middRunMessge);
            }else {
                result = PubUtils.assignmentResult("fall", 500, middRunMessge);
            }
        } catch (Exception e) {
            logger.error("新增中间件异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "新增中间件错误");
        }

        return result;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "updateServer", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult updateServer(String sevIndex,String serverName, String type, String version,String hardware,
                                 String userName,String password,String ip  ) {
        HttpResult result;
        serverName = XssFilterUtil.dealString(serverName);
        type = XssFilterUtil.dealString(type);
        version = XssFilterUtil.dealString(version);
        hardware = XssFilterUtil.dealString(hardware);
        userName = XssFilterUtil.dealString(userName);
        password = XssFilterUtil.dealString(password);
        ip = XssFilterUtil.dealString(ip);
        try {
            String middRunMessge = serverService.updateServer(sevIndex,serverName,type,version,hardware,userName,password,ip);
            if(middRunMessge.equals("修改成功")){
                result = PubUtils.assignmentResult("success", 200, middRunMessge);
            }else {
                result = PubUtils.assignmentResult("fall", 500, middRunMessge);
            }
        } catch (Exception e) {
            logger.error("新增中间件异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "新增中间件错误");
        }

        return result;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "deleteServer", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult deleteServer(String sevIndex) {
        HttpResult result;
        sevIndex = XssFilterUtil.dealString(sevIndex);
        try {
            String middRunMessge = serverService.deleteServer(sevIndex);
            if(middRunMessge.equals("删除成功")){
                result = PubUtils.assignmentResult("success", 200, middRunMessge);
            }else {
                result = PubUtils.assignmentResult("fall", 500, middRunMessge);
            }
        } catch (Exception e) {
            logger.error("删除中间件异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "删除中间件错误");
        }

        return result;
    }

    /**
     * 根据id查询
     */
    @ResponseBody
    @PostMapping(value = "selectServer")
    public Server selectServer(String sevIndex) {
        return serverService.selectServer(sevIndex);
    }
}
