package com.sckw.transport.service;

import com.sckw.core.model.constant.Global;
import com.sckw.core.model.constant.NumberConstant;
import com.sckw.transport.dao.KwtLogisticsOrderMapper;
import com.sckw.transport.dao.KwtWaybillOrderSubtaskMapper;
import com.sckw.transport.model.KwtLogisticsOrder;
import com.sckw.transport.model.KwtWaybillOrderSubtask;
import com.sckw.transport.model.KwtWaybillOrderV1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * @author zk
 * @desc 车辆运单-子运单
 * @date 2024/3/1 0001
 */
@Service
public class KwtWaybillOrderSubtaskService {

    @Autowired
    KwtWaybillOrderSubtaskMapper waybillOrderSubtaskDao;
    @Autowired
    KwtLogisticsOrderMapper logisticsOrderDao;

    /**
     * @param waybillOrder 运单信息
     * @param lOrderId 物流运单ID
     * @param entrustAmount 运输委托信息
     * @param loadTime 装货时间
     * @param unloadTime 卸货时间
     * @desc 车辆运单-子运单信息
     * @author zk
     * @date 2024/3/4
     **/
    public KwtWaybillOrderSubtask sendCarSubtask(KwtWaybillOrderV1 waybillOrder, Long lOrderId, BigDecimal entrustAmount, List<Date> loadTime, List<Date> unloadTime) {
        loadTime.sort(Comparator.naturalOrder());
        unloadTime.sort(Comparator.reverseOrder());
        KwtLogisticsOrder logisticsOrder = logisticsOrderDao.selectById(lOrderId);
        KwtWaybillOrderSubtask orderSubtask = new KwtWaybillOrderSubtask();
        orderSubtask.setEntId(waybillOrder.getEntId());
        orderSubtask.setLOrderId(lOrderId);
        orderSubtask.setWOrderId(waybillOrder.getId());
        orderSubtask.setWOrderNo(waybillOrder.getWOrderNo());
        orderSubtask.setUnit(logisticsOrder != null ? logisticsOrder.getUnit() : null);
        orderSubtask.setEntrustAmount(entrustAmount);
        orderSubtask.setLoadTime(loadTime.get(Global.NUMERICAL_ZERO));
        orderSubtask.setLoadAmount(NumberConstant.ZERO_TWO);
        orderSubtask.setUnloadTime(unloadTime.get(Global.NUMERICAL_ZERO));
        orderSubtask.setUnloadAmount(NumberConstant.ZERO_TWO);
        orderSubtask.setStatus(waybillOrder.getStatus());
        waybillOrderSubtaskDao.insert(orderSubtask);
        return orderSubtask;
    }
}
