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.common.security.service.SecurityServiceInterface;
import com.sptg.iot.api.constant.IotSecurityToken;
import com.sptg.iot.api.dto.IotDeviceDto;
import com.sptg.iot.api.entity.IotDevice;
import com.sptg.iot.api.feigns.IotDeviceFeign;
import com.sptg.permissions.api.entity.Account;
import com.sptg.video.cloud.api.constant.VideoCloudSecurityToken;
import com.sptg.video.cloud.api.entity.VCDevice;
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/device")
@RequiredArgsConstructor
public class IotDeviceController {

    private final IotDeviceFeign iotDeviceFeign;

    /**
     * 功能描述:物联网管理页面初始化
     * @return ModelAndView
     */
    @GetMapping("/index")
    @Security(token = IotSecurityToken.IOT_DEVICE_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView index(){
        return new ModelAndView("iot/iot-device-manage");
    }

    /**
     * 功能描述: 根据条件分页查询iot设备列表
     * @param pageBean 分页条件
     * @return ResultBean<Page>
     */
    @GetMapping(value = "/pagingQuery")
    @Security(token = IotSecurityToken.IOT_DEVICE_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<IotDevice>> pagingQuery(IotDeviceDto iotDeviceDto, PageBean<IotDevice, IotDeviceDto> pageBean){
        // 暂时拿全部的数据
        iotDeviceDto.setPage(pageBean);
        return this.iotDeviceFeign.pagingQuery(iotDeviceDto);
    }

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

    /**
     * 功能描述: 根据prefix条件分页查询iot设备列表
     * @return ResultBean<Page>
     */
    @GetMapping(value = "/sync/listByPrefix")
    @Security(token = IotSecurityToken.IOT_DEVICE_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<String> syncDeviceListByPrefix(IotDeviceDto iotDeviceDto){
        return this.iotDeviceFeign.syncIotDeviceListByPrefix(iotDeviceDto);
    }
}
