package com.pqs9000.controller.area;

import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.commons.device.DeviceGeneral;
import com.njcn.service.configuration.DeviceService;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.PubUtils;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author hongawen
 * @Date: 2018/6/22 13:23
 */
@Controller
@RequestMapping("area")
public class DeviceStatusController {

    private static final Logger logger= LoggerFactory.getLogger(DeviceStatusController.class);

    @Autowired
    private DeviceService deviceService;

    @Autowired
    UserLogDetailService userLog;

    /**
     * 跳转区域统计的终端状态
     */
    @GetMapping("status")
    public String status(){
        return "business/area/deviceStatus";
    }

    @GetMapping("/steadystatTable")
    public String steadystatTable() {
        return "business/area/steadystattable";
    }

    /**
     *区域统计--》终端状态
     */
    @PostMapping("deviceStatus")
    @ResponseBody
    public HttpResult getDeviceStatus(String overview, String area,String scale,String manc,String loadtype,Integer monitorNature,
    		HttpServletRequest request,String startTime,String endTime) {
        HttpResult result=PubUtils.initResult(TokenManager.getToken().getLoginName(),request,LogTypeEnum.BUSSINESS.toString(),0);
        DeviceGeneral data ;
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "deviceStatus");
        if (!datePojo.getValidity()) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }
        try {
            data=deviceService.getDeviceSiftData(overview,area,scale,manc,loadtype,monitorNature,datePojo.getStartTime(),datePojo.getEndTime());
            if(null==data){
                result = PubUtils.assignmentResultLog(null, 500, "获取终端运行数据为空","查询稳态终端状态","成功",result);
            }else{
                result = PubUtils.assignmentResultLog(data, 200, "获取终端运行数据成功","查询稳态终端状态","成功",result);
            }
            userLog.getLog("查询终端状态", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        } catch (Exception e) {
            logger.error("获取终端运行数据异常,异常为："+e.toString());
            result = PubUtils.assignmentResultLog(null, 500, "获取终端运行数据异常","查询稳态终端状态","失败",result);
            userLog.getLog("查询终端状态", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        }
        return result;
    }
}
