yjp пре 1 година
родитељ
комит
140d87d030

+ 19 - 0
project/server/application/domain/class/entity.go

@@ -1,6 +1,7 @@
 package class
 
 import (
+	"git.sxidc.com/go-framework/baize/convenient/domain/operate_log"
 	"git.sxidc.com/go-framework/baize/framework/core/domain"
 	"git.sxidc.com/go-framework/baize/framework/core/domain/entity"
 	"git.sxidc.com/go-framework/baize/framework/core/tag/check"
@@ -82,6 +83,24 @@ func (e *Entity) ForUpdate() error {
 	return nil
 }
 
+func (e *Entity) ObjectInfo() operate_log.ObjectInfo {
+	return operate_log.ObjectInfo{
+		Resource:   e.DomainCamelName(),
+		ResourceID: e.ID,
+	}
+}
+
+func (e *Entity) OperatorInfo() operate_log.OperatorInfo {
+	return operate_log.OperatorInfo{}
+}
+
+func (e *Entity) LogContent() map[string]any {
+	return map[string]any{
+		"name":        e.Name,
+		"student_num": e.StudentNum,
+	}
+}
+
 func (e *Entity) checkUpdateFields(checkResult check.Result) error {
 	if strutils.AllBlank(e.Name) {
 		return errors.New(e.DomainCNName() + "没有传递需要更新的字段")

+ 25 - 1
project/server/application/service/class.go

@@ -2,10 +2,16 @@ package service
 
 import (
 	"baize-demo/project/server/application/domain/class"
+	"git.sxidc.com/go-framework/baize/convenient/domain/operate_log"
 	"git.sxidc.com/go-framework/baize/convenient/entity_crud"
 	"git.sxidc.com/go-framework/baize/framework/binding"
 	"git.sxidc.com/go-framework/baize/framework/core/api"
 	"git.sxidc.com/go-framework/baize/framework/core/application"
+	"git.sxidc.com/go-framework/baize/framework/core/domain"
+	"git.sxidc.com/go-framework/baize/framework/core/domain/entity"
+	"git.sxidc.com/go-framework/baize/framework/core/infrastructure"
+	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
+	"git.sxidc.com/service-supports/fslog"
 )
 
 var classService = &ClassService{}
@@ -32,5 +38,23 @@ func (svc *ClassService) v1(appInstance *application.App) {
 		UpdateJsonBody:     &class.UpdateClassJsonBody{},
 		QueryQueryParams:   &class.GetClassesQueryParams{},
 		GetByIDQueryParams: &class.GetClassByIDQueryParams{},
-	})
+	}, entity_crud.WithCreateCallbacks(&entity_crud.CreateCallbacks{
+		After: func(c *api.Context, e entity.Entity, prepared map[string]any, i *infrastructure.Infrastructure, tx database.Executor) error {
+			go func() {
+				classEntity, err := domain.ToConcrete[*class.Entity](e)
+				if err != nil {
+					fslog.Error(err)
+					return
+				}
+
+				err = operate_log.WriteLog(i.DBExecutor(), dbSchema, "create", classEntity)
+				if err != nil {
+					fslog.Error(err)
+					return
+				}
+			}()
+
+			return nil
+		},
+	}))
 }

+ 1 - 1
project/server/application/service/configuration.go

@@ -19,7 +19,7 @@ func (svc *ConfigurationService) Destroy() error {
 }
 
 func (svc *ConfigurationService) prefixRoot(appInstance *application.App) {
-	configuration.BindConfiguration(appInstance, &configuration.Simple{
+	configuration.Bind(appInstance, &configuration.Simple{
 		Schema: dbSchema,
 	})
 }

+ 25 - 0
project/server/application/service/operate_log.go

@@ -0,0 +1,25 @@
+package service
+
+import (
+	"git.sxidc.com/go-framework/baize/convenient/domain/operate_log"
+	"git.sxidc.com/go-framework/baize/framework/core/application"
+)
+
+var operateLogService = &OperateLogService{}
+
+type OperateLogService struct{}
+
+func (svc *OperateLogService) Init(appInstance *application.App) error {
+	svc.prefixRoot(appInstance)
+	return nil
+}
+
+func (svc *OperateLogService) Destroy() error {
+	return nil
+}
+
+func (svc *OperateLogService) prefixRoot(appInstance *application.App) {
+	operate_log.Bind(appInstance, &operate_log.Simple{
+		Schema: dbSchema,
+	})
+}

+ 1 - 0
project/server/application/service/service.go

@@ -15,6 +15,7 @@ var RegisteredServices = []Service{
 	versionService,
 	configurationService,
 	sqlExecutorService,
+	operateLogService,
 	classService,
 	studentService,
 	identityService,

+ 1 - 1
project/server/application/service/sql_executor.go

@@ -19,5 +19,5 @@ func (svc *SqlExecutorService) Destroy() error {
 }
 
 func (svc *SqlExecutorService) prefixRoot(appInstance *application.App) {
-	sql_executor.BindSqlExecutor(appInstance, &sql_executor.Simple{Schema: dbSchema})
+	sql_executor.Bind(appInstance, &sql_executor.Simple{Schema: dbSchema})
 }

+ 41 - 0
project/server/deployment/data_service/data_containers/operate_log.yaml

@@ -0,0 +1,41 @@
+kind: DataContainer
+spec:
+  namespace: baize
+  data_source: baize
+  name: test.operate_logs
+  spec:
+    table_name: test.operate_logs
+    columns:
+      - name: resource
+        type: varchar(256)
+        comment: 资源名称
+        index: true
+        not_null: true
+      - name: resource_id
+        type: varchar(256)
+        comment: 资源名称
+        index: true
+        not_null: true
+      - name: operate
+        type: varchar(256)
+        comment: 操作
+        index: true
+        not_null: true
+      - name: operator_id
+        type: varchar(256)
+        comment: 操作者ID
+        index: true
+        not_null: true
+      - name: operator_name
+        type: varchar(256)
+        comment: 操作者
+        index: true
+        not_null: true
+      - name: operate_time
+        type: "timestamp with time zone"
+        comment: 操作时间
+        not_null: true
+      - name: content
+        type: text
+        comment: 日志内容
+        not_null: true

+ 1 - 0
project/server/main.go

@@ -20,6 +20,7 @@ import (
 // curl -X GET "http://localhost:31000/example/api/v1/class/query?name=test-new&pageNo=1&pageSize=1"
 // curl -X GET "http://localhost:31000/example/api/v1/class/get?id=2bc2860617584610b0c7a79ed1b45d61"
 // curl -X DELETE "http://localhost:31000/example/api/v1/class/delete?id=e25bd84b39c241a291c39d73c22e9bfe"
+// curl -X GET "http://localhost:31000/example/api/operateLog/query?resource=Class"
 
 // Student
 // curl -X POST -H "Content-Type: application/json" -d '{"name":"test"}' "http://localhost:31000/example/api/v1/student/create"