How does the gorm structure map to multiple tables for new operations

Recently, the gorm structure is used to receive relevant parameters from the front end, and to add, delete, modify and query data. In the past, after receiving all kinds of parameters with native statements, you can directly form the desired statement through the statement, especially for tables with the same data table fields, you only need to change the table name. But after using the structure, how to modify it? I am still not familiar with this gorm just now. Let me record the way I finally realized it. Look directly at the final code

Method 1: Change the table

The structure is written like this

type ObjTable1 struct {
    
    
	PrcoId         int64     `gorm:"column:PROCID;type:bigint;comment:处置信息//处置Id,与报警Id对应,Key"`
	RtuCode        int64     `gorm:"column:RTUCODE;type:bigint;comment:终端号"`
	AlarmCode      int64     `gorm:"column:ALARMCODE;type:bigint;comment:报警代码:<-dic_alarm_type.Code)"`
	AlarmId        int64     `gorm:"column:ALARMID;type:bigint;comment:报警Id"`
	AlarmId1       int64     `gorm:"column:ALARMID1;type:bigint;comment:相同报警原因的第1次报警Id"`
	DispNO         int64     `gorm:"column:DISPNO;type:bigint;comment:调度次数,相同报警原因可能有多次调度"`
	AbnoGrade      int64     `gorm:"column:ABNOGRADE;type:bigint;comment:异常级别:1-3"`
	DispClass      int64     `gorm:"column:DISPCLASS;type:bigint;comment:调度类型:1-4"`
	DispDT         time.Time `gorm:"column:DISPDT;comment:调度时间"`
	DispType       int64     `gorm:"column:DISPTYPE;type:bigint;comment:调度类型:sys_dict_type(cx_disp_type):0-未处置,1-继续观察,2-指令措施,3-派发任务"`
	DispDesc       string    `gorm:"column:DISPDESC;type:varchar(1024);comment:对于:1-继续观察来说,观察多少时间;2-指令措施来说,就是发送的的指令,或设置前后的参数说明,3-派发任务来说,就是维护Id。"`
	AllowMins      int64     `gorm:"column:ALLOWMINS;type:bigint;comment:当前处置允许时长,分"`
	AllowTotalMins int64     `gorm:"column:ALLOWTOTALMINS;type:bigint;comment:当前报警允许总时长,分"`
	RemainMins     int64     `gorm:"column:REMAINMINS;type:bigint;comment:当前处置剩余时长,分"`
	MaintId        int64     `gorm:"column:MAINTID;type:bigint;comment:维护号,<-obj_maint_hist.MaintId"`
	ProcState      int64     `gorm:"column:PROCSTATE;type:bigint;comment:处置状态:sys_dict_type(cx_proc_state):0-未处置,1-系统处置,2-人工处置,3-超时处置"`
	//
	TblName string `gorm:"-"`
}
//重写表名
func (v ObjTable1) TableName() string {
    
    
//初始化默认指向的表名
	if v.TblName == "" {
    
    
		return "OBJ_PROC_HIST"
	}
	return v.TblName
}

call method

Something like this:

	var req po.ObjTable1
	err := c.ShouldBindJSON(&req)
	err = global.DB.Create(&req).Error	//A表插入一条记录
	if err != nil {
    
    
		c.JSON(http.StatusOK, vo.NewResp(utils.CodeFail, "执行失败", nil))
		return
	}
	req.TblName = "OBJ_PROC" //指向b表
	wdb := global.DB.Table(req.TableName())
	err = wdb.Create(&req).Error//B表插入一条记录

Method 2: Transfer between two structures

This way is not much to write, it can solve the problem, probably like this:

//结构体如下
type A struct {
    
    
	PrcoId         int64     `gorm:"column:PROCID;type:bigint;comment:处置信息//处置Id,与报警Id对应,Key"`
	RtuCode        int64     `gorm:"column:RTUCODE;type:bigint;comment:终端号"`
	}
type B struct {
    
    
	PrcoId         int64     `gorm:"column:PROCID;type:bigint;comment:处置信息//处置Id,与报警Id对应,Key"`
	RtuCode        int64     `gorm:"column:RTUCODE;type:bigint;comment:终端号"`
	}	
	
// TypeTo 类型转换, 需要json的tag标签一致
func TypeTo(request, response any) (err error) {
    
    
	dataByte, err := json.Marshal(request)
	if err != nil {
    
    
		return
	}
	return json.Unmarshal(dataByte, response)
}
//调用
TypeTo(&A, &B)

Guess you like

Origin blog.csdn.net/weixin_43578304/article/details/129701925