mh_server/model/base_model.go

129 lines
3.3 KiB
Go
Raw Permalink Normal View History

2021-06-30 02:12:05 +00:00
package model
import (
"time"
"github.com/jinzhu/gorm"
)
//type CRUD interface {
// // TableName 返回表名
// TableName() string
//
// // Create 创建一条记录
// Create() int
//
// // CreateTx 带事务创建记录
// CreateTx(tx *gorm.DB) int
//
// // Delete 删除一条记录
// Delete() int
//
// // Delete 带事务删除一条记录
// DeleteTx(tx *gorm.DB) int
//
// // Update 直接更一条记录
// Update(value map[string]interface{}) int
//
// // UpdateTx 可通过事务
// UpdateTx(tx *gorm.DB, where, value map[string]interface{}) int
//
// // Get 获取一条记录
// //Get(where map[string]interface{}, selectFields ...string) (interface{}, int)
//
// // GetU 获取一条记录,并且可以返回指定字段
// GetU(where map[string]interface{}, value interface{}, selectFields ...string) int
//
// // List 获取一个列表
// //List(where map[string]interface{}, selectFields ...string) (interface{}, int)
//
// // List 获取一个列表,并且可以返回指定字段
// ListU(where map[string]interface{}, value interface{}, selectFields ...string) int
//
// // Aggregate 聚合计算
// Aggregate(where map[string]interface{}, aggregate where.AggregateSymbleBuilder) (int64, int)
//}
var RecordNotFound = gorm.ErrRecordNotFound
type Model struct {
ID uint32 `json:"id" gorm:"primary_key"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"-"`
DeletedAt *time.Time `json:"-" sql:"index"`
}
//func getTodayBegin() time.Time {
// year, month, day := time.Now().Date()
// return time.Date(year, month, day, 0, 0, 0, 0, time.Now().Location())
//}
type Unix struct {
ID uint32 `gorm:"primary_key"`
Created time.Time `gorm:"-"` // 忽略储存在数据库
CreatedUnix int64
Updated time.Time `gorm:"-"` // 忽略储存在数据库
UpdatedUnix int64
}
// 查询前把时间格式化
func (m *Unix) AfterFind() (err error) {
m.Created = time.Unix(m.CreatedUnix, 0).Local()
m.Updated = time.Unix(m.UpdatedUnix, 0).Local()
return
}
// 更新前对时间戳创建
func (m *Unix) BeforeCreate() (err error) {
m.UpdatedUnix = time.Now().Local().Unix()
m.CreatedUnix = time.Now().Local().Unix()
return
}
// 更新前对时间戳更改
func (m *Unix) BeforeUpdate(scope *gorm.Scope) (err error) {
return scope.SetColumn("UpdatedUnix", time.Now().Local().Unix())
}
type Unix64 struct {
ID uint64 `gorm:"primary_key"`
Created time.Time `gorm:"-"` // 忽略储存在数据库
CreatedUnix int64
Updated time.Time `gorm:"-"` // 忽略储存在数据库
UpdatedUnix int64
}
// 查询前把时间格式化
func (m *Unix64) AfterFind() (err error) {
m.Created = time.Unix(m.CreatedUnix, 0).Local()
m.Updated = time.Unix(m.UpdatedUnix, 0).Local()
return
}
// 更新前对时间戳创建
func (m *Unix64) BeforeCreate() {
m.UpdatedUnix = time.Now().Local().Unix()
m.CreatedUnix = time.Now().Local().Unix()
}
// 更新前对时间戳更改
func (m *Unix64) BeforeUpdate(scope *gorm.Scope) (err error) {
return scope.SetColumn("UpdatedUnix", time.Now().Local().Unix())
}
type Deleted struct {
Unix64
DeletedUnix int64 `sql:"index"`
}
// 删除回调
func (d *Deleted) BeforeCreate() {
d.Unix64.BeforeCreate()
d.DeletedUnix = -1
}
// 执行删除操作
func (d *Deleted) BeforeDelete(scope *gorm.Scope) (err error) {
return scope.SetColumn("DeletedUnix", time.Now().Local().Unix())
}