From 39c764922c6e362ae260a69e4b8f317af131d969 Mon Sep 17 00:00:00 2001 From: liuyunhu <3286117488@qq.com> Date: Wed, 27 Mar 2024 16:58:14 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=A6=E8=BE=86=E5=BD=95=E5=85=A5=EF=BC=8C?= =?UTF-8?q?=E7=94=9F=E6=88=90vin=E7=A0=81=EF=BC=8C=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E6=A8=A1=E6=8B=9F=E8=BD=A6=E8=BE=86=E5=92=8C?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E8=BD=A6=E8=BE=86=E4=B8=8A=E7=BA=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../couplet-modules-vehicle/pom.xml | 10 +-- .../controller/LyhVehicleController.java | 33 ++++++++ .../couplet/vehicle/domain/LyhVehicle.java | 2 - .../domain/req/LyhVehicleEditParams.java | 51 ++++++++++++ .../domain/req/LyhVehicleInsertParams.java | 55 +++++++++++++ .../domain/req/LyhVehicleListParams.java | 4 - .../vehicle/service/LyhVehicleService.java | 6 ++ .../service/impl/LyhVehicleServiceImpl.java | 76 ++++++++++++++++++ .../vehicle/utils/SnowflakeIdGenerator.java | 79 +++++++++++++++++++ .../src/test/java/IdTest.java | 20 +++++ 10 files changed, 325 insertions(+), 11 deletions(-) create mode 100644 couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/req/LyhVehicleEditParams.java create mode 100644 couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/req/LyhVehicleInsertParams.java create mode 100644 couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/utils/SnowflakeIdGenerator.java create mode 100644 couplet-modules/couplet-modules-vehicle/src/test/java/IdTest.java diff --git a/couplet-modules/couplet-modules-vehicle/pom.xml b/couplet-modules/couplet-modules-vehicle/pom.xml index d7be645..bd06244 100644 --- a/couplet-modules/couplet-modules-vehicle/pom.xml +++ b/couplet-modules/couplet-modules-vehicle/pom.xml @@ -11,11 +11,11 @@ couplet-modules-vehicle - - - - - + + + + + couplet-modules-vehicle车辆管理模块 diff --git a/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/controller/LyhVehicleController.java b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/controller/LyhVehicleController.java index 73b0e9c..f7900b8 100644 --- a/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/controller/LyhVehicleController.java +++ b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/controller/LyhVehicleController.java @@ -5,6 +5,8 @@ import com.couplet.common.core.web.controller.BaseController; import com.couplet.common.log.annotation.Log; import com.couplet.common.log.enums.BusinessType; import com.couplet.vehicle.domain.LyhVehicle; +import com.couplet.vehicle.domain.req.LyhVehicleEditParams; +import com.couplet.vehicle.domain.req.LyhVehicleInsertParams; import com.couplet.vehicle.domain.req.LyhVehicleListParams; import com.couplet.vehicle.service.LyhVehicleService; import org.springframework.beans.factory.annotation.Autowired; @@ -56,4 +58,35 @@ public class LyhVehicleController extends BaseController { } + /* + * @Author: LiuYunHu + * @Date: 2024/3/27 16:09 + * @Description: 编辑车辆 + * @Param: [editParams] + * @Return: com.couplet.common.core.domain.Result + **/ + @PostMapping("/editById") + @Log(title = "编辑车辆", businessType = BusinessType.UPDATE) + public Result editById(@RequestBody LyhVehicleEditParams editParams) { + + String result = lyhVehicleService.editById(editParams); + + return Result.success(result); + } + + /* + * @Author: LiuYunHu + * @Date: 2024/3/27 16:21 + * @Description: 新增车辆 + * @Param: [insertParams] + * @Return: com.couplet.common.core.domain.Result + **/ + @PostMapping("/insert") + @Log(title = "新增车辆", businessType = BusinessType.INSERT) + public Result insert(@RequestBody LyhVehicleInsertParams insertParams) { + + String result = lyhVehicleService.insert(insertParams); + + return Result.success(result); + } } diff --git a/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/LyhVehicle.java b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/LyhVehicle.java index bec3f15..26058ed 100644 --- a/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/LyhVehicle.java +++ b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/LyhVehicle.java @@ -4,10 +4,8 @@ import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; -import com.couplet.common.core.web.domain.BaseEntity; import lombok.AllArgsConstructor; import lombok.Data; -import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; diff --git a/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/req/LyhVehicleEditParams.java b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/req/LyhVehicleEditParams.java new file mode 100644 index 0000000..7a99349 --- /dev/null +++ b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/req/LyhVehicleEditParams.java @@ -0,0 +1,51 @@ +package com.couplet.vehicle.domain.req; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.SuperBuilder; + +/** + * @ProjectName: five-groups-couplet + * @Author: LiuYunHu + * @CreateTime: 2024/3/27 + * @Description: 车辆编辑入参 + */ + +@Data +@SuperBuilder +@AllArgsConstructor +@NoArgsConstructor +public class LyhVehicleEditParams { + /* + *车辆id + * */ + private Long vehicleId; + + /* + *车辆类型 + * */ + private Integer vehicleType; + + + /* + *电机厂商 + * */ + private String motorManufacturer; + + /* + *电池厂商 + * */ + private String batteryManufacturer; + + /* + *电机编号 + * */ + private String motorNumber; + + /* + *电池编号 + * */ + private String batteryNumber; + +} diff --git a/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/req/LyhVehicleInsertParams.java b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/req/LyhVehicleInsertParams.java new file mode 100644 index 0000000..587a1fd --- /dev/null +++ b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/req/LyhVehicleInsertParams.java @@ -0,0 +1,55 @@ +package com.couplet.vehicle.domain.req; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.experimental.SuperBuilder; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + +/** + * @ProjectName: five-groups-couplet + * @Author: LiuYunHu + * @CreateTime: 2024/3/27 + * @Description: 车辆编辑入参 + */ + +@Data +@SuperBuilder +@AllArgsConstructor +@NoArgsConstructor +public class LyhVehicleInsertParams { + + /* + *车辆类型 + * */ + @NotNull(message = "车辆类型不能为空") + private Integer vehicleType; + + + /* + *电机厂商 + * */ + @NotBlank(message = "电机厂商不能为空") + private String motorManufacturer; + + /* + *电池厂商 + * */ + @NotBlank(message = "电池厂商不能为空") + private String batteryManufacturer; + + /* + *电机编号 + * */ + @NotBlank(message = "电机编号不能为空") + private String motorNumber; + + /* + *电池编号 + * */ + @NotBlank(message = "电池编号不能为空") + private String batteryNumber; + +} diff --git a/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/req/LyhVehicleListParams.java b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/req/LyhVehicleListParams.java index 767ccde..382e5b6 100644 --- a/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/req/LyhVehicleListParams.java +++ b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/domain/req/LyhVehicleListParams.java @@ -1,11 +1,7 @@ package com.couplet.vehicle.domain.req; -import com.baomidou.mybatisplus.annotation.TableField; -import com.couplet.common.core.web.domain.BaseEntity; -import io.swagger.models.auth.In; import lombok.AllArgsConstructor; import lombok.Data; -import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; diff --git a/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/service/LyhVehicleService.java b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/service/LyhVehicleService.java index e92fd4a..b230bd6 100644 --- a/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/service/LyhVehicleService.java +++ b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/service/LyhVehicleService.java @@ -2,6 +2,8 @@ package com.couplet.vehicle.service; import com.baomidou.mybatisplus.extension.service.IService; import com.couplet.vehicle.domain.LyhVehicle; +import com.couplet.vehicle.domain.req.LyhVehicleEditParams; +import com.couplet.vehicle.domain.req.LyhVehicleInsertParams; import com.couplet.vehicle.domain.req.LyhVehicleListParams; import java.util.List; @@ -17,4 +19,8 @@ public interface LyhVehicleService extends IService { List list(LyhVehicleListParams listParams); String deleteById(Long vehicleId); + + String editById(LyhVehicleEditParams editParams); + + String insert(LyhVehicleInsertParams insertParams); } diff --git a/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/service/impl/LyhVehicleServiceImpl.java b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/service/impl/LyhVehicleServiceImpl.java index c824e79..14953f9 100644 --- a/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/service/impl/LyhVehicleServiceImpl.java +++ b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/service/impl/LyhVehicleServiceImpl.java @@ -5,9 +5,12 @@ import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.couplet.common.core.utils.StringUtils; import com.couplet.vehicle.domain.LyhVehicle; +import com.couplet.vehicle.domain.req.LyhVehicleEditParams; +import com.couplet.vehicle.domain.req.LyhVehicleInsertParams; import com.couplet.vehicle.domain.req.LyhVehicleListParams; import com.couplet.vehicle.mapper.LyhVehicleMapper; import com.couplet.vehicle.service.LyhVehicleService; +import com.couplet.vehicle.utils.SnowflakeIdGenerator; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -83,4 +86,77 @@ public class LyhVehicleServiceImpl extends ServiceImpl updateWrapper = new UpdateWrapper<>(); + + //编辑车辆类型 + + updateWrapper.set("vehicle_type", editParams.getVehicleType()) + + //编辑电机厂商 + .set("motor_manufacturer", editParams.getMotorManufacturer()) + + //编辑电池厂商 + .set("battery_manufacturer", editParams.getBatteryManufacturer()) + + //编辑电机编号 + .set("motor_number", editParams.getMotorNumber()) + + //编辑电池编号 + .set("battery_number", editParams.getBatteryNumber()) + + //匹配车辆id + .eq("vehicle_id", editParams.getVehicleId()); + + + boolean update = update(updateWrapper); + + if (!update) { + result = "编辑失败"; + throw new RuntimeException(result); + } + + result = "编辑成功!"; + + return result; + } + + /* + * @Author: LiuYunHu + * @Date: 2024/3/27 16:34 + * @Description: 新增车辆 + * @Param: [insertParams] + * @Return: java.lang.String + **/ + @Override + public String insert(LyhVehicleInsertParams insertParams) { + String result = ""; + + SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1, 1); + long randomId = idGenerator.nextId(); + String vin = "VIN" + randomId; + + int insert = lyhVehicleMapper.insert(new LyhVehicle(null, insertParams.getVehicleType(), insertParams.getMotorManufacturer(), insertParams.getBatteryManufacturer(), insertParams.getMotorNumber(), insertParams.getBatteryNumber(), vin, 0, 0)); + + if (insert == 0) { + result = "新增失败"; + throw new RuntimeException(result); + } + + result = "新增成功!"; + + return result; + } + } diff --git a/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/utils/SnowflakeIdGenerator.java b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/utils/SnowflakeIdGenerator.java new file mode 100644 index 0000000..3f6132c --- /dev/null +++ b/couplet-modules/couplet-modules-vehicle/src/main/java/com/couplet/vehicle/utils/SnowflakeIdGenerator.java @@ -0,0 +1,79 @@ +package com.couplet.vehicle.utils; + +/** + * @ProjectName: five-groups-couplet + * @Author: LiuYunHu + * @CreateTime: 2024/3/27 + * @Description: 雪花ID生成器 + * 使用方法 + * SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1, 1); // 创建一个 ID 生成器,传入机器 ID 和数据中心 ID + * long id = idGenerator.nextId(); // 生成 ID + */ + +public class SnowflakeIdGenerator { + + private final long epoch = 1420041600000L; // 起始时间戳,用于缩小时间戳范围,可根据实际情况调整 + private final long workerIdBits = 5L; // 机器 ID 所占位数 + private final long dataCenterIdBits = 5L; // 数据中心 ID 所占位数 + private final long sequenceBits = 12L; // 序列号所占位数 + + private final long maxWorkerId = -1L ^ (-1L << workerIdBits); // 机器 ID 最大值 + private final long maxDataCenterId = -1L ^ (-1L << dataCenterIdBits); // 数据中心 ID 最大值 + private final long maxSequence = -1L ^ (-1L << sequenceBits); // 序列号最大值 + + private final long workerIdShift = sequenceBits; // 机器 ID 向左移动位数 + private final long dataCenterIdShift = sequenceBits + workerIdBits; // 数据中心 ID 向左移动位数 + private final long timestampShift = sequenceBits + workerIdBits + dataCenterIdBits; // 时间戳向左移动位数 + + private long workerId; // 机器 ID + private long dataCenterId; // 数据中心 ID + private long sequence = 0L; // 序列号 + private long lastTimestamp = -1L; // 上次生成的时间戳 + + public SnowflakeIdGenerator(long workerId, long dataCenterId) { + if (workerId > maxWorkerId || workerId < 0) { + throw new IllegalArgumentException("Worker ID can't be greater than " + maxWorkerId + " or less than 0"); + } + if (dataCenterId > maxDataCenterId || dataCenterId < 0) { + throw new IllegalArgumentException("Data center ID can't be greater than " + maxDataCenterId + " or less than 0"); + } + this.workerId = workerId; + this.dataCenterId = dataCenterId; + } + + public synchronized long nextId() { + long timestamp = timeGen(); + + if (timestamp < lastTimestamp) { + throw new RuntimeException("Clock moved backwards, refusing to generate ID"); + } + + if (timestamp == lastTimestamp) { + sequence = (sequence + 1) & maxSequence; + if (sequence == 0) { + timestamp = tilNextMillis(lastTimestamp); + } + } else { + sequence = 0L; + } + + lastTimestamp = timestamp; + + return ((timestamp - epoch) << timestampShift) | + (dataCenterId << dataCenterIdShift) | + (workerId << workerIdShift) | + sequence; + } + + private long tilNextMillis(long lastTimestamp) { + long timestamp = timeGen(); + while (timestamp <= lastTimestamp) { + timestamp = timeGen(); + } + return timestamp; + } + + private long timeGen() { + return System.currentTimeMillis(); + } +} diff --git a/couplet-modules/couplet-modules-vehicle/src/test/java/IdTest.java b/couplet-modules/couplet-modules-vehicle/src/test/java/IdTest.java new file mode 100644 index 0000000..8a754b4 --- /dev/null +++ b/couplet-modules/couplet-modules-vehicle/src/test/java/IdTest.java @@ -0,0 +1,20 @@ +import com.couplet.vehicle.utils.SnowflakeIdGenerator; + +/** + * @ProjectName: five-groups-couplet + * @Author: LiuYunHu + * @CreateTime: 2024/3/27 + * @Description: + */ + +public class IdTest { + + + public static void main(String[] args) { + SnowflakeIdGenerator idGenerator = new SnowflakeIdGenerator(1, 1); + + long l = idGenerator.nextId(); + System.out.println(l); + + } +}