| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- package busmodels
- import (
- "device-manage/app/admin/models"
- orm "device-manage/common/global"
- "errors"
- "fmt"
- "strconv"
- )
- type BusUpgradeFile struct {
- UpgradeFileId int `json:"upgradeFileId" gorm:"primary_key;AUTO_INCREMENT"`
- AppId int `json:"appId"`
- GroupId int `json:"groupId"`
- FileName string `json:"fileName" gorm:"size:128;"`
- VersionName string `json:"versionName" gorm:"size:128;"`
- PkgName string `json:"pkgName" gorm:"size:128;"`
- SdkVersion string `json:"sdkVersion" gorm:"size:32;"`
- Uid string `json:"uid" gorm:"size:128;"`
- Sha1Code string `json:"sha1Code" gorm:"size:128;"`
- Size string `json:"size" gorm:"size:128;"`
- Remark string `json:"remark" gorm:"size:255;"`
- CreateBy string `json:"createBy" gorm:"size:128;"`
- UpdateBy string `json:"updateBy" gorm:"size:128;"`
- BaseModel
- }
- type QueryUpgradeParams struct {
- FileName string `json:"fileName" gorm:"size:128;"`
- AppId int `json:"appId"`
- PageSize int `json:"pageSize"`
- PageIndex int `json:"pageIndex"`
- Gids []int `json:"gids"`
- }
- func (BusUpgradeFile) TableName() string {
- return "bus_upgrade_file"
- }
- func (e *BusUpgradeFile) Get() (BusUpgradeFile, error) {
- var doc BusUpgradeFile
- table := orm.Eloquent.Table(e.TableName())
- if e.UpgradeFileId != 0 {
- table = table.Where("upgrade_file_id = ?", e.UpgradeFileId)
- }
- if err := table.First(&doc).Error; err != nil {
- return doc, err
- }
- return doc, nil
- }
- //获取最新的版本或者配置文件
- func (e *BusUpgradeFile) GetFirstFile() (BusUpgradeFile, error) {
- var doc BusUpgradeFile
- table := orm.Eloquent.Table(e.TableName())
- table = table.Where("app_id = ?", e.AppId)
- if err := table.Order("upgrade_file_id desc").Limit(1).Find(&doc).Error; err != nil {
- return doc, err
- }
- fmt.Println("GetFirstFile")
- fmt.Println(doc)
- return doc, nil
- }
- func (e *BusUpgradeFile) GetPage(pageSize int, pageIndex int, gids []int) ([]BusUpgradeFile, int, error) {
- var doc []BusUpgradeFile
- tableUpgrade := orm.Eloquent.Table(e.TableName())
- if e.FileName != "" {
- tableUpgrade = tableUpgrade.Where("file_name like ?", "%"+e.FileName+"%")
- }
- if e.AppId > 0 {
- tableUpgrade = tableUpgrade.Where("app_id = ?", e.AppId)
- }
- if len(gids) > 0 {
- tableUpgrade = tableUpgrade.Where("group_id in (?)", gids)
- }
- var count int64
- if err := tableUpgrade.Order("upgrade_file_id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Offset(-1).Limit(-1).Count(&count).Error; err != nil {
- return nil, 0, err
- }
- for k, v := range doc {
- user := models.SysUser{}
- id, _ := strconv.Atoi(v.CreateBy)
- if err := orm.Eloquent.Table("sys_user").Where("user_id = ?", id).Find(&user).Error; err == nil {
- doc[k].CreateBy = user.Username
- }
- id, _ = strconv.Atoi(v.UpdateBy)
- user2 := models.SysUser{}
- if err := orm.Eloquent.Table("sys_user").Where("user_id = ?", id).Find(&user2).Error; err == nil {
- doc[k].UpdateBy = user2.Username
- }
- }
- return doc, int(count), nil
- }
- func (e *BusUpgradeFile) GetFileInfo() (err error) {
- if err = orm.Eloquent.Table(e.TableName()).Where("uid in (?)", e.Uid).Find(&e).Error; err != nil {
- return
- }
- return
- }
- func (e *BusUpgradeFile) UploadFileCheck(uploadType string) (err error) {
- var count int64
- fmt.Println(e.AppId)
- fmt.Println(uploadType)
- if err = orm.Eloquent.Table("bus_application").Where("app_id = ?", e.AppId).Where("upgrade_type = ?", uploadType).Find(&BusApplication{}).Count(&count).Error; err != nil {
- return
- }
- if count <= 0 {
- err = errors.New("upload type error.")
- } else {
- err = nil
- }
- return
- }
- func (e *BusUpgradeFile) Insert() (id int, err error) {
- // check 用户名
- //添加数据
- var app BusApplication
- if err = orm.Eloquent.Table("bus_application").Where("app_id = ?", e.AppId).Find(&app).Error; err != nil {
- return
- }
- e.GroupId = app.GroupId
- if err = orm.Eloquent.Table(e.TableName()).Create(&e).Error; err != nil {
- return
- }
- id = e.UpgradeFileId
- return
- }
- func (e *BusUpgradeFile) Update(id int) (update BusUpgradeFile, err error) {
- if err = orm.Eloquent.Table(e.TableName()).First(&update, id).Error; err != nil {
- return
- }
- //参数1:是要修改的数据
- //参数2:是修改的数据
- if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
- return
- }
- return
- }
- //delelte log
- func (e *BusUpgradeFile) Delete(id int) (success bool, err error) {
- if err = orm.Eloquent.Table(e.TableName()).Where("upgrade_file_id = ?", id).Delete(&BusDeviceLog{}).Error; err != nil {
- success = false
- return
- }
- success = true
- return
- }
- func (e *BusUpgradeFile) BatchDelete(id []int) (Result bool, err error) {
- if err = orm.Eloquent.Table(e.TableName()).Where("upgrade_file_id in (?)", id).Delete(&BusDeviceLog{}).Error; err != nil {
- return
- }
- Result = true
- return
- }
|