package com.pqs9000.controller.business;


import com.njcn.enums.LogTypeEnum;
import com.njcn.pojo.commons.DatePojo;
import com.njcn.pojo.commons.HttpResult;
import com.njcn.pojo.commons.SimpleValue;
import com.njcn.pojo.commons.SteadyTypeData;
import com.njcn.pojo.commons.device.DeviceGeneral;
import com.njcn.pojo.data.AssesData;
import com.njcn.service.configuration.DeviceService;
import com.njcn.service.configuration.LineService;
import com.njcn.service.log.UserLogDetailService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.PubUtils;
import com.pqs9000.pojo.common.PowerQualityData;
import com.pqs9000.service.data.AssesService;
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.util.List;

/**
 * @author hongawen
 * @date 2018/4/3 11:07
 */
@Controller
@RequestMapping("overview")
public class OverviewController {

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

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private LineService lineService;

    @Autowired
    private AssesService assesService;

    @Autowired
    UserLogDetailService userLog;
    @GetMapping("/assesTable")
    public ModelAndView integrityTable(String starttime, String endtime, String area, String interval) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("business/area/assesTable");
        DatePojo datePojo=PubUtils.validateDate(starttime,endtime,"assesTable");
        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   截止时间
     */
    @PostMapping("/assesTableData")
    @ResponseBody
    public HttpResult getAssesTableData(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, "assesTableData");
        if (!StringUtils.isEmpty(datePojo.getMsg())) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }
        List<AssesData> data;
        try {
            data = assesService.getAssesTableData(datePojo.getStartTime(), datePojo.getEndTime(), area);
            result = PubUtils.assignmentResultLog(data, 200, "区域获取电能质量评估列表成功", "查询区域获取电能质量评估列表", "成功", result);
            userLog.getLog("查询电能质量评估列表", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        } catch (Exception e) {
            userLog.getLog("查询电能质量评估列表", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            result = PubUtils.assignmentResultLog(null, 200, "区域获取电能质量评估列表失败", "查询区域获取电能质量评估列表", "失败", result);
        }
        return result;
    }




    /**
     * 概览--》缩略图电能质量--》电压偏差
     *
     * @param startTime 起始时间
     * @param endTime   截止时间
     */
    @PostMapping("powerQualityVol")
    @ResponseBody
    public HttpResult getPowerQualityVol(String startTime, String endTime, String overview, HttpServletRequest request) {
        HttpResult result = PubUtils.initResult(TokenManager.getToken().getLoginName(), request, LogTypeEnum.BUSSINESS.toString(), 0);
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "getPowerQualityVol");
        if (!StringUtils.isEmpty(datePojo.getMsg())) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }
        PowerQualityData data;
        try {
            data = assesService.getPowerQualityVol(datePojo.getStartTime(), datePojo.getEndTime(), overview);
            result = PubUtils.assignmentResultLog(data, 200, "概览获取电压偏差数据成功", "查询概览电能质量评估", "成功", result);
            userLog.getLog("查询电压偏差", "成功", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
        } catch (Exception e) {
            userLog.getLog("查询电压偏差", "失败", PubUtils.getIpAddr(request), LogTypeEnum.BUSSINESS.toString(), 0);
            result = PubUtils.assignmentResultLog(null, 200, "概览获取电压偏差数据失败", "查询概览电能质量评估", "失败", result);
        }
        return result;
    }


    /**
     * 概览--》终端状态 查询稳态概览终端状态
     */
    @PostMapping("deviceStatus")
    @ResponseBody
    public HttpResult getDeviceStatus(String overview, 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.getDeviceData(overview, 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());
            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, 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);
            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;
    }


    /**
     * 概览--》数据完整性
     *
     * @param startTime 起始时间
     * @param endTime   截止时间
     */
    @PostMapping("dataIntegrality")
    @ResponseBody
    public HttpResult dataIntegrality(String startTime, String endTime, String overview, HttpServletRequest request) {
        HttpResult result = PubUtils.initResult(TokenManager.getToken().getLoginName(), request, LogTypeEnum.BUSSINESS.toString(), 0);
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "dataIntegrality");
        if (!StringUtils.isEmpty(datePojo.getMsg())) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }
        DeviceGeneral data;
        try {
            data = deviceService.getDataIntegrality(datePojo.getStartTime(), datePojo.getEndTime(), overview);
            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;
    }

    /**
     * 概览---》终端在线率---》区域点击查询
     *
     * @param startTime 起始时间
     * @param endTime   截止时间
     * @param gist      查询关键词
     */
    @PostMapping("cityOnlineRate")
    @ResponseBody
    public HttpResult cityOnlineRate(String startTime, String endTime, String gist, HttpServletRequest request) {
        HttpResult result;
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "cityOnlineRate");
        if (!StringUtils.isEmpty(datePojo.getMsg())) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }
        List<SimpleValue> values;
        try {
            values = deviceService.getCityOnlineRate(datePojo.getStartTime(), datePojo.getEndTime(), gist);
            result = PubUtils.assignmentResult(values, 200, "获取终端在线率数据成功");
        } catch (Exception e) {
            logger.error("获取终端运行数据失败,方法名：cityOnlineRate");
            result = PubUtils.assignmentResult(null, 500, "获取终端在线率数据失败");
        }
        return result;
    }


    /**
     * 概览---》终端在线率---》终端厂家点击查询
     *
     * @param startTime 起始时间
     * @param endTime   截止时间
     * @param gist      查询关键词
     */
    @PostMapping("companyOnlineRate")
    @ResponseBody
    public HttpResult companyOnlineRate(String startTime, String endTime, String gist, HttpServletRequest request) {
        HttpResult result;
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "companyOnlineRate");
        if (!StringUtils.isEmpty(datePojo.getMsg())) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }
        List<SimpleValue> values;
        try {
            values = deviceService.getCompanyOnlineRate(datePojo.getStartTime(), datePojo.getEndTime(), gist);
            result = PubUtils.assignmentResult(values, 200, "获取终端在线率数据成功");
        } catch (Exception e) {
            logger.error("获取终端运行数据失败,方法名：companyOnlineRate");
            result = PubUtils.assignmentResult(null, 500, "获取终端在线率数据失败");
        }
        return result;
    }

    /**
     * 概览---》数据完整性---》区域点击查询
     *
     * @param startTime 起始时间
     * @param endTime   截止时间
     * @param gist      查询关键词
     */
    @PostMapping("cityIntegrality")
    @ResponseBody
    public HttpResult cityIntegrality(String startTime, String endTime, String gist, HttpServletRequest request) {
        HttpResult result;
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "cityIntegrality");
        if (!StringUtils.isEmpty(datePojo.getMsg())) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }
        List<SimpleValue> values;
        try {
            values = deviceService.getCityIntegrality(datePojo.getStartTime(), datePojo.getEndTime(), gist);
            result = PubUtils.assignmentResult(values, 200, "获取数据完整性数据成功");
        } catch (Exception e) {
            logger.error("获取终端运行数据失败,方法名：cityIntegrality");
            result = PubUtils.assignmentResult(null, 500, "获取数据完整性数据失败");
        }
        return result;
    }

    /**
     * 概览---》数据完整性---》电压等级点击查询
     *
     * @param startTime 起始时间
     * @param endTime   截止时间
     * @param gist      查询关键词
     */
    @PostMapping("voltageIntegrality")
    @ResponseBody
    public HttpResult voltageIntegrality(String startTime, String endTime, String gist, HttpServletRequest request) {
        HttpResult result;
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "voltageIntegrality");
        if (!StringUtils.isEmpty(datePojo.getMsg())) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }
        List<SimpleValue> values;
        try {
            values = deviceService.getVoltageIntegrality(datePojo.getStartTime(), datePojo.getEndTime(), gist);
            result = PubUtils.assignmentResult(values, 200, "获取数据完整性数据成功");
        } catch (Exception e) {
            logger.error("获取终端运行数据失败,方法名：voltageIntegrality");
            result = PubUtils.assignmentResult(null, 500, "获取数据完整性数据失败");
        }
        return result;
    }

    /**
     * 概览---》数据完整性---》终端厂家点击查询
     *
     * @param startTime 起始时间
     * @param endTime   截止时间
     * @param gist      查询关键词
     */
    @PostMapping("companyIntegrality")
    @ResponseBody
    public HttpResult companyIntegrality(String startTime, String endTime, String gist, HttpServletRequest request) {
        HttpResult result;
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "companyIntegrality");
        if (!StringUtils.isEmpty(datePojo.getMsg())) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }
        List<SimpleValue> values;
        try {
            values = deviceService.getCompanyIntegrality(datePojo.getStartTime(), datePojo.getEndTime(), gist);
            result = PubUtils.assignmentResult(values, 200, "获取数据完整性数据成功");
        } catch (Exception e) {
            logger.error("获取终端运行数据失败,方法名：companyIntegrality");
            result = PubUtils.assignmentResult(null, 500, "获取数据完整性数据失败");
        }
        return result;
    }


    /**
     * 概览---》稳态合格率
     *
     * @param startTime 起始时间
     * @param endTime   截止时间
     */
    @PostMapping("frequencyDeviation")
    @ResponseBody
    public HttpResult frequencyDeviation(String startTime, String endTime, String overview, HttpServletRequest request) {
        HttpResult result = PubUtils.initResult(TokenManager.getToken().getLoginName(), request, LogTypeEnum.BUSSINESS.toString(), 0);
        DatePojo datePojo;
        datePojo = PubUtils.validateDate(startTime, endTime, "frequencyDeviation");
        if (!StringUtils.isEmpty(datePojo.getMsg())) {
            result = PubUtils.assignmentResult(null, 500, datePojo.getMsg());
            return result;
        }
        SteadyTypeData steadyTypeData;
        try {
            steadyTypeData = lineService.getFrequencyDeviation(datePojo.getStartTime(), datePojo.getEndTime(), overview);
            if (null == steadyTypeData) {
                result = PubUtils.assignmentResultLog(null, 500, "获取全部数据为空", "查询稳态指标合格率", "成功", result);
            } else {
                result = PubUtils.assignmentResultLog(steadyTypeData, 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;
    }

    @PostMapping("getRole")
    @ResponseBody
    public int getRole(String url) {
        return lineService.getRole(url);
    }

}
