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.YDPdfInfo;
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 org.springframework.stereotype.Component;

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

/**
 * 韵达快递
 */
@Component
public class YdFaceOrderHandler extends BaseFaceOrderHandler{

    @Override
    String getUrl() {
        BaseCommonDict baseCommonDict = getRequestUrls().stream().filter(item -> "YD".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 carrierYdResultInfo = jsonObject.get("carrierYdResultInfo");
        YdFaceOrderAddDTO dto = JSON.parseObject(JSON.toJSONString(carrierYdResultInfo),YdFaceOrderAddDTO.class);
        String createOrderInfo = dto.getCreateOrderInfo();
        JSONObject object = JSON.parseObject(createOrderInfo);
        JSONArray jsonArray1 = object.getJSONArray("data");
        Object o1 = jsonArray1.get(0);
        JSONObject jsonObject1 = JSON.parseObject(JSON.toJSONString(o1));
        Object pdf_info = jsonObject1.get("pdf_info");
        JSONArray pdfArray = JSON.parseArray(pdf_info.toString());
        String pdfArrayOne = pdfArray.getString(0);
        JSONArray jsonArray2 = JSONObject.parseObject(pdfArrayOne, JSONArray.class);
        String jsonData = jsonArray2.getString(0);
        YDPdfInfo pdf = JSONObject.parseObject(jsonData, YDPdfInfo.class);

        WmsFaceOrder faceOrder = new WmsFaceOrder();
        faceOrder.setWaybillCode(dto.getWayBillNo());
        faceOrder.setPosition(pdf.getPosition());
        faceOrder.setPositionNo(pdf.getPositionNo());
        faceOrder.setTpStatus(pdf.getStatus());
        faceOrder.setInnerProvinceName(pdf.getInnerProvinceName());
        faceOrder.setPackageWdjc(pdf.getPackageWdjc());
        faceOrder.setQrCode(pdf.getQrcode());

        wmsFaceOrders.add(faceOrder);


        return wmsFaceOrders;
    }
}
