package com.rj.scheduler;

import com.rj.entity.CustomerProfileAnalysis;
import com.rj.service.biz.ICustomerProfileAnalysisService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

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

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

/**
 * CustomerProfileStatisticScheduler 集成测试
 * 测试真实数据库操作
 * 
 * @author 李中华
 * @date 2025/1/3
 */
@SpringBootTest
@ActiveProfiles("test")
class CustomerProfileStatisticSchedulerIntegrationTest {

    @Autowired
    private CustomerProfileMockInsertDataScheduler scheduler;

    @Autowired
    private ICustomerProfileAnalysisService customerProfileAnalysisService;

    @Test
    void testRealDatabaseOperation() {
        // 准备测试数据
        LocalDateTime testDate = LocalDateTime.of(2025, 7, 8, 10, 0, 0);
        
        // 执行数据生成
        int count = scheduler.generateCustomerProfileDataForDate(testDate);
        
        // 验证结果
        assertTrue(count > 0, "应该生成至少一条记录");
        assertTrue(count <= 50, "生成记录数不应超过50条");
        
        // 验证数据是否保存到数据库
        List<CustomerProfileAnalysis> savedRecords = customerProfileAnalysisService.list();
        assertFalse(savedRecords.isEmpty(), "数据库中应该有保存的记录");
        
        // 验证最新生成的记录
        CustomerProfileAnalysis latestRecord = savedRecords.get(savedRecords.size() - 1);
        assertNotNull(latestRecord.getId(), "记录ID不应为空");
        assertNotNull(latestRecord.getClientName(), "客户姓名不应为空");
        assertNotNull(latestRecord.getDealerName(), "经销商门店名称不应为空");
        assertNotNull(latestRecord.getProjectName(), "项目名称不应为空");
        assertNotNull(latestRecord.getSalesPersonName(), "销售顾问姓名不应为空");
        assertNotNull(latestRecord.getNotes(), "备注不应为空");
        
        // 验证情感分析字段
        assertNotNull(latestRecord.getEntityBrand(), "品牌情感不应为空");
        assertNotNull(latestRecord.getEntityPrice(), "价格情感不应为空");
        assertNotNull(latestRecord.getEntityPower(), "动力情感不应为空");
        
        System.out.println("成功生成并保存了 " + count + " 条记录"+" , 累计总数记录数： "+savedRecords.size());
        System.out.println("最新记录详情：");
        System.out.println("客户姓名: " + latestRecord.getClientName());
        System.out.println("门店: " + latestRecord.getDealerName());
        System.out.println("项目: " + latestRecord.getProjectName());
        System.out.println("销售顾问: " + latestRecord.getSalesPersonName());
        System.out.println("备注: " + latestRecord.getNotes());
    }

    @Test
    void testManualGenerateData() {
        // 准备测试数据
        LocalDateTime testDate = LocalDateTime.of(2025, 1, 3, 15, 30, 0);
        
        // 执行手动数据生成
        int count = scheduler.manualGenerateData(testDate);
        
        // 验证结果
        assertTrue(count > 0, "手动生成应该返回成功记录数");
        
        // 验证数据保存
        List<CustomerProfileAnalysis> savedRecords = customerProfileAnalysisService.list();
        assertFalse(savedRecords.isEmpty(), "数据库中应该有保存的记录");
        
        System.out.println("手动生成成功，保存了 " + savedRecords + " 条记录");
    }


}
