package com.clx.performance.extranal.broker.impl;

import com.alibaba.fastjson.JSON;
import com.clx.performance.config.SettlementConfig;
import com.clx.performance.enums.ResultEnum;
import com.clx.performance.extranal.broker.BrokerService;
import com.clx.performance.feign.BrokerFeign;
import com.clx.performance.vo.pc.broker.TransportParam;
import com.clx.performance.vo.pc.broker.TransportVO;
import com.msl.common.exception.ServiceSystemException;
import com.msl.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class BrokerServiceImpl implements BrokerService {
    @Autowired
    public BrokerFeign brokerFeign;
    @Autowired
    private SettlementConfig settlementConfig;
    @Override
    public void orderChildSync(TransportParam param) {

        param.setUserCode(settlementConfig.getBrokerUserCode());

        Result<Object> result = brokerFeign.saveTransport(param);
        log.info("运单同步网络货运, param:{}", JSON.toJSONString(param));
        if (result.getCode() != 0) {
            log.info("运单同步网络货运失败, childNo:{}, msg:{}", param.getOrderChild().getSourceChildNo(), result.getMsg());
            throw new ServiceSystemException(ResultEnum.FEIGN_ERROR, result.getMsg());
        }
        log.info("运单同步网络货运, childNo:{}, result:{}", param.getOrderChild().getSourceChildNo(), JSON.toJSONString(result));
    }

    @Override
    public void orderChildUpdate(TransportParam param) {
        param.setUserCode(settlementConfig.getBrokerUserCode());

        Result<Object> result = brokerFeign.modifyTransport(param);
        if (result.getCode() != 0) {
            log.info("运单更新网络货运失败, childNo:{}, msg:{}", param.getOrderChild().getSourceChildNo(), result.getMsg());
            throw new ServiceSystemException(ResultEnum.FEIGN_ERROR, result.getMsg());
        }
        log.info("运单更新网络货运, childNo:{}, result:{}", param.getOrderChild().getSourceChildNo(), JSON.toJSONString(result));
    }

    @Override
    public void orderChildPay(String childNo) {

        Result<Object> result = brokerFeign.payConfirm(childNo, settlementConfig.getBrokerUserCode());
        if (result.getCode() != 0) {
            log.info("司机支付, childNo:{}, msg:{}", childNo, result.getMsg());
        }
        log.info("司机支付, childNo:{}, result:{}", childNo, JSON.toJSONString(result));

    }

    @Override
    public TransportVO getOrderChildStatus(String childNo) {

        return getOrderChildStatus(childNo, null);

    }
    @Override
    public TransportVO getOrderChildStatus(String childNo, String settlementNo) {

        Result<TransportVO> result = brokerFeign.getTransportByChildNo(childNo, settlementConfig.getBrokerUserCode(), settlementNo);
        if (result.getCode() != 0) {
            log.info("获取网络货运运单状态, childNo:{}, msg:{}", childNo, result.getMsg());
        }
        log.info("获取网络货运运单状态, childNo:{}, result:{}", childNo, JSON.toJSONString(result));

        return result.getData();
    }

}
