package com.sptg.device.biz.controller;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.device.api.entity.Device;
import com.sptg.device.api.entity.DeviceLog;
import com.sptg.device.api.feigns.DeviceLogFeign;
import com.sptg.device.biz.service.DeviceLogServiceInterface;
import com.sptg.device.biz.service.DeviceServiceInterface;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * title: DeviceLogController
 * package: com.sptg.device.biz.controller
 * description: 设备日志控制器
 *
 *                  
 *               
 */
@RestController
@RequiredArgsConstructor
public class DeviceLogController implements DeviceLogFeign {

    private final DeviceLogServiceInterface deviceLogServiceInterface;

    private final DeviceServiceInterface deviceServiceInterface;

    @Override
    @PostMapping(value = "/device/log/add")
    public ResultBean<Boolean> add(@RequestBody DeviceLog log) {
        Device dbDevice = this.deviceServiceInterface.selectBySerialNumber(log.getSerialNumber());
        Assert.notNull(dbDevice,"设备序列号不存在");
        log.setLogTime(new Date());
        log.setDeviceId(dbDevice.getId());
        boolean result = this.deviceLogServiceInterface.insert(log);
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }
}
