package com.prolog.rdc.tms.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.prolog.rdc.tms.model.dto.basicConfigure.ShipbillMatchLineDTO;
import com.prolog.rdc.tms.model.entity.TmsAppointmentDetailModel;
import com.prolog.rdc.tms.model.entity.TmsShipbillHeaderModel;
import com.prolog.rdc.tms.model.vo.intransit.InTransitQueryVO;

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

/**
 * @author hongbo.pan
 * @date 2022/9/27
 */
public class DataUtils {

    private DataUtils() {}

    /**
     * 拼接预约单明细的派车单
     * @param tmsAppointmentDetailModelList
     * @return
     */
    public static String getLoadingBillIds(List<TmsAppointmentDetailModel> tmsAppointmentDetailModelList) {
        if (CollectionUtil.isNotEmpty(tmsAppointmentDetailModelList)) {
            return tmsAppointmentDetailModelList.stream().map(TmsAppointmentDetailModel::getLoadingBillId)
                    .filter(StrUtil::isNotBlank).distinct().collect(Collectors.joining(","));
        }
        return StrUtil.EMPTY;
    }

    /**
     * setBillIdAndNo
     * @param vo
     */
    public static void setBillIdAndNo(InTransitQueryVO vo) {
        if (StrUtil.isNotBlank(vo.getShipBillId())) {
            vo.setShipBillNo(vo.getShipBillId());
        }
        if (StrUtil.isNotBlank(vo.getShipBillNo())) {
            vo.setShipBillId(vo.getShipBillNo());
        }
        if (StrUtil.isNotBlank(vo.getLoadingBillId())) {
            vo.setLoadingBillNo(vo.getLoadingBillId());
        }
        if (StrUtil.isNotBlank(vo.getLoadingBillNo())) {
            vo.setLoadingBillId(vo.getLoadingBillNo());
        }
    }

    /**
     * setEstimatedTime
     * @param model
     * @param dto
     * @param operateType
     * @return
     */
    public static TmsShipbillHeaderModel setEstimatedTime(TmsShipbillHeaderModel model, ShipbillMatchLineDTO dto, String operateType) {
        if (StrUtil.isNotBlank(dto.getShipperAgingTimeLine()) && StrUtil.isNotBlank(dto.getShipperAgingTime()) && operateType.contains(dto.getShipperAgingTimeLine().substring(0, 1))) {
            Date estimatedPickupTime = null;
            if ("R".equals(dto.getShipperAgingTimeLine().substring(0, 1)) && ObjectUtil.isNotNull(model.getOrderDate())) {
                estimatedPickupTime = DateUtil.parseDateTime(DateUtil.formatDate(DateUtil.offsetDay(model.getOrderDate(), Integer.valueOf(dto.getShipperAgingTimeLine().substring(1, 2)))) + " " + dto.getShipperAgingTime());
            }
            if (!"R".equals(dto.getShipperAgingTimeLine().substring(0, 1))) {
                estimatedPickupTime = DateUtil.parseDateTime(DateUtil.formatDate(DateUtil.offsetDay(DateUtil.date(), Integer.valueOf(dto.getShipperAgingTimeLine().substring(1, 2)))) + " " + dto.getShipperAgingTime());
            }
            model.setEstimatedPickupStartTime(estimatedPickupTime);
            model.setEstimatedPickupEndTime(estimatedPickupTime);
        }
        if (StrUtil.isNotBlank(dto.getConsigneeAgingTimeLine()) && StrUtil.isNotBlank(dto.getConsigneeAgingTime()) && operateType.contains(dto.getConsigneeAgingTimeLine().substring(0, 1))) {
            Date estimatedDeliveryTime = null;
            if ("R".equals(dto.getConsigneeAgingTimeLine().substring(0, 1)) && ObjectUtil.isNotNull(model.getOrderDate())) {
                estimatedDeliveryTime = DateUtil.parseDateTime(DateUtil.formatDate(DateUtil.offsetDay(model.getOrderDate(), Integer.valueOf(dto.getConsigneeAgingTimeLine().substring(1, 2)))) + " " + dto.getConsigneeAgingTime());
            }
            if (!"R".equals(dto.getConsigneeAgingTimeLine().substring(0, 1))) {
                estimatedDeliveryTime = DateUtil.parseDateTime(DateUtil.formatDate(DateUtil.offsetDay(DateUtil.date(), Integer.valueOf(dto.getConsigneeAgingTimeLine().substring(1, 2)))) + " " + dto.getConsigneeAgingTime());
            }
            model.setEstimatedDeliveryStartTime(estimatedDeliveryTime);
            model.setEstimatedDeliveryEndTime(estimatedDeliveryTime);
        }
        return model;
    }
}
