智慧食堂
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_class_with_user.go 3.2 KiB

1 年之前
1 年之前
1 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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) DeleteClassWithUserByClass(classId interface{}) (int64, error) {
  41. if reflect.TypeOf(classId).Kind() == reflect.Slice {
  42. return classWithUserDb.Db.In("class_id", classId).Delete(model2.ClassWithUser{})
  43. } else {
  44. return classWithUserDb.Db.Where("class_id = ?", classId).Delete(model2.ClassWithUser{})
  45. }
  46. }
  47. func (classWithUserDb *ClassWithUserDb) GetClassWithUserByUserIdentityId(userIdentityId int) (m *model2.ClassWithUser, err error) {
  48. m = new(model2.ClassWithUser)
  49. has, err := classWithUserDb.Db.Where("user_identity_id =?", userIdentityId).Get(m)
  50. if err != nil {
  51. return nil, logx.Error(err)
  52. }
  53. if has == false {
  54. return nil, nil
  55. }
  56. return m, nil
  57. }
  58. func (classWithUserDb *ClassWithUserDb) ClassWithUserInsert(m *model2.ClassWithUser) (int, error) {
  59. _, err := classWithUserDb.Db.InsertOne(m)
  60. if err != nil {
  61. return 0, err
  62. }
  63. return m.Id, nil
  64. }
  65. func (classWithUserDb *ClassWithUserDb) ClassWithUserInsertBySession(session *xorm.Session, m *model2.ClassWithUser) (int, error) {
  66. _, err := session.InsertOne(m)
  67. if err != nil {
  68. return 0, err
  69. }
  70. return m.Id, nil
  71. }
  72. func (classWithUserDb *ClassWithUserDb) FindUserIdentity(classId interface{}) (*[]model2.ClassWithUser, error) {
  73. var m []model2.ClassWithUser
  74. if reflect.TypeOf(classId).Kind() == reflect.Slice {
  75. if err := classWithUserDb.Db.In("class_id", classId).Desc("id").Find(&m); err != nil {
  76. return nil, logx.Error(err)
  77. }
  78. } else {
  79. if err := classWithUserDb.Db.Where("class_id =?", classId).Desc("id").Find(&m); err != nil {
  80. return nil, logx.Error(err)
  81. }
  82. }
  83. return &m, nil
  84. }
  85. func (classWithUserDb *ClassWithUserDb) ClassWithUserUpdateByUserIdentity(userIdentityId interface{}, m *model2.ClassWithUser, forceColums ...string) (int64, error) {
  86. var (
  87. affected int64
  88. err error
  89. )
  90. if forceColums != nil {
  91. affected, err = classWithUserDb.Db.Where("user_identity_id=?", userIdentityId).Cols(forceColums...).Update(m)
  92. } else {
  93. affected, err = classWithUserDb.Db.Where("user_identity_id=?", userIdentityId).Update(m)
  94. }
  95. if err != nil {
  96. return 0, err
  97. }
  98. return affected, nil
  99. }