蛋蛋星球RabbitMq消费项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

171 lines
4.4 KiB

  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils"
  5. "applet/app/utils/logx"
  6. "errors"
  7. "fmt"
  8. "reflect"
  9. "xorm.io/xorm"
  10. )
  11. // BatchSelectImGroups 批量查询数据 TODO::和下面的方法重复了,建议采用下面的 `ImGroupFindByParams` 方法
  12. func BatchSelectImGroups(Db *xorm.Engine, params map[string]interface{}) (*[]model.ImGroup, error) {
  13. var ImGroupData []model.ImGroup
  14. if err := Db.In(utils.AnyToString(params["key"]), params["value"]).
  15. Find(&ImGroupData); err != nil {
  16. return nil, logx.Warn(err)
  17. }
  18. return &ImGroupData, nil
  19. }
  20. // ImGroupInsert 插入单条数据
  21. func ImGroupInsert(Db *xorm.Engine, ImGroup *model.ImGroup) (int, error) {
  22. _, err := Db.InsertOne(ImGroup)
  23. if err != nil {
  24. return 0, err
  25. }
  26. return ImGroup.Id, nil
  27. }
  28. // BatchAddImGroups 批量新增数据
  29. func BatchAddImGroups(Db *xorm.Engine, ImGroupData []*model.ImGroup) (int64, error) {
  30. affected, err := Db.Insert(ImGroupData)
  31. if err != nil {
  32. return 0, err
  33. }
  34. return affected, nil
  35. }
  36. func GetImGroupCount(Db *xorm.Engine) int {
  37. var ImGroup model.ImGroup
  38. session := Db.Where("")
  39. count, err := session.Count(&ImGroup)
  40. if err != nil {
  41. return 0
  42. }
  43. return int(count)
  44. }
  45. // ImGroupDelete 删除记录
  46. func ImGroupDelete(Db *xorm.Engine, id interface{}) (int64, error) {
  47. if reflect.TypeOf(id).Kind() == reflect.Slice {
  48. return Db.In("id", id).Delete(model.ImGroup{})
  49. } else {
  50. return Db.Where("id = ?", id).Delete(model.ImGroup{})
  51. }
  52. }
  53. // ImGroupUpdate 更新记录
  54. func ImGroupUpdate(Db *xorm.Engine, id interface{}, ImGroup *model.ImGroup, forceColums ...string) (int64, error) {
  55. var (
  56. affected int64
  57. err error
  58. )
  59. if forceColums != nil {
  60. affected, err = Db.Where("id=?", id).Cols(forceColums...).Update(ImGroup)
  61. } else {
  62. affected, err = Db.Where("id=?", id).Update(ImGroup)
  63. }
  64. if err != nil {
  65. return 0, err
  66. }
  67. return affected, nil
  68. }
  69. // ImGroupGetOneByParams 通过传入的参数查询数据(单条)
  70. func ImGroupGetOneByParams(Db *xorm.Engine, kind int, params map[string]interface{}) (*model.ImGroup, error) {
  71. var m model.ImGroup
  72. var query = fmt.Sprintf("%s =?", params["key"])
  73. if has, err := Db.Where(query, params["value"]).And("kind = ?", kind).Get(&m); err != nil || has == false {
  74. return nil, logx.Error(err)
  75. }
  76. return &m, nil
  77. }
  78. func ImGroupGetOneByParamsForOfficial(Db *xorm.Engine, kind int) (*model.ImGroup, error) {
  79. var m model.ImGroup
  80. has, err := Db.Where("kind = ?", kind).Get(&m)
  81. if err != nil {
  82. return nil, logx.Error(err)
  83. }
  84. if has == false {
  85. return nil, nil
  86. }
  87. return &m, nil
  88. }
  89. func ImGroupGetOneByParamsForFans(Db *xorm.Engine, kind int, uid int64) (*model.ImGroup, error) {
  90. var m model.ImGroup
  91. has, err := Db.Where("kind = ?", kind).And("uid =?", uid).Get(&m)
  92. if err != nil {
  93. return nil, logx.Error(err)
  94. }
  95. if has == false {
  96. return nil, nil
  97. }
  98. return &m, nil
  99. }
  100. func ImGroupFindByParamsByKind(Db *xorm.Engine, kind int, params map[string]interface{}) (*[]model.ImGroup, error) {
  101. var m []model.ImGroup
  102. if params["value"] == nil {
  103. return nil, errors.New("参数有误")
  104. }
  105. if params["key"] == nil {
  106. //查询全部数据
  107. err := Db.Find(&m)
  108. if err != nil {
  109. return nil, logx.Error(err)
  110. }
  111. return &m, nil
  112. } else {
  113. if reflect.TypeOf(params["value"]).Kind() == reflect.Slice {
  114. //指定In查询
  115. if err := Db.In(utils.AnyToString(params["key"]), params["value"]).And("kind =?", kind).Find(&m); err != nil {
  116. return nil, logx.Warn(err)
  117. }
  118. return &m, nil
  119. } else {
  120. var query = fmt.Sprintf("%s =?", params["key"])
  121. err := Db.Where(query, params["value"]).And("kind =?", kind).Find(&m)
  122. if err != nil {
  123. return nil, logx.Error(err)
  124. }
  125. return &m, nil
  126. }
  127. }
  128. }
  129. // ImGroupFindByParams 通过传入的参数查询数据(多条)
  130. func ImGroupFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.ImGroup, error) {
  131. var m []model.ImGroup
  132. if params["value"] == nil {
  133. return nil, errors.New("参数有误")
  134. }
  135. if params["key"] == nil {
  136. //查询全部数据
  137. err := Db.Find(&m)
  138. if err != nil {
  139. return nil, logx.Error(err)
  140. }
  141. return &m, nil
  142. } else {
  143. if reflect.TypeOf(params["value"]).Kind() == reflect.Slice {
  144. //指定In查询
  145. if err := Db.In(utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil {
  146. return nil, logx.Warn(err)
  147. }
  148. return &m, nil
  149. } else {
  150. var query = fmt.Sprintf("%s =?", params["key"])
  151. err := Db.Where(query, params["value"]).Find(&m)
  152. if err != nil {
  153. return nil, logx.Error(err)
  154. }
  155. return &m, nil
  156. }
  157. }
  158. }