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

234 lines
6.8 KiB

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