parent
2fc7c7788c
commit
95098cfd87
|
@ -2,12 +2,10 @@ package com.muyu.controller;
|
|||
|
||||
import com.muyu.common.Result;
|
||||
import com.muyu.domain.Vehicle;
|
||||
import com.muyu.domain.req.VehicleCreateAddReq;
|
||||
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;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -39,6 +37,16 @@ public class VehicleController {
|
|||
vehicleService.generate(sum);
|
||||
return Result.success();
|
||||
}
|
||||
/**
|
||||
* 根据输入的数量进行车辆随机生成
|
||||
* @param sum 车辆总数
|
||||
* @return 结果集
|
||||
*/
|
||||
@PostMapping("/create")
|
||||
public Result<String> create(@RequestBody VehicleCreateAddReq vehicleCreateAddReq){
|
||||
vehicleService.create(vehicleCreateAddReq.getVinStr());
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -60,9 +60,17 @@ public class Vehicle {
|
|||
* @return 车辆数据
|
||||
*/
|
||||
public static Vehicle gen() {
|
||||
return Vehicle.create(VehicleUtils.genVin());
|
||||
|
||||
}
|
||||
/**
|
||||
* 生成车辆数据
|
||||
* @return 车辆数据
|
||||
*/
|
||||
public static Vehicle create(String vin) {
|
||||
BigDecimal battery = VehicleUtils.genBattery();
|
||||
return Vehicle.builder()
|
||||
.vin(VehicleUtils.genVin())
|
||||
.vin(vin)
|
||||
.createTime(new Date())
|
||||
.batteryLevel(battery)
|
||||
.remainingBattery(battery)
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package com.muyu.domain.req;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author DongZl
|
||||
* @description: 车辆添加
|
||||
* @Date 2023-12-2 上午 08:58
|
||||
*/
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class VehicleCreateAddReq {
|
||||
|
||||
/**
|
||||
* 车辆添加VIN
|
||||
*/
|
||||
private String vinStr;
|
||||
}
|
|
@ -18,4 +18,10 @@ public interface VehicleService extends IService<Vehicle> {
|
|||
* @param sum 数量
|
||||
*/
|
||||
void generate(Integer sum);
|
||||
|
||||
/**
|
||||
* 创建IVN
|
||||
* @param vinStr VIN
|
||||
*/
|
||||
void create (String vinStr);
|
||||
}
|
||||
|
|
|
@ -1,13 +1,18 @@
|
|||
package com.muyu.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.muyu.domain.Vehicle;
|
||||
import com.muyu.mapper.VehicleMapper;
|
||||
import com.muyu.service.VehicleInstanceService;
|
||||
import com.muyu.service.VehicleService;
|
||||
import com.muyu.utils.VehicleUtils;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
@ -19,9 +24,14 @@ import java.util.stream.Stream;
|
|||
* @author DongZeLiang
|
||||
* @since 2022-07-05
|
||||
*/
|
||||
@Log4j2
|
||||
@Service
|
||||
public class VechileServiceImpl extends ServiceImpl<VehicleMapper, Vehicle> implements VehicleService {
|
||||
|
||||
|
||||
@Autowired
|
||||
private VehicleInstanceService vehicleInstanceService;
|
||||
|
||||
/**
|
||||
* 根据数量生成测试车辆
|
||||
*
|
||||
|
@ -32,6 +42,37 @@ public class VechileServiceImpl extends ServiceImpl<VehicleMapper, Vehicle> impl
|
|||
public void generate(Integer sum) {
|
||||
List<Vehicle> vehicleList = Stream.generate(Vehicle::gen).limit(sum).toList();
|
||||
this.saveBatch(vehicleList);
|
||||
vehicleList.forEach(vehicleInstanceService::init);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建IVN
|
||||
*
|
||||
* @param vinStr VIN
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public void create (String vinStr) {
|
||||
String[] vinList = vinStr.split("\n");
|
||||
StringBuilder errorMsg = new StringBuilder();
|
||||
for (String vin : vinList) {
|
||||
if (vin.length() != 17){
|
||||
errorMsg.append("vin[").append(vin).append("]").append("不为17位\n");
|
||||
}else {
|
||||
LambdaQueryWrapper<Vehicle> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(Vehicle::getVin, vin);
|
||||
long count = this.count(queryWrapper);
|
||||
if (count == 1){
|
||||
errorMsg.append("vin[").append(vin).append("]").append("已经存在\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (errorMsg.length() != 0){
|
||||
throw new RuntimeException(errorMsg.toString());
|
||||
}
|
||||
List<Vehicle> vehicleList = Arrays.stream(vinList).map(Vehicle::create).toList();
|
||||
this.saveBatch(vehicleList);
|
||||
vehicleList.forEach(vehicleInstanceService::init);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ import org.springframework.stereotype.Service;
|
|||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author DongZeLiang
|
||||
|
@ -69,8 +70,13 @@ public class VehicleInstanceServiceImpl implements VehicleInstanceService {
|
|||
*/
|
||||
@Override
|
||||
public List<VehicleInstanceResp> queryList (VehicleInstanceListReq vehicleInstanceListReq) {
|
||||
return LocalContainer.vehicleDataMap.values()
|
||||
.stream()
|
||||
Stream<VehicleInstance> stream = LocalContainer.vehicleDataMap.values()
|
||||
.stream();
|
||||
if (StringUtils.isNotBlank(vehicleInstanceListReq.getVin())){
|
||||
stream = stream.filter(vehicleInstance ->
|
||||
vehicleInstance.getVin().contains(vehicleInstanceListReq.getVin()));
|
||||
}
|
||||
return stream
|
||||
.map(VehicleInstanceResp::instanceBuild)
|
||||
.toList();
|
||||
}
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -1,5 +1,6 @@
|
|||
package com.muyu.vehicle.thread;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.muyu.vehicle.VehicleInstance;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
|
||||
|
@ -32,6 +33,7 @@ public class VehicleThread implements Runnable {
|
|||
this.vehicleInstance.sendMsg(
|
||||
this.vehicleInstance.getVehicleData().getMsg()
|
||||
);
|
||||
log.info(JSONObject.toJSONString(this.vehicleInstance.getVehicleData()));
|
||||
}else {
|
||||
log.warn("车辆[{}]数据模拟:{}", this.vehicleInstance.getVin(), imitateResult);
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ server:
|
|||
port: 81
|
||||
spring:
|
||||
mvc:
|
||||
static-path-pattern: /static/**
|
||||
static-path-pattern: /static
|
||||
|
||||
datasource:
|
||||
username: muyu
|
||||
|
|
Loading…
Reference in New Issue