package com.xindazhou.business.controller.app.vehicle.convert;

import com.xindazhou.business.controller.app.vehicle.vo.*;
import com.xindazhou.vehicle.api.dto.VehicleRespDTO;
import com.xindazhou.vehicle.api.dto.bind.VehicleBindRespDTO;
import com.xindazhou.vehicle.api.dto.status.VehicleStatusRespDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

/**
 * 车辆 Convert
 *
 * @author xindazhou
 */
@Mapper
public interface VehicleConvert {

    VehicleConvert INSTANCE = Mappers.getMapper(VehicleConvert.class);

    @Mapping(target = "name", source = "plateNumber")
    @Mapping(target = "deviceSn", ignore = true)
    @Mapping(target = "bluetoothMac", ignore = true)
    @Mapping(target = "imageUrl", ignore = true)
    @Mapping(target = "modelId", ignore = true)
    @Mapping(target = "firmwareVersion", ignore = true)
    @Mapping(target = "bindTime", ignore = true)
    VehicleDetailRespVO convert(VehicleRespDTO dto);

    @Mapping(target = "defenseStatus", ignore = true)
    @Mapping(target = "powerStatus", ignore = true)
    @Mapping(target = "doorLockStatus", ignore = true)
    @Mapping(target = "signalStrength", ignore = true)
    @Mapping(target = "bluetoothStatus", ignore = true)
    @Mapping(target = "range", source = "remainingMileage")
    @Mapping(target = "frontTirePressure", ignore = true)
    @Mapping(target = "rearTirePressure", ignore = true)
    VehicleStatusRespVO convertStatus(VehicleStatusRespDTO dto);

    /**
     * 将VehicleRespDTO和VehicleBindRespDTO合并转换为VehicleListRespVO
     */
    default VehicleListRespVO convertList(VehicleRespDTO vehicle, VehicleBindRespDTO binding) {
        VehicleListRespVO vo = new VehicleListRespVO();
        if (vehicle != null) {
            vo.setId(vehicle.getId());
            vo.setVin(vehicle.getVin());
            vo.setBluetoothMac(null); // VehicleRespDTO中没有bluetoothMac，需要从vehicle表查询
            vo.setStatus(vehicle.getStatus());
        }
        if (binding != null) {
            vo.setIsDefault(binding.getBindType() != null && binding.getBindType().equals("OWNER") ? 1 : 0);
            vo.setUserType(binding.getUserType());
            vo.setBindTime(binding.getBindTime());
        }
        return vo;
    }

}

