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.JdFaceOrderAddDTO;
import com.prolog.cs.biz.outbound.model.dto.faceorder.JdFaceOrderInfo;
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 JdFaceOrderHandler extends BaseFaceOrderHandler{

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

    @Override
    public List<WmsFaceOrder> convertResult(JSONObject result) {
        RestMessage<JdFaceOrderAddDTO> 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 carrierJdResultInfo = jsonObject.get("carrierJdResultInfo");

        JdFaceOrderAddDTO dto = JSON.parseObject(JSON.toJSONString(carrierJdResultInfo),JdFaceOrderAddDTO.class);
        String createOrderInfo = dto.getGetPlainTextInfo();
        JSONObject object = JSON.parseObject(createOrderInfo);
        JSONArray jsonArray1 = object.getJSONArray("printDatas");

        for (Object o : jsonArray1) {
            JSONObject jsonObject1 = JSON.parseObject(JSON.toJSONString(o));
            String printData = jsonObject1.getString("printData");
            JdFaceOrderInfo jdFaceOrderInfo = JSONObject.parseObject(printData, JdFaceOrderInfo.class);


            WmsFaceOrder faceOrder = new WmsFaceOrder();
            faceOrder.setWaybillCode(jdFaceOrderInfo.getPackageCode());
            faceOrder.setTransportMode(jdFaceOrderInfo.getTransportMode());
            faceOrder.setTransportType(jdFaceOrderInfo.getTransportType());
            faceOrder.setFreshType(jdFaceOrderInfo.getFreshType());
            faceOrder.setPackageCode(jdFaceOrderInfo.getWaybillCode());
            faceOrder.setPackageCodeSuffix(jdFaceOrderInfo.getPackageCodeSuffix());
            faceOrder.setSourceSortCenterName(jdFaceOrderInfo.getSourceSortCenterName());
            faceOrder.setTargetSortCenterName(jdFaceOrderInfo.getTargetSortCenterName());
            faceOrder.setSourceCrossCodeSourceTabletrolleyCode(jdFaceOrderInfo.getSourceTabletrolleyCode());
            faceOrder.setTargetCrossCodeSourceTabletrolleyCode(jdFaceOrderInfo.getTargetTabletrolleyCode());
            faceOrder.setPreSortSiteName(jdFaceOrderInfo.getPreSortSiteName());

            wmsFaceOrders.add(faceOrder);
        }

        return wmsFaceOrders;
    }
}
