package com.sptg.gateway.controller.iot;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.estate.api.constant.EstateSecurityToken;
import com.sptg.iot.api.constant.IotSecurityToken;
import com.sptg.iot.api.dto.IotAlarmDto;
import com.sptg.iot.api.dto.IotAlarmThresholdDto;
import com.sptg.iot.api.dto.IotAlarmWorkOrderDto;
import com.sptg.iot.api.dto.IotDeviceDto;
import com.sptg.iot.api.entity.IotAuthentication;
import com.sptg.iot.api.entity.IotDevice;
import com.sptg.iot.api.entity.IotSubscribe;
import com.sptg.iot.api.feigns.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author zwx
 * @create 2020/11/10 11:58
 */
@RestController
@RequestMapping("/iot")
@RequiredArgsConstructor
public class IotController {

    private final IotAuthFeign iotAuthFeign;
    private final IotDeviceFeign iotDeviceFeign;
    private final IotAlarmFeign iotAlarmFeign;
    private final IotAlarmWorkOrderFeign iotAlarmWorkOrderFeign;
    private final IotAlarmThresholdFeign iotAlarmThresholdFeign;
    private final IotSubscribeFeign iotSubscribeFeign;

    /**
     * 接口认证
     * @param appId
     * @return
     */
    @PostMapping(value = "/auth/{appId}")
    public ResultBean<String> auth(@PathVariable(value = "appId") String appId){
        ResultBean<IotAuthentication> resultBean = this.iotAuthFeign.auth(appId);
        if(resultBean.getData() != null){
            return new ResultBean<>(ResultEnum.SUCCESS, "认证成功");
        }else{
            return new ResultBean<>(ResultEnum.FAILURE, "认证失败，请查看日志");
        }
    }

    /**
     * 同步设备列表
     * @param iotDeviceDto
     * @return
     */
    @PostMapping(value = "/device/sync/list")
    public ResultBean<String> syncIotDeviceList(@RequestBody IotDeviceDto iotDeviceDto){
        ResultBean<String> resultBean = this.iotDeviceFeign.syncIotDeviceList(iotDeviceDto);
        return resultBean;
    }

    /**
     * 同步设备详情
     * @param iotDeviceDto
     * @return
     */
    @PostMapping(value = "/device/sync/detail/one")
    public ResultBean<String> syncIotDeviceDetailOne(@RequestBody IotDeviceDto iotDeviceDto){
        ResultBean<String> resultBean = this.iotDeviceFeign.syncIotDeviceDetailOne(iotDeviceDto);
        return resultBean;
    }

    /**
     * 批量同步设备详情
     * @param iotDeviceDtoList
     * @return
     */
    @PostMapping(value = "/device/sync/detail/batch")
    public ResultBean<String> syncIotDeviceDetailBatch(@RequestBody List<IotDeviceDto> iotDeviceDtoList){
        ResultBean<String> resultBean = this.iotDeviceFeign.syncIotDeviceDetailBatch(iotDeviceDtoList);
        return resultBean;
    }

    /**
     * 全量同步设备详情
     * @param iotDeviceDto
     * @return
     */
    @PostMapping(value = "/device/sync/detail/all")
    public ResultBean<String> syncIotDeviceDetailAll(@RequestBody IotDeviceDto iotDeviceDto){
        ResultBean<String> resultBean = this.iotDeviceFeign.syncIotDeviceDetailAll(iotDeviceDto);
        return resultBean;
    }

    /**
     * 同步告警列表
     * @param iotAlarmDto
     * @return
     */
    @PostMapping(value = "/alarm/sync/list")
    public ResultBean<String> syncIotAlarmList(@RequestBody IotAlarmDto iotAlarmDto) {
        ResultBean<String> resultBean = this.iotAlarmFeign.syncIotAlarmList(iotAlarmDto);
        return resultBean;
    }

    /**
     * 同步告警详情
     * @param iotAlarmDto
     * @return
     */
    @PostMapping(value = "/alarm/sync/detail/one")
    public ResultBean<String> syncIotAlarmDetailOne(@RequestBody IotAlarmDto iotAlarmDto) {
        ResultBean<String> resultBean = this.iotAlarmFeign.syncIotAlarmDetailOne(iotAlarmDto);
        return resultBean;
    }

    /**
     * 批量同步告警详情
     * @param iotAlarmDtoList
     * @return
     */
    @PostMapping(value = "/alarm/sync/detail/batch")
    public ResultBean<String> syncIotAlarmDetailBatch(@RequestBody List<IotAlarmDto> iotAlarmDtoList) {
        ResultBean<String> resultBean = this.iotAlarmFeign.syncIotAlarmDetailBatch(iotAlarmDtoList);
        return resultBean;
    }

    /**
     * 全量同步告警详情
     * @param iotAlarmDto
     * @return
     */
    @PostMapping(value = "/alarm/sync/detail/all")
    public ResultBean<String> syncIotAlarmDetailBatch(@RequestBody IotAlarmDto iotAlarmDto) {
        ResultBean<String> resultBean = this.iotAlarmFeign.syncIotAlarmDetailAll(iotAlarmDto);
        return resultBean;
    }

    /**
     * 同步告警工单列表
     * @param iotAlarmWorkOrderDto
     * @return
     */
    @PostMapping(value = "/alarmWorkOrder/sync/list")
    public ResultBean<String> syncIotAlarmWorkOrderList(@RequestBody IotAlarmWorkOrderDto iotAlarmWorkOrderDto) {
        ResultBean<String> resultBean = this.iotAlarmWorkOrderFeign.syncIotAlarmWorkOrderList(iotAlarmWorkOrderDto);
        return resultBean;
    }

    /**
     * 同步告警阈值列表
     * @param iotAlarmThresholdDto
     * @return
     */
    @PostMapping(value = "/alarmThreshold/sync/list")
    public ResultBean<String> syncIotAlarmThresholdList(@RequestBody IotAlarmThresholdDto iotAlarmThresholdDto) {
        ResultBean<String> resultBean = this.iotAlarmThresholdFeign.syncIotAlarmThresholdList(iotAlarmThresholdDto);
        return resultBean;
    }

    /**
     * 订阅接口
     * @param iotSubscribe
     * @return
     */
    @PostMapping(value = "/subscribe")
    public ResultBean<String> subscribe(@RequestBody IotSubscribe iotSubscribe){
        ResultBean<String> resultBean = this.iotSubscribeFeign.subscribe(iotSubscribe);
        return resultBean;
    }

    /**
     * 订阅接口查询
     * @param iotSubscribe
     * @return
     */
    @PostMapping(value = "/inquire")
    public ResultBean<String> inquire(@RequestBody IotSubscribe iotSubscribe){
        ResultBean<String> resultBean = this.iotSubscribeFeign.inquire(iotSubscribe);
        return resultBean;
    }

    /**
     * 功能描述:IOT物联网设备列表查询
     * @param iotDeviceDto IOT物联网设备业务对象（含查询条件）
     * @param page 分页条件
     * @return ResultBean<Page<IotDevice>>
     *
     *
     */
    @GetMapping(value = "/page")
    @Security(token = IotSecurityToken.IOT_DEVICE_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<IotDevice>> showIotDeviceList(IotDeviceDto iotDeviceDto, Page<IotDevice> page) {
        // 暂时拿全部的数据
//        iotDeviceDto.setOrgPosition("0/1/");
        iotDeviceDto.setPage(page);
        return this.iotDeviceFeign.pagingQuery(iotDeviceDto);
    }
}
