Compare commits

..

No commits in common. "main" and "v1.0.1024" have entirely different histories.

4 changed files with 53 additions and 35 deletions

View File

@ -48,7 +48,7 @@ var pageInfo = []string{
} }
// Crud -------------------------- 泛型CURD核心结构体 -------------------------- // Crud -------------------------- 泛型CURD核心结构体 --------------------------
// Crud GORM 版本的泛型CURD封装R为对应的模型结构体 C为对应模型结构体的字段结构体 // Crud GORM 版本的泛型CURD封装R为对应的模型结构体
type Crud[R any, C any] struct { type Crud[R any, C any] struct {
Dao IDao[C] Dao IDao[C]
} }

View File

@ -98,7 +98,7 @@ func TestBuildWhere(t *testing.T) {
// TestBuildMap 测试 BuildMap 方法 // TestBuildMap 测试 BuildMap 方法
func TestBuildMap(t *testing.T) { func TestBuildMap(t *testing.T) {
// 创建一个空的 Crud 实例用于测试(不需要实际的 Dao // 创建一个空的 Crud 实例用于测试(不需要实际的 Dao
var crud Crud[interface{}, interface{}] var crud Crud[interface{}]
t.Run("BuildMapWithoutField", func(t *testing.T) { t.Run("BuildMapWithoutField", func(t *testing.T) {
result := crud.BuildMap(">", 18) result := crud.BuildMap(">", 18)
@ -131,7 +131,7 @@ func TestBuildMap(t *testing.T) {
// TestClearField 测试 ClearField 方法 // TestClearField 测试 ClearField 方法
func TestClearField(t *testing.T) { func TestClearField(t *testing.T) {
var crud Crud[interface{}, interface{}] var crud Crud[interface{}]
t.Run("ClearFieldBasic", func(t *testing.T) { t.Run("ClearFieldBasic", func(t *testing.T) {
req := struct { req := struct {
@ -210,7 +210,7 @@ func TestClearField(t *testing.T) {
// TestBuildWhereAndOr 测试 BuildWhereAndOr 方法 // TestBuildWhereAndOr 测试 BuildWhereAndOr 方法
func TestBuildWhereAndOr(t *testing.T) { func TestBuildWhereAndOr(t *testing.T) {
var crud Crud[interface{}, interface{}] var crud Crud[interface{}]
t.Run("SimpleAND", func(t *testing.T) { t.Run("SimpleAND", func(t *testing.T) {
where := crud.BuildWhereAndOr(). where := crud.BuildWhereAndOr().

View File

@ -1,31 +0,0 @@
package base
import (
"github.com/gogf/gf/v2/os/gtime"
"gorm.io/gorm"
)
type IdModel struct {
Id int `json:"id" gorm:"column:id;type:int(11);common:id"`
}
type TimeModel struct {
CreateTime string `json:"create_time" gorm:"column:create_time;type:varchar(255);common:创建时间"`
UpdateTime string `json:"update_time" gorm:"column:update_time;type:varchar(255);common:更新时间"`
}
func (tm *TimeModel) BeforeCreate(scope *gorm.DB) error {
scope.Set("create_time", gtime.Datetime())
scope.Set("update_time", gtime.Datetime())
return nil
}
func (tm *TimeModel) BeforeUpdate(scope *gorm.DB) error {
scope.Set("update_time", gtime.Datetime())
return nil
}
//func (tm *TimeModel) AfterFind(scope *gorm.DB) error {
// tm.CreateTime = gtime.New(tm.CreateTime).String()
// tm.UpdateTime = gtime.New(tm.UpdateTime).String()
// return nil
//}

49
database/model/base.go Normal file
View File

@ -0,0 +1,49 @@
package model
import (
"database/sql/driver"
"fmt"
"time"
)
// LocalTime 自定义时间类型,支持 null 值
type LocalTime struct {
time.Time
}
func (t *LocalTime) MarshalJSON() ([]byte, error) {
if t == nil || t.IsZero() {
return []byte("null"), nil
}
formatted := fmt.Sprintf("\"%s\"", t.Format("2006-01-02 15:04:05"))
return []byte(formatted), nil
}
func (t *LocalTime) Value() (driver.Value, error) {
if t == nil || t.IsZero() {
return nil, nil
}
return t.Time, nil
}
func (t *LocalTime) Scan(v interface{}) error {
if v == nil {
t.Time = time.Time{}
return nil
}
switch value := v.(type) {
case time.Time:
t.Time = value
return nil
default:
return fmt.Errorf("unsupported type: %T", v)
}
}
// BaseModel 基础模型,包含自定义的创建时间和更新时间
type BaseModel struct {
Id int `json:"id" gorm:"column:id;primaryKey;autoIncrement;common:主键ID"` // 主键 ID
CreateTime *LocalTime `json:"create_time" gorm:"column:create_time;type:datetime;autoCreateTime;common:创建时间"`
UpdateTime *LocalTime `json:"update_time" gorm:"column:update_time;type:datetime;autoUpdateTime;common:更新时间"`
}