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

247 rivejä
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("enterprise_id =?", 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", "user", "user_identity.uid = user.id").
  91. Join("LEFT", "class_with_user", "user_identity.id = class_with_user.user_identity_id").
  92. Join("LEFT", "class", "class_with_user.class_id = class.id").
  93. Join("LEFT", "grade", "class.grade_id = grade.id").
  94. Where("user_identity.state =?", enum.UserIdentityStateForNormal).
  95. And("user_identity.uid =?", userIdentityDb.Uid).Desc("user_identity.id").Find(&m); err != nil {
  96. return nil, logx.Error(err)
  97. }
  98. return &m, nil
  99. }
  100. func (userIdentityDb *UserIdentityDb) UserIdentity(id int) (m *UserIdentityWithEnterprise, err error) {
  101. m = new(UserIdentityWithEnterprise)
  102. has, err := userIdentityDb.Db.Join("LEFT", "enterprise", "user_identity.enterprise_id = enterprise.id").
  103. And("user_identity.id =?", id).Desc("user_identity.id").Get(m)
  104. if err != nil {
  105. return nil, logx.Error(err)
  106. }
  107. if has == false {
  108. return nil, nil
  109. }
  110. return m, nil
  111. }
  112. func (userIdentityDb *UserIdentityDb) UserIdentityInsert(m *model.UserIdentity) (int, error) {
  113. _, err := userIdentityDb.Db.InsertOne(m)
  114. if err != nil {
  115. return 0, err
  116. }
  117. return m.Id, nil
  118. }
  119. func (userIdentityDb *UserIdentityDb) UserIdentityInsertBySession(session *xorm.Session, m *model.UserIdentity) (int, error) {
  120. _, err := session.InsertOne(m)
  121. if err != nil {
  122. return 0, err
  123. }
  124. return m.Id, nil
  125. }
  126. func (userIdentityDb *UserIdentityDb) BatchAddUserIdentities(mm []*model.UserIdentity) (int64, error) {
  127. affected, err := userIdentityDb.Db.Insert(mm)
  128. if err != nil {
  129. return 0, err
  130. }
  131. return affected, nil
  132. }
  133. func (userIdentityDb *UserIdentityDb) UserIdentityDelete(id interface{}) (int64, error) {
  134. if reflect.TypeOf(id).Kind() == reflect.Slice {
  135. return Db.In("id", id).Delete(model.UserIdentity{})
  136. } else {
  137. return Db.Where("id = ?", id).Delete(model.UserIdentity{})
  138. }
  139. }
  140. func (userIdentityDb *UserIdentityDb) UserIdentityUpdate(id interface{}, m *model.UserIdentity, forceColums ...string) (int64, error) {
  141. var (
  142. affected int64
  143. err error
  144. )
  145. if forceColums != nil {
  146. affected, err = userIdentityDb.Db.Where("id=?", id).Cols(forceColums...).Update(m)
  147. } else {
  148. affected, err = userIdentityDb.Db.Where("id=?", id).Update(m)
  149. }
  150. if err != nil {
  151. return 0, err
  152. }
  153. return affected, nil
  154. }
  155. func (userIdentityDb *UserIdentityDb) UserIdentityUpdateBySession(session *xorm.Session, id interface{}, m *model.UserIdentity, forceColums ...string) (int64, error) {
  156. var (
  157. affected int64
  158. err error
  159. )
  160. if forceColums != nil {
  161. affected, err = session.Where("id=?", id).Cols(forceColums...).Update(m)
  162. } else {
  163. affected, err = session.Where("id=?", id).Update(m)
  164. }
  165. if err != nil {
  166. return 0, err
  167. }
  168. return affected, nil
  169. }
  170. func (userIdentityDb *UserIdentityDb) CountUserIdentityForEnterprise(enterpriseId, identity int) (total int64, err error) {
  171. var m model.UserIdentity
  172. total, err = userIdentityDb.Db.Where("enterprise_id =? AND identity =?", enterpriseId, identity).Count(&m)
  173. if err != nil {
  174. return
  175. }
  176. return
  177. }
  178. func (userIdentityDb *UserIdentityDb) FindUserIdentityForEnterpriseByIdentity(enterpriseId, identity int) (*[]model.UserIdentity, error) {
  179. var m []model.UserIdentity
  180. err := userIdentityDb.Db.Where("enterprise_id =? AND identity =?", enterpriseId, identity).Find(&m)
  181. if err != nil {
  182. return nil, err
  183. }
  184. return &m, nil
  185. }
  186. func (userIdentityDb *UserIdentityDb) FindUserIdentityForEnterprise(enterpriseId int) (*[]model.UserIdentity, error) {
  187. var m []model.UserIdentity
  188. err := userIdentityDb.Db.Where("enterprise_id =?", enterpriseId).Find(&m)
  189. if err != nil {
  190. return nil, err
  191. }
  192. return &m, nil
  193. }
  194. type UserIdentityWithUser struct {
  195. model.UserIdentity `xorm:"extends"`
  196. model.User `xorm:"extends"`
  197. model.ClassWithUser `xorm:"extends"`
  198. model.Class `xorm:"extends"`
  199. model.Grade `xorm:"extends"`
  200. model.SelfSupportForUserFaceInfo `xorm:"extends"`
  201. model.Enterprise `xorm:"extends"`
  202. }
  203. func (UserIdentityWithUser) TableName() string {
  204. return "user_identity"
  205. }
  206. type UserIdentityWithEnterprise struct {
  207. model.UserIdentity `xorm:"extends"`
  208. model.Enterprise `xorm:"extends"`
  209. }
  210. func (UserIdentityWithEnterprise) TableName() string {
  211. return "user_identity"
  212. }
  213. type UserIdentityWithClass struct {
  214. model.UserIdentity `xorm:"extends"`
  215. model.User `xorm:"extends"`
  216. model.Enterprise `xorm:"extends"`
  217. model.ClassWithUser `xorm:"extends"`
  218. model.Class `xorm:"extends"`
  219. model.Grade `xorm:"extends"`
  220. }
  221. func (UserIdentityWithClass) TableName() string {
  222. return "user_identity"
  223. }