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.OmsDoBillHDO;
import com.prolog.cs.biz.outbound.model.dto.FaceOrderPickTaskDDTO;
import com.prolog.cs.biz.outbound.model.dto.faceorder.DbFaceOrderAddDTO;
import com.prolog.cs.biz.outbound.model.dto.faceorder.SfFaceOrderAddDTO;
import com.prolog.cs.biz.outbound.model.entity.WaveBillDDO;
import com.prolog.cs.biz.outbound.model.entity.WmsFaceOrder;
import com.prolog.cs.biz.outbound.model.vo.faceorder.FaceOrderAddVO;
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 DbKyFaceOrderHandler extends KyBaseFaceOrderHandler{

    public String generateCreateFaceOrderParam(OmsDoBillHDO omsDoBillHDO, List<FaceOrderPickTaskDDTO> list, List<WaveBillDDO> waveBillDDOS,Integer count){
        FaceOrderAddVO faceOrderAddVO = new FaceOrderAddVO();
        String carrierCode = omsDoBillHDO.getCarrierCode();
        if ("DEBANGWULIU".equals(carrierCode)) {
            carrierCode = "DBL";
        }
        faceOrderAddVO.setCarrierCode(carrierCode);
        faceOrderAddVO.setPlatformNo(omsDoBillHDO.getBillno());
//        faceOrderAddVO.setPlatformNo("ZJ"+ ThreadLocalRandom.current().nextInt(100000));
        FaceOrderAddVO.SenderAddressBean senderAddressBean = new FaceOrderAddVO.SenderAddressBean();
        senderAddressBean.setProvince("江西省");
        senderAddressBean.setCity("赣州市");
        senderAddressBean.setArea("章贡区");
        senderAddressBean.setTown("凤凰路");
        senderAddressBean.setAddressDetail("江西省赣州市章贡区经济技术开发区凤凰路7号");
        senderAddressBean.setSenderName("青牛医药");
        senderAddressBean.setDelivererContact("4009158120");
        senderAddressBean.setDelivererContactTel("4009158120");
        faceOrderAddVO.setSenderAddress(senderAddressBean);

        FaceOrderAddVO.ReceiverAddressBean receiverAddressBean = new FaceOrderAddVO.ReceiverAddressBean();
        receiverAddressBean.setReceiveProvinceName(omsDoBillHDO.getReceiveProvinceName());
        receiverAddressBean.setReceiveCityName(omsDoBillHDO.getReceiveCityName());
        receiverAddressBean.setReceiveDistrictName(omsDoBillHDO.getReceiveDistrictName());
        receiverAddressBean.setReceiveTownName(omsDoBillHDO.getReceiveTownName());
        receiverAddressBean.setReceiveAddress(omsDoBillHDO.getReceiveAddress());
        receiverAddressBean.setReceiverName(omsDoBillHDO.getCustPerson());
        receiverAddressBean.setReceiveContact(omsDoBillHDO.getCustPersonTel());
        receiverAddressBean.setReceiveContactTel(omsDoBillHDO.getCustPersonTel());
        faceOrderAddVO.setReceiverAddress(receiverAddressBean);

        List<FaceOrderAddVO.CargoDetailListBean> cargoDetailListBeans = new ArrayList<>();
        for (WaveBillDDO waveBillDDO : waveBillDDOS) {
            FaceOrderAddVO.CargoDetailListBean cargoDetailListBean = new FaceOrderAddVO.CargoDetailListBean();
            cargoDetailListBean.setName(waveBillDDO.getItemName());
            cargoDetailListBean.setCount(count);

            cargoDetailListBeans.add(cargoDetailListBean);
        }

        faceOrderAddVO.setCargoDetailList(cargoDetailListBeans);
        faceOrderAddVO.setTransportType("PACKAGE");
        faceOrderAddVO.setOrderType("2");

        return JSONObject.toJSONString(faceOrderAddVO);
    }

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

    @Override
    public List<WmsFaceOrder> convertResult(JSONObject result,OmsDoBillHDO omsDoBillHDO) {
        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("carrierDbResultInfo");
        DbFaceOrderAddDTO dto = JSON.parseObject(JSON.toJSONString(carrierSfResultInfo),DbFaceOrderAddDTO.class);

        String[] waybillcodes = dto.getWayBillCode().split(",");
        for (String waybillcode : waybillcodes) {
            WmsFaceOrder faceOrder = new WmsFaceOrder();
            faceOrder.setWaybillCode(waybillcode);
            faceOrder.setParentWaybillCode(dto.getWayBillNo());

            wmsFaceOrders.add(faceOrder);
        }

        return wmsFaceOrders;
    }
}
