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.KwtForkliftWaybillOrder;
import com.sckw.transport.model.KwtWaybillOrder;
import com.sckw.transport.model.KwtWaybillOrderSubtask;
import com.sckw.transport.model.param.WaybillOrderLoadingParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * Author: donglang
 * Time: 2025-11-19
 * Des: 已装货 -手动推推送数据
 * Version: 1.0
 */
@Slf4j
@Service
public class LoadingHandler extends AbstractWaybillOrderHandler<WaybillOrderLoadingParam>{

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

    @Override
    protected void checkState(WaybillOrderLoadingParam param, KwtWaybillOrder waybillOrder) {

    }

    @Override
    protected void doBusiness(WaybillOrderLoadingParam param, KwtWaybillOrder waybillOrder) {
        // 装货无额外业务逻辑
    }

    @Override
    protected void calculateAutoDispatchScore(WaybillOrderLoadingParam param, KwtWaybillOrder waybillOrder) {

    }

    @Override
    protected String getProcessName() {
        return "已装货";
    }

    @Override
    protected void getStatus(KwtWaybillOrder waybillOrder) {
        //第一次过磅，状态为：已装货；后续过磅，状态为：补货完成
        CarWaybillV1Enum status = Objects.equals(waybillOrder.getStatus(), CarWaybillV1Enum.REPLENISHING.getCode())
                ? CarWaybillV1Enum.REPLENISH_FINISH : CarWaybillV1Enum.EXIT_COMPLETED;

        // 1. 修改运单状态
        waybillOrder.setStatus(status.getCode());
        waybillOrderRepository.updateById(waybillOrder);

        // 2. 修改子运单状态
        KwtWaybillOrderSubtask waybillSubtask = getWaybillSubtask(waybillOrder.getId());
        waybillSubtask.setStatus(status.getCode());
        waybillOrderSubtaskRepository.updateById(waybillSubtask);

    }

    @Override
    protected String getRemark(WaybillOrderLoadingParam param, KwtWaybillOrder waybillOrder) {
        // 查询铲车订单
        KwtForkliftWaybillOrder forkliftWaybillOrder = forkliftWaybillOrderRepository
                .queryForkliftWaybillOrderByWOrderId(waybillOrder.getId());
        if (forkliftWaybillOrder == null) {
            throw new BusinessPlatfromException(ErrorCodeEnum.FORKLIFT_ORDER_NOT_FOUND, "当前铲车订单不存在！");
        }
        return "装载员[" + forkliftWaybillOrder.getDriverName() + "]已完成装载，装载单号[" + forkliftWaybillOrder.getFOrderNo() + "]";
    }
}
