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

db_user.go 2.1 KiB

1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
1 jaar geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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) FindUser(limit, start int) (*[]model.User, error) {
  49. var m []model.User
  50. if limit == 0 || start == 0 {
  51. if err := userDb.Db.Desc("id").Find(&m); err != nil {
  52. return nil, logx.Error(err)
  53. }
  54. } else {
  55. if err := userDb.Db.Desc("id").Limit(limit, start).Find(m); err != nil {
  56. return nil, logx.Error(err)
  57. }
  58. }
  59. return &m, nil
  60. }
  61. func (userDb *UserDb) UserInsert(m *model.User) (int, error) {
  62. _, err := userDb.Db.InsertOne(m)
  63. if err != nil {
  64. return 0, err
  65. }
  66. return m.Id, nil
  67. }
  68. func (userDb *UserDb) UpdateUser(m *model.User, columns ...string) (int64, error) {
  69. affected, err := userDb.Db.Where("id =?", m.Id).Cols(columns...).Update(m)
  70. if err != nil {
  71. return 0, err
  72. }
  73. return affected, nil
  74. }
  75. func (userDb *UserDb) DeleteUser(id interface{}) (int64, error) {
  76. if reflect.TypeOf(id).Kind() == reflect.Slice {
  77. return userDb.Db.In("id", id).Delete(model.User{})
  78. } else {
  79. return userDb.Db.Where("id = ?", id).Delete(model.User{})
  80. }
  81. }
  82. type UserWithUserIdentity struct {
  83. model.User `xorm:"extends"`
  84. model.UserIdentity `xorm:"extends"`
  85. }
  86. func (UserWithUserIdentity) TableName() string {
  87. return "user"
  88. }