package models
|
|
type App struct {
|
Id string `gorm:"column:id;primary_key;type:varchar(50);unique;not null;" json:"id"`
|
Name string `gorm:"column:name" json:"name"` //中文名称,eg:算力管理
|
Package string `gorm:"column:package;unique;not null;" json:"package"` //英文名字
|
Type string `gorm:"column:type" json:"type"` //新开页面:"1",弹框:"2"
|
Url string `gorm:"column:url" json:"url"` //eg: /view/analysisPower/
|
Title string `gorm:"column:title" json:"title"` //打开弹框或新开页面顶部显示
|
Width int `gorm:"column:width" json:"width"` //定义弹框的默认宽度
|
Height int `gorm:"column:height" json:"height"` //定义弹框的默认高度
|
IconBlob string `gorm:"column:iconBlob" json:"iconBlob"` //图片的base64
|
IconBlob2 string `gorm:"column:iconBlob2" json:"iconBlob2"` //第二套图片的base64
|
Icon string `gorm:"column:icon" json:"icon"` //图片相对路径或者是iconfont
|
Version string `gorm:"column:version;default:''" json:"version"`
|
|
CreateTime string `gorm:"column:create_time" json:"create_time"`
|
CreateBy string `gorm:"column:create_by" json:"create_by"`
|
UpdateTime string `gorm:"column:update_time" json:"update_time"`
|
UpdateBy string `gorm:"column:update_by" json:"update_by"`
|
IsDefault bool `gorm:"column:isDefault;default:0;" json:"isDefault"`
|
|
ProcName string `gorm:"column:procName" json:"procName"` //应用的后端服务名,系统监控器中需要用,由商城端维护
|
|
Price float32 `json:"price"` //商城端价格
|
CanUpOrIns bool `json:"canUpOrIns"` //是否可安装或升级
|
Platforms []PtIns `json:"platforms"` //可适配的平台信息
|
ActivateCode string `gorm:"column:activateCode" json:"activateCode"` // 激活码
|
}
|
|
type PtIns struct {
|
Platform string `json:"platform"`
|
Version string `json:"version"`
|
VGpus string `json:"vGpus"`
|
}
|
|
func (App) TableName() string {
|
return "t_app"
|
}
|
|
func (a *App) SelectById(id string) (rows int64, err error) {
|
dbSelect := db.Table(a.TableName()).Where("id=?", id).First(&a)
|
if dbSelect.Error != nil || dbSelect.RowsAffected == 0 {
|
return 0, dbSelect.Error
|
}
|
return dbSelect.RowsAffected, nil
|
}
|
|
func (a *App) Save() (err error) {
|
tx := db.Table(a.TableName()).Begin()
|
if tx.Error != nil {
|
return tx.Error
|
}
|
|
if err := tx.Save(&a).Error; err != nil {
|
tx.Rollback()
|
return err
|
}
|
|
err = tx.Commit().Error
|
if err == nil {
|
return nil
|
}
|
return err
|
}
|
|
func (a *App) Update() (result bool, err error) {
|
entity := App{}
|
|
rows, err := entity.SelectById(a.Id)
|
if err != nil {
|
return false, err
|
}
|
if rows == 0 {
|
return false, err
|
}
|
if err := db.Table("t_app").Where("id=?", a.Id).Update(&a).Error; err != nil {
|
return false, err
|
}
|
return true, nil
|
}
|
|
func (a *App) UpdateCode(appId, code string) (err error) {
|
if err := db.Table("t_app").Where("id=?", appId).Update("activateCode", code).Error; err != nil {
|
return err
|
}
|
return nil
|
}
|
|
func (a *App) DeleteById(id string) (result bool) {
|
updateResult := db.Exec("delete from "+a.TableName()+" where id=?", id)
|
if updateResult.Error != nil || updateResult.RowsAffected == 0 {
|
return false
|
}
|
return true
|
}
|
|
func (a *App) FindAll(name string) (rows []App, err error) {
|
mutex.Lock()
|
defer mutex.Unlock()
|
|
if name != "" {
|
if err := db.Table(a.TableName()).Where("name like ?", name).Order("create_time asc").Find(&rows).Error; err != nil {
|
return nil, err
|
}
|
return rows, nil
|
} else {
|
if err := db.Table(a.TableName()).Find(&rows).Error; err != nil {
|
return nil, err
|
}
|
return rows, nil
|
}
|
}
|
|
func (a *App) FindAllMap() map[string]App {
|
m := make(map[string]App)
|
list, _ := a.FindAll("")
|
if list != nil {
|
for _, s := range list {
|
m[s.Id] = s
|
}
|
}
|
return m
|
}
|
|
func (a *App) Total(inputText string) (int, error) {
|
var total int
|
sql := "select count(1) as total from " + a.TableName() + " where 1=1"
|
if inputText != "" {
|
sql += " and name like '%" + inputText + "%'"
|
}
|
err := db.Raw(sql).Count(&total).Error
|
if err != nil {
|
return 0, err
|
}
|
return total, nil
|
}
|
|
func (a *App) FindByPage(inputText string, page int, size int) (list []App, err error) {
|
offset := (page - 1) * size
|
sql := "select * from " + a.TableName() + " where 1=1"
|
|
if inputText != "" {
|
sql += " and name like '%" + inputText + "%'"
|
}
|
sql += " order by create_time desc"
|
err = db.Raw(sql).Offset(offset).Limit(size).Find(&list).Error
|
if err != nil {
|
return nil, err
|
}
|
return list, nil
|
}
|