作者 yangfu

模型备注信息

... ... @@ -23,4 +23,7 @@ _testmain.go
*.test
.log
.idea
*.tmp
\ No newline at end of file
*.tmp
swagger.json
swagger.yaml
\ No newline at end of file
... ...
package constant
import "os"
const SERVICE_NAME = "terms.base"
import (
"fmt"
"os"
)
var SERVICE_NAME = "allied-creation-user"
var SERVICE_ENV = "dev"
var CACHE_PREFIX = "allied-creation-user-dev"
var LOG_LEVEL = "debug"
var EnableCaching = false
... ... @@ -11,4 +15,5 @@ func init() {
if os.Getenv("LOG_LEVEL") != "" {
LOG_LEVEL = os.Getenv("LOG_LEVEL")
}
CACHE_PREFIX = fmt.Sprintf("%v-%v", SERVICE_NAME, SERVICE_ENV)
}
... ...
package constant
import "os"
var (
// kafka 地址
KAFKA_HOST = "192.168.0.250:9092,192.168.0.251:9092,192.168.0.252:9092"
// kafka topic log stash
TOPIC_LOG_STASH = "go_stash_dev"
// 是否启用日志收集 (本地不启用)
ENABLE_KAFKA_LOG = false
)
func init() {
if os.Getenv("KAFKA_HOST") != "" {
POSTGRESQL_HOST = os.Getenv("KAFKA_HOST")
}
if os.Getenv("TOPIC_LOG_STASH") != "" {
POSTGRESQL_PORT = os.Getenv("TOPIC_LOG_STASH")
}
if os.Getenv("ENABLE_KAFKA_LOG") != "" {
DISABLE_CREATE_TABLE = true
}
}
... ...
... ... @@ -9,6 +9,7 @@ var POSTGRESQL_HOST = "127.0.0.1"
var POSTGRESQL_PORT = "5432"
var DISABLE_CREATE_TABLE = false
var DISABLE_SQL_GENERATE_PRINT = false
var DISABLE_SQL_GENERATE_COMMENT = false
func init() {
if os.Getenv("POSTGRESQL_DB_NAME") != "" {
... ...
... ... @@ -2,9 +2,13 @@ package constant
import "os"
var REDIS_HOST = "127.0.0.1"
var REDIS_PORT = "6379"
var REDIS_AUTH = ""
var (
REDIS_HOST = "127.0.0.1"
REDIS_PORT = "6379"
REDIS_AUTH = ""
// 是否启用仓储层缓存
ENABLE_REPOSITORY_CACHE = true
)
func init() {
if os.Getenv("REDIS_HOST") != "" {
... ... @@ -17,4 +21,10 @@ func init() {
if _, ok := os.LookupEnv("REDIS_AUTH"); ok {
REDIS_AUTH = os.Getenv("REDIS_AUTH")
}
if os.Getenv("ENABLE_REPOSITORY_CACHE") != "" {
ENABLE_REPOSITORY_CACHE = true
}
if os.Getenv("DISABLE_REPOSITORY_CACHE") != "" {
ENABLE_REPOSITORY_CACHE = false
}
}
... ...
... ... @@ -17,7 +17,7 @@ type Company struct {
CompanyConfig *CompanyConfig `json:"companyConfig"`
// 企业基本信息
CompanyInfo *CompanyInfo `json:"companyInfo"`
// 公司状态
// 公司状态 1:已注册 2:待认证 3:已认证
Status int `json:"status"`
// 创建时间
CreatedAt time.Time `json:"createdAt"`
... ...
... ... @@ -5,6 +5,7 @@ import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/log"
"reflect"
"github.com/go-pg/pg/v10"
"github.com/go-pg/pg/v10/orm"
... ... @@ -38,6 +39,9 @@ func init() {
if err != nil {
panic(err)
}
if !constant.DISABLE_SQL_GENERATE_COMMENT {
AddComments(DB, model)
}
}
}
}
... ... @@ -56,3 +60,26 @@ func (hook SqlGeneratePrintHook) AfterQuery(c context.Context, q *pg.QueryEvent)
log.Logger.Debug(string(sqlStr))
return nil
}
func AddComments(db *pg.DB, model interface{}) {
tableName := db.Model(model).TableModel().Table().SQLName
columnsMap := make(map[string]*orm.Field)
columns := db.Model(model).TableModel().Table().Fields
for _, item := range columns {
columnsMap[item.GoName] = item
}
valueOf := reflect.TypeOf(model)
for i := 0; i < valueOf.Elem().NumField(); i++ {
field := valueOf.Elem().Field(i)
comment := field.Tag.Get("comment")
if comment != "" {
if field.Name == "tableName" {
_, _ = db.Exec(fmt.Sprintf("COMMENT ON TABLE %s IS '%s';", tableName, comment))
} else {
if columnField, ok := columnsMap[field.Name]; ok {
_, _ = db.Exec(fmt.Sprintf("COMMENT ON COLUMN %s.%s IS '%s';", tableName, columnField.SQLName, comment))
}
}
}
}
}
... ...
... ... @@ -6,17 +6,17 @@ import (
)
type Company struct {
tableName string `pg:"users.company,alias:company"`
tableName string `pg:"users.company,alias:company" comment:"企业"`
// 企业id
CompanyId int64
CompanyId int64 `comment:"企业id"`
// 企业配置信息
CompanyConfig *domain.CompanyConfig
CompanyConfig *domain.CompanyConfig `comment:"企业配置信息"`
// 企业基本信息
CompanyInfo *domain.CompanyInfo
// 公司状态
Status int
CompanyInfo *domain.CompanyInfo `comment:"企业基本信息"`
// 公司状态 1:已注册 2:待认证 3:已认证
Status int `comment:"公司状态 1:已注册 2:待认证 3:已认证"`
// 创建时间
CreatedAt time.Time
CreatedAt time.Time `comment:"创建时间"`
// 更新时间
UpdatedAt time.Time
UpdatedAt time.Time `comment:"更新时间"`
}
... ...
package models
type Menu struct {
tableName string `pg:"users.menu,alias:menu"`
tableName string `pg:"users.menu,alias:menu" comment:"菜单"`
// 菜单编号
MenuId int64 `pg:",pk"`
MenuId int64 `pg:",pk" comment:"菜单编号"`
// 父级id
ParentId int64
ParentId int64 `comment:"父级id"`
// 菜单名称
MenuName string
MenuName string `comment:"菜单名称"`
// 菜单编码 SYSTEM_USER_EDIT / 100101 (字符编码)
Code string
Code string `comment:"菜单编码"`
// 权限编码 users:edit
AccessCode string
AccessCode string `comment:"权限编码"`
// 菜单类型 (目录catalog、菜单menu、按钮button)
MenuType string
MenuType string `comment:"菜单类型 (目录catalog、菜单menu、按钮button)"`
// 菜单图标
Icon string
Icon string `comment:"菜单图标"`
// 排序
Sort int
Sort int `comment:"排序"`
// 菜单说明
Remark string
Remark string `comment:"菜单说明"`
// 菜单类别 (web:1、app:2)
Category string
Category string `comment:"菜单类别"`
// 父级节点路径("0,11,12,")
ParentPath string
ParentPath string `comment:"父级节点路径(0,11,12)"`
// 菜单是否公开状态,[2:隐藏],[1:显示],默认显示
IsPublish int
IsPublish int `comment:"菜单是否公开状态,[2:隐藏],[1:显示],默认显示"`
// 启用状态(启用:1 禁用:2),默认启用
EnableStatus int
EnableStatus int `comment:"启用状态(启用:1 禁用:2),默认启用"`
}
... ...
... ... @@ -6,29 +6,29 @@ import (
)
type Org struct {
tableName string `pg:"users.org,alias:org"`
tableName string `pg:"users.org,alias:org" comment:"组织"`
// 组织ID
OrgId int64
OrgId int64 `comment:"组织ID"`
// 企业id
CompanyId int64
CompanyId int64 `comment:"企业id"`
// 创建时间
CreatedAt time.Time
CreatedAt time.Time `comment:"创建时间"`
// 更新时间
UpdatedAt time.Time
UpdatedAt time.Time `comment:"更新时间"`
// 删除时间
DeletedAt time.Time
DeletedAt time.Time `comment:"删除时间"`
// 组织编码
OrgCode string
OrgCode string `comment:"组织编码"`
// 组织名称
OrgName string
OrgName string `comment:"组织名称"`
// 扩展数据
Ext *domain.Ext
Ext *domain.Ext `comment:"扩展数据"`
// 是否是组织标识 1:是 2:不是
IsOrg int
IsOrg int `comment:"是否是组织标识 1:是 2:不是"`
// 组织状态 1:启用 2:禁用 3.删除
OrgStatus int `json:"orgStatus"`
OrgStatus int `json:"orgStatus" comment:"组织状态 1:启用 2:禁用 3.删除"`
// 父级ID
ParentId int64
ParentId int64 `comment:"父级ID"`
// 父级节点路径("0,11,12,")
ParentPath string
ParentPath string `comment:"父级节点路径"`
}
... ...
... ... @@ -6,25 +6,25 @@ import (
)
type Role struct {
tableName string `pg:"users.role,alias:role"`
tableName string `pg:"users.role,alias:role" comment:"角色"`
// 角色ID
RoleId int64
RoleId int64 `comment:"角色ID"`
// 企业id
CompanyId int64
CompanyId int64 `comment:"企业id"`
// 组织ID
OrgId int64
OrgId int64 `comment:"组织ID"`
// 角色类型 1.普通角色 1024:超级管理员
RoleType int
RoleType int `comment:"角色类型 1.普通角色 1024:超级管理员"`
// 角色名称
RoleName string
RoleName string `comment:"角色名称"`
// 有权限的菜单
AccessMenus []int64 `pg:",array"`
AccessMenus []int64 `pg:",array" comment:"有权限的菜单"`
// 描述
Desc int64
Desc int64 `comment:"描述"`
// 扩展数据
Ext *domain.Ext
Ext *domain.Ext `comment:"扩展数据"`
// 创建时间
CreatedAt time.Time
CreatedAt time.Time `comment:"创建时间"`
// 更新时间
UpdatedAt time.Time
UpdatedAt time.Time `comment:"更新时间"`
}
... ...
... ... @@ -8,35 +8,35 @@ import (
type Users struct {
tableName string `pg:"users.users,alias:users"`
// 用户Id 用户唯一标识
UsersId int64
UsersId int64 `comment:"用户Id"`
// 企业id
CompanyId int64
CompanyId int64 `comment:"企业id"`
// 用户基础数据id
UsersBaseId int64
UsersBaseId int64 `comment:"用户基础数据id"`
// 用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加)
UsersType int
UsersType int `comment:"用户类型 1:企业内部用户(内部添加) 2:共创用户 1024:企业注册用户(注册添加"`
// 用户编号 企业内标识
UsersCode string
UsersCode string `comment:"用户编号"`
// 组织机构
OrganizationId int64
OrganizationId int64 `comment:"组织机构"`
// 所属部门
DepartmentId int64
DepartmentId int64 `comment:"所属部门"`
// 用户信息 (冗余,数据存在usersBase里面)
//UsersInfo *domain.UsersInfo
// 用户关联的组织
UsersOrg []*domain.Org `pg:",array"`
UsersOrg []*domain.Org `pg:",array" comment:"用户关联的组织"`
// 用户关联的角色
UsersRole []*domain.Role `pg:",array"`
UsersRole []*domain.Role `pg:",array" comment:"用户关联的角色"`
// 收藏的菜单(工作台)(菜单编码列表)
FavoriteMenus []string `pg:",array"`
FavoriteMenus []string `pg:",array" comment:"收藏的菜单"`
// 共创信息 (共创用户有效)
CooperationInfo *domain.CooperationInfo
CooperationInfo *domain.CooperationInfo `comment:"共创信息 (共创用户有效)"`
// 状态(1:启用 2:禁用 3:注销)
EnableStatus int
EnableStatus int `comment:" 状态(1:启用 2:禁用 3:注销)"`
// 扩展数据
Ext *domain.Ext
Ext *domain.Ext `comment:"扩展数据"`
// 创建时间
CreatedAt time.Time
CreatedAt time.Time `comment:"创建时间"`
// 更新时间
UpdatedAt time.Time
UpdatedAt time.Time `comment:"更新时间"`
}
... ...
... ... @@ -6,23 +6,23 @@ import (
)
type UsersBase struct {
tableName string `pg:"users.users_base,alias:users_base"`
tableName string `pg:"users.users_base,alias:users_base" comment:"用户基础"`
// 用户基础数据id
UsersBaseId int64
UsersBaseId int64 `comment:"用户基础数据id"`
// 用户信息
UsersInfo *domain.UsersInfo
UsersInfo *domain.UsersInfo `comment:"用户信息"`
// 手机号码
Phone string
Phone string `comment:"手机号码"`
// 密码
Password string
Password string `comment:"密码"`
// IM信息
Im *domain.Im
Im *domain.Im `comment:"IM信息"`
// 关联的用户 (冗余)
RelatedUsers []int64 `pg:",array"`
RelatedUsers []int64 `pg:",array" comment:"关联的用户 (冗余)"`
// 账号状态 1:正常 2.禁用 3:注销
Status int
Status int `comment:"账号状态 1:正常 2.禁用 3:注销"`
// 创建时间
CreatedAt time.Time
CreatedAt time.Time `comment:"创建时间"`
// 更新时间
UpdatedAt time.Time
UpdatedAt time.Time `comment:"更新时间"`
}
... ...
... ... @@ -12,4 +12,9 @@ func init() {
Logger = logrus.NewLogrusLogger()
Logger.SetServiceName(constant.SERVICE_NAME)
Logger.SetLevel(constant.LOG_LEVEL)
if constant.ENABLE_KAFKA_LOG {
w, _ := logrus.NewKafkaWriter(constant.KAFKA_HOST, constant.TOPIC_LOG_STASH, false)
Logger.AddHook(w)
}
}
... ...