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

233 rivejä
6.8 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) UserIdentityExistV1(enterpriseId int, name string, classId int) (m *model.UserIdentity, err error) {
  40. m = new(model.UserIdentity)
  41. var mm []model.UserIdentity
  42. if err = userIdentityDb.Db.Where("enterprise_id =?", enterpriseId).And("name =?", name).Find(&mm); err != nil {
  43. return nil, logx.Error(err)
  44. }
  45. if len(mm) <= 0 {
  46. return nil, nil
  47. }
  48. classWithUserDb := ClassWithUserDb{}
  49. classWithUserDb.Set()
  50. for _, v := range mm {
  51. //查找 class_with_user
  52. class, err1 := classWithUserDb.GetClassWithUserByUserIdentityId(v.Id)
  53. if err1 != nil {
  54. return nil, err
  55. }
  56. //if class == nil {
  57. // return nil, errors.New("当前学生身份待管理员确认")
  58. //}
  59. if class.ClassId == classId {
  60. return m, nil
  61. }
  62. }
  63. return nil, nil
  64. }
  65. func (userIdentityDb *UserIdentityDb) FindUserIdentity() (*[]UserIdentityWithEnterprise, error) {
  66. var m []UserIdentityWithEnterprise
  67. if err := userIdentityDb.Db.
  68. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  69. Where("user_identity.state =?", enum.UserIdentityStateForNormal).
  70. And("user_identity.uid =?", userIdentityDb.Uid).Desc("user_identity.id").Find(&m); err != nil {
  71. return nil, logx.Error(err)
  72. }
  73. return &m, nil
  74. }
  75. func (userIdentityDb *UserIdentityDb) FindUserIdentityWithClass() (*[]UserIdentityWithClass, error) {
  76. var m []UserIdentityWithClass
  77. if err := userIdentityDb.Db.
  78. Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  79. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  80. Join("LEFT", "class", "class_with_user.class_id = class.id").
  81. Join("LEFT", "grade", "class.grade_id = grade.id").
  82. Where("user_identity.state =?", enum.UserIdentityStateForNormal).
  83. And("user_identity.uid =?", userIdentityDb.Uid).Desc("user_identity.id").Find(&m); err != nil {
  84. return nil, logx.Error(err)
  85. }
  86. return &m, nil
  87. }
  88. func (userIdentityDb *UserIdentityDb) UserIdentity(id int) (m *UserIdentityWithEnterprise, err error) {
  89. m = new(UserIdentityWithEnterprise)
  90. has, err := userIdentityDb.Db.Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  91. And("user_identity.id =?", id).Desc("user_identity.id").Get(m)
  92. if err != nil {
  93. return nil, logx.Error(err)
  94. }
  95. if has == false {
  96. return nil, nil
  97. }
  98. return m, nil
  99. }
  100. func (userIdentityDb *UserIdentityDb) UserIdentityInsert(m *model.UserIdentity) (int, error) {
  101. _, err := userIdentityDb.Db.InsertOne(m)
  102. if err != nil {
  103. return 0, err
  104. }
  105. return m.Id, nil
  106. }
  107. func (userIdentityDb *UserIdentityDb) UserIdentityInsertBySession(session *xorm.Session, m *model.UserIdentity) (int, error) {
  108. _, err := session.InsertOne(m)
  109. if err != nil {
  110. return 0, err
  111. }
  112. return m.Id, nil
  113. }
  114. func (userIdentityDb *UserIdentityDb) BatchAddUserIdentities(mm []*model.UserIdentity) (int64, error) {
  115. affected, err := userIdentityDb.Db.Insert(mm)
  116. if err != nil {
  117. return 0, err
  118. }
  119. return affected, nil
  120. }
  121. func (userIdentityDb *UserIdentityDb) UserIdentityDelete(id interface{}) (int64, error) {
  122. if reflect.TypeOf(id).Kind() == reflect.Slice {
  123. return Db.In("id", id).Delete(model.UserIdentity{})
  124. } else {
  125. return Db.Where("id = ?", id).Delete(model.UserIdentity{})
  126. }
  127. }
  128. func (userIdentityDb *UserIdentityDb) UserIdentityUpdate(id interface{}, m *model.UserIdentity, forceColums ...string) (int64, error) {
  129. var (
  130. affected int64
  131. err error
  132. )
  133. if forceColums != nil {
  134. affected, err = userIdentityDb.Db.Where("id=?", id).Cols(forceColums...).Update(m)
  135. } else {
  136. affected, err = userIdentityDb.Db.Where("id=?", id).Update(m)
  137. }
  138. if err != nil {
  139. return 0, err
  140. }
  141. return affected, nil
  142. }
  143. func (userIdentityDb *UserIdentityDb) UserIdentityUpdateBySession(session *xorm.Session, id interface{}, m *model.UserIdentity, forceColums ...string) (int64, error) {
  144. var (
  145. affected int64
  146. err error
  147. )
  148. if forceColums != nil {
  149. affected, err = session.Where("id=?", id).Cols(forceColums...).Update(m)
  150. } else {
  151. affected, err = session.Where("id=?", id).Update(m)
  152. }
  153. if err != nil {
  154. return 0, err
  155. }
  156. return affected, nil
  157. }
  158. func (userIdentityDb *UserIdentityDb) CountUserIdentityForEnterprise(enterpriseId, identity int) (total int64, err error) {
  159. var m model.UserIdentity
  160. total, err = userIdentityDb.Db.Where("enterprise_id =? AND identity =?", enterpriseId, identity).Count(&m)
  161. if err != nil {
  162. return
  163. }
  164. return
  165. }
  166. func (userIdentityDb *UserIdentityDb) FindUserIdentityForEnterpriseByIdentity(enterpriseId, identity int) (*[]model.UserIdentity, error) {
  167. var m []model.UserIdentity
  168. err := userIdentityDb.Db.Where("enterprise_id =? AND identity =?", enterpriseId, identity).Find(&m)
  169. if err != nil {
  170. return nil, err
  171. }
  172. return &m, nil
  173. }
  174. func (userIdentityDb *UserIdentityDb) FindUserIdentityForEnterprise(enterpriseId int) (*[]model.UserIdentity, error) {
  175. var m []model.UserIdentity
  176. err := userIdentityDb.Db.Where("enterprise_id =?", enterpriseId).Find(&m)
  177. if err != nil {
  178. return nil, err
  179. }
  180. return &m, nil
  181. }
  182. type UserIdentityWithUser struct {
  183. model.UserIdentity `xorm:"extends"`
  184. model.User `xorm:"extends"`
  185. model.ClassWithUser `xorm:"extends"`
  186. model.Class `xorm:"extends"`
  187. model.Grade `xorm:"extends"`
  188. model.SelfSupportForUserFaceInfo `xorm:"extends"`
  189. model.Enterprise `xorm:"extends"`
  190. }
  191. func (UserIdentityWithUser) TableName() string {
  192. return "user_identity"
  193. }
  194. type UserIdentityWithEnterprise struct {
  195. model.UserIdentity `xorm:"extends"`
  196. model.Enterprise `xorm:"extends"`
  197. }
  198. func (UserIdentityWithEnterprise) TableName() string {
  199. return "user_identity"
  200. }
  201. type UserIdentityWithClass struct {
  202. model.UserIdentity `xorm:"extends"`
  203. model.Enterprise `xorm:"extends"`
  204. model.ClassWithUser `xorm:"extends"`
  205. model.Class `xorm:"extends"`
  206. model.Grade `xorm:"extends"`
  207. }
  208. func (UserIdentityWithClass) TableName() string {
  209. return "user_identity"
  210. }