ES index相当于宽表、字段较多,
通过匿名组合,实际效果是一样的,产生的json相同。
好的定义
定义两个结构体SysDept、Employee,然后
type EsDeptEmp struct {
SysDept
Employee
}
type SysDept struct { // SysDeptBase // ModelBase /* 部门id */ DeptId int64 `gorm:"column:dept_id;type:bigint(20);PRIMARY_KEY;comment:'部门id'" json:"dept_id,string"` /* 父部门id */ ParentId int64 `gorm:"column:parent_id;type:bigint(20);comment:'父部门id';default:0" json:"parent_id,string"` /* 祖级列表 */ Ancestors string `gorm:"column:ancestors;type:varchar(50);comment:'祖级列表'" json:"ancestors"`
...
}
type Employee struct { basedto.BaseEntity `gorm:"-"` /* 员工编号 */ Id int32 `gorm:"column:id;type:int(11);PRIMARY_KEY;comment:'员工编号'" json:"id"` /* 所属部门 */ DepartmentId int32 `gorm:"column:department_id;type:int(11);comment:'所属部门';default:0" json:"dept_id"` /* 员工姓名 */ Name string `gorm:"column:name;type:varchar(16);comment:'员工姓名'" json:"name"` /* 性别 */ Gend
...
}
不好的定义
type EsDeptEmpComplex struct { /* 部门id */ DeptId int64 `gorm:"column:dept_id;type:bigint(20);PRIMARY_KEY;comment:'部门id'" json:"dept_id,string"` /* 父部门id */ ParentId int64 `gorm:"column:parent_id;type:bigint(20);comment:'父部门id';default:0" json:"parent_id,string"` /* 祖级列表 */ Ancestors string `gorm:"column:ancestors;type:varchar(50);comment:'祖级列表'" json:"ancestors"` /* 部门名称 */ DeptName string `gorm:"column:dept_name;type:varchar(30);comment:'部门名称'" json:"dept_name"` /* 显示顺序 */ OrderNum int32 `gorm:"column:order_num;type:int(4);comment:'显示顺序';default:0" json:"order_nm"` /* 负责人 */ Leader string `gorm:"column:leader;type:varchar(20);comment:'负责人'" json:"leader"` /* 联系电话 */ /* 部门状态(0正常 1停用) */ Status string `gorm:"column:status;type:char(1);comment:'部门状态(0正常 1停用)';default:\'0\'" json:"status"` /* 删除标志(0代表存在 2代表删除) */ DelFlag string `gorm:"column:del_flag;type:char(1);comment:'删除标志(0代表存在 2代表删除)';default:\'0\'" json:"del_flag"` /* 创建者 */ CreateBy string `gorm:"column:create_by;type:varchar(64);comment:'创建者'" json:"create_by"` /* 创建时间 */ CreateTime time.Time `gorm:"column:create_time;type:datetime;comment:'创建时间'" json:"create_time"` /* 更新者 */ UpdateBy string `gorm:"column:update_by;type:varchar(64);comment:'更新者'" json:"update_by"` /* 更新时间 */ UpdateTime time.Time `gorm:"column:update_time;type:datetime;comment:'更新时间'" json:"-"` /* 员工编号 */ Id int32 `gorm:"column:id;type:int(11);PRIMARY_KEY;comment:'员工编号'" json:"id"` /* 所属部门 */ DepartmentId int32 `gorm:"column:department_id;type:int(11);comment:'所属部门';default:0" json:"dept_id"` /* 员工姓名 */ Name string `gorm:"column:name;type:varchar(16);comment:'员工姓名'" json:"name"` /* 性别 */ Gender string `gorm:"column:gender;type:char(4);comment:'性别'" json:"gender"` /* 出生日期 */ Birthday time.Time `gorm:"column:birthday;type:date;comment:'出生日期'" json:"birthday"` /* 身份证号 */ IdCard string `gorm:"column:id_card;type:char(18);comment:'身份证号'" json:"id_card"` /* 婚姻状况 */ Wedlock string `gorm:"column:wedlock;type:varchar(8);comment:'婚姻状况'" json:"wedlock"` /* 民族 */ NationId int32 `gorm:"column:nation_id;type:int(8);comment:'民族'" json:"nation_id"` /* 籍贯 */ NativePlace string `gorm:"column:native_place;type:varchar(20);comment:'籍贯'" json:"native_place"` /* 政治面貌 */ PoliticId int32 `gorm:"column:politic_id;type:int(8);comment:'政治面貌'" json:"politic_id"` /* 邮箱 */ Email string `gorm:"column:email;type:varchar(20);comment:'邮箱'" json:"email"` /* 电话号码 */ Phone string `gorm:"column:phone;type:varchar(11);comment:'电话号码'" json:"phone"` /* 联系地址 */ Address string `gorm:"column:address;type:varchar(64);comment:'联系地址'" json:"address"` /* 职称ID */ JobLevelId int32 `gorm:"column:job_level_id;type:int(11);comment:'职称ID'" json:"job_level_id"` /* 职位ID */ PosId int32 `gorm:"column:pos_id;type:int(11);comment:'职位ID'" json:"pos_id"` /* 聘用形式 */ EngageForm string `gorm:"column:engage_form;type:varchar(8);comment:'聘用形式'" json:"engage_form"` /* 最高学历 */ TiptopDegree string `gorm:"column:tiptop_degree;type:varchar(8);comment:'最高学历'" json:"tiptop_degree"` /* 所属专业 */ Specialty string `gorm:"column:specialty;type:varchar(32);comment:'所属专业'" json:"specialty"` /* 毕业院校 */ School string `gorm:"column:school;type:varchar(32);comment:'毕业院校'" json:"school"` /* 入职日期 */ BeginDate time.Time `gorm:"column:begin_date;type:date;comment:'入职日期'" json:"begin_date"` /* 在职状态 */ WorkState string `gorm:"column:work_state;type:varchar(8);comment:'在职状态';default:\'在职\'" json:"work_state"` /* 工号 */ Code string `gorm:"column:core;type:varchar(8);comment:'工号'" json:"core"` /* 合同期限 */ ContractTerm float64 `gorm:"column:contract_term;type:double;comment:'合同期限'" json:"contract_term"` /* 转正日期 */ ConversionTime time.Time `gorm:"column:conversion_time;type:date;comment:'转正日期'" json:"conversion_time"` /* 离职日期 */ NotWokDate time.Time `gorm:"column:not_wok_date;type:date;comment:'离职日期'" json:"not_wok_date"` /* 合同起始日期 */ BeginContract time.Time `gorm:"column:begin_contract;type:date;comment:'合同起始日期'" json:"begin_contract"` /* 合同终止日期 */ EndContract time.Time `gorm:"column:end_contract;type:date;comment:'合同终止日期'" json:"end_contract"` /* 工龄 */ WorkAge int32 `gorm:"column:work_age;type:int(11);comment:'工龄'" json:"work_age"` /* */ WorkId string `gorm:"column:work_id;type:varchar(16);comment:''" json:"work_id"` }
测试用例
func Test0011_json(t *testing.T) { var es = NewEsDepEmp() logrus.Info(jsonutils.ToJsonPretty(es)) } func Test0012_json(t *testing.T) { var es = NewEsDepEmpComplex() logrus.Info(jsonutils.ToJsonPretty(es)) } 结果一样:
INFO[2024-08-813 09:00:19]D:/go-ichub/go/gowebfactroy-v3/internal/shop/model/EsDeptEmp_test.go:15 gitee.com/ichub/gowebfactroy-v3/internal/shop/model.Test0012_json() {
"parent_id": "0",
"ancestors": "",
"dept_name": "",
"order_nm": 0,
"leader": "",
"status": "",
"del_flag": "",
"create_by": "",
"create_time": "0001-01-01T00:00:00Z",
"update_by": "",
"id": 0,
"name": "",
"gender": "",
"birthday": "0001-01-01T00:00:00Z",
"id_card": "",
"wedlock": "",
"nation_id": 0,
"native_place": "",
"politic_id": 0,
"email": "",
"phone": "",
"address": "",
"job_level_id": 0,
"pos_id": 0,
"engage_form": "",
"tiptop_degree": "",
"specialty": "",
"school": "",
"begin_date": "0001-01-01T00:00:00Z",
"work_state": "",
"core": "",
"contract_term": 0,
"conversion_time": "0001-01-01T00:00:00Z",
"not_wok_date": "0001-01-01T00:00:00Z",
"begin_contract": "0001-01-01T00:00:00Z",
"end_contract": "0001-01-01T00:00:00Z",
"work_age": 0,
"work_id": ""
}