蛋蛋星球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.
 
 
 
 

122 lines
3.2 KiB

  1. package gim
  2. import (
  3. "applet/app/db/gim/model"
  4. "applet/app/utils"
  5. "applet/app/utils/logx"
  6. "errors"
  7. "fmt"
  8. "reflect"
  9. "xorm.io/xorm"
  10. )
  11. // BatchSelectGroupUsers 批量查询数据 TODO::和下面的方法重复了,建议采用下面的 `GroupUserFindByParams` 方法
  12. func BatchSelectGroupUsers(Db *xorm.Engine, params map[string]interface{}) (*[]model.GroupUser, error) {
  13. var GroupUserData []model.GroupUser
  14. if err := Db.In(utils.AnyToString(params["key"]), params["value"]).
  15. Find(&GroupUserData); err != nil {
  16. return nil, logx.Warn(err)
  17. }
  18. return &GroupUserData, nil
  19. }
  20. // GroupUserInsert 插入单条数据
  21. func GroupUserInsert(Db *xorm.Engine, GroupUser *model.GroupUser) (int64, error) {
  22. _, err := Db.InsertOne(GroupUser)
  23. if err != nil {
  24. return 0, err
  25. }
  26. return GroupUser.Id, nil
  27. }
  28. // BatchAddGroupUsers 批量新增数据
  29. func BatchAddGroupUsers(Db *xorm.Engine, GroupUserData []*model.GroupUser) (int64, error) {
  30. affected, err := Db.Insert(GroupUserData)
  31. if err != nil {
  32. return 0, err
  33. }
  34. return affected, nil
  35. }
  36. func GetGroupUserCount(Db *xorm.Engine) int {
  37. var GroupUser model.GroupUser
  38. session := Db.Where("")
  39. count, err := session.Count(&GroupUser)
  40. if err != nil {
  41. return 0
  42. }
  43. return int(count)
  44. }
  45. // GroupUserDelete 删除记录
  46. func GroupUserDelete(Db *xorm.Engine, id interface{}) (int64, error) {
  47. if reflect.TypeOf(id).Kind() == reflect.Slice {
  48. return Db.In("id", id).Delete(model.GroupUser{})
  49. } else {
  50. return Db.Where("id = ?", id).Delete(model.GroupUser{})
  51. }
  52. }
  53. // GroupUserUpdate 更新记录
  54. func GroupUserUpdate(Db *xorm.Engine, id interface{}, GroupUser *model.GroupUser, 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(GroupUser)
  61. } else {
  62. affected, err = Db.Where("id=?", id).Update(GroupUser)
  63. }
  64. if err != nil {
  65. return 0, err
  66. }
  67. return affected, nil
  68. }
  69. // GroupUserGetOneByParams 通过传入的参数查询数据(单条)
  70. func GroupUserGetOneByParams(Db *xorm.Engine, memberType int, params map[string]interface{}) (*model.GroupUser, error) {
  71. var m model.GroupUser
  72. var query = fmt.Sprintf("%s =?", params["key"])
  73. has, err := Db.Where(query, params["value"]).And("member_type = ?", memberType).Get(&m)
  74. if err != nil {
  75. return nil, logx.Error(err)
  76. }
  77. if has == false {
  78. return nil, nil
  79. }
  80. return &m, nil
  81. }
  82. // GroupUserFindByParams 通过传入的参数查询数据(多条)
  83. func GroupUserFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.GroupUser, error) {
  84. var m []model.GroupUser
  85. if params["value"] == nil {
  86. return nil, errors.New("参数有误")
  87. }
  88. if params["key"] == nil {
  89. //查询全部数据
  90. err := Db.Find(&m)
  91. if err != nil {
  92. return nil, logx.Error(err)
  93. }
  94. return &m, nil
  95. } else {
  96. if reflect.TypeOf(params["value"]).Kind() == reflect.Slice {
  97. //指定In查询
  98. if err := Db.In(utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil {
  99. return nil, logx.Warn(err)
  100. }
  101. return &m, nil
  102. } else {
  103. var query = fmt.Sprintf("%s =?", params["key"])
  104. err := Db.Where(query, params["value"]).Find(&m)
  105. if err != nil {
  106. return nil, logx.Error(err)
  107. }
  108. return &m, nil
  109. }
  110. }
  111. }