package com.gxwebsoft.shop.service;

import com.gxwebsoft.shop.entity.ShopGoods;
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;

/**
 * 商品销量累加功能测试
 *
 * @author WebSoft
 * @since 2025-08-23
 */
@Slf4j
@SpringBootTest
@ActiveProfiles("dev")
public class ShopGoodsSalesTest {

    @Resource
    private ShopGoodsService shopGoodsService;

    /**
     * 测试累加商品销量功能
     */
    @Test
    public void testAddSaleCount() {
        // 测试商品ID（请根据实际数据库中的商品ID进行调整）
        Integer testGoodsId = 1;
        Integer addCount = 5;

        log.info("=== 开始测试商品销量累加功能 ===");

        // 1. 查询商品当前销量
        ShopGoods goodsBefore = shopGoodsService.getById(testGoodsId);
        if (goodsBefore == null) {
            log.error("测试失败：商品不存在 - 商品ID: {}", testGoodsId);
            return;
        }

        Integer salesBefore = goodsBefore.getSales() != null ? goodsBefore.getSales() : 0;
        log.info("累加前商品销量 - 商品ID: {}, 商品名称: {}, 当前销量: {}",
            testGoodsId, goodsBefore.getName(), salesBefore);

        // 2. 执行销量累加
        boolean result = shopGoodsService.addSaleCount(testGoodsId, addCount);
        log.info("销量累加操作结果: {}", result ? "成功" : "失败");

        // 3. 查询累加后的销量
        ShopGoods goodsAfter = shopGoodsService.getById(testGoodsId);
        Integer salesAfter = goodsAfter.getSales() != null ? goodsAfter.getSales() : 0;
        log.info("累加后商品销量 - 商品ID: {}, 商品名称: {}, 累加后销量: {}",
            testGoodsId, goodsAfter.getName(), salesAfter);

        // 4. 验证结果
        Integer expectedSales = salesBefore + addCount;
        if (salesAfter.equals(expectedSales)) {
            log.info("✅ 测试成功！销量正确累加 - 预期: {}, 实际: {}", expectedSales, salesAfter);
        } else {
            log.error("❌ 测试失败！销量累加不正确 - 预期: {}, 实际: {}", expectedSales, salesAfter);
        }

        log.info("=== 商品销量累加功能测试完成 ===");
    }

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

        // 测试null商品ID
        boolean result1 = shopGoodsService.addSaleCount(null, 5);
        log.info("null商品ID测试结果: {}", result1 ? "失败(应该返回false)" : "成功");

        // 测试null销量
        boolean result2 = shopGoodsService.addSaleCount(1, null);
        log.info("null销量测试结果: {}", result2 ? "失败(应该返回false)" : "成功");

        // 测试负数销量
        boolean result3 = shopGoodsService.addSaleCount(1, -1);
        log.info("负数销量测试结果: {}", result3 ? "失败(应该返回false)" : "成功");

        // 测试零销量
        boolean result4 = shopGoodsService.addSaleCount(1, 0);
        log.info("零销量测试结果: {}", result4 ? "失败(应该返回false)" : "成功");

        // 测试不存在的商品ID
        boolean result5 = shopGoodsService.addSaleCount(999999, 5);
        log.info("不存在商品ID测试结果: {}", result5 ? "失败(应该返回false)" : "成功");

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

    /**
     * 测试大批量累加
     */
    @Test
    public void testBatchAddSaleCount() {
        Integer testGoodsId = 1;

        log.info("=== 开始测试批量累加 ===");

        // 查询初始销量
        ShopGoods goodsBefore = shopGoodsService.getById(testGoodsId);
        if (goodsBefore == null) {
            log.error("测试失败：商品不存在 - 商品ID: {}", testGoodsId);
            return;
        }

        Integer salesBefore = goodsBefore.getSales() != null ? goodsBefore.getSales() : 0;
        log.info("批量累加前销量: {}", salesBefore);

        // 模拟多次购买
        int totalAdded = 0;
        for (int i = 1; i <= 10; i++) {
            boolean result = shopGoodsService.addSaleCount(testGoodsId, i);
            if (result) {
                totalAdded += i;
                log.info("第{}次累加成功，累加数量: {}", i, i);
            } else {
                log.error("第{}次累加失败", i);
            }
        }

        // 验证最终结果
        ShopGoods goodsAfter = shopGoodsService.getById(testGoodsId);
        Integer salesAfter = goodsAfter.getSales() != null ? goodsAfter.getSales() : 0;
        Integer expectedSales = salesBefore + totalAdded;

        log.info("批量累加结果 - 累加前: {}, 总累加量: {}, 累加后: {}, 预期: {}",
            salesBefore, totalAdded, salesAfter, expectedSales);

        if (salesAfter.equals(expectedSales)) {
            log.info("✅ 批量累加测试成功！");
        } else {
            log.error("❌ 批量累加测试失败！");
        }

        log.info("=== 批量累加测试完成 ===");
    }
}
