package com.pqs9900.controller.DataInfo;

import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.DeviceType;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.utils.PubUtils;
import com.njcn.utils.UserUtil;
import com.pqs9900.pojo.DataInfo.DevState;
import com.pqs9900.pojo.DataInfo.DevStateDetail;
import com.pqs9900.pojo.DataInfo.FlowDetail;
import com.pqs9900.service.DataInfo.DevStateService;
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.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.util.ArrayList;
import java.util.List;

/**
 * @Author denghuajun
 * @Description //TODO 终端状态
 * @Date 2019/3/15 15:40
 **/
@Controller
@RequestMapping("devstate")
public class DevStateController {
    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(DevStateController.class);

    @Autowired
    private DevStateService devStateService;

    @Autowired
    UserUtil userUtil;

    @RequestMapping(value = "getDevStateDetail", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getDevStateDetail(HttpServletRequest request, HttpSession session, String startTime, String
            endTime) {
        HttpResult result;
        List<DevStateDetail> devStateDetails = new ArrayList<>();
        try {
            DeviceType deviceType = userUtil.getOnLineDeviceType();
            devStateDetails = devStateService.getDevStateDetail(deviceType.getDataType());
            result = PubUtils.assignmentResult(devStateDetails, 200, "获取终端状态成功");
        } catch (Exception e) {
            logger.error("查询所有终端状态异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "获取中终端状态错误");
        }

        return result;
    }

    @RequestMapping(value = "getDevStateInfo", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getDevStateInfo(HttpServletRequest request, HttpSession session, Long devIndex, String startTime, String
            endTime) {
        HttpResult result;
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "getDevStateInfo");
        List<DevStateDetail> devStateDetails = new ArrayList<>();
        try {
            DeviceType deviceType = userUtil.getOnLineDeviceType();
            devStateDetails = devStateService.getDevStateInfo(devIndex, datePojo.getStartTime(), datePojo.getEndTime());
            result = PubUtils.assignmentResult(devStateDetails, 200, "获取终端状态统计成功");
        } catch (Exception e) {
            logger.error("查询所有终端状态统计异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "获取终端状态统计错误");
        }

        return result;
    }

    @RequestMapping(value = "getDeviceDetail", method = RequestMethod.POST)
    @ResponseBody
    public HttpResult getDeviceDetail(HttpServletRequest request, HttpSession session, Long devIndex) {
        HttpResult result;
        DevStateDetail devStateDetails;
        try {
            devStateDetails = devStateService.getDeviceDetail(devIndex);
            result = PubUtils.assignmentResult(devStateDetails, 200, "获取终端详情成功");
        } catch (Exception e) {
            logger.error("查询所有终端详情异常，异常是" + e.toString());
            result = PubUtils.assignmentResult(null, 500, "获取终端详情错误");
        }

        return result;
    }
}
