package com.rj.service;

/**
 * Author: 李中华  wx: spllzh  email(qq): 28668817@qq.com
 * Date: 2025/8/5 19:02
 **/

import com.rj.entity.DeviceManagement;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

//@SpringBootTest(classes = AISmartCard20250803Application.class)
class DeviceManagementServiceTest {

    @Autowired
    private IDeviceManagementService deviceManagementService;

    @Test
    void testSaveDeviceManagement() {
        // 创建测试数据
        DeviceManagement deviceManagement = new DeviceManagement();
        deviceManagement.setDeviceCode("DEVICE001");
        deviceManagement.setDealershipId("DEAL001");
        deviceManagement.setDealershipName("北京奥迪4S店");
        deviceManagement.setLastOnlineTime(LocalDateTime.now().minusHours(2));
        deviceManagement.setLastPowerOnTime(LocalDateTime.now().minusDays(1));
        deviceManagement.setLastPowerOffTime(LocalDateTime.now().minusHours(8));
        deviceManagement.setWifiName("Audi_WiFi_001");
        deviceManagement.setUnuploadedCount(5);
        deviceManagement.setStorageSize("64GB");
        deviceManagement.setStorageUsed("32GB");
        deviceManagement.setRemainingBattery("85%");
        deviceManagement.setChargingStatus("充电中");
        deviceManagement.setBindUserId("USER001");
        deviceManagement.setBindUserName("张三");
        deviceManagement.setProjectId("PROJ001");
        deviceManagement.setProjectName("奥迪A6L项目");
        deviceManagement.setBindStatus(true);
        deviceManagement.setCreateTime(LocalDateTime.now());
        deviceManagement.setUpdateTime(LocalDateTime.now());

        // 测试保存功能
        boolean result = deviceManagementService.save(deviceManagement);

        // 验证保存成功
        assertTrue(result);
        assertNotNull(deviceManagement.getId());
    }

    @Test
    void testGetDeviceManagementById() {
        // 先保存一条测试数据
        DeviceManagement deviceManagement = new DeviceManagement();
        deviceManagement.setDeviceCode("DEVICE002");
        deviceManagement.setDealershipId("DEAL002");
        deviceManagement.setDealershipName("上海宝马4S店");
        deviceManagement.setLastOnlineTime(LocalDateTime.now().minusHours(1));
        deviceManagement.setLastPowerOnTime(LocalDateTime.now().minusDays(2));
        deviceManagement.setLastPowerOffTime(LocalDateTime.now().minusHours(12));
        deviceManagement.setWifiName("BMW_WiFi_001");
        deviceManagement.setUnuploadedCount(3);
        deviceManagement.setStorageSize("128GB");
        deviceManagement.setStorageUsed("64GB");
        deviceManagement.setRemainingBattery("60%");
        deviceManagement.setChargingStatus("未充电");
        deviceManagement.setBindUserId("USER002");
        deviceManagement.setBindUserName("李四");
        deviceManagement.setProjectId("PROJ002");
        deviceManagement.setProjectName("宝马5系项目");
        deviceManagement.setBindStatus(true);
        deviceManagement.setCreateTime(LocalDateTime.now());
        deviceManagement.setUpdateTime(LocalDateTime.now());
        
        deviceManagementService.save(deviceManagement);

        // 测试根据ID查询功能
        DeviceManagement foundDevice = deviceManagementService.getById(deviceManagement.getId());

        // 验证查询结果
        assertNotNull(foundDevice);
        assertEquals("DEVICE002", foundDevice.getDeviceCode());
        assertEquals("上海宝马4S店", foundDevice.getDealershipName());
        assertEquals("李四", foundDevice.getBindUserName());
        assertEquals("60%", foundDevice.getRemainingBattery());
    }

    @Test
    void testGetDeviceManagementByCode() {
        // 先保存一条测试数据
        DeviceManagement deviceManagement = new DeviceManagement();
        deviceManagement.setDeviceCode("DEVICE003");
        deviceManagement.setDealershipId("DEAL003");
        deviceManagement.setDealershipName("广州奔驰4S店");
        deviceManagement.setLastOnlineTime(LocalDateTime.now().minusMinutes(30));
        deviceManagement.setLastPowerOnTime(LocalDateTime.now().minusDays(1));
        deviceManagement.setLastPowerOffTime(LocalDateTime.now().minusHours(6));
        deviceManagement.setWifiName("BENZ_WiFi_001");
        deviceManagement.setUnuploadedCount(8);
        deviceManagement.setStorageSize("256GB");
        deviceManagement.setStorageUsed("128GB");
        deviceManagement.setRemainingBattery("45%");
        deviceManagement.setChargingStatus("充电中");
        deviceManagement.setBindUserId("USER003");
        deviceManagement.setBindUserName("王五");
        deviceManagement.setProjectId("PROJ003");
        deviceManagement.setProjectName("奔驰E级项目");
        deviceManagement.setBindStatus(false);
        deviceManagement.setCreateTime(LocalDateTime.now());
        deviceManagement.setUpdateTime(LocalDateTime.now());
        
        deviceManagementService.save(deviceManagement);

        // 测试根据设备编号查询功能
        List<DeviceManagement> deviceList = deviceManagementService.lambdaQuery()
                .eq(DeviceManagement::getDeviceCode, "DEVICE003")
                .list();

        // 验证查询结果
        assertNotNull(deviceList);
        assertFalse(deviceList.isEmpty());
        assertEquals("DEVICE003", deviceList.get(0).getDeviceCode());
        assertEquals("广州奔驰4S店", deviceList.get(0).getDealershipName());
        assertEquals("王五", deviceList.get(0).getBindUserName());
    }

    @Test
    void testGetDeviceManagementByCodeWithNoResults() {
        // 测试查询不存在的设备编号
        List<DeviceManagement> deviceList = deviceManagementService.lambdaQuery()
                .eq(DeviceManagement::getDeviceCode, "NOT_EXIST_001")
                .list();

        // 验证查询结果为空
        assertNotNull(deviceList);
        assertTrue(deviceList.isEmpty());
    }

    @Test
    void testUpdateDeviceManagement() {
        // 先保存一条测试数据
        DeviceManagement deviceManagement = new DeviceManagement();
        deviceManagement.setDeviceCode("DEVICE004");
        deviceManagement.setDealershipId("DEAL004");
        deviceManagement.setDealershipName("深圳雷克萨斯4S店");
        deviceManagement.setLastOnlineTime(LocalDateTime.now().minusHours(3));
        deviceManagement.setLastPowerOnTime(LocalDateTime.now().minusDays(1));
        deviceManagement.setLastPowerOffTime(LocalDateTime.now().minusHours(10));
        deviceManagement.setWifiName("LEXUS_WiFi_001");
        deviceManagement.setUnuploadedCount(2);
        deviceManagement.setStorageSize("512GB");
        deviceManagement.setStorageUsed("256GB");
        deviceManagement.setRemainingBattery("90%");
        deviceManagement.setChargingStatus("未充电");
        deviceManagement.setBindUserId("USER004");
        deviceManagement.setBindUserName("赵六");
        deviceManagement.setProjectId("PROJ004");
        deviceManagement.setProjectName("雷克萨斯ES项目");
        deviceManagement.setBindStatus(true);
        deviceManagement.setCreateTime(LocalDateTime.now());
        deviceManagement.setUpdateTime(LocalDateTime.now());
        
        deviceManagementService.save(deviceManagement);

        // 更新设备信息
        deviceManagement.setUnuploadedCount(0);
        deviceManagement.setRemainingBattery("95%");
        deviceManagement.setChargingStatus("充电中");
        deviceManagement.setUpdateTime(LocalDateTime.now());

        // 测试更新功能
        boolean result = deviceManagementService.updateById(deviceManagement);

        // 验证更新成功
        assertTrue(result);

        // 重新查询验证更新结果
        DeviceManagement updatedDevice = deviceManagementService.getById(deviceManagement.getId());
        assertEquals(0, updatedDevice.getUnuploadedCount());
        assertEquals("95%", updatedDevice.getRemainingBattery());
        assertEquals("充电中", updatedDevice.getChargingStatus());
    }

    @Test
    void testDeleteDeviceManagement() {
        // 先保存一条测试数据
        DeviceManagement deviceManagement = new DeviceManagement();
        deviceManagement.setDeviceCode("DEVICE005");
        deviceManagement.setDealershipId("DEAL005");
        deviceManagement.setDealershipName("杭州沃尔沃4S店");
        deviceManagement.setLastOnlineTime(LocalDateTime.now().minusHours(4));
        deviceManagement.setLastPowerOnTime(LocalDateTime.now().minusDays(2));
        deviceManagement.setLastPowerOffTime(LocalDateTime.now().minusHours(14));
        deviceManagement.setWifiName("VOLVO_WiFi_001");
        deviceManagement.setUnuploadedCount(1);
        deviceManagement.setStorageSize("1TB");
        deviceManagement.setStorageUsed("512GB");
        deviceManagement.setRemainingBattery("30%");
        deviceManagement.setChargingStatus("充电中");
        deviceManagement.setBindUserId("USER005");
        deviceManagement.setBindUserName("孙七");
        deviceManagement.setProjectId("PROJ005");
        deviceManagement.setProjectName("沃尔沃S90项目");
        deviceManagement.setBindStatus(true);
        deviceManagement.setCreateTime(LocalDateTime.now());
        deviceManagement.setUpdateTime(LocalDateTime.now());
        
        deviceManagementService.save(deviceManagement);
        String deviceId = deviceManagement.getId();

        // 测试删除功能
        boolean result = deviceManagementService.removeById(deviceId);

        // 验证删除成功
        assertTrue(result);

        // 验证数据已被删除
        DeviceManagement deletedDevice = deviceManagementService.getById(deviceId);
        assertNull(deletedDevice);
    }

    @Test
    void testBatchInsertDeviceManagement() {
        // 创建30条测试数据
        for (int i = 1; i <= 30; i++) {
            DeviceManagement deviceManagement = new DeviceManagement();
            deviceManagement.setDeviceCode("DEVICE" + String.format("%03d", i));
            deviceManagement.setDealershipId("DEAL" + String.format("%03d", i));
            deviceManagement.setDealershipName("4S店" + i);
            deviceManagement.setLastOnlineTime(LocalDateTime.now().minusHours(i));
            deviceManagement.setLastPowerOnTime(LocalDateTime.now().minusDays(i % 7 + 1));
            deviceManagement.setLastPowerOffTime(LocalDateTime.now().minusHours(i * 2));
            deviceManagement.setWifiName("WiFi_" + String.format("%03d", i));
            deviceManagement.setUnuploadedCount(i % 10); // 0-9个未上传
            deviceManagement.setStorageSize(getRandomStorageSize());
            deviceManagement.setStorageUsed(getRandomStorageUsed());
            deviceManagement.setRemainingBattery(getRandomBattery());
            deviceManagement.setChargingStatus(getRandomChargingStatus());
            deviceManagement.setBindUserId("USER" + String.format("%03d", i));
            deviceManagement.setBindUserName("用户" + i);
            deviceManagement.setProjectId("PROJ" + String.format("%03d", i));
            deviceManagement.setProjectName("项目" + i);
            deviceManagement.setBindStatus(i % 3 != 0); // 每3个中1个未绑定
            deviceManagement.setCreateTime(LocalDateTime.now().minusDays(i));
            deviceManagement.setUpdateTime(LocalDateTime.now().minusHours(i));

            // 保存数据
            boolean result = deviceManagementService.save(deviceManagement);
            assertTrue(result, "第" + i + "条数据保存失败");
            assertNotNull(deviceManagement.getId(), "第" + i + "条数据ID为空");
        }

        // 验证总共插入了30条数据
        long count = deviceManagementService.count();
        assertTrue(count >= 30, "插入的数据数量不足30条，实际数量：" + count);
        
        System.out.println("成功插入30条测试数据，当前数据库总记录数：" + count);
    }

    /**
     * 获取随机的存储大小
     */
    private String getRandomStorageSize() {
        String[] sizes = {"64GB", "128GB", "256GB", "512GB", "1TB", "2TB"};
        return sizes[(int) (Math.random() * sizes.length)];
    }

    /**
     * 获取随机的已用存储
     */
    private String getRandomStorageUsed() {
        String[] used = {"16GB", "32GB", "64GB", "128GB", "256GB", "512GB"};
        return used[(int) (Math.random() * used.length)];
    }

    /**
     * 获取随机的电池电量
     */
    private String getRandomBattery() {
        String[] batteries = {"20%", "35%", "50%", "65%", "80%", "95%", "100%"};
        return batteries[(int) (Math.random() * batteries.length)];
    }

    /**
     * 获取随机的充电状态
     */
    private String getRandomChargingStatus() {
        String[] statuses = {"充电中", "未充电", "充满电", "低电量"};
        return statuses[(int) (Math.random() * statuses.length)];
    }
} 