fix 主键雪花ID支持

This commit is contained in:
yxh 2023-09-26 16:23:44 +08:00
parent 23bc361bb8
commit 7b8de1b5ea
19 changed files with 143 additions and 43 deletions

View File

@ -101,6 +101,7 @@ type ToolsGenTableColumnsEditReq struct {
TreeParentCode string `p:"tree_parent_code"`
TreeName string `p:"tree_name"`
ExcelPort string `p:"excelPort"`
UseSnowId string `p:"useSnowId"`
}
type ToolsGenTableColumnsEditRes struct {

1
go.mod
View File

@ -64,6 +64,7 @@ require (
github.com/rivo/uniseg v0.4.4 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/sirupsen/logrus v1.4.0 // indirect
github.com/sony/sonyflake v1.2.0 // indirect
github.com/stathat/consistent v1.0.0 // indirect
github.com/tidwall/gjson v1.13.0 // indirect
github.com/tidwall/match v1.1.1 // indirect

2
go.sum
View File

@ -204,6 +204,8 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykE
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/sony/sonyflake v1.2.0 h1:Pfr3A+ejSg+0SPqpoAmQgEtNDAhc2G1SUYk205qVMLQ=
github.com/sony/sonyflake v1.2.0/go.mod h1:LORtCywH/cq10ZbyfhKrHYgAUGH7mOBa76enV9txy/Y=
github.com/stathat/consistent v1.0.0 h1:ZFJ1QTRn8npNBKW065raSZ8xfOqhpb8vLOkfp4CcL/U=
github.com/stathat/consistent v1.0.0/go.mod h1:uajTPbgSygZBJ+V+0mY7meZ8i0XAcZs7AQ6V121XSxw=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=

View File

@ -9,6 +9,7 @@ import (
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/cache"
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/captcha"
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/middleware"
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/snowIDGen"
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/sysConfig"
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/sysDictData"
_ "github.com/tiger1103/gfast/v3/internal/app/common/logic/sysDictType"

View File

@ -0,0 +1,41 @@
/*
* @desc:雪花ID生成
* @company:云南奇讯科技有限公司
* @Author: yixiaohu<yxh669@qq.com>
* @Date: 2023/6/2 14:52
*/
package snowIDGen
import (
"github.com/gogf/gf/v2/os/gtime"
"github.com/sony/sonyflake"
"github.com/tiger1103/gfast/v3/internal/app/common/service"
)
var machineID uint16 = 1
func init() {
service.RegisterSnowID(New())
}
func New() service.ISnowID {
return &sSnowID{
sonyflake.NewSonyflake(sonyflake.Settings{
StartTime: gtime.NewFromStr("2010-05-01").Time,
MachineID: GetMachineId,
}),
}
}
type sSnowID struct {
*sonyflake.Sonyflake
}
func (s *sSnowID) GenID() (uint64, error) {
return s.NextID()
}
func GetMachineId() (uint16, error) {
return machineID, nil
}

View File

@ -0,0 +1,27 @@
// ================================================================================
// Code generated by GoFrame CLI tool. DO NOT EDIT.
// You can delete these comments if you wish manually maintain this interface file.
// ================================================================================
package service
type (
ISnowID interface {
GenID() (uint64, error)
}
)
var (
localSnowID ISnowID
)
func SnowID() ISnowID {
if localSnowID == nil {
panic("implement not found for interface ISnowID, forgot register?")
}
return localSnowID
}
func RegisterSnowID(i ISnowID) {
localSnowID = i
}

View File

@ -38,6 +38,8 @@ type ToolsGenTableColumns struct {
SortColumn string // 排序字段名
SortType string // 排序方式 (asc顺序 desc倒序)
ShowDetail string // 是否有查看详情功能
ExcelPort string // 是否有导入导出excel功能
UseSnowId string //主键是否使用雪花ID
}
// toolsGenTableColumns holds the columns for table tools_gen_table.
@ -60,6 +62,8 @@ var toolsGenTableColumns = ToolsGenTableColumns{
SortColumn: "sort_column",
SortType: "sort_type",
ShowDetail: "show_detail",
ExcelPort: "excel_port",
UseSnowId: "use_snow_id",
}
// NewToolsGenTableDao creates and returns a new DAO object for table data access.

View File

@ -97,7 +97,7 @@ func (s *sSysUserOnline) CheckUserOnline(ctx context.Context) {
},
},
}
var total int
for {
var (
res *system.SysUserOnlineSearchRes
@ -113,11 +113,11 @@ func (s *sSysUserOnline) CheckUserOnline(ctx context.Context) {
}
for _, v := range res.List {
if b := s.UserIsOnline(ctx, v.Token); !b {
s.DeleteOnlineByToken(ctx, v.Token)
err = s.DeleteOnlineByToken(ctx, v.Token)
if err != nil {
g.Log().Error(ctx, err)
}
}
if param.PageNum*param.PageSize >= total {
break
}
param.PageNum++
}

View File

@ -368,6 +368,9 @@ func (s *sToolsGenTable) SaveEdit(ctx context.Context, req *system.ToolsGenTable
if req.ExcelPort != "" {
table.ExcelPort = gconv.Bool(req.ExcelPort)
}
if req.UseSnowId != "" {
table.UseSnowId = gconv.Bool(req.UseSnowId)
}
if req.TplCategory != "" {
table.TplCategory = req.TplCategory
}
@ -758,7 +761,7 @@ func (s *sToolsGenTable) SelectRecordById(ctx context.Context, tableId int64) (t
err = gconv.Struct(m1, columnEx)
columnMap[columnName] = columnEx
allColumnExs[i] = columnEx
tableEx.IsPkInsertable = tableEx.IsPkInsertable || column.IsPk && !column.IsIncrement
tableEx.IsPkInsertable = tableEx.IsPkInsertable || column.IsPk && !column.IsIncrement && !tableEx.UseSnowId
tableEx.IsPkListable = tableEx.IsPkListable || column.IsPk && column.IsList
if column.IsEdit && !service.ToolsGenTableColumn().IsNotEdit(columnName) && !column.IsPk {
editColumns = append(editColumns, columnEx)
@ -1319,11 +1322,11 @@ func (s *sToolsGenTable) SyncTable(ctx context.Context, tableId int64) (err erro
}
// goFmt formats the source file and adds or removes import statements as necessary.
func (s *sToolsGenTable) goFmt(path string)(err error) {
func (s *sToolsGenTable) goFmt(path string) (err error) {
replaceFunc := func(path, content string) string {
res, err := imports.Process(path, []byte(content), nil)
if err != nil {
g.Log().Printf(context.Background(),`error format "%s" go files: %v`, path, err)
g.Log().Printf(context.Background(), `error format "%s" go files: %v`, path, err)
return content
}
return string(res)

View File

@ -31,4 +31,5 @@ type ToolsGenTable struct {
SortType interface{} // 排序方式 (asc顺序 desc倒序)
ShowDetail interface{} // 是否有查看详情功能
ExcelPort interface{} // 是否有导入导出excel功能
UseSnowId interface{} //主键是否使用雪花ID
}

View File

@ -29,4 +29,5 @@ type ToolsGenTable struct {
SortType string `json:"sortType" description:"排序方式 (asc顺序 desc倒序)"`
ShowDetail bool `json:"showDetail" description:"是否有查看详情功能"`
ExcelPort bool `json:"excelPort" description:"是否有excel导入导出功能"`
UseSnowId bool `json:"useSnowId" description:"主键是否使用雪花ID"`
}

View File

@ -36,6 +36,7 @@ type ToolsGenTableEx struct {
SortType string // 缺省排序方式 (asc顺序 desc倒序)
ShowDetail bool // 是否有查看详情功能
ExcelPort bool // 是否有导入导出excel功能
UseSnowId bool // 主键是否使用雪花ID
TreeCode string // 树编码字段
TreeParentCode string // 树父编码字段
TreeName string // 树名称字段

View File

@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"github.com/gogf/gf/v2/encoding/gbase64"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/net/goai"
@ -23,7 +24,7 @@ var (
Brief: "start http server",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
g.Log().SetFlags(glog.F_ASYNC | glog.F_TIME_DATE | glog.F_TIME_TIME | glog.F_FILE_LONG)
g.Log().Info(ctx, "GFast version:", consts.Version)
g.Log().Info(ctx, gbase64.MustDecodeString(consts.Logo), "Version:", consts.Version)
s := g.Server()
s.Group("/", func(group *ghttp.RouterGroup) {
router.R.BindController(ctx, group)

View File

@ -8,5 +8,6 @@
package consts
const (
Version = "3.2.8"
Logo = `CiAgIF9fX19fX19fX19fXyAgICAgICAgICAgX18gCiAgLyBfX19fLyBfX19fL19fXyBfX19fX18vIC9fCiAvIC8gX18vIC9fICAvIF9fIGAvIF9fXy8gX18vCi8gL18vIC8gX18vIC8gL18vIChfXyAgKSAvXyAgClxfX19fL18vICAgIFxfXyxfL19fX18vXF9fLyAg`
Version = "3.2.9"
)

View File

@ -28,6 +28,7 @@ type OSS struct {
EndPoint string `json:"endPoint"`
BucketName string `json:"bucketName"`
IsHttps bool `json:"isHttps"`
Path string `json:"path"`
}
func (s *OSS) Upload(ctx context.Context, file *ghttp.UploadFile) (result system.UploadResponse, err error) {
@ -37,25 +38,25 @@ func (s *OSS) Upload(ctx context.Context, file *ghttp.UploadFile) (result system
bucket *oss.Bucket
fp multipart.File
)
err = g.Cfg().MustGet(ctx,"upload.oss").Scan(&s)
liberr.ErrIsNil(ctx,err)
client,err = s.getClient()
liberr.ErrIsNil(ctx,err)
err = g.Cfg().MustGet(ctx, "upload.oss").Scan(&s)
liberr.ErrIsNil(ctx, err)
client, err = s.getClient()
liberr.ErrIsNil(ctx, err)
// 获取存储空间。
bucket, err = client.Bucket(s.BucketName)
liberr.ErrIsNil(ctx,err)
liberr.ErrIsNil(ctx, err)
name := gfile.Basename(file.Filename)
name = strings.ToLower(strconv.FormatInt(gtime.TimestampNano(), 36) + grand.S(6))
name = name + gfile.Ext(file.Filename)
fp,err = file.Open()
liberr.ErrIsNil(ctx,err)
err = bucket.PutObject(name,fp)
liberr.ErrIsNil(ctx,err)
name = s.Path + "/" + name + gfile.Ext(file.Filename)
fp, err = file.Open()
liberr.ErrIsNil(ctx, err)
err = bucket.PutObject(name, fp)
liberr.ErrIsNil(ctx, err)
schema := "http"
if s.IsHttps {
schema = "https"
}
url :=schema+"://"+s.EndPoint+"/"+name
url := schema + "://" + s.EndPoint + "/" + name
result = system.UploadResponse{
Size: file.Size,
Path: url,
@ -67,12 +68,11 @@ func (s *OSS) Upload(ctx context.Context, file *ghttp.UploadFile) (result system
return
}
func (s *OSS) getClient() (client *oss.Client, err error) {
// 设置连接数为10每个主机的最大闲置连接数为20每个主机的最大连接数为20。
conn := oss.MaxConns(10,20,20)
conn := oss.MaxConns(10, 20, 20)
// 设置HTTP连接超时时间为20秒HTTP读取或写入超时时间为60秒。
time := oss.Timeout(20,60)
time := oss.Timeout(20, 60)
// 设置是否支持将自定义域名作为Endpoint默认不支持。
cname := oss.UseCname(true)
// 设置HTTP的User-Agent头默认为aliyun-sdk-go。
@ -88,6 +88,6 @@ func (s *OSS) getClient() (client *oss.Client, err error) {
client, err = oss.New(s.EndPoint,
s.AccessKeyID,
s.AccessKeySecret,
conn,time,cname,userAgent,verifySsl,redirect,crc,logLevel)
conn, time, cname, userAgent, verifySsl, redirect, crc, logLevel)
return
}

View File

@ -107,6 +107,7 @@ upload:
endPoint: "xxx.xxxx.com" #自定域名
bucketName: "xxx" #使用的存储桶
isHttps: true #是否开启https访问
path:"gfast" #上传文件保存路径
# 代码生成配置
gen:

View File

@ -17,6 +17,7 @@ package logic
{{$usedSystemModule:=false}}
{{$gstr:=false}}
{{$hasLinkTable:=false}}
{{$useCommonService:=false}}
{{range $index, $column := .table.Columns}}
{{if eq $column.HtmlType "images" "file" "files"}}
{{$libUtils = true}}
@ -33,7 +34,9 @@ package logic
{{end}}
{{end}}
{{end}}
{{if and .table.UseSnowId (not .table.IsPkInsertable)}}
{{$useCommonService = true}}
{{end}}
import (
"context"
"github.com/gogf/gf/v2/frame/g"
@ -43,6 +46,9 @@ import (
{{if or .table.HasConversion (eq .table.TplCategory "tree")}}
"github.com/gogf/gf/v2/util/gconv"
{{end}}
{{if $useCommonService}}
commonService "github.com/tiger1103/gfast/v3/internal/app/common/service"
{{end}}
"{{.goModName}}/{{.table.PackageName}}/dao"
"{{.goModName}}/{{.table.PackageName}}/model"
"{{.goModName}}/{{.table.PackageName}}/model/do"
@ -358,6 +364,11 @@ func (s *s{{.table.ClassName}})GetBy{{$pkGoField}}(ctx context.Context, {{$.tabl
////
func (s *s{{.table.ClassName}})Add(ctx context.Context, req *model.{{.table.ClassName}}AddReq) (err error){
err = g.Try(ctx, func(ctx context.Context) {
{{if and .table.UseSnowId (not .table.IsPkInsertable)}}
var {{.table.PkColumn.HtmlField}} uint64
{{.table.PkColumn.HtmlField}}, err = commonService.SnowID().GenID()
liberr.ErrIsNil(ctx, err)
{{end}}
{{range $index, $column := .table.EditColumns}}
{{if eq $column.HtmlType "checkbox" "selects" "treeSelects"}}
{{$column.HtmlField}} := ""
@ -375,6 +386,8 @@ func (s *s{{.table.ClassName}})Add(ctx context.Context, req *model.{{.table.Clas
_, err = dao.{{.table.ClassName}}.Ctx(ctx).Insert(do.{{.table.ClassName}}{
{{if .table.IsPkInsertable}}
{{.table.PkColumn.GoField}}:req.{{.table.PkColumn.GoField}},
{{else if .table.UseSnowId}}
{{.table.PkColumn.GoField}}:{{.table.PkColumn.HtmlField}},
{{end}}
{{range $index, $column := .table.EditColumns}}
{{if eq $column.HtmlType "checkbox" "selects" "treeSelects"}}

View File

@ -54,7 +54,7 @@ import (
type {{.table.ClassName}}InfoRes struct {
gmeta.Meta `orm:"table:{{.table.TableName}}"`
{{range $index, $column := .table.Columns}}
{{if $column.IsPk}}{{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `orm:"{{$column.ColumnName}},primary" json:"{{$column.HtmlField}}" dc:"{{$column.ColumnComment}}"` // {{$column.ColumnComment}} {{else}}{{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `orm:"{{$column.ColumnName}}" json:"{{$column.HtmlField}}" dc:"{{$column.ColumnComment}}"` // {{$column.ColumnComment}} {{end}}
{{if $column.IsPk}}{{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `orm:"{{$column.ColumnName}},primary" json:"{{$column.HtmlField}}{{if $.table.UseSnowId}},string{{end}}" dc:"{{$column.ColumnComment}}"` // {{$column.ColumnComment}} {{else}}{{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `orm:"{{$column.ColumnName}}" json:"{{$column.HtmlField}}" dc:"{{$column.ColumnComment}}"` // {{$column.ColumnComment}} {{end}}
{{range $ti, $linkedTable := $.table.LinkedTables}}
{{if eq $column.LinkTableClass $linkedTable.ClassName}}
Linked{{$column.GoField}} {{if eq $column.HtmlType "selects" "checkbox" "treeSelects"}}[]{{end}}*Linked{{$.table.ClassName}}{{$linkedTable.ClassName}} `{{if not (eq $column.HtmlType "selects" "checkbox" "treeSelects")}}orm:"with:{{$column.LinkLabelId}}={{$column.ColumnName}}" {{end}}json:"linked{{$column.GoField}}"`
@ -69,7 +69,6 @@ type {{.table.ClassName}}InfoRes struct {
{{end}}
}
////
{{range $ti, $linkedTable := .table.LinkedTables}}
type Linked{{$.table.ClassName}}{{$linkedTable.ClassName}} struct {
gmeta.Meta `orm:"table:{{$linkedTable.TableName}}"`
@ -88,7 +87,7 @@ type {{.table.ClassName}}ListRes struct{
{{if eq .table.TplCategory "tree"}}
{{range $index, $column := .table.Columns}}
{{if or (eq $column.HtmlField $.table.TreeCode) (eq $column.HtmlField $.table.TreeParentCode) (eq $column.HtmlField $.table.TreeName) }}
{{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `json:"{{$column.HtmlField}}" dc:"{{$column.ColumnComment}}"`
{{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `json:"{{$column.HtmlField}}{{if and $column.IsPk $.table.UseSnowId}},string{{end}}" dc:"{{$column.ColumnComment}}"`
{{end}}
{{end}}
{{range $index, $column := .table.Columns}}
@ -109,7 +108,7 @@ type {{.table.ClassName}}ListRes struct{
{{end}}
{{else}}
{{if not .table.IsPkListable }}
{{.table.PkColumn.GoField}} {{.table.PkColumn.GoType}} `json:"{{.table.PkColumn.HtmlField}}" dc:"{{.table.PkColumn.ColumnComment}}"`
{{.table.PkColumn.GoField}} {{.table.PkColumn.GoType}} `json:"{{.table.PkColumn.HtmlField}}{{if $.table.UseSnowId}},string{{end}}" dc:"{{.table.PkColumn.ColumnComment}}"`
{{end}}
{{range $index, $column := .table.ListColumns}}
{{if eq $column.HtmlField "createdBy"}}
@ -118,7 +117,7 @@ type {{.table.ClassName}}ListRes struct{
{{if eq $column.HtmlField "updatedBy"}}
UpdatedUser *{{if $hasUser}}systemModel.{{end}}LinkUserRes `orm:"with:id=updated_by" json:"updatedBy"`
{{end}}
{{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `json:"{{$column.HtmlField}}" dc:"{{$column.ColumnComment}}"`
{{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `json:"{{$column.HtmlField}}{{if and $column.IsPk $.table.UseSnowId}},string{{end}}" dc:"{{$column.ColumnComment}}"`
{{range $ti, $linkedTable := $.table.LinkedTables}}
{{if eq $column.LinkTableClass $linkedTable.ClassName}}
Linked{{$column.GoField}} {{if eq $column.HtmlType "selects" "checkbox" "treeSelects"}}[]{{end}}*Linked{{$.table.ClassName}}{{$linkedTable.ClassName}} `{{if not (eq $column.HtmlType "selects" "checkbox" "treeSelects")}}orm:"with:{{$column.LinkLabelId}}={{$column.ColumnName}}" {{end}}json:"linked{{$column.GoField}}" dc:"{{$column.ColumnComment}}"`
@ -128,7 +127,7 @@ type {{.table.ClassName}}ListRes struct{
{{end}}
}
////
// {{.table.ClassName}}SearchReq 分页请求参数
type {{.table.ClassName}}SearchReq struct {
{{if ne $.table.ModuleName "common"}}
@ -139,14 +138,16 @@ type {{.table.ClassName}}SearchReq struct {
{{end}}
}
////
// {{.table.ClassName}}SearchRes 列表返回结果
type {{.table.ClassName}}SearchRes struct {
comModel.ListRes
List []*{{.table.ClassName}}ListRes `json:"list"`
}
{{if gt (len .table.LinkedTables) 0}}
////
//相关连表查询数据
type Linked{{$.table.ClassName}}DataSearchRes struct{
{{range $ti, $linkedTable := .table.LinkedTables}}
@ -155,7 +156,7 @@ type Linked{{$.table.ClassName}}DataSearchRes struct{
}
{{end}}
////
// {{.table.ClassName}}AddReq 添加操作请求参数
type {{.table.ClassName}}AddReq struct {
{{if .table.IsPkInsertable}}
@ -169,7 +170,7 @@ type {{.table.ClassName}}AddReq struct {
{{end}}
}
////
// {{.table.ClassName}}EditReq 修改操作请求参数
type {{.table.ClassName}}EditReq struct {
{{.table.PkColumn.GoField}} {{.table.PkColumn.GoType}} `p:"{{.table.PkColumn.HtmlField}}" v:"required#主键ID不能为空" dc:"{{.table.PkColumn.ColumnComment}}"`

View File

@ -23,7 +23,7 @@ import (
type {{.table.ClassName}} struct {
gmeta.Meta `orm:"table:{{.table.TableName}}"`
{{range $index, $column := .table.Columns}}
{{if $column.IsPk}}{{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `orm:"{{$column.ColumnName}},primary" json:"{{$column.HtmlField}}"` // {{$column.ColumnComment}} {{else}}{{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `orm:"{{$column.ColumnName}}" json:"{{$column.HtmlField}}"` // {{$column.ColumnComment}} {{end}}
{{if $column.IsPk}}{{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `orm:"{{$column.ColumnName}},primary" json:"{{$column.HtmlField}}{{if $.table.UseSnowId}},string{{end}}"` // {{$column.ColumnComment}} {{else}}{{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `orm:"{{$column.ColumnName}}" json:"{{$column.HtmlField}}"` // {{$column.ColumnComment}} {{end}}
{{if ne $column.LinkTableName ""}}
{{range $ti, $linkedTable := $.table.LinkedTables}}
{{if eq $column.LinkTableClass $linkedTable.ClassName}}