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

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