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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.tmerclub.cloud.common.product.vo.GiveawayVO;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.product.dto.GiveawayDTO;
import com.tmerclub.cloud.product.dto.GiveawaySpuDTO;
import com.moyuer.cloud.systemtest.api.amount.ProductApi;
import org.springframework.beans.BeanUtils;

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

/**
 * @author lpj
 */
public class GiveawayData {
    /**
     * 新增赠品数据
     */
    public static GiveawayDTO getGiveawayDTO(Long mainProductId, List<Long> matchingProductIds) {
        GiveawayDTO giveawayDTO = new GiveawayDTO();
        giveawayDTO.setName("测试赠品活动");
        giveawayDTO.setStartTime(DateUtil.parse("2023-1-1 00:00:00","yyyy-MM-dd HH:mm:ss"));
        giveawayDTO.setEndTime(DateUtil.parse("2026-1-1 00:00:00","yyyy-MM-dd HH:mm:ss"));
        giveawayDTO.setStatus(1);
        giveawayDTO.setSpuId(mainProductId);
        giveawayDTO.setBuyNum(3);
        giveawayDTO.setGiveawaySpuDTOs(getGiveawaySpuDTO(matchingProductIds));

        return giveawayDTO;
    }

    private static List<GiveawaySpuDTO> getGiveawaySpuDTO(List<Long> matchingProductIds) {
        List<GiveawaySpuDTO> giveawaySpuDTOList = new ArrayList<>();
        for (Long matchingProductId : matchingProductIds) {
            GiveawaySpuDTO giveawaySpuDTO = new GiveawaySpuDTO();
            SpuVO spu = ProductApi.info(matchingProductId, true);
            giveawaySpuDTO.setSpuId(spu.getSpuId());
            giveawaySpuDTO.setSpuName(spu.getName());
            giveawaySpuDTO.setRefundPrice(1100L);
            giveawaySpuDTO.setGiveawayNum(1);
            for (int i=0; i<spu.getSkus().size(); i++) {
                giveawaySpuDTO.setSkuId(spu.getSkus().get(i).getSkuId());
                giveawaySpuDTO.setSkuName(spu.getSkus().get(i).getSkuName());
                giveawaySpuDTOList.add(giveawaySpuDTO);
            }
        }

        return giveawaySpuDTOList;
    }

    /**
     * 修改赠品活动数据，修改购买数量条件为5
     */
    public static GiveawayDTO getUpdateGiveawayDTO(GiveawayVO giveawayVO) {
        GiveawayDTO giveawayDTO = new GiveawayDTO();
        BeanUtils.copyProperties(giveawayVO, giveawayDTO);
        giveawayDTO.setBuyNum(5);

        List<GiveawaySpuDTO> giveawaySpuDTOList = giveawayVO.getGiveawaySpus().stream().map(i -> BeanUtil.copyProperties(i, GiveawaySpuDTO.class)).collect(Collectors.toList());
        giveawayDTO.setGiveawaySpuDTOs(giveawaySpuDTOList);

        return giveawayDTO;
    }
}
