feat:完善项目
This commit is contained in:
84
app/common/init.go
Normal file
84
app/common/init.go
Normal file
@ -0,0 +1,84 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"SciencesServer/app/common/model"
|
||||
"SciencesServer/config"
|
||||
"SciencesServer/serve/orm"
|
||||
)
|
||||
|
||||
type synchronized struct {
|
||||
iModel model.IModel
|
||||
iValues func() interface{}
|
||||
Catch func() interface{}
|
||||
}
|
||||
|
||||
type caches struct {
|
||||
iModel model.IModel
|
||||
iValues func() interface{}
|
||||
toCache func(values interface{})
|
||||
}
|
||||
|
||||
func initModel() {
|
||||
db := orm.GetDB()
|
||||
|
||||
function := func(synchronized ...*synchronized) {
|
||||
for _, v := range synchronized {
|
||||
if !db.Migrator().HasTable(v.iModel) {
|
||||
_ = db.Migrator().CreateTable(v.iModel)
|
||||
|
||||
if v.iValues != nil && v.iValues() != nil {
|
||||
db.Table(v.iModel.TableName()).Create(v.iValues())
|
||||
}
|
||||
} else if v.Catch != nil && v.Catch() != nil {
|
||||
v.Catch()
|
||||
}
|
||||
}
|
||||
}
|
||||
function(
|
||||
&synchronized{iModel: model.NewSysTenant()}, &synchronized{iModel: model.NewSysTenantMenu()},
|
||||
&synchronized{iModel: model.NewSysConfig()},
|
||||
&synchronized{iModel: model.NewSysMenu()}, &synchronized{iModel: model.NewSysAuth()},
|
||||
&synchronized{iModel: model.NewSysUser(), iValues: func() interface{} {
|
||||
return &model.SysUser{Account: "admin", Name: "商挈智能", Mobile: "13888888888", Password: "123456",
|
||||
IsAdmin: model.SysUserAdministratorForAdmin, Remark: "超级管理员"}
|
||||
}},
|
||||
&synchronized{iModel: model.NewSysUserTenant()},
|
||||
&synchronized{iModel: model.NewSysDepartment()},
|
||||
&synchronized{iModel: model.NewSysRole()}, &synchronized{iModel: model.NewSysRoleMenu()}, &synchronized{iModel: model.NewSysRoleAuth()},
|
||||
&synchronized{iModel: model.NewSysUserRole()},
|
||||
// 日志管理
|
||||
&synchronized{iModel: model.NewSysLog()}, &synchronized{iModel: model.NewSysUserLoginLog()},
|
||||
)
|
||||
}
|
||||
func initCacheMode() {
|
||||
db := orm.GetDB()
|
||||
|
||||
function := func(cache ...*caches) {
|
||||
for _, v := range cache {
|
||||
if db.Migrator().HasTable(v.iModel) {
|
||||
if v.iValues != nil {
|
||||
if values := v.iValues(); values != nil {
|
||||
v.toCache(values)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
function(
|
||||
&caches{iModel: model.NewSysTenant(), iValues: func() interface{} {
|
||||
out := make([]*model.SysConfig, 0)
|
||||
_ = model.Find(model.NewSysConfig(), &out)
|
||||
return out
|
||||
}, toCache: func(values interface{}) {
|
||||
out := values.([]*model.SysConfig)
|
||||
for _, v := range out {
|
||||
config.SystemConfig[v.Key] = v.Value
|
||||
}
|
||||
}},
|
||||
)
|
||||
}
|
||||
|
||||
func Init() {
|
||||
initModel()
|
||||
initCacheMode()
|
||||
}
|
74
app/common/model/common.go
Normal file
74
app/common/model/common.go
Normal file
@ -0,0 +1,74 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"SciencesServer/utils"
|
||||
)
|
||||
|
||||
type Gender struct {
|
||||
Gender GenderKind `gorm:"column:gender;type:tinyint(1);default:1;comment:性别(1:男,2:女)" json:"gender"` // 性别(1:男,2:女)
|
||||
}
|
||||
|
||||
type GenderKind int
|
||||
|
||||
const (
|
||||
// GenderKindForMale 男性
|
||||
GenderKindForMale GenderKind = iota + 1
|
||||
// GenderKindForFemale 女性
|
||||
GenderKindForFemale
|
||||
)
|
||||
|
||||
func (m *Gender) GenderTitle() string {
|
||||
if m.Gender == GenderKindForMale {
|
||||
return "男"
|
||||
} else if m.Gender == GenderKindForFemale {
|
||||
return "女"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Image 单一图片信息
|
||||
type Image struct {
|
||||
Image string `gorm:"column:image;type:varchar(255);default:null;comment:图片" json:"image"`
|
||||
}
|
||||
|
||||
func (m *Image) Analysis(domain string) string {
|
||||
return domain + m.Image
|
||||
}
|
||||
|
||||
// Images 多个图片信息
|
||||
type Images struct {
|
||||
Images string `gorm:"column:images;type:text;default:null;comment:图片" json:"images"`
|
||||
}
|
||||
|
||||
// AnalysisSlice Slice解析
|
||||
func (m *Images) AnalysisSlice(domain string) []string {
|
||||
images := make([]string, 0)
|
||||
utils.FromJSON(m.Images, &images)
|
||||
|
||||
for k, v := range images {
|
||||
images[k] = domain + v
|
||||
}
|
||||
return images
|
||||
}
|
||||
|
||||
// Position 坐标信息
|
||||
type Position struct {
|
||||
Longitude float64 `json:"longitude"` // 经度
|
||||
Latitude float64 `json:"latitude"` // 纬度
|
||||
}
|
||||
|
||||
// Format 格式化
|
||||
func (m *Position) Format() string {
|
||||
return utils.AnyToJSON(m)
|
||||
}
|
||||
|
||||
// Tags 标签
|
||||
type Tags struct {
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
// Format 格式化
|
||||
func (m *Tags) Format() string {
|
||||
return utils.AnyToJSON(m)
|
||||
}
|
421
app/common/model/model.go
Normal file
421
app/common/model/model.go
Normal file
@ -0,0 +1,421 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"SciencesServer/serve/orm"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// IModel
|
||||
type IModel interface {
|
||||
GetID() uint64
|
||||
TableName() string
|
||||
SetDatabase(database string, key ...string)
|
||||
}
|
||||
|
||||
// Model
|
||||
type Model struct {
|
||||
ID uint64 `gorm:"column:id;primaryKey;autoIncrement;comment:主键" json:"id" form:"id"`
|
||||
|
||||
Database string `json:"-" gorm:"-"`
|
||||
}
|
||||
|
||||
// ModelTenant
|
||||
type ModelTenant struct {
|
||||
TenantID uint64 `gorm:"column:tenant_id;index:idx_sys_tenant_id;type:int(11);default:0;comment:租户ID" json:"-"`
|
||||
}
|
||||
|
||||
// ModelDeleted
|
||||
type ModelDeleted struct {
|
||||
IsDeleted DeleteStatus `gorm:"column:is_deleted;type:tinyint(1);default:0;comment:删除状态(0:未删除,1:已删除)" json:"-" form:"is_deleted"`
|
||||
}
|
||||
|
||||
// ModelAt
|
||||
type ModelAt struct {
|
||||
CreatedAt time.Time `gorm:"column:created_at;type:datetime;not null;comment:创建时间" json:"created_at" form:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;not null;comment:更新时间" json:"updated_at" form:"updated_at"`
|
||||
}
|
||||
|
||||
// DeleteStatus 删除状态
|
||||
type DeleteStatus int
|
||||
|
||||
const (
|
||||
// DeleteStatusForNot 未删除
|
||||
DeleteStatusForNot DeleteStatus = iota
|
||||
// DeleteStatusForAlready 已删除
|
||||
DeleteStatusForAlready
|
||||
)
|
||||
|
||||
const (
|
||||
// FieldsForID 主键ID
|
||||
FieldsForID string = "id"
|
||||
// FieldsForUpdatedAt 更新时间
|
||||
FieldsForUpdatedAt string = "updated_at"
|
||||
// FieldsForDeleted 删除字段名
|
||||
FieldsForDeleted string = "is_deleted"
|
||||
)
|
||||
|
||||
const (
|
||||
SubDatabase string = "tenant"
|
||||
)
|
||||
|
||||
func (m *Model) GetID() uint64 {
|
||||
return m.ID
|
||||
}
|
||||
|
||||
func (m *Model) SetDatabase(database string, key ...string) {
|
||||
m.Database = database + "_" + strings.Join(key, "_")
|
||||
}
|
||||
|
||||
func (m *Model) NewTableName(tableName string) string {
|
||||
if m.Database == "" {
|
||||
return tableName
|
||||
}
|
||||
return m.Database + "." + tableName
|
||||
}
|
||||
|
||||
func (m *ModelAt) BeforeCreate(db *gorm.DB) error {
|
||||
m.CreatedAt = time.Now()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *ModelAt) BeforeUpdate(db *gorm.DB) error {
|
||||
m.UpdatedAt = time.Now()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *ModelDeleted) IsDelete() bool {
|
||||
return m.IsDeleted == DeleteStatusForAlready
|
||||
}
|
||||
|
||||
func Create(model IModel, session ...*gorm.DB) error {
|
||||
if len(session) > 0 {
|
||||
return session[0].Table(model.TableName()).Create(model).Error
|
||||
}
|
||||
return orm.GetDB().Table(model.TableName()).Create(model).Error
|
||||
}
|
||||
|
||||
func Creates(model IModel, objs interface{}, session ...*gorm.DB) error {
|
||||
if len(session) > 0 {
|
||||
return session[0].Table(model.TableName()).Create(objs).Error
|
||||
}
|
||||
return orm.GetDB().Table(model.TableName()).Create(objs).Error
|
||||
}
|
||||
|
||||
func Save(model IModel, session ...*gorm.DB) error {
|
||||
if len(session) > 0 {
|
||||
return session[0].Table(model.TableName()).Save(model).Error
|
||||
}
|
||||
return orm.GetDB().Table(model.TableName()).Save(model).Error
|
||||
}
|
||||
|
||||
func Updates(model IModel, value interface{}, session ...*gorm.DB) error {
|
||||
if len(session) > 0 {
|
||||
return session[0].Table(model.TableName()).Where(fmt.Sprintf("%s = %d", FieldsForID, model.GetID())).Updates(value).Error
|
||||
}
|
||||
return orm.GetDB().Table(model.TableName()).Where(fmt.Sprintf("%s = %d", FieldsForID, model.GetID())).Updates(value).Error
|
||||
}
|
||||
|
||||
func UpdatesWhere(model IModel, value interface{}, where []*ModelWhere, session ...*gorm.DB) error {
|
||||
db := orm.GetDB()
|
||||
|
||||
if len(session) > 0 {
|
||||
db = session[0]
|
||||
}
|
||||
db = db.Table(model.TableName())
|
||||
|
||||
if len(where) > 0 {
|
||||
for _, wo := range where {
|
||||
db = db.Where(wo.Condition, wo.Value)
|
||||
}
|
||||
}
|
||||
return db.Updates(value).Error
|
||||
}
|
||||
|
||||
func Delete(model IModel, session ...*gorm.DB) error {
|
||||
db := orm.GetDB()
|
||||
|
||||
if len(session) > 0 {
|
||||
db = session[0]
|
||||
}
|
||||
db = db.Table(model.TableName()).Where(fmt.Sprintf("%s = %d", FieldsForID, model.GetID()))
|
||||
|
||||
if db.Migrator().HasColumn(model, FieldsForDeleted) {
|
||||
return db.Updates(map[string]interface{}{FieldsForDeleted: DeleteStatusForAlready, FieldsForUpdatedAt: time.Now()}).Error
|
||||
}
|
||||
return db.Delete(model).Error
|
||||
}
|
||||
|
||||
func DeleteWhere(model IModel, where []*ModelWhere, session ...*gorm.DB) error {
|
||||
db := orm.GetDB()
|
||||
|
||||
if len(session) > 0 {
|
||||
db = session[0]
|
||||
}
|
||||
db = db.Table(model.TableName())
|
||||
|
||||
if len(where) > 0 {
|
||||
for _, wo := range where {
|
||||
db = db.Where(wo.Condition, wo.Value)
|
||||
}
|
||||
}
|
||||
if db.Migrator().HasColumn(model, FieldsForDeleted) {
|
||||
return db.Updates(map[string]interface{}{FieldsForDeleted: DeleteStatusForAlready, FieldsForUpdatedAt: time.Now()}).Error
|
||||
}
|
||||
return db.Delete(model).Error
|
||||
}
|
||||
|
||||
func Count(model IModel, count *int64, where ...*ModelWhere) error {
|
||||
db := orm.GetDB().Table(model.TableName())
|
||||
|
||||
if len(where) > 0 {
|
||||
for _, wo := range where {
|
||||
db = db.Where(wo.Condition, wo.Value)
|
||||
}
|
||||
}
|
||||
if db.Migrator().HasColumn(model, FieldsForDeleted) {
|
||||
db = db.Where(FieldsForDeleted, DeleteStatusForNot)
|
||||
}
|
||||
return db.Count(count).Error
|
||||
}
|
||||
|
||||
func First(model IModel) (bool, error) {
|
||||
db := orm.GetDB().Table(model.TableName())
|
||||
|
||||
if db.Migrator().HasColumn(model, FieldsForDeleted) {
|
||||
db = db.Where(FieldsForDeleted, DeleteStatusForNot)
|
||||
}
|
||||
if err := db.First(model).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func FirstWhere(model IModel, where ...*ModelWhere) (bool, error) {
|
||||
db := orm.GetDB().Table(model.TableName())
|
||||
|
||||
if len(where) > 0 {
|
||||
for _, wo := range where {
|
||||
db = db.Where(wo.Condition, wo.Value)
|
||||
}
|
||||
} else {
|
||||
db = db.Where(fmt.Sprintf("%s = %d", FieldsForID, model.GetID()))
|
||||
}
|
||||
if db.Migrator().HasColumn(model, FieldsForDeleted) {
|
||||
db = db.Where(FieldsForDeleted, DeleteStatusForNot)
|
||||
}
|
||||
if err := db.First(model).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func FirstField(model IModel, field []string, where ...*ModelWhere) (bool, error) {
|
||||
db := orm.GetDB().Table(model.TableName())
|
||||
|
||||
if len(where) > 0 {
|
||||
for _, wo := range where {
|
||||
db = db.Where(wo.Condition, wo.Value)
|
||||
}
|
||||
} else {
|
||||
db = db.Where(fmt.Sprintf("%s = %d", FieldsForID, model.GetID()))
|
||||
}
|
||||
if db.Migrator().HasColumn(model, FieldsForDeleted) {
|
||||
db = db.Where(FieldsForDeleted, DeleteStatusForNot)
|
||||
}
|
||||
if err := db.Select(field).First(model).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func Last(model IModel) (bool, error) {
|
||||
db := orm.GetDB().Table(model.TableName())
|
||||
|
||||
if db.Migrator().HasColumn(model, FieldsForDeleted) {
|
||||
db = db.Where(FieldsForDeleted, DeleteStatusForNot)
|
||||
}
|
||||
if err := db.Order("id " + OrderModeToDesc).First(model).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func LastWhere(model IModel, where ...*ModelWhere) (bool, error) {
|
||||
db := orm.GetDB().Table(model.TableName())
|
||||
|
||||
if len(where) > 0 {
|
||||
for _, wo := range where {
|
||||
db = db.Where(wo.Condition, wo.Value)
|
||||
}
|
||||
} else {
|
||||
db = db.Where(fmt.Sprintf("%s = %d", FieldsForID, model.GetID()))
|
||||
}
|
||||
if db.Migrator().HasColumn(model, FieldsForDeleted) {
|
||||
db = db.Where(FieldsForDeleted, DeleteStatusForNot)
|
||||
}
|
||||
if err := db.Order("id " + OrderModeToDesc).First(model).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func LastWhereOrder(model IModel, where ...*ModelWhereOrder) (bool, error) {
|
||||
db := orm.GetDB().Table(model.TableName())
|
||||
|
||||
if len(where) > 0 {
|
||||
for _, wo := range where {
|
||||
if wo.Where != nil {
|
||||
db = db.Where(wo.Where.Condition, wo.Where.Value)
|
||||
}
|
||||
if wo.Order != nil {
|
||||
db = db.Order(fmt.Sprintf("%s %s", wo.Order.Field, wo.Order.Mode))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
db = db.Where(fmt.Sprintf("%s = %d", FieldsForID, model.GetID()))
|
||||
}
|
||||
if db.Migrator().HasColumn(model, FieldsForDeleted) {
|
||||
db = db.Where(FieldsForDeleted, DeleteStatusForNot)
|
||||
}
|
||||
if err := db.Limit(1).First(model).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func Find(model IModel, out interface{}, where ...*ModelWhereOrder) error {
|
||||
db := orm.GetDB().Table(model.TableName())
|
||||
|
||||
if len(where) > 0 {
|
||||
for _, wo := range where {
|
||||
if wo.Where != nil {
|
||||
db = db.Where(wo.Where.Condition, wo.Where.Value)
|
||||
}
|
||||
if wo.Order != nil {
|
||||
db = db.Order(fmt.Sprintf("%s %s", wo.Order.Field, wo.Order.Mode))
|
||||
}
|
||||
}
|
||||
}
|
||||
if db.Migrator().HasColumn(model, FieldsForDeleted) {
|
||||
db = db.Where(FieldsForDeleted, DeleteStatusForNot)
|
||||
}
|
||||
return db.Find(out).Error
|
||||
}
|
||||
|
||||
func Scan(model IModel, out interface{}, where ...*ModelWhereOrder) error {
|
||||
db := orm.GetDB().Table(model.TableName())
|
||||
|
||||
if len(where) > 0 {
|
||||
for _, wo := range where {
|
||||
if wo.Where != nil {
|
||||
db = db.Where(wo.Where.Condition, wo.Where.Value)
|
||||
}
|
||||
if wo.Order != nil {
|
||||
db = db.Order(fmt.Sprintf("%s %s", wo.Order.Field, wo.Order.Mode))
|
||||
}
|
||||
}
|
||||
}
|
||||
if db.Migrator().HasColumn(model, FieldsForDeleted) {
|
||||
db = db.Where(FieldsForDeleted, DeleteStatusForNot)
|
||||
}
|
||||
return db.Scan(out).Error
|
||||
}
|
||||
|
||||
func ScanFields(model IModel, out interface{}, fields []string, where ...*ModelWhereOrder) error {
|
||||
db := orm.GetDB().Table(model.TableName()).Select(fields)
|
||||
|
||||
if len(where) > 0 {
|
||||
for _, wo := range where {
|
||||
if wo.Where != nil {
|
||||
db = db.Where(wo.Where.Condition, wo.Where.Value)
|
||||
}
|
||||
if wo.Order != nil {
|
||||
db = db.Order(fmt.Sprintf("%s %s", wo.Order.Field, wo.Order.Mode))
|
||||
}
|
||||
}
|
||||
}
|
||||
if db.Migrator().HasColumn(model, FieldsForDeleted) {
|
||||
db = db.Where(FieldsForDeleted, DeleteStatusForNot)
|
||||
}
|
||||
return db.Scan(out).Error
|
||||
}
|
||||
|
||||
func Pluck(model IModel, field string, out interface{}, where ...*ModelWhere) error {
|
||||
db := orm.GetDB().Table(model.TableName())
|
||||
|
||||
if len(where) > 0 {
|
||||
for _, wo := range where {
|
||||
db = db.Where(wo.Condition, wo.Value)
|
||||
}
|
||||
}
|
||||
if db.Migrator().HasColumn(model, FieldsForDeleted) {
|
||||
db = db.Where(FieldsForDeleted, DeleteStatusForNot)
|
||||
}
|
||||
return db.Pluck(field, out).Error
|
||||
}
|
||||
|
||||
func Pages(model IModel, out interface{}, page, pageSize int, count *int64, where ...*ModelWhereOrder) error {
|
||||
db := orm.GetDB().Table(model.TableName())
|
||||
|
||||
if len(where) > 0 {
|
||||
for _, wo := range where {
|
||||
if wo.Where != nil {
|
||||
db = db.Where(wo.Where.Condition, wo.Where.Value)
|
||||
}
|
||||
if wo.Order != nil {
|
||||
db = db.Order(fmt.Sprintf("%s %s", wo.Order.Field, wo.Order.Mode))
|
||||
}
|
||||
}
|
||||
}
|
||||
if db.Migrator().HasColumn(model, FieldsForDeleted) {
|
||||
db = db.Where(FieldsForDeleted, DeleteStatusForNot)
|
||||
}
|
||||
if err := db.Count(count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return db.Offset((page - 1) * pageSize).Limit(pageSize).Scan(out).Error
|
||||
}
|
||||
|
||||
func PagesFields(model IModel, out interface{}, fields []string, page, pageSize int, count *int64, where ...*ModelWhereOrder) error {
|
||||
db := orm.GetDB().Table(model.TableName()).Select(fields)
|
||||
|
||||
if len(where) > 0 {
|
||||
for _, wo := range where {
|
||||
if wo.Where != nil {
|
||||
db = db.Where(wo.Where.Condition, wo.Where.Value)
|
||||
}
|
||||
if wo.Order != nil {
|
||||
db = db.Order(fmt.Sprintf("%s %s", wo.Order.Field, wo.Order.Mode))
|
||||
}
|
||||
}
|
||||
}
|
||||
if db.Migrator().HasColumn(model, FieldsForDeleted) {
|
||||
db = db.Where(FieldsForDeleted, DeleteStatusForNot)
|
||||
}
|
||||
if err := db.Count(count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return db.Offset((page - 1) * pageSize).Limit(pageSize).Scan(out).Error
|
||||
}
|
32
app/common/model/model_test.go
Normal file
32
app/common/model/model_test.go
Normal file
@ -0,0 +1,32 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
GormEngine "github.com/belief428/gorm-engine"
|
||||
"github.com/belief428/gorm-engine/engine"
|
||||
"github.com/belief428/gorm-engine/logic"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var (
|
||||
_db *gorm.DB
|
||||
)
|
||||
|
||||
func mysql() logic.IEngine {
|
||||
return &engine.MConfig{
|
||||
User: "appuser", Password: "ABCabc01!",
|
||||
Host: "192.168.99.188", Port: 3306,
|
||||
DBName: "iot", Parameters: "charset=utf8mb4,utf8&parseTime=True&loc=Asia%2FShanghai",
|
||||
}
|
||||
}
|
||||
|
||||
func _init() {
|
||||
_db = GormEngine.NewEngine()(1, &GormEngine.EngineConfig{
|
||||
Debug: true,
|
||||
TablePrefix: "",
|
||||
Complex: false,
|
||||
MaxIdleConns: 50,
|
||||
MaxOpenConns: 150,
|
||||
MaxLifetime: 3600,
|
||||
}).Start(mysql())
|
||||
}
|
40
app/common/model/sys_auth.go
Normal file
40
app/common/model/sys_auth.go
Normal file
@ -0,0 +1,40 @@
|
||||
package model
|
||||
|
||||
type SysAuth struct {
|
||||
Model
|
||||
ParentID uint64 `gorm:"column:parent_id;type:int;default:0;comment:父级ID" json:"-"`
|
||||
Kind SysAuthKind `gorm:"column:kind;type:tinyint(1);default:1;comment:类型(1:模块,2:权限)" json:"kind"`
|
||||
Name string `gorm:"column:name;type:varchar(30);default:null;comment:名称" json:"name"`
|
||||
Auth string `gorm:"column:auth;type:varchar(100);default:null;comment:权限/路由" json:"auth"`
|
||||
Sort int `gorm:"column:sort;type:tinyint(3);default:0;comment:排序" json:"-"`
|
||||
Remark string `gorm:"column:remark;type:varchar(255);default:null;comment:备注信息" json:"remark"`
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
// SysAuthKind 权限分类
|
||||
type SysAuthKind int
|
||||
|
||||
const (
|
||||
// SysAuthKindForModule 模块
|
||||
SysAuthKindForModule SysAuthKind = iota + 1
|
||||
// SysAuthKindForAuth 权限
|
||||
SysAuthKindForAuth
|
||||
)
|
||||
|
||||
func (m *SysAuth) TableName() string {
|
||||
return "sys_auth"
|
||||
}
|
||||
|
||||
func (m *SysAuth) KindTitle() string {
|
||||
if m.Kind == SysAuthKindForModule {
|
||||
return "模块"
|
||||
} else if m.Kind == SysAuthKindForAuth {
|
||||
return "权限"
|
||||
}
|
||||
return "-"
|
||||
}
|
||||
|
||||
func NewSysAuth() *SysAuth {
|
||||
return &SysAuth{}
|
||||
}
|
21
app/common/model/sys_auth_rule.go
Normal file
21
app/common/model/sys_auth_rule.go
Normal file
@ -0,0 +1,21 @@
|
||||
package model
|
||||
|
||||
// SysAuthRule 用户角色权限规则,公用组件库-实例: `p, admin, tenant, data, read`
|
||||
type SysAuthRule struct {
|
||||
Model
|
||||
Ptype string `gorm:"column:ptype;type:varchar(100);default:null;comment:策略(组g/人员p)" json:"ptype"` // 相当于policy.csv文件中 一行中的 p
|
||||
V0 string `gorm:"column:v0;type:varchar(100);default:null;comment:角色/用户" json:"v0"` // 相当于policy.csv文件中 一行中的 role (角色)(例如:admin)
|
||||
V1 string `gorm:"column:v1;type:varchar(100);default:null;comment:平台/角色" json:"v1"` // 相当于policy.csv文件中 一行中的 sub (用户)(例如:tenant)
|
||||
V2 string `gorm:"column:v2;type:varchar(100);default:null;comment:路由/平台" json:"v2"` // 相当于policy.csv文件中 一行中的 obj (被操作的服务器资源)(例如:data)
|
||||
V3 string `gorm:"column:v3;type:varchar(100);default:null;comment:请求方式" json:"v3"` // 相当于policy.csv文件中 一行中的 act (操作者的行为)(例如:read)
|
||||
V4 string `gorm:"column:v4;type:varchar(100);default:null;comment:-" json:"v4"`
|
||||
V5 string `gorm:"column:v5;type:varchar(100);default:null;comment:-" json:"v5"`
|
||||
}
|
||||
|
||||
func (m *SysAuthRule) TableName() string {
|
||||
return "sys_auth_rule"
|
||||
}
|
||||
|
||||
func NewSysAuthRule() *SysAuthRule {
|
||||
return &SysAuthRule{}
|
||||
}
|
36
app/common/model/sys_config.go
Normal file
36
app/common/model/sys_config.go
Normal file
@ -0,0 +1,36 @@
|
||||
package model
|
||||
|
||||
// SysConfig 系统配置信息
|
||||
type SysConfig struct {
|
||||
Model
|
||||
Kind SysConfigKind `gorm:"column:kind;type:tinyint(3);default:0;comment:类型" json:"kind"`
|
||||
Name string `gorm:"column:name;type:varchar(30);default:null;comment:名称" json:"name"`
|
||||
Key string `gorm:"column:key;type:varchar(30);default:null;comment:标识" json:"key"`
|
||||
Value string `gorm:"column:value;type:varchar(255);default:null;comment:内容" json:"value"`
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
// SysConfigKind 系统配置分类
|
||||
type SysConfigKind int
|
||||
|
||||
const (
|
||||
// SysConfigKindForBasic 基本配置
|
||||
SysConfigKindForBasic SysConfigKind = iota + 1
|
||||
// SysConfigKindForWeChat 微信配置
|
||||
SysConfigKindForWeChat
|
||||
// SysConfigKindForAliPay 支付宝配置
|
||||
SysConfigKindForAliPay
|
||||
// SysConfigKindForGD 高德配置
|
||||
SysConfigKindForGaoDe
|
||||
// SysConfigKindForBaiDu 百度配置
|
||||
SysConfigKindForBaiDu
|
||||
)
|
||||
|
||||
func (m *SysConfig) TableName() string {
|
||||
return "sys_config"
|
||||
}
|
||||
|
||||
func NewSysConfig() *SysConfig {
|
||||
return &SysConfig{}
|
||||
}
|
21
app/common/model/sys_department.go
Normal file
21
app/common/model/sys_department.go
Normal file
@ -0,0 +1,21 @@
|
||||
package model
|
||||
|
||||
type SysDepartment struct {
|
||||
Model
|
||||
ModelTenant
|
||||
ParentID uint64 `gorm:"column:parent_id;type:int;default:0;comment:父级ID" json:"parent_id"`
|
||||
Title string `gorm:"column:title;type:varchar(20);default:null;comment:部门名称" json:"title"`
|
||||
Name string `gorm:"column:name;type:varchar(20);default:null;comment:联系人" json:"name"`
|
||||
Mobile string `gorm:"column:mobile;type:varchar(15);default:null;comment:联系方式" json:"mobile"`
|
||||
Remark string `gorm:"column:remark;type:varchar(255);default:null;comment:备注信息" json:"remark"`
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
func (m *SysDepartment) TableName() string {
|
||||
return m.NewTableName("sys_department")
|
||||
}
|
||||
|
||||
func NewSysDepartment() *SysDepartment {
|
||||
return &SysDepartment{}
|
||||
}
|
23
app/common/model/sys_log.go
Normal file
23
app/common/model/sys_log.go
Normal file
@ -0,0 +1,23 @@
|
||||
package model
|
||||
|
||||
// SysLog 系统日志
|
||||
type SysLog struct {
|
||||
Model
|
||||
ModelTenant
|
||||
UID uint64 `gorm:"column:uid;type:int;default:0;comment:用户uuid" json:"-"`
|
||||
Name string `gorm:"column:name;type:varchar(20);default:null;comment:真实姓名" json:"name"`
|
||||
Method string `gorm:"column:method;type:varchar(8);default:null;comment:请求方式" json:"method"`
|
||||
Path string `gorm:"column:path;type:varchar(8);default:0;comment:请求地址" json:"event"`
|
||||
Params string `gorm:"column:params;type:text;default:null;comment:参数信息" json:"params"`
|
||||
IP string `gorm:"column:ip;type:char(16);default:null;comment:IP地址" json:"ip"`
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
func (m *SysLog) TableName() string {
|
||||
return m.NewTableName("sys_log")
|
||||
}
|
||||
|
||||
func NewSysLog() *SysLog {
|
||||
return &SysLog{}
|
||||
}
|
59
app/common/model/sys_menu.go
Normal file
59
app/common/model/sys_menu.go
Normal file
@ -0,0 +1,59 @@
|
||||
package model
|
||||
|
||||
type SysMenu struct {
|
||||
Model
|
||||
SysMenuBasic
|
||||
Auth SysMenuAuth `gorm:"column:auth;type:tinyint(1);default:0;comment:查看权限(0:通用,1:超管)" json:"auth"`
|
||||
Sort int `gorm:"column:sort;type:tinyint(3);default:0;comment:排序,数值越大,优先排序" json:"sort"`
|
||||
Remark string `gorm:"column:remark;type:varchar(255);default:null;comment:菜单备注" json:"remark"`
|
||||
Status SysMenuStatus `gorm:"column:status;type:tinyint(1);default:1;comment:状态" json:"status"`
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
type SysMenuBasic struct {
|
||||
ParentID uint64 `gorm:"column:parent_id;type:int;default:0;comment:父级ID" json:"parent_id"`
|
||||
Name string `gorm:"column:name;type:varchar(30);default:null;comment:菜单名" json:"name"`
|
||||
Kind SysMenuKind `gorm:"column:kind;type:tinyint(1);default:1;comment:类型(1:目录,2:菜单)" json:"kind"`
|
||||
Link string `gorm:"column:link;type:varchar(80);default:null;comment:菜单链接" json:"link"`
|
||||
Component string `gorm:"column:component;type:varchar(80);default:null;comment:组件标识" json:"component"`
|
||||
Icon string `gorm:"column:icon;type:varchar(50);default:null;comment:菜单图标" json:"icon"`
|
||||
}
|
||||
|
||||
// SysMenuKind 菜单类型
|
||||
type SysMenuKind int
|
||||
|
||||
const (
|
||||
// SysMenuKindForCatalogue 目录
|
||||
SysMenuKindForCatalogue SysMenuKind = iota + 1
|
||||
// SysMenuKindForMenu 菜单
|
||||
SysMenuKindForMenu
|
||||
)
|
||||
|
||||
// SysMenuAuth 菜单权限
|
||||
type SysMenuAuth int
|
||||
|
||||
const (
|
||||
// SysMenuAuthForOrdinary 普通权限
|
||||
SysMenuAuthForOrdinary SysMenuAuth = iota
|
||||
// SysMenuAuthForSystem 系统权限
|
||||
SysMenuAuthForSystem
|
||||
)
|
||||
|
||||
// SysMenuStatus 菜单状态
|
||||
type SysMenuStatus int
|
||||
|
||||
const (
|
||||
// SysMenuStatusForNormal 正常
|
||||
SysMenuStatusForNormal SysMenuStatus = iota + 1
|
||||
// SysMenuStatusForDisable 禁用
|
||||
SysMenuStatusForDisable
|
||||
)
|
||||
|
||||
func (m *SysMenu) TableName() string {
|
||||
return "sys_menu"
|
||||
}
|
||||
|
||||
func NewSysMenu() *SysMenu {
|
||||
return &SysMenu{}
|
||||
}
|
39
app/common/model/sys_role.go
Normal file
39
app/common/model/sys_role.go
Normal file
@ -0,0 +1,39 @@
|
||||
package model
|
||||
|
||||
type SysRole struct {
|
||||
Model
|
||||
ModelTenant
|
||||
Name string `gorm:"column:name;type:varchar(30);default:null;comment:角色名" json:"name"`
|
||||
Remark string `gorm:"column:remark;type:varchar(255);default:null;comment:角色备注" json:"remark"`
|
||||
Sort int `gorm:"column:sort;type:tinyint(3);default:0;comment:排序" json:"-"`
|
||||
Status SysRoleStatus `gorm:"column:status;type:tinyint(1);default:0;comment:状态" json:"-"`
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
// SysRoleStatus 角色状态
|
||||
type SysRoleStatus int
|
||||
|
||||
const (
|
||||
// SysRoleStatusForNormal 正常
|
||||
SysRoleStatusForNormal SysRoleStatus = iota + 1
|
||||
// SysRoleStatusForDisable 禁用
|
||||
SysRoleStatusForDisable
|
||||
)
|
||||
|
||||
func (m *SysRole) TableName() string {
|
||||
return m.NewTableName("sys_role")
|
||||
}
|
||||
|
||||
func (m *SysRole) StatusTitle() string {
|
||||
if m.Status == SysRoleStatusForNormal {
|
||||
return "正常"
|
||||
} else if m.Status == SysRoleStatusForDisable {
|
||||
return "禁用"
|
||||
}
|
||||
return "-"
|
||||
}
|
||||
|
||||
func NewSysRole() *SysRole {
|
||||
return &SysRole{}
|
||||
}
|
18
app/common/model/sys_role_auth.go
Normal file
18
app/common/model/sys_role_auth.go
Normal file
@ -0,0 +1,18 @@
|
||||
package model
|
||||
|
||||
type SysRoleAuth struct {
|
||||
Model
|
||||
ModelTenant
|
||||
RoleID uint64 `gorm:"column:role_id;type:int;default:0;comment:角色ID" json:"-"`
|
||||
AuthID uint64 `gorm:"column:auth_id;type:int;default:0;comment:权限ID" json:"-"`
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
func (m *SysRoleAuth) TableName() string {
|
||||
return m.NewTableName("sys_role_auth")
|
||||
}
|
||||
|
||||
func NewSysRoleAuth() *SysRoleAuth {
|
||||
return &SysRoleAuth{}
|
||||
}
|
18
app/common/model/sys_role_menu.go
Normal file
18
app/common/model/sys_role_menu.go
Normal file
@ -0,0 +1,18 @@
|
||||
package model
|
||||
|
||||
type SysRoleMenu struct {
|
||||
Model
|
||||
ModelTenant
|
||||
RoleID uint64 `gorm:"column:role_id;type:int;default:0;comment:角色ID" json:"-"`
|
||||
MenuID uint64 `gorm:"column:menu_id;type:int;default:0;comment:菜单ID" json:"-"`
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
func (m *SysRoleMenu) TableName() string {
|
||||
return m.NewTableName("sys_role_menu")
|
||||
}
|
||||
|
||||
func NewSysRoleMenu() *SysRoleMenu {
|
||||
return &SysRoleMenu{}
|
||||
}
|
69
app/common/model/sys_tenant.go
Normal file
69
app/common/model/sys_tenant.go
Normal file
@ -0,0 +1,69 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"SciencesServer/utils"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type SysTenant struct {
|
||||
Model
|
||||
ParentID uint64 `gorm:"column:parent_id;type:int;default:0;comment:父级ID" json:"-"`
|
||||
Key string `gorm:"column:key;type:varchar(100);default:null;comment:租户/租户客户标识" json:"key"`
|
||||
Image
|
||||
Name string `gorm:"column:name;type:varchar(30);default:null;comment:租户名称/租户客户名称" json:"name"`
|
||||
Config string `gorm:"column:config;type:text;comment:租户/租户客户配置信息" json:"-"`
|
||||
Deadline time.Time `gorm:"column:deadline;type:datetime;default:null;comment:租户/租户客户协议截止时间" json:"deadline"`
|
||||
Status SysTenantStatus `gorm:"column:status;type:tinyint(1);default:1;comment:租户/租户客户状态" json:"status"`
|
||||
Remark string `gorm:"column:remark;type:varchar(255);default:null;comment:租户/租户客户备注" json:"remark"`
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
type SysTenantConfig struct {
|
||||
MaxDevices int `json:"max_devices"` // 最大可连接设备数
|
||||
MaxCustomer int `json:"max_customer"` // 最大可拥有的客户数
|
||||
Protocol uint `json:"protocol"` // 支持的协议模式
|
||||
}
|
||||
|
||||
type SysTenantStatus int
|
||||
|
||||
const (
|
||||
// SysTenantStatusForNormal 正常
|
||||
SysTenantStatusForNormal SysTenantStatus = iota + 1
|
||||
// SysTenantStatusForWellExpire 协议将到期
|
||||
SysTenantStatusForWellExpire
|
||||
// SysTenantStatusForExpired 协议已过期
|
||||
SysTenantStatusForExpired
|
||||
// SysTenantStatusForDisable 已禁用
|
||||
SysTenantStatusForDisable
|
||||
)
|
||||
|
||||
func (m *SysTenant) TableName() string {
|
||||
return "sys_tenant"
|
||||
}
|
||||
|
||||
func (m *SysTenant) BeforeCreate(db *gorm.DB) error {
|
||||
snowflake, _ := utils.NewSnowflake(1)
|
||||
m.Key = utils.IntToString(snowflake.GetID())
|
||||
m.CreatedAt = time.Now()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *SysTenant) ConfigInfo() *SysTenantConfig {
|
||||
config := new(SysTenantConfig)
|
||||
_ = utils.FromJSON(m.Config, config)
|
||||
return config
|
||||
}
|
||||
|
||||
func (m *SysTenant) StatusTitle() string {
|
||||
if m.Status == SysTenantStatusForDisable {
|
||||
return "禁用"
|
||||
}
|
||||
return "正常"
|
||||
}
|
||||
|
||||
func NewSysTenant() *SysTenant {
|
||||
return &SysTenant{}
|
||||
}
|
17
app/common/model/sys_tenant_auth.go
Normal file
17
app/common/model/sys_tenant_auth.go
Normal file
@ -0,0 +1,17 @@
|
||||
package model
|
||||
|
||||
type SysTenantAuth struct {
|
||||
Model
|
||||
ModelTenant
|
||||
AuthID uint64 `gorm:"column:auth_id;type:int;default:0;comment:权限ID" json:"-"`
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
func (m *SysTenantAuth) TableName() string {
|
||||
return m.NewTableName("sys_tenant_auth")
|
||||
}
|
||||
|
||||
func NewSysTenantAuth() *SysTenantAuth {
|
||||
return &SysTenantAuth{}
|
||||
}
|
17
app/common/model/sys_tenant_menu.go
Normal file
17
app/common/model/sys_tenant_menu.go
Normal file
@ -0,0 +1,17 @@
|
||||
package model
|
||||
|
||||
type SysTenantMenu struct {
|
||||
Model
|
||||
ModelTenant
|
||||
MenuID uint64 `gorm:"column:menu_id;type:int;default:0;comment:菜单ID" json:"-"`
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
func (m *SysTenantMenu) TableName() string {
|
||||
return m.NewTableName("sys_tenant_menu")
|
||||
}
|
||||
|
||||
func NewSysTenantMenu() *SysTenantMenu {
|
||||
return &SysTenantMenu{}
|
||||
}
|
60
app/common/model/sys_user.go
Normal file
60
app/common/model/sys_user.go
Normal file
@ -0,0 +1,60 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"SciencesServer/utils"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type SysUser struct {
|
||||
Model
|
||||
UUID uint64 `gorm:"column:uuid;uniqueIndex:idx_sys_user_uuid;type:int;default:0;comment:用户唯一UUID" json:"-"`
|
||||
Account string `gorm:"column:account;type:varchar(50);default:null;comment:账户名" json:"account"`
|
||||
Avatar string `gorm:"column:avatar;type:varchar(255);default:null;comment:头像" json:"avatar"`
|
||||
Name string `gorm:"column:name;type:varchar(20);default:null;comment:真实姓名" json:"name"`
|
||||
Mobile string `gorm:"column:mobile;index:idx_sys_user_mobile;type:varchar(15);default:null;comment:联系方式" json:"mobile"`
|
||||
Email string `gorm:"column:email;type:varchar(50);default:null;comment:邮箱" json:"email"`
|
||||
Gender
|
||||
Password string `gorm:"column:password;type:varchar(100);default:null;comment:密码" json:"-"`
|
||||
Salt string `gorm:"column:salt;type:varchar(10);default:null;comment:盐值" json:"-"`
|
||||
IsAdmin SysUserAdministrator `gorm:"column:is_admin;type:tinyint(1);default:0;comment:管理员(0:普通用户,1:管理员)" json:"-"`
|
||||
Remark string `gorm:"column:remark;type:varchar(255);default:null;comment:备注" json:"-"`
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
type SysUserAdministrator int
|
||||
|
||||
const (
|
||||
// SysUserAdministratorForOrdinary 普通人员
|
||||
SysUserAdministratorForOrdinary SysUserAdministrator = iota
|
||||
// SysUserAdministratorForAdmin 管理员
|
||||
SysUserAdministratorForAdmin
|
||||
)
|
||||
|
||||
func (m *SysUser) TableName() string {
|
||||
return "sys_user"
|
||||
}
|
||||
|
||||
func (m *SysUser) BeforeCreate(db *gorm.DB) error {
|
||||
m.Pass()
|
||||
snowflake, _ := utils.NewSnowflake(1)
|
||||
m.UUID = uint64(snowflake.GetID())
|
||||
m.CreatedAt = time.Now()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *SysUser) Pass() {
|
||||
m.Salt = utils.GetRandomString(8)
|
||||
m.Password = utils.HashString([]byte(utils.Md5String(m.Password, m.Salt)))
|
||||
}
|
||||
|
||||
func (m *SysUser) UUIDString() string {
|
||||
return fmt.Sprintf("%d", m.UUID)
|
||||
}
|
||||
|
||||
func NewSysUser() *SysUser {
|
||||
return &SysUser{}
|
||||
}
|
19
app/common/model/sys_user_login_log.go
Normal file
19
app/common/model/sys_user_login_log.go
Normal file
@ -0,0 +1,19 @@
|
||||
package model
|
||||
|
||||
type SysUserLoginLog struct {
|
||||
Model
|
||||
ModelTenant
|
||||
UID uint64 `gorm:"column:uid;type:int;default:0;comment:用户uuid" json:"-"`
|
||||
Equipment string `gorm:"column:equipment;type:char(10);default:null;comment:登录设备" json:"equipment"`
|
||||
IP string `gorm:"column:ip;type:char(16);default:null;comment:IP地址" json:"ip"`
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
func (m *SysUserLoginLog) TableName() string {
|
||||
return "sys_user_login_log"
|
||||
}
|
||||
|
||||
func NewSysUserLoginLog() *SysUserLoginLog {
|
||||
return &SysUserLoginLog{}
|
||||
}
|
18
app/common/model/sys_user_role.go
Normal file
18
app/common/model/sys_user_role.go
Normal file
@ -0,0 +1,18 @@
|
||||
package model
|
||||
|
||||
type SysUserRole struct {
|
||||
Model
|
||||
ModelTenant
|
||||
UID uint64 `gorm:"column:uid;type:int;default:0;comment:用户uuid" json:"-"`
|
||||
RoleID uint64 `gorm:"column:role_id;type:int;default:0;comment:角色ID" json:"-"`
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
func (m *SysUserRole) TableName() string {
|
||||
return m.NewTableName("sys_user_role")
|
||||
}
|
||||
|
||||
func NewSysUserRole() *SysUserRole {
|
||||
return &SysUserRole{}
|
||||
}
|
40
app/common/model/sys_user_tenant.go
Normal file
40
app/common/model/sys_user_tenant.go
Normal file
@ -0,0 +1,40 @@
|
||||
package model
|
||||
|
||||
type SysUserTenant struct {
|
||||
Model
|
||||
ModelTenant
|
||||
UID uint64 `gorm:"column:uid;type:int;default:0;comment:用户uuid" json:"-"`
|
||||
Department string `gorm:"column:department;type:varchar(100);default:null;comment:部门信息" json:"department"`
|
||||
Role string `gorm:"column:role;type:varchar(100);default:null;comment:角色信息" json:"role"`
|
||||
Identity SysUserTenantIdentity `gorm:"column:identity;type:tinyint(1);default:0;comment:用户身份(1:管理员,2:用户)" json:"-"`
|
||||
Status SysUserTenantStatus `gorm:"column:status;type:tinyint(1);default:1;comment:状态(1:启用,2:禁用)" json:"-"`
|
||||
ModelDeleted
|
||||
ModelAt
|
||||
}
|
||||
|
||||
type SysUserTenantIdentity int
|
||||
|
||||
const (
|
||||
// SysUserTenantIdentityForSystemAdmin 管理员
|
||||
SysUserTenantIdentityForSystemAdmin SysUserTenantIdentity = iota + 1
|
||||
// SysUserTenantIdentityForSystemUser 用户
|
||||
SysUserTenantIdentityForSystemUser
|
||||
)
|
||||
|
||||
// SysUserTenantStatus 状态
|
||||
type SysUserTenantStatus int
|
||||
|
||||
const (
|
||||
// SysUserTenantStatusForEnable 启用
|
||||
SysUserTenantStatusForEnable SysUserTenantStatus = iota + 1
|
||||
// SysUserTenantStatusForDisable 禁用
|
||||
SysUserTenantStatusForDisable
|
||||
)
|
||||
|
||||
func (m *SysUserTenant) TableName() string {
|
||||
return m.NewTableName("sys_user_tenant")
|
||||
}
|
||||
|
||||
func NewSysUserTenant() *SysUserTenant {
|
||||
return &SysUserTenant{}
|
||||
}
|
134
app/common/model/where.go
Normal file
134
app/common/model/where.go
Normal file
@ -0,0 +1,134 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ModelWhere
|
||||
type ModelWhere struct {
|
||||
Condition string `json:"condition"` // 条件
|
||||
Value interface{} `json:"value"` // 内容
|
||||
}
|
||||
|
||||
// ModelOrder
|
||||
type ModelOrder struct {
|
||||
Field string `json:"field"` // 字段值
|
||||
Mode OrderMode `json:"mode"` // 排序模式
|
||||
}
|
||||
|
||||
// ModelWhereOrder
|
||||
type ModelWhereOrder struct {
|
||||
Where *ModelWhere
|
||||
Order *ModelOrder
|
||||
}
|
||||
|
||||
// OrderMode
|
||||
type OrderMode string
|
||||
|
||||
const (
|
||||
// OrderModeToAsc asc
|
||||
OrderModeToAsc = "asc"
|
||||
// OrderModeToDesc desc
|
||||
OrderModeToDesc = "desc"
|
||||
)
|
||||
|
||||
// NewWhere where
|
||||
func NewWhere(filed string, value interface{}) *ModelWhere {
|
||||
return &ModelWhere{
|
||||
Condition: filed + " = ?",
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
|
||||
// NewWhereLike like where
|
||||
func NewWhereLike(filed string, value interface{}) *ModelWhere {
|
||||
return &ModelWhere{
|
||||
Condition: filed + " LIKE ?",
|
||||
Value: "%" + fmt.Sprintf("%v", value) + "%",
|
||||
}
|
||||
}
|
||||
|
||||
func NewWhereIn(filed string, value interface{}) *ModelWhere {
|
||||
return &ModelWhere{
|
||||
Condition: filed + " IN ?",
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
|
||||
func NewWhereNotIn(filed string, value interface{}) *ModelWhere {
|
||||
return &ModelWhere{
|
||||
Condition: filed + " NOT IN ?",
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
|
||||
func NewWhereCondition(filed, condition string, value interface{}) *ModelWhere {
|
||||
return &ModelWhere{
|
||||
Condition: filed + " " + condition + " ?",
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
|
||||
func NewWhereValue(value interface{}) *ModelWhere {
|
||||
return &ModelWhere{
|
||||
Condition: "",
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
|
||||
func NewWhereFindInSet(filed string, value interface{}) *ModelWhere {
|
||||
return &ModelWhere{
|
||||
Condition: fmt.Sprintf("FIND_IN_SET(?, %s)", filed),
|
||||
Value: value,
|
||||
}
|
||||
}
|
||||
|
||||
func NewWhereSectionAt(filed string, value []string) []*ModelWhere {
|
||||
return []*ModelWhere{
|
||||
&ModelWhere{
|
||||
Condition: filed + " >= ?",
|
||||
Value: value[0],
|
||||
},
|
||||
&ModelWhere{
|
||||
Condition: filed + " <= ?",
|
||||
Value: value[1],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func NewWhereSectionTime(filed string, value []string) []*ModelWhere {
|
||||
return []*ModelWhere{
|
||||
&ModelWhere{
|
||||
Condition: filed + " >= ?",
|
||||
Value: value[0] + " 00:00:00",
|
||||
},
|
||||
&ModelWhere{
|
||||
Condition: filed + " <= ?",
|
||||
Value: value[1] + " 23:59:59",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// NewOrder order Asc Or Desc
|
||||
func NewOrder(filed string, order OrderMode) *ModelOrder {
|
||||
return &ModelOrder{Field: filed, Mode: order}
|
||||
}
|
||||
|
||||
// NewOrderSplit param to split for order Asc Or Desc
|
||||
func NewOrderSplit(param string) *ModelOrder {
|
||||
params := strings.Split(param, "-")
|
||||
|
||||
if len(params) <= 1 {
|
||||
return nil
|
||||
}
|
||||
order := new(ModelOrder)
|
||||
|
||||
order.Field = params[0]
|
||||
order.Mode = OrderModeToAsc
|
||||
|
||||
if params[1] == OrderModeToDesc {
|
||||
order.Mode = OrderModeToDesc
|
||||
}
|
||||
return order
|
||||
}
|
Reference in New Issue
Block a user