Compare commits
9 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
f50930ec74 | |
|
|
d50f150d98 | |
|
|
0238ec9a01 | |
|
|
1cc976ed72 | |
|
|
7d3ddc62e3 | |
|
|
427f568db6 | |
|
|
828e19de93 | |
|
|
1e18be8326 | |
|
|
21e9f7c79d |
|
|
@ -48,7 +48,7 @@ var pageInfo = []string{
|
||||||
}
|
}
|
||||||
|
|
||||||
// Crud -------------------------- 泛型CURD核心结构体 --------------------------
|
// Crud -------------------------- 泛型CURD核心结构体 --------------------------
|
||||||
// Crud GORM 版本的泛型CURD封装,R为对应的模型结构体
|
// Crud GORM 版本的泛型CURD封装,R为对应的模型结构体, C为对应模型结构体的字段结构体
|
||||||
type Crud[R any, C any] struct {
|
type Crud[R any, C any] struct {
|
||||||
Dao IDao[C]
|
Dao IDao[C]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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{}]
|
var crud Crud[interface{}, 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{}]
|
var crud Crud[interface{}, 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{}]
|
var crud Crud[interface{}, interface{}]
|
||||||
|
|
||||||
t.Run("SimpleAND", func(t *testing.T) {
|
t.Run("SimpleAND", func(t *testing.T) {
|
||||||
where := crud.BuildWhereAndOr().
|
where := crud.BuildWhereAndOr().
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,31 @@
|
||||||
|
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
|
||||||
|
//}
|
||||||
|
|
@ -1,49 +0,0 @@
|
||||||
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:更新时间"`
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue