package com.prolog.cs.biz.outbound.service.impl.faceorder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.prolog.cs.basic.common.model.BaseCommonDict;
import com.prolog.cs.biz.outbound.model.dto.faceorder.EmsFaceOrderAddDTO;
import com.prolog.cs.biz.outbound.model.dto.faceorder.YdFaceOrderAddDTO;
import com.prolog.cs.biz.outbound.model.entity.WmsFaceOrder;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

/**
 * ems快递
 */
@Slf4j
@Component
public class EmsFaceOrderHandler extends BaseFaceOrderHandler{

    @Override
    String getUrl() {
        BaseCommonDict baseCommonDict = getRequestUrls().stream().filter(item -> "EMS".equals(item.getDictCode())).findFirst().orElse(null);
        if(baseCommonDict == null){
            throw new BizException("请配置EMS快递创建面单请求地址！");
        }
        return baseCommonDict.getDictName();
    }

    @Override
    public List<WmsFaceOrder> convertResult(JSONObject result) {
        Boolean success = (Boolean) result.get("success");
        if(!success){
            throw new BizException("请求面单失败!");
        }

        JSONArray jsonArray = result.getJSONArray("data");
        if(jsonArray == null || jsonArray.size() <= 0){
            throw new BizException("请求面单数据失败");
        }
        String jsonString = JSONArray.toJSONString(jsonArray);
        List<EmsFaceOrderAddDTO> emsFaceOrderAddDTOS = JSONArray.parseArray(jsonString, EmsFaceOrderAddDTO.class);
        log.info("解析EMS请求面单返回结果：{}",JSON.toJSONString(emsFaceOrderAddDTOS));

        List<WmsFaceOrder> wmsFaceOrders = new ArrayList<>();

//        JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(data1));
//        Object obj = jsonArray.get(0);
//        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(obj));
//        Object carrierEmsResultInfo = jsonObject.get("carrierEmsResultInfo");
//
//        EmsFaceOrderAddDTO dto = JSON.parseObject(JSON.toJSONString(carrierEmsResultInfo),EmsFaceOrderAddDTO.class);
//        dto.getWayBillNo()
//        String wayBillNo = dto.getWayBillNo();
//        String[] split = wayBillNo.split(",");
//        for (int i = 0; i < split.length; i++) {
//            if(i == 0){
//                continue;
//            }
//            WmsFaceOrder faceOrder = new WmsFaceOrder();
//            faceOrder.setWaybillCode(split[i]);
//
//            wmsFaceOrders.add(faceOrder);
//        }



        for (EmsFaceOrderAddDTO emsFaceOrderAddDTO : emsFaceOrderAddDTOS) {
            String wayBillNo = emsFaceOrderAddDTO.getWayBillNo();
            String[] split = wayBillNo.split(",");
            for (int i = 0; i < split.length; i++) {
//                if(i == 0){
//                    continue;
//                }
                WmsFaceOrder faceOrder = new WmsFaceOrder();
                faceOrder.setWaybillCode(split[i]);
//                faceOrder.setParentWaybillCode(split[0]);
                wmsFaceOrders.add(faceOrder);
            }
        }
        return wmsFaceOrders;
    }
}
