login_service.go 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. package service
  2. import (
  3. "XT_New/models"
  4. "XT_New/utils"
  5. "github.com/jinzhu/gorm"
  6. )
  7. func IsSuperAdmin(mobile string) bool {
  8. var count int
  9. readUserDb.Model(&models.AdminUser{}).Where("mobile = ? AND is_super_admin = 1 AND status = 1", mobile).Count(&count)
  10. return count == 1
  11. }
  12. func GetValidAdminUserByMobileReturnErr(mobile string) (*models.AdminUser, error) {
  13. var user models.AdminUser
  14. err := readUserDb.Where("mobile = ? AND status = 1", mobile).First(&user).Error
  15. if err != nil {
  16. if err == gorm.ErrRecordNotFound {
  17. return nil, nil
  18. } else {
  19. return nil, err
  20. }
  21. }
  22. return &user, nil
  23. }
  24. func DidAdminUserCreateOrg(adminUserID int64) (bool, error) {
  25. var count int
  26. err := readUserDb.Model(&models.Org{}).Where("creator = ? AND status <> 0", adminUserID).Count(&count).Error
  27. return count > 0, err
  28. }
  29. func DidAdminUserOrgCreateApp(adminUserID int, appType int) (bool, error) {
  30. var count int
  31. db := readUserDb.Model(&models.OrgApp{}).Where("creator = ? AND status = 1 AND open_status = 1", adminUserID)
  32. if appType != 0 {
  33. db = db.Where("app_type = ?", appType)
  34. }
  35. err := db.Count(&count).Error
  36. return count > 0, err
  37. }
  38. func GetAdminUserLastLoginLog(adminUserID int, appType int) (*models.AdminUserLoginLog, error) {
  39. var record models.AdminUserLoginLog
  40. var err error
  41. if appType > 0 {
  42. err = readUserDb.Last(&record, "admin_user_id = ? AND operate_type <> 2 AND app_type = ?", adminUserID, appType).Error
  43. } else {
  44. err = readUserDb.Last(&record, "admin_user_id = ? AND operate_type <> 2", adminUserID).Error
  45. }
  46. if err == gorm.ErrRecordNotFound {
  47. return nil, nil
  48. }
  49. return &record, err
  50. }
  51. func GetAdminUserPrioritizedAppType(adminUserID int) (int8, error) {
  52. rows, err := readUserDb.Raw("SELECT app.app_type FROM sgj_user_org_app AS app, sgj_user_admin_role AS r WHERE r.admin_user_id = ? AND r.app_id = app.id AND app.status = 1 AND app.open_status = 1 AND r.status = 1 ORDER BY app.org_id ASC, app.app_type ASC LIMIT 1;", adminUserID).Rows()
  53. defer rows.Close()
  54. if err != nil {
  55. return 0, err
  56. } else {
  57. if rows.Next() {
  58. var appType int8
  59. rows.Scan(&appType)
  60. return appType, nil
  61. } else {
  62. return 0, nil
  63. }
  64. }
  65. }
  66. func GetAdminUserAllOrgWithUID(adminUserID int, isSuperAdmin bool) ([]*models.Org, error) {
  67. if isSuperAdmin {
  68. var org models.Org
  69. err := readUserDb.Preload("OrgGallery", "status = 1").Where("creator = ? AND status <> 0", adminUserID).First(&org).Error
  70. if err != nil {
  71. return nil, err
  72. }
  73. return []*models.Org{&org}, nil
  74. } else {
  75. rows, err := readUserDb.Raw("SELECT org.*, COUNT(DISTINCT org.id) FROM sgj_user_admin_role AS u_r, sgj_user_org AS org WHERE u_r.admin_user_id = ? AND u_r.org_id = org.id AND u_r.status = 1 AND org.status <> 0 GROUP BY org.id ORDER BY org.id;", adminUserID).Rows()
  76. defer rows.Close()
  77. if err != nil {
  78. return nil, err
  79. } else {
  80. orgs := make([]*models.Org, 0)
  81. for rows.Next() {
  82. var org models.Org
  83. if scanErr := readUserDb.Preload("OrgGallery", "status = 1").ScanRows(rows, &org); scanErr != nil {
  84. return nil, scanErr
  85. } else {
  86. orgs = append(orgs, &org)
  87. }
  88. }
  89. return orgs, nil
  90. }
  91. }
  92. }
  93. func GetAdminUserAllOrgApp(adminUserID int, orgID int) ([]*models.OrgApp, error) {
  94. rows, err := readUserDb.Raw("SELECT app.* from sgj_user_admin_role AS u_r, sgj_user_org_app AS app WHERE u_r.admin_user_id = ? AND u_r.org_id = ? AND u_r.app_id = app.id AND u_r.status = 1 AND app.status = 1 ORDER BY app_type ASC;", adminUserID, orgID).Rows()
  95. defer rows.Close()
  96. if err != nil {
  97. return nil, err
  98. } else {
  99. apps := make([]*models.OrgApp, 0)
  100. for rows.Next() {
  101. var app models.OrgApp
  102. if scanErr := readUserDb.ScanRows(rows, &app); scanErr != nil {
  103. return nil, scanErr
  104. } else {
  105. apps = append(apps, &app)
  106. }
  107. }
  108. return apps, nil
  109. }
  110. }
  111. func InsertLoginLog(loginLog *models.AdminUserLoginLog) error {
  112. tx := writeUserDb.Begin()
  113. if err := tx.Create(loginLog).Error; err != nil {
  114. tx.Rollback()
  115. return err
  116. }
  117. return tx.Commit().Error
  118. }
  119. //func GetAppRole(adminUserID int, orgID int, appID int) (*models.App_Role, error) {
  120. // var appRole models.App_Role
  121. // err := readUserDb.Where("admin_user_id = ? AND org_id = ? AND app_id = ? AND status = 1", adminUserID, orgID, appID).First(&appRole).Error
  122. // if err != nil {
  123. // return nil, err
  124. // }
  125. // return &appRole, nil
  126. //}
  127. // 获取最近被创建的血透系统管理员角色(AppType为3)
  128. func GetLastXTAdminRole(adminUserID int64, appType int) (*models.App_Role, error) {
  129. var appRole models.App_Role
  130. // select a_r.* from sgj_user_admin_role as a_r
  131. // join sgj_user_admin as a on a.id = a_r.admin_user_id
  132. // join sgj_user_org_app as app on app.id = a_r.app_id and app.status = 1 and app.app_type = 3
  133. // where a_r.status = 1 and a_r.admin_user_id = 380
  134. err := readUserDb.Table("sgj_user_admin_role").
  135. Select("sgj_user_admin_role.*").
  136. Joins("join sgj_user_admin as a on a.id = sgj_user_admin_role.admin_user_id").
  137. Joins("join sgj_user_org_app as app on app.id = sgj_user_admin_role.app_id and app.status = 1 and app.app_type = ?", appType).
  138. Where("sgj_user_admin_role.status = 1 and sgj_user_admin_role.admin_user_id = ?", adminUserID).
  139. Order("sgj_user_admin_role.id desc").
  140. First(&appRole).
  141. Error
  142. if err != nil {
  143. if err == gorm.ErrRecordNotFound {
  144. return nil, nil
  145. } else {
  146. return nil, err
  147. }
  148. }
  149. return &appRole, nil
  150. }
  151. func GetOrgById(orgID int64) (*models.Org, error) {
  152. var org models.Org
  153. err := readUserDb.Model(&models.Org{}).Where("id = ?", orgID).First(&org).Error
  154. if err != nil {
  155. if err == gorm.ErrRecordNotFound {
  156. return nil, nil
  157. } else {
  158. return nil, err
  159. }
  160. }
  161. return &org, nil
  162. }
  163. func GetAppById(appID int64) (*models.OrgApp, error) {
  164. var app models.OrgApp
  165. err := readUserDb.Model(&models.OrgApp{}).Where("id = ?", appID).First(&app).Error
  166. if err != nil {
  167. if err == gorm.ErrRecordNotFound {
  168. return nil, nil
  169. } else {
  170. return nil, err
  171. }
  172. }
  173. return &app, nil
  174. }
  175. func GetOrgServeSubscibe(orgID int64) (*models.ServeSubscibe, error) {
  176. var model models.ServeSubscibe
  177. err := readUserDb.Model(&models.ServeSubscibe{}).Where("org_id = ? AND status = 1", orgID).First(&model).Error
  178. if err != nil {
  179. if err == gorm.ErrRecordNotFound {
  180. return nil, nil
  181. } else {
  182. return nil, err
  183. }
  184. }
  185. return &model, nil
  186. }
  187. //
  188. func GetAppRoleById(id int64) (*models.App_Role, error) {
  189. var model models.App_Role
  190. err := readUserDb.Model(&models.App_Role{}).Where("id = ? AND status = 1", id).First(&model).Error
  191. if err != nil {
  192. if err == gorm.ErrRecordNotFound {
  193. return nil, nil
  194. } else {
  195. return nil, err
  196. }
  197. }
  198. return &model, nil
  199. }
  200. func IsMobileRegister(mobile string) bool {
  201. var count int
  202. err := readUserDb.
  203. Model(&models.AdminUser{}).
  204. Where("mobile = ?", mobile).
  205. Count(&count).
  206. Error
  207. if err != nil {
  208. utils.ErrorLog("判断手机号是否被注册时失败: %v", err)
  209. return true
  210. }
  211. return count > 0
  212. }
  213. func FindAppRoleById(id int64) (*models.App_Role, error) {
  214. var model models.App_Role
  215. err := readUserDb.Model(&models.App_Role{}).Where("id = ? ", id).First(&model).Error
  216. if err != nil {
  217. if err == gorm.ErrRecordNotFound {
  218. return nil, nil
  219. } else {
  220. return nil, err
  221. }
  222. }
  223. return &model, nil
  224. }