zhangzengfei
2023-11-28 3a706d3378aa3626501370352963883fd2783558
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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
}