feat 结构调整
This commit is contained in:
16
Public/Tools/mysql_model/gen.ps1
Normal file
16
Public/Tools/mysql_model/gen.ps1
Normal file
@@ -0,0 +1,16 @@
|
||||
# gen.ps1 - 极简版
|
||||
& .\gentool.exe `
|
||||
-dsn "root:gR9pV4tY7zR6qL3e@tcp(47.108.184.184:3306)/user_db?charset=utf8mb4&parseTime=True&loc=Local" `
|
||||
-fieldSignable `
|
||||
-outPath "./user_db/query"
|
||||
|
||||
Get-ChildItem ./user_db/model/*.gen.go | ForEach-Object {
|
||||
$c = Get-Content $_.FullName -Raw -Encoding UTF8
|
||||
if ($c -match '\bSn\s+int64\b' -and $c -notmatch 'BeforeCreate') {
|
||||
$c -match 'type\s+(\w+)\s+struct' | Out-Null
|
||||
$n = $matches[1]
|
||||
$c = $c -replace '(?s)(import\s*\([^)]*)', "`$1`t`"common/utils`"`n"
|
||||
$hook = "`n`n// auto sn`nfunc (m *$n) BeforeCreate(_ *gorm.DB) error {`n`tif m.Sn == 0 {`n`t`tm.Sn = utils.SnowflakeInstance().Generate().Int64()`n`t}`n`treturn nil`n}"
|
||||
Set-Content $_.FullName ($c.TrimEnd() + $hook) -Encoding UTF8
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
@echo off
|
||||
|
||||
gentool ^
|
||||
-dsn "root:gR9pV4tY7zR6qL3e@tcp(47.108.184.184:3306)/point?charset=utf8mb4&parseTime=True&loc=Local" ^
|
||||
-fieldSignable
|
||||
@@ -1,4 +1,4 @@
|
||||
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||
|
||||
@@ -8,15 +8,17 @@ import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"common/utils"
|
||||
)
|
||||
|
||||
const TableNameUser = "users"
|
||||
|
||||
// User mapped from table <users>
|
||||
type User struct {
|
||||
ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
|
||||
Sn string `gorm:"column:sn;not null;comment:业务唯一编号" json:"sn"` // 业务唯一编号
|
||||
ID uint64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"`
|
||||
Sn int64 `gorm:"column:sn;not null;comment:业务唯一编号" json:"sn"` // 业务唯一编号
|
||||
Name string `gorm:"column:name;not null" json:"name"`
|
||||
Phone string `gorm:"column:phone;not null" json:"phone"`
|
||||
CreatedAt time.Time `gorm:"column:created_at;not null" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at;not null" json:"updated_at"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"`
|
||||
@@ -26,3 +28,11 @@ type User struct {
|
||||
func (*User) TableName() string {
|
||||
return TableNameUser
|
||||
}
|
||||
|
||||
// auto sn
|
||||
func (m *User) BeforeCreate(_ *gorm.DB) error {
|
||||
if m.Sn == 0 {
|
||||
m.Sn = utils.SnowflakeInstance().Generate().Int64()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -25,9 +25,10 @@ func newUser(db *gorm.DB, opts ...gen.DOOption) user {
|
||||
|
||||
tableName := _user.userDo.TableName()
|
||||
_user.ALL = field.NewAsterisk(tableName)
|
||||
_user.ID = field.NewInt64(tableName, "id")
|
||||
_user.Sn = field.NewString(tableName, "sn")
|
||||
_user.ID = field.NewUint64(tableName, "id")
|
||||
_user.Sn = field.NewInt64(tableName, "sn")
|
||||
_user.Name = field.NewString(tableName, "name")
|
||||
_user.Phone = field.NewString(tableName, "phone")
|
||||
_user.CreatedAt = field.NewTime(tableName, "created_at")
|
||||
_user.UpdatedAt = field.NewTime(tableName, "updated_at")
|
||||
_user.DeletedAt = field.NewField(tableName, "deleted_at")
|
||||
@@ -41,9 +42,10 @@ type user struct {
|
||||
userDo userDo
|
||||
|
||||
ALL field.Asterisk
|
||||
ID field.Int64
|
||||
Sn field.String // 业务唯一编号
|
||||
ID field.Uint64
|
||||
Sn field.Int64 // 业务唯一编号
|
||||
Name field.String
|
||||
Phone field.String
|
||||
CreatedAt field.Time
|
||||
UpdatedAt field.Time
|
||||
DeletedAt field.Field
|
||||
@@ -63,9 +65,10 @@ func (u user) As(alias string) *user {
|
||||
|
||||
func (u *user) updateTableName(table string) *user {
|
||||
u.ALL = field.NewAsterisk(table)
|
||||
u.ID = field.NewInt64(table, "id")
|
||||
u.Sn = field.NewString(table, "sn")
|
||||
u.ID = field.NewUint64(table, "id")
|
||||
u.Sn = field.NewInt64(table, "sn")
|
||||
u.Name = field.NewString(table, "name")
|
||||
u.Phone = field.NewString(table, "phone")
|
||||
u.CreatedAt = field.NewTime(table, "created_at")
|
||||
u.UpdatedAt = field.NewTime(table, "updated_at")
|
||||
u.DeletedAt = field.NewField(table, "deleted_at")
|
||||
@@ -93,10 +96,11 @@ func (u *user) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||
}
|
||||
|
||||
func (u *user) fillFieldMap() {
|
||||
u.fieldMap = make(map[string]field.Expr, 6)
|
||||
u.fieldMap = make(map[string]field.Expr, 7)
|
||||
u.fieldMap["id"] = u.ID
|
||||
u.fieldMap["sn"] = u.Sn
|
||||
u.fieldMap["name"] = u.Name
|
||||
u.fieldMap["phone"] = u.Phone
|
||||
u.fieldMap["created_at"] = u.CreatedAt
|
||||
u.fieldMap["updated_at"] = u.UpdatedAt
|
||||
u.fieldMap["deleted_at"] = u.DeletedAt
|
||||
@@ -10,4 +10,7 @@ go mod tidy
|
||||
cd ../scene
|
||||
go mod tidy
|
||||
|
||||
cd ../user
|
||||
go mod tidy
|
||||
|
||||
Pause
|
||||
Reference in New Issue
Block a user