You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package Test
import (
"encoding/json"
"fmt"
CopyUtil "github.com/rocket049/gostructcopy"
"reflect"
"testing"
)
type Person struct {
Name string
Age int
Email string
}
type ChangeLog struct {
Field string `json:"field"`
Source interface{} `json:"source"`
Target interface{} `json:"target"`
}
//复制相似的结构
//go get github.com/rocket049/gostructcopy
func TestOrmModelLog(t *testing.T) {
a := struct {
Id int
Name string
Weight int
a int
}{100, "Dog", 200, 9}
b := struct {
Id int
Name string
Weight int
Desc string
OperateName string
b int
}{}
//拷贝过来主体的属性
CopyUtil.StructCopy(&a, &b)
//增加没有属性
b.Desc = "黄海测试的修改内容"
b.OperateName = "insert"
jsonBytes, err := json.Marshal(b)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(jsonBytes))
}
func TestActionLog(t *testing.T) {
//管理员的操作日志
//1、增加
//把“增加”和增加完的内容记录下来即可。
//
//2、修改、删除打标识
//修改标识: UPDATE
//内容:对比两条记录,有啥区别?同时需要记录最后的修改后内容
var a Person
a.Name = "张三"
a.Age = 18
a.Email = "10102@qq.com"
var a1 Person
a1.Name = "张三(修改)"
a1.Age = 18
a1.Email = "10102@qq.com(修改)"
//对比两个Struct
t1 := reflect.TypeOf(a)
v := reflect.ValueOf(a)
v1 := reflect.ValueOf(a1)
var changLogs []ChangeLog
for k := 0; k < t1.NumField(); k++ {
if v.Field(k).Interface() != v1.Field(k).Interface() {
var c ChangeLog
c.Field = t1.Field(k).Name
c.Source = v.Field(k).Interface()
c.Target = v1.Field(k).Interface()
changLogs = append(changLogs, c)
}
}
//json化
jsonBytes, err := json.Marshal(changLogs)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(jsonBytes))
//需要记录操作日志
//1、操作人员
//2、操作时间
//3、操作IP地址
//4、哪个资源学生家长老师班级组织机构)
//5、哪种操作:增加、修改、删除(伪删除)
//6、修改了哪些属性
//7、原来的啥样
//8、修改完了啥样
//9、此资源的相关查询维度信息身份证号如果是人员的话单位编码以方便以后扩展查询功能。
//10、应该是直接写入kafka而不是记录到mysql中需要有一个kafka的专用topic用于记录信息并通过logstash导入到elasticsearch中并提供查询功能
}