user_resource.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "go-common/app/service/live/resource/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. "time"
  9. "github.com/jinzhu/gorm"
  10. )
  11. var (
  12. addSql = "INSERT INTO `user_resource`(`res_type`,`custom_id`,`title`,`url`,`weight`,`status`,`creator`) values (?,?,?,?,?,?,?);"
  13. rowFields = "`id`, `res_type`,`custom_id`,`title`,`url`,`weight`,`status`,`creator`,UNIX_TIMESTAMP(`ctime`), UNIX_TIMESTAMP(`mtime`)"
  14. )
  15. // AddUserResource 添加用户资源到DB
  16. func (d *Dao) AddUserResource(c context.Context, res *model.UserResource) (newRes model.UserResource, err error) {
  17. if res == nil {
  18. return
  19. }
  20. var reply sql.Result
  21. if _, err = d.db.Begin(c); err != nil {
  22. log.Error("初始化DB错误(%v)", err)
  23. return
  24. }
  25. reply, err = d.db.Exec(c, addSql, res.ResType, res.CustomID, res.Title, res.URL, res.Weight, res.Status, res.Creator)
  26. if err != nil {
  27. log.Error("执行SQL语句 err: %v", err)
  28. return
  29. }
  30. lastID_, _ := reply.LastInsertId()
  31. newRes, err = d.GetUserResourceInfoByID(c, int32(lastID_))
  32. return
  33. }
  34. // EditUserResource 编辑已有资源
  35. func (d *Dao) EditUserResource(c context.Context, resType int32, customID int32, update map[string]interface{}) (effectRow int32, newRes model.UserResource, err error) {
  36. if update == nil {
  37. return
  38. }
  39. var tx = d.rsDB
  40. tableInfo := &model.UserResource{}
  41. var reply = tx.Model(tableInfo).
  42. Where("`res_type` = ? AND `custom_id` = ?", resType, customID).
  43. Update(update)
  44. log.Info("effected rows: %d, res_type : %d custom_id : %d", reply.RowsAffected, resType, customID)
  45. if reply.Error != nil {
  46. log.Error("resource.editResource error: %v", err)
  47. return
  48. }
  49. effectRow = int32(reply.RowsAffected)
  50. newRes, err = d.GetUserResourceInfo(c, resType, customID)
  51. return
  52. }
  53. // SetUserResourceStatus 设置资源状态
  54. func (d *Dao) SetUserResourceStatus(c context.Context, resType int32, customID int32, status int32) (effectRow int32, err error) {
  55. update := make(map[string]interface{})
  56. update["status"] = status
  57. effectRow, _, err = d.EditUserResource(c, resType, customID, update)
  58. if err != nil {
  59. log.Error("修改资源状态: %v", err)
  60. }
  61. return
  62. }
  63. // GetMaxCustomID 根据资源类型获取当前最大的资源ID
  64. func (d *Dao) GetMaxCustomID(c context.Context, resType int32) (maxCustomID int32, err error) {
  65. tableInfo := &model.UserResource{}
  66. var ret sql.NullInt64
  67. err = d.rsDB.Model(tableInfo).Debug().
  68. Select("max(custom_id) as mcid").
  69. Where("res_type=?", resType).
  70. Row().Scan(&ret)
  71. if err != nil {
  72. log.Error("查找最大的资源ID res_type : %d : %v", resType, err)
  73. return
  74. }
  75. maxCustomID = int32(ret.Int64)
  76. log.Info("类型为 %d 最大的资源ID是 %d", resType, maxCustomID)
  77. return
  78. }
  79. // getRowResult Helper方法
  80. func getRowResult(queryResult *gorm.DB) (res model.UserResource, err error) {
  81. var count int32
  82. err = queryResult.Count(&count).Error
  83. if err != nil {
  84. log.Error("user_resource.GetUserResourceInfoByID %v", err)
  85. err = ecode.SeltResErr
  86. return
  87. }
  88. if count == 0 {
  89. log.Info("user_resource.getRowResult 查询结果为空")
  90. err = ecode.SeltResErr
  91. return
  92. }
  93. var retID, retResType, retCustomID, retWeight, retStatus, retCtime, retMtime sql.NullInt64
  94. var retTitle, retURL, retCreator sql.NullString
  95. err = queryResult.Row().Scan(&retID, &retResType, &retCustomID, &retTitle, &retURL, &retWeight, &retStatus, &retCreator, &retCtime, &retMtime)
  96. if err != nil {
  97. log.Error("resource.GetUserResourceInfoByID error: %v", err)
  98. err = ecode.SeltResErr
  99. return
  100. }
  101. res.ID = int32(retID.Int64)
  102. res.ResType = int32(retResType.Int64)
  103. res.CustomID = int32(retCustomID.Int64)
  104. res.Title = retTitle.String
  105. res.URL = retURL.String
  106. res.Weight = int32(retWeight.Int64)
  107. res.Status = int32(retStatus.Int64)
  108. res.Creator = retCreator.String
  109. res.Ctime = time.Unix(retCtime.Int64, 0)
  110. res.Ctime = time.Unix(retMtime.Int64, 0)
  111. return
  112. }
  113. // GetUserResourceInfo 获取单个配置
  114. func (d *Dao) GetUserResourceInfo(c context.Context, resType int32, customID int32) (res model.UserResource, err error) {
  115. tableInfo := &model.UserResource{}
  116. queryResult := d.rsDBReader.Model(tableInfo).Select(rowFields).
  117. Where("res_type=? AND custom_id=?", resType, customID)
  118. res, err = getRowResult(queryResult)
  119. return
  120. }
  121. // GetUserResourceInfoByID 根据ID获取单个配置
  122. func (d *Dao) GetUserResourceInfoByID(c context.Context, id int32) (res model.UserResource, err error) {
  123. tableInfo := &model.UserResource{}
  124. queryResult := d.rsDBReader.Model(tableInfo).Select(rowFields).
  125. Where("id=?", id)
  126. res, err = getRowResult(queryResult)
  127. return
  128. }
  129. // ListUserResourceInfo 获取配置列表
  130. func (d *Dao) ListUserResourceInfo(c context.Context, resType int32, page int32, pageSize int32) (list []model.UserResource, err error) {
  131. var tx = d.rsDBReader
  132. tableInfo := &model.UserResource{}
  133. err = tx.Model(tableInfo).
  134. Select("`id`, `res_type`,`custom_id`,`title`,`url`,`weight`,`status`,`creator`,`ctime`, `mtime`").
  135. Where("res_type=?", resType).
  136. Order("id ASC").
  137. Limit(pageSize).
  138. Offset((page - 1) * pageSize).
  139. Find(&list).Error
  140. if err != nil {
  141. log.Error("resource.editResource error: %v", err)
  142. return
  143. }
  144. return
  145. }