智慧食堂
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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