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.IotApiLogDto;
import com.sptg.iot.api.entity.IotApiLog;
import com.sptg.iot.api.feigns.IotApiLogFeign;
import com.sptg.iot.api.feigns.IotApiLogFeign;
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/apiLog")
@RequiredArgsConstructor
public class IotApiLogController {

    private final IotApiLogFeign iotApiLogFeign;

    /**
     * 功能描述:iot接口日志列表初始化页面
     * @return ModelAndView
     */
    @GetMapping("/index")
    @Security(token = IotSecurityToken.IOT_API_LOG_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView index(){
        return new ModelAndView("iot/iot-api-log-manage");
    }

    /**
     * 功能描述: 根据条件分页查询iot接口日志列表
     * @param pageBean 分页条件
     * @return ResultBean<Page>
     */
    @GetMapping(value = "/pagingQuery")
    @Security(token = IotSecurityToken.IOT_API_LOG_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<IotApiLog>> pagingQuery(IotApiLogDto iotApiLogDto, PageBean<IotApiLog, IotApiLogDto> pageBean){
        // 暂时拿全部的数据
        iotApiLogDto.setPage(pageBean);
        return this.iotApiLogFeign.pagingQuery(iotApiLogDto);
    }

    /**
     * 功能描述: iot接口日志详情界面
     * @return ModelAndView
     */
    @GetMapping(value = "/detail/{id}")
    @Security(token = IotSecurityToken.IOT_API_LOG_MANAGE, format = DataFormatEnum.VIEW)
    public ModelAndView detail(@PathVariable(value = "id") Long id){
        IotApiLog iotApiLog = this.iotApiLogFeign.getIotApiLogById(id);
        if (iotApiLog == null){
            return new ModelAndView("common/404");
        }
        ModelAndView modelAndView = new ModelAndView("iot/iot-api-log-detail");
        modelAndView.addObject("iotApiLog",iotApiLog);
        return modelAndView;
    }
}
