package com.hotwj.platform.config.vehicleThreeInspect.strategy;

import com.hotwj.platform.config.vehicleThreeInspect.domain.HotVehicleThreeInspect;
import com.hotwj.platform.config.vehicleThreeInspect.mapper.HotVehicleThreeInspectMapper;
import com.hotwj.platform.flow.domain.SysFlowInstance;
import com.hotwj.platform.flow.domain.dto.FlowNextDto;
import com.hotwj.platform.flow.strategy.IFlowStrategy;
import com.hotwj.platform.resourceManagement.vehicleManagement.domain.HotVehicle;
import com.hotwj.platform.resourceManagement.vehicleManagement.mapper.HotVehicleMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 车辆三检流程策略
 *
 * @author shihongwei
 * @date 2026-01-25
 */
@Component
@RequiredArgsConstructor
public class VehicleThreeInspectStrategy implements IFlowStrategy {

    private final HotVehicleThreeInspectMapper inspectMapper;
    private final HotVehicleMapper vehicleMapper;

    @Override
    public String getFlowCode() {
        return "VEHICLE_THREE_INSPECT";
    }

    @Override
    public String getFlowName() {
        return "车辆三检";
    }

    @Override
    public FlowNextDto next(SysFlowInstance instance, String currentNodeCode) {
        // 简单流程：只有一级审批，审批通过即结束
        // 如果需要多级审批，可以在这里根据 currentNodeCode 判断返回下一级节点
        // 这里返回 null 表示没有下一节点，流程结束
        return null;
    }

    @Override
    public FlowNextDto getInitialNode(String businessId) {
        String nodeName = "车辆三检审批";
        try {
            Long id = Long.valueOf(businessId);
            HotVehicleThreeInspect inspect = inspectMapper.selectById(id);
            if (inspect != null) {
                // 获取车牌号
                String plateNumber = "";
                HotVehicle vehicle = vehicleMapper.selectById(inspect.getVehicleId());
                if (vehicle != null) {
                    plateNumber = vehicle.getPlateNumber();
                }

                // 获取检查阶段
                String phaseName = getPhaseName(inspect.getInspectPhase());

                nodeName = String.format("(%s) (%s) 审核", plateNumber, phaseName);
            }
        } catch (Exception e) {
            // 忽略异常，使用默认名称
        }
        return new FlowNextDto("NODE_INSPECT_AUDIT", nodeName, null);
    }

    private String getPhaseName(Long phase) {
        if (phase == null) {
            return "未知阶段";
        }
        if (phase == 1L) {
            return "出车前检查";
        } else if (phase == 2L) {
            return "行车中检查";
        } else if (phase == 3L) {
            return "收车后检查";
        }
        return "未知阶段";
    }
}
