init: initial commit
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
package global
|
||||
|
||||
import (
|
||||
"sundynix-go/config"
|
||||
"sundynix-go/utils/timer"
|
||||
|
||||
"github.com/bsm/redislock"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/spf13/viper"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/sync/singleflight"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 全局变量 加载在内存中
|
||||
var (
|
||||
Viper *viper.Viper
|
||||
Logger *zap.Logger
|
||||
Config *config.Config
|
||||
DB *gorm.DB
|
||||
Redis redis.UniversalClient
|
||||
Locker *redislock.Client // 分布式锁
|
||||
ConcurrencyControl = &singleflight.Group{}
|
||||
Timer timer.Timer = timer.NewTimerTask()
|
||||
WxPayClient *core.Client
|
||||
)
|
||||
@@ -0,0 +1,35 @@
|
||||
package global
|
||||
|
||||
import (
|
||||
"sundynix-go/utils/uniqueid"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type BaseModel struct {
|
||||
Id string `gorm:"size:50;primaryKey" json:"id"` // 主键ID
|
||||
CreatedAt time.Time `json:"createdAt" gorm:"autoCreateTime"`
|
||||
UpdatedAt time.Time `json:"updatedAt" gorm:"autoCreateTime;autoUpdateTime"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"` // 删除时间
|
||||
CreatedAtStr string `json:"createdAtStr" gorm:"-"`
|
||||
}
|
||||
|
||||
// BeforeCreate 定义一个钩子,在创建之前执行自动插入字段
|
||||
func (model *BaseModel) BeforeCreate(db *gorm.DB) (err error) {
|
||||
//生成主键的string uniqueid
|
||||
db.Statement.SetColumn("id", uniqueid.GenerateId())
|
||||
return
|
||||
}
|
||||
|
||||
// BeforeUpdate 定义一个钩子,在更新之前执行自动更新字段
|
||||
func (model *BaseModel) BeforeUpdate(db *gorm.DB) (err error) {
|
||||
db.Statement.SetColumn("updated_at", time.Now())
|
||||
return
|
||||
}
|
||||
|
||||
// AfterFind 钩子,在查询之后执行
|
||||
func (model *BaseModel) AfterFind(tx *gorm.DB) (err error) {
|
||||
model.CreatedAtStr = model.CreatedAt.Format("2006-01-02 15:04:05")
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user