智慧食堂
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

166 righe
4.6 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) UserIdentityInsert(m *model.UserIdentity) (int, error) {
  50. _, err := userIdentityDb.Db.InsertOne(m)
  51. if err != nil {
  52. return 0, err
  53. }
  54. return m.Id, nil
  55. }
  56. func (userIdentityDb *UserIdentityDb) UserIdentityInsertBySession(session *xorm.Session, m *model.UserIdentity) (int, error) {
  57. _, err := session.InsertOne(m)
  58. if err != nil {
  59. return 0, err
  60. }
  61. return m.Id, nil
  62. }
  63. func (userIdentityDb *UserIdentityDb) BatchAddUserIdentities(mm []*model.UserIdentity) (int64, error) {
  64. affected, err := userIdentityDb.Db.Insert(mm)
  65. if err != nil {
  66. return 0, err
  67. }
  68. return affected, nil
  69. }
  70. func (userIdentityDb *UserIdentityDb) UserIdentityDelete(id interface{}) (int64, error) {
  71. if reflect.TypeOf(id).Kind() == reflect.Slice {
  72. return Db.In("id", id).Delete(model.UserIdentity{})
  73. } else {
  74. return Db.Where("id = ?", id).Delete(model.UserIdentity{})
  75. }
  76. }
  77. func (userIdentityDb *UserIdentityDb) UserIdentityUpdate(id interface{}, m *model.UserIdentity, forceColums ...string) (int64, error) {
  78. var (
  79. affected int64
  80. err error
  81. )
  82. if forceColums != nil {
  83. affected, err = userIdentityDb.Db.Where("id=?", id).Cols(forceColums...).Update(m)
  84. } else {
  85. affected, err = userIdentityDb.Db.Where("id=?", id).Update(m)
  86. }
  87. if err != nil {
  88. return 0, err
  89. }
  90. return affected, nil
  91. }
  92. func (userIdentityDb *UserIdentityDb) UserIdentityUpdateBySession(session *xorm.Session, id interface{}, m *model.UserIdentity, forceColums ...string) (int64, error) {
  93. var (
  94. affected int64
  95. err error
  96. )
  97. if forceColums != nil {
  98. affected, err = session.Where("id=?", id).Cols(forceColums...).Update(m)
  99. } else {
  100. affected, err = session.Where("id=?", id).Update(m)
  101. }
  102. if err != nil {
  103. return 0, err
  104. }
  105. return affected, nil
  106. }
  107. func (userIdentityDb *UserIdentityDb) CountUserIdentityForEnterprise(enterpriseId, identity int) (total int64, err error) {
  108. var m model.UserIdentity
  109. total, err = userIdentityDb.Db.Where("enterprise_id =? AND identity =?", enterpriseId, identity).Count(&m)
  110. if err != nil {
  111. return
  112. }
  113. return
  114. }
  115. func (userIdentityDb *UserIdentityDb) FindUserIdentityForEnterpriseByIdentity(enterpriseId, identity int) (*[]model.UserIdentity, error) {
  116. var m []model.UserIdentity
  117. err := userIdentityDb.Db.Where("enterprise_id =? AND identity =?", enterpriseId, identity).Find(&m)
  118. if err != nil {
  119. return nil, err
  120. }
  121. return &m, nil
  122. }
  123. func (userIdentityDb *UserIdentityDb) FindUserIdentityForEnterprise(enterpriseId int) (*[]model.UserIdentity, error) {
  124. var m []model.UserIdentity
  125. err := userIdentityDb.Db.Where("enterprise_id =?", enterpriseId).Find(&m)
  126. if err != nil {
  127. return nil, err
  128. }
  129. return &m, nil
  130. }
  131. type UserIdentityWithUser struct {
  132. model.UserIdentity `xorm:"extends"`
  133. model.User `xorm:"extends"`
  134. model.ClassWithUser `xorm:"extends"`
  135. model.Class `xorm:"extends"`
  136. model.Grade `xorm:"extends"`
  137. model.SelfSupportForUserFaceInfo `xorm:"extends"`
  138. }
  139. func (UserIdentityWithUser) TableName() string {
  140. return "user_identity"
  141. }
  142. type UserIdentityWithEnterprise struct {
  143. model.UserIdentity `xorm:"extends"`
  144. model.Enterprise `xorm:"extends"`
  145. }
  146. func (UserIdentityWithEnterprise) TableName() string {
  147. return "user_identity"
  148. }