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.WaybillOrderUnLoadingLeaveParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * Author: donglang
 * Time: 2026-01-20
 * Des: 卸货离场
 * Version: 1.0
 */

@Slf4j
@Service
public class UnloadingLeavedHandler extends AbstractWaybillOrderHandler<WaybillOrderUnLoadingLeaveParam> {

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

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

    @Override
    protected void doBusiness(WaybillOrderUnLoadingLeaveParam 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.UNLOADING.getCode());
        if (gatekeeper == null) {
            throw new BusinessPlatfromException(ErrorCodeEnum.GATEKEEPER_ORDER_NOT_FOUND, "当前门卫卸货订单数据不存在！");
        }
        if (!Objects.equals(gatekeeper.getStatus(), GatekeeperStatusEnum.IN_YARD.getCode())) {
            throw new BusinessPlatfromException(ErrorCodeEnum.GATEKEEPER_ORDER_STATUS_ERROR, "门卫状态不是“卸货已进场”状态, 无法推进下一节点！！");
        }

        //修改门卫：卸货待放行
        gatekeeper.setStatus(GatekeeperStatusEnum.PENDING_RELEASE.getCode());
        gatekeeperWaybillOrderRepository.updateById(gatekeeper);
    }


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

    }

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

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

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


    @Override
    protected String getRemark(WaybillOrderUnLoadingLeaveParam param, KwtWaybillOrder waybillOrder) {
        return "车辆[" + waybillOrder.getTruckNo() + "]已确认卸货离场";
    }
}
