parent
ce4f9d7b05
commit
fa69241b9d
|
@ -0,0 +1,11 @@
|
|||
package com.muyu.common;
|
||||
|
||||
/**
|
||||
* @author DongZeLiang
|
||||
* @version 1.0
|
||||
* @description 系统常量
|
||||
* @date 2023/11/15
|
||||
*/
|
||||
public class SystemConstant {
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.muyu.config;
|
||||
|
||||
import com.muyu.common.Result;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
/**
|
||||
* @author DongZeLiang
|
||||
* @version 1.0
|
||||
* @description 异常类
|
||||
* @date 2023/11/15
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
public class ExceptionAdvice {
|
||||
|
||||
/**
|
||||
* 运行时异常拦截
|
||||
* @param runtimeException 运行时异常
|
||||
* @return 公共返回结果
|
||||
*/
|
||||
@ExceptionHandler(value = RuntimeException.class)
|
||||
public Result<String> runtimeExceptionHandler(RuntimeException runtimeException){
|
||||
return Result.error(runtimeException.getMessage());
|
||||
}
|
||||
}
|
|
@ -5,6 +5,7 @@ import com.muyu.domain.Vehicle;
|
|||
import com.muyu.service.VehicleService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
|
@ -17,7 +18,6 @@ import java.util.List;
|
|||
* @date 2023/11/9
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/Information")
|
||||
public class VehicleController {
|
||||
|
||||
@Autowired
|
||||
|
@ -27,4 +27,15 @@ public class VehicleController {
|
|||
public Result<List<Vehicle>> list(){
|
||||
return Result.success(vehicleService.list());
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据输入的数量进行车辆随机生成
|
||||
* @param sum 车辆总数
|
||||
* @return 结果集
|
||||
*/
|
||||
@GetMapping("/gen/{sum}")
|
||||
public Result<String> generate(@PathVariable(value = "sum") Integer sum){
|
||||
vehicleService.generate(sum);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,11 +3,13 @@ package com.muyu.domain;
|
|||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.muyu.utils.VehicleUtils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
|
@ -23,9 +25,49 @@ import java.util.Date;
|
|||
@TableName("vehicle")
|
||||
public class Vehicle {
|
||||
|
||||
/**
|
||||
* VIN
|
||||
*/
|
||||
@TableId(value = "vin")
|
||||
private String vin;
|
||||
|
||||
/**
|
||||
* 电池剩余电量
|
||||
*/
|
||||
@TableField("remaining_battery")
|
||||
private BigDecimal remainingBattery;
|
||||
|
||||
/**
|
||||
* 电池电量
|
||||
*/
|
||||
@TableField("battery_level")
|
||||
private BigDecimal batteryLevel;
|
||||
|
||||
/**
|
||||
* 总里程
|
||||
*/
|
||||
@TableField("total_mileage")
|
||||
private BigDecimal totalMileage;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@TableField("create_time")
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 生成车辆数据
|
||||
* @return 车辆数据
|
||||
*/
|
||||
public static Vehicle gen() {
|
||||
BigDecimal battery = VehicleUtils.genBattery();
|
||||
return Vehicle.builder()
|
||||
.vin(VehicleUtils.genVin())
|
||||
.createTime(new Date())
|
||||
.batteryLevel(battery)
|
||||
.remainingBattery(battery)
|
||||
.totalMileage(BigDecimal.ZERO)
|
||||
.build();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,4 +13,9 @@ import com.muyu.domain.Vehicle;
|
|||
*/
|
||||
public interface VehicleService extends IService<Vehicle> {
|
||||
|
||||
/**
|
||||
* 根据数量生成测试车辆
|
||||
* @param sum 数量
|
||||
*/
|
||||
void generate(Integer sum);
|
||||
}
|
||||
|
|
|
@ -4,7 +4,12 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|||
import com.muyu.domain.Vehicle;
|
||||
import com.muyu.mapper.VehicleMapper;
|
||||
import com.muyu.service.VehicleService;
|
||||
import com.muyu.utils.VehicleUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -17,4 +22,15 @@ import org.springframework.stereotype.Service;
|
|||
@Service
|
||||
public class VechileServiceImpl extends ServiceImpl<VehicleMapper, Vehicle> implements VehicleService {
|
||||
|
||||
/**
|
||||
* 根据数量生成测试车辆
|
||||
*
|
||||
* @param sum 数量
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public void generate(Integer sum) {
|
||||
List<Vehicle> vehicleList = Stream.generate(Vehicle::gen).limit(sum).toList();
|
||||
this.saveBatch(vehicleList);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package com.muyu.utils;
|
||||
|
||||
import com.muyu.common.SystemConstant;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Random;
|
||||
|
||||
public class VehicleUtils {
|
||||
|
||||
/**
|
||||
* 生成VIN
|
||||
* @return 返回结果对象
|
||||
*/
|
||||
public static String genVin() {
|
||||
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
Random random = new Random();
|
||||
StringBuilder sb = new StringBuilder(17);
|
||||
for (int i = 0; i < 17; i++) {
|
||||
int index = (int) (random.nextFloat() * characters.length());
|
||||
sb.append(characters.charAt(index));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private static Random random = new Random();
|
||||
|
||||
/**
|
||||
* 生成电池额度
|
||||
* @return 电池额度
|
||||
*/
|
||||
public static BigDecimal genBattery(){
|
||||
return BigDecimal.valueOf(random.nextInt(54, 60) * 1000L);
|
||||
}
|
||||
|
||||
}
|
|
@ -7,8 +7,9 @@ create table if not exists t_user (
|
|||
) ;
|
||||
|
||||
create table if not exists vehicle (
|
||||
`id` int primary key not null ,
|
||||
`vin` char (50) not null,
|
||||
`total_mileage` int not null,
|
||||
`vin` char (50) primary key not null,
|
||||
`remaining_battery` DOUBLE not null,
|
||||
`total_mileage` DOUBLE not null,
|
||||
`battery_level` DOUBLE not null,
|
||||
`create_time` datetime not null
|
||||
) ;
|
||||
|
|
Loading…
Reference in New Issue