package com.aiotagro.cattletrade.business.controller;

import com.aiotagro.cattletrade.business.entity.IotDeviceData;
import com.aiotagro.cattletrade.business.mapper.IotDeviceDataMapper;
import com.aiotagro.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * IoT设备数据测试控制器
 *
 * @author System
 * @date 2025-01-16
 */
@RestController
@RequestMapping("/iotTest")
public class IotDeviceTestController {

    @Autowired
    private IotDeviceDataMapper iotDeviceDataMapper;

    /**
     * 直接查询数据库中的设备数据
     */
    @GetMapping("/count")
    public AjaxResult getDeviceCount() {
        try {
            Long count = iotDeviceDataMapper.selectCount(null);
            return AjaxResult.success("数据库中的设备数量: " + count);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 查询前5条设备数据
     */
    @GetMapping("/list")
    public AjaxResult getDeviceList() {
        try {
            List<IotDeviceData> devices = iotDeviceDataMapper.selectList(null);
            return AjaxResult.success(devices);
        } catch (Exception e) {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }

    /**
     * 检查定时任务状态
     */
    @GetMapping("/status")
    public AjaxResult getTaskStatus() {
        try {
            Long count = iotDeviceDataMapper.selectCount(null);
            return AjaxResult.success("定时任务状态正常，数据库中有 " + count + " 条设备数据");
        } catch (Exception e) {
            return AjaxResult.error("定时任务状态异常: " + e.getMessage());
        }
    }
}
