package com.bohuikeji.delivery.backstage.thread;


import com.bohuikeji.delivery.backstage.entity.TLogistics;
import com.bohuikeji.delivery.backstage.entity.TOrder;
import com.bohuikeji.delivery.backstage.service.ITLogisticsService;
import com.bohuikeji.delivery.backstage.service.ITOrderService;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/***
 * 订单批量方法线程
 * @author 10552
 */
public class OrderSaveThread implements Runnable{
    private ITOrderService orderService;
    private List<TOrder> orderList;
    private ITLogisticsService logisticsService;

    public OrderSaveThread(ITOrderService orderService, ITLogisticsService logisticsService, List<TOrder> orderList){
        this.orderService=orderService;
        this.orderList=orderList;
        this.logisticsService=logisticsService;
    }
    @Override
    public void run() {
        /**
         * 物流单号缓存集合
         */
        List<TLogistics> tLogisticsList=new ArrayList<>(2);
        if(orderService.saveBatch(orderList)){
            //遍历订单列表，生产物流记录
            orderList.forEach(order -> {
                for(int i=0;i<order.getPackageCount();i++){
                    TLogistics logistics=new TLogistics();
                    logistics.setLogisticsNum(order.getOrderNum()+"-"+(i+1));
                    logistics.setOrderNum(order.getOrderNum());
                    logistics.setOrderId(order.getOrderId());
                    logistics.setLogisticsStatus("待取件");
                    logistics.setPlayerId(order.getPlayerId());
                    logistics.setCreateTime(LocalDateTime.now());
                    tLogisticsList.add(logistics);
                }
                if(logisticsService.saveBatch(tLogisticsList)){
                    tLogisticsList.clear();
                }
            });
        }


    }
}
