package com.fs.hisStore.task;

import com.fs.erp.domain.ErpOrder;
import com.fs.erp.domain.FsErpFinishPush;
import com.fs.erp.dto.ErpOrderResponse;
import com.fs.erp.mapper.FsErpFinishPushMapper;
import com.fs.erp.service.IErpOrderService;
import com.fs.hisStore.domain.FsStoreOrderScrm;
import com.fs.hisStore.service.IFsStoreOrderScrmService;
import com.fs.live.domain.LiveOrder;
import com.fs.live.service.ILiveOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("erpTask")
@Slf4j
public class ErpTask {

    @Autowired
    private FsErpFinishPushMapper fsErpFinishPushMapper;
    @Autowired
    private IErpOrderService erpOrderService;

    @Autowired
    private IFsStoreOrderScrmService fsStoreOrderService;

    @Autowired
    private ILiveOrderService liveOrderService;


    /**
     * 推送完成订单到ERP
     */
    public void pushFinishOrderToErp(){
        List<FsErpFinishPush> fsErpFinishPushes = fsErpFinishPushMapper.queryPenddingOrder();
        for (FsErpFinishPush fsErpFinishPush : fsErpFinishPushes) {
            FsStoreOrderScrm fsStoreOrder = fsStoreOrderService.selectFsStoreOrderById(fsErpFinishPush.getOrderId());
            if (fsStoreOrder != null) {
                try {

                    ErpOrder erpOrder = fsStoreOrderService.getErpOrder(fsStoreOrder);

                    ErpOrderResponse erpOrderResponse = erpOrderService.finishOrder(erpOrder);

                    fsErpFinishPush.setParams(erpOrderResponse.getRequestRawData());
                    fsErpFinishPush.setResult(erpOrderResponse.getResponseRawData());
                    fsErpFinishPush.setUpdateTime(new Date());

                    if(erpOrderResponse.getSuccess()!= null && erpOrderResponse.getSuccess()){
                        fsErpFinishPush.setTaskStatus(1);

                        log.error("推送完成订单到ERP成功! 订单号: {}",fsErpFinishPush.getOrderId());
                    } else {
                        fsErpFinishPush.setTaskStatus(2);
                        fsErpFinishPush.setRetryCount(fsErpFinishPush.getRetryCount()+1);
                        log.error("推送完成订单到ERP失败! 订单号: {}",fsErpFinishPush.getOrderId());
                    }
                } catch (Throwable e) {
                    fsErpFinishPush.setRetryCount(fsErpFinishPush.getRetryCount()+1);
                    fsErpFinishPush.setErrorMessage(ExceptionUtils.getStackTrace(e));
                    fsErpFinishPush.setTaskStatus(2);
                    log.error("订单推送失败!原因: {}", ExceptionUtils.getStackTrace(e),e);
                    continue;
                }
            } else {
                LiveOrder liveOrder = liveOrderService.selectLiveOrderByOrderId(String.valueOf(fsErpFinishPush.getOrderId()));
                if (liveOrder != null) {
                    try {
                        ErpOrder erpOrder = liveOrderService.getErpOrder(liveOrder);

                        ErpOrderResponse erpOrderResponse = erpOrderService.finishOrder(erpOrder);

                        fsErpFinishPush.setParams(erpOrderResponse.getRequestRawData());
                        fsErpFinishPush.setResult(erpOrderResponse.getResponseRawData());
                        fsErpFinishPush.setUpdateTime(new Date());

                        if (erpOrderResponse.getSuccess() != null && erpOrderResponse.getSuccess()) {
                            fsErpFinishPush.setTaskStatus(1);

                            log.error("推送完成订单到ERP成功! 订单号: {}", fsErpFinishPush.getOrderId());
                        } else {
                            fsErpFinishPush.setTaskStatus(2);
                            fsErpFinishPush.setRetryCount(fsErpFinishPush.getRetryCount() + 1);
                            log.error("推送完成订单到ERP失败! 订单号: {}", fsErpFinishPush.getOrderId());
                        }
                    } catch (Throwable e) {
                        fsErpFinishPush.setRetryCount(fsErpFinishPush.getRetryCount() + 1);
                        fsErpFinishPush.setErrorMessage(ExceptionUtils.getStackTrace(e));
                        fsErpFinishPush.setTaskStatus(2);
                        log.error("订单推送失败!原因: {}", ExceptionUtils.getStackTrace(e), e);
                        continue;
                    }
                } else {
                    log.error("订单不存在! 订单号: {}",fsErpFinishPush.getOrderId());
                    continue;
                }
            }

            fsErpFinishPushMapper.updateById(fsErpFinishPush);
        }

    }

}
