package org.dromara.rfid.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.rfid.domain.RfidDevice;
import org.dromara.rfid.domain.vo.RfidDashboardStatsVo;
import org.dromara.rfid.service.IRfidDeviceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * RFID 首页监控 Controller
 *
 * @author zch
 * @date 2025-11-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/rfid/dashboard")
public class RfidDashboardController {

    private final IRfidDeviceService rfidDeviceService;

    /** 在线状态：在线 */
    private static final String ONLINE_STATUS_ONLINE = "1";

    /** 在线状态：离线 */
    private static final String ONLINE_STATUS_OFFLINE = "0";

    /** 告警状态：告警 */
    private static final String ALARM_STATUS_ALARM = "1";

    /**
     * 获取首页统计数据
     */
    @GetMapping("/stats")
    public R<RfidDashboardStatsVo> getStats() {
        RfidDashboardStatsVo stats = new RfidDashboardStatsVo();

        // 设备总数
        stats.setTotalDevices(rfidDeviceService.count());

        // 在线数量 (online_status = 1)
        stats.setOnlineDevices(rfidDeviceService.count(
            Wrappers.<RfidDevice>lambdaQuery().eq(RfidDevice::getOnlineStatus, ONLINE_STATUS_ONLINE)//在线状态(0-离线;1-在线)
        ));

        // 离线数量 (online_status = 0)
        stats.setOfflineDevices(rfidDeviceService.count(
            Wrappers.<RfidDevice>lambdaQuery().eq(RfidDevice::getOnlineStatus, ONLINE_STATUS_OFFLINE)//在线状态(0-离线;1-在线)
        ));

        // 告警数量 (alarm_status = 1)
        stats.setAlarmDevices(rfidDeviceService.count(
            Wrappers.<RfidDevice>lambdaQuery().eq(RfidDevice::getAlarmStatus, ALARM_STATUS_ALARM)//告警状态(0-正常;1-告警)
        ));

        return R.ok(stats);
    }
}
