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

114 line
3.3 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.Period `xorm:"extends"`
  14. model2.Enterprise `xorm:"extends"`
  15. }
  16. func (ClassWithUser) TableName() string {
  17. return "class_with_user"
  18. }
  19. type ClassWithUserDb struct {
  20. Db *xorm.Engine `json:"db"`
  21. }
  22. func (classWithUserDb *ClassWithUserDb) Set() { // set方法
  23. classWithUserDb.Db = Db
  24. }
  25. func (classWithUserDb *ClassWithUserDb) GetInfoByUserIdentityId(userIdentityId int) (m *ClassWithUser, err error) {
  26. m = new(ClassWithUser)
  27. has, err := classWithUserDb.Db.Where("class_with_user.user_identity_id =?", userIdentityId).
  28. Join("LEFT", "user_identity", "class_with_user.user_identity_id = user_identity.id").
  29. Join("LEFT", "class", "class_with_user.class_id = class.id").
  30. Join("LEFT", "grade", "class.grade_id = grade.id").
  31. Join("LEFT", "period", "grade.period_id = period.id").
  32. Join("LEFT", "enterprise", "grade.enterprise_id = enterprise.id").
  33. Get(m)
  34. if err != nil {
  35. return nil, logx.Error(err)
  36. }
  37. if has == false {
  38. return nil, nil
  39. }
  40. return m, nil
  41. }
  42. func (classWithUserDb *ClassWithUserDb) DeleteClassWithUserByClass(classId interface{}) (int64, error) {
  43. if reflect.TypeOf(classId).Kind() == reflect.Slice {
  44. return classWithUserDb.Db.In("class_id", classId).Delete(model2.ClassWithUser{})
  45. } else {
  46. return classWithUserDb.Db.Where("class_id = ?", classId).Delete(model2.ClassWithUser{})
  47. }
  48. }
  49. func (classWithUserDb *ClassWithUserDb) GetClassWithUserByUserIdentityId(userIdentityId int) (m *model2.ClassWithUser, err error) {
  50. m = new(model2.ClassWithUser)
  51. has, err := classWithUserDb.Db.Where("user_identity_id =?", userIdentityId).Get(m)
  52. if err != nil {
  53. return nil, logx.Error(err)
  54. }
  55. if has == false {
  56. return nil, nil
  57. }
  58. return m, nil
  59. }
  60. func (classWithUserDb *ClassWithUserDb) ClassWithUserInsert(m *model2.ClassWithUser) (int, error) {
  61. _, err := classWithUserDb.Db.InsertOne(m)
  62. if err != nil {
  63. return 0, err
  64. }
  65. return m.Id, nil
  66. }
  67. func (classWithUserDb *ClassWithUserDb) ClassWithUserInsertBySession(session *xorm.Session, m *model2.ClassWithUser) (int, error) {
  68. _, err := session.InsertOne(m)
  69. if err != nil {
  70. return 0, err
  71. }
  72. return m.Id, nil
  73. }
  74. func (classWithUserDb *ClassWithUserDb) FindUserIdentity(classId interface{}) (*[]model2.ClassWithUser, error) {
  75. var m []model2.ClassWithUser
  76. if reflect.TypeOf(classId).Kind() == reflect.Slice {
  77. if err := classWithUserDb.Db.In("class_id", classId).Desc("id").Find(&m); err != nil {
  78. return nil, logx.Error(err)
  79. }
  80. } else {
  81. if err := classWithUserDb.Db.Where("class_id =?", classId).Desc("id").Find(&m); err != nil {
  82. return nil, logx.Error(err)
  83. }
  84. }
  85. return &m, nil
  86. }
  87. func (classWithUserDb *ClassWithUserDb) ClassWithUserUpdateByUserIdentity(userIdentityId interface{}, m *model2.ClassWithUser, forceColums ...string) (int64, error) {
  88. var (
  89. affected int64
  90. err error
  91. )
  92. if forceColums != nil {
  93. affected, err = classWithUserDb.Db.Where("user_identity_id=?", userIdentityId).Cols(forceColums...).Update(m)
  94. } else {
  95. affected, err = classWithUserDb.Db.Where("user_identity_id=?", userIdentityId).Update(m)
  96. }
  97. if err != nil {
  98. return 0, err
  99. }
  100. return affected, nil
  101. }