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

179 lines
5.0 KiB

  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/enum"
  5. "applet/app/utils/logx"
  6. "reflect"
  7. "xorm.io/xorm"
  8. )
  9. type UserIdentityDb struct {
  10. Db *xorm.Engine `json:"db"`
  11. Uid int `json:"uid"`
  12. }
  13. func (userIdentityDb *UserIdentityDb) Set(uid int) { // set方法
  14. userIdentityDb.Db = Db
  15. userIdentityDb.Uid = uid
  16. }
  17. func (userIdentityDb *UserIdentityDb) GetUserIdentity(id int) (m *model.UserIdentity, err error) {
  18. m = new(model.UserIdentity)
  19. has, err := userIdentityDb.Db.Where("id =?", id).Get(m)
  20. if err != nil {
  21. return nil, logx.Error(err)
  22. }
  23. if has == false {
  24. return nil, nil
  25. }
  26. return m, nil
  27. }
  28. func (userIdentityDb *UserIdentityDb) UserIdentityExist(enterpriseId int, idNo string) (m *model.UserIdentity, err error) {
  29. m = new(model.UserIdentity)
  30. has, err := userIdentityDb.Db.Where("enterprise_id =?", enterpriseId).And("id_no =?", idNo).Get(m)
  31. if err != nil {
  32. return nil, logx.Error(err)
  33. }
  34. if has == false {
  35. return nil, nil
  36. }
  37. return m, nil
  38. }
  39. func (userIdentityDb *UserIdentityDb) FindUserIdentity() (*[]UserIdentityWithEnterprise, error) {
  40. var m []UserIdentityWithEnterprise
  41. if err := userIdentityDb.Db.
  42. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  43. Where("user_identity.state =?", enum.UserIdentityStateForNormal).
  44. And("user_identity.uid =?", userIdentityDb.Uid).Desc("user_identity.id").Find(&m); err != nil {
  45. return nil, logx.Error(err)
  46. }
  47. return &m, nil
  48. }
  49. func (userIdentityDb *UserIdentityDb) UserIdentity(id int) (m *UserIdentityWithEnterprise, err error) {
  50. m = new(UserIdentityWithEnterprise)
  51. has, err := userIdentityDb.Db.Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  52. And("user_identity.id =?", id).Desc("user_identity.id").Get(m)
  53. if err != nil {
  54. return nil, logx.Error(err)
  55. }
  56. if has == false {
  57. return nil, nil
  58. }
  59. return m, nil
  60. }
  61. func (userIdentityDb *UserIdentityDb) UserIdentityInsert(m *model.UserIdentity) (int, error) {
  62. _, err := userIdentityDb.Db.InsertOne(m)
  63. if err != nil {
  64. return 0, err
  65. }
  66. return m.Id, nil
  67. }
  68. func (userIdentityDb *UserIdentityDb) UserIdentityInsertBySession(session *xorm.Session, m *model.UserIdentity) (int, error) {
  69. _, err := session.InsertOne(m)
  70. if err != nil {
  71. return 0, err
  72. }
  73. return m.Id, nil
  74. }
  75. func (userIdentityDb *UserIdentityDb) BatchAddUserIdentities(mm []*model.UserIdentity) (int64, error) {
  76. affected, err := userIdentityDb.Db.Insert(mm)
  77. if err != nil {
  78. return 0, err
  79. }
  80. return affected, nil
  81. }
  82. func (userIdentityDb *UserIdentityDb) UserIdentityDelete(id interface{}) (int64, error) {
  83. if reflect.TypeOf(id).Kind() == reflect.Slice {
  84. return Db.In("id", id).Delete(model.UserIdentity{})
  85. } else {
  86. return Db.Where("id = ?", id).Delete(model.UserIdentity{})
  87. }
  88. }
  89. func (userIdentityDb *UserIdentityDb) UserIdentityUpdate(id interface{}, m *model.UserIdentity, forceColums ...string) (int64, error) {
  90. var (
  91. affected int64
  92. err error
  93. )
  94. if forceColums != nil {
  95. affected, err = userIdentityDb.Db.Where("id=?", id).Cols(forceColums...).Update(m)
  96. } else {
  97. affected, err = userIdentityDb.Db.Where("id=?", id).Update(m)
  98. }
  99. if err != nil {
  100. return 0, err
  101. }
  102. return affected, nil
  103. }
  104. func (userIdentityDb *UserIdentityDb) UserIdentityUpdateBySession(session *xorm.Session, id interface{}, m *model.UserIdentity, forceColums ...string) (int64, error) {
  105. var (
  106. affected int64
  107. err error
  108. )
  109. if forceColums != nil {
  110. affected, err = session.Where("id=?", id).Cols(forceColums...).Update(m)
  111. } else {
  112. affected, err = session.Where("id=?", id).Update(m)
  113. }
  114. if err != nil {
  115. return 0, err
  116. }
  117. return affected, nil
  118. }
  119. func (userIdentityDb *UserIdentityDb) CountUserIdentityForEnterprise(enterpriseId, identity int) (total int64, err error) {
  120. var m model.UserIdentity
  121. total, err = userIdentityDb.Db.Where("enterprise_id =? AND identity =?", enterpriseId, identity).Count(&m)
  122. if err != nil {
  123. return
  124. }
  125. return
  126. }
  127. func (userIdentityDb *UserIdentityDb) FindUserIdentityForEnterpriseByIdentity(enterpriseId, identity int) (*[]model.UserIdentity, error) {
  128. var m []model.UserIdentity
  129. err := userIdentityDb.Db.Where("enterprise_id =? AND identity =?", enterpriseId, identity).Find(&m)
  130. if err != nil {
  131. return nil, err
  132. }
  133. return &m, nil
  134. }
  135. func (userIdentityDb *UserIdentityDb) FindUserIdentityForEnterprise(enterpriseId int) (*[]model.UserIdentity, error) {
  136. var m []model.UserIdentity
  137. err := userIdentityDb.Db.Where("enterprise_id =?", enterpriseId).Find(&m)
  138. if err != nil {
  139. return nil, err
  140. }
  141. return &m, nil
  142. }
  143. type UserIdentityWithUser struct {
  144. model.UserIdentity `xorm:"extends"`
  145. model.User `xorm:"extends"`
  146. model.ClassWithUser `xorm:"extends"`
  147. model.Class `xorm:"extends"`
  148. model.Grade `xorm:"extends"`
  149. model.SelfSupportForUserFaceInfo `xorm:"extends"`
  150. }
  151. func (UserIdentityWithUser) TableName() string {
  152. return "user_identity"
  153. }
  154. type UserIdentityWithEnterprise struct {
  155. model.UserIdentity `xorm:"extends"`
  156. model.Enterprise `xorm:"extends"`
  157. }
  158. func (UserIdentityWithEnterprise) TableName() string {
  159. return "user_identity"
  160. }