package com.gxwebsoft.shop.service;

import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.UserService;
import com.gxwebsoft.shop.entity.ShopOrder;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 订单更新业务测试
 *
 * @author WebSoft
 * @since 2025-08-23
 */
@Slf4j
@SpringBootTest
@ActiveProfiles("dev")
public class ShopOrderUpdate10550ServiceTest {

    @Resource
    private ShopOrderUpdate10550Service shopOrderUpdate10550Service;
    
    @Resource
    private UserService userService;

    /**
     * 测试用户等级升级功能
     */
    @Test
    public void testUserGradeUpgrade() {
        log.info("=== 开始测试用户等级升级功能 ===");

        // 创建测试订单
        ShopOrder testOrder = createTestOrder();
        
        // 查询用户升级前的信息
        User userBefore = userService.getByIdIgnoreTenant(testOrder.getUserId());
        if (userBefore != null) {
            log.info("升级前用户信息 - ID: {}, 等级: {}, 消费金额: {}", 
                userBefore.getUserId(), userBefore.getGradeId(), userBefore.getExpendMoney());
        }

        // 执行订单更新业务
        shopOrderUpdate10550Service.update(testOrder);

        // 查询用户升级后的信息
        User userAfter = userService.getByIdIgnoreTenant(testOrder.getUserId());
        if (userAfter != null) {
            log.info("升级后用户信息 - ID: {}, 等级: {}, 消费金额: {}", 
                userAfter.getUserId(), userAfter.getGradeId(), userAfter.getExpendMoney());
        }

        log.info("=== 用户等级升级功能测试完成 ===");
    }

    /**
     * 测试合伙人条件配置获取
     */
    @Test
    public void testPartnerConditionConfig() {
        log.info("=== 开始测试合伙人条件配置获取 ===");

        // 创建测试订单
        ShopOrder testOrder = createTestOrder();
        
        // 执行订单更新业务（会在日志中显示合伙人条件）
        shopOrderUpdate10550Service.update(testOrder);

        log.info("=== 合伙人条件配置获取测试完成 ===");
    }

    /**
     * 测试异常处理
     */
    @Test
    public void testExceptionHandling() {
        log.info("=== 开始测试异常处理 ===");

        // 测试null订单
        try {
            shopOrderUpdate10550Service.update(null);
            log.info("null订单处理：正常（应该有异常日志）");
        } catch (Exception e) {
            log.info("null订单处理：捕获异常 - {}", e.getMessage());
        }

        // 测试无效用户ID的订单
        ShopOrder invalidOrder = new ShopOrder();
        invalidOrder.setOrderId(999999);
        invalidOrder.setUserId(999999);
        invalidOrder.setTenantId(10550);
        invalidOrder.setPayPrice(new BigDecimal("100.00"));
        
        try {
            shopOrderUpdate10550Service.update(invalidOrder);
            log.info("无效用户订单处理：正常（应该有警告日志）");
        } catch (Exception e) {
            log.info("无效用户订单处理：捕获异常 - {}", e.getMessage());
        }

        log.info("=== 异常处理测试完成 ===");
    }

    /**
     * 测试批量订单处理
     */
    @Test
    public void testBatchOrderProcessing() {
        log.info("=== 开始测试批量订单处理 ===");

        // 模拟多个订单
        for (int i = 1; i <= 5; i++) {
            ShopOrder order = createTestOrder();
            order.setOrderId(1000 + i);
            order.setPayPrice(new BigDecimal("50.00").multiply(BigDecimal.valueOf(i)));
            
            log.info("处理第{}个订单 - 订单ID: {}, 金额: {}", i, order.getOrderId(), order.getPayPrice());
            shopOrderUpdate10550Service.update(order);
        }

        log.info("=== 批量订单处理测试完成 ===");
    }

    /**
     * 创建测试订单
     */
    private ShopOrder createTestOrder() {
        ShopOrder order = new ShopOrder();
        order.setOrderId(1001);
        order.setUserId(1); // 请根据实际数据库中的用户ID调整
        order.setTenantId(10550);
        order.setPayPrice(new BigDecimal("500.00")); // 测试金额
        order.setTotalPrice(new BigDecimal("500.00"));
        return order;
    }
}
