package com.haobang.syslog;

import com.common.entity.DeviceDevice;
import com.common.mapper.DeviceDeviceMapper;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import java.util.List;

/**
 * 达梦数据库连接测试 — 查询 device_device 表
 */
@SpringBootTest
@ActiveProfiles("dev")
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class DeviceDeviceTest {

    @Autowired
    private DeviceDeviceMapper deviceDeviceMapper;

    // ==================== 基础查询测试 ====================

    @Test
    @Order(1)
    @DisplayName("查询设备总数")
    void testCount() {
        System.out.println("\n========== 查询设备总数 ==========");
        Long count = deviceDeviceMapper.count();
        System.out.println("device_device 表数据总量: " + count);
        assert count != null && count >= 0;
    }

    @Test
    @Order(2)
    @DisplayName("查询所有设备")
    void testSelectAll() {
        System.out.println("\n========== 查询所有设备 ==========");
        List<DeviceDevice> list = deviceDeviceMapper.selectAll();
        System.out.println("查询到 " + list.size() + " 条记录：");
        for (int i = 0; i < Math.min(list.size(), 10); i++) {
            DeviceDevice d = list.get(i);
            System.out.printf("  [%d] id=%-6d name=%-20s ip=%-18s status=%-4s vendor=%-15s delFlag=%s%n",
                    i + 1, d.getId(), d.getName(), d.getIp(), d.getStatus(), d.getVendor(), d.getDelFlag());
        }
        if (list.size() > 10) {
            System.out.println("  ... 仅展示前10条，共 " + list.size() + " 条");
        }
        assert list != null;
    }

    @Test
    @Order(3)
    @DisplayName("查询未删除的有效设备 (del_flag='0')")
    void testSelectActiveDevices() {
        System.out.println("\n========== 查询有效设备 (del_flag='0') ==========");
        List<DeviceDevice> list = deviceDeviceMapper.selectActiveDevices();
        System.out.println("查询到 " + list.size() + " 条有效记录：");
        for (int i = 0; i < Math.min(list.size(), 10); i++) {
            DeviceDevice d = list.get(i);
            System.out.printf("  [%d] id=%-6d name=%-20s ip=%-18s status=%-4s vendor=%-15s%n",
                    i + 1, d.getId(), d.getName(), d.getIp(), d.getStatus(), d.getVendor());
        }
        if (list.size() > 10) {
            System.out.println("  ... 仅展示前10条，共 " + list.size() + " 条");
        }
        assert list != null;
    }

    @Test
    @Order(4)
    @DisplayName("分页查询 — 前5条")
    void testSelectByPage() {
        System.out.println("\n========== 分页查询 (第1页, 每页5条) ==========");
        List<DeviceDevice> list = deviceDeviceMapper.selectByPage(0, 5);
        System.out.println("查询到 " + list.size() + " 条记录：");
        for (DeviceDevice d : list) {
            System.out.println("  " + d);
        }
        assert list != null && list.size() <= 5;
    }

    // ==================== 精确查询测试 ====================

    @Test
    @Order(5)
    @DisplayName("根据ID查询设备")
    void testSelectById() {
        System.out.println("\n========== 根据ID查询 ==========");
        // 先查所有取第一条的ID
        List<DeviceDevice> all = deviceDeviceMapper.selectAll();
        if (all != null && !all.isEmpty()) {
            Integer firstId = all.get(0).getId();
            DeviceDevice d = deviceDeviceMapper.selectById(firstId);
            System.out.println("ID=" + firstId + " 查询结果：");
            System.out.println("  " + d);
            assert d != null && d.getId().equals(firstId);
        } else {
            System.out.println("device_device 表无数据，跳过按ID查询");
        }
    }

    // ==================== 分页查询测试 ====================

    @Test
    @Order(6)
    @DisplayName("分页查询 — 第2页")
    void testSelectByPage2() {
        System.out.println("\n========== 分页查询 (第2页, 每页5条) ==========");
        List<DeviceDevice> list = deviceDeviceMapper.selectByPage(5, 5);
        System.out.println("查询到 " + list.size() + " 条记录：");
        for (DeviceDevice d : list) {
            System.out.printf("  id=%-6d name=%-20s ip=%-18s%n", d.getId(), d.getName(), d.getIp());
        }
        assert list != null;
    }

    // ==================== 完整字段展示 ====================

    @Test
    @Order(7)
    @DisplayName("完整字段展示 — 查询1条设备的所有字段")
    void testShowFullFields() {
        System.out.println("\n========== 完整字段展示 ==========");
        List<DeviceDevice> all = deviceDeviceMapper.selectAll();
        if (all != null && !all.isEmpty()) {
            DeviceDevice d = all.get(0);
            System.out.println("┌─────────────────────────────────────────────┐");
            System.out.printf("│ %-12s: %-30s │%n", "id", d.getId());
            System.out.printf("│ %-12s: %-30s │%n", "name", d.getName());
            System.out.printf("│ %-12s: %-30s │%n", "ip", d.getIp());
            System.out.printf("│ %-12s: %-30s │%n", "vendor", d.getVendor());
            System.out.printf("│ %-12s: %-30s │%n", "productName", d.getProductName());
            System.out.printf("│ %-12s: %-30s │%n", "deviceGroup", d.getDeviceGroup());
            System.out.printf("│ %-12s: %-30s │%n", "deviceType", d.getDeviceType());
            System.out.printf("│ %-12s: %-30s │%n", "status", d.getStatus());
            System.out.printf("│ %-12s: %-30s │%n", "isMonitoring", d.getIsMonitoring());
            System.out.printf("│ %-12s: %-30s │%n", "organizationId", d.getOrganizationId());
            System.out.printf("│ %-12s: %-30s │%n", "agentId", d.getAgentId());
            System.out.printf("│ %-12s: %-30s │%n", "tenantId", d.getTenantId());
            System.out.printf("│ %-12s: %-30s │%n", "delFlag", d.getDelFlag());
            System.out.printf("│ %-12s: %-30s │%n", "managerName", d.getManagerName());
            System.out.printf("│ %-12s: %-30s │%n", "createdAt", d.getCreatedAt());
            System.out.printf("│ %-12s: %-30s │%n", "updatedAt", d.getUpdatedAt());
            System.out.printf("│ %-12s: %-30s │%n", "lastReceiveTime", d.getLastReceiveTime());
            System.out.printf("│ %-12s: %-30s │%n", "createTime", d.getCreateTime());
            System.out.printf("│ %-12s: %-30s │%n", "updateTime", d.getUpdateTime());
            System.out.printf("│ %-12s: %-30s │%n", "licenseStartTime", d.getLicenseStartTime());
            System.out.printf("│ %-12s: %-30s │%n", "licenseEndTime", d.getLicenseEndTime());
            System.out.printf("│ %-12s: %-30s │%n", "decodeType", d.getDecodeType());
            System.out.printf("│ %-12s: %-30s │%n", "missPolicy", d.getMissPolicy());
            System.out.printf("│ %-12s: %-30s │%n", "securityScopeId", d.getSecurityScopeId());
            System.out.printf("│ %-12s: %-30s │%n", "ownerId", d.getOwnerId());
            System.out.printf("│ %-12s: %-30s │%n", "sshConfigId", d.getSshConfigId());
            System.out.printf("│ %-12s: %-30s │%n", "createdById", d.getCreatedById());
            System.out.printf("│ %-12s: %-30s │%n", "createBy", d.getCreateBy());
            System.out.printf("│ %-12s: %-30s │%n", "updateBy", d.getUpdateBy());
            System.out.printf("│ %-12s: %-30s │%n", "createDept", d.getCreateDept());
            System.out.printf("│ %-12s: %-30s │%n", "deviceCollectId", d.getDeviceCollectId());
            System.out.printf("│ %-12s: %-30s │%n", "todayParseCount", d.getTodayParseCount());
            System.out.printf("│ %-12s: %-30s │%n", "todayNonLogCount", d.getTodayNonLogCount());
            System.out.printf("│ %-12s: %-30s │%n", "detailId", d.getDetailId());
            System.out.printf("│ %-12s: %-30s │%n", "controlAgentId", d.getControlAgentId());
            System.out.println("└─────────────────────────────────────────────┘");
        } else {
            System.out.println("device_device 表无数据");
        }
    }
}
