package com.bohuikeji.delivery.backstage.util;

import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @author ：mmzs
 * @date ：Created in 2021/3/8 12:18
 * @description：订单处理工具类
 * @modified By：
 * @version: 1$
 */
@Slf4j
public class OrderUtils {


    /**
     * 判断订单是否为急件
     *
     * @param order
     * @return
     */
    public static boolean isUrgent(TOrder order) {
        String way = order.getWayName();
        String[] was = {"极速达", "一对一", "一小时内", "急件","极送-平均1小时","直送(一对一)","小公主配送A组"};
        for (String wa : was) {
            if (way.contains(wa)) {
                return true;
            }
        }
        return false;
    }
    /**
     * 获取剩余取件时间
     * @param order
     * @return
     */
    public static LocalDateTime getSurplusTime(TOrder order) {
        String orderPickStr=order.getPickTime();
        LocalDateTime now=LocalDateTime.now().plusMinutes(5);
        if("今天/2小时内".equals(orderPickStr)){
            return now;
        }
        if("今天/一小时内".equals(orderPickStr)){
            return now;
        }
        if("今天/半小时内".equals(orderPickStr)){
            return now;
        }
        if("今天/立即取件".equals(orderPickStr)){
            return now;
        }
        LocalDate date=LocalDate.now();
        if (orderPickStr.contains("明天")){
            date=date.plusDays(1);
        }
        else if(orderPickStr.contains("后天")){
            date=date.plusDays(2);
        }
        if (orderPickStr.length()<14){
            String[] nums= StringUtils.strGetNum(orderPickStr).split(";");
            for (int i = 0; i < nums.length; i++) {
                if (nums[i].length()<2){
                    nums[i]=0+nums[i];
                }
            }
            return LocalDateTime.of(date,LocalTime.parse(nums[2]+":"+nums[3]+":00"));
        }
        return LocalDateTime.of(date,LocalTime.parse(orderPickStr.substring(9,14)+":00"));
    }

    /**
     * 计算订单取件的时间区间
     * @param orderPickStr 订单取件时间字段文本
     * @return 返回时间数组，长度=2，index:0开始时间;index:1结束时间
     */
    public static LocalDateTime[] getOrSurplusTimeArea(String orderPickStr){
        LocalDateTime now=LocalDateTime.now().plusMinutes(5);
        LocalDateTime[] res={now,null};
        if("今天/2小时内".equals(orderPickStr)){
            res[1]=now.plusHours(2);
            return res;
        }
        if("今天/一小时内".equals(orderPickStr)){
            res[1]=now.plusHours(1);
            return res;
        }
        if("今天/半小时内".equals(orderPickStr)){
            res[1]=now.plusMinutes(30);
            return res;
        }
        if("今天/立即取件".equals(orderPickStr)){
            res[1]=now.plusMinutes(30);
            return res;
        }
        LocalDate date=LocalDate.now();
        if (orderPickStr.contains("明天")){
            date=date.plusDays(1);
        }
        else if(orderPickStr.contains("后天")){
            date=date.plusDays(2);
        }
        String[] nums = StringUtils.strGetNum(orderPickStr).split(";");
        if (orderPickStr.length()<14) {
            for (int i = 0; i < nums.length; i++) {
                if (nums[i].length()<2){
                    nums[i]=0+nums[i];
                }
            }
        }
        res[0]=LocalDateTime.of(date,LocalTime.parse(nums[0]+":"+nums[1]+":00"));
        res[1]=LocalDateTime.of(date,LocalTime.parse(nums[2]+":"+nums[3]+":00"));
        if (res[0].compareTo(now)<1){
            res[0]=now.plusMinutes(5);
        }
        if (res[1].compareTo(now)<1){
            res[1]=now.plusMinutes(35);
        }
        return res;
    }
}
