119 lines
4.5 KiB
Java
119 lines
4.5 KiB
Java
package com.muyu.service.impl;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
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.vehicle.VehicleInstance;
|
|
import com.muyu.vehicle.core.LocalContainer;
|
|
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.Collection;
|
|
import java.util.List;
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
import java.util.stream.Stream;
|
|
|
|
/**
|
|
* <p>
|
|
* 车辆 服务实现类
|
|
* </p>
|
|
*
|
|
* @author DongZeLiang
|
|
* @since 2022-07-05
|
|
*/
|
|
@Log4j2
|
|
@Service
|
|
public class VechileServiceImpl extends ServiceImpl<VehicleMapper, Vehicle> implements VehicleService {
|
|
|
|
|
|
@Autowired
|
|
private VehicleInstanceService vehicleInstanceService;
|
|
|
|
/**
|
|
* 根据数量生成测试车辆
|
|
*
|
|
* @param sum 数量
|
|
*/
|
|
@Override
|
|
@Transactional
|
|
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.isEmpty()) {
|
|
throw new RuntimeException(errorMsg.toString());
|
|
}
|
|
List<Vehicle> vehicleList = Arrays.stream(vinList).map(Vehicle::create).toList();
|
|
this.saveBatch(vehicleList);
|
|
vehicleList.forEach(vehicleInstanceService::init);
|
|
}
|
|
|
|
/**
|
|
* 同步数据库
|
|
*/
|
|
@Override
|
|
public void syncDb () {
|
|
try {
|
|
log.info("同步数据库开始");
|
|
long startTime = System.currentTimeMillis();
|
|
Collection<VehicleInstance> vehicleInstanceList = LocalContainer.getOnlineVehicleInstance();
|
|
// 成功数量
|
|
AtomicInteger syncSuccessSum = new AtomicInteger();
|
|
List<Vehicle> vehicleList = vehicleInstanceList.stream()
|
|
.filter(VehicleInstance::isOnline)
|
|
.map(Vehicle::instanceBuild)
|
|
.toList();
|
|
vehicleList.forEach(vehicle -> {
|
|
LambdaUpdateWrapper<Vehicle> updateWrapper = new LambdaUpdateWrapper<>();
|
|
updateWrapper.set(Vehicle::getRemainingBattery, vehicle.getRemainingBattery());
|
|
updateWrapper.set(Vehicle::getTotalMileage, vehicle.getTotalMileage());
|
|
updateWrapper.eq(Vehicle::getVin, vehicle.getVin());
|
|
boolean update = this.update(updateWrapper);
|
|
if (update){
|
|
syncSuccessSum.incrementAndGet();
|
|
log.debug("车辆:[{}] - 数据同步成功 - 电池容量:[{}毫安] 总公里数量:[{}KM]", vehicle.getVin(), vehicle.getRemainingBattery(), vehicle.getTotalMileage());
|
|
}{
|
|
log.error("车辆:[{}] - 数据同步失败 - 电池容量:[{}毫安] 总公里数量:[{}KM]", vehicle.getVin(), vehicle.getRemainingBattery(), vehicle.getTotalMileage());
|
|
}
|
|
});
|
|
log.info("同步数据库结束 - 耗时:[{}MS],同步量:[{}辆],成功:[{}辆],失败:[{}辆]",
|
|
System.currentTimeMillis() - startTime, vehicleList.size(),syncSuccessSum.get(), vehicleList.size() - syncSuccessSum.get());
|
|
}catch (Exception exception){
|
|
log.error("数据同步异常:{}", exception.getMessage(), exception);
|
|
}
|
|
}
|
|
|
|
|
|
}
|