package com.tmerclub.cloud.systemtest.test.amount;

import com.tmerclub.cloud.common.product.vo.app.CategoryAppVO;
import com.tmerclub.cloud.order.vo.OrderDetailVO;
import com.moyuer.cloud.systemtest.api.amount.CategoryApi;
import com.moyuer.cloud.systemtest.api.amount.PayApi;
import com.moyuer.cloud.systemtest.api.amount.ProductApi;
import com.moyuer.cloud.systemtest.api.login.UserApi;
import com.moyuer.cloud.systemtest.api.order.MyOrderApi;
import com.moyuer.cloud.systemtest.api.platform.UserLevelApi;
import com.moyuer.cloud.systemtest.api.platform.UserRightsApi;
import com.moyuer.cloud.systemtest.api.store.ShopWalletApi;
import com.moyuer.cloud.systemtest.data.amount.SpuData;
import com.moyuer.cloud.systemtest.data.order.OrderDtoData;
import com.moyuer.cloud.systemtest.data.order.SubmitOrderDtoData;
import com.moyuer.cloud.systemtest.data.order.WalletAssert;
import com.moyuer.cloud.systemtest.data.platform.CategoryDtoData;
import com.moyuer.cloud.systemtest.data.platform.UserLevelDtoData;
import com.moyuer.cloud.systemtest.data.platform.UserRightsDtoData;
import com.moyuer.cloud.systemtest.data.store.DeliveryData;
import org.testng.annotations.Test;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lpj
 */
public class MemberAmountTest {
    private static List<Long> getCategoryId(Boolean isMainShop) {
        List<CategoryAppVO> categoryAppVOList = CategoryApi.listShopCategory(isMainShop);
        Long shopCategoryId = categoryAppVOList.get(categoryAppVOList.size() - 1).getCategoryId();
        Long categoryId = CategoryApi.listSigningCategory(isMainShop).get(0).getCategoryId();
        CategoryApi.update(CategoryDtoData.getCategoryDTO(categoryId));
        return Stream.of(categoryId, shopCategoryId).collect(Collectors.toList());
    }

    @Test(description = "会员折扣优惠金额是否正确")
    public void memberAmountTest() {
        List<Long> categoryIds = getCategoryId(true);
        Long transportId = DeliveryData.getFreeFeeDelivery(true);
        Long productId = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 7337L), true);

        //新增打8折权益
        Long discountUserRightsId = UserRightsApi.save(UserRightsDtoData.getDiscounttUserRightsDTO(70));
        //用户会员等级添加包邮和打8折权益
        UserLevelApi.update(UserLevelDtoData.getUserLevelDTO(UserApi.getUserInfo().getUserLevelId(), Collections.singletonList(discountUserRightsId)));

        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productId, 1));
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId)).get(0);
        PayApi.orderPay(orderNum);

        UserRightsApi.delete(discountUserRightsId);
        ProductApi.delete(productId, true);

        //验证数据
        WalletAssert.shopWalletLogAssert(ShopWalletApi.getMongoShopWalletLogBO(orderNum, true).get(0), 0, 1, 7191L, 5136L, 2201L, 0L, 0L, 146L, 0);
        OrderDetailVO orderDetailVO = ShopWalletApi.getOrderDetailVO(orderNum, true);
        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(0), 7337L, 5136L, 0L, 0L, 2201L, 0L, 0L, 1.99, 146L,
                0L, 2201L, 0L, 0L, 0L, null, null, 0L, null, 1, null);
    }

    @Test(description = "商品金额为小数额，会员折扣优惠金额数据是否正确")
    public void minPriceMemberAmountTest() {
        List<Long> categoryIds = getCategoryId(true);
        Long transportId = DeliveryData.getFreeFeeDelivery(true);
        Long productId = ProductApi.createProduct(SpuData.getRealSpu(categoryIds.get(0), categoryIds.get(1), transportId, 1L), true);

        //新增打8折权益
        Long discountUserRightsId = UserRightsApi.save(UserRightsDtoData.getDiscounttUserRightsDTO(10));
        //用户会员等级添加包邮和打8折权益
        UserLevelApi.update(UserLevelDtoData.getUserLevelDTO(UserApi.getUserInfo().getUserLevelId(), Collections.singletonList(discountUserRightsId)));

        MyOrderApi.confirm(OrderDtoData.getOrderDtoData(productId, 1));
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productId)).get(0);
        PayApi.orderPay(orderNum);

        UserRightsApi.delete(discountUserRightsId);
        ProductApi.delete(productId, true);

        //验证数据
        WalletAssert.shopWalletLogAssert(ShopWalletApi.getMongoShopWalletLogBO(orderNum, true).get(0), 0, 1, 1L, 1L, 0L, 0L, 0L, 0L, 0);
        OrderDetailVO orderDetailVO = ShopWalletApi.getOrderDetailVO(orderNum, true);
        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(0), 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1.99, 0L,
                0L, 0L, 0L, 0L, 0L, null, null, 0L, null, 1, null);
    }
}
