package com.sptg.iot.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.iot.api.beans.ApiResultBean;
import com.sptg.iot.api.dto.DeviceStatusNoticeDto;
import com.sptg.iot.api.dto.HeartbeatNoticeDto;
import com.sptg.iot.api.dto.IotDeviceDto;
import com.sptg.iot.api.entity.IotDevice;
import com.sptg.iot.api.feigns.IotDeviceFeign;
import com.sptg.iot.biz.service.IotDeviceServiceInterface;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author zwx
 * @create 2020/10/22 21:07
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class IotDeviceController implements IotDeviceFeign {

    private final IotDeviceServiceInterface iotDeviceServiceInterface;

    @Override
    @PostMapping(value = "/iot/device/list")
    public ResultBean<Page<IotDevice>> showIotDeviceList(@RequestBody IotDeviceDto iotDeviceDto) {
        ResultBean<Page<IotDevice>> pageResultBean = this.iotDeviceServiceInterface.showIotDeviceList(iotDeviceDto);
        return pageResultBean;
    }

    @Override
    @PostMapping(value = "/iot/device/pagingQuery")
    public ResultBean<Page<IotDevice>> pagingQuery(@RequestBody IotDeviceDto iotDeviceDto) {
        return this.iotDeviceServiceInterface.pagingQuery(iotDeviceDto);
    }

    /**
     * 物联网设备注册
     * @param iotDeviceDto
     * @return
     */
    @Override
    @PostMapping(value = "/iot/device/register")
    public ResultBean<String> iotDeviceRegister(@RequestBody IotDeviceDto iotDeviceDto) {
        ResultBean<String> resultBean = this.iotDeviceServiceInterface.iotDeviceRegister(iotDeviceDto);
        return resultBean;
    }

    @Override
    @PostMapping(value = "/iot/device/sync/list")
    public ResultBean<String> syncIotDeviceList(@RequestBody IotDeviceDto iotDeviceDto) {
        ResultBean<String> resultBean = this.iotDeviceServiceInterface.syncIotDeviceList(iotDeviceDto);
        return resultBean;
    }

    @Override
    @PostMapping(value = "/iot/device/sync/listByPrefix")
    public ResultBean<String> syncIotDeviceListByPrefix(@RequestBody IotDeviceDto iotDeviceDto) {
        List<String> prefixList = iotDeviceDto.getPrefixList();
        ResultBean<String> resultBean = this.iotDeviceServiceInterface.syncIotDeviceListByPrefix(prefixList);
        return resultBean;
    }

    /**
     * 基于存量的设备
     * @param iotDeviceDto
     * @return
     */
    @Override
    @PostMapping(value = "/iot/device/sync/detail/one")
    public ResultBean<String> syncIotDeviceDetailOne(@RequestBody IotDeviceDto iotDeviceDto) {
        ResultBean<String> resultBean = this.iotDeviceServiceInterface.syncIotDeviceDetailOne(iotDeviceDto);
        return resultBean;
    }

    @Override
    @PostMapping(value = "/iot/device/sync/detail/batch")
    public ResultBean<String> syncIotDeviceDetailBatch(@RequestBody List<IotDeviceDto> iotDeviceDtoList) {
        ResultBean<String> resultBean = this.iotDeviceServiceInterface.syncIotDeviceDetailBatch(iotDeviceDtoList);
        return resultBean;
    }

    @Override
    @PostMapping(value = "/iot/device/sync/detail/all")
    public ResultBean<String> syncIotDeviceDetailAll(@RequestBody IotDeviceDto iotDeviceDto) {
        ResultBean<String> resultBean = this.iotDeviceServiceInterface.syncIotDeviceDetailAll(iotDeviceDto);
        return resultBean;
    }

    @Override
    @GetMapping(value = "/iot/device/getIotDeviceById/{id}")
    public IotDevice getIotDeviceById(@PathVariable(value = "id") Long id) {
        return this.iotDeviceServiceInterface.getIotDeviceById(id);
    }

    @Override
    @GetMapping(value = "/iot/device/getByDeviceId/{device_id}")
    public IotDevice getIotDeviceByDeviceId(@PathVariable(value = "device_id") String device_id) {
        return this.iotDeviceServiceInterface.getIotDeviceByDeviceId(device_id);
    }

    @Override
    @PostMapping(value = "/iot/device/updateStatus")
    public ResultBean<String> updateIotDeviceStatus(@RequestBody IotDevice iotDevice) {
        ResultBean<String> resultBean = this.iotDeviceServiceInterface.deviceStatusChange(iotDevice);
        return resultBean;
    }

    @Override
    @PostMapping(value = "/iot/device/update")
    public ResultBean<String> update(@RequestBody IotDevice iotDevice) {
        boolean b = this.iotDeviceServiceInterface.updateById(iotDevice);
        if(b){
            return new ResultBean<>(ResultEnum.SUCCESS, "更新成功");
        }
        return new ResultBean<>(ResultEnum.ERROR, "更新失败");
    }

    @Override
    @PostMapping(value = "/iot/device/heartbeat/notice")
    public ResultBean<IotDevice> deviceHeartbeatNotice(@RequestBody List<HeartbeatNoticeDto> heartbeatDtoList) {
        ResultBean<IotDevice> resultBean = this.iotDeviceServiceInterface.deviceHeartbeatNotice(heartbeatDtoList);
        return resultBean;
    }

    @Override
    @PostMapping(value = "/iot/device/status/notice")
    public ResultBean<IotDevice> deviceStatusNotice(@RequestBody DeviceStatusNoticeDto deviceStatusDto) {
        ResultBean<IotDevice> resultBean = this.iotDeviceServiceInterface.deviceStatusNotice(deviceStatusDto);
        return resultBean;
    }

    @Override
    @GetMapping(value = "/iot/device/getList")
    public ResultBean<List<IotDevice>> getList() {
        List<IotDevice> list = this.iotDeviceServiceInterface.getList();
        return new ResultBean<List<IotDevice>>(ResultEnum.SUCCESS, list);
    }

    @Override
    @PostMapping(value = "/iot/device/updateBatch")
    public ResultBean<String> updateBatch(@RequestBody List<IotDevice> iotDeviceList) {
        boolean b = this.iotDeviceServiceInterface.updateBatchById(iotDeviceList);
        if(b){
            return new ResultBean<>(ResultEnum.SUCCESS, "物联网设备更新成功");
        }else{
            return new ResultBean<>(ResultEnum.FAILURE, "物联网设备更新失败");
        }
    }

    @Override
    @PostMapping(value = "/iot/device/insertOrUpdateBatch")
    public ResultBean<String> insertOrUpdateBatch(@RequestBody List<IotDevice> iotDeviceList) {
        boolean b = this.iotDeviceServiceInterface.insertOrUpdateBatch(iotDeviceList);
        if(b){
            return new ResultBean<>(ResultEnum.SUCCESS, "物联网设备添加/更新成功");
        }else{
            return new ResultBean<>(ResultEnum.FAILURE, "物联网设备添加/更新失败");
        }
    }
}
