package cn.iocoder.yudao.module.asset.service.event;

import cn.iocoder.yudao.module.asset.dal.dataobject.vehicle.VehicleStatusDO;
import cn.iocoder.yudao.module.asset.dal.mysql.vehicle.VehicleStatusMapper;
import cn.iocoder.yudao.module.asset.service.delivery.event.DeliveryCompletedEvent;
import cn.iocoder.yudao.module.asset.service.replacement.event.ReplacementReturnConfirmedEvent;
import cn.iocoder.yudao.module.asset.service.returnorder.event.ReturnApprovedEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * 车辆状态事件监听器
 * 负责在业务事件发生时更新车辆状态
 *
 * @author 芋道源码
 */
@Component
@Slf4j
public class VehicleStatusEventListener {

    @Resource
    private VehicleStatusMapper vehicleStatusMapper;

    /**
     * 交车完成：更新车辆库位状态为「已交付车-租赁交车」，标记已交车
     */
    @TransactionalEventListener
    public void onDeliveryCompleted(DeliveryCompletedEvent event) {
        log.info("[onDeliveryCompleted] deliveryOrderId={}, vehicleId={}", event.getDeliveryOrderId(), event.getVehicleId());
        VehicleStatusDO vehicleStatus = vehicleStatusMapper.selectByVehicleId(event.getVehicleId());
        if (vehicleStatus == null) {
            log.warn("[onDeliveryCompleted] vehicle status not found, vehicleId={}", event.getVehicleId());
            return;
        }
        vehicleStatusMapper.updateById(VehicleStatusDO.builder()
                .id(vehicleStatus.getId())
                .storageStatus(5) // 已交付车-租赁交车
                .operateStatus(2) // 租赁
                .isDelivered(true)
                .build());
    }

    /**
     * 还车审批通过：更新所有还车车辆的库位状态为「库存车-可交付车」，标记已还车
     */
    @TransactionalEventListener
    public void onReturnApproved(ReturnApprovedEvent event) {
        log.info("[onReturnApproved] returnOrderId={}, vehicleIds={}", event.getReturnOrderId(), event.getVehicleIds());
        List<Long> vehicleIds = event.getVehicleIds();
        if (vehicleIds == null || vehicleIds.isEmpty()) {
            return;
        }
        List<VehicleStatusDO> vehicleStatuses = vehicleStatusMapper.selectByVehicleIds(vehicleIds);
        for (VehicleStatusDO vehicleStatus : vehicleStatuses) {
            vehicleStatusMapper.updateById(VehicleStatusDO.builder()
                    .id(vehicleStatus.getId())
                    .storageStatus(2) // 库存车-可交付车
                    .operateStatus(1) // 库存
                    .isReturned(true)
                    .isDelivered(false)
                    .build());
        }
    }

    /**
     * 替换车换回确认：更新原车辆库位状态为「库存车-可交付车」
     */
    @TransactionalEventListener
    public void onReplacementReturnConfirmed(ReplacementReturnConfirmedEvent event) {
        log.info("[onReplacementReturnConfirmed] replacementId={}, originalVehicleId={}",
                event.getReplacementId(), event.getOriginalVehicleId());
        VehicleStatusDO vehicleStatus = vehicleStatusMapper.selectByVehicleId(event.getOriginalVehicleId());
        if (vehicleStatus == null) {
            log.warn("[onReplacementReturnConfirmed] vehicle status not found, vehicleId={}", event.getOriginalVehicleId());
            return;
        }
        vehicleStatusMapper.updateById(VehicleStatusDO.builder()
                .id(vehicleStatus.getId())
                .storageStatus(2) // 库存车-可交付车
                .operateStatus(1) // 库存
                .isDelivered(false)
                .build());
    }

}
