package com.sptg.gateway.controller.iot;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.PageBean;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.iot.api.constant.IotSecurityToken;
import com.sptg.iot.api.dto.IotAlarmDto;
import com.sptg.iot.api.entity.IotAlarm;
import com.sptg.iot.api.entity.IotDevice;
import com.sptg.iot.api.feigns.IotAlarmFeign;
import com.sptg.iot.api.feigns.IotAlarmFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author zwx
 * @create 2020/11/17 14:40
 */
@RestController
@RequestMapping(value = "/iot/alarm")
@RequiredArgsConstructor
public class IotAlarmController {

    private final IotAlarmFeign iotAlarmFeign;

    /**
     * 功能描述:iot设备告警列表初始化页面
     * @return ModelAndView
     */
    @GetMapping("/index")
    @Security(token = IotSecurityToken.IOT_ALARM_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView index(){
        return new ModelAndView("iot/iot-alarm-manage");
    }

    /**
     * 功能描述: 根据条件分页查询iot告警列表
     * @param pageBean 分页条件
     * @return ResultBean<Page>
     */
    @GetMapping(value = "/pagingQuery")
    @Security(token = IotSecurityToken.IOT_ALARM_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<IotAlarm>> pagingQuery(IotAlarmDto IotAlarmDto, PageBean<IotAlarm, IotAlarmDto> pageBean){
        IotAlarmDto.setPage(pageBean);
        return this.iotAlarmFeign.pagingQuery(IotAlarmDto);
    }

    /**
     * 功能描述: iot设备详情界面
     * @return ModelAndView
     */
    @GetMapping(value = "/detail/{id}")
    @Security(token = IotSecurityToken.IOT_ALARM_MANAGE, format = DataFormatEnum.VIEW)
    public ModelAndView detail(@PathVariable(value = "id") Long id){
        IotAlarm iotAlarm = this.iotAlarmFeign.getIotAlarmById(id);
        if (iotAlarm == null){
            return new ModelAndView("common/404");
        }
        ModelAndView modelAndView = new ModelAndView("iot/iot-alarm-detail");
        modelAndView.addObject("iotAlarm",iotAlarm);
        return modelAndView;
    }
}
