upgradefile.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package busmodels
  2. import (
  3. "device-manage/app/admin/models"
  4. orm "device-manage/common/global"
  5. "errors"
  6. "fmt"
  7. "strconv"
  8. )
  9. type BusUpgradeFile struct {
  10. UpgradeFileId int `json:"upgradeFileId" gorm:"primary_key;AUTO_INCREMENT"`
  11. AppId int `json:"appId"`
  12. GroupId int `json:"groupId"`
  13. FileName string `json:"fileName" gorm:"size:128;"`
  14. VersionName string `json:"versionName" gorm:"size:128;"`
  15. PkgName string `json:"pkgName" gorm:"size:128;"`
  16. SdkVersion string `json:"sdkVersion" gorm:"size:32;"`
  17. Uid string `json:"uid" gorm:"size:128;"`
  18. Sha1Code string `json:"sha1Code" gorm:"size:128;"`
  19. Size string `json:"size" gorm:"size:128;"`
  20. Remark string `json:"remark" gorm:"size:255;"`
  21. CreateBy string `json:"createBy" gorm:"size:128;"`
  22. UpdateBy string `json:"updateBy" gorm:"size:128;"`
  23. BaseModel
  24. }
  25. type QueryUpgradeParams struct {
  26. FileName string `json:"fileName" gorm:"size:128;"`
  27. AppId int `json:"appId"`
  28. PageSize int `json:"pageSize"`
  29. PageIndex int `json:"pageIndex"`
  30. Gids []int `json:"gids"`
  31. }
  32. func (BusUpgradeFile) TableName() string {
  33. return "bus_upgrade_file"
  34. }
  35. func (e *BusUpgradeFile) Get() (BusUpgradeFile, error) {
  36. var doc BusUpgradeFile
  37. table := orm.Eloquent.Table(e.TableName())
  38. if e.UpgradeFileId != 0 {
  39. table = table.Where("upgrade_file_id = ?", e.UpgradeFileId)
  40. }
  41. if err := table.First(&doc).Error; err != nil {
  42. return doc, err
  43. }
  44. return doc, nil
  45. }
  46. //获取最新的版本或者配置文件
  47. func (e *BusUpgradeFile) GetFirstFile() (BusUpgradeFile, error) {
  48. var doc BusUpgradeFile
  49. table := orm.Eloquent.Table(e.TableName())
  50. table = table.Where("app_id = ?", e.AppId)
  51. if err := table.Order("upgrade_file_id desc").Limit(1).Find(&doc).Error; err != nil {
  52. return doc, err
  53. }
  54. fmt.Println("GetFirstFile")
  55. fmt.Println(doc)
  56. return doc, nil
  57. }
  58. func (e *BusUpgradeFile) GetPage(pageSize int, pageIndex int, gids []int) ([]BusUpgradeFile, int, error) {
  59. var doc []BusUpgradeFile
  60. tableUpgrade := orm.Eloquent.Table(e.TableName())
  61. if e.FileName != "" {
  62. tableUpgrade = tableUpgrade.Where("file_name like ?", "%"+e.FileName+"%")
  63. }
  64. if e.AppId > 0 {
  65. tableUpgrade = tableUpgrade.Where("app_id = ?", e.AppId)
  66. }
  67. if len(gids) > 0 {
  68. tableUpgrade = tableUpgrade.Where("group_id in (?)", gids)
  69. }
  70. var count int64
  71. 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 {
  72. return nil, 0, err
  73. }
  74. for k, v := range doc {
  75. user := models.SysUser{}
  76. id, _ := strconv.Atoi(v.CreateBy)
  77. if err := orm.Eloquent.Table("sys_user").Where("user_id = ?", id).Find(&user).Error; err == nil {
  78. doc[k].CreateBy = user.Username
  79. }
  80. id, _ = strconv.Atoi(v.UpdateBy)
  81. user2 := models.SysUser{}
  82. if err := orm.Eloquent.Table("sys_user").Where("user_id = ?", id).Find(&user2).Error; err == nil {
  83. doc[k].UpdateBy = user2.Username
  84. }
  85. }
  86. return doc, int(count), nil
  87. }
  88. func (e *BusUpgradeFile) GetFileInfo() (err error) {
  89. if err = orm.Eloquent.Table(e.TableName()).Where("uid in (?)", e.Uid).Find(&e).Error; err != nil {
  90. return
  91. }
  92. return
  93. }
  94. func (e *BusUpgradeFile) UploadFileCheck(uploadType string) (err error) {
  95. var count int64
  96. fmt.Println(e.AppId)
  97. fmt.Println(uploadType)
  98. if err = orm.Eloquent.Table("bus_application").Where("app_id = ?", e.AppId).Where("upgrade_type = ?", uploadType).Find(&BusApplication{}).Count(&count).Error; err != nil {
  99. return
  100. }
  101. if count <= 0 {
  102. err = errors.New("upload type error.")
  103. } else {
  104. err = nil
  105. }
  106. return
  107. }
  108. func (e *BusUpgradeFile) Insert() (id int, err error) {
  109. // check 用户名
  110. //添加数据
  111. var app BusApplication
  112. if err = orm.Eloquent.Table("bus_application").Where("app_id = ?", e.AppId).Find(&app).Error; err != nil {
  113. return
  114. }
  115. e.GroupId = app.GroupId
  116. if err = orm.Eloquent.Table(e.TableName()).Create(&e).Error; err != nil {
  117. return
  118. }
  119. id = e.UpgradeFileId
  120. return
  121. }
  122. func (e *BusUpgradeFile) Update(id int) (update BusUpgradeFile, err error) {
  123. if err = orm.Eloquent.Table(e.TableName()).First(&update, id).Error; err != nil {
  124. return
  125. }
  126. //参数1:是要修改的数据
  127. //参数2:是修改的数据
  128. if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
  129. return
  130. }
  131. return
  132. }
  133. //delelte log
  134. func (e *BusUpgradeFile) Delete(id int) (success bool, err error) {
  135. if err = orm.Eloquent.Table(e.TableName()).Where("upgrade_file_id = ?", id).Delete(&BusDeviceLog{}).Error; err != nil {
  136. success = false
  137. return
  138. }
  139. success = true
  140. return
  141. }
  142. func (e *BusUpgradeFile) BatchDelete(id []int) (Result bool, err error) {
  143. if err = orm.Eloquent.Table(e.TableName()).Where("upgrade_file_id in (?)", id).Delete(&BusDeviceLog{}).Error; err != nil {
  144. return
  145. }
  146. Result = true
  147. return
  148. }