github.com/wfusion/gofusion@v1.1.14/db/model.go (about) 1 package db 2 3 import ( 4 "time" 5 6 "github.com/google/uuid" 7 "github.com/pkg/errors" 8 "gorm.io/gorm" 9 10 "github.com/wfusion/gofusion/common/utils" 11 "github.com/wfusion/gofusion/common/utils/serialize/json" 12 "github.com/wfusion/gofusion/db/softdelete" 13 ) 14 15 type Data struct { 16 ID uint64 `gorm:"column:id;primary_key;autoIncrement" json:"id"` 17 CreateTimeMs int64 `gorm:"column:create_time;type:bigint;autoCreateTime:milli" json:"createTime"` 18 ModifyTimeMs int64 `gorm:"column:modify_time;type:bigint;autoUpdateTime:milli" json:"modifyTime"` 19 } 20 21 func (d *Data) CreateTime() time.Time { 22 return utils.GetTime(d.CreateTimeMs) 23 } 24 func (d *Data) ModifyTime() time.Time { 25 return utils.GetTime(d.ModifyTimeMs) 26 } 27 func (d *Data) Clone() *Data { 28 if d == nil { 29 return nil 30 } 31 return &Data{ 32 ID: d.ID, 33 CreateTimeMs: d.CreateTimeMs, 34 ModifyTimeMs: d.ModifyTimeMs, 35 } 36 } 37 func (d *Data) Equals(o *Data) bool { 38 if d == nil && o == nil { 39 return true 40 } 41 if d == nil || o == nil { 42 return false 43 } 44 return d.ID == o.ID && 45 d.CreateTimeMs == o.CreateTimeMs && 46 d.ModifyTimeMs == o.ModifyTimeMs 47 } 48 49 // DataSoftDeleted 50 //nolint: revive // struct tag too long issue 51 type DataSoftDeleted struct { 52 ID uint64 `gorm:"column:id;primary_key;autoIncrement" json:"id"` 53 CreateTimeMs int64 `gorm:"column:create_time;type:bigint;autoCreateTime:milli" json:"createTime"` 54 ModifyTimeMs int64 `gorm:"column:modify_time;type:bigint;autoUpdateTime:milli" json:"modifyTime"` 55 DeleteTimeMs softdelete.Timestamp `gorm:"column:delete_time;type:bigint;index:,composite:soft_delete" json:"deleteTime"` 56 Deleted softdelete.Deleted `gorm:"column:deleted;index:,composite:soft_delete;default:false" json:"deleted"` 57 } 58 59 func (d *DataSoftDeleted) CreateTime() time.Time { 60 return utils.GetTime(d.CreateTimeMs) 61 } 62 func (d *DataSoftDeleted) ModifyTime() time.Time { 63 return utils.GetTime(d.ModifyTimeMs) 64 } 65 func (d *DataSoftDeleted) DeleteTime() *time.Time { 66 if !d.DeleteTimeMs.Valid { 67 return nil 68 } 69 return utils.AnyPtr(utils.GetTime(d.DeleteTimeMs.Int64)) 70 } 71 func (d *DataSoftDeleted) Clone() *DataSoftDeleted { 72 if d == nil { 73 return nil 74 } 75 return &DataSoftDeleted{ 76 ID: d.ID, 77 Deleted: d.Deleted, 78 CreateTimeMs: d.CreateTimeMs, 79 ModifyTimeMs: d.ModifyTimeMs, 80 DeleteTimeMs: d.DeleteTimeMs, 81 } 82 } 83 func (d *DataSoftDeleted) Equals(o *DataSoftDeleted) bool { 84 if d == nil && o == nil { 85 return true 86 } 87 if d == nil || o == nil { 88 return false 89 } 90 return d.ID == o.ID && 91 d.CreateTimeMs == o.CreateTimeMs && 92 d.ModifyTimeMs == o.ModifyTimeMs && 93 d.DeleteTimeMs == o.DeleteTimeMs && 94 d.Deleted == o.Deleted 95 } 96 97 type Business struct { 98 Data 99 100 UUID uuid.UUID `gorm:"column:uuid;type:varchar(36);uniqueIndex" json:"uuid"` 101 BizCreateTimeMs int64 `gorm:"column:biz_create_time;type:bigint" json:"bizCreateTime"` 102 BizModifyTimeMs int64 `gorm:"column:biz_modify_time;type:bigint" json:"bizModifyTime"` 103 } 104 105 func (b *Business) BeforeCreate(tx *gorm.DB) (err error) { 106 if b == nil { 107 tx.Statement.SetColumn("uuid", utils.UUID()) 108 return 109 } 110 if b.UUID == [16]byte{0} { 111 b.UUID = uuid.New() 112 } 113 return 114 } 115 func (b *Business) BeforeCreateFn(tx *gorm.DB) func() error { 116 return func() (err error) { 117 return b.BeforeCreate(tx) 118 } 119 } 120 func (b *Business) BizCreateTime() time.Time { 121 return utils.GetTime(b.BizCreateTimeMs) 122 } 123 func (b *Business) BizModifyTime() time.Time { 124 return utils.GetTime(b.BizModifyTimeMs) 125 } 126 func (b *Business) Clone() *Business { 127 if b == nil { 128 return nil 129 } 130 return &Business{ 131 Data: *b.Data.Clone(), 132 UUID: b.UUID, 133 BizCreateTimeMs: b.BizCreateTimeMs, 134 BizModifyTimeMs: b.BizModifyTimeMs, 135 } 136 } 137 func (b *Business) Equals(o *Business) bool { 138 if b == nil && o == nil { 139 return true 140 } 141 if b == nil || o == nil { 142 return false 143 } 144 return b.Data.Equals(&o.Data) && 145 b.UUID == o.UUID && 146 b.BizCreateTimeMs == o.BizCreateTimeMs && 147 b.BizModifyTimeMs == o.BizModifyTimeMs 148 } 149 150 type BusinessSoftDeleted struct { 151 DataSoftDeleted 152 153 UUID uuid.UUID `gorm:"column:uuid;type:varchar(36);uniqueIndex:uniq_uuid" json:"uuid"` 154 BizCreateTimeMs int64 `gorm:"column:biz_create_time;type:bigint" json:"bizCreateTime"` 155 BizModifyTimeMs int64 `gorm:"column:biz_modify_time;type:bigint" json:"bizModifyTime"` 156 } 157 158 func (b *BusinessSoftDeleted) BeforeCreate(tx *gorm.DB) (err error) { 159 if b == nil { 160 tx.Statement.SetColumn("uuid", utils.UUID()) 161 tx.Statement.SetColumn("delete_time", nil) 162 return 163 } 164 if b.UUID == [16]byte{0} { 165 b.UUID = uuid.New() 166 } 167 return 168 } 169 func (b *BusinessSoftDeleted) BeforeCreateFn(tx *gorm.DB) func() error { 170 return func() (err error) { 171 return b.BeforeCreate(tx) 172 } 173 } 174 func (b *BusinessSoftDeleted) BizCreateTime() time.Time { 175 return utils.GetTime(b.BizCreateTimeMs) 176 } 177 func (b *BusinessSoftDeleted) BizModifyTime() time.Time { 178 return utils.GetTime(b.BizModifyTimeMs) 179 } 180 func (b *BusinessSoftDeleted) Clone() *BusinessSoftDeleted { 181 if b == nil { 182 return nil 183 } 184 return &BusinessSoftDeleted{ 185 DataSoftDeleted: *b.DataSoftDeleted.Clone(), 186 UUID: b.UUID, 187 BizCreateTimeMs: b.BizCreateTimeMs, 188 BizModifyTimeMs: b.BizModifyTimeMs, 189 } 190 } 191 func (b *BusinessSoftDeleted) Equals(o *BusinessSoftDeleted) bool { 192 if b == nil && o == nil { 193 return true 194 } 195 if b == nil || o == nil { 196 return false 197 } 198 return b.DataSoftDeleted.Equals(&o.DataSoftDeleted) && 199 b.UUID == o.UUID && 200 b.BizCreateTimeMs == o.BizCreateTimeMs && 201 b.BizModifyTimeMs == o.BizModifyTimeMs 202 } 203 204 func CheckGormErrorFn(tx *gorm.DB) func() error { return func() error { return tx.Error } } 205 func JsonUnmarshalFn(tx *gorm.DB, obj, field any) func() error { 206 return func() (err error) { 207 if tx.Error != nil { 208 return 209 } 210 211 var bs []byte 212 switch v := field.(type) { 213 case string: 214 bs = []byte(v) 215 case *string: 216 if v == nil { 217 return 218 } 219 bs = []byte(*v) 220 case []byte: 221 bs = v 222 case *[]byte: 223 if v == nil { 224 return 225 } 226 bs = *v 227 default: 228 return tx.AddError(errors.New("unsupported unmarshal field type")) 229 } 230 // be compatible with empty value 231 if len(bs) == 0 { 232 bs = []byte("null") 233 } 234 235 return tx.AddError(json.Unmarshal(bs, &obj)) 236 } 237 } 238 func JsonMarshalFn(tx *gorm.DB, obj, field any) func() error { 239 return func() (err error) { 240 if tx.Error != nil { 241 return 242 } 243 244 switch v := field.(type) { 245 case **string, **[]byte, *string, *[]byte: 246 if v == nil { 247 return 248 } 249 bs, err := json.Marshal(obj) 250 if err != nil { 251 return tx.AddError(errors.Wrap(err, "gorm json marshal error")) 252 } 253 switch f := field.(type) { 254 case *string: 255 *f = string(bs) 256 case **string: 257 *f = utils.AnyPtr(string(bs)) 258 case *[]byte: 259 *f = bs 260 case **[]byte: 261 *f = utils.AnyPtr(bs) 262 } 263 default: 264 return tx.AddError(errors.New("unsupported marshal field type")) 265 } 266 return 267 } 268 269 }