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

import cn.hutool.core.collection.CollectionUtil;
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.YdKyFaceOrderInfo;
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.FaceOrderYDKYAddVO;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.utils.UUIDUtils;
import org.springframework.stereotype.Component;

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

@Component
public class YdKyFaceOrderHandler extends KyBaseFaceOrderHandler{
    @Override
    String generateCreateFaceOrderParam(OmsDoBillHDO omsDoBillHDO, List<FaceOrderPickTaskDDTO> list, List<WaveBillDDO> waveBillDDOS, Integer count) {
        String carrierCode = omsDoBillHDO.getCarrierCode();
        FaceOrderYDKYAddVO faceOrderYDKYAddVO = new FaceOrderYDKYAddVO();
        faceOrderYDKYAddVO.setCarrierCode("YUNDAKUAIYUN");


        List<FaceOrderYDKYAddVO.OrdersDTO> dtoList = new ArrayList<>();
        int index = 0;
        for (FaceOrderPickTaskDDTO ddto : list) {
            FaceOrderYDKYAddVO.OrdersDTO ordersDTO = new FaceOrderYDKYAddVO.OrdersDTO();
            String cargoName = ddto.getItemName();
            ordersDTO.setOrderNo(omsDoBillHDO.getBillno() + "_" + index++);
            ordersDTO.setSerialNo(UUIDUtils.generateWithoutSeperator());
            ordersDTO.setServiceType(1);
            ordersDTO.setProductType(1);
            ordersDTO.setDeliveryType(1);
            ordersDTO.setWeight(omsDoBillHDO.getTotWeight().toString());
            ordersDTO.setVolume(omsDoBillHDO.getTotVolume().toString());
            ordersDTO.setPieceAmount(ddto.getPcs());
            ordersDTO.setCargoName(cargoName);

            ordersDTO.setSenderName("青牛医药");
            ordersDTO.setSenderPhone("4009158120");
            ordersDTO.setSenderTel("4009158120");
            ordersDTO.setSenderDetailAddress("江西省赣州市章贡区经济技术开发区凤凰路7号");

            ordersDTO.setReceiverName(omsDoBillHDO.getCustPerson());
            ordersDTO.setReceiverPhone(omsDoBillHDO.getCustPersonTel());
            ordersDTO.setReceiverTel(omsDoBillHDO.getCustPersonTel());
            ordersDTO.setReceiverDetailAddress(omsDoBillHDO.getReceiveAddress());
            ordersDTO.setReceiverProvince(omsDoBillHDO.getReceiveProvinceName());
            ordersDTO.setReceiverCity(omsDoBillHDO.getReceiveCityName());
            ordersDTO.setReceiverStreet(omsDoBillHDO.getReceiveTownName());
            ordersDTO.setReceiverDetailAddress(omsDoBillHDO.getReceiveAddress());
            dtoList.add(ordersDTO);
        }
        faceOrderYDKYAddVO.setOrders(dtoList);
        return JSONObject.toJSONString(faceOrderYDKYAddVO);
    }

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

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

        YdKyFaceOrderInfo ydKyFaceOrderInfo = JSONObject.parseObject(result.toJSONString(), YdKyFaceOrderInfo.class);
        List<YdKyFaceOrderInfo.ResultInfoDTO> resultInfo = ydKyFaceOrderInfo.getResultInfo();
        for (YdKyFaceOrderInfo.ResultInfoDTO resultInfoDTO : resultInfo) {
            List<String> mailnoSub = resultInfoDTO.getMailnoSub();
            if (CollectionUtil.isEmpty(mailnoSub)) continue;
            for (String s : mailnoSub) {
                WmsFaceOrder faceOrder = new WmsFaceOrder();
                faceOrder.setWaybillCode(s);
                faceOrder.setPosition(resultInfoDTO.getReceiverSite());
                faceOrder.setPositionNo(resultInfoDTO.getReceiverSiteCode().toString());
                faceOrder.setParentWaybillCode(resultInfoDTO.getMailno());
                //faceOrder.setTpStatus(resultInfoDTO.getStatus());
                //faceOrder.setInnerProvinceName(pdf.getInnerProvinceName());
                //faceOrder.setPackageWdjc(pdf.getPackageWdjc());
                //faceOrder.setQrCode(pdf.getQrcode());

                wmsFaceOrders.add(faceOrder);
            }
        }
        return wmsFaceOrders;
    }
}
