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.sso.pojo.user.User;
import com.njcn.utils.PubUtils;
import com.njcn.utils.XssFilterUtil;
import com.pqs9900.pojo.DataInfo.MiddRun;
import com.pqs9900.pojo.DataInfo.MiddRunState;
import com.pqs9900.pojo.DataInfo.MiddRunTree;
import com.pqs9900.service.DataInfo.MiddRunService;
import com.pqs9900.service.DataInfo.MiddRunStateService;
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.Date;
import java.util.List;

/**
 * @Author denghuajun
 * @Description //TODO 中间件表
 * @Date 2019/3/15 14:24
 **/
@Controller
@RequestMapping("middRun")
public class MiddRunController {
    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(MiddRunController.class);

    @Autowired
    MiddRunService runService;
    @Autowired
    MiddRunStateService runStateService;
    @Autowired
    UserLogDetailService logDetailService;
    @Autowired
    UserMapper u;

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

        return result;
    }

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

        return result;
    }

    @RequestMapping(value = "getMiddRunDetail", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getMiddRunDetail(HttpServletRequest request, HttpSession session, String middIndex) {
        HttpResult result;
        List<MiddRun> middRuns=new ArrayList<>();
        try {
            middRuns = runService.getMiddRun(middIndex);
            result = PubUtils.assignmentResult(middRuns, 200, "获取中间件列表成功");
        } catch (Exception e) {
            logger.error("查询所有中间件信息异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "获取中间件列表错误");
        }

        return result;
    }

    @RequestMapping(value = "getMiddRunDetails", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getMiddRunDetails(HttpServletRequest request, HttpSession session) {
        HttpResult result;
        List<MiddRun> middRuns=new ArrayList<>();
        try {
            middRuns = runService.getMiddRuns();
            result = PubUtils.assignmentResult(middRuns, 200, "获取中间件列表成功");
        } catch (Exception e) {
            logger.error("查询所有中间件信息异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "获取中间件列表错误");
        }

        return result;
    }

    /**
     * 新增
     */
    @RequestMapping(value = "saveMiddRun", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult saveMiddRun(String middRunName,String ip,int port) {
        HttpResult result;
        middRunName = XssFilterUtil.dealString(middRunName);
        ip = XssFilterUtil.dealString(ip);
        try {
            String middRunMessge = runService.saveMiddRun(middRunName,ip,port);
            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 = "updateMiddRun", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult updateMiddRun(String middIndex,String middRunName,String ip,int port) {
        HttpResult result;
        middRunName = XssFilterUtil.dealString(middRunName);
        ip = XssFilterUtil.dealString(ip);
        try {
            String middRunMessge = runService.updateMiddRun(middIndex,middRunName,ip,port);
            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 = "deleteMiddRun", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult deleteMiddRun(String middIndex) {
        HttpResult result;
        middIndex = XssFilterUtil.dealString(middIndex);
        try {
            String middRunMessge = runService.deleteMiddRun(middIndex);
            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 = "selectMiddRun")
    public MiddRun selectMiddRun(String middIndex) {
        return runService.selectMiddRun(middIndex);
    }
}
