package com.muyu.vehicleGateway.web.domain;

import com.baomidou.mybatisplus.annotation.*;
import com.muyu.vehicleGateway.vehicle.VehicleInstance;
import com.muyu.vehicleGateway.vehicle.model.VehicleData;
import com.muyu.vehicleGateway.web.util.VehicleUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.Date;
import java.util.function.Supplier;

/**
 * @ClassName VehicleInfo
 * @Description 车辆信息
 * @Author LiHD
 * @Date 2024/9/26 23:48
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName("vehicle_info")
public class VehicleInfo {

    /**
     * 主键id
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * VIN
     */
    private String vin;

    /**
     * VIN
     */
    @TableField(value = "tenant_id", fill = FieldFill.INSERT)
    private String tenantId;

    /**
     * 报文模板id
     */
    @TableField("message_template_id")
    private Long messageTemplateId;

    /**
     * 电池剩余电量
     */
    @TableField("remaining_battery")
    private BigDecimal remainingBattery;

    /**
     * 电池电量
     */
    @TableField("battery_level")
    private BigDecimal batteryLevel;

    /**
     * 上一次经度
     */
    @TableField("last_longitude")
    private String lastLongitude;

    /**
     * 上一次纬度
     */
    @TableField("last_latitude")
    private String lastLatitude;

    /**
     * 总里程
     */
    @TableField("total_mileage")
    private BigDecimal totalMileage;

    /**
     * 创建时间
     */
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private Date createTime;

    /**
     * 生成车辆数据
     *
     * @param messageTemplateId 报文ID
     * @return 车辆数据
     */
    public static VehicleInfo gen(Supplier<Long> messageTemplateId) {
        return VehicleInfo.create(VehicleUtils.genVin(), messageTemplateId);
    }

    /**
     * 生成车辆数据
     *
     * @param vin
     * @param messageTemplateId
     * @return 车辆数据
     */
    public static VehicleInfo create(String vin, Supplier<Long> messageTemplateId) {
        BigDecimal battery = VehicleUtils.genBattery();
        return VehicleInfo.builder()
                .vin(vin)
                .messageTemplateId(messageTemplateId.get())
                .createTime(new Date())
                .remainingBattery(battery)
                .batteryLevel(battery)
                .totalMileage(BigDecimal.ZERO)
                .build();
    }

    public static VehicleInfo instanceBuild(VehicleInstance vehicleInstance) {
        VehicleData vehicle = vehicleInstance.getVehicleData();
        return VehicleInfo.builder()
                .vin(vehicleInstance.getVin())
                .remainingBattery(vehicle.getRemainingBattery())
                .totalMileage(vehicle.getMileage())
                .build();
    }

}
