package com.sptg.gateway.controller.iot;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.iot.api.beans.ApiResultBean;
import com.sptg.iot.api.dto.AlarmNoticeDto;
import com.sptg.iot.api.dto.DeviceStatusNoticeDto;
import com.sptg.iot.api.dto.HeartbeatNoticeDto;
import com.sptg.iot.api.entity.IotDevice;
import com.sptg.iot.api.feigns.IotAlarmFeign;
import com.sptg.iot.api.feigns.IotApiLogFeign;
import com.sptg.iot.api.feigns.IotDeviceFeign;
import com.sptg.third.api.feigns.grid.GridIotFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zwx
 * @create 2020/10/22 23:03
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class IotSubscribeNoticeController {

    private final IotDeviceFeign iotDeviceFeign;
    private final IotAlarmFeign iotAlarmFeign;
    private final IotApiLogFeign iotApiLogFeign;
    private final GridIotFeign gridIotFeign;

    /**
     * 告警上报接口
     * @param alarmNoticeDtoList
     * @return
     */
    @PostMapping(value = "/alarm/notice")
    public ApiResultBean alarmNotice(@RequestBody List<AlarmNoticeDto> alarmNoticeDtoList){
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "成功");
        map.put("apiMethod", "alarm/notice");
        String reqData = JsonUtils.objectToJson(alarmNoticeDtoList);
        map.put("reqData", reqData);
        this.iotApiLogFeign.recordIotApiLog(map);
        this.iotAlarmFeign.alarmNotice(alarmNoticeDtoList);
        this.gridIotFeign.gridIotAlarmPush(alarmNoticeDtoList);


        return ApiResultBean.ok();
    }

    /**
     * 心跳上报接口
     * 每一次进来的集合，都是同一个设备的心跳数据
     * 遍历集合，可获得关于该设备的所有数据字段
     * @param heartbeatDtoList
     * @return
     */
    @PostMapping(value = "/heartbeat/notice")
    public ApiResultBean heartbeatNotice(@RequestBody List<HeartbeatNoticeDto> heartbeatDtoList){
        ResultBean<IotDevice> resultBean = this.iotDeviceFeign.deviceHeartbeatNotice(heartbeatDtoList);
        IotDevice iotDevice = resultBean.getData();
        if(iotDevice != null){
            this.gridIotFeign.gridIotDeviceHeartbeatPush(iotDevice);
        }
        return ApiResultBean.ok();
    }

    /**
     * 设备状态更新上报接口
     * @param deviceStatusDto
     * @return
     */
    @PostMapping(value = "/deviceStatus/notice")
    public ApiResultBean deviceStatusNotice(@RequestBody DeviceStatusNoticeDto deviceStatusDto){
        ResultBean<IotDevice> resultBean = this.iotDeviceFeign.deviceStatusNotice(deviceStatusDto);
        IotDevice iotDevice = resultBean.getData();
        if(iotDevice != null){
            this.gridIotFeign.gridIotDeviceStatusPush(iotDevice);
        }
        return ApiResultBean.ok();
    }
}
