From f1d181592f1df0f049aaf6f9d5f9f6435670b60c Mon Sep 17 00:00:00 2001 From: yinq Date: Tue, 15 Apr 2025 18:16:33 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=A2=9E?= =?UTF-8?q?=E4=B8=BB=E9=94=AE=EF=BC=8C=E6=89=B9=E9=87=8F=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=EF=BC=8C=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/FUNDING.yml | 1 - .../tagApi/common/utils/uuid/IdGenerator.java | 123 ++++++++++++++++++ .../framework/config/SecurityConfig.java | 1 + .../tagApi/service/constant/ApiConstants.java | 70 ++++++++++ .../controller/KDocsApiController.java | 120 ++++++++++++++++- .../hw/tagApi/service/domain/ApiContent.java | 45 +++++++ .../hw/tagApi/service/domain/ApiRequest.java | 45 +++++++ .../service/mapper/HwTagRecordMapper.java | 8 ++ .../tagApi/service/service/IKDocsService.java | 49 +++++++ .../service/impl/HwTagRecordServiceImpl.java | 13 +- .../service/impl/KDocsServiceImpl.java | 81 ++++++++++++ .../mapper/service/HwTagRecordMapper.xml | 18 +++ 12 files changed, 568 insertions(+), 6 deletions(-) delete mode 100644 .github/FUNDING.yml create mode 100644 tagApi-common/src/main/java/hw/tagApi/common/utils/uuid/IdGenerator.java create mode 100644 tagApi-service/src/main/java/hw/tagApi/service/constant/ApiConstants.java create mode 100644 tagApi-service/src/main/java/hw/tagApi/service/domain/ApiContent.java create mode 100644 tagApi-service/src/main/java/hw/tagApi/service/domain/ApiRequest.java create mode 100644 tagApi-service/src/main/java/hw/tagApi/service/service/IKDocsService.java create mode 100644 tagApi-service/src/main/java/hw/tagApi/service/service/impl/KDocsServiceImpl.java diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml deleted file mode 100644 index 5f29471..0000000 --- a/.github/FUNDING.yml +++ /dev/null @@ -1 +0,0 @@ -custom: http://doc.ruoyi.vip/tagApi-vue/other/donate.html diff --git a/tagApi-common/src/main/java/hw/tagApi/common/utils/uuid/IdGenerator.java b/tagApi-common/src/main/java/hw/tagApi/common/utils/uuid/IdGenerator.java new file mode 100644 index 0000000..962eb48 --- /dev/null +++ b/tagApi-common/src/main/java/hw/tagApi/common/utils/uuid/IdGenerator.java @@ -0,0 +1,123 @@ +package hw.tagApi.common.utils.uuid; + +import java.net.NetworkInterface; +import java.net.SocketException; +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.Enumeration; + +/** + * ID生成器工具类 + * 仿照MyBatis-Plus的雪花算法实现 + * + * @author Yinq + * @date 2025-04-15 + */ +public class IdGenerator { + /** + * 开始时间戳(2025-01-01 00:00:00) + */ + private static final long START_TIMESTAMP = LocalDateTime.of(2025, 1, 1, 0, 0, 0) + .toInstant(ZoneOffset.of("+8")).toEpochMilli(); + + /** + * 机器ID所占位数 + */ + private static final long WORKER_ID_BITS = 10L; + + /** + * 序列号所占位数 + */ + private static final long SEQUENCE_BITS = 12L; + + /** + * 机器ID最大值 + */ + private static final long MAX_WORKER_ID = ~(-1L << WORKER_ID_BITS); + + /** + * 序列号掩码 + */ + private static final long SEQUENCE_MASK = ~(-1L << SEQUENCE_BITS); + + /** + * 机器ID左移位数 + */ + private static final long WORKER_ID_SHIFT = SEQUENCE_BITS; + + /** + * 时间戳左移位数 + */ + private static final long TIMESTAMP_SHIFT = SEQUENCE_BITS + WORKER_ID_BITS; + + /** + * 序列号 + */ + private static long sequence = 0L; + + /** + * 上次生成ID的时间戳 + */ + private static long lastTimestamp = -1L; + + /** + * 机器ID + */ + private static long workerId; + + static { + try { + workerId = getWorkerId(); + } catch (Exception e) { + workerId = 1L; + } + } + + /** + * 获取机器ID + */ + private static long getWorkerId() throws SocketException { + long macPiece; + Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); + while (networkInterfaces.hasMoreElements()) { + NetworkInterface networkInterface = networkInterfaces.nextElement(); + byte[] mac = networkInterface.getHardwareAddress(); + if (mac != null) { + macPiece = ((0x000000FFL & (mac[mac.length - 1])) | (0x0000FF00L & (((mac[mac.length - 2]) << 8)))) >> 6; + return macPiece; + } + } + return 1L; + } + + /** + * 生成下一个ID + */ + public static synchronized Long nextId() { + long timestamp = System.currentTimeMillis(); + if (timestamp < lastTimestamp) { + throw new RuntimeException("时钟回拨,拒绝生成ID"); + } + if (timestamp == lastTimestamp) { + sequence = (sequence + 1) & SEQUENCE_MASK; + if (sequence == 0) { + timestamp = tilNextMillis(lastTimestamp); + } + } else { + sequence = 0L; + } + lastTimestamp = timestamp; + return ((timestamp - START_TIMESTAMP) << TIMESTAMP_SHIFT) | (workerId << WORKER_ID_SHIFT) | sequence; + } + + /** + * 等待下一个毫秒 + */ + private static long tilNextMillis(long lastTimestamp) { + long timestamp = System.currentTimeMillis(); + while (timestamp <= lastTimestamp) { + timestamp = System.currentTimeMillis(); + } + return timestamp; + } +} \ No newline at end of file diff --git a/tagApi-framework/src/main/java/hw/tagApi/framework/config/SecurityConfig.java b/tagApi-framework/src/main/java/hw/tagApi/framework/config/SecurityConfig.java index 21bb606..e904a6a 100644 --- a/tagApi-framework/src/main/java/hw/tagApi/framework/config/SecurityConfig.java +++ b/tagApi-framework/src/main/java/hw/tagApi/framework/config/SecurityConfig.java @@ -114,6 +114,7 @@ public class SecurityConfig requests.antMatchers("/login", "/register", "/captchaImage").permitAll() // 静态资源,可匿名访问 .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll() + .antMatchers(HttpMethod.POST, "/docs/**").permitAll() .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll() // 除上面外的所有请求全部需要鉴权认证 .anyRequest().authenticated(); diff --git a/tagApi-service/src/main/java/hw/tagApi/service/constant/ApiConstants.java b/tagApi-service/src/main/java/hw/tagApi/service/constant/ApiConstants.java new file mode 100644 index 0000000..5d401a7 --- /dev/null +++ b/tagApi-service/src/main/java/hw/tagApi/service/constant/ApiConstants.java @@ -0,0 +1,70 @@ +package hw.tagApi.service.constant; + +/** + * API常量类 + * + * @author Yinq + * @date 2025-04-15 + */ +public class ApiConstants { + + /** + * 数据库状态 + */ + public static final String DATA_STATUS = "00"; + /** + * 数据导入 + */ + public static final String DATA_IMPORT = "10"; + + /** + * 数据查询 + */ + public static final String DATA_QUERY = "20"; + /** + * 数据导出 + */ + public static final String DATA_EXPORT = "30"; + + /** + * 导入中 + */ + public static final String IMPORTING = "导入中"; + /** + * 系统错误 + */ + public static final String SYSTEM_ERROR = "系统错误"; + + /** + * 搜索中 + */ + public static final String SEARCHING = "搜索中"; + /** + * 搜索超时 + */ + public static final String SEARCH_TIMEOUT = "搜索超时"; + + /** + * 导出中 + */ + public static final String EXPORTING = "导出中"; + + + public static final String SUCCESS = "SUCCESS"; + public static final String FAILED = "FAILED"; + + public static class ImportStatus { + public static final String IMPORT_SUCCESS = "IMPORT_SUCCESS"; + public static final String IMPORT_FAILED = "IMPORT_FAILED"; + } + + public static class ExportStatus { + public static final String EXPORT_SUCCESS = "EXPORT_SUCCESS"; + public static final String EXPORT_FAILED = "EXPORT_FAILED"; + } + + public static class SearchStatus { + public static final String SEARCH_SUCCESS = "SEARCH_SUCCESS"; + public static final String NO_RECORD = "NO_RECORD"; + } +} \ No newline at end of file diff --git a/tagApi-service/src/main/java/hw/tagApi/service/controller/KDocsApiController.java b/tagApi-service/src/main/java/hw/tagApi/service/controller/KDocsApiController.java index a82b7b2..3ad993d 100644 --- a/tagApi-service/src/main/java/hw/tagApi/service/controller/KDocsApiController.java +++ b/tagApi-service/src/main/java/hw/tagApi/service/controller/KDocsApiController.java @@ -1,14 +1,23 @@ package hw.tagApi.service.controller; import hw.tagApi.common.core.controller.BaseController; -import hw.tagApi.service.service.IHwTagRecordService; +import hw.tagApi.common.core.domain.AjaxResult; +import hw.tagApi.service.constant.ApiConstants; +import hw.tagApi.service.domain.ApiContent; +import hw.tagApi.service.domain.ApiRequest; +import hw.tagApi.service.service.IKDocsService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; - +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** - * 标签记录Controller + * 云接口Controller * * @author Yinq * @date 2025-04-15 @@ -16,9 +25,112 @@ import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/docs") public class KDocsApiController extends BaseController { + + private static final Logger log = LoggerFactory.getLogger(KDocsApiController.class); + @Autowired - private IHwTagRecordService hwTagRecordService; + private IKDocsService ikDocsService; + /** + * 统一接口处理 + * + * @param request 请求对象,包含CASE和CONTENT + * @return 处理结果 + */ + @PostMapping("/api") + public AjaxResult handleRequest(@RequestHeader("API-Key") String apiKey, @RequestBody ApiRequest request) { + // 验证API-Key + if (!validateApiKey(apiKey)) { + log.warn("无效的API-Key: {}", apiKey); + return AjaxResult.error("无效的API-Key"); + } + try { + String caseCode = request.getCASE(); + List content = request.getCONTENT(); + // 打印接收到的请求参数 + log.info("接收到请求 - CASE: {}, CONTENT: {}", caseCode, content); + + switch (caseCode) { + case ApiConstants.DATA_STATUS: + log.info("查询数据库状态"); + + return AjaxResult.success("数据库状态正常"); + case ApiConstants.DATA_IMPORT: + // 数据导入 + if (content == null || content.isEmpty()) { + log.warn("导入数据为空"); + return AjaxResult.error(ApiConstants.ImportStatus.IMPORT_FAILED, "导入数据不能为空"); + } + log.info("开始导入数据: {}", content); + // TODO: 调用导入服务 + return AjaxResult.success(ApiConstants.ImportStatus.IMPORT_SUCCESS); + + case ApiConstants.DATA_QUERY: + // 数据查询 + if (content == null || content.isEmpty()) { + log.warn("查询条件为空"); + return AjaxResult.error(ApiConstants.SearchStatus.NO_RECORD, "查询条件不能为空"); + } + log.info("开始查询数据: {}", content); + // TODO: 调用查询服务 + return AjaxResult.success(ApiConstants.SearchStatus.SEARCH_SUCCESS); + + case ApiConstants.DATA_EXPORT: + // 数据导出 + log.info("开始导出数据"); + Map response = new HashMap<>(); + response.put("CASE", ApiConstants.DATA_EXPORT); + + List> result = new ArrayList<>(); + + Map item1 = new HashMap<>(); + Map fields1 = new HashMap<>(); + fields1.put("导出状态", "导出中"); + fields1.put("情况说明", "预计需要3分钟"); + item1.put("fields", fields1); + item1.put("id", "ci"); + result.add(item1); + + Map item2 = new HashMap<>(); + Map fields2 = new HashMap<>(); + fields2.put("导出状态", "导出失败"); + fields2.put("情况说明", ""); + item2.put("fields", fields2); + item2.put("id", "56"); + result.add(item2); + + Map item3 = new HashMap<>(); + Map fields3 = new HashMap<>(); + fields3.put("导出状态", "导出中"); + fields3.put("情况说明", "预计需要10分钟"); + item3.put("fields", fields3); + item3.put("id", "9a"); + result.add(item3); + + response.put("RESULT", result); + log.info("导出数据完成: {}", response); + return AjaxResult.success(response); + + default: + log.warn("未知的功能代码: {}", caseCode); + return AjaxResult.error("未知的功能代码"); + } + } catch (Exception e) { + log.error("处理请求时发生错误", e); + return AjaxResult.error("系统错误:" + e.getMessage()); + } + } + + /** + * 验证API-Key + * + * @param apiKey API密钥 + * @return 是否有效 + */ + private boolean validateApiKey(String apiKey) { + // TODO: 实现API-Key的验证逻辑 + return true; + } } diff --git a/tagApi-service/src/main/java/hw/tagApi/service/domain/ApiContent.java b/tagApi-service/src/main/java/hw/tagApi/service/domain/ApiContent.java new file mode 100644 index 0000000..226c0e9 --- /dev/null +++ b/tagApi-service/src/main/java/hw/tagApi/service/domain/ApiContent.java @@ -0,0 +1,45 @@ +package hw.tagApi.service.domain; + +import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.Map; + +/** + * API请求内容对象 + * + * @author Yinq + * @date 2025-04-15 + */ +public class ApiContent { + + /** 字段集合 */ + @JsonProperty("fields") + private Map fields; + + /** 记录ID */ + @JsonProperty("id") + private String id; + + public Map getFields() { + return fields; + } + + public void setFields(Map fields) { + this.fields = fields; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @Override + public String toString() { + return "ApiContent{" + + "fields=" + fields + + ", id='" + id + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/tagApi-service/src/main/java/hw/tagApi/service/domain/ApiRequest.java b/tagApi-service/src/main/java/hw/tagApi/service/domain/ApiRequest.java new file mode 100644 index 0000000..3840dce --- /dev/null +++ b/tagApi-service/src/main/java/hw/tagApi/service/domain/ApiRequest.java @@ -0,0 +1,45 @@ +package hw.tagApi.service.domain; + +import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.List; + +/** + * API请求对象 + * + * @author Yinq + * @date 2025-04-15 + */ +public class ApiRequest { + + /** 功能代码 */ + @JsonProperty("CASE") + private String CASE; + + /** 请求内容 */ + @JsonProperty("CONTENT") + private List CONTENT; + + public String getCASE() { + return CASE; + } + + public void setCASE(String CASE) { + this.CASE = CASE; + } + + public List getCONTENT() { + return CONTENT; + } + + public void setCONTENT(List CONTENT) { + this.CONTENT = CONTENT; + } + + @Override + public String toString() { + return "ApiRequest{" + + "CASE='" + CASE + '\'' + + ", CONTENT=" + CONTENT + + '}'; + } +} \ No newline at end of file diff --git a/tagApi-service/src/main/java/hw/tagApi/service/mapper/HwTagRecordMapper.java b/tagApi-service/src/main/java/hw/tagApi/service/mapper/HwTagRecordMapper.java index 11dfcb7..285a59b 100644 --- a/tagApi-service/src/main/java/hw/tagApi/service/mapper/HwTagRecordMapper.java +++ b/tagApi-service/src/main/java/hw/tagApi/service/mapper/HwTagRecordMapper.java @@ -35,6 +35,14 @@ public interface HwTagRecordMapper */ public int insertHwTagRecord(HwTagRecord hwTagRecord); + /** + * 批量新增标签记录 + * + * @param hwTagRecordList 标签记录列表 + * @return 结果 + */ + public int batchInsertHwTagRecord(List hwTagRecordList); + /** * 修改标签记录 * diff --git a/tagApi-service/src/main/java/hw/tagApi/service/service/IKDocsService.java b/tagApi-service/src/main/java/hw/tagApi/service/service/IKDocsService.java new file mode 100644 index 0000000..c32a575 --- /dev/null +++ b/tagApi-service/src/main/java/hw/tagApi/service/service/IKDocsService.java @@ -0,0 +1,49 @@ +package hw.tagApi.service.service; + +/** + * 云文档服务接口 + * + * @author Yinq + * @date 2025-04-15 + */ +public interface IKDocsService { + + /** + * 获取系统状态 + * + * @return 系统状态 + */ + String getSystemStatus(); + + /** + * 导入数据 + * + * @param data 待导入的数据 + * @return 导入结果 + */ + String importData(String data); + + /** + * 删除数据 + * + * @param id 数据ID + * @return 删除结果 + */ + String deleteData(String id); + + /** + * 查询数据 + * + * @param query 查询条件 + * @return 查询结果 + */ + String queryData(String query); + + /** + * 导出数据 + * + * @param query 导出条件 + * @return 导出结果 + */ + String exportData(String query); +} diff --git a/tagApi-service/src/main/java/hw/tagApi/service/service/impl/HwTagRecordServiceImpl.java b/tagApi-service/src/main/java/hw/tagApi/service/service/impl/HwTagRecordServiceImpl.java index 1c1cb53..d3433f7 100644 --- a/tagApi-service/src/main/java/hw/tagApi/service/service/impl/HwTagRecordServiceImpl.java +++ b/tagApi-service/src/main/java/hw/tagApi/service/service/impl/HwTagRecordServiceImpl.java @@ -1,7 +1,9 @@ package hw.tagApi.service.service.impl; +import java.util.ArrayList; import java.util.List; import hw.tagApi.common.utils.DateUtils; +import hw.tagApi.common.utils.uuid.IdGenerator; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import hw.tagApi.service.mapper.HwTagRecordMapper; @@ -66,7 +68,7 @@ public class HwTagRecordServiceImpl implements IHwTagRecordService @Override public int updateHwTagRecord(HwTagRecord hwTagRecord) { - hwTagRecord.setUpdateTime(DateUtils.getNowDate()); + initTagRecord(hwTagRecord); return hwTagRecordMapper.updateHwTagRecord(hwTagRecord); } @@ -93,4 +95,13 @@ public class HwTagRecordServiceImpl implements IHwTagRecordService { return hwTagRecordMapper.deleteHwTagRecordByRId(rId); } + + /** + * 初始化hwTagRecord + * @param hwTagRecord hwTagRecord + */ + public void initTagRecord(HwTagRecord hwTagRecord){ + hwTagRecord.setrId(IdGenerator.nextId()); + hwTagRecord.setCreateTime(DateUtils.getNowDate()); + } } diff --git a/tagApi-service/src/main/java/hw/tagApi/service/service/impl/KDocsServiceImpl.java b/tagApi-service/src/main/java/hw/tagApi/service/service/impl/KDocsServiceImpl.java new file mode 100644 index 0000000..594165a --- /dev/null +++ b/tagApi-service/src/main/java/hw/tagApi/service/service/impl/KDocsServiceImpl.java @@ -0,0 +1,81 @@ +package hw.tagApi.service.service.impl; + +import hw.tagApi.service.service.IKDocsService; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import hw.tagApi.service.constant.ApiConstants; +import hw.tagApi.service.mapper.HwTagRecordMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 云文档服务实现类 + * + * @author Yinq + * @date 2025-04-15 + */ +@Service +public class KDocsServiceImpl implements IKDocsService +{ + private static final Logger log = LoggerFactory.getLogger(KDocsServiceImpl.class); + + private HwTagRecordMapper hwTagRecordMapper; + + @Override + public String getSystemStatus() { + Map response = new HashMap<>(); + response.put("CASE", ApiConstants.DATA_IMPORT); + + List> result = new ArrayList<>(); + + Map statusItem = new HashMap<>(); + Map statusFields = new HashMap<>(); + statusFields.put("导入状态", ApiConstants.IMPORTING); + statusItem.put("fields", statusFields); + statusItem.put("id", "16"); + result.add(statusItem); + + Map errorItem = new HashMap<>(); + Map errorFields = new HashMap<>(); + errorFields.put("导入模式", ApiConstants.SYSTEM_ERROR); + errorFields.put("情况说明", ""); + errorItem.put("fields", errorFields); + errorItem.put("id", "s5"); + result.add(errorItem); + + response.put("RESULT", result); + return null; + } + + @Override + public String importData(String data) { + // TODO: 实现数据导入逻辑 + return getSystemStatus(); + } + + @Override + public String deleteData(String id) { + // TODO: 实现数据删除逻辑 + return getSystemStatus(); + } + + @Override + public String queryData(String query) { + // TODO: 实现数据查询逻辑 + return getSystemStatus(); + } + + @Override + public String exportData(String query) { + // TODO: 实现数据导出逻辑 + return getSystemStatus(); + } + + + +} diff --git a/tagApi-service/src/main/resources/mapper/service/HwTagRecordMapper.xml b/tagApi-service/src/main/resources/mapper/service/HwTagRecordMapper.xml index fee75a1..5663fd4 100644 --- a/tagApi-service/src/main/resources/mapper/service/HwTagRecordMapper.xml +++ b/tagApi-service/src/main/resources/mapper/service/HwTagRecordMapper.xml @@ -144,4 +144,22 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{rId} + + + + insert into hw_tag_record ( + r_id, t_id, epc, password, order_code, batch_number, + tag_sequence, total_quantity, tag_batch, model_code, + processing_time, operator_id, fields_1, fields_2, fields_3, + create_by, create_time, update_by, update_time, remark + ) values + + ( + #{item.rId}, #{item.tId}, #{item.epc}, #{item.password}, #{item.orderCode}, #{item.batchNumber}, + #{item.tagSequence}, #{item.totalQuantity}, #{item.tagBatch}, #{item.modelCode}, + #{item.processingTime}, #{item.operatorId}, #{item.fields1}, #{item.fields2}, #{item.fields3}, + #{item.createBy}, sysdate(), #{item.updateBy}, sysdate(), #{item.remark} + ) + + \ No newline at end of file