From 385a2a6eccf2375891760201767c8eaf305dc0b4 Mon Sep 17 00:00:00 2001 From: Cheney Date: Mon, 18 Nov 2024 15:01:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=86=E9=92=A5=E5=80=BC=E6=9F=A5=E6=89=BE?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sunyard/chsm/mapper/KeyRecordMapper.java | 14 ++++++ .../chsm/service/KeyRecordService.java | 15 +++++++ .../service/impl/KeyRecordServiceImpl.java | 45 +++++++++++++++++++ .../service/SYMEncryptControllerTest.java | 28 ++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 chsm-common/src/main/java/com/sunyard/chsm/mapper/KeyRecordMapper.java create mode 100644 chsm-common/src/main/java/com/sunyard/chsm/service/KeyRecordService.java create mode 100644 chsm-common/src/main/java/com/sunyard/chsm/service/impl/KeyRecordServiceImpl.java create mode 100644 chsm-web-server/src/test/java/com/sunyard/chsm/service/SYMEncryptControllerTest.java diff --git a/chsm-common/src/main/java/com/sunyard/chsm/mapper/KeyRecordMapper.java b/chsm-common/src/main/java/com/sunyard/chsm/mapper/KeyRecordMapper.java new file mode 100644 index 0000000..7604932 --- /dev/null +++ b/chsm-common/src/main/java/com/sunyard/chsm/mapper/KeyRecordMapper.java @@ -0,0 +1,14 @@ +package com.sunyard.chsm.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.sunyard.chsm.model.entity.KeyInfo; +import com.sunyard.chsm.model.entity.KeyRecord; +import org.apache.ibatis.annotations.Mapper; + +/** + * @author liulu + * @since 2024/10/22 + */ +@Mapper +public interface KeyRecordMapper extends BaseMapper { +} diff --git a/chsm-common/src/main/java/com/sunyard/chsm/service/KeyRecordService.java b/chsm-common/src/main/java/com/sunyard/chsm/service/KeyRecordService.java new file mode 100644 index 0000000..bc1f146 --- /dev/null +++ b/chsm-common/src/main/java/com/sunyard/chsm/service/KeyRecordService.java @@ -0,0 +1,15 @@ +package com.sunyard.chsm.service; + +import com.sunyard.chsm.model.entity.KeyRecord; + +public interface KeyRecordService { + KeyRecord selectById(Long id); + + + /** + * 根据 keyId 查找密钥 + * @param keyId keyId + * @return 可用值或 null + */ + KeyRecord selectByKeyId(Long keyId); +} diff --git a/chsm-common/src/main/java/com/sunyard/chsm/service/impl/KeyRecordServiceImpl.java b/chsm-common/src/main/java/com/sunyard/chsm/service/impl/KeyRecordServiceImpl.java new file mode 100644 index 0000000..7a32a8a --- /dev/null +++ b/chsm-common/src/main/java/com/sunyard/chsm/service/impl/KeyRecordServiceImpl.java @@ -0,0 +1,45 @@ +package com.sunyard.chsm.service.impl; + + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.sunyard.chsm.mapper.KeyRecordMapper; +import com.sunyard.chsm.model.entity.KeyRecord; +import com.sunyard.chsm.service.KeyRecordService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import javax.annotation.Resource; +import java.time.LocalDateTime; + +/** + * @author Cheney + */ +@Slf4j +@Service +@Transactional +public class KeyRecordServiceImpl implements KeyRecordService { + + @Resource + private KeyRecordMapper keyRecordMapper; + + @Override + public KeyRecord selectById(Long id) { + return keyRecordMapper.selectById(id); + } + + @Override + public KeyRecord selectByKeyId(Long keyId) { + LocalDateTime now = LocalDateTime.now(); + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + // 添加 keyId 的条件 + queryWrapper.eq(KeyRecord::getKeyId, keyId); + // 添加 effectiveTime 小于当前时间的条件 + queryWrapper.lt(KeyRecord::getEffectiveTime, now); + // 添加 expiredTime 大于当前时间的条件 + queryWrapper.gt(KeyRecord::getExpiredTime, now); + + return keyRecordMapper.selectOne(queryWrapper); + } +} diff --git a/chsm-web-server/src/test/java/com/sunyard/chsm/service/SYMEncryptControllerTest.java b/chsm-web-server/src/test/java/com/sunyard/chsm/service/SYMEncryptControllerTest.java new file mode 100644 index 0000000..ca5ca23 --- /dev/null +++ b/chsm-web-server/src/test/java/com/sunyard/chsm/service/SYMEncryptControllerTest.java @@ -0,0 +1,28 @@ +package com.sunyard.chsm.service; + +import com.sunyard.chsm.WebServerApp; +import com.sunyard.chsm.model.dto.KeyInfoDTO; +import com.sunyard.chsm.model.entity.KeyRecord; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@Slf4j +@SpringBootTest(classes = WebServerApp.class) +public class SYMEncryptControllerTest { + + private static final long TEST_APP_ID = 1852232967292882946L; + private static final long TEST_KEY_ID = 1852232967292882945L; + + @Autowired + private KeyRecordService keyRecordService; + + + @Test + void testSelectKey() { + KeyRecord key = keyRecordService.selectByKeyId(TEST_KEY_ID); + System.out.println(key); + } + +}