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

104 lines
2.9 KiB

  1. package db
  2. import (
  3. model2 "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "reflect"
  6. "xorm.io/xorm"
  7. )
  8. type ClassWithUser struct {
  9. model2.ClassWithUser `xorm:"extends"`
  10. model2.UserIdentity `xorm:"extends"`
  11. model2.Class `xorm:"extends"`
  12. model2.Grade `xorm:"extends"`
  13. model2.Enterprise `xorm:"extends"`
  14. }
  15. func (ClassWithUser) TableName() string {
  16. return "class_with_user"
  17. }
  18. type ClassWithUserDb struct {
  19. Db *xorm.Engine `json:"db"`
  20. }
  21. func (classWithUserDb *ClassWithUserDb) Set() { // set方法
  22. classWithUserDb.Db = Db
  23. }
  24. func (classWithUserDb *ClassWithUserDb) GetInfoByUserIdentityId(userIdentityId int) (m *ClassWithUser, err error) {
  25. m = new(ClassWithUser)
  26. has, err := classWithUserDb.Db.Where("class_with_user.user_identity_id =?", userIdentityId).
  27. Join("LEFT", "user_identity", "class_with_user.user_identity_id = user_identity.id").
  28. Join("LEFT", "class", "class_with_user.class_id = class.id").
  29. Join("LEFT", "grade", "class.grade_id = grade.id").
  30. Join("LEFT", "enterprise", "grade.enterprise_id = enterprise.id").
  31. Get(m)
  32. if err != nil {
  33. return nil, logx.Error(err)
  34. }
  35. if has == false {
  36. return nil, nil
  37. }
  38. return m, nil
  39. }
  40. func (classWithUserDb *ClassWithUserDb) GetClassWithUserByUserIdentityId(userIdentityId int) (m *model2.ClassWithUser, err error) {
  41. m = new(model2.ClassWithUser)
  42. has, err := classWithUserDb.Db.Where("user_identity_id =?", userIdentityId).Get(m)
  43. if err != nil {
  44. return nil, logx.Error(err)
  45. }
  46. if has == false {
  47. return nil, nil
  48. }
  49. return m, nil
  50. }
  51. func (classWithUserDb *ClassWithUserDb) ClassWithUserInsert(m *model2.ClassWithUser) (int, error) {
  52. _, err := classWithUserDb.Db.InsertOne(m)
  53. if err != nil {
  54. return 0, err
  55. }
  56. return m.Id, nil
  57. }
  58. func (classWithUserDb *ClassWithUserDb) ClassWithUserInsertBySession(session *xorm.Session, m *model2.ClassWithUser) (int, error) {
  59. _, err := session.InsertOne(m)
  60. if err != nil {
  61. return 0, err
  62. }
  63. return m.Id, nil
  64. }
  65. func (classWithUserDb *ClassWithUserDb) FindUserIdentity(classId interface{}) (*[]model2.ClassWithUser, error) {
  66. var m []model2.ClassWithUser
  67. if reflect.TypeOf(classId).Kind() == reflect.Slice {
  68. if err := classWithUserDb.Db.In("class_id", classId).Desc("id").Find(&m); err != nil {
  69. return nil, logx.Error(err)
  70. }
  71. } else {
  72. if err := classWithUserDb.Db.Where("class_id =?", classId).Desc("id").Find(&m); err != nil {
  73. return nil, logx.Error(err)
  74. }
  75. }
  76. return &m, nil
  77. }
  78. func (classWithUserDb *ClassWithUserDb) ClassWithUserUpdateByUserIdentity(userIdentityId interface{}, m *model2.ClassWithUser, forceColums ...string) (int64, error) {
  79. var (
  80. affected int64
  81. err error
  82. )
  83. if forceColums != nil {
  84. affected, err = classWithUserDb.Db.Where("user_identity_id=?", userIdentityId).Cols(forceColums...).Update(m)
  85. } else {
  86. affected, err = classWithUserDb.Db.Where("user_identity_id=?", userIdentityId).Update(m)
  87. }
  88. if err != nil {
  89. return 0, err
  90. }
  91. return affected, nil
  92. }