智慧食堂
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

db_user.go 2.0 KiB

1 yıl önce
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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("enterprise_id", id).Delete(model.User{})
  78. } else {
  79. return userDb.Db.Where("enterprise_id = ?", id).Delete(model.User{})
  80. }
  81. }