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

import com.tmerclub.cloud.common.order.dto.OrderDTO;
import com.tmerclub.cloud.common.product.vo.app.CategoryAppVO;
import com.tmerclub.cloud.discount.dto.DiscountItemDTO;
import com.tmerclub.cloud.multishop.mongo.MongoShopWalletLogBO;
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.marketing.CouponApi;
import com.moyuer.cloud.systemtest.api.marketing.DiscountApi;
import com.moyuer.cloud.systemtest.api.marketing.DistributionProdApi;
import com.moyuer.cloud.systemtest.api.order.MyOrderApi;
import com.moyuer.cloud.systemtest.api.order.ShopCartApi;
import com.moyuer.cloud.systemtest.api.platform.ScoreConfigApi;
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.marketing.CouponData;
import com.moyuer.cloud.systemtest.data.marketing.DiscountData;
import com.moyuer.cloud.systemtest.data.marketing.DistributionProdData;
import com.moyuer.cloud.systemtest.data.order.OrderDtoData;
import com.moyuer.cloud.systemtest.data.order.ShopCartData;
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.ScoreConfigDtoData;
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.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

    private static List<Long> getCategoryIdB() {
        List<CategoryAppVO> categoryAppVOList = CategoryApi.listShopCategory(false);
        Long shopCategoryId = categoryAppVOList.get(categoryAppVOList.size() - 1).getCategoryId();
        Long categoryId = CategoryApi.listSigningCategory(false).get(0).getCategoryId();
        CategoryApi.update(CategoryDtoData.getCategoryDTO(categoryId));
        return Stream.of(categoryId, shopCategoryId).collect(Collectors.toList());
    }

    @Test(description = "单店铺下使用多种优惠，优惠金额、付款金额是否正确")
    public void sameShopMultipleDiscountsAmountTest() {
        Long transportId = DeliveryData.getFreeFeeDelivery(true);
        List<Long> categoryIdsA = getCategoryIdA();
        Long categoryIdA = categoryIdsA.get(0);
        Long shopCategoryIdA = categoryIdsA.get(1);
        Long productIdA1 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdA, shopCategoryIdA, transportId, 6912L), true);
        Long productIdA2 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdA, shopCategoryIdA, transportId, 7222L), true);
        Long productIdA3 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdA, shopCategoryIdA, transportId, 3070L), true);
        Long productIdA4 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdA, shopCategoryIdA, transportId, 2000L), true);

        //商品a1, a4参与满减活动A-每满7减5.33
        List<DiscountItemDTO> discountItemDtoListA = List.of(DiscountData.getDiscountItemDTO(700L, 533L));
        List<Long> discountProductIdsA = Arrays.asList(productIdA1, productIdA4);
        DiscountApi.save(DiscountData.getDiscount(0, 1, discountItemDtoListA, 50000L, 1, discountProductIdsA, 1), true);
        Long discountIdA = DiscountApi.getDiscountPage(true).get(0).getDiscountId();

        //商品a2, a3参与满减活动B-满2件打8.5折，满3件打7.7折
        List<DiscountItemDTO> discountItemDtoListB = List.of(DiscountData.getDiscountItemDTO(200L, 850L), DiscountData.getDiscountItemDTO(300L, 770L));
        List<Long> discountProductIdsB = Arrays.asList(productIdA2, productIdA3);
        DiscountApi.save(DiscountData.getDiscount(3, 0, discountItemDtoListB, 50000L, 1, discountProductIdsB, 1), true);
        Long discountIdB = DiscountApi.getDiscountPage(true).get(0).getDiscountId();

        //商品a1, a2, a4参与店铺优惠券，满50打3.7折
        List<Long> shopCouponProductIds = Arrays.asList(productIdA1, productIdA2, productIdA4);
        CouponApi.saveShopCoupon(CouponData.getCouponDTO(1, 0, 2, 5000L, null, 3.7, 2, 10, 100, 1, shopCouponProductIds));
        Long shopCouponId = CouponApi.getShopCouponPage().get(0).getCouponId();
        CouponApi.receive(shopCouponId);

        //商品a1, a2参与平台优惠券，满7.2减5.02
        List<Long> platformCouponProductIds = Arrays.asList(productIdA1, productIdA2, productIdA4);
        CouponApi.savePlatformCoupon(CouponData.getCouponDTO(1, 0, 1, 720L, 502L, null, 2, 10, 100, 1, platformCouponProductIds));
        Long platformCouponId = CouponApi.getPlatformCouponPage().get(0).getCouponId();
        CouponApi.receive(platformCouponId);

        //修改积分配置，每15积分抵扣一元，最大使用积分比例33.7%
        ScoreConfigApi.update(ScoreConfigDtoData.getScoreConfigDTO(15L ,33.7));

        //将商品加入购物车
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA1, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA2, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA3, 3));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA4, 1));

        OrderDTO orderDTO = OrderDtoData.getOrderDtoData(productIdA1);
        //使用优惠券
        orderDTO.setCouponIds(Arrays.asList(CouponApi.getUserCouponPage().get(0).getCouponUserId(), CouponApi.getUserCouponPage().get(1).getCouponUserId()));
        //使用最大可使用积分
        orderDTO.setIsScorePay(1);
        orderDTO.setUserUseScore(MyOrderApi.confirm(orderDTO).getMaxUsableScore());
        //确认订单
        MyOrderApi.confirm(orderDTO);
        //提交订单
        Long orderNum = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productIdA1)).get(0);
        //支付订单
        PayApi.orderPay(orderNum);

        CouponApi.deletePlatformCoupon(platformCouponId);
        CouponApi.deleteShopCoupon(shopCouponId);
        DiscountApi.delete(discountIdB, true);
        DiscountApi.delete(discountIdA, true);
        ProductApi.delete(productIdA4, true);
        ProductApi.delete(productIdA3, true);
        ProductApi.delete(productIdA2, true);
        ProductApi.delete(productIdA1, true);

        //验证数据
        WalletAssert.shopWalletLogAssert(ShopWalletApi.getMongoShopWalletLogBO(orderNum, true).get(0), 0, 1, 9880L, 6351L, 3729L, 15264L, 0L, 200L, 0);
        OrderDetailVO orderDetailVO = ShopWalletApi.getOrderDetailVO(orderNum, true);
        for (int i=0; i<orderDetailVO.getOrderItemDetailList().size(); i++) {
            //商品a1结算明细验证
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(6912L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 6912L, 401L, 0L, 6190L, 321L, 0L, 30L, 1.99, 14L,
                        200L, 0L, 121L, 1229L, 4961L, null, null, 0L, null, 1, null);
            }
            //商品a2结算明细验证
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(7222L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 7222L, 1133L, 0L, 5164L, 925L, 0L, 87L, 1.99, 41L,
                        580L, 0L, 345L, 3503L, 1661L, null, null, 0L, null, 1, null);
            }
            //商品a3结算明细验证
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(9210L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 9210L, 4702L, 0L, 2120L, 2388L, 0L, 358L, 1.99, 141L,
                        2387L, 0L, 1L, 2L, 2118L, null, null, 0L, null, 3, null);
            }
            //商品a4结算明细验证
            if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(2000L)) {
                WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 2000L, 115L, 0L, 1790L, 95L, 0L, 9L, 1.99, 4L,
                        60L, 0L, 35L, 355L, 1435L, null, null, 0L, null, 1, null);
            }
        }
    }

    @Test(description = "多店铺下使用多种优惠，优惠金额、付款金额是否正确")
    public void differentShopMultipleDiscountsAmountTest() {
        //A店铺商品a1, a2, a3, a4
        Long transportIdA = DeliveryData.getFreeFeeDelivery(true);
        List<Long> categoryIdsA = getCategoryIdA();
        Long categoryIdA = categoryIdsA.get(0);
        Long shopCategoryIdA = categoryIdsA.get(1);
        Long productIdA1 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdA, shopCategoryIdA, transportIdA, 6912L), true);
        Long productIdA2 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdA, shopCategoryIdA, transportIdA, 7222L), true);
        Long productIdA3 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdA, shopCategoryIdA, transportIdA, 3070L), true);
        Long productIdA4 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdA, shopCategoryIdA, transportIdA, 2000L), true);

        //商品a1, a4参与满减活动A-每满7减5.33
        List<DiscountItemDTO> discountItemDtoListA = List.of(DiscountData.getDiscountItemDTO(700L, 533L));
        List<Long> discountProductIdsA = Arrays.asList(productIdA1, productIdA4);
        DiscountApi.save(DiscountData.getDiscount(0, 1, discountItemDtoListA, 50000L, 1, discountProductIdsA, 1), true);
        Long discountIdA = DiscountApi.getDiscountPage(true).get(0).getDiscountId();

        //商品a2, a3参与满减活动B-满2件打8.5折，满3件打7.7折
        List<DiscountItemDTO> discountItemDtoListB = List.of(DiscountData.getDiscountItemDTO(200L, 850L), DiscountData.getDiscountItemDTO(300L, 770L));
        List<Long> discountProductIdsB = Arrays.asList(productIdA2, productIdA3);
        DiscountApi.save(DiscountData.getDiscount(3, 0, discountItemDtoListB, 50000L, 1, discountProductIdsB, 1), true);
        Long discountIdB = DiscountApi.getDiscountPage(true).get(0).getDiscountId();

        //商品a1, a2, a4参与店铺优惠券，满50打3.7折
        List<Long> shopCouponProductIds = Arrays.asList(productIdA1, productIdA2, productIdA4);
        CouponApi.saveShopCoupon(CouponData.getCouponDTO(1, 0, 2, 5000L, null, 3.7, 2, 10, 100, 1, shopCouponProductIds));
        Long shopCouponId = CouponApi.getShopCouponPage().get(0).getCouponId();
        CouponApi.receive(shopCouponId);

        //B店铺，商品b1, b2, b3
        Long transportIdB = DeliveryData.getFreeFeeDelivery(false);
        List<Long> categoryIdsB = getCategoryIdB();
        Long categoryIdB = categoryIdsB.get(0);
        Long shopCategoryIdB = categoryIdsB.get(1);
        Long productIdB1 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdB, shopCategoryIdB, transportIdB, 16912L), false);
        Long productIdB2 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdB, shopCategoryIdB, transportIdB, 7222L), false);
        Long productIdB3 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdB, shopCategoryIdB, transportIdB, 3070L), false);

        //商品a2, a3参与满减活动B-满1件打8.5折，满2件打7.5折
        List<DiscountItemDTO> discountItemDtoListC = List.of(DiscountData.getDiscountItemDTO(100L, 850L), DiscountData.getDiscountItemDTO(200L, 750L));
        List<Long> discountProductIdsC = Arrays.asList(productIdB1, productIdB3);
        DiscountApi.save(DiscountData.getDiscount(3, 0, discountItemDtoListC, 50000L, 1, discountProductIdsC, 1), false);
        Long discountIdC = DiscountApi.getDiscountPage(false).get(0).getDiscountId();

        //商品a1, a2, b3参与平台优惠券，满50减7.2
        List<Long> platformCouponProductIds = Arrays.asList(productIdA1, productIdA2, productIdB3);
        CouponApi.savePlatformCoupon(CouponData.getCouponDTO(1, 0, 1, 5000L, 720L, null, 2, 10, 100, 1, platformCouponProductIds));
        Long platformCouponId = CouponApi.getPlatformCouponPage().get(0).getCouponId();
        CouponApi.receive(platformCouponId);

        //修改积分配置，每15积分抵扣一元，最大使用积分比例30.7%
        ScoreConfigApi.update(ScoreConfigDtoData.getScoreConfigDTO(15L ,30.7));

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

        //将商品加入购物车
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA1, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA2, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA3, 3));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA4, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB1, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB2, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB3, 1));

        //确认订单数据
        Long shopIdA = ProductApi.getProductInfo(productIdA1).getShopId();
        Long shopIdB = ProductApi.getProductInfo(productIdB1).getShopId();
        OrderDTO orderDTO = OrderDtoData.getOrderDtoData(Arrays.asList(shopIdA, shopIdB));
        //使用优惠券
        orderDTO.setCouponIds(Arrays.asList(CouponApi.getUserCouponPage().get(0).getCouponUserId(), CouponApi.getUserCouponPage().get(1).getCouponUserId()));
        //使用最大可使用积分
        orderDTO.setIsScorePay(1);
        orderDTO.setUserUseScore(MyOrderApi.confirm(orderDTO).getMaxUsableScore());
        //确认订单
        MyOrderApi.confirm(orderDTO);
        //提交订单
        List<Long> orderNumberList = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(productIdA1));
        //支付订单
        PayApi.orderPay(orderNumberList);

        UserRightsApi.delete(discountUserRightsId);
        CouponApi.deletePlatformCoupon(platformCouponId);
        CouponApi.deleteShopCoupon(shopCouponId);
        DiscountApi.delete(discountIdC, false);
        DiscountApi.delete(discountIdB, true);
        DiscountApi.delete(discountIdA, true);
        ProductApi.delete(productIdB3, false);
        ProductApi.delete(productIdB2, false);
        ProductApi.delete(productIdB1, false);
        ProductApi.delete(productIdA4, true);
        ProductApi.delete(productIdA3, true);
        ProductApi.delete(productIdA2, true);
        ProductApi.delete(productIdA1, true);

        //验证数据
        for(Long orderNum : orderNumberList) {
            if(MyOrderApi.orderDetail(orderNum).getShopId().equals(shopIdA)) {
                WalletAssert.shopWalletLogAssert(ShopWalletApi.getMongoShopWalletLogBO(orderNum, true).get(0), 0, 1, 9880L, 6109L, 3971L, 15264L, 0L, 200L, 0);
                OrderDetailVO orderDetailVO = ShopWalletApi.getOrderDetailVO(orderNum, true);
                for (int i=0; i<orderDetailVO.getOrderItemDetailList().size(); i++) {
                    //商品a1结算明细验证
                    if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(6912L)) {
                        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 6912L, 391L, 0L, 6190L, 331L, 0L, 26L, 1.99, 14L,
                                173L, 56L, 102L, 1229L, 4961L, null, null, 0L, null, 1, null);
                    }
                    //商品a2结算明细验证
                    if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(7222L)) {
                        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 7222L, 1115L, 0L, 5164L, 943L, 0L, 74L, 1.99, 41L,
                                493L, 159L, 291L, 3503L, 1661L, null, null, 0L, null, 1, null);
                    }
                    //商品a3结算明细验证
                    if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(9210L)) {
                        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 9210L, 4472L, 0L, 2120L, 2618L, 0L, 297L, 1.99, 141L,
                                1980L, 638L, 0L, 2L, 2118L, null, null, 0L, null, 3, null);
                    }
                    //商品a4结算明细验证
                    if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(2000L)) {
                        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 2000L, 131L, 0L, 1790L, 79L, 0L, 9L, 1.99, 4L,
                                60L, 19L, 0L, 355L, 1435L, null, null, 0L, null, 1, null);
                    }
                }
            }
            if(MyOrderApi.orderDetail(orderNum).getShopId().equals(shopIdB)) {
                WalletAssert.shopWalletLogAssert(ShopWalletApi.getMongoShopWalletLogBO(orderNum, false).get(0), 0, 1, 21766L, 13811L, 8397L, 4996L, 0L, 442L, 0);
                OrderDetailVO orderDetailVO = ShopWalletApi.getOrderDetailVO(orderNum, false);
                for (int i = 0; i < orderDetailVO.getOrderItemDetailList().size(); i++) {
                    //商品b1结算明细验证
                    if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(16912L)) {
                        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 16912L, 8014L, 0L, 4228L, 4670L, 0L, 529L, 1.99, 252L,
                                3528L, 1141L, 1L, 0L, 4228L, null, null, 0L, null, 1, null);
                    }
                    //商品b2结算明细验证
                    if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(7222L)) {
                        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 7222L, 4552L, 0L, 0L, 2670L, 0L, 303L, 1.99, 144L,
                                2020L, 650L, 0L, 0L, 0L, null, null, 0L, null, 1, null);
                    }
                    //商品b3结算明细验证
                    if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(3070L)) {
                        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 3070L, 1245L, 0L, 768L, 1057L, 0L, 83L, 1.99, 46L,
                                553L, 178L, 326L, 0L, 768L, null, null, 0L, null, 1, null);
                    }
                }
            }
        }
    }

    @Test(description = "结算明细使用各种优惠，金额明细是否正确")
    public void shopWalletDetail() throws InterruptedException {
        //分类A：9.1%，分类B：0.1%，分类C：0%
        Long categoryIdFirst = CategoryApi.save(CategoryDtoData.getCategoryDTO(0, 0L, 0.0));
        Long categoryIdSec = CategoryApi.save(CategoryDtoData.getCategoryDTO(1, categoryIdFirst, 0.0));
        Long categoryIdA = CategoryApi.save(CategoryDtoData.getCategoryDTO(2, categoryIdSec, 9.1));
        Long categoryIdB = CategoryApi.save(CategoryDtoData.getCategoryDTO(2, categoryIdSec, 0.1));
        Long categoryIdC = CategoryApi.save(CategoryDtoData.getCategoryDTO(2, categoryIdSec, 0.0));

        //A店铺签约分类A、B、C
        CategoryApi.addSigningCategory(CategoryDtoData.getCategoryShopDtoList(Arrays.asList(categoryIdA, categoryIdB, categoryIdC)), true);
        //A店铺获取店铺分类idA
        List<CategoryAppVO> categoryAppVoListA = CategoryApi.listShopCategory(true);
        Long shopCategoryIdA = categoryAppVoListA.get(categoryAppVoListA.size() - 1).getCategoryId();

        //A店铺商品a1, a2, a3, a4
        Long transportIdA = DeliveryData.getFreeFeeDelivery(true);
        Long productIdA1 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdA, shopCategoryIdA, transportIdA, 6912L), true);
        Long productIdA2 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdA, shopCategoryIdA, transportIdA, 7222L), true);
        Long productIdA3 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdC, shopCategoryIdA, transportIdA, 3070L), true);
        Long productIdA4 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdC, shopCategoryIdA, transportIdA, 2000L), true);

        //商品a1, a4参与满减活动A-每满7减5.33
        List<DiscountItemDTO> discountItemDtoListA = List.of(DiscountData.getDiscountItemDTO(700L, 533L));
        List<Long> discountProductIdsA = Arrays.asList(productIdA1, productIdA4);
        DiscountApi.save(DiscountData.getDiscount(0, 1, discountItemDtoListA, 50000L, 1, discountProductIdsA, 1), true);
        Long discountIdA = DiscountApi.getDiscountPage(true).get(0).getDiscountId();

        //商品a2, a3参与满减活动B-满2件打8.5折，满3件打7.7折
        List<DiscountItemDTO> discountItemDtoListB = List.of(DiscountData.getDiscountItemDTO(200L, 850L), DiscountData.getDiscountItemDTO(300L, 770L));
        List<Long> discountProductIdsB = Arrays.asList(productIdA2, productIdA3);
        DiscountApi.save(DiscountData.getDiscount(3, 0, discountItemDtoListB, 50000L, 1, discountProductIdsB, 1), true);
        Long discountIdB = DiscountApi.getDiscountPage(true).get(0).getDiscountId();

        //商品a1, a2, a4参与店铺优惠券，满50打3.7折
        List<Long> shopCouponProductIds = Arrays.asList(productIdA1, productIdA2, productIdA4);
        CouponApi.saveShopCoupon(CouponData.getCouponDTO(1, 0, 2, 5000L, null, 3.7, 2, 10, 100, 1, shopCouponProductIds));
        Long shopCouponId = CouponApi.getShopCouponPage().get(0).getCouponId();
        CouponApi.receive(shopCouponId);

        //商品a1为分销商品，直推奖励1%，间推奖励为1.21%
        DistributionProdApi.save(DistributionProdData.getDistributionSpuDTO(productIdA1, 0, 100L, 1, 121L, 1), true);
        //等待保存
        TimeUnit.SECONDS.sleep(2L);
        //获取分销商品id
        Long distributionProdIdA1 = DistributionProdApi.getDistributionProdPage(true).get(0).getDistributionInfo().getDistributionSpuId();

        //B店铺签约分类A、B、C
        CategoryApi.addSigningCategory(CategoryDtoData.getCategoryShopDtoList(Arrays.asList(categoryIdA, categoryIdB, categoryIdC)), false);
        //B店铺获取店铺分类IdB
        List<CategoryAppVO> categoryAppVoListB = CategoryApi.listShopCategory(false);
        Long shopCategoryIdB = categoryAppVoListB.get(categoryAppVoListB.size() - 1).getCategoryId();
        //B店铺，商品b1, b2, b3
        Long transportIdB = DeliveryData.getFreeFeeDelivery(false);

        Long productIdB1 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdB, shopCategoryIdB, transportIdB, 16912L), false);
        Long productIdB2 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdA, shopCategoryIdB, transportIdB, 7222L), false);
        Long productIdB3 = ProductApi.createProduct(SpuData.getRealSpu(categoryIdC, shopCategoryIdB, transportIdB, 3070L), false);

        //商品a2, a3参与满减活动B-满1件打8.5折，满2件打7.5折
        List<DiscountItemDTO> discountItemDtoListC = List.of(DiscountData.getDiscountItemDTO(100L, 850L), DiscountData.getDiscountItemDTO(200L, 750L));
        List<Long> discountProductIdsC = Arrays.asList(productIdB1, productIdB3);
        DiscountApi.save(DiscountData.getDiscount(3, 0, discountItemDtoListC, 50000L, 1, discountProductIdsC, 1), false);
        Long discountIdC = DiscountApi.getDiscountPage(false).get(0).getDiscountId();

        //商品b1为分销商品，直推奖励2，间推奖励为2.1
        DistributionProdApi.save(DistributionProdData.getDistributionSpuDTO(productIdB1, 1, 200L, 1, 210L, 1), false);
        //等待保存
        TimeUnit.SECONDS.sleep(2L);
        //获取分销商品id
        Long distributionProdIdB1 = DistributionProdApi.getDistributionProdPage(false).get(0).getDistributionInfo().getDistributionSpuId();

        //商品a1, a2, b3参与平台优惠券，满50减7.2
        List<Long> platformCouponProductIds = Arrays.asList(productIdA1, productIdA2, productIdB3);
        CouponApi.savePlatformCoupon(CouponData.getCouponDTO(1, 0, 1, 5000L, 720L, null, 2, 10, 100, 1, platformCouponProductIds));
        Long platformCouponId = CouponApi.getPlatformCouponPage().get(0).getCouponId();
        CouponApi.receive(platformCouponId);

        //修改积分配置，每15积分抵扣一元，最大使用积分比例30.7%
        ScoreConfigApi.update(ScoreConfigDtoData.getScoreConfigDTO(15L ,30.7));

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

        //将商品加入购物车
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA1, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA2, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA3, 3));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdA4, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB1, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB2, 1));
        ShopCartApi.addItem(ShopCartData.getShopCartItem(productIdB3, 1));

        //确认订单数据
        Long shopIdA = ProductApi.getProductInfo(productIdA1).getShopId();
        Long shopIdB = ProductApi.getProductInfo(productIdB1).getShopId();
        OrderDTO orderDTO = OrderDtoData.getOrderDtoData(Arrays.asList(shopIdA, shopIdB));
        //使用优惠券
        orderDTO.setCouponIds(Arrays.asList(CouponApi.getUserCouponPage().get(0).getCouponUserId(), CouponApi.getUserCouponPage().get(1).getCouponUserId()));
        //使用积分700
        orderDTO.setIsScorePay(1);
        orderDTO.setUserUseScore(700L);
        //确认订单
        MyOrderApi.confirm(orderDTO);
        //提交订单
        List<Long> orderNumberList = MyOrderApi.submit(SubmitOrderDtoData.getSubmitOrderDtoData(Arrays.asList(shopIdA, shopIdB)));
        //支付订单
        PayApi.orderPay(orderNumberList);

        DistributionProdApi.delete(distributionProdIdB1, false);
        DistributionProdApi.delete(distributionProdIdA1, true);
        UserRightsApi.delete(discountUserRightsId);
        CouponApi.deletePlatformCoupon(platformCouponId);
        CouponApi.deleteShopCoupon(shopCouponId);
        DiscountApi.delete(discountIdC, false);
        DiscountApi.delete(discountIdB, true);
        DiscountApi.delete(discountIdA, true);
        ProductApi.delete(productIdB3, false);
        ProductApi.delete(productIdB2, false);
        ProductApi.delete(productIdB1, false);
        ProductApi.delete(productIdA4, true);
        ProductApi.delete(productIdA3, true);
        ProductApi.delete(productIdA2, true);
        ProductApi.delete(productIdA1, true);
        CategoryApi.delete(categoryIdC);
        CategoryApi.delete(categoryIdB);
        CategoryApi.delete(categoryIdA);
        CategoryApi.delete(categoryIdSec);
        CategoryApi.delete(categoryIdFirst);

        //验证数据
        for(Long orderNum : orderNumberList) {
            if(MyOrderApi.orderDetail(orderNum).getShopId().equals(shopIdA)) {
                List<MongoShopWalletLogBO> mongoShopWalletLogBOList = ShopWalletApi.getMongoShopWalletLogBO(orderNum, true);
                for(MongoShopWalletLogBO mongoShopWalletLogBO : mongoShopWalletLogBOList) {
                    if(mongoShopWalletLogBO.getReason().equals(0)) {
                        WalletAssert.shopWalletLogAssert(mongoShopWalletLogBO, 0, 1, 9827L, 7382L, 2698L, 15264L, 9L, 253L, 0);
                    }
                }
                OrderDetailVO orderDetailVO = ShopWalletApi.getOrderDetailVO(orderNum, true);
                for (int i=0; i<orderDetailVO.getOrderItemDetailList().size(); i++) {
                    //商品a1结算明细验证
                    if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(6912L)) {
                        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 6912L, 471L, 0L, 6190L, 251L, 9L, 14L, 9.1, 66L,
                                93L, 56L, 102L, 1229L, 4961L, null, null, 0L, null, 1, null);
                    }
                    //商品a2结算明细验证
                    if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(7222L)) {
                        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 7222L, 1348L, 0L, 5164L, 710L, 0L, 39L, 9.1, 187L,
                                260L, 159L, 291L, 3503L, 1661L, null, null, 0L, null, 1, null);
                    }
                    //商品a3结算明细验证
                    if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(9210L)) {
                        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 9210L, 5405L, 0L, 2120L, 1685L, 0L, 157L, 0.0, 0L,
                                1047L, 638L, 0L, 2L, 2118L, null, null, 0L, null, 3, null);
                    }
                    //商品a4结算明细验证
                    if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(2000L)) {
                        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 2000L, 158L, 0L, 1790L, 52L, 0L, 5L, 0.0, 0L,
                                33L, 19L, 0L, 355L, 1435L, null, null, 0L, null, 1, null);
                    }
                }
            }
            if(MyOrderApi.orderDetail(orderNum).getShopId().equals(shopIdB)) {
                List<MongoShopWalletLogBO> mongoShopWalletLogBOList = ShopWalletApi.getMongoShopWalletLogBO(orderNum, false);
                for(MongoShopWalletLogBO mongoShopWalletLogBO : mongoShopWalletLogBOList) {
                    if(mongoShopWalletLogBO.getReason().equals(0)) {
                        WalletAssert.shopWalletLogAssert(mongoShopWalletLogBO, 0, 1, 21538L, 16678L, 5530L, 4996L, 410L, 670L, 0);
                    }
                }
                OrderDetailVO orderDetailVO = ShopWalletApi.getOrderDetailVO(orderNum, false);
                for (int i = 0; i < orderDetailVO.getOrderItemDetailList().size(); i++) {
                    //商品b1结算明细验证
                    if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(16912L)) {
                        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 16912L, 9668L, 0L, 4228L, 3016L, 410L, 281L, 0.1, 13L,
                                1874L, 1141L, 1L, 0L, 4228L, null, null, 0L, null, 1, null);
                    }
                    //商品b2结算明细验证
                    if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(7222L)) {
                        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 7222L, 5505L, 0L, 0L, 1717L, 0L, 160L, 9.1, 657L,
                                1067L, 650L, 0L, 0L, 0L, null, null, 0L, null, 1, null);
                    }
                    //商品b3结算明细验证
                    if (orderDetailVO.getOrderItemDetailList().get(i).getSpuTotalAmount().equals(3070L)) {
                        WalletAssert.shopOrderDetailAssert(orderDetailVO.getOrderItemDetailList().get(i), 3070L, 1505L, 0L, 768L, 797L, 0L, 44L, 0.0, 0L,
                                293L, 178L, 326L, 0L, 768L, null, null, 0L, null, 1, null);
                    }
                }
            }
        }
    }
}
