object_table.go 2.8 KB
package models

import (
	"fmt"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
	"gorm.io/gorm"
	"gorm.io/plugin/soft_delete"
	"time"
)

type ObjectTable struct {
	Id            int                   `json:"id" gorm:"primaryKey;autoIncrement:false"`                                    // ID
	TableId       int                   `json:"tableId" gorm:"index:idx_object_table_id"`                                    //表ID
	Name          string                `json:"name"`                                                                        //表名
	TableType     string                `json:"tableType" gorm:"primaryKey;autoIncrement:false;index:idx_object_table_type"` //模块
	CompanyId     int64                 `json:"companyId" gorm:"index:idx_object_table_company"`                             //公司id
	ParentId      int                   `json:"parentId" gorm:"default:0"`                                                   //父id
	Flag          string                `json:"flag"`                                                                        //分组:Group 集合:Set
	Version       int                   `json:",omitempty"`                                                                  //版本
	IsLocal       bool                  `json:"isLocal" gorm:"default:false"`                                                //是否有本地存储
	RemoteDeleted int                   `json:"remoteDeleted"`                                                               //远端删除
	IsDel         soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"`                                    //删除标记
	CreatedAt     int64                 `json:",omitempty"`                                                                  //创建时间
	UpdatedAt     int64                 `json:",omitempty"`                                                                  //更新时间
	DeletedAt     int64                 `json:",omitempty"`                                                                  //删除时间
}

func (m *ObjectTable) TableName() string {
	return "object_table"
}

func (m *ObjectTable) BeforeCreate(tx *gorm.DB) (err error) {
	m.CreatedAt = time.Now().Unix()
	m.UpdatedAt = time.Now().Unix()
	return
}

func (m *ObjectTable) BeforeUpdate(tx *gorm.DB) (err error) {
	m.UpdatedAt = time.Now().Unix()
	return
}

func (m *ObjectTable) CacheKeyFunc() string {
	if m.Id == 0 {
		return ""
	}
	return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}

func (m *ObjectTable) CacheKeyFuncByObject(obj interface{}) string {
	if v, ok := obj.(*ObjectTable); ok {
		return v.CacheKeyFunc()
	}
	return ""
}

func (m *ObjectTable) CachePrimaryKeyFunc() string {
	if len("") == 0 {
		return ""
	}
	return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}