package com.zm.order.tool.server.handler;

import com.alibaba.fastjson.JSON;
import com.zm.order.tool.server.dto.BasePerformanceDTO;
import com.zm.order.tool.server.dto.OrderPerformanceDTO;
import com.zm.order.tool.server.dto.QueryPerformanceDTO;
import com.zm.order.tool.server.enums.PerformanceTypeEnum;
import com.zm.order.tool.server.model.BakingIndexOrder;
import com.zm.order.tool.server.model.Order;
import com.zm.order.tool.server.model.RetailIndexOrder;
import com.zm.util.dto.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 订单同城配送处理器
 * 
 * @Author chang.n
 * @ClassName PerformanceDeliveryHandle
 * @Date 2020/4/14 2:50 PM
 */
@Component
public class PerformanceDispatchHandle implements RetailPerformanceHandler {

    private static final Logger logger = LoggerFactory.getLogger(PerformanceDispatchHandle.class);

    private static final String MOBILE = "mobile";

    private static final String ORDER_ADDRESS = "orderAddress";

    /**
     * 预约配送开始时间
     */
    private static final String DELIVERY_START_AT = "reserve_delivery_start_at";

    /**
     * 预约配送结束时间
     */
    private static final String DELIVERY_END_AT = "reserve_delivery_end_at";
    
    @Override
    public String performanceType() {
        return PerformanceTypeEnum.DELIVERY.getType().toString();
    }

    @Override
    public Result createPerformance(OrderPerformanceDTO orderPerformanceDTO) {
        return Result.successNullData();
    }

    @Override
    public Result operatePerformance(OrderPerformanceDTO orderPerformanceDTO, Order order) {
        return Result.successNullData();
    }

    @Override
    public Result<List<BasePerformanceDTO>> queryPerformance(QueryPerformanceDTO performanceDTO) {
        return Result.successNullData();
    }

    @Override
    public void buildPerformanceInfo(RetailIndexOrder indexOrder, Order order) {
        try {
            if (!StringUtils.isEmpty(order.getPerformanceInfo())) {
                String performanceInfo = order.getPerformanceInfo();
                Map<String, Object> performanceMap = JSON.parseObject(performanceInfo);
                if (Objects.nonNull(performanceMap.get(ORDER_ADDRESS))) {
                    Map<String, Object> orderAddressMap = JSON.parseObject(performanceMap.get(ORDER_ADDRESS).toString());
                    if (!StringUtils.isEmpty(orderAddressMap.get(MOBILE))) {
                        Object mobile = orderAddressMap.get(MOBILE);
                        indexOrder.setDeliveryMobile(mobile.toString());
                    }
                }
                if (!StringUtils.isEmpty(performanceMap.get(DELIVERY_START_AT))
                    && !StringUtils.isEmpty(performanceMap.get(DELIVERY_END_AT))) {
                    Object deliveryStartAt = performanceMap.get(DELIVERY_START_AT);
                    Object deliveryEndAt = performanceMap.get(DELIVERY_END_AT);
                    if (deliveryStartAt instanceof String && deliveryEndAt instanceof String) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                        indexOrder.setReserveDeliveryAtStart(sdf.parse(deliveryStartAt.toString()));
                        indexOrder.setReserveDeliveryAtEnd(sdf.parse(deliveryEndAt.toString()));
                    } else {
                        Long deliveryStart = Long.valueOf(deliveryStartAt.toString());
                        Long deliveryEnd = Long.valueOf(deliveryEndAt.toString());
                        indexOrder.setReserveDeliveryAtStart(new Date(deliveryStart));
                        indexOrder.setReserveDeliveryAtEnd(new Date(deliveryEnd));
                    }
                }
            }
        } catch (Exception e) {
            logger.error(
                "PerformanceDispatchHandle[buildBaseInsertInfo]get performanceInfo failed,request:{},cause by:{}",
                order, e);
            throw new RuntimeException("履约信息查询异常");
        }
    }

    @Override
    public void buildPerformanceIndex(BakingIndexOrder indexOrder, Order order) {
        if (!StringUtils.isEmpty(order.getPerformanceInfo())) {
            Map<String, Object> performanceMap = JSON.parseObject(order.getPerformanceInfo());
            if (!StringUtils.isEmpty(performanceMap.get(DELIVERY_START_AT))
                    && !StringUtils.isEmpty(performanceMap.get(DELIVERY_END_AT))) {
                Long deliveryStart = Long.valueOf(performanceMap.get(DELIVERY_START_AT).toString());
                Long deliveryEnd = Long.valueOf(performanceMap.get(DELIVERY_END_AT).toString());
                indexOrder.setReserveDeliveryAtStart(new Date(deliveryStart));
                indexOrder.setReserveDeliveryAtEnd(new Date(deliveryEnd));
            }
        }
    }
}
