package com.lightinit.hsdatashow.controller.ms;

import com.lightinit.hsdatashow.entity.*;
import com.lightinit.hsdatashow.model.ResultState;
import com.lightinit.hsdatashow.service.ms.IMSCommunicationService;
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.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * @Author: sunxiang
 * @Date: 2018/8/31 11:12
 * @Description:
 **/
@Controller
@RequestMapping(value = "/ms/commu")
public class MSCommunicationController {


    private static LinkedHashMap<String,String> getChartTable(){
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("视频监控","VideoMonitor");
        map.put("电视和wifi开通情况","TvWifi");
        map.put("区域通信故障数量","AreaCommunicationBreak");
        map.put("通信故障占比情况","AreaCommunicationBreakProportion");
        map.put("光纤","Fiber");
        return map;
    }

    @Autowired
    private IMSCommunicationService msCommunicationService;
    @RequestMapping(value="index.htm")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("ms/communication/index");
        modelAndView.addObject("chartTable",getChartTable());
        return modelAndView;
    }

    @RequestMapping(value="list.action")
    public ModelAndView list(String tableType,int pageNo){
        ModelAndView modelAndView=new ModelAndView("ms/communication/"+tableType+"_list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        List list=msCommunicationService.list(tableType,pager);
        long total=msCommunicationService.queryCount(tableType);
        pager.setTotal(total);
        modelAndView.addObject("list",list);
        modelAndView.addObject("pager",pager);
        return modelAndView;
    }

    @RequestMapping(value="add.htm")
    public ModelAndView add(String tableType,Long id){
        ModelAndView modelAndView=new ModelAndView("ms/communication/"+tableType+"_add");
        Object obj=null;
        if(id !=null){
            obj=msCommunicationService.findById(tableType,id);
        }
        modelAndView.addObject("outputModel",obj);
        return modelAndView;
    }

    @RequestMapping(value="delete.action")
    @ResponseBody
    public ResultState delete(String tableType, Long id) {
        ResultState resultState =msCommunicationService.delete(tableType,id);
        return resultState;
    }

    @RequestMapping(value="addVideoMonitor.action")
    @ResponseBody
    public ResultState addVideoMonitor(VideoMonitor videoMonitor){
        boolean isEdit=videoMonitor.getId()==null?false:true;
        ResultState resultState=msCommunicationService.add(videoMonitor,isEdit);
        return resultState;
    }

    @RequestMapping(value="addTvWifi.action")
    @ResponseBody
    public ResultState addTvWifi(TvWifi tvWifi){
        boolean isEdit=tvWifi.getId()==null?false:true;
        ResultState resultState=msCommunicationService.add(tvWifi,isEdit);
        return resultState;
    }

    @RequestMapping(value="addAreaCommunicationBreak.action")
    @ResponseBody
    public ResultState addAreaCommunicationBreak(AreaCommunicationBreak areaCommunicationBreak){
        boolean isEdit=areaCommunicationBreak.getId()==null?false:true;
        ResultState resultState=msCommunicationService.add(areaCommunicationBreak,isEdit);
        return resultState;
    }

    @RequestMapping(value="addAreaCommunicationBreakProportion.action")
    @ResponseBody
    public ResultState addAreaCommunicationBreakProportion(AreaCommunicationBreakProportion areaCommunicationBreakProportion){
        boolean isEdit=areaCommunicationBreakProportion.getId()==null?false:true;
        ResultState resultState=msCommunicationService.add(areaCommunicationBreakProportion,isEdit);
        return resultState;
    }

    @RequestMapping(value="addFiber.action")
    @ResponseBody
    public ResultState addFiber(Fiber fiber){
        boolean isEdit=fiber.getId()==null?false:true;
        ResultState resultState=msCommunicationService.add(fiber,isEdit);
        return resultState;
    }
}
