package com.pqs9200.controller.business;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.njcn.enums.ProjectEnum;
import com.njcn.pojo.commons.WebItem;
import com.njcn.pojo.system.SubSystem;
import com.njcn.service.system.SystemService;
import com.njcn.shiro.token.TokenManager;
import com.njcn.utils.AppConfig;
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.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping(value = "business")
public class BusinessController {

    ObjectMapper objectMapper = new ObjectMapper();

    @Resource
    private SystemService systemService;

    @Autowired
    private AppConfig appConfig;

    // 日志记录
    private static final Logger logger = LoggerFactory.getLogger(BusinessController.class);

    /**
     * 高级分析中，省级专员页面跳转
     *
     * @param request
     * @return
     */
    // 跳转高级分析事件关联分析处理页面
    @RequestMapping(value = "relevance")
    public String relevance(HttpServletRequest request, HttpSession session) {
        session.setAttribute("line_index", null);
        return "business/advanced/relevance";
    }

    //获取未处理暂降事件
    @RequestMapping(value = "relevances")
    public String relevances(HttpServletRequest request, HttpSession session,String line_index) {
    	line_index = XssFilterUtil.dealString(line_index);
        if (line_index != null) {
            session.setAttribute("line_index", line_index);
        }
        return "business/advanced/relevance";
    }

    // 跳转高级分析影响范围页面
    @RequestMapping(value = "range")
    public String range(HttpServletRequest request) {
        return "business/advanced/range";
    }

    // 跳转高级分析多事件波形比较页面
    @RequestMapping(value = "eventscompare")
    public String events(HttpServletRequest request) {
        return "business/advanced/eventsCompare";
    }

    //跳转归一化结果策略展示
    @RequestMapping(value = "processEvent")
    public String processEvent() {
        return "business/advanced/processEvent";
    }

    // 跳转事件关联分析归一化界面
    @RequestMapping(value = "eventAnalysis")
    public String eventAnalysis(HttpServletRequest request) {
        return "business/advanced/eventAnalysis";
    }

    @RequestMapping(value = "relevanceresult")
    public String relevanceresult(HttpServletRequest request) {
        return "business/advanced/relevanceresult";
    }

    @RequestMapping(value = "relevancewaveform")
    public String relevancewaveform(HttpServletRequest request) {
        return "business/advanced/relevancewaveform";
    }

    @RequestMapping(value = "rangewaveform")
    public String rangewaveform(HttpServletRequest request) {
        return "business/advanced/rangewaveform";
    }

    // 跳转到范围分析界面
    @RequestMapping(value = "fwfx")
    public String fwfx(HttpServletRequest request, HttpSession session,String eventass_index) {
    	eventass_index = XssFilterUtil.dealString(eventass_index);
        session.setAttribute("eventass_index", eventass_index);
        return "business/overview/fwfx";
    }

    // 跳转高级分析波形页面
    @RequestMapping(value = "waveform")
    public String waveform(HttpServletRequest request, HttpSession session) {
        return "business/advanced/waveform";
    }

    // 波形对比
    @RequestMapping(value = "wavecompare")
    public String wavecompare(HttpServletRequest request) {
        return "business/advanced/wavecompare";
    }

    // 新增波形对比文件
    @RequestMapping(value = "addwave")
    public String addwave(HttpServletRequest request) {
        return "business/advanced/addwave";
    }

    // 新增波形对比
    @RequestMapping(value = "addwavecompare")
    public String addwavecompare(HttpServletRequest request) {
        return "business/advanced/addwavecompare";
    }

    // 删除波形对比
    @RequestMapping(value = "deletewave")
    public String deletewave(HttpServletRequest request) {
        return "business/advanced/deletewaveform";
    }

    //高级分析数据界面图形展示
    @RequestMapping(value = "analysis")
    public String analysis(HttpServletRequest request, HttpSession session,String maxy,String miny,String time,String pt) {
    	maxy = XssFilterUtil.dealString(maxy);
    	miny = XssFilterUtil.dealString(miny);
    	time = XssFilterUtil.dealString(time);
    	pt = XssFilterUtil.dealString(pt);
        session.setAttribute("maxy", maxy);
        session.setAttribute("miny", miny);
        session.setAttribute("time", time);
        session.setAttribute("pt", pt);
        return "business/area/analysis";
    }

    //跳转报表
    @GetMapping("weeklyreport")
    public String weeklyReport() {
        return "business/report/weeklyreport";
    }

    /**
     * 普通用户就可以访问
     *
     * @param request
     * @param session
     * @return
     */
    // 跳转暂降列表波形分析页面
    @RequestMapping(value = "vdwaveform")
    public String vdWaveform(HttpServletRequest request, HttpSession session,String eventIndex) {
    	eventIndex = XssFilterUtil.dealString(eventIndex);
        session.setAttribute("eventIndex", eventIndex);
        return "business/area/waveform";
    }

    // 跳转暂降列表波形对比页面
    @RequestMapping(value = "waveCompare")
    public ModelAndView waveCompare(String eventIndex) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("business/area/waveCompare");
        modelAndView.addObject("eventIndex",eventIndex);
        return modelAndView;
    }

    // 跳转监测点页面
    @RequestMapping(value = "monitor")
    public String monitor(HttpServletRequest request, HttpSession session,String line_index) {
    	line_index = XssFilterUtil.dealString(line_index);
        if (line_index != null) {
            session.setAttribute("line_index", line_index);
        }
        return "business/monitor/monitor";
    }

    // 跳转showeventdatalist页面
    @RequestMapping(value = "showeventdatalist")
    public String showeventdatalist(HttpServletRequest request, HttpSession session,String lineIndex) {
    	lineIndex = XssFilterUtil.dealString(lineIndex);
        session.setAttribute("lineIndex", lineIndex);
        return "business/monitor/showeventdatalist";
    }

    // 跳转showeventtime页面
    @RequestMapping(value = "showeventtime")
    public String showeventtime(HttpServletRequest request, HttpSession session,String lineIndex) {
    	lineIndex = XssFilterUtil.dealString(lineIndex);
        session.setAttribute("lineIndex", lineIndex);
        return "business/monitor/showeventtime";
    }

    // 跳转showeventchance页面
    @RequestMapping(value = "showeventchance")
    public String showeventchance(HttpServletRequest request, HttpSession session,String lineIndex) {
    	lineIndex = XssFilterUtil.dealString(lineIndex);
        session.setAttribute("lineIndex", lineIndex);
        return "business/monitor/showeventchance";
    }

    // 跳转showdevicedetail页面
    @RequestMapping(value = "showdevicedetail")
    public String showdevicedetail(HttpServletRequest request, HttpSession session,String lineIndex, String longitude,String latitude,String image) {
    	lineIndex = XssFilterUtil.dealString(lineIndex);
    	longitude = XssFilterUtil.dealString(longitude);
    	latitude = XssFilterUtil.dealString(latitude);
    	image = XssFilterUtil.dealString(image);
        session.setAttribute("lineIndex", lineIndex);
        session.setAttribute("longitude", longitude);
        session.setAttribute("latitude", latitude);
        session.setAttribute("image", image);
        return "business/monitor/showdevicedetail";
    }

    // 跳转showeventdensity页面
    @RequestMapping(value = "showeventdensity")
    public String showeventdensity(HttpServletRequest request, HttpSession session,String lineIndex) {
    	lineIndex = XssFilterUtil.dealString(lineIndex);
        session.setAttribute("lineIndex", lineIndex);
        return "business/monitor/showeventdensity";
    }

    // 跳转densityDetail页面
    @RequestMapping(value = "densityDetail")
    public String densityDetail(HttpServletRequest request, HttpSession session,String lineIndex,String startTime,String endTime) {
    	lineIndex = XssFilterUtil.dealString(lineIndex);
    	startTime = XssFilterUtil.dealString(startTime);
    	endTime = XssFilterUtil.dealString(endTime);
        session.setAttribute("lineIndex", lineIndex);
        session.setAttribute("startTime", startTime);
        session.setAttribute("endTime", endTime);
        return "business/monitor/densityDetail";
    }

    // 跳转事件统计
    @RequestMapping(value = "tongji")
    public String tongji(HttpServletRequest request) {
        return "business/monitor/tongji";
    }

    // 跳转事件分析
    @RequestMapping(value = "analyze")
    public String analyze(HttpServletRequest request) {
        return "business/monitor/analyze";
    }

    // 跳转运行管理
    @RequestMapping(value = "manage")
    public String manage(HttpServletRequest request) {
        return "business/monitor/manage";
    }

    // 跳转运行管理台账页面
    @RequestMapping(value = "machine")
    public String machine(HttpServletRequest request) {
        return "business/manage/machineAccount";
    }

    // 跳转终端新增界面
    @RequestMapping(value = "addDev")
    public String addDev(HttpServletRequest request) {
        return "business/manage/addDev";
    }

    // 跳转终端运行管理页面
    @RequestMapping(value = "runtime")
    public String runtime(HttpServletRequest request, HttpSession session,String line_index) {
        return "business/manage/runtime";
    }

//    // 跳转到终端状态更新列表
//    @GetMapping(value = "terminallog")
//    public String terminallog() {
//        return "business/manage/terminalLogManage";
//    }

    // 跳转终端运行状态页面
    @RequestMapping(value = "runtimestatus")
    public String runtimestatus(HttpServletRequest request) {
        return "business/manage/runtimeStatus";
    }

    // 跳转终端运行状态页面
    @RequestMapping(value = "onlinerate")
    public String onlinerate(HttpServletRequest request) {
        return "business/manage/onlineRate";
    }


    // 跳转概览首页
    @RequestMapping(value = "glindex")
    public String dlxxt(HttpServletRequest request) {
        return "business/overview/glindex";
    }

    // 跳转到地图界面
    @RequestMapping(value = "dlxxt")
    public String indexmap(HttpServletRequest request) {
        return "business/overview/dlxxt";
    }

    @RequestMapping(value = "balance")
    public String indexbalance(HttpServletRequest request) {
        return "business/overview/balance";
    }

    // 跳转showbximg页面
    @RequestMapping(value = "showbximg")
    public String showbximg(HttpServletRequest request, HttpSession session,String eventIndex) {
    	eventIndex = XssFilterUtil.dealString(eventIndex);
        session.setAttribute("eventIndex", eventIndex);
        return "business/monitor/showbximg";
    }

    // 跳转到终端运行信息界面
    @RequestMapping(value = "sbyxzt")
    public String sbyxzt(HttpServletRequest request) {
        return "business/overview/sbyxzt";
    }

    // 跳转到区域电网信息界面
    @RequestMapping(value = "qygk")
    public String qygk(HttpServletRequest request) {
        return "business/overview/qygk";
    }

    // 跳转终端统计页面
    @RequestMapping(value = "statistics")
    public String statistics(HttpServletRequest request) {
        return "business/manage/machineStatistics";
    }

    // 跳转通讯测试页面
    @RequestMapping(value = "communicate")
    public String communicate(HttpServletRequest request) {
        return "business/manage/communicate";
    }

    // 跳转定值管理页面
    @RequestMapping(value = "valuemanage")
    public String valuemanage(HttpServletRequest request) {
        return "business/manage/manage";
    }

    // 跳转区域概览页面
    @RequestMapping(value = "general")
    public String general(HttpServletRequest request) {
        return "business/area/general";
    }

    @RequestMapping(value = "balancearea")
    public String balance(HttpServletRequest request) {
        return "business/area/balance";
    }

    @RequestMapping(value = "mapmonitor")
    public String mapmonitor(HttpServletRequest request) {
        return "business/overview/mapMonitor";
    }

    @RequestMapping(value = "mapevent")
    public String mapevent(HttpServletRequest request) {
        return "business/overview/mapEvent";
    }

    @RequestMapping(value = "devmap")
    public String devmap(HttpServletRequest request) {
        return "business/overview/devmap";
    }

    @RequestMapping(value = "qypg")
    public String qypg(HttpServletRequest request) {
        return "business/overview/qypg";
    }

    @RequestMapping(value = "eventmap")
    public String eventmap(HttpServletRequest request) {
        return "business/overview/eventmap";
    }

    // 跳转区域分析页面
    @RequestMapping(value = "areastatistics")
    public String areaStatistics(HttpServletRequest request) {
        return "business/area/areaStatistics";
    }

    // 跳转暂降程度页面
    @RequestMapping(value = "level")
    public String level(HttpServletRequest request) {
        return "business/area/level";
    }

    // 跳转暂降列表页面
    @RequestMapping(value = "vdlist")
    public String vdList(HttpServletRequest request) {
        return "business/area/vdList";
    }

    // 跳转暂降事件列表范围查看
    @RequestMapping(value = "viewScope")
    public String viewScope(HttpServletRequest request) {
        return "business/area/viewScope";
    }

    @RequestMapping(value = "userhelp")
    public String userHelp(HttpServletRequest request, HttpSession session,String parameter) {
    	parameter = XssFilterUtil.dealString(parameter);
        session.setAttribute("parameter", parameter);
        return "business/manage/userHelp";
    }

    @RequestMapping(value = "sourcemanage")
    public String sourceManage(HttpServletRequest request) {
        return "business/manage/sourceManage";
    }

    /******/

    @GetMapping("/doBusiness")
    public ModelAndView doBusiness() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("business/main");
        //判断是否需要显示切换系统以及待切换的系统信息
        List<SubSystem> subSystems = systemService.getActiveSystem();
        List<WebItem> webItems=new ArrayList<>();
        boolean displayFlag = false;
        for (SubSystem subSystem : subSystems) {
            if (!subSystem.getSystemEName().equalsIgnoreCase(ProjectEnum.PQS9200.getItem())) {
                displayFlag = true;
                WebItem webItem=new WebItem();
                webItem.setItem(subSystem.getSystemEName());
                webItem.setName(subSystem.getSystemEName().toUpperCase() + subSystem.getSystemName());
                webItems.add(webItem);
            }
        }
//        WebItem webItem=new WebItem();
//        webItem.setItem("pqs9300");
//        webItem.setName("PQS9800大屏展示系统");
//        webItems.add(webItem);
        if (Objects.equals(TokenManager.getNickname(), appConfig.getThirdName())){
            displayFlag = false;
        }
        modelAndView.addObject("webItems",webItems);
        modelAndView.addObject("displayFlag", displayFlag);
        return modelAndView;
    }

    /**
     * 跳转到终端异常
     */
    @RequestMapping("sbyc")
    public String sbyc(HttpServletRequest request) {
        return "business/overview/sbyc";
    }

    /**
     * 跳转到终端异常
     */
    @RequestMapping("zjsj")
    public String zjsj(HttpServletRequest request) {
        return "business/overview/zjsj";
    }


    @RequestMapping(value = "chart")
    public String chart(HttpServletRequest request) {
        return "business/settings/userLogChart";
    }

    @RequestMapping(value = "loginmanage")
    public String loginmanage(HttpServletRequest request) {
        return "business/settings/userLoginManage";
    }


    //读取配置文件
    @PostMapping("getWebsocketURL")
    @ResponseBody
    public String getWebSocketURL() {
        return appConfig.getWebsocket();
    }

    //跳转报告界面
    @RequestMapping(value = "report")
    public String report(HttpServletRequest request) {
        return "business/report/report";
    }

    //跳转报告界面
    @RequestMapping(value = "areareport")
    public String areareport(HttpServletRequest request) {
        return "business/report/areareport";
    }

    //跳转报告界面
    @RequestMapping(value = "eventreport")
    public String eventreport(HttpServletRequest request) {
        return "business/report/eventreport";
    }

    //跳转报告界面
    @RequestMapping(value = "addrpdata")
    public String addRPData(HttpServletRequest request) {
        return "business/settings/addRPData";
    }

    //跳转报告界面
    @RequestMapping(value = "modrpdata")
    public String modRpData(HttpServletRequest request, HttpSession session,String rdIndex) {
    	rdIndex = XssFilterUtil.dealString(rdIndex);
        session.setAttribute("rdIndex", rdIndex);
        return "business/settings/modRPDate";
    }

    //跳转报告界面
    @RequestMapping(value = "addrpploy")
    public String addRPPloy(HttpServletRequest request) {
        return "business/settings/addRPPloy";
    }

    //跳转报告界面
    @RequestMapping(value = "modrpploy")
    public String modRpPloy(HttpServletRequest request, HttpSession session,String rpIndex) {
    	rpIndex = XssFilterUtil.dealString(rpIndex);
        session.setAttribute("rpIndex", rpIndex);
        return "business/settings/modRpPloy";
    }

    //跳转报告界面
    @RequestMapping(value = "addnode")
    public String addNode(HttpServletRequest request) {
        return "business/settings/addNode";
    }

    //跳转报告界面
    @RequestMapping(value = "addploy")
    public String addPloy(HttpServletRequest request) {
        return "business/settings/addPloy";
    }

    /**
     * 跳转新增前置机
     */
    @RequestMapping(value = "addnodeinfo")
    public String addnodeinfo(HttpServletRequest request) {
        return "business/manage/addNodeInfo";
    }

    /**
     * 跳转前置机信息
     */
    @RequestMapping(value = "nodeinfo")
    public String nodeinfo(HttpServletRequest request) {
        return "business/manage/nodeInfo";
    }

    /**
     * 修改前置机信息
     */
    @RequestMapping(value = "modnode")
    public String modnode(HttpServletRequest request, HttpSession session,String nodeIndex) {
    	nodeIndex = XssFilterUtil.dealString(nodeIndex);
        session.setAttribute("nodeIndex", nodeIndex);
        return "business/manage/modNode";
    }

    /**
     * 导入终端
     */
    @RequestMapping(value = "importdev")
    public String importdev(HttpServletRequest request) {
        return "business/manage/importdev";
    }

    /**
     * 跳转404页面
     */
    @RequestMapping(value = "404")
    public String notFound(HttpServletRequest request) {
        return "business/error/404";
    }

    @RequestMapping("/userDetail")
    public String userDetail(HttpServletRequest request, HttpSession session,String userIndex) {
    	userIndex = XssFilterUtil.dealString(userIndex);
        session.setAttribute("userIndex", userIndex);
        return "business/userDetail";
    }

    /**
     * 跳转到修改密码
     */
    @RequestMapping("updatePassWord")
    public String updatePassWord(HttpServletRequest request, HttpSession session,String userIndex) {
    	userIndex = XssFilterUtil.dealString(userIndex);
        session.setAttribute("userIndex", userIndex);
        return "business/updatePassWord";
    }

    /**
     * 跳转到事件提示
     */
    @RequestMapping("tipwindow")
    public String tipwindow(HttpServletRequest request) {
        return "business/overview/tipwindow";
    }

    /**
     * 暂降列表页面多条件筛选按钮弹出页面
     * @author cdf
     * @date 2021/1/20
     */
    @RequestMapping("vdlistquery")
    public String vdlistquery(){
        return "business/area/vdListQuery";
    }

    /**
     * 跳转网络拓扑图界面
     * xuyang
     */
    @RequestMapping(value = "ycjxt")
    public String ycjxt(HttpServletRequest request) {
        return "business/line/ycjxt";
    }


    @GetMapping(value = "bfour")
    public String bfour(HttpServletRequest request) {
        return "business/svg/b4";
    }

    @GetMapping(value = "waterstation")
    public String waterstation(HttpServletRequest request) {
        return "business/svg/waterStation";
    }

    @GetMapping(value = "bfive")
    public String bfive(HttpServletRequest request) {
        return "business/svg/b5";
    }

    /**
     * 跳转到监测点台账信息
     */
    @RequestMapping("lineledger")
    public String lineLedger() {
        return "business/manage/lineLedger";
    }


    @GetMapping("sagdomainshow")
    public String sagDomainShow(){
        return "business/advanced/sagDomainShow";
    }

    @GetMapping("sagdomainedit")
    public String sagDomainEdit(HttpServletRequest request, HttpSession session, String svgIndex){
        svgIndex = XssFilterUtil.dealString(svgIndex);
        session.setAttribute("svgIndex", svgIndex);
        return "business/advanced/sagDomainEdit";
    }

    @GetMapping("sagDomainDiagram")
    public String sagDomainDiagram(HttpServletRequest request, HttpSession session, String svgName){
        svgName = XssFilterUtil.dealString(svgName);
        session.setAttribute("svgName", svgName);
        return "business/advanced/sagDomainDiagram";
    }
}
