Browse Source

添加操作日志成熟领域的

yjp 1 year ago
parent
commit
a5a718133e

+ 41 - 0
convenient/data_containers/operate_log/operate_log.yaml

@@ -0,0 +1,41 @@
+kind: DataContainer
+spec:
+  namespace: # 替换
+  data_source: # 替换
+  name: # 替换.operate_logs
+  spec:
+    table_name: # 替换.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 - 1
convenient/domain/configuration/api.go

@@ -82,7 +82,7 @@ func (simple *Simple) bind(binder *binding.Binder) {
 	})
 }
 
-func BindConfiguration(app *application.App, simple *Simple) {
+func Bind(app *application.App, simple *Simple) {
 	binder := binding.NewBinder(app.Api().ChooseRouter(api.RouterPrefix, ""), app.Infrastructure())
 	simple.bind(binder)
 }

+ 10 - 0
convenient/domain/operate_log/Info.go

@@ -0,0 +1,10 @@
+package operate_log
+
+type Info struct {
+	Resource     string `json:"resource" sqlresult:"column:resource"`
+	ResourceID   string `json:"resourceId" sqlresult:"column:resource_id"`
+	Operate      string `json:"operate" sqlresult:"column:operate"`
+	OperatorName string `json:"operatorName" sqlresult:"column:operator_name"`
+	OperateTime  string `json:"operateTime" sqlresult:"column:operate_time"`
+	Content      string `json:"content" sqlresult:"column:content"`
+}

+ 95 - 0
convenient/domain/operate_log/api.go

@@ -0,0 +1,95 @@
+package operate_log
+
+import (
+	"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/api/request"
+	"git.sxidc.com/go-framework/baize/framework/core/api/response"
+	"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/infrastructure"
+	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
+	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
+	"git.sxidc.com/go-tools/utils/strutils"
+)
+
+// Simple Bind参数
+type Simple struct {
+	// schema
+	Schema string
+}
+
+func (simple *Simple) bind(binder *binding.Binder) {
+	tableName := domain.TableName(simple.Schema, &ValueObject{})
+
+	binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[Info]]{
+		Path:             "/operateLog/query",
+		SendResponseFunc: response.SendInfosResponse[Info],
+		RequestParams:    &QueryLogsQueryParams{},
+		ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[Info], error) {
+			errResponse := response.InfosData[Info]{
+				Infos: make([]Info, 0),
+			}
+
+			dbExecutor := i.DBExecutor()
+
+			queryParams, err := request.ToConcrete[*QueryLogsQueryParams](params)
+			if err != nil {
+				return errResponse, err
+			}
+
+			conditions := sql.NewConditions()
+
+			if strutils.IsStringNotEmpty(queryParams.Resource) {
+				conditions.Like(ColumnResource, "%"+queryParams.Resource+"%")
+			}
+
+			if strutils.IsStringNotEmpty(queryParams.ResourceID) {
+				conditions.Equal(ColumnResourceID, queryParams.ResourceID)
+			}
+
+			if strutils.IsStringNotEmpty(queryParams.Operate) {
+				conditions.Like(ColumnOperate, "%"+queryParams.Operate+"%")
+			}
+
+			if strutils.IsStringNotEmpty(queryParams.OperatorName) {
+				conditions.Like(ColumnOperatorName, "%"+queryParams.OperatorName+"%")
+			}
+
+			if strutils.IsStringNotEmpty(queryParams.StartOperateTime) {
+				conditions.GreaterThanAndEqual(ColumnOperateTime, queryParams.StartOperateTime)
+			}
+
+			if strutils.IsStringNotEmpty(queryParams.EndOperateTime) {
+				conditions.LessThanAndEqual(ColumnOperateTime, queryParams.EndOperateTime)
+			}
+
+			results, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
+				TableName:  tableName,
+				Conditions: conditions,
+				PageNo:     queryParams.PageNo,
+				PageSize:   queryParams.PageSize,
+			})
+			if err != nil {
+				return errResponse, err
+			}
+
+			infos := make([]Info, 0)
+			err = sql.ParseSqlResult(results, &infos)
+			if err != nil {
+				return errResponse, err
+			}
+
+			return response.InfosData[Info]{
+				Infos:      infos,
+				TotalCount: totalCount,
+				PageNo:     queryParams.PageNo,
+			}, nil
+		},
+	})
+}
+
+func Bind(app *application.App, simple *Simple) {
+	binder := binding.NewBinder(app.Api().ChooseRouter(api.RouterPrefix, ""), app.Infrastructure())
+	simple.bind(binder)
+}

+ 15 - 0
convenient/domain/operate_log/request_params.go

@@ -0,0 +1,15 @@
+package operate_log
+
+import "git.sxidc.com/go-framework/baize/framework/core/api/request"
+
+type (
+	QueryLogsQueryParams struct {
+		request.BaseQueryParams
+		Resource         string `form:"resource"`
+		ResourceID       string `form:"resourceId"`
+		Operate          string `form:"operate"`
+		OperatorName     string `form:"operatorName"`
+		StartOperateTime string `form:"startOperateTime"`
+		EndOperateTime   string `form:"endOperateTime"`
+	}
+)

+ 56 - 0
convenient/domain/operate_log/value_object.go

@@ -0,0 +1,56 @@
+package operate_log
+
+import (
+	"git.sxidc.com/go-framework/baize/framework/core/domain"
+	"git.sxidc.com/go-framework/baize/framework/core/domain/value_object"
+	"time"
+)
+
+const (
+	FieldResource     = "Resource"
+	FieldResourceID   = "ResourceID"
+	FieldOperate      = "Operate"
+	FieldOperatorID   = "OperatorID"
+	FieldOperatorName = "OperatorName"
+	FieldOperateTime  = "OperateTime"
+	FieldContent      = "Content"
+)
+
+var fieldMap = map[string]string{
+	FieldResource:     "资源名称",
+	FieldResourceID:   "资源ID",
+	FieldOperate:      "操作",
+	FieldOperatorID:   "操作者ID",
+	FieldOperatorName: "操作者",
+	FieldOperateTime:  "操作时间",
+	FieldContent:      "日志内容",
+}
+
+var (
+	ColumnResource     = domain.ColumnName(FieldResource)
+	ColumnResourceID   = domain.ColumnName(FieldResourceID)
+	ColumnOperate      = domain.ColumnName(FieldOperate)
+	ColumnOperatorID   = domain.ColumnName(FieldOperatorID)
+	ColumnOperatorName = domain.ColumnName(FieldOperatorName)
+	ColumnOperateTime  = domain.ColumnName(FieldOperateTime)
+	ColumnContent      = domain.ColumnName(FieldContent)
+)
+
+type ValueObject struct {
+	value_object.Base
+	Resource     string    `sqlmapping:"column:resource" sqlresult:"column:resource"`
+	ResourceID   string    `sqlmapping:"column:resource_id" sqlresult:"column:resource_id"`
+	Operate      string    `sqlmapping:"column:operate" sqlresult:"column:operate"`
+	OperatorID   string    `sqlmapping:"column:operator_id" sqlresult:"column:operator_id"`
+	OperatorName string    `sqlmapping:"column:operator_name" sqlresult:"column:operator_name"`
+	OperateTime  time.Time `sqlmapping:"column:operate_time" sqlresult:"column:operate_time"`
+	Content      string    `sqlmapping:"column:content" sqlresult:"column:content"`
+}
+
+func (v *ValueObject) DomainCNName() string {
+	return "操作日志"
+}
+
+func (v *ValueObject) DomainCamelName() string {
+	return "OperateLog"
+}

+ 58 - 0
convenient/domain/operate_log/writer.go

@@ -0,0 +1,58 @@
+package operate_log
+
+import (
+	"encoding/json"
+	"git.sxidc.com/go-framework/baize/framework/core/domain"
+	"git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
+	"time"
+)
+
+type ObjectInfo struct {
+	Resource   string
+	ResourceID string
+}
+
+type OperatorInfo struct {
+	OperatorID   string
+	OperatorName string
+}
+
+type CanLogObject interface {
+	ObjectInfo() ObjectInfo
+	OperatorInfo() OperatorInfo
+	LogContent() map[string]any
+}
+
+func WriteLog(dbExecutor database.Executor, dbSchema string, operate string, objects ...CanLogObject) error {
+	vs := make([]ValueObject, len(objects))
+
+	now := time.Now().Local()
+
+	for _, object := range objects {
+		objectInfo := object.ObjectInfo()
+		operatorInfo := object.OperatorInfo()
+		content := object.LogContent()
+
+		jsonContent, err := json.Marshal(content)
+		if err != nil {
+			return err
+		}
+
+		vs = append(vs, ValueObject{
+			Resource:     objectInfo.Resource,
+			ResourceID:   objectInfo.ResourceID,
+			Operate:      operate,
+			OperatorID:   operatorInfo.OperatorID,
+			OperatorName: operatorInfo.OperatorName,
+			OperateTime:  now,
+			Content:      string(jsonContent),
+		})
+	}
+
+	err := database.InsertEntity(dbExecutor, domain.TableName(dbSchema, &ValueObject{}), vs)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}

+ 1 - 1
convenient/domain/sql_executor/api.go

@@ -139,7 +139,7 @@ func (simple *Simple) bind(binder *binding.Binder) {
 	})
 }
 
-func BindSqlExecutor(app *application.App, simple *Simple) {
+func Bind(app *application.App, simple *Simple) {
 	binder := binding.NewBinder(app.Api().ChooseRouter(api.RouterPrefix, ""), app.Infrastructure())
 	simple.bind(binder)
 }