82 lines
3.7 KiB
Go
82 lines
3.7 KiB
Go
package models
|
||
|
||
import (
|
||
"sync"
|
||
"time"
|
||
)
|
||
|
||
// SystemFile 附件表 yz_system_files
|
||
//
|
||
// 归属语义(2026-09-09 文件存储分层改造后明确):
|
||
// - tid : 租户 ID
|
||
// - uid : 上传者 ID
|
||
// - tuid : 归属用户 ID(scope=user 时必填;scope=tenant 时为 NULL)
|
||
// - source: 来源端 backend / platform
|
||
// - scope : tenant-租户共享 / user-用户个人
|
||
// - object_key: 存储相对路径,不含域名,如 backend/234573/67091493/2026/09/09/xxx.png
|
||
type SystemFile struct {
|
||
ID uint64 `orm:"column(id);pk;auto" json:"id"`
|
||
Tid uint64 `orm:"column(tid)" json:"tid"`
|
||
Uid *uint64 `orm:"column(uid);null" json:"uid"`
|
||
Tuid *uint64 `orm:"column(tuid);null" json:"tuid"`
|
||
Name string `orm:"column(name);size(255)" json:"name"`
|
||
Type uint8 `orm:"column(type);default(2)" json:"type"`
|
||
Cate uint64 `orm:"column(cate);default(0)" json:"cate"`
|
||
Size uint64 `orm:"column(size);default(0)" json:"size"`
|
||
Src string `orm:"column(src);size(512)" json:"src"`
|
||
Uploader uint64 `orm:"column(uploader);default(0)" json:"uploader"`
|
||
Md5 string `orm:"column(md5);size(32)" json:"md5"`
|
||
Source string `orm:"column(source);size(16);default(backend)" json:"source"`
|
||
Scope string `orm:"column(scope);size(16);default(tenant)" json:"scope"`
|
||
Storage string `orm:"column(storage);size(16);default()" json:"storage"`
|
||
ObjectKey string `orm:"column(object_key);size(512);default()" json:"object_key"`
|
||
CreateTime time.Time `orm:"column(create_time);type(datetime);auto_now_add" json:"create_time"`
|
||
UpdateTime *time.Time `orm:"column(update_time);type(datetime);null;auto_now" json:"update_time"`
|
||
DeleteTime *time.Time `orm:"column(delete_time);type(datetime);null" json:"delete_time"`
|
||
}
|
||
|
||
func (m *SystemFile) TableName() string {
|
||
return "yz_system_files"
|
||
}
|
||
|
||
// 归属与存储字段的取值常量
|
||
const (
|
||
FileSourceBackend = "backend" // 租户后台端
|
||
FileSourcePlatform = "platform" // 平台端
|
||
|
||
FileScopeTenant = "tenant" // 租户共享文件
|
||
FileScopeUser = "user" // 用户个人文件
|
||
)
|
||
|
||
var systemFileStorageColumnsOnce sync.Once
|
||
|
||
// EnsureSystemFileStorageColumns 为附件表补齐文件存储分层改造所需字段。
|
||
// 历史库可能早于该特性建表,字段/索引已存在时 MySQL 报 duplicate,统一忽略,不阻断主流程。
|
||
// 写入 yz_system_files 之前必须调用,否则 INSERT 会因 Unknown column 失败。
|
||
func EnsureSystemFileStorageColumns() {
|
||
if Orm == nil {
|
||
return
|
||
}
|
||
systemFileStorageColumnsOnce.Do(func() {
|
||
alters := []string{
|
||
"ALTER TABLE yz_system_files ADD COLUMN source varchar(16) NOT NULL DEFAULT 'backend' COMMENT '来源端: backend-租户后台 platform-平台端'",
|
||
"ALTER TABLE yz_system_files ADD COLUMN scope varchar(16) NOT NULL DEFAULT 'tenant' COMMENT '归属: tenant-租户共享 user-用户个人'",
|
||
"ALTER TABLE yz_system_files ADD COLUMN storage varchar(16) NOT NULL DEFAULT '' COMMENT '存储类型: local/qiniu'",
|
||
"ALTER TABLE yz_system_files ADD COLUMN object_key varchar(512) NOT NULL DEFAULT '' COMMENT '存储相对路径(不含域名)'",
|
||
}
|
||
for _, sql := range alters {
|
||
_, _ = Orm.Raw(sql).Exec()
|
||
}
|
||
// 索引:MD5 精确去重 + 归属列表过滤
|
||
// md5 在部分历史库中是 TEXT 类型,MySQL 不允许对 TEXT/BLOB 建整列索引,
|
||
// 故使用前缀长度 md5(32)(MD5 十六进制串固定 32 字符)。
|
||
indexes := []string{
|
||
"ALTER TABLE yz_system_files ADD KEY idx_file_dedup (source, scope, tid, tuid, md5(32))",
|
||
"ALTER TABLE yz_system_files ADD KEY idx_file_owner (source, tid, scope, tuid, delete_time)",
|
||
}
|
||
for _, sql := range indexes {
|
||
_, _ = Orm.Raw(sql).Exec()
|
||
}
|
||
})
|
||
}
|