package com.rj.service;

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

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

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

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

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

    @Autowired
    private ISalesManagementService salesManagementService;

    @Test
    void testSaveSalesManagement() {
        // 创建测试数据
        SalesManagement salesManagement = new SalesManagement();
        salesManagement.setSalesName("张三");
        salesManagement.setLoginAccount("zhangsan001");
        salesManagement.setRecordingCount(25);
        salesManagement.setAvgRecordingDuration(new BigDecimal("18.5"));
        salesManagement.setTotalRecordingHours(new BigDecimal("7.5"));
        salesManagement.setProjectId("PROJ001");
        salesManagement.setProjectName("奥迪A6L项目");
        salesManagement.setDealershipId("DEAL001");
        salesManagement.setDealershipName("北京奥迪4S店");
        salesManagement.setRole("销售顾问");
        salesManagement.setCreateTime(LocalDateTime.now());
        salesManagement.setLastRecordingTime(LocalDateTime.now().minusHours(2));
        salesManagement.setLastLoginTime(LocalDateTime.now().minusHours(1));
        salesManagement.setLastLoginIp("192.168.1.100");
        salesManagement.setStatus(true);
        salesManagement.setUpdateTime(LocalDateTime.now());

        // 测试保存功能
        boolean result = salesManagementService.save(salesManagement);

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

    @Test
    void testGetSalesManagementById() {
        // 先保存一条测试数据
        SalesManagement salesManagement = new SalesManagement();
        salesManagement.setSalesName("李四");
        salesManagement.setLoginAccount("lisi002");
        salesManagement.setRecordingCount(30);
        salesManagement.setAvgRecordingDuration(new BigDecimal("22.0"));
        salesManagement.setTotalRecordingHours(new BigDecimal("11.0"));
        salesManagement.setProjectId("PROJ002");
        salesManagement.setProjectName("宝马5系项目");
        salesManagement.setDealershipId("DEAL002");
        salesManagement.setDealershipName("上海宝马4S店");
        salesManagement.setRole("高级销售顾问");
        salesManagement.setCreateTime(LocalDateTime.now());
        salesManagement.setLastRecordingTime(LocalDateTime.now().minusHours(3));
        salesManagement.setLastLoginTime(LocalDateTime.now().minusHours(2));
        salesManagement.setLastLoginIp("192.168.1.101");
        salesManagement.setStatus(true);
        salesManagement.setUpdateTime(LocalDateTime.now());
        
        salesManagementService.save(salesManagement);

        // 测试根据ID查询功能
        SalesManagement foundSales = salesManagementService.getById(salesManagement.getId());

        // 验证查询结果
        assertNotNull(foundSales);
        assertEquals("李四", foundSales.getSalesName());
        assertEquals("lisi002", foundSales.getLoginAccount());
        assertEquals(30, foundSales.getRecordingCount());
        assertEquals("高级销售顾问", foundSales.getRole());
    }

    @Test
    void testGetSalesManagementByAccount() {
        // 先保存一条测试数据
        SalesManagement salesManagement = new SalesManagement();
        salesManagement.setSalesName("王五");
        salesManagement.setLoginAccount("wangwu003");
        salesManagement.setRecordingCount(18);
        salesManagement.setAvgRecordingDuration(new BigDecimal("16.5"));
        salesManagement.setTotalRecordingHours(new BigDecimal("5.0"));
        salesManagement.setProjectId("PROJ003");
        salesManagement.setProjectName("奔驰E级项目");
        salesManagement.setDealershipId("DEAL003");
        salesManagement.setDealershipName("广州奔驰4S店");
        salesManagement.setRole("销售经理");
        salesManagement.setCreateTime(LocalDateTime.now());
        salesManagement.setLastRecordingTime(LocalDateTime.now().minusHours(4));
        salesManagement.setLastLoginTime(LocalDateTime.now().minusHours(3));
        salesManagement.setLastLoginIp("192.168.1.102");
        salesManagement.setStatus(true);
        salesManagement.setUpdateTime(LocalDateTime.now());
        
        salesManagementService.save(salesManagement);

        // 测试根据登录账号查询功能
        List<SalesManagement> salesList = salesManagementService.lambdaQuery()
                .eq(SalesManagement::getLoginAccount, "wangwu003")
                .list();

        // 验证查询结果
        assertNotNull(salesList);
        assertFalse(salesList.isEmpty());
        assertEquals("王五", salesList.get(0).getSalesName());
        assertEquals("wangwu003", salesList.get(0).getLoginAccount());
        assertEquals("销售经理", salesList.get(0).getRole());
    }

    @Test
    void testGetSalesManagementByAccountWithNoResults() {
        // 测试查询不存在的登录账号
        List<SalesManagement> salesList = salesManagementService.lambdaQuery()
                .eq(SalesManagement::getLoginAccount, "notexist001")
                .list();

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

    @Test
    void testUpdateSalesManagement() {
        // 先保存一条测试数据
        SalesManagement salesManagement = new SalesManagement();
        salesManagement.setSalesName("赵六");
        salesManagement.setLoginAccount("zhaoliu004");
        salesManagement.setRecordingCount(22);
        salesManagement.setAvgRecordingDuration(new BigDecimal("19.5"));
        salesManagement.setTotalRecordingHours(new BigDecimal("7.0"));
        salesManagement.setProjectId("PROJ004");
        salesManagement.setProjectName("雷克萨斯ES项目");
        salesManagement.setDealershipId("DEAL004");
        salesManagement.setDealershipName("深圳雷克萨斯4S店");
        salesManagement.setRole("销售顾问");
        salesManagement.setCreateTime(LocalDateTime.now());
        salesManagement.setLastRecordingTime(LocalDateTime.now().minusHours(5));
        salesManagement.setLastLoginTime(LocalDateTime.now().minusHours(4));
        salesManagement.setLastLoginIp("192.168.1.103");
        salesManagement.setStatus(true);
        salesManagement.setUpdateTime(LocalDateTime.now());
        
        salesManagementService.save(salesManagement);

        // 更新销售信息
        salesManagement.setRecordingCount(28);
        salesManagement.setAvgRecordingDuration(new BigDecimal("21.0"));
        salesManagement.setTotalRecordingHours(new BigDecimal("9.8"));
        salesManagement.setRole("高级销售顾问");
        salesManagement.setUpdateTime(LocalDateTime.now());

        // 测试更新功能
        boolean result = salesManagementService.updateById(salesManagement);

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

        // 重新查询验证更新结果
        SalesManagement updatedSales = salesManagementService.getById(salesManagement.getId());
        assertEquals(28, updatedSales.getRecordingCount());
        assertEquals(new BigDecimal("21.0"), updatedSales.getAvgRecordingDuration());
        assertEquals(new BigDecimal("9.8"), updatedSales.getTotalRecordingHours());
        assertEquals("高级销售顾问", updatedSales.getRole());
    }

    @Test
    void testDeleteSalesManagement() {
        // 先保存一条测试数据
        SalesManagement salesManagement = new SalesManagement();
        salesManagement.setSalesName("孙七");
        salesManagement.setLoginAccount("sunqi005");
        salesManagement.setRecordingCount(15);
        salesManagement.setAvgRecordingDuration(new BigDecimal("15.0"));
        salesManagement.setTotalRecordingHours(new BigDecimal("3.8"));
        salesManagement.setProjectId("PROJ005");
        salesManagement.setProjectName("沃尔沃S90项目");
        salesManagement.setDealershipId("DEAL005");
        salesManagement.setDealershipName("杭州沃尔沃4S店");
        salesManagement.setRole("销售顾问");
        salesManagement.setCreateTime(LocalDateTime.now());
        salesManagement.setLastRecordingTime(LocalDateTime.now().minusHours(6));
        salesManagement.setLastLoginTime(LocalDateTime.now().minusHours(5));
        salesManagement.setLastLoginIp("192.168.1.104");
        salesManagement.setStatus(true);
        salesManagement.setUpdateTime(LocalDateTime.now());
        
        salesManagementService.save(salesManagement);
        String salesId = salesManagement.getId();

        // 测试删除功能
        boolean result = salesManagementService.removeById(salesId);

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

        // 验证数据已被删除
        SalesManagement deletedSales = salesManagementService.getById(salesId);
        assertNull(deletedSales);
    }

    @Test
    void testBatchInsertSalesManagement() {
        // 创建30条测试数据
        for (int i = 1; i <= 30; i++) {
            SalesManagement salesManagement = new SalesManagement();
            salesManagement.setSalesName("销售员" + i % 5);
            salesManagement.setLoginAccount("sales" + String.format("%03d", i));
            salesManagement.setRecordingCount(10 + (i % 25)); // 10-34条录音
            salesManagement.setAvgRecordingDuration(new BigDecimal(String.valueOf(12 + i * 0.3)));
            salesManagement.setTotalRecordingHours(new BigDecimal(String.valueOf(2 + i * 0.2)));
            salesManagement.setProjectId("PROJ" + String.format("%03d", i));
            salesManagement.setProjectName("项目" + i);
            salesManagement.setDealershipId("DEAL" + String.format("%03d", i));
            salesManagement.setDealershipName("4S店" + i%5);
            salesManagement.setRole(getRandomRole());
            salesManagement.setCreateTime(LocalDateTime.now().minusDays(i));
            salesManagement.setLastRecordingTime(LocalDateTime.now().minusHours(i));
            salesManagement.setLastLoginTime(LocalDateTime.now().minusHours(i * 2));
            salesManagement.setLastLoginIp("192.168.1." + (100 + i));
            salesManagement.setStatus(i % 5 != 0); // 每5个中停用1个
            salesManagement.setUpdateTime(LocalDateTime.now().minusHours(i));

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

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

    /**
     * 获取随机的销售角色
     */
    private String getRandomRole() {
        String[] roles = {"销售顾问", "高级销售顾问", "销售经理", "销售总监", "销售主管"};
        return roles[(int) (Math.random() * roles.length)];
    }
} 