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

import com.moyuer.cloud.delivery.dto.AreaDTO;
import com.moyuer.cloud.delivery.dto.TransfeeDTO;
import com.moyuer.cloud.delivery.dto.TransportDTO;
import com.moyuer.cloud.delivery.vo.TransportVO;
import com.moyuer.cloud.systemtest.api.platform.PlatformTransportApi;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author lpj
 */
public class PlatformDeliveryData {
    /**
     * 平台端获取包邮模板ID
     */
    public static Long getPlatformFreeFeeDelivery() {
        List<TransportVO> transportVOList = PlatformTransportApi.platformInfoTransport();
        long count = transportVOList.stream().filter(t -> Objects.equals(t.getIsFreeFee(), 1)).count();

        if (count == 0 ||transportVOList.size() == 0) {
            PlatformTransportApi.platformAddTransport(getFreeTransportDTO());
            return PlatformTransportApi.platformInfoTransport().get(0).getTransportId();
        }
        for (TransportVO transportVO : transportVOList) {
            if (transportVO.getIsFreeFee() == 1) {
                return transportVO.getTransportId();
            }
        }
        return null;
    }

    /**
     *新增包邮模板
     * */
    public static TransportDTO getFreeTransportDTO() {
        TransportDTO transportDTO = new TransportDTO();
        transportDTO.setTransName("包邮模板");
        transportDTO.setChargeType(0);
        transportDTO.setIsFreeFee(1);
        transportDTO.setHasFreeCondition(0);

        TransfeeDTO transfeeDTO = new TransfeeDTO();
        transfeeDTO.setFirstFee(0L);
        transfeeDTO.setFirstPiece(1D);
        transfeeDTO.setContinuousFee(0L);
        transfeeDTO.setContinuousPiece(1D);

        ArrayList<AreaDTO> cityList = new ArrayList<>();
        AreaDTO areaDTO = new AreaDTO();
        cityList.add(areaDTO);
        transfeeDTO.setCityList(cityList);

        List<TransfeeDTO> transfeeDTOList = new ArrayList<>();
        transfeeDTOList.add(transfeeDTO);
        transportDTO.setTransFees(transfeeDTOList);
        return transportDTO;
    }
}
