package com.ylx.giftCard.mapper;

import org.junit.jupiter.api.Test;

import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class GiftCardOrderMapperXmlTest {

    @Test
    public void pcPurchaseShoppingFundsSqlFiltersByUserStatusTimeAndSortsByCreateTimeDesc() throws Exception {
        String xml = readMapperXml();

        assertTrue(xml.contains("selectPcPurchaseShoppingFundsDetail"));
        assertTrue(xml.contains("selectPcPurchaseShoppingFundsSummary"));
        assertTrue(xml.contains("gco.user_id = #{dto.userId}"));
        assertTrue(xml.contains("gco.status = 1"));
        assertTrue(xml.contains("gco.create_time &gt;= #{dto.startTime}"));
        assertTrue(xml.contains("gco.create_time &lt;= #{dto.endTime}"));
        assertTrue(xml.contains("ORDER BY"));
        assertTrue(xml.contains("gco.create_time DESC"));
    }

    @Test
    public void adminGiftCardOrderPageSqlJoinsMerchantPhoneAndFiltersPrototypeFields() throws Exception {
        String xml = readMapperXml();

        assertTrue(xml.contains("selectAdminGiftCardOrderPage"));
        assertTrue(xml.contains("gco.order_no AS orderId"));
        assertTrue(xml.contains("gco.order_no AS orderNo"));
        assertTrue(xml.contains("LEFT JOIN ma_technician mt ON mt.id = gco.merchant_id"));
        assertTrue(xml.contains("mt.te_phone AS merchantAccount"));
        assertTrue(xml.contains("gco.user_name LIKE CONCAT('%', #{dto.userName}, '%')"));
        assertTrue(xml.contains("gco.merchant_name LIKE CONCAT('%', #{dto.merchantName}, '%')"));
        assertTrue(xml.contains("gco.create_time &gt;= #{dto.startTime}"));
        assertTrue(xml.contains("gco.create_time &lt;= #{dto.endTime}"));
        assertTrue(xml.contains("gco.create_time DESC"));
    }

    @Test
    public void adminGiftCardOrderExportSqlExportsAllListColumnsWithSameFilters() throws Exception {
        String xml = readMapperXml();

        assertTrue(xml.contains("selectAdminGiftCardOrderExportList"));
        assertTrue(xml.contains("gco.order_no AS orderId"));
        assertTrue(xml.contains("gco.gift_card_name AS giftCardName"));
        assertTrue(xml.contains("gco.pay_amount AS payAmount"));
        assertTrue(xml.contains("gco.user_name AS userName"));
        assertTrue(xml.contains("gco.user_phone AS userPhone"));
        assertTrue(xml.contains("gco.create_time AS orderTime"));
        assertTrue(xml.contains("gco.commission_amount AS commissionAmount"));
        assertTrue(xml.contains("mt.te_phone AS merchantAccount"));
        assertTrue(xml.contains("gco.merchant_name AS merchantName"));
        assertTrue(xml.contains("gco.merchant_nick_name AS merchantNickName"));
        assertTrue(xml.contains("LEFT JOIN ma_technician mt ON mt.id = gco.merchant_id"));
    }

    @Test
    public void giftCardOrderExportVoUsesPrototypeColumnNames() throws Exception {
        String source = new String(Files.readAllBytes(Paths.get(
                "src/main/java/com/ylx/giftCard/domain/vo/GiftCardOrderExportVO.java")), StandardCharsets.UTF_8);

        assertTrue(source.contains("@Excel(name = \"订单ID\", sort = 1)"));
        assertTrue(source.contains("@Excel(name = \"购物卡名称\", sort = 2)"));
        assertTrue(source.contains("@Excel(name = \"金额\", sort = 3)"));
        assertTrue(source.contains("@Excel(name = \"用户姓名\", sort = 4)"));
        assertTrue(source.contains("@Excel(name = \"用户电话\", sort = 5)"));
        assertTrue(source.contains("@Excel(name = \"下单时间\", sort = 6"));
        assertTrue(source.contains("@Excel(name = \"商户提成金额\", sort = 7)"));
        assertTrue(source.contains("@Excel(name = \"商户账户\", sort = 8)"));
        assertTrue(source.contains("@Excel(name = \"商户姓名\", sort = 9)"));
        assertTrue(source.contains("@Excel(name = \"商户昵称\", sort = 10)"));
    }

    private String readMapperXml() throws Exception {
        try (InputStream inputStream = getClass().getClassLoader()
                .getResourceAsStream("mapper/giftCard/GiftCardOrderMapper.xml")) {
            assertNotNull(inputStream, "GiftCardOrderMapper.xml should exist in test classpath");
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            return new String(bytes, StandardCharsets.UTF_8);
        }
    }
}
