user_service.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package service
  2. import (
  3. "Xcx_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "time"
  7. )
  8. func GetAdminUserByUserID(userID int64) (*models.AdminUser, error) {
  9. var user models.AdminUser
  10. err := readUserDb.Model(&models.AdminUser{}).Where("id = ?", userID).First(&user).Error
  11. if err != nil {
  12. if err == gorm.ErrRecordNotFound {
  13. return nil, nil
  14. } else {
  15. return nil, err
  16. }
  17. }
  18. return &user, nil
  19. }
  20. type AdminUserList struct {
  21. Id int64 `json:"id"`
  22. Name string `json:"name"`
  23. UserType int64 `json:"user_type"`
  24. }
  25. func GetAllDoctorAndNurse(orgId, appid int64) (doctors []AdminUserList, nurses []AdminUserList, err error) {
  26. var users []AdminUserList
  27. err = readUserDb.Table("sgj_user_admin_role as uar").Joins("JOIN sgj_user_admin as ua ON ua.id = uar.admin_user_id").Where("uar.status=1 and org_id=? and app_id =? and uar.user_type IN (2,3) and ua.status=1", orgId, appid).Select("ua.id, uar.user_name as name, uar.user_type").Scan(&users).Error
  28. if err != nil {
  29. return
  30. }
  31. if len(users) == 0 {
  32. return
  33. }
  34. for _, item := range users {
  35. if item.UserType == 2 {
  36. doctors = append(doctors, item)
  37. } else {
  38. nurses = append(nurses, item)
  39. }
  40. }
  41. return
  42. }
  43. func GetAllAdminUsers(orgId, appid int64) (list []*AdminUserList, err error) {
  44. //fmt.Println("orgid", orgId)
  45. //fmt.Println("appid", appid)
  46. err = readUserDb.Table("sgj_user_admin_role as uar").Joins("JOIN sgj_user_admin as ua ON ua.id = uar.admin_user_id").Where("uar.status=1 and uar.org_id=? and uar.app_id =? and ua.status=1", orgId, appid).Select("ua.id, uar.user_name as name, uar.user_type").Scan(&list).Error
  47. //err = readUserDb.Table("sgj_user_admin_role as uar").Joins("JOIN sgj_user_admin as ua ON ua.id = uar.admin_user_id").Where("uar.status=1 and ua.status=1").Select("ua.id, uar.user_name as name, uar.user_type").Scan(&list).Error
  48. return
  49. }
  50. func GetAllAdminUsersTwo(orgId int64) (list []*AdminUserList, err error) {
  51. //fmt.Println("orgid", orgId)
  52. //fmt.Println("appid", appid)
  53. err = readUserDb.Table("sgj_user_admin_role as uar").Joins("JOIN sgj_user_admin as ua ON ua.id = uar.admin_user_id").Where("uar.status=1 and uar.org_id=? and ua.status=1", orgId).Select("ua.id, uar.user_name as name, uar.user_type").Scan(&list).Error
  54. //err = readUserDb.Table("sgj_user_admin_role as uar").Joins("JOIN sgj_user_admin as ua ON ua.id = uar.admin_user_id").Where("uar.status=1 and ua.status=1").Select("ua.id, uar.user_name as name, uar.user_type").Scan(&list).Error
  55. return
  56. }
  57. func GetAdminUser(orgId, appid, id int64) (*AdminUserList, error) {
  58. var err error
  59. var user AdminUserList
  60. err = readUserDb.Table("sgj_user_admin_role as uar").Joins("JOIN sgj_user_admin as ua ON ua.id = uar.admin_user_id").Where("uar.status=1 and uar.org_id=? and uar.app_id =? and uar.admin_user_id = ? and ua.status=1", orgId, appid, id).Select("ua.id, uar.user_name as name, uar.user_type").Order("uar.id ASC").Take(&user).Error
  61. if err == gorm.ErrRecordNotFound {
  62. return nil, nil
  63. }
  64. if err != nil {
  65. return nil, err
  66. }
  67. return &user, nil
  68. }
  69. func GetSgjUserByMobild(mobile string) (user models.SgjUser, err error) {
  70. err = readUserDb.Model(&models.SgjUser{}).Where("mobile=?", mobile).First(&user).Error
  71. return
  72. }
  73. func GetSgjCoustomerByMobile(orgId int64, mobile string) (*models.SgjCustomer, error) {
  74. var c models.SgjCustomer
  75. var err error
  76. err = readUserDb.Model(&models.SgjCustomer{}).Where("user_org_id=? and mobile=?", orgId, mobile).First(&c).Error
  77. return &c, err
  78. }
  79. func GetAllGeneralAdminUsers(orgId, appid int64) (users []AdminUserList, err error) {
  80. err = readUserDb.Table("sgj_user_admin_role as uar").Joins("JOIN sgj_user_admin as ua ON ua.id = uar.admin_user_id").Where("uar.status=1 and org_id=? and app_id =? and uar.user_type <> 1 and ua.status=1", orgId, appid).Select("ua.id, uar.user_name as name, uar.user_type").Scan(&users).Error
  81. return
  82. }
  83. func GetDryWeightByPatientId(patientid int64, orgid int64) (pre []*models.SgjPatientDryweight, err error) {
  84. err = XTReadDB().Where("patient_id = ? AND user_org_id = ?", patientid, orgid).Last(&pre).Error
  85. return pre, err
  86. }
  87. func GetAllDoctor(orgid int64, appid int64) (appRole []*models.App_Role, err error) {
  88. err = UserReadDB().Where("org_id = ? AND app_id = ? AND status = ? AND (user_type = ? OR user_type = ?) ", orgid, appid, 1, 2, 1).Find(&appRole).Error
  89. return appRole, err
  90. }
  91. func GetAllDoctorThree(orgid int64) (appRole []*models.App_Role, err error) {
  92. err = UserReadDB().Where("org_id = ? AND status = ? AND (user_type = ? OR user_type = ?) ", orgid, 1, 2, 1).Find(&appRole).Error
  93. return appRole, err
  94. }
  95. func QueryDryWeight(orgid int64, patientid int64) (*models.SgjPatientDryweight, error) {
  96. var dryweight models.SgjPatientDryweight
  97. var err error
  98. err = XTReadDB().Where("user_org_id = ? AND patient_id = ?", orgid, patientid).Find(&dryweight).Error
  99. if err == gorm.ErrRecordNotFound {
  100. return nil, err
  101. }
  102. if err != nil {
  103. return nil, err
  104. }
  105. return &dryweight, nil
  106. }
  107. func CreatePatientWeight(dryweight *models.SgjPatientDryweight) error {
  108. err := XTWriteDB().Create(&dryweight).Error
  109. return err
  110. }
  111. func GetLastData(orgid int64) (models.SgjPatientDryweight, error) {
  112. dryweight := models.SgjPatientDryweight{}
  113. err := XTReadDB().Where("user_org_id = ? AND status = ?", orgid, 1).Last(&dryweight).Error
  114. return dryweight, err
  115. }
  116. func UpdateDialysisPrescription(patientid int64, orgid int64, dryweight float64, prescription models.PredialysisEvaluation) error {
  117. fmt.Println("patientid", patientid)
  118. fmt.Println("orgid", orgid)
  119. fmt.Println("dryweight", dryweight)
  120. err := XTWriteDB().Model(&prescription).Where("patient_id = ? AND user_org_id = ?", patientid, orgid).Update(map[string]interface{}{"dry_weight": dryweight, "mtime": time.Now().Unix()}).Last(&prescription).Error
  121. fmt.Println("错误是设么", err)
  122. return err
  123. }
  124. func GetAllData(orgid int64, id int64, page int64, limit int64) (dry []*models.XtPatientDryweight, total int64, err error) {
  125. offset := (page - 1) * limit
  126. table := XTReadDB().Table("sgj_users.sgj_user_admin_role AS a")
  127. fmt.Println("table", table)
  128. err = XTReadDB().Table("xt_patient_dryweight as x").Select("x.id,x.dry_weight,x.creator,x.remakes,x.patient_id,x.ctime,x.status,x.user_org_id,x.adjusted_value,a.user_name").Where("x.user_org_id = ? AND x.patient_id = ? AND x.status = ? ", orgid, id, 1).Count(&total).Joins("LEFT JOIN sgj_users.sgj_user_admin_role AS a ON a.admin_user_id = x.creator").Order("x.ctime desc").Offset(offset).Limit(limit).Group("id").Scan(&dry).Error
  129. if err != nil {
  130. return
  131. }
  132. return
  133. }
  134. func GetDryWeightDetailById(id int64) (models.SgjPatientDryweight, error) {
  135. dryweight := models.SgjPatientDryweight{}
  136. err := XTReadDB().Model(&dryweight).Where("id=? and status = 1", id).Find(&dryweight).Error
  137. return dryweight, err
  138. }
  139. func ModifyDryWeightData(adjust *models.SgjPatientDryweight, id int64) error {
  140. err := XTWriteDB().Model(&adjust).Where("id=?", id).Updates(map[string]interface{}{"dry_weight": adjust.DryWeight, "creator": adjust.Creator, "remakes": adjust.Remakes, "adjusted_value": adjust.AdjustedValue}).Error
  141. return err
  142. }
  143. func GetAllHisDoctor(orgid int64) (appRole []*models.App_Role, err error) {
  144. err = UserReadDB().Where("org_id = ? AND status = 1 AND (user_type = ? OR user_type = ?) ", orgid, 2, 1).Find(&appRole).Error
  145. return appRole, err
  146. }