package repository import ( "encoding/json" "gorm.io/gorm" "wireguard-dashboard/client" "wireguard-dashboard/model/entity" "wireguard-dashboard/model/vo" ) type system struct { *gorm.DB } func System() system { return system{ client.DB, } } // GetConfigByCode // @description: // @receiver r // @param code // @return data // @return err func (r system) GetConfigByCode(code string) (data *entity.Setting, err error) { err = r.Model(&entity.Setting{}).Where("code = ?", code).First(&data).Error return } // GetServerSetting // @description: 获取服务端全局配置 // @receiver r // @return data // @return err func (r system) GetServerSetting() (data *vo.ServerSetting, err error) { config, err := r.GetConfigByCode("SERVER_SETTING") if err != nil { return nil, err } if err = json.Unmarshal([]byte(config.Data), &data); err != nil { return } return } // Save // @description: 新增/编辑配置 // @receiver r // @param ent // @return err func (r system) Save(ent *entity.Setting) (err error) { conf, err := r.GetConfigByCode(ent.Code) // 新增 if err != nil || conf == nil { return r.Model(&entity.Setting{}).Create(ent).Error } // 更新 return r.Model(&entity.Setting{}).Where("code = ?", ent.Code).Updates(&ent).Error }