package com.sckw.transport.handler;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sckw.core.common.enums.enums.ErrorCodeEnum;
import com.sckw.core.exception.BusinessPlatfromException;
import com.sckw.core.model.enums.AddressTypeEnum;
import com.sckw.core.model.enums.CarWaybillV1Enum;
import com.sckw.fleet.api.model.vo.RTruckVo;
import com.sckw.transport.model.KwtWaybillOrder;
import com.sckw.transport.model.KwtWaybillOrderNode;
import com.sckw.transport.model.KwtWaybillOrderTicket;
import com.sckw.transport.model.param.WaybillOrderCmeIntoWeighParam;
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: 到达装货地点Handler
 * Version: 1.0
 */
@Slf4j
@Service
public class ComeIntoHandler extends AbstractWaybillOrderHandler<WaybillOrderCmeIntoWeighParam>{

    @Autowired
    private KwtWaybillOrderTicketRepository waybillOrderTicketRepository;


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

    @Override
    protected void checkState(WaybillOrderCmeIntoWeighParam param, KwtWaybillOrder waybillOrder) {
        if (!Objects.equals(CarWaybillV1Enum.PENDING_VEHICLE.getCode(), waybillOrder.getStatus())) {
            throw new BusinessPlatfromException(ErrorCodeEnum.WAYBILL_ORDER_STATUS_ERROR, "当前物流运单状态不正确，无法推进下一节点");
        }
    }

    @Override
    protected void doBusiness(WaybillOrderCmeIntoWeighParam param, KwtWaybillOrder waybillOrder) {
        //1.填充运单皮重
        LambdaQueryWrapper<KwtWaybillOrderTicket>  queryWrapper = Wrappers.<KwtWaybillOrderTicket>lambdaQuery()
                .eq(KwtWaybillOrderTicket::getWOrderId, param.getWaybillOrderId())
                .eq(KwtWaybillOrderTicket::getType, AddressTypeEnum.SHIPMENT.getCode());
        KwtWaybillOrderTicket orderTicket = waybillOrderTicketRepository.getOne(queryWrapper);
        orderTicket.setTareAmount(param.getTareAmount());
        waybillOrderTicketRepository.save(orderTicket);

        //2.填充首次皮重
        RTruckVo truck = remoteFleetService.findTruckByTruckNo(param.getTruckNo());
        if (truck != null && truck.getTareWeight() == null) {
            RTruckVo truckNo = new RTruckVo();
            truckNo.setId(truck.getId());
            truckNo.setTareWeight(param.getTareAmount());
            remoteFleetService.updateTruckTareAmount(truckNo);
        }
    }

    @Override
    protected void updateStatus(WaybillOrderCmeIntoWeighParam param, KwtWaybillOrder waybillOrder) {
        // 到达装货地点状态
        waybillOrder.setStatus(CarWaybillV1Enum.REFUSE_TRAFFIC.getCode());
        waybillOrderRepository.updateById(waybillOrder);
    }


    @Override
    protected String getProcessName() {
        return "到达装货地点";
    }

    @Override
    protected String getRemark(WaybillOrderCmeIntoWeighParam param, KwtWaybillOrder waybillOrder) {
        return "";
    }

    /**
     * 重写 createNodeTrace，生成两条轨迹记录
     */
    @Override
    protected void createNodeTrace(WaybillOrderCmeIntoWeighParam param, KwtWaybillOrder waybillOrder) {
        // 第一条：装载完成
        KwtWaybillOrderNode node1 = getWaybillOrderNode(param, waybillOrder);
        node1.setRemark("车辆[" + waybillOrder.getTruckNo() + "]已到达装货点");
        waybillOrderNodeRepository.save(node1);
        log.info("记录【已装货】节点轨迹成功，节点ID：{}", node1.getId());

        // 第二条：称重信息
        KwtWaybillOrderNode node2 = getWaybillOrderNode(param, waybillOrder);
        node2.setWeighUrl(param.getWeighUrl());
        node2.setRemark("[" + param.getWeighbridgeName() + "]称重[" + param.getTareAmount() + "吨]");
        waybillOrderNodeRepository.save(node2);
        log.info("记录【称重】节点轨迹成功，节点ID：{}", node2.getId());
    }

}
