print_schedule_dialysis_service.go 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package schedule_dialysis
  2. import (
  3. "Xcx_New/models"
  4. p_service "Xcx_New/service"
  5. "Xcx_New/utils"
  6. "fmt"
  7. "time"
  8. "github.com/jinzhu/gorm"
  9. )
  10. func GetSchedules(orgID int64, schIDs []string) ([]*ScheduleVM, error) {
  11. recordDateStr := time.Now().Format("2006-01-02")
  12. recordDate, _ := utils.ParseTimeStringToTime("2006-01-02", recordDateStr)
  13. var schedules []*ScheduleVM
  14. db := p_service.XTReadDB()
  15. err := db.Model(&ScheduleVM{}).
  16. Preload("Patient", "user_org_id = ? AND status = 1", orgID).
  17. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  18. return db.Where("user_org_id = ? AND status = 1", orgID).Preload("DeviceNumber", "org_id = ? AND status = 1", orgID)
  19. }).
  20. Preload("Prescription", "user_org_id = ? AND status = 1", orgID).
  21. Preload("ReceiveAssessment", "user_org_id = ? AND status = 1", orgID).
  22. Preload("XtReceiveTreatmentAsses", "user_org_id = ? AND status = 1", orgID).
  23. Preload("AssessmentBeforeDislysis", func(db *gorm.DB) *gorm.DB {
  24. return db.Where("user_org_id = ? AND status = 1", orgID)
  25. }).
  26. Preload("AssessmentAfterDislysis", "user_org_id = ? AND status = 1", orgID).
  27. Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  28. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgID, recordDate.Unix())
  29. }).Preload("MonitoringRecords", func(db *gorm.DB) *gorm.DB {
  30. return db.Where("user_org_id = ? AND status = 1", orgID).
  31. Order("operate_time asc")
  32. }).
  33. Preload("DeviceNumber", "org_id = ? AND status = 1", orgID).
  34. Preload("DeviceZone", "org_id = ? AND status = 1", orgID).
  35. Preload("DoubleCheck", "user_org_id = ? AND status = 1", orgID).
  36. Preload("Summer", "user_org_id = ? AND status = 1", orgID).
  37. Preload("Advices", func(db *gorm.DB) *gorm.DB {
  38. return db.Select("id, user_org_id, patient_id, advice_type, advice_date, record_date, start_time, drug_spec_unit,advice_name,advice_desc, reminder_date, drug_spec, drug_spec_unit, single_dose, single_dose_unit, prescribing_number, prescribing_number_unit, delivery_way, execution_frequency, advice_doctor, status, created_time,updated_time, advice_affirm, remark, stop_time, stop_reason, stop_doctor, stop_state, parent_id, execution_time, execution_staff, execution_state, checker, check_state, check_time, groupno, IF(parent_id > 0, parent_id, id) as advice_order").
  39. Where("user_org_id = ? AND advice_type = 2 AND status = 1", orgID).
  40. Order("start_time asc, groupno desc, advice_order desc, id asc")
  41. }).
  42. Where("user_org_id = ? AND status = 1 AND id in (?)", orgID, schIDs).
  43. Find(&schedules).
  44. Error
  45. if err != nil {
  46. return nil, err
  47. }
  48. for _, item := range schedules {
  49. dialysis_count, _ := GetDialysisOrderCount(orgID, item.PatientID, item.ScheduleDate)
  50. fmt.Println(dialysis_count)
  51. item.Patient.TotalDialysis = dialysis_count
  52. fmt.Println(item.Patient.TotalDialysis)
  53. }
  54. return schedules, nil
  55. }
  56. func GetMedicalStaffs(orgID int64, appID int64) ([]*MedicalStaffVM, error) {
  57. var staffs []*MedicalStaffVM
  58. db := p_service.UserReadDB()
  59. // err := db.Table("sgj_user_admin_role as uar").
  60. // Joins("JOIN sgj_user_admin as ua ON ua.id = uar.admin_user_id").
  61. // Joins("LEFT JOIN sgj_user_admin_electronic_signature as es ON es.creator = uar.admin_user_id and es.status = 1").
  62. // Where("uar.status = 1 and uar.org_id = ? and uar.app_id = ? and uar.user_type IN (2,3) and ua.status = 1", orgID, appID).
  63. // Where("uar.status = 1 and uar.org_id = ? and uar.app_id = ? and ua.status = 1", orgID, appID).
  64. // Select("ua.id, uar.user_name as name, uar.user_type, es.url as es_url").
  65. // Scan(&staffs).
  66. // Error
  67. err := db.Table("sgj_user_admin_electronic_signature").
  68. Where("org_id=? and app_id=? and status=1", orgID, appID).
  69. Select("creator as id, url as es_url").Scan(&staffs).Error
  70. if err != nil {
  71. return nil, err
  72. }
  73. return staffs, nil
  74. }
  75. type AdminUserList struct {
  76. Id int64 `json:"id"`
  77. Name string `json:"name"`
  78. UserType int64 `json:"user_type"`
  79. }
  80. type AdminUserListTwo struct {
  81. Id int64 `json:"id"`
  82. Name string `json:"name"`
  83. UserType int64 `json:"user_type"`
  84. Url string `json:"url"`
  85. }
  86. func GetAllAdminUsers(orgId int64, appid int64) (list []*AdminUserList, err error) {
  87. db := p_service.UserReadDB()
  88. err = db.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
  89. return
  90. }
  91. func GetAllName(orgId int64, appid int64) (list []AdminUserListTwo, err error) {
  92. db := p_service.UserReadDB()
  93. err = db.Table("sgj_user_admin_role as uar").Joins("left join sgj_user_admin as ua on ua.id = uar.admin_user_id").Joins("left join sgj_user_admin_electronic_signature as e on e.creator = uar.admin_user_id").Where("uar.status=1 and uar.org_id=? and uar.app_id =? and ua.status=1 and e.status = 1 and e.org_id = ? and e.app_id = ?", orgId, appid, orgId, appid).Select("ua.id, uar.user_name as name, uar.user_type,e.url").Scan(&list).Error
  94. return
  95. }
  96. func GetPatientCoursesRecords(orgID int64, id int64) (records models.PatientDiseaseCourse, err error) {
  97. db := p_service.XTReadDB()
  98. err = db.Model(&models.PatientDiseaseCourse{}).Where("org_id = ? and id = ? and status = 1", orgID, id).Find(&records).Error
  99. return
  100. }
  101. func FindPatientWithDeviceById(orgID int64, patient_id int64, time int64) (patient models.SchedualPatient2, err error) {
  102. db := p_service.XTReadDB()
  103. err = db.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  104. return db.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  105. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  106. Where("user_org_id = ? AND schedule_date = ? ", orgID, time)
  107. }).Where("user_org_id=? and id = ? and status=1", orgID, patient_id).First(&patient).Error
  108. return
  109. }
  110. func GetLastAfterWeight(orgID int64, patientID int64, maxDate int64) (*models.AssessmentAfterDislysis, error) {
  111. var record models.AssessmentAfterDislysis
  112. err := p_service.XTReadDB().Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&record).Error
  113. if err != nil {
  114. if err == gorm.ErrRecordNotFound {
  115. return nil, nil
  116. } else {
  117. return nil, err
  118. }
  119. }
  120. return &record, nil
  121. }
  122. //func GetAllName(orgid int64,appid int64)(sign,err error){
  123. //
  124. // p_service.XTReadDB().Model(&sign).Where("org_id = ? and app_id =? and status = 1")
  125. //
  126. //}
  127. func FindPrintStockGoodInfoByType(types int, startTime int64, end_time int64, orgId int64) (list []*models.StockInfo, err error) {
  128. db := p_service.XTReadDB()
  129. db = db.Model(&models.StockInfo{})
  130. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgId)
  131. if types == 1 {
  132. db = db.Joins("JOIN xt_warehouse_info AS info ON info.good_id=xt_good_information.id AND info.status = 1 AND info.org_id = ?", orgId).Group("xt_good_information.id")
  133. db = db.Preload("QueryWarehousingInfo", func(db *gorm.DB) *gorm.DB {
  134. return db.Where("xt_warehouse_info.org_id = ? AND xt_warehouse_info.status = 1", orgId).Joins("JOIN xt_warehouse AS warehouse ON warehouse.id = xt_warehouse_info.warehousing_id AND warehouse.status = 1 AND warehouse.warehousing_time >=? AND warehouse.warehousing_time<= ? AND warehouse.org_id = ?", startTime, end_time, orgId)
  135. })
  136. } else if types == 2 {
  137. db = db.Joins("JOIN xt_sales_return_info AS info ON info.good_id=xt_good_information.id AND info.status = 1 AND info.org_id = ?", orgId).Group("xt_good_information.id")
  138. db = db.Preload("QuerySalesReturnInfo", func(db *gorm.DB) *gorm.DB {
  139. return db.Where("xt_sales_return_info.org_id = ? AND xt_sales_return_info.status = 1", orgId).Joins("JOIN xt_sales_return AS sales ON sales.id = xt_sales_return_info.sales_return_id AND sales.status = 1 AND sales.return_time >=? AND sales.return_time<= ? AND sales.org_id = ?", startTime, end_time, orgId)
  140. })
  141. } else if types == 3 {
  142. db = db.Joins("JOIN xt_warehouse_out_info AS info ON info.good_id=xt_good_information.id AND info.status = 1 AND info.org_id = ?", orgId).Group("xt_good_information.id")
  143. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  144. return db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1", orgId).Joins("JOIN xt_warehouse_out ON xt_warehouse_out.id = xt_warehouse_out_info.warehouse_out_id AND xt_warehouse_out.status = 1 AND xt_warehouse_out.warehouse_out_time >=? AND xt_warehouse_out.warehouse_out_time<= ? AND xt_warehouse_out.org_id = ? ", startTime, end_time, orgId)
  145. })
  146. } else if types == 4 {
  147. db = db.Joins("JOIN xt_cancel_stock_info AS info ON info.good_id=xt_good_information.id AND info.status = 1 AND info.org_id = ?", orgId).Group("xt_good_information.id")
  148. db = db.Preload("QueryCancelStockInfo", func(db *gorm.DB) *gorm.DB {
  149. return db.Where("xt_cancel_stock_info.org_id = ? AND xt_cancel_stock_info.status = 1", orgId).Joins("JOIN xt_cancel_stock AS cancel ON cancel.id = xt_cancel_stock_info.cancel_stock_id AND cancel.status = 1 AND cancel.return_time >=? AND cancel.return_time<= ? AND cancel.org_id = ?", startTime, end_time, orgId)
  150. })
  151. }
  152. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgId)
  153. err = db.Order("ctime desc").Find(&list).Error
  154. return
  155. }
  156. func GetOrgInfoTemplate(orgID int64) (models.GobalTemplate, error) {
  157. var templateInfo models.GobalTemplate
  158. var err error
  159. err = p_service.XTReadDB().Model(&models.GobalTemplate{}).Where("org_id=? and status=1", orgID).First(&templateInfo).Error
  160. return templateInfo, err
  161. }
  162. func GetDialysisOrderCount(orgID int64, patient_id int64, recordDate int64) (count int64, err error) {
  163. err = p_service.XTReadDB().Model(&models.DialysisOrder{}).Where("dialysis_date <= ? AND status = 1 AND stage = 2 AND user_org_id = ? AND patient_id = ?", recordDate, orgID, patient_id).Count(&count).Error
  164. return
  165. }