Compare commits

..

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

4 changed files with 29 additions and 35 deletions

View File

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

View File

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

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

@ -0,0 +1,25 @@
package model
import (
"time"
"gorm.io/gorm"
)
// BaseModel 基础模型
type BaseModel struct {
Id uint `gorm:"column:id;primaryKey;autoIncrement;common:主键ID" json:"id"`
CreateTime string `gorm:"autoCreateTime;column:create_time;type:datetime" json:"create_time"`
UpdateTime string `gorm:"autoUpdateTime;column:update_time;type:datetime" json:"update_time"`
}
func (b BaseModel) BeforeCreate(tx *gorm.DB) error {
b.CreateTime = time.Now().Format("2006-01-02 15:04:05")
b.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
return nil
}
func (b BaseModel) BeforeUpdate(tx *gorm.DB) error {
b.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
return nil
}