diff --git a/pom.xml b/pom.xml index 93cea25..f656a5d 100644 --- a/pom.xml +++ b/pom.xml @@ -188,7 +188,7 @@ com.imyeyu.java timi-java - 0.0.6 + 0.0.7 com.imyeyu.utils diff --git a/src/main/java/com/imyeyu/api/modules/gao/controller/GaoRegistrationEventController.java b/src/main/java/com/imyeyu/api/modules/gao/controller/GaoRegistrationEventController.java index e9e7319..0063ad6 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/controller/GaoRegistrationEventController.java +++ b/src/main/java/com/imyeyu/api/modules/gao/controller/GaoRegistrationEventController.java @@ -72,6 +72,14 @@ public class GaoRegistrationEventController { service.update(registrationEvent); } + @AOPLog + @RequestRateLimit + @RequireGaoPermission(GaoPermissionCode.REGISTRATION_EVENT_UPDATE) + @PostMapping("/sort") + public void sort(@RequestBody List idList) { + service.sort(idList); + } + @AOPLog @RequestRateLimit @RequireGaoPermission(GaoPermissionCode.REGISTRATION_EVENT_DELETE) diff --git a/src/main/java/com/imyeyu/api/modules/gao/service/GaoRegistrationEventService.java b/src/main/java/com/imyeyu/api/modules/gao/service/GaoRegistrationEventService.java index 893a77a..f6f609c 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/service/GaoRegistrationEventService.java +++ b/src/main/java/com/imyeyu/api/modules/gao/service/GaoRegistrationEventService.java @@ -30,4 +30,9 @@ public interface GaoRegistrationEventService extends BaseService listRoleCode(String registrationEventId); + + /// 按客户端提交的顺序更新登记事件排序 + /// + /// @param idList 登记事件 ID 列表 + void sort(List idList); } diff --git a/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoCustomerServiceImplement.java b/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoCustomerServiceImplement.java index 9ada769..e0d6d89 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoCustomerServiceImplement.java +++ b/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoCustomerServiceImplement.java @@ -77,7 +77,7 @@ public class GaoCustomerServiceImplement extends AbstractEntityService implements GaoRegistrationEventService { - private final GaoRegistrationEventMapper mapper; private final ObjectMapper jackson; + private final LoggerService loggerService; + + private final GaoRegistrationEventMapper mapper; private final GaoRegistrationEventRoleRelationMapper roleRelationMapper; @Override @@ -53,9 +54,7 @@ public class GaoRegistrationEventServiceImplement extends AbstractEntityService< @Override public PageResult pageWithRegistrationEventLimit(Page page) { PageResult result = page(page); - result.setList(result.getList().stream() - .filter(item -> item.getStatus() != GaoRegistrationEvent.Status.DELETED) - .toList()); + result.setList(result.getList().stream().filter(item -> item.getStatus() != GaoRegistrationEvent.Status.DELETED).toList()); loadRegistrationEventLimit(result.getList()); return result; } @@ -63,7 +62,7 @@ public class GaoRegistrationEventServiceImplement extends AbstractEntityService< @Transactional(TimiServerDBConfig.ROLLBACKER) @Override public void create(GaoRegistrationEvent entity) { - Logger logger = buildLogger("GAO_REGISTRATION_EVENT_CREATE"); + Logger logger = new Logger(Logger.Module.GAO, "GAO_REGISTRATION_EVENT_CREATE"); try { logger.setContent(jackson.writeValueAsString(entity)); checkEntity(entity, false); @@ -88,9 +87,10 @@ public class GaoRegistrationEventServiceImplement extends AbstractEntityService< @Transactional(TimiServerDBConfig.ROLLBACKER) @Override public void update(GaoRegistrationEvent entity) { - Logger logger = buildLogger("GAO_REGISTRATION_EVENT_UPDATE"); + Logger logger = new Logger(Logger.Module.GAO, "GAO_REGISTRATION_EVENT_UPDATE"); try { logger.setContent(jackson.writeValueAsString(entity)); + Integer sort = entity.getSort(); checkEntity(entity, true); GaoRegistrationEvent current = get(entity.getId()); current.setName(entity.getName()); @@ -102,7 +102,9 @@ public class GaoRegistrationEventServiceImplement extends AbstractEntityService< current.setLimitValue(entity.getLimitValue()); current.setBeginAt(entity.getBeginAt()); current.setEndAt(entity.getEndAt()); - current.setSort(entity.getSort()); + if (sort != null) { + current.setSort(sort); + } mapper.update(current); saveRoleRelation(entity.getId(), entity.getRoleCodeList()); logger.setLevel(Logger.Level.INFO); @@ -124,7 +126,7 @@ public class GaoRegistrationEventServiceImplement extends AbstractEntityService< @Transactional(TimiServerDBConfig.ROLLBACKER) @Override public void delete(String id) { - Logger logger = buildLogger("GAO_REGISTRATION_EVENT_DELETE"); + Logger logger = new Logger(Logger.Module.GAO, "GAO_REGISTRATION_EVENT_DELETE"); try { logger.setContent(id); GaoRegistrationEvent entity = get(id); @@ -157,9 +159,7 @@ public class GaoRegistrationEventServiceImplement extends AbstractEntityService< public List listEnabled() { List list = mapper.selectEnabledList(Time.now()); loadRegistrationEventLimit(list); - list = list.stream() - .filter(item -> !TimiJava.isEmpty(item.getRoleCodeList())) - .toList(); + list = list.stream().filter(item -> !TimiJava.isEmpty(item.getRoleCodeList())).toList(); return list; } @@ -168,6 +168,22 @@ public class GaoRegistrationEventServiceImplement extends AbstractEntityService< return roleRelationMapper.selectRoleCodeListByRegistrationEventId(registrationEventId); } + @Transactional(TimiServerDBConfig.ROLLBACKER) + @Override + public void sort(List idList) { + if (idList == null || idList.isEmpty()) { + return; + } + List distinctIdList = new ArrayList<>(new LinkedHashSet<>(idList)); + Map eventMap = mapper.selectByIdList(distinctIdList).stream().collect(Collectors.toMap(GaoRegistrationEvent::getId, item -> item)); + TimiException.requiredTrue(eventMap.size() == distinctIdList.size(), "not found registrationEvent"); + for (int index = 0; index < distinctIdList.size(); index++) { + GaoRegistrationEvent registrationEvent = eventMap.get(distinctIdList.get(index)); + registrationEvent.setSort(index); + mapper.update(registrationEvent); + } + } + private void checkEntity(GaoRegistrationEvent entity, boolean requireId) { TimiException.required(entity, "not found registrationEvent"); if (requireId) { @@ -183,10 +199,9 @@ public class GaoRegistrationEventServiceImplement extends AbstractEntityService< if (entity.getLimitType() == null) { entity.setLimitType(GaoRegistrationEvent.LimitType.NONE); } - TimiException.requiredTrue( - entity.getRoleCodeList() != null && entity.getRoleCodeList().stream().anyMatch(item -> item != null && !item.isBlank()), - "not found registrationEvent.roleCodeList" - ); + TimiException.requiredTrue(entity.getRoleCodeList() != null && entity.getRoleCodeList() + .stream() + .anyMatch(item -> item != null && !item.isBlank()), "not found registrationEvent.roleCodeList"); checkRegistrationLimit(entity); checkRegisterablePeriod(entity); if (entity.getColor() != null) { @@ -198,9 +213,7 @@ public class GaoRegistrationEventServiceImplement extends AbstractEntityService< } private void checkRegistrationLimit(GaoRegistrationEvent entity) { - if (entity.getLimitType() == GaoRegistrationEvent.LimitType.INTERVAL_HOUR - || entity.getLimitType() == GaoRegistrationEvent.LimitType.TOTAL_LIMIT - || entity.getLimitType() == GaoRegistrationEvent.LimitType.CUSTOMER_TOTAL_LIMIT) { + if (entity.getLimitType() == GaoRegistrationEvent.LimitType.INTERVAL_HOUR || entity.getLimitType() == GaoRegistrationEvent.LimitType.TOTAL_LIMIT || entity.getLimitType() == GaoRegistrationEvent.LimitType.CUSTOMER_TOTAL_LIMIT) { TimiException.required(entity.getLimitValue(), "not found registrationEvent.limitValue"); TimiException.requiredTrue(0 < entity.getLimitValue(), "registrationEvent.limitValue lte 0"); return; @@ -212,10 +225,7 @@ public class GaoRegistrationEventServiceImplement extends AbstractEntityService< if (entity.getBeginAt() == null && entity.getEndAt() == null) { return; } - TimiException.requiredTrue( - entity.getBeginAt() == null || entity.getEndAt() == null || entity.getBeginAt() <= entity.getEndAt(), - "registrationEvent.beginAt gt registrationEvent.endAt" - ); + TimiException.requiredTrue(entity.getBeginAt() == null || entity.getEndAt() == null || entity.getBeginAt() <= entity.getEndAt(), "registrationEvent.beginAt gt registrationEvent.endAt"); } private void saveRoleRelation(String registrationEventId, List roleCodeList) { @@ -235,33 +245,16 @@ public class GaoRegistrationEventServiceImplement extends AbstractEntityService< } } - private void loadRegistrationEventLimit(GaoRegistrationEvent registrationEvent) { - registrationEvent.setRoleCodeList(roleRelationMapper.selectRoleCodeListByRegistrationEventId(registrationEvent.getId())); - } - private void loadRegistrationEventLimit(List registrationEventList) { if (registrationEventList == null || registrationEventList.isEmpty()) { return; } - List registrationEventIdList = registrationEventList.stream() - .map(GaoRegistrationEvent::getId) - .distinct() - .toList(); - Map> roleCodeMap = roleRelationMapper.selectByRegistrationEventIdList(new ArrayList<>(registrationEventIdList)).stream() - .collect(Collectors.groupingBy( - GaoRegistrationEventRoleRelation::getRegistrationEventId, - Collectors.mapping(GaoRegistrationEventRoleRelation::getRoleCode, Collectors.toList()) - )); + List registrationEventIdList = registrationEventList.stream().map(GaoRegistrationEvent::getId).distinct().toList(); + Map> roleCodeMap = roleRelationMapper.selectByRegistrationEventIdList(new ArrayList<>(registrationEventIdList)) + .stream() + .collect(Collectors.groupingBy(GaoRegistrationEventRoleRelation::getRegistrationEventId, Collectors.mapping(GaoRegistrationEventRoleRelation::getRoleCode, Collectors.toList()))); for (GaoRegistrationEvent registrationEvent : registrationEventList) { registrationEvent.setRoleCodeList(roleCodeMap.getOrDefault(registrationEvent.getId(), List.of())); } } - - private Logger buildLogger(String operation) { - Logger logger = new Logger(); - logger.setModule(Logger.Module.GAO); - logger.setOperation(operation); - logger.setIp(TimiSpring.getRequestIP()); - return logger; - } } diff --git a/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoUserServiceImplement.java b/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoUserServiceImplement.java index fcca309..1ec583b 100644 --- a/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoUserServiceImplement.java +++ b/src/main/java/com/imyeyu/api/modules/gao/service/implement/GaoUserServiceImplement.java @@ -59,7 +59,7 @@ public class GaoUserServiceImplement extends AbstractEntityService, Creatable { /** 创建时间 */ private Long createdAt; - public Logger(Level level) { + public Logger(Module module, String operation) { + this.module = module; + this.operation = operation; + } + + public Logger(Module module, Level level, String operation) { + this.module = module; this.level = level; + this.operation = operation; + } + + public static Logger info(Module module, String operation) { + return new Logger(module, Level.INFO, operation); + } + + public static Logger warning(Module module, String operation) { + return new Logger(module, Level.WARN, operation); + } + + public static Logger error(Module module, String operation) { + return new Logger(module, Level.ERROR, operation); } } diff --git a/src/main/java/com/imyeyu/api/modules/system/service/LoggerService.java b/src/main/java/com/imyeyu/api/modules/system/service/LoggerService.java index d62fba0..45e4463 100644 --- a/src/main/java/com/imyeyu/api/modules/system/service/LoggerService.java +++ b/src/main/java/com/imyeyu/api/modules/system/service/LoggerService.java @@ -16,41 +16,29 @@ public interface LoggerService extends GettableService, Pageable void createSync(Logger logger); - void create(Logger.Level level, Logger.Module module, String operation, String userId, String content, String result); + void create(Logger.Module module, Logger.Level level, String operation, String content, String result); - default void info(Logger.Module module, String operation, String userId) { - create(Logger.Level.INFO, module, operation, userId, null, null); + default void info(Logger.Module module, String operation) { + create(module, Logger.Level.INFO, operation, null, null); } default void info(Logger.Module module, String operation, String content, String result) { - create(Logger.Level.INFO, module, operation, null, content, result); + create(module, Logger.Level.INFO, operation, content, result); } - default void info(Logger.Module module, String operation, String userId, String content, String result) { - create(Logger.Level.INFO, module, operation, userId, content, result); - } - - default void warning(Logger.Module module, String operation, String userId) { - create(Logger.Level.WARN, module, operation, userId, null, null); + default void warning(Logger.Module module, String operation) { + create(module, Logger.Level.WARN, operation, null, null); } default void warning(Logger.Module module, String operation, String content, String result) { - create(Logger.Level.WARN, module, operation, null, content, result); + create(module, Logger.Level.WARN, operation, content, result); } - default void warning(Logger.Module module, String operation, String userId, String content, String result) { - create(Logger.Level.WARN, module, operation, userId, content, result); - } - - default void error(Logger.Module module, String operation, String userId) { - create(Logger.Level.ERROR, module, operation, userId, null, null); + default void error(Logger.Module module, String operation) { + create(module, Logger.Level.ERROR, operation, null, null); } default void error(Logger.Module module, String operation, String content, String result) { - create(Logger.Level.ERROR, module, operation, null, content, result); - } - - default void error(Logger.Module module, String operation, String userId, String content, String result) { - create(Logger.Level.ERROR, module, operation, userId, content, result); + create(module, Logger.Level.ERROR, operation, content, result); } } diff --git a/src/main/java/com/imyeyu/api/modules/system/service/implement/LoggerServiceImplement.java b/src/main/java/com/imyeyu/api/modules/system/service/implement/LoggerServiceImplement.java index a85c322..5e849c8 100644 --- a/src/main/java/com/imyeyu/api/modules/system/service/implement/LoggerServiceImplement.java +++ b/src/main/java/com/imyeyu/api/modules/system/service/implement/LoggerServiceImplement.java @@ -3,6 +3,10 @@ package com.imyeyu.api.modules.system.service.implement; import com.imyeyu.api.modules.system.entity.Logger; import com.imyeyu.api.modules.system.mapper.LoggerMapper; import com.imyeyu.api.modules.system.service.LoggerService; +import com.imyeyu.api.modules.user.entity.User; +import com.imyeyu.api.modules.user.service.UserLoginService; +import com.imyeyu.java.TimiJava; +import com.imyeyu.spring.TimiSpring; import com.imyeyu.spring.mapper.BaseMapper; import com.imyeyu.spring.service.AbstractEntityService; import lombok.RequiredArgsConstructor; @@ -18,6 +22,8 @@ import org.springframework.stereotype.Service; @RequiredArgsConstructor public class LoggerServiceImplement extends AbstractEntityService implements LoggerService { + private final UserLoginService userLoginService; + private final LoggerMapper mapper; @Override @@ -28,19 +34,23 @@ public class LoggerServiceImplement extends AbstractEntityService super.create(logger)); + Thread.startVirtualThread(() -> createSync(logger)); } @Override public void createSync(Logger logger) { + if (TimiJava.isEmpty(logger.getUserId())) { + User loginUser = userLoginService.getLoginUser(); + if (TimiJava.isNotEmpty(loginUser)) { + logger.setUserId(loginUser.getId()); + } + } + logger.setIp(TimiJava.defaultIfEmpty(logger.getIp(), TimiSpring.getRequestIP())); super.create(logger); } - public void create(Logger.Level level, Logger.Module module, String operation, String userId, String content, String result) { - Logger log = new Logger(level); - log.setModule(module); - log.setOperation(operation); - log.setUserId(userId); + public void create(Logger.Module module, Logger.Level level, String operation, String content, String result) { + Logger log = new Logger(module, level, operation); log.setContent(content); log.setResult(result); create(log); diff --git a/src/main/java/com/imyeyu/api/modules/user/service/implement/UserLoginServiceImplement.java b/src/main/java/com/imyeyu/api/modules/user/service/implement/UserLoginServiceImplement.java index ca9c817..8977985 100644 --- a/src/main/java/com/imyeyu/api/modules/user/service/implement/UserLoginServiceImplement.java +++ b/src/main/java/com/imyeyu/api/modules/user/service/implement/UserLoginServiceImplement.java @@ -25,8 +25,8 @@ import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.concurrent.ThreadLocalRandom; import java.util.UUID; +import java.util.concurrent.ThreadLocalRandom; /// 用户登录服务实现 /// @@ -95,7 +95,9 @@ public class UserLoginServiceImplement implements UserLoginService, TimiJava { @Override public User getLoginUser() { String token = TimiSpring.getToken(); - TimiException.required(token, "not found token"); + if (TimiJava.isEmpty(token)) { + return null; + } String userId = redisUserToken.get(getUserTokenKey(token)); if (TimiJava.isEmpty(userId)) { return null; diff --git a/src/main/java/com/imyeyu/api/util/InitBuiltinUser.java b/src/main/java/com/imyeyu/api/util/InitBuiltinUser.java index 65ed2d0..5850f68 100644 --- a/src/main/java/com/imyeyu/api/util/InitBuiltinUser.java +++ b/src/main/java/com/imyeyu/api/util/InitBuiltinUser.java @@ -93,7 +93,13 @@ public class InitBuiltinUser implements ApplicationRunner { userService.create(user); String logContent = "初始化 %s 自举用户成功,请立即修改密码:%s".formatted(BootstrapData.SYS_USER_NAME, password); - loggerService.info(Logger.Module.SYSTEM, "INITIALIZE_SYSTEM_USER", user.getId(), logContent, null); + + Logger logger = new Logger(); + logger.setModule(Logger.Module.SYSTEM); + logger.setOperation("INITIALIZE_SYSTEM_USER"); + logger.setUserId(user.getId()); + logger.setContent(logContent); + loggerService.create(logger); } // 授权系统用户角色 {