Recientemente, la estructura gorm se utiliza para recibir parámetros relevantes desde el front-end y para agregar, eliminar, modificar y consultar datos. En el pasado, después de recibir todo tipo de parámetros con declaraciones nativas, puede formar directamente la declaración deseada a través de la declaración, especialmente para tablas con los mismos campos de tabla de datos, solo necesita cambiar el nombre de la tabla. Pero después de usar la estructura, ¿cómo modificarla? Todavía no estoy familiarizado con este gorm, déjame registrar la forma en que finalmente me di cuenta. Mira directamente el código final.
Método 1: cambiar la tabla
La estructura está escrita así.
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
}
método de llamada
Algo como esto:
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表插入一条记录
Método 2: Transferencia entre dos estructuras
De esta manera no hay mucho que escribir, puede resolver el problema, probablemente así:
//结构体如下
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)