mysql_mobile_machine.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. package dao
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "strconv"
  6. "go-common/app/admin/ep/merlin/model"
  7. "go-common/library/ecode"
  8. pkgerr "github.com/pkg/errors"
  9. )
  10. // FindMobileMachineBySerial Find Device FarmBy Serial.
  11. func (d *Dao) FindMobileMachineBySerial(serial string) (mobileMachine *model.MobileMachine, err error) {
  12. mobileMachine = &model.MobileMachine{}
  13. if err = d.db.Where("serial = ?", serial).Find(mobileMachine).Error; err == ecode.NothingFound {
  14. err = nil
  15. }
  16. return
  17. }
  18. // DeleteMobileMachineByUUID Delete Mobile Machine By UUID.
  19. func (d *Dao) DeleteMobileMachineByUUID(UUID string) (delCnt int, err error) {
  20. err = pkgerr.WithStack(d.db.Model(&model.MobileMachine{}).Where("uuid <> ? and action <>?", UUID, model.MBHostDel).Count(&delCnt).Update("action", model.MBOffline).Error)
  21. return
  22. }
  23. // DeleteMobileMachineNotInHost Delete Mobile Machine Not In Host.
  24. func (d *Dao) DeleteMobileMachineNotInHost(hostList []string) (delCnt int, err error) {
  25. err = pkgerr.WithStack(d.db.Model(&model.MobileMachine{}).Where("host not in (?)", hostList).Count(&delCnt).Update("action", model.MBHostDel).Error)
  26. return
  27. }
  28. // FindMobileMachineByID Find Mobile Machine By Id.
  29. func (d *Dao) FindMobileMachineByID(deviceID int64) (mobileMachine *model.MobileMachine, err error) {
  30. mobileMachine = &model.MobileMachine{}
  31. err = pkgerr.WithStack(d.db.Where("id = ?", deviceID).Find(mobileMachine).Error)
  32. return
  33. }
  34. // InsertMobileMachine Insert Device Farm.
  35. func (d *Dao) InsertMobileMachine(mobileMachine *model.MobileMachine) (err error) {
  36. return pkgerr.WithStack(d.db.Create(&mobileMachine).Error)
  37. }
  38. // UpdateMobileMachineWsurlAndState Update Mobile Machine Wsurl And State.
  39. func (d *Dao) UpdateMobileMachineWsurlAndState(mobileMachine *model.MobileMachine) (err error) {
  40. return pkgerr.WithStack(d.db.Model(&model.MobileMachine{}).Where("serial=?", mobileMachine.Serial).Updates(map[string]interface{}{"wsurl": mobileMachine.WsURL, "state": mobileMachine.State, "upload_url": mobileMachine.UploadURL}).Error)
  41. }
  42. // UpdateMobileMachine Update Mobile Machine.
  43. func (d *Dao) UpdateMobileMachine(mobileMachine *model.MobileMachine) (err error) {
  44. return pkgerr.WithStack(d.db.Model(&model.MobileMachine{}).Where("serial=?", mobileMachine.Serial).Updates(mobileMachine).Error)
  45. }
  46. // UpdateMobileMachineByRelease Update Mobile Machine By Release.
  47. func (d *Dao) UpdateMobileMachineByRelease(mobileMachine *model.MobileMachine) (err error) {
  48. return pkgerr.WithStack(d.db.Model(&model.MobileMachine{}).Where("serial=?", mobileMachine.Serial).Updates(map[string]interface{}{"username": mobileMachine.Username, "end_time": mobileMachine.EndTime}).Error)
  49. }
  50. //FindMobileMachines Find Mobile Machines.
  51. func (d *Dao) FindMobileMachines(queryRequest *model.QueryMobileDeviceRequest) (total int64, mobileMachines []*model.MobileMachine, err error) {
  52. gDB := d.db.Model(&model.MobileMachine{}).Where("action> ? ", model.MBHostDel)
  53. if queryRequest.MobileID > 0 {
  54. gDB = gDB.Where("id=?", queryRequest.MobileID)
  55. }
  56. if queryRequest.Serial != "" {
  57. gDB = gDB.Where("serial=?", queryRequest.Serial)
  58. }
  59. if queryRequest.Name != "" {
  60. gDB = gDB.Where("name=?", queryRequest.Name)
  61. }
  62. if queryRequest.Username != "" {
  63. gDB = gDB.Where("username=?", queryRequest.Username)
  64. }
  65. if queryRequest.OwnerName != "" {
  66. gDB = gDB.Where("owner_name=?", queryRequest.OwnerName)
  67. }
  68. if queryRequest.CPU != "" {
  69. gDB = gDB.Where("cpu=?", queryRequest.CPU)
  70. }
  71. if queryRequest.Version != "" {
  72. gDB = gDB.Where("version=?", queryRequest.Version)
  73. }
  74. if queryRequest.Mode != "" {
  75. gDB = gDB.Where("mode=?", queryRequest.Mode)
  76. }
  77. if queryRequest.Type > -1 {
  78. gDB = gDB.Where("type=?", queryRequest.Type)
  79. }
  80. if queryRequest.State != "" {
  81. gDB = gDB.Where("state=?", queryRequest.State)
  82. }
  83. if queryRequest.Online {
  84. gDB = gDB.Where("action=?", model.MBOnline)
  85. }
  86. if queryRequest.Usage == model.MBFree {
  87. gDB = gDB.Where("username='' and action=?", model.MBOnline)
  88. }
  89. if queryRequest.Usage == model.MBInUse {
  90. gDB = gDB.Where("username<>'' and action=?", model.MBOnline)
  91. }
  92. if queryRequest.Usage == model.MBNoConnect {
  93. gDB = gDB.Where("action=?", model.MBOffline)
  94. }
  95. if err = pkgerr.WithStack(gDB.Count(&total).Error); err != nil {
  96. return
  97. }
  98. //先出action>0的,未连接的排在后面
  99. err = pkgerr.WithStack(gDB.Order("action desc,id desc").Offset((queryRequest.PageNum - 1) * queryRequest.PageSize).Limit(queryRequest.PageSize).Find(&mobileMachines).Error)
  100. return
  101. }
  102. // FindMobileMachineCategory Find Mobile Machine Category.
  103. func (d *Dao) FindMobileMachineCategory(isShowOffline bool) (mobileCategory *model.MobileCategory, err error) {
  104. var (
  105. rows *sql.Rows
  106. cpus []string
  107. versions []string
  108. modes []string
  109. states []string
  110. types []int
  111. usages []int
  112. sqlGroupTpl string
  113. inUseCnt int
  114. freeCnt int
  115. noConCnt int
  116. )
  117. if isShowOffline {
  118. sqlGroupTpl = "select %s from mobile_machines where action> " + strconv.Itoa(model.MBHostDel) + " group by %s"
  119. } else {
  120. sqlGroupTpl = "select %s from mobile_machines where action>-1 group by %s"
  121. }
  122. tx := d.db.Begin()
  123. if err = tx.Error; err != nil {
  124. return
  125. }
  126. //cpu
  127. if rows, err = tx.Raw(fmt.Sprintf(sqlGroupTpl, "cpu", "cpu")).Rows(); err != nil {
  128. return
  129. }
  130. for rows.Next() {
  131. var cpu string
  132. if err = rows.Scan(&cpu); err != nil {
  133. return
  134. }
  135. cpus = append(cpus, cpu)
  136. }
  137. //version
  138. if rows, err = tx.Raw(fmt.Sprintf(sqlGroupTpl, "version", "version")).Rows(); err != nil {
  139. return
  140. }
  141. for rows.Next() {
  142. var version string
  143. if err = rows.Scan(&version); err != nil {
  144. return
  145. }
  146. versions = append(versions, version)
  147. }
  148. //mode
  149. if rows, err = tx.Raw(fmt.Sprintf(sqlGroupTpl, "mode", "mode")).Rows(); err != nil {
  150. return
  151. }
  152. for rows.Next() {
  153. var mode string
  154. if err = rows.Scan(&mode); err != nil {
  155. return
  156. }
  157. modes = append(modes, mode)
  158. }
  159. //state
  160. if rows, err = tx.Raw(fmt.Sprintf(sqlGroupTpl, "state", "state")).Rows(); err != nil {
  161. return
  162. }
  163. for rows.Next() {
  164. var state string
  165. if err = rows.Scan(&state); err != nil {
  166. return
  167. }
  168. states = append(states, state)
  169. }
  170. //type
  171. if rows, err = tx.Raw(fmt.Sprintf(sqlGroupTpl, "type", "type")).Rows(); err != nil {
  172. return
  173. }
  174. for rows.Next() {
  175. var stype int
  176. if err = rows.Scan(&stype); err != nil {
  177. return
  178. }
  179. types = append(types, stype)
  180. }
  181. //usage
  182. if err = tx.Model(&model.MobileMachine{}).Where("username<>'' and action>0").Count(&inUseCnt).Error; err != nil {
  183. return
  184. }
  185. if err = tx.Model(&model.MobileMachine{}).Where("username='' and action>0").Count(&freeCnt).Error; err != nil {
  186. return
  187. }
  188. if err = tx.Model(&model.MobileMachine{}).Where("action<0").Count(&noConCnt).Error; err != nil {
  189. return
  190. }
  191. if inUseCnt > 0 {
  192. usages = append(usages, model.MBInUse)
  193. }
  194. if freeCnt > 0 {
  195. usages = append(usages, model.MBFree)
  196. }
  197. if noConCnt > 0 {
  198. usages = append(usages, model.MBNoConnect)
  199. }
  200. mobileCategory = &model.MobileCategory{}
  201. mobileCategory.CPUs = cpus
  202. mobileCategory.Versions = versions
  203. mobileCategory.Modes = modes
  204. mobileCategory.States = states
  205. mobileCategory.Types = types
  206. mobileCategory.Usages = usages
  207. if err = tx.Commit().Error; err != nil {
  208. tx.Rollback()
  209. }
  210. return
  211. }
  212. // FindAllMobileImages Find All Mobile Images.
  213. func (d *Dao) FindAllMobileImages() (mobileImages []*model.MobileImage, err error) {
  214. err = pkgerr.WithStack(d.db.Model(&model.MobileImage{}).Find(&mobileImages).Error)
  215. return
  216. }
  217. // FindMobileImageByMode Find Mobile Image By Mode.
  218. func (d *Dao) FindMobileImageByMode(mode string) (mobileImage *model.MobileImage, err error) {
  219. mobileImage = &model.MobileImage{}
  220. if err = d.db.Where("mode=?", mode).First(mobileImage).Error; err == ecode.NothingFound {
  221. err = nil
  222. }
  223. return
  224. }
  225. // LendOutMobileMachine Lend Out Mobile.
  226. func (d *Dao) LendOutMobileMachine(deviceID int64, serial, username string) (err error) {
  227. tx := d.db.Begin()
  228. if err = tx.Error; err != nil {
  229. return
  230. }
  231. mobileMachineLog := &model.MobileMachineLog{
  232. OperateType: model.MBLendOutLog,
  233. Username: username,
  234. MachineID: deviceID,
  235. OperateResult: model.OperationSuccessForMachineLog,
  236. }
  237. if err = tx.Create(mobileMachineLog).Error; err != nil {
  238. tx.Rollback()
  239. return
  240. }
  241. if err = tx.Model(&model.MobileMachine{}).Where("serial=?", serial).Update("is_lendout", model.MBLendOut).Error; err != nil {
  242. tx.Rollback()
  243. return
  244. }
  245. if err = tx.Commit().Error; err != nil {
  246. tx.Rollback()
  247. }
  248. return
  249. }
  250. // ReturnMobileMachine return Mobile.
  251. func (d *Dao) ReturnMobileMachine(deviceID int64, serial, username string) (err error) {
  252. tx := d.db.Begin()
  253. if err = tx.Error; err != nil {
  254. return
  255. }
  256. mobileMachineLog := &model.MobileMachineLog{
  257. OperateType: model.MBReturnLog,
  258. Username: username,
  259. MachineID: deviceID,
  260. OperateResult: model.OperationSuccessForMachineLog,
  261. }
  262. if err = tx.Create(mobileMachineLog).Error; err != nil {
  263. tx.Rollback()
  264. return
  265. }
  266. if err = tx.Model(&model.MobileMachine{}).Where("serial=?", serial).Update("is_lendout", model.MBOnSite).Error; err != nil {
  267. tx.Rollback()
  268. return
  269. }
  270. if err = tx.Commit().Error; err != nil {
  271. tx.Rollback()
  272. }
  273. return
  274. }