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

import com.moyuer.cloud.delivery.dto.AreaDTO;
import com.moyuer.cloud.delivery.dto.TransfeeDTO;
import com.moyuer.cloud.delivery.dto.TransfeeFreeDTO;
import com.moyuer.cloud.delivery.dto.TransportDTO;
import com.moyuer.cloud.delivery.vo.TransportVO;
import com.moyuer.cloud.systemtest.api.store.TransportApi;

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

/**
 * @author Tu
 */
public class DeliveryData {

    /**
     * 获取包邮模板ID
     * @param isMainShop true 使用shop_token
     */
    public static Long getFreeFeeDelivery(Boolean isMainShop) {
        List<TransportVO> transportVOList = TransportApi.infoTransport(isMainShop);
        long count = transportVOList.stream().filter(t -> Objects.equals(t.getIsFreeFee(), 1)).count();

        if (count == 0 ||transportVOList.size() == 0) {
            TransportApi.addTransport(DeliveryData.getFreeTransportDTO(), isMainShop);
            return TransportApi.infoTransport(isMainShop).get(0).getTransportId();
        }
        for (TransportVO transportVO : transportVOList) {
            if (transportVO.getIsFreeFee() == 1) {
                return transportVO.getTransportId();
            }
        }
        return null;
    }

    /**
     * 供应商包邮ID
     * @param isMainSupplier true 使用supplier_token
     */
    public static Long getSupplierFreeFeeDelivery(Boolean isMainSupplier) {
        List<TransportVO> transportVOList = TransportApi.supplierTransportPage(isMainSupplier);
        long count = transportVOList.stream().filter(t -> Objects.equals(t.getIsFreeFee(), 1)).count();

        if (count == 0 ||transportVOList.size() == 0) {
            TransportApi.addSupplierTransport(DeliveryData.getFreeTransportDTO(), isMainSupplier);
            return TransportApi.supplierTransportPage(isMainSupplier).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;
    }
    /**
     * 广东可送，运费8
     * */
    public static TransportDTO getTransportDTO() {
        TransportDTO transportDTO = new TransportDTO();
        transportDTO.setTransName("广东可配送-自动化添加" + System.currentTimeMillis());
        transportDTO.setChargeType(0);
        transportDTO.setIsFreeFee(0);
        transportDTO.setHasFreeCondition(0);

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

        List<AreaDTO> cityList = new ArrayList<>();
        AreaDTO areaDTO = new AreaDTO();
        areaDTO.setAreaName("荔湾区");
        areaDTO.setAreaId(440103L);
        areaDTO.setParentId(4401L);
        areaDTO.setLevel(3);
        cityList.add(areaDTO);
        transfeeDTO.setCityList(cityList);

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

        return transportDTO;
    }

    /**
     * 满条件包邮
     * @param chargeType 收费方式 件数 0， 重量 1， 体积 2
     * @param freeType 包邮条件 件数/重量/体积 0， 金额 1， 件数/重量/体积+金额2
     * @param price 满件数/重量/体积的条件
     * @param amount 满金额的条件
     */
    public static TransportDTO getTransportDTO(Integer chargeType, Double firstPiece, Long firstFee, Double continuousPiece, Long continuousFee, Integer hasFreeCondition, Integer freeType, Double price, Long amount) {
        TransportDTO transportDTO = new TransportDTO();
        transportDTO.setTransName("可送荔湾-自动化-满条件包邮" + System.currentTimeMillis());
        transportDTO.setChargeType(chargeType);
        transportDTO.setIsFreeFee(0);
        transportDTO.setHasFreeCondition(hasFreeCondition);

        TransfeeDTO transfeeDTO = new TransfeeDTO();
        transfeeDTO.setFirstFee(firstFee);
        transfeeDTO.setFirstPiece(firstPiece);
        transfeeDTO.setContinuousFee(continuousFee);
        transfeeDTO.setContinuousPiece(continuousPiece);

        List<AreaDTO> cityList = new ArrayList<>();
        AreaDTO areaDTO = new AreaDTO();
        areaDTO.setAreaName("荔湾区");
        areaDTO.setAreaId(440103L);
        areaDTO.setParentId(4401L);
        areaDTO.setLevel(3);
        cityList.add(areaDTO);
        transfeeDTO.setCityList(cityList);

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

        List<TransfeeFreeDTO> transfeeFreeDtoList = new ArrayList<>();
        TransfeeFreeDTO transfeeFreeDto = new TransfeeFreeDTO();
        transfeeFreeDto.setFreeType(freeType);
        transfeeFreeDto.setPiece(price);
        transfeeFreeDto.setAmount(amount);
        transfeeFreeDto.setFreeCityList(cityList);

        transfeeFreeDtoList.add(transfeeFreeDto);
        transportDTO.setTransFeeFrees(transfeeFreeDtoList);

        return transportDTO;
    }
}
