package com.dgtly.wxportal.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dgtly.common.exception.base.BaseException;
import com.dgtly.wxportal.domain.EReceivingNote;
import com.dgtly.wxportal.domain.OrderSelfNote;
import com.dgtly.wxportal.service.IEReceivingNoteService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Formatter;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author qxp
 * @since 2020-07-30
 */
@Slf4j
@Service
public class EReceivingNoteServiceImpl implements IEReceivingNoteService {

    private static final Logger logger = LoggerFactory.getLogger(EReceivingNoteServiceImpl.class);
    @Autowired
    private RestTemplate restTemplate;


    @Value(value = "${ruoyi.cloudPath}")
    private String cloudPath;


    //确认收货接口
//    private String confirmUrl="order/order/deliver-quantity-confirm/confirmCollectQuery";
    private String confirmUrl="order//order/tms-logistics-status/tmsShipNumberConfirmCollectQuery";


    @Override
    public EReceivingNote selectEReceivingNote(String orderId,String tmsShipNumber,String customerCode
                                                                 ,String customerName,String userName) {
        String url = cloudPath +confirmUrl;
        String queryStr = "?tmsShipNumber=%s&orderNumber=%s&customerCode=%s";
        queryStr = new Formatter().format(queryStr,tmsShipNumber,orderId,customerCode).toString();
        url = url+queryStr;
        String str= "";
        EReceivingNote receivingNote ;

        try{
            str = restTemplate.getForObject(url, String.class);
            log.info("str ："+str);
            JSONObject json = JSONObject.parseObject(str);
            if(json.getInteger("code")==200){
                 receivingNote = json.getObject("data",EReceivingNote.class);
                receivingNote.setCustomerName(customerName);
                receivingNote.setAgent(userName);
                receivingNote.setOrderNumber(orderId);
                receivingNote.setShipmentNumber(tmsShipNumber);
            }else{
                throw new BaseException("order",json.getString("message"));
            }
        }catch(Exception e){
            logger.error("查询基础订单详情报错"+url,e);
            throw e;
        }

        return receivingNote;
    }




}
