mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-28 08:26:36 +08:00
IP库增加制品管理/统计中相关区域名称可以显示别名
This commit is contained in:
140
internal/db/models/ip_library_artifact_dao.go
Normal file
140
internal/db/models/ip_library_artifact_dao.go
Normal file
@@ -0,0 +1,140 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/iplibrary"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iwind/TeaGo/Tea"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||
)
|
||||
|
||||
const (
|
||||
IPLibraryArtifactStateEnabled = 1 // 已启用
|
||||
IPLibraryArtifactStateDisabled = 0 // 已禁用
|
||||
)
|
||||
|
||||
type IPLibraryArtifactDAO dbs.DAO
|
||||
|
||||
func NewIPLibraryArtifactDAO() *IPLibraryArtifactDAO {
|
||||
return dbs.NewDAO(&IPLibraryArtifactDAO{
|
||||
DAOObject: dbs.DAOObject{
|
||||
DB: Tea.Env,
|
||||
Table: "edgeIPLibraryArtifacts",
|
||||
Model: new(IPLibraryArtifact),
|
||||
PkName: "id",
|
||||
},
|
||||
}).(*IPLibraryArtifactDAO)
|
||||
}
|
||||
|
||||
var SharedIPLibraryArtifactDAO *IPLibraryArtifactDAO
|
||||
|
||||
func init() {
|
||||
dbs.OnReady(func() {
|
||||
SharedIPLibraryArtifactDAO = NewIPLibraryArtifactDAO()
|
||||
})
|
||||
}
|
||||
|
||||
// EnableIPLibraryArtifact 启用条目
|
||||
func (this *IPLibraryArtifactDAO) EnableIPLibraryArtifact(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", IPLibraryArtifactStateEnabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// DisableIPLibraryArtifact 禁用条目
|
||||
func (this *IPLibraryArtifactDAO) DisableIPLibraryArtifact(tx *dbs.Tx, id int64) error {
|
||||
_, err := this.Query(tx).
|
||||
Pk(id).
|
||||
Set("state", IPLibraryArtifactStateDisabled).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// FindEnabledIPLibraryArtifact 查找启用中的条目
|
||||
func (this *IPLibraryArtifactDAO) FindEnabledIPLibraryArtifact(tx *dbs.Tx, id int64) (*IPLibraryArtifact, error) {
|
||||
result, err := this.Query(tx).
|
||||
Pk(id).
|
||||
State(IPLibraryArtifactStateEnabled).
|
||||
Find()
|
||||
if result == nil {
|
||||
return nil, err
|
||||
}
|
||||
return result.(*IPLibraryArtifact), err
|
||||
}
|
||||
|
||||
// CreateArtifact 创建制品
|
||||
func (this *IPLibraryArtifactDAO) CreateArtifact(tx *dbs.Tx, name string, fileId int64, libraryFileId int64, meta *iplibrary.Meta) (int64, error) {
|
||||
var op = NewIPLibraryArtifactOperator()
|
||||
op.Name = name
|
||||
op.FileId = fileId
|
||||
op.LibraryFileId = libraryFileId
|
||||
|
||||
metaJSON, err := json.Marshal(meta)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
op.Meta = metaJSON
|
||||
op.State = IPLibraryArtifactStateEnabled
|
||||
|
||||
var code = stringutil.Md5(utils.Sha1RandomString())[:8]
|
||||
meta.Code = code
|
||||
op.Code = code // 要比较短,方便识别
|
||||
|
||||
return this.SaveInt64(tx, op)
|
||||
}
|
||||
|
||||
// FindAllArtifacts 查找制品列表
|
||||
func (this *IPLibraryArtifactDAO) FindAllArtifacts(tx *dbs.Tx) (result []*IPLibraryArtifact, err error) {
|
||||
_, err = this.Query(tx).
|
||||
State(IPLibraryArtifactStateEnabled).
|
||||
DescPk().
|
||||
Slice(&result).
|
||||
FindAll()
|
||||
return
|
||||
}
|
||||
|
||||
// FindPublicArtifact 查找当前使用的制品
|
||||
func (this *IPLibraryArtifactDAO) FindPublicArtifact(tx *dbs.Tx) (*IPLibraryArtifact, error) {
|
||||
one, err := this.Query(tx).
|
||||
State(IPLibraryArtifactStateEnabled).
|
||||
Attr("isPublic", true).
|
||||
Result("id", "fileId", "code").
|
||||
Find()
|
||||
if err != nil || one == nil {
|
||||
return nil, err
|
||||
}
|
||||
return one.(*IPLibraryArtifact), nil
|
||||
}
|
||||
|
||||
// UpdateArtifactPublic 使用某个制品
|
||||
func (this *IPLibraryArtifactDAO) UpdateArtifactPublic(tx *dbs.Tx, artifactId int64, isPublic bool) error {
|
||||
// 取消使用
|
||||
if !isPublic {
|
||||
return this.Query(tx).
|
||||
Pk(artifactId).
|
||||
Set("isPublic", false).
|
||||
UpdateQuickly()
|
||||
}
|
||||
|
||||
// 使用
|
||||
|
||||
// 先取消别的
|
||||
err := this.Query(tx).
|
||||
Neq("id", artifactId).
|
||||
State(IPLibraryArtifactStateEnabled).
|
||||
Attr("isPublic", true).
|
||||
Set("isPublic", false).
|
||||
UpdateQuickly()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return this.Query(tx).
|
||||
Pk(artifactId).
|
||||
Set("isPublic", true).
|
||||
UpdateQuickly()
|
||||
}
|
||||
6
internal/db/models/ip_library_artifact_dao_test.go
Normal file
6
internal/db/models/ip_library_artifact_dao_test.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package models_test
|
||||
|
||||
import (
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/iwind/TeaGo/bootstrap"
|
||||
)
|
||||
32
internal/db/models/ip_library_artifact_model.go
Normal file
32
internal/db/models/ip_library_artifact_model.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package models
|
||||
|
||||
import "github.com/iwind/TeaGo/dbs"
|
||||
|
||||
// IPLibraryArtifact IP库制品
|
||||
type IPLibraryArtifact struct {
|
||||
Id uint32 `field:"id"` // ID
|
||||
Name string `field:"name"` // 名称
|
||||
FileId uint64 `field:"fileId"` // 文件ID
|
||||
LibraryFileId uint32 `field:"libraryFileId"` // IP库文件ID
|
||||
CreatedAt uint64 `field:"createdAt"` // 创建时间
|
||||
Meta dbs.JSON `field:"meta"` // 元数据
|
||||
IsPublic bool `field:"isPublic"` // 是否为公用
|
||||
Code string `field:"code"` // 代号
|
||||
State uint8 `field:"state"` // 状态
|
||||
}
|
||||
|
||||
type IPLibraryArtifactOperator struct {
|
||||
Id any // ID
|
||||
Name any // 名称
|
||||
FileId any // 文件ID
|
||||
LibraryFileId any // IP库文件ID
|
||||
CreatedAt any // 创建时间
|
||||
Meta any // 元数据
|
||||
IsPublic any // 是否为公用
|
||||
Code any // 代号
|
||||
State any // 状态
|
||||
}
|
||||
|
||||
func NewIPLibraryArtifactOperator() *IPLibraryArtifactOperator {
|
||||
return &IPLibraryArtifactOperator{}
|
||||
}
|
||||
1
internal/db/models/ip_library_artifact_model_ext.go
Normal file
1
internal/db/models/ip_library_artifact_model_ext.go
Normal file
@@ -0,0 +1 @@
|
||||
package models
|
||||
@@ -405,7 +405,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
||||
|
||||
var libraryCode = utils.Sha1RandomString() // 每次都生成新的code
|
||||
var filePath = dir + "/" + this.composeFilename(libraryFileId, libraryCode)
|
||||
writer, err := iplibrary.NewFileWriter(filePath, &iplibrary.Meta{
|
||||
var meta = &iplibrary.Meta{
|
||||
Author: "", // 将来用户可以自行填写
|
||||
CreatedAt: time.Now().Unix(),
|
||||
Countries: countries,
|
||||
@@ -413,13 +413,15 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
||||
Cities: cities,
|
||||
Towns: towns,
|
||||
Providers: providers,
|
||||
})
|
||||
}
|
||||
writer, err := iplibrary.NewFileWriter(filePath, meta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = writer.Close()
|
||||
_ = os.Remove(filePath)
|
||||
}()
|
||||
|
||||
err = writer.WriteMeta()
|
||||
@@ -576,6 +578,12 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
||||
return err
|
||||
}
|
||||
|
||||
// 添加制品
|
||||
_, err = SharedIPLibraryArtifactDAO.CreateArtifact(tx, libraryFile.Name, generatedFileId, libraryFileId, meta)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user