package com.sckw.transport.handler;


import com.alibaba.fastjson.JSON;
import com.sckw.core.common.enums.enums.ErrorCodeEnum;
import com.sckw.core.exception.BusinessPlatfromException;
import com.sckw.core.model.enums.CarWaybillV1Enum;
import com.sckw.core.model.enums.GatekeeperStatusEnum;
import com.sckw.core.model.enums.GatekeeperTypeEnum;
import com.sckw.transport.model.KwtGatekeeperWaybillOrder;
import com.sckw.transport.model.KwtWaybillOrder;
import com.sckw.transport.model.KwtWaybillOrderSubtask;
import com.sckw.transport.model.param.WaybillOrderLeaveParam;
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 LeaveHandler extends AbstractWaybillOrderHandler<WaybillOrderLeaveParam> {

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

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

    @Override
    protected void doBusiness(WaybillOrderLeaveParam param, KwtWaybillOrder waybillOrder) {
        log.info("推进门卫数据为待放行：{}", JSON.toJSONString(waybillOrder));

        // 修改门卫订单状态为：待放行
        updateGatekeeperOrderStatus(waybillOrder);
    }


    /**
     * 修改门卫订单状态
     * @param waybillOrder
     * @return
     */
    public void updateGatekeeperOrderStatus(KwtWaybillOrder waybillOrder) {
        KwtGatekeeperWaybillOrder gatekeeper = gatekeeperWaybillOrderRepository
                .queryGatekeeperWaybillOrderByWOrderId(waybillOrder.getId(), GatekeeperTypeEnum.LOADING.getCode());
        if (gatekeeper == null) {
            throw new BusinessPlatfromException(ErrorCodeEnum.GATEKEEPER_ORDER_NOT_FOUND, "当前门卫订单数据不存在！");
        }
        if (!(GatekeeperStatusEnum.IN_YARD.getCode().equals(gatekeeper.getStatus()))) {
            throw new BusinessPlatfromException(ErrorCodeEnum.GATEKEEPER_ORDER_STATUS_ERROR, "当前门卫订单状态异常，不能推进为待放行状态！");
        }
        //修改门卫：待放行
        gatekeeper.setStatus(GatekeeperStatusEnum.PENDING_RELEASE.getCode());
        gatekeeperWaybillOrderRepository.updateById(gatekeeper);
    }

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

    }

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

    @Override
    protected void getStatus(KwtWaybillOrder waybillOrder) {
        // 1. 修改运单状态
        waybillOrder.setStatus(CarWaybillV1Enum.WAIT_RELEASE.getCode());
        waybillOrderRepository.updateById(waybillOrder);

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

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