package com.clx.performance.job;

import cn.hutool.core.collection.CollectionUtil;
import com.clx.performance.dao.settle.OrderChildSyncTransportRecordDao;
import com.clx.performance.feign.TransportFeignService;
import com.clx.performance.model.settle.OrderChildSyncTransportRecord;
import com.msl.common.dto.HttpDTO;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
@AllArgsConstructor
public class OrderChildSyncTransportJob {

    private final OrderChildSyncTransportRecordDao orderChildSyncTransportRecordDao;

    private final TransportFeignService transportFeignService;

    /**
     * 同步网络货运
     */
    @XxlJob("orderChildSyncTransportJob")
    public void orderChildSyncTransportJob() {
        List<OrderChildSyncTransportRecord> recordList  = orderChildSyncTransportRecordDao.selectListForJob();
        if (CollectionUtil.isEmpty(recordList)) {
            return;
        }

        List<String> orderChilList = recordList.stream().map(OrderChildSyncTransportRecord::getChildNo).collect(Collectors.toList());
        HttpDTO httpDTO = transportFeignService.orderChildPaySync(orderChilList);

        for (OrderChildSyncTransportRecord record : recordList) {
            OrderChildSyncTransportRecord update = new OrderChildSyncTransportRecord();
            update.setId(record.getId());
            update.setStatus(1);
            orderChildSyncTransportRecordDao.updateEntityByKey(update);
        }
    }
}
