package com.gxwebsoft.shop.service;

import com.gxwebsoft.shop.entity.ShopOrderGoods;
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.util.List;

/**
 * 订单商品忽略租户隔离功能测试
 *
 * @author WebSoft
 * @since 2025-08-23
 */
@Slf4j
@SpringBootTest
@ActiveProfiles("dev")
public class ShopOrderGoodsIgnoreTenantTest {

    @Resource
    private ShopOrderGoodsService shopOrderGoodsService;

    /**
     * 测试忽略租户隔离查询订单商品
     */
    @Test
    public void testGetListByOrderIdIgnoreTenant() {
        // 测试订单ID（请根据实际数据库中的订单ID进行调整）
        Integer testOrderId = 1;

        log.info("=== 开始测试忽略租户隔离查询订单商品功能 ===");

        // 1. 使用普通方法查询订单商品（受租户隔离影响）
        List<ShopOrderGoods> orderGoodsNormal = shopOrderGoodsService.getListByOrderId(testOrderId);
        log.info("普通查询结果 - 订单ID: {}, 商品数量: {}", testOrderId, 
            orderGoodsNormal != null ? orderGoodsNormal.size() : 0);

        // 2. 使用忽略租户隔离方法查询订单商品
        List<ShopOrderGoods> orderGoodsIgnoreTenant = shopOrderGoodsService.getListByOrderIdIgnoreTenant(testOrderId);
        log.info("忽略租户隔离查询结果 - 订单ID: {}, 商品数量: {}", testOrderId, 
            orderGoodsIgnoreTenant != null ? orderGoodsIgnoreTenant.size() : 0);

        // 3. 验证结果
        if (orderGoodsIgnoreTenant != null && !orderGoodsIgnoreTenant.isEmpty()) {
            log.info("✅ 忽略租户隔离查询成功！");
            for (ShopOrderGoods orderGoods : orderGoodsIgnoreTenant) {
                log.info("订单商品详情 - ID: {}, 商品ID: {}, 商品名称: {}, 数量: {}, 租户ID: {}", 
                    orderGoods.getId(),
                    orderGoods.getGoodsId(), 
                    orderGoods.getGoodsName(), 
                    orderGoods.getTotalNum(),
                    orderGoods.getTenantId());
            }
        } else {
            log.warn("⚠️ 忽略租户隔离查询结果为空，可能订单不存在或没有商品");
        }

        log.info("=== 忽略租户隔离查询订单商品功能测试完成 ===");
    }

    /**
     * 测试参数验证
     */
    @Test
    public void testGetListByOrderIdIgnoreTenantValidation() {
        log.info("=== 开始测试参数验证 ===");

        // 测试null订单ID
        List<ShopOrderGoods> result1 = shopOrderGoodsService.getListByOrderIdIgnoreTenant(null);
        log.info("null订单ID测试结果: {}", result1.isEmpty() ? "成功(返回空列表)" : "失败");

        // 测试不存在的订单ID
        List<ShopOrderGoods> result2 = shopOrderGoodsService.getListByOrderIdIgnoreTenant(999999);
        log.info("不存在订单ID测试结果: {}", result2.isEmpty() ? "成功(返回空列表)" : "失败");

        log.info("=== 参数验证测试完成 ===");
    }

    /**
     * 测试跨租户查询
     */
    @Test
    public void testCrossTenantQuery() {
        log.info("=== 开始测试跨租户查询 ===");

        // 查询不同租户的订单商品（请根据实际数据调整）
        Integer[] testOrderIds = {1, 2, 3, 4, 5};

        for (Integer orderId : testOrderIds) {
            List<ShopOrderGoods> orderGoodsList = shopOrderGoodsService.getListByOrderIdIgnoreTenant(orderId);
            if (orderGoodsList != null && !orderGoodsList.isEmpty()) {
                log.info("订单ID: {}, 商品数量: {}", orderId, orderGoodsList.size());
                for (ShopOrderGoods orderGoods : orderGoodsList) {
                    log.info("  - 商品: {} (ID: {}), 数量: {}, 租户: {}", 
                        orderGoods.getGoodsName(), 
                        orderGoods.getGoodsId(),
                        orderGoods.getTotalNum(),
                        orderGoods.getTenantId());
                }
            } else {
                log.info("订单ID: {} - 无商品或不存在", orderId);
            }
        }

        log.info("=== 跨租户查询测试完成 ===");
    }

    /**
     * 测试批量查询性能
     */
    @Test
    public void testBatchQuery() {
        log.info("=== 开始测试批量查询性能 ===");

        Integer[] testOrderIds = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        
        long startTime = System.currentTimeMillis();
        
        int totalGoods = 0;
        for (Integer orderId : testOrderIds) {
            List<ShopOrderGoods> orderGoodsList = shopOrderGoodsService.getListByOrderIdIgnoreTenant(orderId);
            totalGoods += orderGoodsList.size();
        }
        
        long endTime = System.currentTimeMillis();
        long duration = endTime - startTime;
        
        log.info("批量查询结果 - 查询订单数: {}, 总商品数: {}, 耗时: {}ms", 
            testOrderIds.length, totalGoods, duration);

        log.info("=== 批量查询性能测试完成 ===");
    }
}
