package com.tmerclub.cloud.systemtest.data.marketing;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.tmerclub.cloud.combo.vo.ComboVO;
import com.tmerclub.cloud.common.order.dto.DvyTypeDTO;
import com.tmerclub.cloud.common.order.dto.OrderDTO;
import com.tmerclub.cloud.common.order.dto.ShopCartItemDTO;
import com.tmerclub.cloud.common.product.dto.CartComboMatchSpuDTO;
import com.tmerclub.cloud.common.product.dto.ComboDTO;
import com.tmerclub.cloud.common.product.vo.ComboSpuSkuVO;
import com.tmerclub.cloud.common.product.vo.ComboSpuVO;
import com.tmerclub.cloud.common.product.vo.SkuVO;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.common.util.Arith;
import com.moyuer.cloud.systemtest.api.amount.ProductApi;
import com.moyuer.cloud.systemtest.api.marketing.ComboApi;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lpj
 */
public class ComboData {
    /**
     * 新增套餐数据
     */
    public static ComboDTO getComboDTO(Long mainSpuId, List<Long> matchingSpuIds) {
        ComboDTO comboDTO = new ComboDTO();
        comboDTO.setName("测试套餐");
        comboDTO.setStartTime(DateUtil.parse("2023-1-1 00:00:00","yyyy-MM-dd HH:mm:ss"));
        comboDTO.setEndTime(DateUtil.parse("2026-1-1 00:00:00","yyyy-MM-dd HH:mm:ss"));
        comboDTO.setMainSpu(getComboMainSpu(mainSpuId));
        comboDTO.setMatchingSpus(getMatchingComboSpu(matchingSpuIds));

        return comboDTO;
    }

    private static ComboSpuVO getComboMainSpu(Long mainSpuId) {
        ComboSpuVO mainSpu = new ComboSpuVO();
        SpuVO spu = ProductApi.info(mainSpuId, true);
        mainSpu.setSpuId(spu.getSpuId());
        mainSpu.setComboPrice(6600L);
        mainSpu.setSpuName(spu.getName());
        mainSpu.setSpuStatus(spu.getStatus());
        mainSpu.setLeastNum(2);
        mainSpu.setPic("/test.png");
        mainSpu.setRequired(0);

        List<SkuVO> skuList = spu.getSkus();
        List<ComboSpuSkuVO> comboSpuSkuVOList = skuList.stream().map(i -> BeanUtil.copyProperties(i, ComboSpuSkuVO.class)).collect(Collectors.toList());
        comboSpuSkuVOList.forEach(i->i.setMatchingPrice(6600L));
        mainSpu.setSkuList(comboSpuSkuVOList);

        return mainSpu;
    }

    private static List<ComboSpuVO> getMatchingComboSpu(List<Long> matchingSpuIds) {
        List<ComboSpuVO> matchingSpus = new ArrayList<>();
        for (Long matchingSpuId : matchingSpuIds) {
            ComboSpuVO matchingSpu = new ComboSpuVO();
            SpuVO spu = ProductApi.info(matchingSpuId, true);
            matchingSpu.setSpuId(spu.getSpuId());
            matchingSpu.setComboPrice(6600L);
            matchingSpu.setSpuName(spu.getName());
            matchingSpu.setSpuStatus(spu.getStatus());
            matchingSpu.setLeastNum(2);
            matchingSpu.setPic("/test.png");
            matchingSpu.setRequired(0);

            List<SkuVO> skuList = spu.getSkus();
            List<ComboSpuSkuVO> comboSpuSkuVOList = skuList.stream().map(i -> BeanUtil.copyProperties(i, ComboSpuSkuVO.class)).collect(Collectors.toList());
            comboSpuSkuVOList.forEach(i->i.setMatchingPrice(6600L));
            matchingSpu.setSkuList(comboSpuSkuVOList);
            matchingSpus.add(matchingSpu);
        }

        return matchingSpus;
    }

    /**
     * 修改套餐状态数据
     */
    public static ComboDTO getUpdateStatusData(Long comboId, Integer status) {
        ComboDTO comboDTO = new ComboDTO();
        comboDTO.setComboId(comboId);
        comboDTO.setStatus(status);

        return comboDTO;
    }

    /**
     * 修改套餐数据
     */
    public static ComboDTO getUpdateComboDTO(ComboVO comboVO) {
        ComboDTO comboDTO = new ComboDTO();
        BeanUtils.copyProperties(comboVO, comboDTO);

        ComboSpuVO mainSpu = new ComboSpuVO();
        BeanUtils.copyProperties(comboVO.getMainSpu(), mainSpu);

        List<ComboSpuSkuVO> comboSpuSkuVOList = comboVO.getMainSpu().getSkuList().stream().map(i -> BeanUtil.copyProperties(i, ComboSpuSkuVO.class)).collect(Collectors.toList());
        mainSpu.setSkuList(comboSpuSkuVOList);
        comboDTO.setMainSpu(mainSpu);

        comboDTO.setMatchingSpus(new ArrayList<>());
        return comboDTO;
    }

    /**
     * 套餐下单数据
     */
    public static OrderDTO getComboOrderDTO(Long comboId,Integer comboCount) {
        OrderDTO orderDTO = new OrderDTO();
        ComboVO combo = ComboApi.getComboInfo(comboId);
        orderDTO.setAddrId(0L);
        orderDTO.setCouponIds(new ArrayList<>());

        DvyTypeDTO dvyTypeDTO = new DvyTypeDTO();
        dvyTypeDTO.setDvyType(1);
        dvyTypeDTO.setShopId(combo.getShopId());
        ArrayList<DvyTypeDTO> dvyTypeDTOList = new ArrayList<>();
        dvyTypeDTOList.add(dvyTypeDTO);
        orderDTO.setDvyTypes(dvyTypeDTOList);

        orderDTO.setIsScorePay(0);
        orderDTO.setUserChangeCoupon(0);
        orderDTO.setUserUseScore(0L);

        ShopCartItemDTO shopCartItemDTO = new ShopCartItemDTO();
        shopCartItemDTO.setComboId(comboId);
        shopCartItemDTO.setComboMainLeastNum(combo.getMainSpu().getLeastNum());
        shopCartItemDTO.setComboCount(comboCount);
        shopCartItemDTO.setCount(comboCount);
        shopCartItemDTO.setShopId(combo.getShopId());
        shopCartItemDTO.setSkuId(combo.getMainSpu().getSkuList().get(0).getSkuId());
        shopCartItemDTO.setSpuId(combo.getMainSpuId());

        List<CartComboMatchSpuDTO> cartComboMatchSpuDTOList = new ArrayList<>();
        for (com.tmerclub.cloud.combo.vo.ComboSpuVO comboSpuVO: combo.getMatchingSpus()) {
            CartComboMatchSpuDTO cartComboMatchSpuDTO = new CartComboMatchSpuDTO();
            cartComboMatchSpuDTO.setCount((int) Arith.mul(comboSpuVO.getLeastNum(), comboCount));
            cartComboMatchSpuDTO.setSkuId(comboSpuVO.getSkuList().get(0).getSkuId());
            cartComboMatchSpuDTO.setSpuId(comboSpuVO.getSpuId());
            cartComboMatchSpuDTOList.add(cartComboMatchSpuDTO);
        }
        shopCartItemDTO.setCartComboMatchSpuDTOS(cartComboMatchSpuDTOList);

        orderDTO.setShopCartItem(shopCartItemDTO);

        return orderDTO;
    }
}
