From b91fdc439ca1ba0b98409de0f1c0edf6fd626ee9 Mon Sep 17 00:00:00 2001 From: ‘liusuyi’ <1951119284@qq.com> Date: 星期五, 21 七月 2023 14:16:35 +0800 Subject: [PATCH] 增加app一键报警 --- src/main/java/com/ard/alarm/apponekey/service/impl/ArdAlarmApponekeyServiceImpl.java | 111 +++++ src/main/java/com/ard/utils/hiksdk/util/minio/MinioUtils.java | 7 src/main/java/com/ard/utils/uuid/IdUtils.java | 50 ++ src/main/java/com/ard/alarm/apponekey/controller/ArdAlarmApponekeyController.java | 40 ++ src/main/java/com/ard/utils/http/HttpStatus.java | 95 ++++ src/main/java/com/ard/utils/http/AjaxResult.java | 204 ++++++++++ src/main/java/com/ard/alarm/apponekey/service/IArdAlarmApponekeyService.java | 61 +++ src/main/java/com/ard/alarm/apponekey/mapper/ArdAlarmApponekeyMapper.java | 63 +++ src/main/java/com/ard/alarm/apponekey/domain/ArdAlarmApponekey.java | 40 ++ src/main/java/com/ard/utils/uuid/UUID.java | 485 ++++++++++++++++++++++++ 10 files changed, 1,154 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/ard/alarm/apponekey/controller/ArdAlarmApponekeyController.java b/src/main/java/com/ard/alarm/apponekey/controller/ArdAlarmApponekeyController.java new file mode 100644 index 0000000..eb9295d --- /dev/null +++ b/src/main/java/com/ard/alarm/apponekey/controller/ArdAlarmApponekeyController.java @@ -0,0 +1,40 @@ +package com.ard.alarm.apponekey.controller; + +import com.ard.utils.hiksdk.util.minio.MinioUtils; +import com.ard.utils.http.AjaxResult; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; +import com.ard.alarm.apponekey.domain.ArdAlarmApponekey; +import com.ard.alarm.apponekey.service.IArdAlarmApponekeyService; +import org.springframework.web.multipart.MultipartFile; + + +/** + * app涓�閿姤璀ontroller + * + * @author ard + * @date 2023-07-21 + */ +@RestController +@RequestMapping("/alarm/apponekey") +public class ArdAlarmApponekeyController { + @Autowired + private IArdAlarmApponekeyService ardAlarmApponekeyService; + + + /** + * 鏂板app涓�閿姤璀� + */ + @PostMapping + public AjaxResult add(String userId,String name, Double longitude,Double latitude,Double altitude,MultipartFile file) { + String url = MinioUtils.putObjectAndGetUrl("record", file); + ArdAlarmApponekey ardAlarmApponekey =new ArdAlarmApponekey(); + ardAlarmApponekey.setUserId(userId); + ardAlarmApponekey.setName(name); + ardAlarmApponekey.setLongitude(longitude); + ardAlarmApponekey.setLatitude(latitude); + ardAlarmApponekey.setAltitude(altitude); + ardAlarmApponekey.setRecordUrl(url); + return AjaxResult.success(ardAlarmApponekeyService.insertArdAlarmApponekey(ardAlarmApponekey)); + } +} diff --git a/src/main/java/com/ard/alarm/apponekey/domain/ArdAlarmApponekey.java b/src/main/java/com/ard/alarm/apponekey/domain/ArdAlarmApponekey.java new file mode 100644 index 0000000..2e85fc7 --- /dev/null +++ b/src/main/java/com/ard/alarm/apponekey/domain/ArdAlarmApponekey.java @@ -0,0 +1,40 @@ +package com.ard.alarm.apponekey.domain; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import java.util.Date; + +/** + * app涓�閿姤璀﹀璞� ard_alarm_apponekey + * + * @author ard + * @date 2023-07-21 + */ +@Data +public class ArdAlarmApponekey +{ + private static final long serialVersionUID = 1L; + + /** ID */ + private String id; + + /** app鐢ㄦ埛ID */ + private String userId; + /** app鐢ㄦ埛濮撳悕 */ + private String name; + /** 缁忓害 */ + private Double longitude; + + /** 绾害 */ + private Double latitude; + + /** 楂樺眰 */ + private Double altitude; + + /** 褰曞儚url */ + private String recordUrl; + + /** 鎺ユ敹鍒涘缓鏃堕棿 */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date createTime; +} diff --git a/src/main/java/com/ard/alarm/apponekey/mapper/ArdAlarmApponekeyMapper.java b/src/main/java/com/ard/alarm/apponekey/mapper/ArdAlarmApponekeyMapper.java new file mode 100644 index 0000000..beb1681 --- /dev/null +++ b/src/main/java/com/ard/alarm/apponekey/mapper/ArdAlarmApponekeyMapper.java @@ -0,0 +1,63 @@ +package com.ard.alarm.apponekey.mapper; + +import java.util.List; +import com.ard.alarm.apponekey.domain.ArdAlarmApponekey; +import org.apache.ibatis.annotations.Mapper; + +/** + * app涓�閿姤璀apper鎺ュ彛 + * + * @author ard + * @date 2023-07-21 + */ +@Mapper +public interface ArdAlarmApponekeyMapper +{ + /** + * 鏌ヨapp涓�閿姤璀� + * + * @param id app涓�閿姤璀︿富閿� + * @return app涓�閿姤璀� + */ + public ArdAlarmApponekey selectArdAlarmApponekeyById(String id); + + /** + * 鏌ヨapp涓�閿姤璀﹀垪琛� + * + * @param ardAlarmApponekey app涓�閿姤璀� + * @return app涓�閿姤璀﹂泦鍚� + */ + public List<ArdAlarmApponekey> selectArdAlarmApponekeyList(ArdAlarmApponekey ardAlarmApponekey); + + /** + * 鏂板app涓�閿姤璀� + * + * @param ardAlarmApponekey app涓�閿姤璀� + * @return 缁撴灉 + */ + public int insertArdAlarmApponekey(ArdAlarmApponekey ardAlarmApponekey); + + /** + * 淇敼app涓�閿姤璀� + * + * @param ardAlarmApponekey app涓�閿姤璀� + * @return 缁撴灉 + */ + public int updateArdAlarmApponekey(ArdAlarmApponekey ardAlarmApponekey); + + /** + * 鍒犻櫎app涓�閿姤璀� + * + * @param id app涓�閿姤璀︿富閿� + * @return 缁撴灉 + */ + public int deleteArdAlarmApponekeyById(String id); + + /** + * 鎵归噺鍒犻櫎app涓�閿姤璀� + * + * @param ids 闇�瑕佸垹闄ょ殑鏁版嵁涓婚敭闆嗗悎 + * @return 缁撴灉 + */ + public int deleteArdAlarmApponekeyByIds(String[] ids); +} diff --git a/src/main/java/com/ard/alarm/apponekey/service/IArdAlarmApponekeyService.java b/src/main/java/com/ard/alarm/apponekey/service/IArdAlarmApponekeyService.java new file mode 100644 index 0000000..6d69469 --- /dev/null +++ b/src/main/java/com/ard/alarm/apponekey/service/IArdAlarmApponekeyService.java @@ -0,0 +1,61 @@ +package com.ard.alarm.apponekey.service; + +import java.util.List; +import com.ard.alarm.apponekey.domain.ArdAlarmApponekey; + +/** + * app涓�閿姤璀ervice鎺ュ彛 + * + * @author ard + * @date 2023-07-21 + */ +public interface IArdAlarmApponekeyService +{ + /** + * 鏌ヨapp涓�閿姤璀� + * + * @param id app涓�閿姤璀︿富閿� + * @return app涓�閿姤璀� + */ + public ArdAlarmApponekey selectArdAlarmApponekeyById(String id); + + /** + * 鏌ヨapp涓�閿姤璀﹀垪琛� + * + * @param ardAlarmApponekey app涓�閿姤璀� + * @return app涓�閿姤璀﹂泦鍚� + */ + public List<ArdAlarmApponekey> selectArdAlarmApponekeyList(ArdAlarmApponekey ardAlarmApponekey); + + /** + * 鏂板app涓�閿姤璀� + * + * @param ardAlarmApponekey app涓�閿姤璀� + * @return 缁撴灉 + */ + public int insertArdAlarmApponekey(ArdAlarmApponekey ardAlarmApponekey); + + /** + * 淇敼app涓�閿姤璀� + * + * @param ardAlarmApponekey app涓�閿姤璀� + * @return 缁撴灉 + */ + public int updateArdAlarmApponekey(ArdAlarmApponekey ardAlarmApponekey); + + /** + * 鎵归噺鍒犻櫎app涓�閿姤璀� + * + * @param ids 闇�瑕佸垹闄ょ殑app涓�閿姤璀︿富閿泦鍚� + * @return 缁撴灉 + */ + public int deleteArdAlarmApponekeyByIds(String[] ids); + + /** + * 鍒犻櫎app涓�閿姤璀︿俊鎭� + * + * @param id app涓�閿姤璀︿富閿� + * @return 缁撴灉 + */ + public int deleteArdAlarmApponekeyById(String id); +} diff --git a/src/main/java/com/ard/alarm/apponekey/service/impl/ArdAlarmApponekeyServiceImpl.java b/src/main/java/com/ard/alarm/apponekey/service/impl/ArdAlarmApponekeyServiceImpl.java new file mode 100644 index 0000000..213eb9f --- /dev/null +++ b/src/main/java/com/ard/alarm/apponekey/service/impl/ArdAlarmApponekeyServiceImpl.java @@ -0,0 +1,111 @@ +package com.ard.alarm.apponekey.service.impl; + +import java.util.Date; +import java.util.List; + +import com.alibaba.fastjson2.JSON; +import com.ard.utils.mqtt.MqttConsumer; +import com.ard.utils.uuid.IdUtils; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import com.ard.alarm.apponekey.mapper.ArdAlarmApponekeyMapper; +import com.ard.alarm.apponekey.domain.ArdAlarmApponekey; +import com.ard.alarm.apponekey.service.IArdAlarmApponekeyService; + +import javax.annotation.Resource; + +import static jdk.nashorn.internal.runtime.regexp.joni.Config.log; + +/** + * app涓�閿姤璀ervice涓氬姟灞傚鐞� + * + * @author ard + * @date 2023-07-21 + */ +@Service +@Slf4j(topic = "apponekey") +public class ArdAlarmApponekeyServiceImpl implements IArdAlarmApponekeyService { + @Resource + private ArdAlarmApponekeyMapper ardAlarmApponekeyMapper; + + /** + * 鏌ヨapp涓�閿姤璀� + * + * @param id app涓�閿姤璀︿富閿� + * @return app涓�閿姤璀� + */ + @Override + public ArdAlarmApponekey selectArdAlarmApponekeyById(String id) { + return ardAlarmApponekeyMapper.selectArdAlarmApponekeyById(id); + } + + /** + * 鏌ヨapp涓�閿姤璀﹀垪琛� + * + * @param ardAlarmApponekey app涓�閿姤璀� + * @return app涓�閿姤璀� + */ + @Override + public List<ArdAlarmApponekey> selectArdAlarmApponekeyList(ArdAlarmApponekey ardAlarmApponekey) { + return ardAlarmApponekeyMapper.selectArdAlarmApponekeyList(ardAlarmApponekey); + } + + /** + * 鏂板app涓�閿姤璀� + * + * @param ardAlarmApponekey app涓�閿姤璀� + * @return 缁撴灉 + */ + @Override + public int insertArdAlarmApponekey(ArdAlarmApponekey ardAlarmApponekey) { + try { + ardAlarmApponekey.setId(IdUtils.fastSimpleUUID()); + ardAlarmApponekey.setCreateTime(new Date()); + printLog(ardAlarmApponekey); + MqttConsumer.publish(2, false, "apponekey", JSON.toJSONString(ardAlarmApponekey)); + return 1; + } catch (Exception ex) { + log.error("涓�閿姤璀﹀鐞嗗紓甯�:" + ex.getMessage()); + return 0; + } + } + + private void printLog(ArdAlarmApponekey ardAlarmApponekey) { + log.info("鏀跺埌鏁版嵁:" + "銆怚D銆�" + ardAlarmApponekey.getId() + "銆恥serId銆�" + ardAlarmApponekey.getUserId() + + "銆愮粡搴︺��" + ardAlarmApponekey.getLongitude() + "銆愮含搴︺��" + ardAlarmApponekey.getLatitude() + + "銆愰珮灞傘��" + ardAlarmApponekey.getAltitude() + "銆愬垱寤烘椂闂淬��" + ardAlarmApponekey.getCreateTime()); + } + + /** + * 淇敼app涓�閿姤璀� + * + * @param ardAlarmApponekey app涓�閿姤璀� + * @return 缁撴灉 + */ + @Override + public int updateArdAlarmApponekey(ArdAlarmApponekey ardAlarmApponekey) { + return ardAlarmApponekeyMapper.updateArdAlarmApponekey(ardAlarmApponekey); + } + + /** + * 鎵归噺鍒犻櫎app涓�閿姤璀� + * + * @param ids 闇�瑕佸垹闄ょ殑app涓�閿姤璀︿富閿� + * @return 缁撴灉 + */ + @Override + public int deleteArdAlarmApponekeyByIds(String[] ids) { + return ardAlarmApponekeyMapper.deleteArdAlarmApponekeyByIds(ids); + } + + /** + * 鍒犻櫎app涓�閿姤璀︿俊鎭� + * + * @param id app涓�閿姤璀︿富閿� + * @return 缁撴灉 + */ + @Override + public int deleteArdAlarmApponekeyById(String id) { + return ardAlarmApponekeyMapper.deleteArdAlarmApponekeyById(id); + } +} diff --git a/src/main/java/com/ard/utils/hiksdk/util/minio/MinioUtils.java b/src/main/java/com/ard/utils/hiksdk/util/minio/MinioUtils.java index a21ee68..0e600e0 100644 --- a/src/main/java/com/ard/utils/hiksdk/util/minio/MinioUtils.java +++ b/src/main/java/com/ard/utils/hiksdk/util/minio/MinioUtils.java @@ -154,7 +154,7 @@ //鏂囦欢鍚� String originalFilename = file.getOriginalFilename(); //鏂扮殑鏂囦欢鍚� - String fileName = UUID.randomUUID().toString().replace("-", "") + originalFilename; + String fileName = UUID.randomUUID().toString().replace("-", "") +"_"+ originalFilename; try { InputStream inputStream = file.getInputStream(); /*涓婁紶瀵硅薄*/ @@ -170,10 +170,13 @@ /*鑾峰彇url*/ GetPresignedObjectUrlArgs getPresignedObjectUrlArgs = GetPresignedObjectUrlArgs .builder() + .method(Method.GET) .bucket(bucketName) .object(fileName) .build(); - return MinioClientSingleton.getMinioClient().getPresignedObjectUrl(getPresignedObjectUrlArgs); + String presignedObjectUrl = MinioClientSingleton.getMinioClient().getPresignedObjectUrl(getPresignedObjectUrlArgs); + String ObjectUrl = presignedObjectUrl.substring(0, presignedObjectUrl.indexOf("?")); + return ObjectUrl; } catch (Exception ex) { log.error("涓婁紶瀵硅薄杩斿洖url寮傚父锛�" + ex.getMessage()); } diff --git a/src/main/java/com/ard/utils/http/AjaxResult.java b/src/main/java/com/ard/utils/http/AjaxResult.java new file mode 100644 index 0000000..f2d1beb --- /dev/null +++ b/src/main/java/com/ard/utils/http/AjaxResult.java @@ -0,0 +1,204 @@ +package com.ard.utils.http; + +import java.util.HashMap; + +/** + * 鎿嶄綔娑堟伅鎻愰啋 + * + * @author ruoyi + */ +public class AjaxResult extends HashMap<String, Object> +{ + private static final long serialVersionUID = 1L; + + /** 鐘舵�佺爜 */ + public static final String CODE_TAG = "code"; + + /** 杩斿洖鍐呭 */ + public static final String MSG_TAG = "msg"; + + /** 鏁版嵁瀵硅薄 */ + public static final String DATA_TAG = "data"; + + /** + * 鍒濆鍖栦竴涓柊鍒涘缓鐨� AjaxResult 瀵硅薄锛屼娇鍏惰〃绀轰竴涓┖娑堟伅銆� + */ + public AjaxResult() + { + } + + /** + * 鍒濆鍖栦竴涓柊鍒涘缓鐨� AjaxResult 瀵硅薄 + * + * @param code 鐘舵�佺爜 + * @param msg 杩斿洖鍐呭 + */ + public AjaxResult(int code, String msg) + { + super.put(CODE_TAG, code); + super.put(MSG_TAG, msg); + } + + /** + * 鍒濆鍖栦竴涓柊鍒涘缓鐨� AjaxResult 瀵硅薄 + * + * @param code 鐘舵�佺爜 + * @param msg 杩斿洖鍐呭 + * @param data 鏁版嵁瀵硅薄 + */ + public AjaxResult(int code, String msg, Object data) + { + super.put(CODE_TAG, code); + super.put(MSG_TAG, msg); + if (data!=null) + { + super.put(DATA_TAG, data); + } + } + + /** + * 杩斿洖鎴愬姛娑堟伅 + * + * @return 鎴愬姛娑堟伅 + */ + public static AjaxResult success() + { + return AjaxResult.success("鎿嶄綔鎴愬姛"); + } + + /** + * 杩斿洖鎴愬姛鏁版嵁 + * + * @return 鎴愬姛娑堟伅 + */ + public static AjaxResult success(Object data) + { + return AjaxResult.success("鎿嶄綔鎴愬姛", data); + } + + /** + * 杩斿洖鎴愬姛娑堟伅 + * + * @param msg 杩斿洖鍐呭 + * @return 鎴愬姛娑堟伅 + */ + public static AjaxResult success(String msg) + { + return AjaxResult.success(msg, null); + } + + /** + * 杩斿洖鎴愬姛娑堟伅 + * + * @param msg 杩斿洖鍐呭 + * @param data 鏁版嵁瀵硅薄 + * @return 鎴愬姛娑堟伅 + */ + public static AjaxResult success(String msg, Object data) + { + return new AjaxResult(HttpStatus.SUCCESS, msg, data); + } + + /** + * 杩斿洖璀﹀憡娑堟伅 + * + * @param msg 杩斿洖鍐呭 + * @return 璀﹀憡娑堟伅 + */ + public static AjaxResult warn(String msg) + { + return AjaxResult.warn(msg, null); + } + + /** + * 杩斿洖璀﹀憡娑堟伅 + * + * @param msg 杩斿洖鍐呭 + * @param data 鏁版嵁瀵硅薄 + * @return 璀﹀憡娑堟伅 + */ + public static AjaxResult warn(String msg, Object data) + { + return new AjaxResult(HttpStatus.WARN, msg, data); + } + + /** + * 杩斿洖閿欒娑堟伅 + * + * @return 閿欒娑堟伅 + */ + public static AjaxResult error() + { + return AjaxResult.error("鎿嶄綔澶辫触"); + } + + /** + * 杩斿洖閿欒娑堟伅 + * + * @param msg 杩斿洖鍐呭 + * @return 閿欒娑堟伅 + */ + public static AjaxResult error(String msg) + { + return AjaxResult.error(msg, null); + } + + /** + * 杩斿洖閿欒娑堟伅 + * + * @param msg 杩斿洖鍐呭 + * @param data 鏁版嵁瀵硅薄 + * @return 閿欒娑堟伅 + */ + public static AjaxResult error(String msg, Object data) + { + return new AjaxResult(HttpStatus.ERROR, msg, data); + } + + /** + * 杩斿洖閿欒娑堟伅 + * + * @param code 鐘舵�佺爜 + * @param msg 杩斿洖鍐呭 + * @return 閿欒娑堟伅 + */ + public static AjaxResult error(int code, String msg) + { + return new AjaxResult(code, msg, null); + } + + /** + * 鏂逛究閾惧紡璋冪敤 + * + * @param key 閿� + * @param value 鍊� + * @return 鏁版嵁瀵硅薄 + */ + @Override + public AjaxResult put(String key, Object value) + { + super.put(key, value); + return this; + } + /** + * 鍝嶅簲杩斿洖缁撴灉 + * + * @param rows 褰卞搷琛屾暟 + * @return 鎿嶄綔缁撴灉 + */ + public static AjaxResult toAjax(int rows) + { + return rows > 0 ? AjaxResult.success() : AjaxResult.error(); + } + /** + * 鍝嶅簲杩斿洖缁撴灉 + * + * @param result 缁撴灉 + * @return 鎿嶄綔缁撴灉 + */ + public static AjaxResult toAjax(boolean result) + { + return result ? success() : error(); + } +} + diff --git a/src/main/java/com/ard/utils/http/HttpStatus.java b/src/main/java/com/ard/utils/http/HttpStatus.java new file mode 100644 index 0000000..cafc54c --- /dev/null +++ b/src/main/java/com/ard/utils/http/HttpStatus.java @@ -0,0 +1,95 @@ +package com.ard.utils.http; + +/** + * 杩斿洖鐘舵�佺爜 + * + * @author ruoyi + */ +public class HttpStatus +{ + /** + * 鎿嶄綔鎴愬姛 + */ + public static final int SUCCESS = 200; + + /** + * 瀵硅薄鍒涘缓鎴愬姛 + */ + public static final int CREATED = 201; + + /** + * 璇锋眰宸茬粡琚帴鍙� + */ + public static final int ACCEPTED = 202; + + /** + * 鎿嶄綔宸茬粡鎵ц鎴愬姛锛屼絾鏄病鏈夎繑鍥炴暟鎹� + */ + public static final int NO_CONTENT = 204; + + /** + * 璧勬簮宸茶绉婚櫎 + */ + public static final int MOVED_PERM = 301; + + /** + * 閲嶅畾鍚� + */ + public static final int SEE_OTHER = 303; + + /** + * 璧勬簮娌℃湁琚慨鏀� + */ + public static final int NOT_MODIFIED = 304; + + /** + * 鍙傛暟鍒楄〃閿欒锛堢己灏戯紝鏍煎紡涓嶅尮閰嶏級 + */ + public static final int BAD_REQUEST = 400; + + /** + * 鏈巿鏉� + */ + public static final int UNAUTHORIZED = 401; + + /** + * 璁块棶鍙楅檺锛屾巿鏉冭繃鏈� + */ + public static final int FORBIDDEN = 403; + + /** + * 璧勬簮锛屾湇鍔℃湭鎵惧埌 + */ + public static final int NOT_FOUND = 404; + + /** + * 涓嶅厑璁哥殑http鏂规硶 + */ + public static final int BAD_METHOD = 405; + + /** + * 璧勬簮鍐茬獊锛屾垨鑰呰祫婧愯閿� + */ + public static final int CONFLICT = 409; + + /** + * 涓嶆敮鎸佺殑鏁版嵁锛屽獟浣撶被鍨� + */ + public static final int UNSUPPORTED_TYPE = 415; + + /** + * 绯荤粺鍐呴儴閿欒 + */ + public static final int ERROR = 500; + + /** + * 鎺ュ彛鏈疄鐜� + */ + public static final int NOT_IMPLEMENTED = 501; + + /** + * 绯荤粺璀﹀憡娑堟伅 + */ + public static final int WARN = 601; +} + diff --git a/src/main/java/com/ard/utils/uuid/IdUtils.java b/src/main/java/com/ard/utils/uuid/IdUtils.java new file mode 100644 index 0000000..7c708ae --- /dev/null +++ b/src/main/java/com/ard/utils/uuid/IdUtils.java @@ -0,0 +1,50 @@ +package com.ard.utils.uuid; + + +/** + * ID鐢熸垚鍣ㄥ伐鍏风被 + * + * @author ruoyi + */ +public class IdUtils +{ + /** + * 鑾峰彇闅忔満UUID + * + * @return 闅忔満UUID + */ + public static String randomUUID() + { + return UUID.randomUUID().toString(); + } + + /** + * 绠�鍖栫殑UUID锛屽幓鎺変簡妯嚎 + * + * @return 绠�鍖栫殑UUID锛屽幓鎺変簡妯嚎 + */ + public static String simpleUUID() + { + return UUID.randomUUID().toString(true); + } + + /** + * 鑾峰彇闅忔満UUID锛屼娇鐢ㄦ�ц兘鏇村ソ鐨凾hreadLocalRandom鐢熸垚UUID + * + * @return 闅忔満UUID + */ + public static String fastUUID() + { + return UUID.fastUUID().toString(); + } + + /** + * 绠�鍖栫殑UUID锛屽幓鎺変簡妯嚎锛屼娇鐢ㄦ�ц兘鏇村ソ鐨凾hreadLocalRandom鐢熸垚UUID + * + * @return 绠�鍖栫殑UUID锛屽幓鎺変簡妯嚎 + */ + public static String fastSimpleUUID() + { + return UUID.fastUUID().toString(true); + } +} diff --git a/src/main/java/com/ard/utils/uuid/UUID.java b/src/main/java/com/ard/utils/uuid/UUID.java new file mode 100644 index 0000000..bffe30b --- /dev/null +++ b/src/main/java/com/ard/utils/uuid/UUID.java @@ -0,0 +1,485 @@ +package com.ard.utils.uuid; + +import com.sun.xml.internal.ws.util.UtilException; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.util.Random; +import java.util.concurrent.ThreadLocalRandom; + +/** + * 鎻愪緵閫氱敤鍞竴璇嗗埆鐮侊紙universally unique identifier锛夛紙UUID锛夊疄鐜� + * + * @author ruoyi + */ +public final class UUID implements java.io.Serializable, Comparable<UUID> +{ + private static final long serialVersionUID = -1185015143654744140L; + + /** + * SecureRandom 鐨勫崟渚� + * + */ + private static class Holder + { + static final SecureRandom numberGenerator = getSecureRandom(); + } + + /** 姝UID鐨勬渶楂�64鏈夋晥浣� */ + private final long mostSigBits; + + /** 姝UID鐨勬渶浣�64鏈夋晥浣� */ + private final long leastSigBits; + + /** + * 绉佹湁鏋勯�� + * + * @param data 鏁版嵁 + */ + private UUID(byte[] data) + { + long msb = 0; + long lsb = 0; + assert data.length == 16 : "data must be 16 bytes in length"; + for (int i = 0; i < 8; i++) + { + msb = (msb << 8) | (data[i] & 0xff); + } + for (int i = 8; i < 16; i++) + { + lsb = (lsb << 8) | (data[i] & 0xff); + } + this.mostSigBits = msb; + this.leastSigBits = lsb; + } + + /** + * 浣跨敤鎸囧畾鐨勬暟鎹瀯閫犳柊鐨� UUID銆� + * + * @param mostSigBits 鐢ㄤ簬 {@code UUID} 鐨勬渶楂樻湁鏁� 64 浣� + * @param leastSigBits 鐢ㄤ簬 {@code UUID} 鐨勬渶浣庢湁鏁� 64 浣� + */ + public UUID(long mostSigBits, long leastSigBits) + { + this.mostSigBits = mostSigBits; + this.leastSigBits = leastSigBits; + } + + /** + * 鑾峰彇绫诲瀷 4锛堜吉闅忔満鐢熸垚鐨勶級UUID 鐨勯潤鎬佸伐鍘傘�� 浣跨敤鍔犲瘑鐨勬湰鍦扮嚎绋嬩吉闅忔満鏁扮敓鎴愬櫒鐢熸垚璇� UUID銆� + * + * @return 闅忔満鐢熸垚鐨� {@code UUID} + */ + public static UUID fastUUID() + { + return randomUUID(false); + } + + /** + * 鑾峰彇绫诲瀷 4锛堜吉闅忔満鐢熸垚鐨勶級UUID 鐨勯潤鎬佸伐鍘傘�� 浣跨敤鍔犲瘑鐨勫己浼殢鏈烘暟鐢熸垚鍣ㄧ敓鎴愯 UUID銆� + * + * @return 闅忔満鐢熸垚鐨� {@code UUID} + */ + public static UUID randomUUID() + { + return randomUUID(true); + } + + /** + * 鑾峰彇绫诲瀷 4锛堜吉闅忔満鐢熸垚鐨勶級UUID 鐨勯潤鎬佸伐鍘傘�� 浣跨敤鍔犲瘑鐨勫己浼殢鏈烘暟鐢熸垚鍣ㄧ敓鎴愯 UUID銆� + * + * @param isSecure 鏄惁浣跨敤{@link SecureRandom}濡傛灉鏄彲浠ヨ幏寰楁洿瀹夊叏鐨勯殢鏈虹爜锛屽惁鍒欏彲浠ュ緱鍒版洿濂界殑鎬ц兘 + * @return 闅忔満鐢熸垚鐨� {@code UUID} + */ + public static UUID randomUUID(boolean isSecure) + { + final Random ng = isSecure ? Holder.numberGenerator : getRandom(); + + byte[] randomBytes = new byte[16]; + ng.nextBytes(randomBytes); + randomBytes[6] &= 0x0f; /* clear version */ + randomBytes[6] |= 0x40; /* set to version 4 */ + randomBytes[8] &= 0x3f; /* clear variant */ + randomBytes[8] |= 0x80; /* set to IETF variant */ + return new UUID(randomBytes); + } + + /** + * 鏍规嵁鎸囧畾鐨勫瓧鑺傛暟缁勮幏鍙栫被鍨� 3锛堝熀浜庡悕绉扮殑锛塙UID 鐨勯潤鎬佸伐鍘傘�� + * + * @param name 鐢ㄤ簬鏋勯�� UUID 鐨勫瓧鑺傛暟缁勩�� + * + * @return 鏍规嵁鎸囧畾鏁扮粍鐢熸垚鐨� {@code UUID} + */ + public static UUID nameUUIDFromBytes(byte[] name) + { + MessageDigest md; + try + { + md = MessageDigest.getInstance("MD5"); + } + catch (NoSuchAlgorithmException nsae) + { + throw new InternalError("MD5 not supported"); + } + byte[] md5Bytes = md.digest(name); + md5Bytes[6] &= 0x0f; /* clear version */ + md5Bytes[6] |= 0x30; /* set to version 3 */ + md5Bytes[8] &= 0x3f; /* clear variant */ + md5Bytes[8] |= 0x80; /* set to IETF variant */ + return new UUID(md5Bytes); + } + + /** + * 鏍规嵁 {@link #toString()} 鏂规硶涓弿杩扮殑瀛楃涓叉爣鍑嗚〃绀哄舰寮忓垱寤簕@code UUID}銆� + * + * @param name 鎸囧畾 {@code UUID} 瀛楃涓� + * @return 鍏锋湁鎸囧畾鍊肩殑 {@code UUID} + * @throws IllegalArgumentException 濡傛灉 name 涓� {@link #toString} 涓弿杩扮殑瀛楃涓茶〃绀哄舰寮忎笉绗︽姏鍑烘寮傚父 + * + */ + public static UUID fromString(String name) + { + String[] components = name.split("-"); + if (components.length != 5) + { + throw new IllegalArgumentException("Invalid UUID string: " + name); + } + for (int i = 0; i < 5; i++) + { + components[i] = "0x" + components[i]; + } + + long mostSigBits = Long.decode(components[0]).longValue(); + mostSigBits <<= 16; + mostSigBits |= Long.decode(components[1]).longValue(); + mostSigBits <<= 16; + mostSigBits |= Long.decode(components[2]).longValue(); + + long leastSigBits = Long.decode(components[3]).longValue(); + leastSigBits <<= 48; + leastSigBits |= Long.decode(components[4]).longValue(); + + return new UUID(mostSigBits, leastSigBits); + } + + /** + * 杩斿洖姝� UUID 鐨� 128 浣嶅�间腑鐨勬渶浣庢湁鏁� 64 浣嶃�� + * + * @return 姝� UUID 鐨� 128 浣嶅�间腑鐨勬渶浣庢湁鏁� 64 浣嶃�� + */ + public long getLeastSignificantBits() + { + return leastSigBits; + } + + /** + * 杩斿洖姝� UUID 鐨� 128 浣嶅�间腑鐨勬渶楂樻湁鏁� 64 浣嶃�� + * + * @return 姝� UUID 鐨� 128 浣嶅�间腑鏈�楂樻湁鏁� 64 浣嶃�� + */ + public long getMostSignificantBits() + { + return mostSigBits; + } + + /** + * 涓庢 {@code UUID} 鐩稿叧鑱旂殑鐗堟湰鍙�. 鐗堟湰鍙锋弿杩版 {@code UUID} 鏄浣曠敓鎴愮殑銆� + * <p> + * 鐗堟湰鍙峰叿鏈変互涓嬪惈鎰�: + * <ul> + * <li>1 鍩轰簬鏃堕棿鐨� UUID + * <li>2 DCE 瀹夊叏 UUID + * <li>3 鍩轰簬鍚嶇О鐨� UUID + * <li>4 闅忔満鐢熸垚鐨� UUID + * </ul> + * + * @return 姝� {@code UUID} 鐨勭増鏈彿 + */ + public int version() + { + // Version is bits masked by 0x000000000000F000 in MS long + return (int) ((mostSigBits >> 12) & 0x0f); + } + + /** + * 涓庢 {@code UUID} 鐩稿叧鑱旂殑鍙樹綋鍙枫�傚彉浣撳彿鎻忚堪 {@code UUID} 鐨勫竷灞�銆� + * <p> + * 鍙樹綋鍙峰叿鏈変互涓嬪惈鎰忥細 + * <ul> + * <li>0 涓� NCS 鍚戝悗鍏煎淇濈暀 + * <li>2 <a href="http://www.ietf.org/rfc/rfc4122.txt">IETF RFC 4122</a>(Leach-Salz), 鐢ㄤ簬姝ょ被 + * <li>6 淇濈暀锛屽井杞悜鍚庡吋瀹� + * <li>7 淇濈暀渚涗互鍚庡畾涔変娇鐢� + * </ul> + * + * @return 姝� {@code UUID} 鐩稿叧鑱旂殑鍙樹綋鍙� + */ + public int variant() + { + // This field is composed of a varying number of bits. + // 0 - - Reserved for NCS backward compatibility + // 1 0 - The IETF aka Leach-Salz variant (used by this class) + // 1 1 0 Reserved, Microsoft backward compatibility + // 1 1 1 Reserved for future definition. + return (int) ((leastSigBits >>> (64 - (leastSigBits >>> 62))) & (leastSigBits >> 63)); + } + + /** + * 涓庢 UUID 鐩稿叧鑱旂殑鏃堕棿鎴冲�笺�� + * + * <p> + * 60 浣嶇殑鏃堕棿鎴冲�兼牴鎹 {@code UUID} 鐨� time_low銆乼ime_mid 鍜� time_hi 瀛楁鏋勯�犮��<br> + * 鎵�寰楀埌鐨勬椂闂存埑浠� 100 姣井绉掍负鍗曚綅锛屼粠 UTC锛堥�氱敤鍗忚皟鏃堕棿锛� 1582 骞� 10 鏈� 15 鏃ラ浂鏃跺紑濮嬨�� + * + * <p> + * 鏃堕棿鎴冲�间粎鍦ㄥ湪鍩轰簬鏃堕棿鐨� UUID锛堝叾 version 绫诲瀷涓� 1锛変腑鎵嶆湁鎰忎箟銆�<br> + * 濡傛灉姝� {@code UUID} 涓嶆槸鍩轰簬鏃堕棿鐨� UUID锛屽垯姝ゆ柟娉曟姏鍑� UnsupportedOperationException銆� + * + * @throws UnsupportedOperationException 濡傛灉姝� {@code UUID} 涓嶆槸 version 涓� 1 鐨� UUID銆� + */ + public long timestamp() throws UnsupportedOperationException + { + checkTimeBase(); + return (mostSigBits & 0x0FFFL) << 48// + | ((mostSigBits >> 16) & 0x0FFFFL) << 32// + | mostSigBits >>> 32; + } + + /** + * 涓庢 UUID 鐩稿叧鑱旂殑鏃堕挓搴忓垪鍊笺�� + * + * <p> + * 14 浣嶇殑鏃堕挓搴忓垪鍊兼牴鎹 UUID 鐨� clock_seq 瀛楁鏋勯�犮�俢lock_seq 瀛楁鐢ㄤ簬淇濊瘉鍦ㄥ熀浜庢椂闂寸殑 UUID 涓殑鏃堕棿鍞竴鎬с�� + * <p> + * {@code clockSequence} 鍊间粎鍦ㄥ熀浜庢椂闂寸殑 UUID锛堝叾 version 绫诲瀷涓� 1锛変腑鎵嶆湁鎰忎箟銆� 濡傛灉姝� UUID 涓嶆槸鍩轰簬鏃堕棿鐨� UUID锛屽垯姝ゆ柟娉曟姏鍑� + * UnsupportedOperationException銆� + * + * @return 姝� {@code UUID} 鐨勬椂閽熷簭鍒� + * + * @throws UnsupportedOperationException 濡傛灉姝� UUID 鐨� version 涓嶄负 1 + */ + public int clockSequence() throws UnsupportedOperationException + { + checkTimeBase(); + return (int) ((leastSigBits & 0x3FFF000000000000L) >>> 48); + } + + /** + * 涓庢 UUID 鐩稿叧鐨勮妭鐐瑰�笺�� + * + * <p> + * 48 浣嶇殑鑺傜偣鍊兼牴鎹 UUID 鐨� node 瀛楁鏋勯�犮�傛瀛楁鏃ㄥ湪鐢ㄤ簬淇濆瓨鏈哄櫒鐨� IEEE 802 鍦板潃锛岃鍦板潃鐢ㄤ簬鐢熸垚姝� UUID 浠ヤ繚璇佺┖闂村敮涓�鎬с�� + * <p> + * 鑺傜偣鍊间粎鍦ㄥ熀浜庢椂闂寸殑 UUID锛堝叾 version 绫诲瀷涓� 1锛変腑鎵嶆湁鎰忎箟銆�<br> + * 濡傛灉姝� UUID 涓嶆槸鍩轰簬鏃堕棿鐨� UUID锛屽垯姝ゆ柟娉曟姏鍑� UnsupportedOperationException銆� + * + * @return 姝� {@code UUID} 鐨勮妭鐐瑰�� + * + * @throws UnsupportedOperationException 濡傛灉姝� UUID 鐨� version 涓嶄负 1 + */ + public long node() throws UnsupportedOperationException + { + checkTimeBase(); + return leastSigBits & 0x0000FFFFFFFFFFFFL; + } + + /** + * 杩斿洖姝@code UUID} 鐨勫瓧绗︿覆琛ㄧ幇褰㈠紡銆� + * + * <p> + * UUID 鐨勫瓧绗︿覆琛ㄧず褰㈠紡鐢辨 BNF 鎻忚堪锛� + * + * <pre> + * {@code + * UUID = <time_low>-<time_mid>-<time_high_and_version>-<variant_and_sequence>-<node> + * time_low = 4*<hexOctet> + * time_mid = 2*<hexOctet> + * time_high_and_version = 2*<hexOctet> + * variant_and_sequence = 2*<hexOctet> + * node = 6*<hexOctet> + * hexOctet = <hexDigit><hexDigit> + * hexDigit = [0-9a-fA-F] + * } + * </pre> + * + * </blockquote> + * + * @return 姝@code UUID} 鐨勫瓧绗︿覆琛ㄧ幇褰㈠紡 + * @see #toString(boolean) + */ + @Override + public String toString() + { + return toString(false); + } + + /** + * 杩斿洖姝@code UUID} 鐨勫瓧绗︿覆琛ㄧ幇褰㈠紡銆� + * + * <p> + * UUID 鐨勫瓧绗︿覆琛ㄧず褰㈠紡鐢辨 BNF 鎻忚堪锛� + * + * <pre> + * {@code + * UUID = <time_low>-<time_mid>-<time_high_and_version>-<variant_and_sequence>-<node> + * time_low = 4*<hexOctet> + * time_mid = 2*<hexOctet> + * time_high_and_version = 2*<hexOctet> + * variant_and_sequence = 2*<hexOctet> + * node = 6*<hexOctet> + * hexOctet = <hexDigit><hexDigit> + * hexDigit = [0-9a-fA-F] + * } + * </pre> + * + * </blockquote> + * + * @param isSimple 鏄惁绠�鍗曟ā寮忥紝绠�鍗曟ā寮忎负涓嶅甫'-'鐨刄UID瀛楃涓� + * @return 姝@code UUID} 鐨勫瓧绗︿覆琛ㄧ幇褰㈠紡 + */ + public String toString(boolean isSimple) + { + final StringBuilder builder = new StringBuilder(isSimple ? 32 : 36); + // time_low + builder.append(digits(mostSigBits >> 32, 8)); + if (!isSimple) + { + builder.append('-'); + } + // time_mid + builder.append(digits(mostSigBits >> 16, 4)); + if (!isSimple) + { + builder.append('-'); + } + // time_high_and_version + builder.append(digits(mostSigBits, 4)); + if (!isSimple) + { + builder.append('-'); + } + // variant_and_sequence + builder.append(digits(leastSigBits >> 48, 4)); + if (!isSimple) + { + builder.append('-'); + } + // node + builder.append(digits(leastSigBits, 12)); + + return builder.toString(); + } + + /** + * 杩斿洖姝� UUID 鐨勫搱甯岀爜銆� + * + * @return UUID 鐨勫搱甯岀爜鍊笺�� + */ + @Override + public int hashCode() + { + long hilo = mostSigBits ^ leastSigBits; + return ((int) (hilo >> 32)) ^ (int) hilo; + } + + /** + * 灏嗘瀵硅薄涓庢寚瀹氬璞℃瘮杈冦�� + * <p> + * 褰撲笖浠呭綋鍙傛暟涓嶄负 {@code null}銆佽�屾槸涓�涓� UUID 瀵硅薄銆佸叿鏈変笌姝� UUID 鐩稿悓鐨� varriant銆佸寘鍚浉鍚岀殑鍊硷紙姣忎竴浣嶅潎鐩稿悓锛夋椂锛岀粨鏋滄墠涓� {@code true}銆� + * + * @param obj 瑕佷笌涔嬫瘮杈冪殑瀵硅薄 + * + * @return 濡傛灉瀵硅薄鐩稿悓锛屽垯杩斿洖 {@code true}锛涘惁鍒欒繑鍥� {@code false} + */ + @Override + public boolean equals(Object obj) + { + if ((null == obj) || (obj.getClass() != UUID.class)) + { + return false; + } + UUID id = (UUID) obj; + return (mostSigBits == id.mostSigBits && leastSigBits == id.leastSigBits); + } + + // Comparison Operations + + /** + * 灏嗘 UUID 涓庢寚瀹氱殑 UUID 姣旇緝銆� + * + * <p> + * 濡傛灉涓や釜 UUID 涓嶅悓锛屼笖绗竴涓� UUID 鐨勬渶楂樻湁鏁堝瓧娈靛ぇ浜庣浜屼釜 UUID 鐨勫搴斿瓧娈碉紝鍒欑涓�涓� UUID 澶т簬绗簩涓� UUID銆� + * + * @param val 涓庢 UUID 姣旇緝鐨� UUID + * + * @return 鍦ㄦ UUID 灏忎簬銆佺瓑浜庢垨澶т簬 val 鏃讹紝鍒嗗埆杩斿洖 -1銆�0 鎴� 1銆� + * + */ + @Override + public int compareTo(UUID val) + { + // The ordering is intentionally set up so that the UUIDs + // can simply be numerically compared as two numbers + return (this.mostSigBits < val.mostSigBits ? -1 : // + (this.mostSigBits > val.mostSigBits ? 1 : // + (this.leastSigBits < val.leastSigBits ? -1 : // + (this.leastSigBits > val.leastSigBits ? 1 : // + 0)))); + } + + // ------------------------------------------------------------------------------------------------------------------- + // Private method start + /** + * 杩斿洖鎸囧畾鏁板瓧瀵瑰簲鐨刪ex鍊� + * + * @param val 鍊� + * @param digits 浣� + * @return 鍊� + */ + private static String digits(long val, int digits) + { + long hi = 1L << (digits * 4); + return Long.toHexString(hi | (val & (hi - 1))).substring(1); + } + + /** + * 妫�鏌ユ槸鍚︿负time-based鐗堟湰UUID + */ + private void checkTimeBase() + { + if (version() != 1) + { + throw new UnsupportedOperationException("Not a time-based UUID"); + } + } + + /** + * 鑾峰彇{@link SecureRandom}锛岀被鎻愪緵鍔犲瘑鐨勫己闅忔満鏁扮敓鎴愬櫒 (RNG) + * + * @return {@link SecureRandom} + */ + public static SecureRandom getSecureRandom() + { + try + { + return SecureRandom.getInstance("SHA1PRNG"); + } + catch (NoSuchAlgorithmException e) + { + throw new UtilException(e); + } + } + + /** + * 鑾峰彇闅忔満鏁扮敓鎴愬櫒瀵硅薄<br> + * ThreadLocalRandom鏄疛DK 7涔嬪悗鎻愪緵骞跺彂浜х敓闅忔満鏁帮紝鑳藉瑙e喅澶氫釜绾跨▼鍙戠敓鐨勭珵浜変簤澶恒�� + * + * @return {@link ThreadLocalRandom} + */ + public static ThreadLocalRandom getRandom() + { + return ThreadLocalRandom.current(); + } +} -- Gitblit v1.9.3