Feature: add operation log (#16)

* feat: auto record operation log

* feat: add list project operation log api

* feat: update frontend resource
This commit is contained in:
vran
2022-02-18 23:29:31 +08:00
committed by GitHub
parent 133b9476e5
commit 2ed115d070
81 changed files with 1741 additions and 98 deletions

View File

@@ -2,72 +2,75 @@ package com.databasir.dao.impl;
import com.databasir.dao.exception.DataNotExistsException;
import lombok.RequiredArgsConstructor;
import org.jooq.*;
import org.jooq.Record;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;
@RequiredArgsConstructor
public abstract class BaseDao<T extends Record, R> {
public abstract class BaseDao<PO> {
private final Table<T> table;
private final Table<?> table;
private final Class<R> pojoType;
private final Class<PO> pojoType;
public BaseDao(Table<?> table, Class<PO> pojoType) {
this.table = table;
this.pojoType = pojoType;
}
public abstract DSLContext getDslContext();
public boolean existsById(Integer id) {
public <T extends Serializable> boolean existsById(T id) {
return getDslContext().fetchExists(table, identity().eq(id));
}
public Integer insertAndReturnId(R pojo) {
T record = getDslContext().newRecord(table, pojo);
public <T> T insertAndReturnId(PO pojo) {
Record record = getDslContext().newRecord(table, pojo);
UpdatableRecord<?> updatableRecord = (UpdatableRecord<?>) record;
updatableRecord.store();
Object value = updatableRecord.getValue(table.getIdentity().getField());
return (Integer) value;
return (T) identityType().cast(value);
}
public int batchInsert(Collection<R> pojoList) {
public int batchInsert(Collection<PO> pojoList) {
List<TableRecord<?>> records = pojoList.stream()
.map(pojo -> {
T record = getDslContext().newRecord(table, pojo);
Record record = getDslContext().newRecord(table, pojo);
return (TableRecord<?>) record;
})
.collect(Collectors.toList());
return Arrays.stream(getDslContext().batchInsert(records).execute()).sum();
}
public int deleteById(Integer id) {
public <T extends Serializable> int deleteById(T id) {
return getDslContext()
.deleteFrom(table).where(identity().eq(id))
.execute();
}
public int updateById(R pojo) {
T record = getDslContext().newRecord(table, pojo);
public int updateById(PO pojo) {
Record record = getDslContext().newRecord(table, pojo);
record.changed(table.getIdentity().getField(), false);
return getDslContext().executeUpdate((UpdatableRecord<?>) record);
}
public Optional<R> selectOptionalById(Integer id) {
public <T extends Serializable> Optional<PO> selectOptionalById(T id) {
return getDslContext()
.select(table.fields()).from(table).where(identity().eq(id))
.fetchOptionalInto(pojoType);
}
public R selectById(Integer id) {
public <T extends Serializable> PO selectById(T id) {
return selectOptionalById(id)
.orElseThrow(() -> new DataNotExistsException("data not exists in " + table.getName() + " with id = " + id));
}
public List<R> selectInIds(List<Integer> ids) {
public List<PO> selectInIds(List<? extends Serializable> ids) {
if (ids == null || ids.isEmpty()) {
return Collections.emptyList();
}
@@ -77,12 +80,12 @@ public abstract class BaseDao<T extends Record, R> {
.fetchInto(pojoType);
}
public Page<R> selectByPage(Pageable request, Condition condition) {
public Page<PO> selectByPage(Pageable request, Condition condition) {
Integer count = getDslContext()
.selectCount().from(table).where(condition)
.fetchOne(0, int.class);
int total = count == null ? 0 : count;
List<R> data = getDslContext()
List<PO> data = getDslContext()
.selectFrom(table).where(condition)
.orderBy(getSortFields(request.getSort()))
.offset(request.getOffset()).limit(request.getPageSize())
@@ -110,15 +113,19 @@ public abstract class BaseDao<T extends Record, R> {
return querySortFields;
}
protected Field<Integer> identity() {
Identity<T, ?> identity = table.getIdentity();
protected <T extends Serializable> Field<T> identity() {
Identity<?, ?> identity = table.getIdentity();
if (identity == null) {
throw new IllegalStateException("can not find identity column in " + table.getName());
}
return identity.getField().cast(Integer.class);
return identity.getField().cast(identityType());
}
protected Table<T> table() {
protected <T extends Serializable> Class<T> identityType() {
return (Class<T>) Integer.class;
}
protected Table<?> table() {
return this.table;
}
}

View File

@@ -16,7 +16,7 @@ import static com.databasir.dao.Tables.DATA_SOURCE;
@Repository
public class DataSourceDao extends BaseDao<DataSourceRecord, DataSourcePojo> {
public class DataSourceDao extends BaseDao<DataSourcePojo> {
@Autowired
@Getter

View File

@@ -1,7 +1,6 @@
package com.databasir.dao.impl;
import com.databasir.dao.tables.pojos.DataSourcePropertyPojo;
import com.databasir.dao.tables.records.DataSourcePropertyRecord;
import lombok.Getter;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
@@ -13,7 +12,7 @@ import static com.databasir.dao.Tables.DATA_SOURCE_PROPERTY;
@Repository
public class DataSourcePropertyDao extends BaseDao<DataSourcePropertyRecord, DataSourcePropertyPojo> {
public class DataSourcePropertyDao extends BaseDao<DataSourcePropertyPojo> {
@Autowired
@Getter

View File

@@ -13,7 +13,7 @@ import static com.databasir.dao.Tables.DATABASE_DOCUMENT;
@Repository
public class DatabaseDocumentDao extends BaseDao<DatabaseDocumentRecord, DatabaseDocumentPojo> {
public class DatabaseDocumentDao extends BaseDao<DatabaseDocumentPojo> {
@Autowired
@Getter

View File

@@ -1,7 +1,6 @@
package com.databasir.dao.impl;
import com.databasir.dao.tables.pojos.DatabaseDocumentHistoryPojo;
import com.databasir.dao.tables.records.DatabaseDocumentHistoryRecord;
import com.databasir.dao.value.DatabaseDocumentVersionPojo;
import lombok.Getter;
import org.jooq.Condition;
@@ -19,7 +18,7 @@ import static com.databasir.dao.Tables.DATABASE_DOCUMENT_HISTORY;
@Repository
public class DatabaseDocumentHistoryDao extends BaseDao<DatabaseDocumentHistoryRecord, DatabaseDocumentHistoryPojo> {
public class DatabaseDocumentHistoryDao extends BaseDao<DatabaseDocumentHistoryPojo> {
@Autowired
@Getter

View File

@@ -1,7 +1,6 @@
package com.databasir.dao.impl;
import com.databasir.dao.tables.pojos.DocumentRemarkPojo;
import com.databasir.dao.tables.records.DocumentRemarkRecord;
import lombok.Getter;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
@@ -13,7 +12,7 @@ import static com.databasir.dao.Tables.DOCUMENT_REMARK;
@Repository
public class DocumentRemarkDao extends BaseDao<DocumentRemarkRecord, DocumentRemarkPojo> {
public class DocumentRemarkDao extends BaseDao<DocumentRemarkPojo> {
@Autowired
@Getter

View File

@@ -1,7 +1,6 @@
package com.databasir.dao.impl;
import com.databasir.dao.tables.pojos.GroupPojo;
import com.databasir.dao.tables.records.GroupRecord;
import lombok.Getter;
import org.jooq.Condition;
import org.jooq.DSLContext;
@@ -10,6 +9,7 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@@ -17,7 +17,7 @@ import java.util.Optional;
import static com.databasir.dao.Tables.GROUP;
@Repository
public class GroupDao extends BaseDao<GroupRecord, GroupPojo> {
public class GroupDao extends BaseDao<GroupPojo> {
@Autowired
@Getter
@@ -27,10 +27,11 @@ public class GroupDao extends BaseDao<GroupRecord, GroupPojo> {
super(GROUP, GroupPojo.class);
}
@Override
public int deleteById(Integer id) {
public <T extends Serializable> int deleteById(T id) {
return dslContext
.update(table()).set(GROUP.DELETED, true).where(GROUP.ID.eq(id))
.update(table()).set(GROUP.DELETED, true).where(GROUP.ID.eq((Integer) id))
.execute();
}
@@ -40,14 +41,14 @@ public class GroupDao extends BaseDao<GroupRecord, GroupPojo> {
}
@Override
public Optional<GroupPojo> selectOptionalById(Integer id) {
public <T extends Serializable> Optional<GroupPojo> selectOptionalById(T id) {
return getDslContext()
.select(GROUP.fields()).from(GROUP).where(GROUP.ID.eq(id).and(GROUP.DELETED.eq(false)))
.select(GROUP.fields()).from(GROUP).where(GROUP.ID.eq((Integer) id).and(GROUP.DELETED.eq(false)))
.fetchOptionalInto(GroupPojo.class);
}
@Override
public List<GroupPojo> selectInIds(List<Integer> ids) {
public List<GroupPojo> selectInIds(List<? extends Serializable> ids) {
if (ids == null || ids.isEmpty()) {
return Collections.emptyList();
}

View File

@@ -1,7 +1,6 @@
package com.databasir.dao.impl;
import com.databasir.dao.tables.pojos.LoginPojo;
import com.databasir.dao.tables.records.LoginRecord;
import lombok.Getter;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
@@ -14,7 +13,7 @@ import static com.databasir.dao.Tables.LOGIN;
@Repository
public class LoginDao extends BaseDao<LoginRecord, LoginPojo> {
public class LoginDao extends BaseDao<LoginPojo> {
@Autowired
@Getter

View File

@@ -0,0 +1,28 @@
package com.databasir.dao.impl;
import com.databasir.dao.tables.pojos.OperationLogPojo;
import lombok.Getter;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.io.Serializable;
import static com.databasir.dao.Tables.OPERATION_LOG;
@Repository
public class OperationLogDao extends BaseDao<OperationLogPojo> {
@Autowired
@Getter
private DSLContext dslContext;
public OperationLogDao() {
super(OPERATION_LOG, OperationLogPojo.class);
}
@Override
protected <T extends Serializable> Class<T> identityType() {
return (Class<T>) Long.class;
}
}

View File

@@ -1,7 +1,6 @@
package com.databasir.dao.impl;
import com.databasir.dao.tables.pojos.ProjectPojo;
import com.databasir.dao.tables.records.ProjectRecord;
import com.databasir.dao.value.GroupProjectCountPojo;
import lombok.Getter;
import org.jooq.Condition;
@@ -12,6 +11,7 @@ import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
@@ -22,7 +22,7 @@ import static com.databasir.dao.Tables.PROJECT;
@Repository
public class ProjectDao extends BaseDao<ProjectRecord, ProjectPojo> {
public class ProjectDao extends BaseDao<ProjectPojo> {
@Autowired
@Getter
@@ -39,7 +39,7 @@ public class ProjectDao extends BaseDao<ProjectRecord, ProjectPojo> {
}
@Override
public Optional<ProjectPojo> selectOptionalById(Integer id) {
public <T extends Serializable> Optional<ProjectPojo> selectOptionalById(T id) {
return getDslContext()
.select(PROJECT.fields()).from(PROJECT)
.where(identity().eq(id).and(PROJECT.DELETED.eq(false)))
@@ -47,7 +47,7 @@ public class ProjectDao extends BaseDao<ProjectRecord, ProjectPojo> {
}
@Override
public boolean existsById(Integer id) {
public <T extends Serializable> boolean existsById(T id) {
return getDslContext().fetchExists(table(), identity().eq(id).and(PROJECT.DELETED.eq(false)));
}

View File

@@ -16,7 +16,7 @@ import java.util.Optional;
import static com.databasir.dao.Tables.PROJECT_SYNC_RULE;
@Repository
public class ProjectSyncRuleDao extends BaseDao<ProjectSyncRuleRecord, ProjectSyncRulePojo> {
public class ProjectSyncRuleDao extends BaseDao<ProjectSyncRulePojo> {
@Autowired
@Getter

View File

@@ -2,7 +2,6 @@ package com.databasir.dao.impl;
import com.databasir.dao.exception.DataNotExistsException;
import com.databasir.dao.tables.pojos.SysKeyPojo;
import com.databasir.dao.tables.records.SysKeyRecord;
import lombok.Getter;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
@@ -13,7 +12,7 @@ import java.util.Optional;
import static com.databasir.dao.Tables.SYS_KEY;
@Repository
public class SysKeyDao extends BaseDao<SysKeyRecord, SysKeyPojo> {
public class SysKeyDao extends BaseDao<SysKeyPojo> {
@Autowired
@Getter

View File

@@ -13,7 +13,7 @@ import java.util.Optional;
import static com.databasir.dao.Tables.SYS_MAIL;
@Repository
public class SysMailDao extends BaseDao<SysMailRecord, SysMailPojo> {
public class SysMailDao extends BaseDao<SysMailPojo> {
@Autowired
@Getter

View File

@@ -1,7 +1,6 @@
package com.databasir.dao.impl;
import com.databasir.dao.tables.pojos.TableColumnDocumentPojo;
import com.databasir.dao.tables.records.TableColumnDocumentRecord;
import lombok.Getter;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
@@ -13,7 +12,7 @@ import static com.databasir.dao.Tables.TABLE_COLUMN_DOCUMENT;
@Repository
public class TableColumnDocumentDao extends BaseDao<TableColumnDocumentRecord, TableColumnDocumentPojo> {
public class TableColumnDocumentDao extends BaseDao<TableColumnDocumentPojo> {
@Autowired
@Getter

View File

@@ -1,7 +1,6 @@
package com.databasir.dao.impl;
import com.databasir.dao.tables.pojos.TableDocumentPojo;
import com.databasir.dao.tables.records.TableDocumentRecord;
import lombok.Getter;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
@@ -12,7 +11,7 @@ import java.util.List;
import static com.databasir.dao.Tables.TABLE_DOCUMENT;
@Repository
public class TableDocumentDao extends BaseDao<TableDocumentRecord, TableDocumentPojo> {
public class TableDocumentDao extends BaseDao<TableDocumentPojo> {
@Autowired
@Getter

View File

@@ -1,7 +1,6 @@
package com.databasir.dao.impl;
import com.databasir.dao.tables.pojos.TableIndexDocumentPojo;
import com.databasir.dao.tables.records.TableIndexDocumentRecord;
import lombok.Getter;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
@@ -13,7 +12,7 @@ import static com.databasir.dao.Tables.TABLE_INDEX_DOCUMENT;
@Repository
public class TableIndexDocumentDao extends BaseDao<TableIndexDocumentRecord, TableIndexDocumentPojo> {
public class TableIndexDocumentDao extends BaseDao<TableIndexDocumentPojo> {
@Autowired
@Getter

View File

@@ -1,7 +1,6 @@
package com.databasir.dao.impl;
import com.databasir.dao.tables.pojos.TableTriggerDocumentPojo;
import com.databasir.dao.tables.records.TableTriggerDocumentRecord;
import lombok.Getter;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
@@ -12,7 +11,7 @@ import java.util.List;
import static com.databasir.dao.Tables.TABLE_TRIGGER_DOCUMENT;
@Repository
public class TableTriggerDocumentDao extends BaseDao<TableTriggerDocumentRecord, TableTriggerDocumentPojo> {
public class TableTriggerDocumentDao extends BaseDao<TableTriggerDocumentPojo> {
@Autowired
@Getter

View File

@@ -2,7 +2,6 @@ package com.databasir.dao.impl;
import com.databasir.dao.Databasir;
import com.databasir.dao.tables.pojos.UserPojo;
import com.databasir.dao.tables.records.UserRecord;
import com.databasir.dao.value.GroupMemberDetailPojo;
import lombok.Getter;
import org.jooq.Condition;
@@ -23,7 +22,7 @@ import static com.databasir.dao.Tables.USER_ROLE;
@Repository
public class UserDao extends BaseDao<UserRecord, UserPojo> {
public class UserDao extends BaseDao<UserPojo> {
@Autowired
@Getter

View File

@@ -1,7 +1,6 @@
package com.databasir.dao.impl;
import com.databasir.dao.tables.pojos.UserRolePojo;
import com.databasir.dao.tables.records.UserRoleRecord;
import com.databasir.dao.value.GroupMemberSimplePojo;
import lombok.Getter;
import org.jooq.Condition;
@@ -19,7 +18,7 @@ import static com.databasir.dao.Tables.USER_ROLE;
@Repository
public class UserRoleDao extends BaseDao<UserRoleRecord, UserRolePojo> {
public class UserRoleDao extends BaseDao<UserRolePojo> {
@Autowired
@Getter

View File

@@ -0,0 +1,18 @@
CREATE TABLE operation_log
(
id BIGINT PRIMARY KEY AUTO_INCREMENT NOT NULL,
operator_user_id INT NOT NULL COMMENT 'ref to user.id',
operator_username VARCHAR(128) NOT NULL COMMENT 'user.username',
operator_nickname VARCHAR(255) NOT NULL COMMENT 'user.nickname',
operation_module VARCHAR(128) NOT NULL,
operation_code VARCHAR(255) NOT NULL,
operation_name VARCHAR(255) NOT NULL,
operation_response JSON NOT NULL,
is_success BOOLEAN NOT NULL DEFAULT FALSE,
involved_project_id INT DEFAULT NULL COMMENT 'ref to project.id',
involved_group_id INT DEFAULT NULL COMMENT 'ref to group.id',
involved_user_id INT DEFAULT NULL COMMENT 'ref to user.id',
create_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) CHARSET utf8mb4
COLLATE utf8mb4_unicode_ci;