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.SfFaceOrderAddDTO;
import com.prolog.cs.biz.outbound.model.entity.WmsFaceOrder;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
import org.springframework.stereotype.Component;

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

/**
 * 顺丰快递
 */
@Component
public class SfFaceOrderHandler extends BaseFaceOrderHandler{

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

    @Override
    public List<WmsFaceOrder> convertResult(JSONObject result) {
        RestMessage restMessage = JSON.parseObject(result.toJSONString(), RestMessage.class);
        if(restMessage == null || !restMessage.isSuccess() || restMessage.getData() == null){
            throw new BizException("请求面单失败!");
        }

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

        Object data1 = restMessage.getData();
        JSONArray jsonArray = JSON.parseArray(JSON.toJSONString(data1));
        Object obj = jsonArray.get(0);
        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(obj));
        Object carrierSfResultInfo = jsonObject.get("carrierSfResultInfo");
        SfFaceOrderAddDTO dto = JSON.parseObject(JSON.toJSONString(carrierSfResultInfo),SfFaceOrderAddDTO.class);

        String createOrderInfo = dto.getCreateOrderInfo();
        JSONObject object = JSON.parseObject(createOrderInfo);
        if(object == null){
            object = JSON.parseObject(dto.getCreateOrderInfoRepeat());
        }
        String apiResultData = object.getString("apiResultData");
        JSONObject apiResultDataObj = JSON.parseObject(apiResultData);
        String msgData = apiResultDataObj.getString("msgData");
        JSONObject msgDataObj = JSON.parseObject(msgData);
        JSONArray waybillNoInfoList = msgDataObj.getJSONArray("waybillNoInfoList");

        for (Object o : waybillNoInfoList) {

            JSONObject waybillNoinfoObj = JSON.parseObject(JSON.toJSONString(o));

//            String waybillType = waybillNoinfoObj.getString("waybillType");
//            if("1".equals(waybillType)){
//                continue;
//            }

            WmsFaceOrder faceOrder = new WmsFaceOrder();
            faceOrder.setWaybillCode(waybillNoinfoObj.getString("waybillNo"));

            if(dto.getRouteList() != null && dto.getRouteList().size() > 0){
                SfFaceOrderAddDTO.RouteListBean routeListBean = dto.getRouteList().get(0);
                faceOrder.setProCode(routeListBean.getProCode());
                faceOrder.setPosition(routeListBean.getCodingMapping());
                faceOrder.setPositionNo(routeListBean.getDestRouteLabel());
                faceOrder.setQrCode(routeListBean.getTwoDimensionCode());
            }

            wmsFaceOrders.add(faceOrder);
        }

        return wmsFaceOrders;
    }
}
