user_service.go 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "time"
  6. "github.com/jinzhu/gorm"
  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. Status int64 `json:"status"`
  25. }
  26. func GetAllDoctorAndNurse(orgId int64, appid int64) (doctors []AdminUserList, nurses []AdminUserList, err error) {
  27. var users []AdminUserList
  28. 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,uar.status").Scan(&users).Error
  29. if err != nil {
  30. return
  31. }
  32. if len(users) == 0 {
  33. return
  34. }
  35. for _, item := range users {
  36. if item.UserType == 2 {
  37. doctors = append(doctors, item)
  38. } else {
  39. nurses = append(nurses, item)
  40. }
  41. }
  42. return
  43. }
  44. func GetAllDoctorAndNurseSix(orgId int64, appid int64) (doctors []AdminUserList, nurses []AdminUserList, err error) {
  45. var users []AdminUserList
  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("org_id=? and app_id =? and uar.user_type IN (2,3)", orgId, appid).Select("ua.id, uar.user_name as name, uar.user_type,uar.status").Scan(&users).Error
  47. if err != nil {
  48. return
  49. }
  50. if len(users) == 0 {
  51. return
  52. }
  53. for _, item := range users {
  54. if item.UserType == 2 {
  55. doctors = append(doctors, item)
  56. } else {
  57. nurses = append(nurses, item)
  58. }
  59. }
  60. return
  61. }
  62. func GetAllDoctorAndNurseSeven(orgId int64) (doctors []AdminUserList, nurses []AdminUserList, err error) {
  63. var users []AdminUserList
  64. err = readUserDb.Table("sgj_user_admin_role as uar").Joins("JOIN sgj_user_admin as ua ON ua.id = uar.admin_user_id").Where("org_id=? and uar.user_type IN (2,3)", orgId).Select("ua.id, uar.user_name as name, uar.user_type,uar.status").Scan(&users).Error
  65. if err != nil {
  66. return
  67. }
  68. if len(users) == 0 {
  69. return
  70. }
  71. for _, item := range users {
  72. if item.UserType == 2 {
  73. doctors = append(doctors, item)
  74. } else {
  75. nurses = append(nurses, item)
  76. }
  77. }
  78. return
  79. }
  80. func GetAllAdminUsers(orgId, appid int64) (list []*AdminUserList, err error) {
  81. 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.org_id=? and uar.app_id =?", orgId, appid).Select("ua.id, uar.user_name as name, uar.user_type,uar.status").Scan(&list).Error
  82. //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
  83. return
  84. }
  85. func GetAllAdminUsersTwo(orgId int64) (list []*AdminUserList, err error) {
  86. 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,uar.status").Scan(&list).Error
  87. //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
  88. return
  89. }
  90. func GetAdminUser(orgId, appid, id int64) (*AdminUserList, error) {
  91. var err error
  92. var user AdminUserList
  93. 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,uar.status").Order("uar.id ASC").Take(&user).Error
  94. if err == gorm.ErrRecordNotFound {
  95. return nil, nil
  96. }
  97. if err != nil {
  98. return nil, err
  99. }
  100. return &user, nil
  101. }
  102. func GetSgjUserByMobild(mobile string) (user models.SgjUser, err error) {
  103. err = readUserDb.Model(&models.SgjUser{}).Where("mobile=?", mobile).First(&user).Error
  104. return
  105. }
  106. func GetSgjCoustomerByMobile(orgId int64, mobile string) (*models.SgjCustomer, error) {
  107. var c models.SgjCustomer
  108. var err error
  109. err = readUserDb.Model(&models.SgjCustomer{}).Where("user_org_id=? and mobile=?", orgId, mobile).First(&c).Error
  110. return &c, err
  111. }
  112. func GetAllGeneralAdminUsers(orgId, appid int64) (users []AdminUserList, err error) {
  113. 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
  114. return
  115. }
  116. func GetDryWeightByPatientId(patientid int64, orgid int64) (pre []*models.SgjPatientDryweight, err error) {
  117. err = XTReadDB().Where("patient_id = ? AND user_org_id = ?", patientid, orgid).Last(&pre).Error
  118. return pre, err
  119. }
  120. func GetAllDoctor(orgid int64, appid int64) (appRole []*models.App_Role, err error) {
  121. err = UserReadDB().Where("org_id = ? AND app_id = ? AND status = ? AND (user_type = ? OR user_type = ?) ", orgid, appid, 1, 2, 1).Find(&appRole).Error
  122. return appRole, err
  123. }
  124. func GetAllDoctorThree(orgid int64) (appRole []*models.App_Role, err error) {
  125. err = UserReadDB().Where("org_id = ? AND status = ? AND (user_type = ? OR user_type = ?) ", orgid, 1, 2, 1).Find(&appRole).Error
  126. return appRole, err
  127. }
  128. func QueryDryWeight(orgid int64, patientid int64) (*models.SgjPatientDryweight, error) {
  129. var dryweight models.SgjPatientDryweight
  130. var err error
  131. err = XTReadDB().Where("user_org_id = ? AND patient_id = ?", orgid, patientid).Find(&dryweight).Error
  132. if err == gorm.ErrRecordNotFound {
  133. return nil, err
  134. }
  135. if err != nil {
  136. return nil, err
  137. }
  138. return &dryweight, nil
  139. }
  140. func CreatePatientWeight(dryweight *models.SgjPatientDryweight) error {
  141. err := XTWriteDB().Create(&dryweight).Error
  142. return err
  143. }
  144. func GetLastData(orgid int64) (models.SgjPatientDryweight, error) {
  145. dryweight := models.SgjPatientDryweight{}
  146. err := XTReadDB().Where("user_org_id = ? AND status = ?", orgid, 1).Last(&dryweight).Error
  147. return dryweight, err
  148. }
  149. func UpdateDialysisPrescription(patientid int64, orgid int64, dryweight float64, prescription models.PredialysisEvaluation) error {
  150. err := XTWriteDB().Model(&prescription).Where("patient_id = ? AND user_org_id = ? and assessment_date = ?", patientid, orgid, prescription.AssessmentDate).Update(map[string]interface{}{"dry_weight": prescription.DryWeight, "mtime": time.Now().Unix()}).Error
  151. fmt.Println("错误是设么", err)
  152. return err
  153. }
  154. func GetAllData(orgid int64, id int64, page int64, limit int64) (dry []*models.XtPatientDryweight, total int64, err error) {
  155. offset := (page - 1) * limit
  156. table := XTReadDB().Table("sgj_users.sgj_user_admin_role AS a")
  157. fmt.Println("table", table)
  158. 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
  159. if err != nil {
  160. return
  161. }
  162. return
  163. }
  164. func GetDryWeightDetailById(id int64) (models.SgjPatientDryweight, error) {
  165. dryweight := models.SgjPatientDryweight{}
  166. err := XTReadDB().Model(&dryweight).Where("id=? and status = 1", id).Find(&dryweight).Error
  167. return dryweight, err
  168. }
  169. func ModifyDryWeightData(adjust *models.SgjPatientDryweight, id int64) error {
  170. 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
  171. return err
  172. }
  173. func GetAllHisDoctor(orgid int64) (appRole []*models.App_Role, err error) {
  174. err = UserReadDB().Where("org_id = ? AND status = 1 AND (user_type = ? OR user_type = ?) ", orgid, 2, 1).Find(&appRole).Error
  175. return appRole, err
  176. }
  177. func GetDrugFlowDetailById(drug_id int64, org_id int64) (flow []*models.DrugFlow, err error) {
  178. if org_id == 10188 {
  179. err = XTReadDB().Model(&flow).Where("drug_id = ? and status =1 and user_org_id =? and system_time>=1717171200", drug_id, org_id).Order("ctime asc").Find(&flow).Error
  180. } else {
  181. err = XTReadDB().Model(&flow).Where("drug_id = ? and status =1 and user_org_id =?", drug_id, org_id).Order("ctime asc").Find(&flow).Error
  182. }
  183. return flow, err
  184. }