package com.sckw.transport.handler;


import com.sckw.core.common.enums.enums.ErrorCodeEnum;
import com.sckw.core.exception.BusinessPlatfromException;
import com.sckw.core.model.enums.CarWaybillV1Enum;
import com.sckw.transport.model.KwtWaybillOrder;
import com.sckw.transport.model.param.WaybillOrderLeaveParam;
import com.sckw.transport.repository.KwtWaybillOrderSubtaskRepository;
import com.sckw.transport.repository.KwtWaybillOrderTicketRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * Author: donglang
 * Time: 2025-11-19
 * Des: 离场
 * Version: 1.0
 */

@Slf4j
@Service
public class LeaveHandler extends AbstractWaybillOrderHandler<WaybillOrderLeaveParam> {

    @Autowired
    private KwtWaybillOrderTicketRepository waybillOrderTicketRepository;
    @Autowired
    private KwtWaybillOrderSubtaskRepository waybillOrderSubtaskRepository;


    @Override
    protected KwtWaybillOrder getWaybillOrder(WaybillOrderLeaveParam param) {
        return getWaybillOrder(param.getWaybillOrderId());
    }

    @Override
    protected void checkState(WaybillOrderLeaveParam param, KwtWaybillOrder waybill) {
        if (!Objects.equals(CarWaybillV1Enum.EXIT_COMPLETED.getCode(), waybill.getStatus())) {
            throw new BusinessPlatfromException(ErrorCodeEnum.WAYBILL_ORDER_STATUS_ERROR, "运单状态不是已装货状态, 无法推进下一节点！");
        }
    }

    @Override
    protected void doBusiness(WaybillOrderLeaveParam param, KwtWaybillOrder waybill) {
        // 离场无额外业务逻辑
    }

    @Override
    protected void updateStatus(WaybillOrderLeaveParam param, KwtWaybillOrder waybill) {
        // 已离场状态
        waybill.setStatus(CarWaybillV1Enum.WAIT_LOADING.getCode());
        waybillOrderRepository.updateById(waybill);
    }

    @Override
    protected String getProcessName() {
        return "离场";
    }

    @Override
    protected String getRemark(WaybillOrderLeaveParam param, KwtWaybillOrder waybillOrder) {
        return "司机[" + waybillOrder.getDriverName() + "]已确认离场";
    }
}
