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.pojo.commons.runmanage.OnlineRateData;
import com.njcn.service.configuration.DeviceService;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.PubUtils;
import com.njcn.utils.XssFilterUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
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 org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

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

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

    @Autowired
    private DeviceService deviceService;


    @Autowired
    UserLogDetailService userLog;

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

    /**
     * 跳转运行管理的在线率页面
     */
    @GetMapping("onlineratetable")
    public ModelAndView onlineRateTable(String starttime,String endtime,String area,String interval) throws UnsupportedEncodingException {
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("business/manage/onlineRateTable");
        starttime = XssFilterUtil.dealString(starttime);
        endtime = XssFilterUtil.dealString(endtime);
        area = XssFilterUtil.dealString(area);
        interval = XssFilterUtil.dealString(interval);
        DatePojo datePojo=PubUtils.validateDate(starttime,endtime,"onlineratetable");
        if(!StringUtils.isEmpty(datePojo.getMsg())){
            modelAndView.addObject("startTime", "");
            modelAndView.addObject("endTime", "");
        }else{
            modelAndView.addObject("startTime", starttime);
            modelAndView.addObject("endTime", endtime);
        }
        modelAndView.addObject("area",area);
        modelAndView.addObject("interval",interval);
        return modelAndView;
    }

    /**
     * 运行管理中的终端在线率数据查询展示
     * @param startTime 起始时间
     * @param endTime 截止时间
     * @param area 查询的区域
     */
    @PostMapping("getOnlineRateData")
    @ResponseBody
    public HttpResult getOnlineRateData(String startTime,String endTime,String area,HttpServletRequest request){
        HttpResult result=PubUtils.initResult(TokenManager.getToken().getLoginName(),request,LogTypeEnum.BUSSINESS.toString(),0);
        DatePojo datePojo;
        datePojo=PubUtils.validateDate(startTime,endTime,"getOnlineRateData");
        if(!StringUtils.isEmpty(datePojo.getMsg())){
            result=PubUtils.assignmentResult(null,500,datePojo.getMsg());
            return result;
        }
        List<OnlineRateData> onlineRateDatas;
        try{
            onlineRateDatas=deviceService.getOnlineRate(datePojo.getStartTime(),datePojo.getEndTime(),area);
            if(null==onlineRateDatas){
                result = PubUtils.assignmentResultLog(null, 500, "获取终端在线率数据为空","查询在线率列表","成功",result);
            }else{
                result = PubUtils.assignmentResultLog(onlineRateDatas, 200, "获取终端在线率数据成功","查询在线率列表","成功",result);
            }
            userLog.getLog("查询终端在线率", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        }catch(Exception e){
            logger.error("获取终端运行数据异常,异常为："+e.toString());
            userLog.getLog("查询终端在线率", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            result = PubUtils.assignmentResultLog(null, 500, "获取终端在线率数据异常","查询在线率列表","失败",result);
        }
        return result;
    }

    /**
     * 区域统计--》在线率
     * @param startTime 起始时间
     * @param endTime 截止时间
     */
    @PostMapping("onlineRate")
    @ResponseBody
    public HttpResult getOnlineRate(String startTime,String endTime,String overview,String area,HttpServletRequest request){
        HttpResult result=PubUtils.initResult(TokenManager.getToken().getLoginName(),request,LogTypeEnum.BUSSINESS.toString(),0);
        DatePojo datePojo;
        datePojo=PubUtils.validateDate(startTime,endTime,"getOnlineRate");
        if(!StringUtils.isEmpty(datePojo.getMsg())){
            result=PubUtils.assignmentResult(null,500,datePojo.getMsg());
            return result;
        }
        DeviceGeneral data ;
        try {
            data=deviceService.getDeviceOnlineRate(datePojo.getStartTime(),datePojo.getEndTime(),overview,area);
            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());
            userLog.getLog("查询终端运行数据", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            result = PubUtils.assignmentResultLog(null, 500, "获取终端在线率数据异常","查询终端在线率","失败",result);
        }
        return result;
    }


}
