智慧食堂
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.

преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
преди 1 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "reflect"
  6. "xorm.io/xorm"
  7. )
  8. type UserDb struct {
  9. Db *xorm.Engine `json:"db"`
  10. CompanyId int `json:"company_id"`
  11. }
  12. func (userDb *UserDb) Set() { // set方法
  13. userDb.Db = Db
  14. }
  15. func (userDb *UserDb) GetUser(id int) (m *model.User, err error) {
  16. m = new(model.User)
  17. has, err := userDb.Db.Where("id =?", id).Get(m)
  18. if err != nil {
  19. return nil, logx.Error(err)
  20. }
  21. if has == false {
  22. return nil, nil
  23. }
  24. return m, nil
  25. }
  26. func (userDb *UserDb) GetUserByName(userName string) (m *model.User, err error) {
  27. m = new(model.User)
  28. has, err := userDb.Db.Where("name =?", userName).Get(m)
  29. if err != nil {
  30. return nil, logx.Error(err)
  31. }
  32. if has == false {
  33. return nil, nil
  34. }
  35. return m, nil
  36. }
  37. func (userDb *UserDb) GetUserByPhone(phone string) (m *model.User, err error) {
  38. m = new(model.User)
  39. has, err := userDb.Db.Where("phone =?", phone).Get(m)
  40. if err != nil {
  41. return nil, logx.Error(err)
  42. }
  43. if has == false {
  44. return nil, nil
  45. }
  46. return m, nil
  47. }
  48. func (userDb *UserDb) GetUserByOpenId(openId string) (m *model.User, err error) {
  49. m = new(model.User)
  50. has, err := userDb.Db.Where("open_id =?", openId).Get(m)
  51. if err != nil {
  52. return nil, logx.Error(err)
  53. }
  54. if has == false {
  55. return nil, nil
  56. }
  57. return m, nil
  58. }
  59. func (userDb *UserDb) FindUser(limit, start int) (*[]model.User, error) {
  60. var m []model.User
  61. if limit == 0 || start == 0 {
  62. if err := userDb.Db.Desc("id").Find(&m); err != nil {
  63. return nil, logx.Error(err)
  64. }
  65. } else {
  66. if err := userDb.Db.Desc("id").Limit(limit, start).Find(m); err != nil {
  67. return nil, logx.Error(err)
  68. }
  69. }
  70. return &m, nil
  71. }
  72. func (userDb *UserDb) UserInsert(m *model.User) (int, error) {
  73. _, err := userDb.Db.InsertOne(m)
  74. if err != nil {
  75. return 0, err
  76. }
  77. return m.Id, nil
  78. }
  79. func (userDb *UserDb) UpdateUser(m *model.User, columns ...string) (int64, error) {
  80. affected, err := userDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  81. if err != nil {
  82. return 0, err
  83. }
  84. return affected, nil
  85. }
  86. func (userDb *UserDb) DeleteUser(id interface{}) (int64, error) {
  87. if reflect.TypeOf(id).Kind() == reflect.Slice {
  88. return userDb.Db.In("id", id).Delete(model.User{})
  89. } else {
  90. return userDb.Db.Where("id = ?", id).Delete(model.User{})
  91. }
  92. }
  93. type UserWithUserIdentity struct {
  94. model.User `xorm:"extends"`
  95. model.UserIdentity `xorm:"extends"`
  96. }
  97. func (UserWithUserIdentity) TableName() string {
  98. return "user"
  99. }