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

147 regels
4.0 KiB

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