From c67284ff85ba3fece0859662bab36b0082597a84 Mon Sep 17 00:00:00 2001 From: bwcx_jzy Date: Fri, 13 Aug 2021 13:05:57 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A4=E6=96=AD=E4=BB=93=E5=BA=93=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E5=AD=98=E5=9C=A8=EF=BC=8Cdb=20server=20=E8=B0=83?= =?UTF-8?q?=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/GlobalDefaultExceptionHandler.java | 5 +- .../build/RepositoryController.java | 32 +++- .../jpom/model/log/SshTerminalExecuteLog.java | 22 +-- .../dblog/DbBuildHistoryLogService.java | 3 +- .../dblog/DbMonitorNotifyLogService.java | 55 ++++--- .../service/dblog/DbOutGivingLogService.java | 47 +++--- .../dblog/DbSystemMonitorLogService.java | 19 ++- .../dblog/DbUserOperateLogService.java | 143 +++++++++--------- .../jpom/service/dblog/RepositoryService.java | 13 ++ .../dblog/SshTerminalExecuteLogService.java | 12 +- .../service/h2db/BaseDbCommonService.java | 35 ++++- .../io/jpom/service/h2db/BaseDbService.java | 22 +-- .../src/main/resources/bin/h2-db-v1.sql | 5 + 13 files changed, 220 insertions(+), 193 deletions(-) diff --git a/modules/server/src/main/java/io/jpom/common/GlobalDefaultExceptionHandler.java b/modules/server/src/main/java/io/jpom/common/GlobalDefaultExceptionHandler.java index ca307c84b..5b808b564 100644 --- a/modules/server/src/main/java/io/jpom/common/GlobalDefaultExceptionHandler.java +++ b/modules/server/src/main/java/io/jpom/common/GlobalDefaultExceptionHandler.java @@ -1,6 +1,7 @@ package io.jpom.common; import cn.hutool.core.exceptions.ExceptionUtil; +import cn.hutool.core.exceptions.ValidateException; import cn.hutool.extra.servlet.ServletUtil; import cn.jiangzeyin.common.DefaultSystemLog; import cn.jiangzeyin.common.JsonMessage; @@ -96,13 +97,13 @@ public class GlobalDefaultExceptionHandler { } /** - * 声明要捕获的异常 (参数或者状态异常) + * 声明要捕获的异常 (参数,状态,验证异常) * * @param request 请求 * @param response 响应 * @param e 异常 */ - @ExceptionHandler({IllegalArgumentException.class, IllegalStateException.class}) + @ExceptionHandler({IllegalArgumentException.class, IllegalStateException.class, ValidateException.class}) public void paramExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) { DefaultSystemLog.getLog().error("controller " + request.getRequestURI(), e); ServletUtil.write(response, JsonMessage.getString(405, e.getMessage()), MediaType.APPLICATION_JSON_VALUE); diff --git a/modules/server/src/main/java/io/jpom/controller/build/RepositoryController.java b/modules/server/src/main/java/io/jpom/controller/build/RepositoryController.java index c499a4cbc..237018544 100644 --- a/modules/server/src/main/java/io/jpom/controller/build/RepositoryController.java +++ b/modules/server/src/main/java/io/jpom/controller/build/RepositoryController.java @@ -1,7 +1,6 @@ package io.jpom.controller.build; -import cn.hutool.core.date.LocalDateTimeUtil; -import cn.hutool.core.util.IdUtil; +import cn.hutool.core.lang.Validator; import cn.hutool.db.Entity; import cn.hutool.db.Page; import cn.hutool.db.PageResult; @@ -17,6 +16,7 @@ import io.jpom.plugin.ClassFeature; import io.jpom.plugin.Feature; import io.jpom.plugin.MethodFeature; import io.jpom.service.dblog.RepositoryService; +import org.springframework.util.Assert; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @@ -68,21 +68,37 @@ public class RepositoryController { @PostMapping(value = "/build/repository/edit") @Feature(method = MethodFeature.EDIT) public Object editRepository(RepositoryModel repositoryModelReq) { + this.checkInfo(repositoryModelReq); if (null == repositoryModelReq.getId()) { // insert data - if (null == repositoryModelReq.getModifyTime()) { - repositoryModelReq.setModifyTime(LocalDateTimeUtil.format(LocalDateTimeUtil.now(), "YYYY-MM-dd HH:mm:ss")); - } - repositoryModelReq.setId(IdUtil.fastSimpleUUID()); repositoryService.insert(repositoryModelReq); } else { // update data - repositoryModelReq.setModifyTime(LocalDateTimeUtil.format(LocalDateTimeUtil.now(), "YYYY-MM-dd HH:mm:ss")); repositoryService.updateById(repositoryModelReq); } return JsonMessage.toJson(200, "操作成功"); } + /** + * 检查信息 + * + * @param repositoryModelReq 仓库信息 + */ + private void checkInfo(RepositoryModel repositoryModelReq) { + Assert.hasText(repositoryModelReq.getName(), "请填写仓库名称"); + Integer repoType = repositoryModelReq.getRepoType(); + Assert.state(repoType != null && (repoType == 1 || repoType == 0), "请选择仓库类型"); + Assert.hasText(repositoryModelReq.getGitUrl(), "请填写仓库地址"); + // 判断仓库是否重复 + Entity entity = Entity.create(); + if (repositoryModelReq.getId() != null) { + Validator.validateGeneral(repositoryModelReq.getId(), "错误的ID"); + entity.set("id", "<> " + repositoryModelReq.getId()); + } + entity.set("gitUrl", repositoryModelReq.getGitUrl()); + Assert.state(!repositoryService.exists(entity), "已经存在对应的仓库信息啦"); + } + /** * delete * @@ -92,7 +108,7 @@ public class RepositoryController { @PostMapping(value = "/build/repository/delete") @Feature(method = MethodFeature.DEL) public Object delRepository(String id) { - repositoryService.deleteById(id); + repositoryService.delByKey(id); return JsonMessage.getString(200, "删除成功"); } } diff --git a/modules/server/src/main/java/io/jpom/model/log/SshTerminalExecuteLog.java b/modules/server/src/main/java/io/jpom/model/log/SshTerminalExecuteLog.java index b72bcb4ba..aeec8d2c2 100644 --- a/modules/server/src/main/java/io/jpom/model/log/SshTerminalExecuteLog.java +++ b/modules/server/src/main/java/io/jpom/model/log/SshTerminalExecuteLog.java @@ -2,8 +2,9 @@ package io.jpom.model.log; import cn.hutool.core.util.StrUtil; import io.jpom.JpomApplication; -import io.jpom.model.BaseJsonModel; +import io.jpom.model.BaseDbModel; import io.jpom.model.data.UserModel; +import io.jpom.service.h2db.TableName; /** * ssh 终端执行日志 @@ -11,16 +12,8 @@ import io.jpom.model.data.UserModel; * @author jiangzeyin * @date 2021/08/04 */ -public class SshTerminalExecuteLog extends BaseJsonModel { - /** - * 表名 - */ - public static final String TABLE_NAME = SshTerminalExecuteLog.class.getSimpleName().toUpperCase(); - - /** - * 数据id - */ - private String id; +@TableName("SSHTERMINALEXECUTELOG") +public class SshTerminalExecuteLog extends BaseDbModel { /** * 操作ip */ @@ -123,13 +116,6 @@ public class SshTerminalExecuteLog extends BaseJsonModel { this.commands = commands; } - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } public String getSshName() { return sshName; diff --git a/modules/server/src/main/java/io/jpom/service/dblog/DbBuildHistoryLogService.java b/modules/server/src/main/java/io/jpom/service/dblog/DbBuildHistoryLogService.java index 19513aa67..15aa4888d 100644 --- a/modules/server/src/main/java/io/jpom/service/dblog/DbBuildHistoryLogService.java +++ b/modules/server/src/main/java/io/jpom/service/dblog/DbBuildHistoryLogService.java @@ -38,8 +38,7 @@ public class DbBuildHistoryLogService extends BaseDbCommonService { - public DbMonitorNotifyLogService() { - super(MonitorNotifyLog.TABLE_NAME, MonitorNotifyLog.class); - setKey("logId"); - } + public DbMonitorNotifyLogService() { + super(MonitorNotifyLog.TABLE_NAME, "logId", MonitorNotifyLog.class); + } - @Override - public void insert(MonitorNotifyLog monitorNotifyLog) { - super.insert(monitorNotifyLog); - // - DbConfig.autoClear(getTableName(), "createTime"); - } + @Override + public void insert(MonitorNotifyLog monitorNotifyLog) { + super.insert(monitorNotifyLog); + // + DbConfig.autoClear(getTableName(), "createTime"); + } - /** - * 修改执行结果 - * - * @param logId 通知id - * @param status 状态 - * @param errorMsg 错误消息 - */ - public void updateStatus(String logId, boolean status, String errorMsg) { - Entity entity = new Entity(); - entity.set("notifyStatus", status); - if (errorMsg != null) { - entity.set("notifyError", errorMsg); - } - // - Entity where = new Entity(); - where.set("logId", logId); - super.update(entity, where); - } + /** + * 修改执行结果 + * + * @param logId 通知id + * @param status 状态 + * @param errorMsg 错误消息 + */ + public void updateStatus(String logId, boolean status, String errorMsg) { + Entity entity = new Entity(); + entity.set("notifyStatus", status); + if (errorMsg != null) { + entity.set("notifyError", errorMsg); + } + // + Entity where = new Entity(); + where.set("logId", logId); + super.update(entity, where); + } } diff --git a/modules/server/src/main/java/io/jpom/service/dblog/DbOutGivingLogService.java b/modules/server/src/main/java/io/jpom/service/dblog/DbOutGivingLogService.java index 756980aca..22ba2ecab 100644 --- a/modules/server/src/main/java/io/jpom/service/dblog/DbOutGivingLogService.java +++ b/modules/server/src/main/java/io/jpom/service/dblog/DbOutGivingLogService.java @@ -15,30 +15,29 @@ import org.springframework.stereotype.Service; @Service public class DbOutGivingLogService extends BaseDbCommonService { - public DbOutGivingLogService() { - super(OutGivingLog.TABLE_NAME, OutGivingLog.class); - setKey("id"); - } + public DbOutGivingLogService() { + super(OutGivingLog.TABLE_NAME, "id", OutGivingLog.class); + } - @Override - public void insert(OutGivingLog outGivingLog) { - outGivingLog.setStartTime(System.currentTimeMillis()); - if (outGivingLog.getStatus() == OutGivingNodeProject.Status.Cancel.getCode()) { - outGivingLog.setEndTime(System.currentTimeMillis()); - } - super.insert(outGivingLog); - } + @Override + public void insert(OutGivingLog outGivingLog) { + outGivingLog.setStartTime(System.currentTimeMillis()); + if (outGivingLog.getStatus() == OutGivingNodeProject.Status.Cancel.getCode()) { + outGivingLog.setEndTime(System.currentTimeMillis()); + } + super.insert(outGivingLog); + } - @Override - public int update(OutGivingLog outGivingLog) { - Entity entity = new Entity(); - entity.set("status", outGivingLog.getStatus()); - // 结束 - entity.set("endTime", System.currentTimeMillis()); - entity.set("result", outGivingLog.getResult()); - // - Entity where = new Entity(); - where.set("id", outGivingLog.getId()); - return super.update(entity, where); - } + @Override + public int update(OutGivingLog outGivingLog) { + Entity entity = new Entity(); + entity.set("status", outGivingLog.getStatus()); + // 结束 + entity.set("endTime", System.currentTimeMillis()); + entity.set("result", outGivingLog.getResult()); + // + Entity where = new Entity(); + where.set("id", outGivingLog.getId()); + return super.update(entity, where); + } } diff --git a/modules/server/src/main/java/io/jpom/service/dblog/DbSystemMonitorLogService.java b/modules/server/src/main/java/io/jpom/service/dblog/DbSystemMonitorLogService.java index d1ad0c347..750152e1a 100644 --- a/modules/server/src/main/java/io/jpom/service/dblog/DbSystemMonitorLogService.java +++ b/modules/server/src/main/java/io/jpom/service/dblog/DbSystemMonitorLogService.java @@ -13,15 +13,14 @@ import org.springframework.stereotype.Service; @Service public class DbSystemMonitorLogService extends BaseDbCommonService { - public DbSystemMonitorLogService() { - super(SystemMonitorLog.TABLE_NAME, SystemMonitorLog.class); - setKey("id"); - } + public DbSystemMonitorLogService() { + super(SystemMonitorLog.TABLE_NAME, "id", SystemMonitorLog.class); + } - public PageResult getMonitorData(long startTime, long endTime) { - Entity entity = new Entity(SystemMonitorLog.TABLE_NAME); - entity.set(" MONITORTIME", ">= " + startTime); - entity.set("MONITORTIME", "<= " + endTime); - return listPage(entity, null); - } + public PageResult getMonitorData(long startTime, long endTime) { + Entity entity = new Entity(SystemMonitorLog.TABLE_NAME); + entity.set(" MONITORTIME", ">= " + startTime); + entity.set("MONITORTIME", "<= " + endTime); + return listPage(entity, null); + } } diff --git a/modules/server/src/main/java/io/jpom/service/dblog/DbUserOperateLogService.java b/modules/server/src/main/java/io/jpom/service/dblog/DbUserOperateLogService.java index bd7c4fdf0..7918197c9 100644 --- a/modules/server/src/main/java/io/jpom/service/dblog/DbUserOperateLogService.java +++ b/modules/server/src/main/java/io/jpom/service/dblog/DbUserOperateLogService.java @@ -28,79 +28,78 @@ import java.util.List; @Service public class DbUserOperateLogService extends BaseDbCommonService { - private final MonitorUserOptService monitorUserOptService; - private final UserService userService; - private final BuildService buildService; + private final MonitorUserOptService monitorUserOptService; + private final UserService userService; + private final BuildService buildService; - public DbUserOperateLogService(MonitorUserOptService monitorUserOptService, - UserService userService, - BuildService buildService) { - super(UserOperateLogV1.TABLE_NAME, UserOperateLogV1.class); - this.monitorUserOptService = monitorUserOptService; - this.userService = userService; - this.buildService = buildService; - setKey("reqId"); - } + public DbUserOperateLogService(MonitorUserOptService monitorUserOptService, + UserService userService, + BuildService buildService) { + super(UserOperateLogV1.TABLE_NAME, "reqId", UserOperateLogV1.class); + this.monitorUserOptService = monitorUserOptService; + this.userService = userService; + this.buildService = buildService; + } - @Override - public void insert(UserOperateLogV1 userOperateLogV1) { - super.insert(userOperateLogV1); - DbConfig.autoClear(getTableName(), "optTime"); - ThreadUtil.execute(() -> { - UserOperateLogV1.OptType optType = BaseEnum.getEnum(UserOperateLogV1.OptType.class, userOperateLogV1.getOptType()); - if (optType == null) { - return; - } - UserModel optUserItem = userService.getItem(userOperateLogV1.getUserId()); - if (optUserItem == null) { - return; - } - String otherMsg = ""; - if (optType == UserOperateLogV1.OptType.StartBuild || optType == UserOperateLogV1.OptType.EditBuild) { - BuildModel item = buildService.getItem(userOperateLogV1.getDataId()); - if (item != null) { - otherMsg = StrUtil.format("操作的构建名称:{}\n", item.getName()); - } - } - List monitorUserOptModels = monitorUserOptService.listByType(optType, userOperateLogV1.getUserId()); - if (CollUtil.isEmpty(monitorUserOptModels)) { - return; - } - for (MonitorUserOptModel monitorUserOptModel : monitorUserOptModels) { - List notifyUser = monitorUserOptModel.getNotifyUser(); - if (CollUtil.isEmpty(notifyUser)) { - continue; - } - for (String userId : notifyUser) { - UserModel item = userService.getItem(userId); - if (item == null) { - continue; - } - // - String context = StrUtil.format("操作用户:{}\n操作状态:{}\n操作类型:{}\n操作节点:{}\n 操作数据id: {}\n操作IP: {}\n{}", - optUserItem.getName(), userOperateLogV1.getOptStatusMsg(), userOperateLogV1.getOptTypeMsg(), - userOperateLogV1.getNodeId(), userOperateLogV1.getDataId(), userOperateLogV1.getIp(), otherMsg); - // 邮箱 - String email = item.getEmail(); - if (StrUtil.isNotEmpty(email)) { - MonitorModel.Notify notify1 = new MonitorModel.Notify(MonitorModel.NotifyType.mail, email); - ThreadUtil.execute(() -> NotifyUtil.send(notify1, "用户操作报警", context)); + @Override + public void insert(UserOperateLogV1 userOperateLogV1) { + super.insert(userOperateLogV1); + DbConfig.autoClear(getTableName(), "optTime"); + ThreadUtil.execute(() -> { + UserOperateLogV1.OptType optType = BaseEnum.getEnum(UserOperateLogV1.OptType.class, userOperateLogV1.getOptType()); + if (optType == null) { + return; + } + UserModel optUserItem = userService.getItem(userOperateLogV1.getUserId()); + if (optUserItem == null) { + return; + } + String otherMsg = ""; + if (optType == UserOperateLogV1.OptType.StartBuild || optType == UserOperateLogV1.OptType.EditBuild) { + BuildModel item = buildService.getItem(userOperateLogV1.getDataId()); + if (item != null) { + otherMsg = StrUtil.format("操作的构建名称:{}\n", item.getName()); + } + } + List monitorUserOptModels = monitorUserOptService.listByType(optType, userOperateLogV1.getUserId()); + if (CollUtil.isEmpty(monitorUserOptModels)) { + return; + } + for (MonitorUserOptModel monitorUserOptModel : monitorUserOptModels) { + List notifyUser = monitorUserOptModel.getNotifyUser(); + if (CollUtil.isEmpty(notifyUser)) { + continue; + } + for (String userId : notifyUser) { + UserModel item = userService.getItem(userId); + if (item == null) { + continue; + } + // + String context = StrUtil.format("操作用户:{}\n操作状态:{}\n操作类型:{}\n操作节点:{}\n 操作数据id: {}\n操作IP: {}\n{}", + optUserItem.getName(), userOperateLogV1.getOptStatusMsg(), userOperateLogV1.getOptTypeMsg(), + userOperateLogV1.getNodeId(), userOperateLogV1.getDataId(), userOperateLogV1.getIp(), otherMsg); + // 邮箱 + String email = item.getEmail(); + if (StrUtil.isNotEmpty(email)) { + MonitorModel.Notify notify1 = new MonitorModel.Notify(MonitorModel.NotifyType.mail, email); + ThreadUtil.execute(() -> NotifyUtil.send(notify1, "用户操作报警", context)); - } - // dingding - String dingDing = item.getDingDing(); - if (StrUtil.isNotEmpty(dingDing)) { - MonitorModel.Notify notify1 = new MonitorModel.Notify(MonitorModel.NotifyType.dingding, dingDing); - ThreadUtil.execute(() -> NotifyUtil.send(notify1, "用户操作报警", context)); - } - // 企业微信 - String workWx = item.getWorkWx(); - if (StrUtil.isNotEmpty(workWx)) { - MonitorModel.Notify notify1 = new MonitorModel.Notify(MonitorModel.NotifyType.workWx, workWx); - ThreadUtil.execute(() -> NotifyUtil.send(notify1, "用户操作报警", context)); - } - } - } - }); - } + } + // dingding + String dingDing = item.getDingDing(); + if (StrUtil.isNotEmpty(dingDing)) { + MonitorModel.Notify notify1 = new MonitorModel.Notify(MonitorModel.NotifyType.dingding, dingDing); + ThreadUtil.execute(() -> NotifyUtil.send(notify1, "用户操作报警", context)); + } + // 企业微信 + String workWx = item.getWorkWx(); + if (StrUtil.isNotEmpty(workWx)) { + MonitorModel.Notify notify1 = new MonitorModel.Notify(MonitorModel.NotifyType.workWx, workWx); + ThreadUtil.execute(() -> NotifyUtil.send(notify1, "用户操作报警", context)); + } + } + } + }); + } } diff --git a/modules/server/src/main/java/io/jpom/service/dblog/RepositoryService.java b/modules/server/src/main/java/io/jpom/service/dblog/RepositoryService.java index cbb2febde..352728f76 100644 --- a/modules/server/src/main/java/io/jpom/service/dblog/RepositoryService.java +++ b/modules/server/src/main/java/io/jpom/service/dblog/RepositoryService.java @@ -1,5 +1,6 @@ package io.jpom.service.dblog; +import cn.hutool.core.date.LocalDateTimeUtil; import io.jpom.model.data.RepositoryModel; import io.jpom.service.h2db.BaseDbService; import org.springframework.stereotype.Service; @@ -11,5 +12,17 @@ import org.springframework.stereotype.Service; @Service public class RepositoryService extends BaseDbService { + @Override + public void insert(RepositoryModel repositoryModelReq) { + if (null == repositoryModelReq.getModifyTime()) { + repositoryModelReq.setModifyTime(LocalDateTimeUtil.format(LocalDateTimeUtil.now(), "YYYY-MM-dd HH:mm:ss")); + } + super.insert(repositoryModelReq); + } + @Override + public void updateById(RepositoryModel info) { + info.setModifyTime(LocalDateTimeUtil.format(LocalDateTimeUtil.now(), "YYYY-MM-dd HH:mm:ss")); + super.updateById(info); + } } diff --git a/modules/server/src/main/java/io/jpom/service/dblog/SshTerminalExecuteLogService.java b/modules/server/src/main/java/io/jpom/service/dblog/SshTerminalExecuteLogService.java index 572d7cef6..f6b5f7c50 100644 --- a/modules/server/src/main/java/io/jpom/service/dblog/SshTerminalExecuteLogService.java +++ b/modules/server/src/main/java/io/jpom/service/dblog/SshTerminalExecuteLogService.java @@ -1,12 +1,11 @@ package io.jpom.service.dblog; import cn.hutool.core.date.SystemClock; -import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; import io.jpom.model.data.SshModel; import io.jpom.model.data.UserModel; import io.jpom.model.log.SshTerminalExecuteLog; -import io.jpom.service.h2db.BaseDbCommonService; +import io.jpom.service.h2db.BaseDbService; import org.springframework.stereotype.Service; import java.util.List; @@ -20,12 +19,7 @@ import java.util.stream.Collectors; * @date 2021/08/04 */ @Service -public class SshTerminalExecuteLogService extends BaseDbCommonService { - - public SshTerminalExecuteLogService() { - super(SshTerminalExecuteLog.TABLE_NAME, SshTerminalExecuteLog.class); - setKey("id"); - } +public class SshTerminalExecuteLogService extends BaseDbService { /** * 批量记录日志 @@ -41,7 +35,7 @@ public class SshTerminalExecuteLogService extends BaseDbCommonService executeLogs = commands.stream().filter(StrUtil::isNotEmpty).map(s -> { SshTerminalExecuteLog sshTerminalExecuteLog = new SshTerminalExecuteLog(); - sshTerminalExecuteLog.setId(IdUtil.fastSimpleUUID()); + //sshTerminalExecuteLog.setId(IdUtil.fastSimpleUUID()); if (sshItem != null) { sshTerminalExecuteLog.setSshId(sshItem.getId()); sshTerminalExecuteLog.setSshName(sshItem.getName()); diff --git a/modules/server/src/main/java/io/jpom/service/h2db/BaseDbCommonService.java b/modules/server/src/main/java/io/jpom/service/h2db/BaseDbCommonService.java index da98ebea0..031d78058 100644 --- a/modules/server/src/main/java/io/jpom/service/h2db/BaseDbCommonService.java +++ b/modules/server/src/main/java/io/jpom/service/h2db/BaseDbCommonService.java @@ -38,17 +38,19 @@ public abstract class BaseDbCommonService { /** * 主键 */ - private String key; + private final String key; - public BaseDbCommonService(String tableName, Class tClass) { + public BaseDbCommonService(String tableName, String key, Class tClass) { this.tableName = this.covetTableName(tableName, tClass); this.tClass = tClass; + this.key = key; } @SuppressWarnings("unchecked") - public BaseDbCommonService(String tableName) { + public BaseDbCommonService(String tableName, String key) { this.tClass = (Class) TypeUtil.getTypeArgument(this.getClass()); this.tableName = this.covetTableName(tableName, this.tClass); + this.key = key; } @@ -60,8 +62,8 @@ public abstract class BaseDbCommonService { return tableName; } - protected void setKey(String key) { - this.key = key; + protected String getKey() { + return key; } /** @@ -215,6 +217,29 @@ public abstract class BaseDbCommonService { } } + /** + * 判断是否存在 + * + * @param where 条件 + * @return true 存在 + */ + public boolean exists(Entity where) { + if (!DbConfig.getInstance().isInit()) { + // ignore + return false; + } + where.setTableName(getTableName()); + Db db = Db.use(); + db.setWrapper((Character) null); + long count; + try { + count = db.count(where); + } catch (SQLException e) { + throw new JpomRuntimeException("数据库异常", e); + } + return count > 0; + } + /** * 分页查询 * diff --git a/modules/server/src/main/java/io/jpom/service/h2db/BaseDbService.java b/modules/server/src/main/java/io/jpom/service/h2db/BaseDbService.java index 94b8e7d14..ff42e712f 100644 --- a/modules/server/src/main/java/io/jpom/service/h2db/BaseDbService.java +++ b/modules/server/src/main/java/io/jpom/service/h2db/BaseDbService.java @@ -1,6 +1,7 @@ package io.jpom.service.h2db; import cn.hutool.core.date.SystemClock; +import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.db.Entity; import io.jpom.common.Const; @@ -18,8 +19,7 @@ import java.util.Collection; public abstract class BaseDbService extends BaseDbCommonService { public BaseDbService() { - super(null); - setKey(Const.ID_STR); + super(null, Const.ID_STR); } @Override @@ -33,6 +33,7 @@ public abstract class BaseDbService extends BaseDbCommonS public void insert(T t) { // def create time t.setCreateTimeMillis(ObjectUtil.defaultIfNull(t.getCreateTimeMillis(), SystemClock.now())); + t.setId(ObjectUtil.defaultIfNull(t.getId(), IdUtil.fastSimpleUUID())); super.insert(t); } @@ -40,7 +41,10 @@ public abstract class BaseDbService extends BaseDbCommonS @Override public void insert(Collection t) { // def create time - t.forEach(t1 -> t1.setCreateTimeMillis(ObjectUtil.defaultIfNull(t1.getCreateTimeMillis(), SystemClock.now()))); + t.forEach(t1 -> { + t1.setCreateTimeMillis(ObjectUtil.defaultIfNull(t1.getCreateTimeMillis(), SystemClock.now())); + t1.setId(ObjectUtil.defaultIfNull(t1.getId(), IdUtil.fastSimpleUUID())); + }); super.insert(t); } @@ -59,16 +63,4 @@ public abstract class BaseDbService extends BaseDbCommonS where.set(Const.ID_STR, info.getId()); super.update(entity, where); } - - - /** - * delete by id - * - * @param id 主键 - */ - public void deleteById(String id) { - Entity where = new Entity(getTableName()); - where.set(Const.ID_STR, id); - del(where); - } } diff --git a/modules/server/src/main/resources/bin/h2-db-v1.sql b/modules/server/src/main/resources/bin/h2-db-v1.sql index 1ad029aac..ab8f2c87d 100644 --- a/modules/server/src/main/resources/bin/h2-db-v1.sql +++ b/modules/server/src/main/resources/bin/h2-db-v1.sql @@ -122,3 +122,8 @@ CREATE TABLE IF NOT EXISTS PUBLIC.SSHTERMINALEXECUTELOG CONSTRAINT SSHTERMINALEXECUTELOG_PK PRIMARY KEY (ID) ); COMMENT ON TABLE SSHTERMINALEXECUTELOG is 'ssh 终端操作记录表'; + +ALTER TABLE SSHTERMINALEXECUTELOG + ADD IF NOT EXISTS CREATETIMEMILLIS BIGINT COMMENT '数据创建时间'; +ALTER TABLE SSHTERMINALEXECUTELOG + ADD IF NOT EXISTS MODIFYTIMEMILLIS BIGINT COMMENT '数据修改时间';