mobile_dialysis_service.go 63KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "time"
  7. )
  8. // func GetSchedualPatients(orgID int64) ([]*MDialysisScheduleVM, error) {
  9. // var vms []*MDialysisScheduleVM
  10. // err := readDb.
  11. // Table("xt_schedule as sch").
  12. // Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  13. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  14. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  15. // Preload("TreatmentMode", "status = 1").
  16. // Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  17. // Preload("DialysisOrder.MonitoringRecords", "status = 1 AND user_org_id = ?", orgID).
  18. // Where("sch.status = 1 AND sch.user_org_id = ? AND schedule_date = 1536768000", orgID).
  19. // Find(&vms).
  20. // Error
  21. // return vms, err
  22. // }
  23. func MobileGetDialysisScheduals(orgID int64, scheduleDate int64, scheduleType int64) ([]*MDialysisScheduleVM, error) {
  24. var vms []*MDialysisScheduleVM
  25. db := readDb.
  26. Table("xt_schedule as sch").
  27. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  28. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  29. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  30. Preload("Advices", "status = 1 AND user_org_id = ? AND advice_type = 2", orgID).
  31. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  32. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  33. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  34. Preload("DialysisOrder.DeviceNumber", "status = 1 AND org_id = ?", orgID).
  35. Preload("TreatmentSummary", "status = 1 AND user_org_id = ?", orgID).
  36. // Preload("DialysisOrder.MonitoringRecords", func(rdb *gorm.DB) *gorm.DB {
  37. // return rdb.Where("status = 1 AND user_org_id = ?", orgID).Order("operate_time DESC")
  38. // }).
  39. Where("sch.status = 1 AND sch.user_org_id = ?", orgID)
  40. if scheduleDate != 0 {
  41. db = db.Where("schedule_date = ?", scheduleDate)
  42. }
  43. if scheduleType != 0 {
  44. db = db.Where("schedule_type = ?", scheduleType)
  45. }
  46. err := db.Find(&vms).Error
  47. return vms, err
  48. }
  49. func MobileGetWaitingScheduals(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  50. var vms []*MDialysisScheduleVM
  51. db := readDb.
  52. Table("xt_schedule as sch").
  53. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  54. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  55. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  56. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  57. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  58. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  59. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  60. Preload("Advices", "status = 1 AND user_org_id = ? AND advice_type = 2 ", orgID).
  61. Preload("TreatmentMode", "status = 1").
  62. Preload("TreatmentSummary", "status = 1 AND user_org_id = ?", orgID).
  63. Where("sch.status = 1 AND sch.user_org_id = ?", orgID)
  64. if scheduleDate != 0 {
  65. db = db.Where("schedule_date = ?", scheduleDate)
  66. }
  67. err := db.Find(&vms).Error
  68. return vms, err
  69. }
  70. type VMTreatmentSummary struct {
  71. ID int64 `gorm:"column:id" json:"id"`
  72. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  73. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  74. AssessmentDate int64 `gorm:"column:assessment_date" json:"assessment_date"`
  75. DialysisSummary string `gorm:"column:dialysis_summary" json:"dialysis_summary" form:"dialysis_summary"`
  76. }
  77. func (VMTreatmentSummary) TableName() string {
  78. return "xt_treatment_summary"
  79. }
  80. type MDialysisScheduleVM struct {
  81. ID int64 `gorm:"column:id" json:"id"`
  82. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  83. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  84. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  85. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  86. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  87. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  88. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  89. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  90. Status int64 `gorm:"column:status" json:"status"`
  91. SchedualPatient *MSchedualPatientVMList `gorm:"ForeignKey:PatientId" json:"patient"`
  92. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  93. DialysisOrder *MDialysisOrderVMList `gorm:"ForeignKey:DialysisDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"dialysis_order"`
  94. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:RecordDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"prescription"`
  95. AssessmentBeforeDislysis *models.PredialysisEvaluationList `gorm:"ForeignKey:AssessmentDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"assessment_before_dislysis"`
  96. Advices []*VMDoctorAdvice `gorm:"ForeignKey:AdviceDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"doctor_advice"`
  97. TreatmentSummary *VMTreatmentSummary `gorm:"ForeignKey:AssessmentDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"treatment_summary"`
  98. }
  99. func (MDialysisScheduleVM) TableName() string {
  100. return "xt_schedule"
  101. }
  102. type MDeviceNumberVM struct {
  103. models.DeviceNumber
  104. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  105. }
  106. func (MDeviceNumberVM) TableName() string {
  107. return "xt_device_number"
  108. }
  109. type MSchedualPatientVMList struct {
  110. ID int64 `gorm:"column:id" json:"id" form:"id"`
  111. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  112. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  113. PatientType int64 `gorm:"column:patient_type" json:"patient_type" form:"patient_type"`
  114. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  115. Avatar string `gorm:"column:avatar" json:"avatar" form:"avatar"`
  116. Gender int64 `gorm:"column:gender" json:"gender" form:"gender"`
  117. Birthday int64 `gorm:"column:birthday" json:"birthday" form:"birthday"`
  118. Age int64 `gorm:"column:age" json:"age"`
  119. Name string `gorm:"column:name" json:"name" form:"name"`
  120. IdCardNo string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
  121. UserSysBeforeCount int64 `gorm:"column:user_sys_before_count" json:"user_sys_before_count" form:"user_sys_before_count"`
  122. }
  123. func (MSchedualPatientVMList) TableName() string {
  124. return "xt_patients"
  125. }
  126. type MSchedualPatientVM struct {
  127. ID int64 `gorm:"column:id" json:"id" form:"id"`
  128. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  129. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  130. PatientType int64 `gorm:"column:patient_type" json:"patient_type" form:"patient_type"`
  131. Avatar string `gorm:"column:avatar" json:"avatar" form:"avatar"`
  132. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  133. AdmissionNumber string `gorm:"column:admission_number" json:"admission_number" form:"admission_number"`
  134. Source int64 `gorm:"column:source" json:"source" form:"source"`
  135. Lapseto int64 `gorm:"column:lapseto" json:"lapseto" form:"lapseto"`
  136. PartitionId int64 `gorm:"column:partition_id" json:"partition_id" form:"partition_id"`
  137. BedId int64 `gorm:"column:bed_id" json:"bed_id" form:"bed_id"`
  138. Name string `gorm:"column:name" json:"name" form:"name"`
  139. Alias string `gorm:"column:alias" json:"alias" form:"alias"`
  140. Gender int64 `gorm:"column:gender" json:"gender" form:"gender"`
  141. Nation string `gorm:"column:nation" json:"nation" form:"nation"`
  142. NativePlace string `gorm:"column:native_place" json:"native_place" form:"native_place"`
  143. MaritalStatus int64 `gorm:"column:marital_status" json:"marital_status" form:"marital_status"`
  144. IdCardNo string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
  145. Birthday int64 `gorm:"column:birthday" json:"birthday" form:"birthday"`
  146. ReimbursementWayId int64 `gorm:"column:reimbursement_way_id" json:"reimbursement_way_id" form:"reimbursement_way_id"`
  147. HealthCareType int64 `gorm:"column:health_care_type" json:"health_care_type" form:"health_care_type"`
  148. HealthCareNo string `gorm:"column:health_care_no" json:"health_care_no" form:"health_care_no"`
  149. HealthCareDueDate int64 `gorm:"column:health_care_due_date" json:"health_care_due_date" form:"health_care_due_date"`
  150. Height int64 `gorm:"column:height" json:"height" form:"height"`
  151. BloodType int64 `gorm:"column:blood_type" json:"blood_type" form:"blood_type"`
  152. Rh int64 `gorm:"column:rh" json:"rh" form:"rh"`
  153. HealthCareDueAlertDate int64 `gorm:"column:health_care_due_alert_date" json:"health_care_due_alert_date" form:"health_care_due_alert_date"`
  154. EducationLevel int64 `gorm:"column:education_level" json:"education_level" form:"education_level"`
  155. Profession int64 `gorm:"column:profession" json:"profession" form:"profession"`
  156. Phone string `gorm:"column:phone" json:"phone" form:"phone"`
  157. HomeTelephone string `gorm:"column:home_telephone" json:"home_telephone" form:"home_telephone"`
  158. RelativePhone string `gorm:"column:relative_phone" json:"relative_phone" form:"relative_phone"`
  159. RelativeRelations string `gorm:"column:relative_relations" json:"relative_relations" form:"relative_relations"`
  160. HomeAddress string `gorm:"column:home_address" json:"home_address" form:"home_address"`
  161. WorkUnit string `gorm:"column:work_unit" json:"work_unit" form:"work_unit"`
  162. UnitAddress string `gorm:"column:unit_address" json:"unit_address" form:"unit_address"`
  163. Children int64 `gorm:"column:children" json:"children" form:"children"`
  164. ReceivingDate int64 `gorm:"column:receiving_date" json:"receiving_date" form:"receiving_date"`
  165. IsHospitalFirstDialysis int64 `gorm:"column:is_hospital_first_dialysis" json:"is_hospital_first_dialysis" form:"is_hospital_first_dialysis"`
  166. FirstDialysisDate int64 `gorm:"column:first_dialysis_date" json:"first_dialysis_date" form:"first_dialysis_date"`
  167. FirstDialysisHospital string `gorm:"column:first_dialysis_hospital" json:"first_dialysis_hospital" form:"first_dialysis_hospital"`
  168. InductionPeriod int64 `gorm:"column:induction_period" json:"induction_period" form:"induction_period"`
  169. InitialDialysis int64 `gorm:"column:initial_dialysis" json:"initial_dialysis" form:"initial_dialysis"`
  170. TotalDialysis int64 `gorm:"column:total_dialysis" json:"total_dialysis" form:"total_dialysis"`
  171. AttendingDoctorId int64 `gorm:"column:attending_doctor_id" json:"attending_doctor_id" form:"attending_doctor_id"`
  172. HeadNurseId int64 `gorm:"column:head_nurse_id" json:"head_nurse_id" form:"head_nurse_id"`
  173. Evaluate string `gorm:"column:evaluate" json:"evaluate" form:"evaluate"`
  174. Diagnose string `gorm:"column:diagnose" json:"diagnose" form:"diagnose"`
  175. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  176. RegistrarsId int64 `gorm:"column:registrars_id" json:"registrars_id" form:"registrars_id"`
  177. Registrars string `gorm:"column:registrars" json:"registrars" form:"registrars"`
  178. QrCode string `gorm:"column:qr_code" json:"qr_code" form:"qr_code"`
  179. BindingState int64 `gorm:"column:binding_state" json:"binding_state" form:"binding_state"`
  180. PatientComplains string `gorm:"column:patient_complains" json:"patient_complains"` // 主诉
  181. PresentHistory string `gorm:"column:present_history" json:"present_history"` // 现病史
  182. PastHistory string `gorm:"column:past_history" json:"past_history"` // 既往史
  183. Temperature float64 `gorm:"column:temperature" json:"temperature"` // 体格检查-体温
  184. Pulse int64 `gorm:"column:pulse" json:"pulse"` // 体格检查-脉搏
  185. Respiratory int64 `gorm:"column:respiratory" json:"respiratory"` // 体格检查-呼吸频率
  186. SBP int64 `gorm:"column:sbp" json:"sbp"` // 体格检查-收缩压
  187. DBP int64 `gorm:"column:dbp" json:"dbp"` // 体格检查-舒张压
  188. Status int64 `gorm:"column:status" json:"status" form:"status"`
  189. Age int64 `gorm:"column:age" json:"age"`
  190. IsOpenRemind int64 `gorm:"column:is_open_remind" json:"is_open_remind"`
  191. DialysisAge int64 `gorm:"column:dialysis_age" json:"dialysis_age" form:"dialysis_age"`
  192. ExpenseKind int64 `gorm:"column:expense_kind" json:"expense_kind" form:"expense_kind"`
  193. TellPhone string `gorm:"column:tell_phone" json:"tell_phone" form:"tell_phone"`
  194. FirstTreatmentDate int64 `gorm:"column:first_treatment_date" json:"first_treatment_date" form:"first_treatment_date"`
  195. ContactName string `gorm:"column:contact_name" json:"contact_name" form:"contact_name"`
  196. UserSysBeforeCount int64 `gorm:"column:user_sys_before_count" json:"user_sys_before_count" form:"user_sys_before_count"`
  197. }
  198. func (MSchedualPatientVM) TableName() string {
  199. return "xt_patients"
  200. }
  201. type MDialysisOrderVM struct {
  202. ID int64 `gorm:"column:id" json:"id"`
  203. DialysisDate int64 `gorm:"column:dialysis_date" json:"dialysis_date"`
  204. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  205. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  206. // PrescriptionId int64 `gorm:"column:prescription_id" json:"prescription_id"`
  207. Stage int64 `gorm:"column:stage" json:"stage"`
  208. // Remark string `gorm:"column:remark" json:"remark"`
  209. BedID int64 `gorm:"column:bed_id" json:"bed_id"`
  210. StartNurse int64 `gorm:"column:start_nurse" json:"start_nurse"`
  211. FinishNurse int64 `gorm:"column:finish_nurse" json:"finish_nurse"`
  212. Status int64 `gorm:"column:status" json:"status"`
  213. PunctureNurse int64 `gorm:"column:puncture_nurse" json:"puncture_nurse"`
  214. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedID" json:"device_number"`
  215. MonitoringRecords []*models.MonitoringRecord `gorm:"ForeignKey:DialysisOrderId" json:"monitoring_records"`
  216. Creator int64 `gorm:"column:creator" json:"creator"`
  217. Modifier int64 `gorm:"column:modifier" json:"modifier"`
  218. FinishCreator int64 `gorm:"column:finish_creator" json:"finish_creator"`
  219. FinishModifier int64 `gorm:"column:finish_modifier" json:"finish_modifier"`
  220. SchedualType int64 `gorm:"column:schedual_type" json:"schedual_type"`
  221. WashpipeNurse int64 `gorm:"column:washpipe_nurse" json:"washpipe_nurse" form:"washpipe_nurse"`
  222. }
  223. func (MDialysisOrderVM) TableName() string {
  224. return "xt_dialysis_order"
  225. }
  226. type MDialysisOrderVMList struct {
  227. ID int64 `gorm:"column:id" json:"id"`
  228. DialysisDate int64 `gorm:"column:dialysis_date" json:"dialysis_date"`
  229. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  230. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  231. // PrescriptionId int64 `gorm:"column:prescription_id" json:"prescription_id"`
  232. Stage int64 `gorm:"column:stage" json:"stage"`
  233. // Remark string `gorm:"column:remark" json:"remark"`
  234. BedID int64 `gorm:"column:bed_id" json:"bed_id"`
  235. StartNurse int64 `gorm:"column:start_nurse" json:"start_nurse"`
  236. Status int64 `gorm:"column:status" json:"status"`
  237. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedID" json:"device_number"`
  238. Creator int64 `gorm:"column:creator" json:"creator"`
  239. WashpipeNurse int64 `gorm:"column:washpipe_nurse" json:"washpipe_nurse" form:"washpipe_nurse"`
  240. }
  241. func (MDialysisOrderVMList) TableName() string {
  242. return "xt_dialysis_order"
  243. }
  244. type VMDoctorAdvice struct {
  245. ID int64 `gorm:"column:id" json:"id" form:"id"`
  246. GroupNo int64 `gorm:"column:groupno" json:"groupno" form:"groupno"`
  247. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  248. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  249. AdviceDate int64 `gorm:"column:advice_date" json:"advice_date" form:"advice_date"`
  250. Status int64 `gorm:"column:status" json:"status" form:"status"`
  251. ExecutionState int64 `gorm:"column:execution_state" json:"execution_state" form:"execution_state"`
  252. RecordDate int64 `gorm:"column:record_date" json:"record_date"`
  253. }
  254. func (VMDoctorAdvice) TableName() string {
  255. return "xt_doctor_advice"
  256. }
  257. // 获取透析记录
  258. func MobileGetDialysisRecord(orgID int64, patientID int64, recordDate int64) (*models.DialysisOrder, error) {
  259. var record models.DialysisOrder
  260. err := readDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgID, patientID, recordDate).First(&record).Error
  261. if err != nil {
  262. if err == gorm.ErrRecordNotFound {
  263. return nil, nil
  264. } else {
  265. return nil, err
  266. }
  267. }
  268. return &record, nil
  269. }
  270. // 用户基本信息
  271. func MobileGetPatientDetail(orgID int64, patientID int64) (*MPatient, error) {
  272. var patient MPatient
  273. err := readDb.Model(&MPatient{}).Where("status = 1 AND user_org_id = ? AND id = ?", orgID, patientID).First(&patient).Error
  274. if err != nil {
  275. if err == gorm.ErrRecordNotFound {
  276. return nil, nil
  277. } else {
  278. return nil, err
  279. }
  280. }
  281. return &patient, nil
  282. }
  283. // 用户排班信息
  284. func MobileGetSchedualDetail(orgID int64, patientID int64, schedualDate int64) (*MDialysisScheduleVM, error) {
  285. var vm MDialysisScheduleVM
  286. err := readDb.
  287. Table("xt_schedule").
  288. // Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  289. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  290. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND patient_id = ?", orgID, schedualDate, patientID).
  291. First(&vm).Error
  292. if err != nil {
  293. if err == gorm.ErrRecordNotFound {
  294. return nil, nil
  295. } else {
  296. return nil, err
  297. }
  298. }
  299. return &vm, err
  300. }
  301. // 用户排班信息
  302. func MobileGetPatientSchedual(orgID int64, patientID int64, schedualDate int64) (*models.Schedule, error) {
  303. var schedule models.Schedule
  304. err := readDb.
  305. Table("xt_schedule").
  306. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND patient_id = ?", orgID, schedualDate, patientID).
  307. First(&schedule).Error
  308. if err != nil {
  309. if err == gorm.ErrRecordNotFound {
  310. return nil, nil
  311. } else {
  312. return nil, err
  313. }
  314. }
  315. return &schedule, nil
  316. }
  317. type MPatient struct {
  318. ID int64 `gorm:"column:id" json:"id" form:"id"`
  319. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  320. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  321. PatientType int64 `gorm:"column:patient_type" json:"patient_type" form:"patient_type"`
  322. Avatar string `gorm:"column:avatar" json:"avatar" form:"avatar"`
  323. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  324. AdmissionNumber string `gorm:"column:admission_number" json:"admission_number" form:"admission_number"`
  325. Source int64 `gorm:"column:source" json:"source" form:"source"`
  326. Lapseto int64 `gorm:"column:lapseto" json:"lapseto" form:"lapseto"`
  327. PartitionId int64 `gorm:"column:partition_id" json:"partition_id" form:"partition_id"`
  328. BedId int64 `gorm:"column:bed_id" json:"bed_id" form:"bed_id"`
  329. Name string `gorm:"column:name" json:"name" form:"name"`
  330. Alias string `gorm:"column:alias" json:"alias" form:"alias"`
  331. Gender int64 `gorm:"column:gender" json:"gender" form:"gender"`
  332. Nation string `gorm:"column:nation" json:"nation" form:"nation"`
  333. NativePlace string `gorm:"column:native_place" json:"native_place" form:"native_place"`
  334. MaritalStatus int64 `gorm:"column:marital_status" json:"marital_status" form:"marital_status"`
  335. IdCardNo string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
  336. Birthday int64 `gorm:"column:birthday" json:"birthday" form:"birthday"`
  337. ReimbursementWayId int64 `gorm:"column:reimbursement_way_id" json:"reimbursement_way_id" form:"reimbursement_way_id"`
  338. HealthCareType int64 `gorm:"column:health_care_type" json:"health_care_type" form:"health_care_type"`
  339. HealthCareNo string `gorm:"column:health_care_no" json:"health_care_no" form:"health_care_no"`
  340. HealthCareDueDate int64 `gorm:"column:health_care_due_date" json:"health_care_due_date" form:"health_care_due_date"`
  341. Height int64 `gorm:"column:height" json:"height" form:"height"`
  342. BloodType int64 `gorm:"column:blood_type" json:"blood_type" form:"blood_type"`
  343. Rh int64 `gorm:"column:rh" json:"rh" form:"rh"`
  344. HealthCareDueAlertDate int64 `gorm:"column:health_care_due_alert_date" json:"health_care_due_alert_date" form:"health_care_due_alert_date"`
  345. EducationLevel int64 `gorm:"column:education_level" json:"education_level" form:"education_level"`
  346. Profession int64 `gorm:"column:profession" json:"profession" form:"profession"`
  347. Phone string `gorm:"column:phone" json:"phone" form:"phone"`
  348. HomeTelephone string `gorm:"column:home_telephone" json:"home_telephone" form:"home_telephone"`
  349. RelativePhone string `gorm:"column:relative_phone" json:"relative_phone" form:"relative_phone"`
  350. RelativeRelations string `gorm:"column:relative_relations" json:"relative_relations" form:"relative_relations"`
  351. HomeAddress string `gorm:"column:home_address" json:"home_address" form:"home_address"`
  352. WorkUnit string `gorm:"column:work_unit" json:"work_unit" form:"work_unit"`
  353. UnitAddress string `gorm:"column:unit_address" json:"unit_address" form:"unit_address"`
  354. Children int64 `gorm:"column:children" json:"children" form:"children"`
  355. ReceivingDate int64 `gorm:"column:receiving_date" json:"receiving_date" form:"receiving_date"`
  356. IsHospitalFirstDialysis int64 `gorm:"column:is_hospital_first_dialysis" json:"is_hospital_first_dialysis" form:"is_hospital_first_dialysis"`
  357. FirstDialysisDate int64 `gorm:"column:first_dialysis_date" json:"first_dialysis_date" form:"first_dialysis_date"`
  358. FirstDialysisHospital string `gorm:"column:first_dialysis_hospital" json:"first_dialysis_hospital" form:"first_dialysis_hospital"`
  359. InductionPeriod int64 `gorm:"column:induction_period" json:"induction_period" form:"induction_period"`
  360. InitialDialysis int64 `gorm:"column:initial_dialysis" json:"initial_dialysis" form:"initial_dialysis"`
  361. TotalDialysis int64 `gorm:"column:total_dialysis" json:"total_dialysis" form:"total_dialysis"`
  362. AttendingDoctorId int64 `gorm:"column:attending_doctor_id" json:"attending_doctor_id" form:"attending_doctor_id"`
  363. HeadNurseId int64 `gorm:"column:head_nurse_id" json:"head_nurse_id" form:"head_nurse_id"`
  364. Evaluate string `gorm:"column:evaluate" json:"evaluate" form:"evaluate"`
  365. Diagnose string `gorm:"column:diagnose" json:"diagnose" form:"diagnose"`
  366. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  367. RegistrarsId int64 `gorm:"column:registrars_id" json:"registrars_id" form:"registrars_id"`
  368. Registrars string `gorm:"column:registrars" json:"registrars" form:"registrars"`
  369. QrCode string `gorm:"column:qr_code" json:"qr_code" form:"qr_code"`
  370. BindingState int64 `gorm:"column:binding_state" json:"binding_state" form:"binding_state"`
  371. PatientComplains string `gorm:"column:patient_complains" json:"patient_complains"` // 主诉
  372. PresentHistory string `gorm:"column:present_history" json:"present_history"` // 现病史
  373. PastHistory string `gorm:"column:past_history" json:"past_history"` // 既往史
  374. Temperature float64 `gorm:"column:temperature" json:"temperature"` // 体格检查-体温
  375. Pulse int64 `gorm:"column:pulse" json:"pulse"` // 体格检查-脉搏
  376. Respiratory int64 `gorm:"column:respiratory" json:"respiratory"` // 体格检查-呼吸频率
  377. SBP int64 `gorm:"column:sbp" json:"sbp"` // 体格检查-收缩压
  378. DBP int64 `gorm:"column:dbp" json:"dbp"` // 体格检查-舒张压
  379. Status int64 `gorm:"column:status" json:"status" form:"status"`
  380. Age int64 `gorm:"column:age" json:"age"`
  381. IsOpenRemind int64 `gorm:"column:is_open_remind" json:"is_open_remind"`
  382. CreatedTime int64 `gorm:"column:created_time" json:"created_time" form:"created_time"`
  383. DialysisAge int64 `gorm:"column:dialysis_age" json:"dialysis_age" form:"dialysis_age"`
  384. ExpenseKind int64 `gorm:"column:expense_kind" json:"expense_kind" form:"expense_kind"`
  385. TellPhone string `gorm:"column:tell_phone" json:"tell_phone" form:"tell_phone"`
  386. FirstTreatmentDate int64 `gorm:"column:first_treatment_date" json:"first_treatment_date" form:"first_treatment_date"`
  387. ContactName string `gorm:"column:contact_name" json:"contact_name" form:"contact_name"`
  388. UserSysBeforeCount int64 `gorm:"column:user_sys_before_count" json:"user_sys_before_count" form:"user_sys_before_count"`
  389. }
  390. func (MPatient) TableName() string {
  391. return "xt_patients"
  392. }
  393. // 接诊评估
  394. func MobileGetReceiverTreatmentAccessRecord(orgID int64, patientID int64, recordDate int64) (*models.ReceiveTreatmentAsses, error) {
  395. var record models.ReceiveTreatmentAsses
  396. err = readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  397. if err != nil {
  398. if err == gorm.ErrRecordNotFound {
  399. return nil, nil
  400. } else {
  401. return nil, err
  402. }
  403. }
  404. return &record, nil
  405. }
  406. // 透前评估
  407. func MobileGetPredialysisEvaluation(orgID int64, patientID int64, recordDate int64) (*models.PredialysisEvaluation, error) {
  408. fmt.Println("recordDate", recordDate)
  409. var record models.PredialysisEvaluation
  410. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  411. if err != nil {
  412. if err == gorm.ErrRecordNotFound {
  413. return nil, nil
  414. } else {
  415. return nil, err
  416. }
  417. }
  418. return &record, nil
  419. }
  420. // 获取 maxDate 之前一次的透前评估记录
  421. func MobileGetLastTimePredialysisEvaluation(orgID int64, patientID int64, maxDate int64) (*models.PredialysisEvaluation, error) {
  422. var record models.PredialysisEvaluation
  423. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&record).Error
  424. if err != nil {
  425. if err == gorm.ErrRecordNotFound {
  426. return nil, nil
  427. } else {
  428. return nil, err
  429. }
  430. }
  431. return &record, nil
  432. }
  433. // 临时医嘱
  434. func MobileGetDoctorAdvices(orgID int64, patientID int64, recordDate int64) ([]*models.DoctorAdvice, error) {
  435. var records []*models.DoctorAdvice
  436. // err := readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).Find(&records).Error
  437. err := readDb.
  438. Model(&models.DoctorAdvice{}).
  439. Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ? and (advice_type = 2 or advice_type = 3)", patientID, orgID, recordDate).
  440. Select("id,user_org_id,patient_id,advice_type,advice_date,record_date,start_time,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,IF(parent_id>0, parent_id, id) as advice_order,groupno").
  441. Order("start_time asc, groupno desc, advice_order desc, id").
  442. Scan(&records).Error
  443. if err != nil {
  444. return nil, err
  445. }
  446. return records, nil
  447. }
  448. func MobileGetDoctorAdvicesByGroups(orgID int64, patientID int64, recordDate int64) ([]*models.DoctorAdvice, error) {
  449. var records []*models.DoctorAdvice
  450. // err := readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).Find(&records).Error
  451. err := readDb.
  452. Model(&models.DoctorAdvice{}).
  453. Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ? and advice_type = 2", patientID, orgID, recordDate).
  454. Select("id, user_org_id, patient_id, advice_type, advice_date, record_date, start_time, 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").
  455. Order("start_time asc, groupno desc, advice_order desc, id asc").
  456. Scan(&records).Error
  457. if err != nil {
  458. return nil, err
  459. }
  460. return records, nil
  461. }
  462. // 透析记录
  463. func MobileGetSchedualDialysisRecord(orgID int64, patientID int64, recordDate int64) (*models.DialysisOrder, error) {
  464. var record models.DialysisOrder
  465. err := readDb.Model(&models.DialysisOrder{}).Preload("DeviceNumber", "org_id = ? AND status = 1", orgID).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgID, patientID, recordDate).First(&record).Error
  466. if err != nil {
  467. if err == gorm.ErrRecordNotFound {
  468. return nil, nil
  469. } else {
  470. return nil, err
  471. }
  472. }
  473. return &record, nil
  474. }
  475. // 双人核对
  476. func MobileGetDoubleCheck(orgID int64, patientID int64, recordDate int64) (*models.DoubleCheck, error) {
  477. var record models.DoubleCheck
  478. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? and user_org_id = ? and status = 1 and check_date = ?", patientID, orgID, recordDate).First(&record).Error
  479. if err != nil {
  480. if err == gorm.ErrRecordNotFound {
  481. return nil, nil
  482. } else {
  483. return nil, err
  484. }
  485. }
  486. return &record, nil
  487. }
  488. // 透析监测记录
  489. func MobileGetMonitorRecords(orgID int64, patientID int64, recordDate int64) ([]*models.MonitoringRecord, error) {
  490. var records []*models.MonitoringRecord
  491. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1 and monitoring_date = ?", patientID, orgID, recordDate).Order("operate_time asc").Find(&records).Error
  492. if err != nil {
  493. return nil, err
  494. }
  495. return records, nil
  496. }
  497. func MobileGetMonitorRecordFirst(orgID int64, patientID int64, recordDate int64) (*models.MonitoringRecord, error) {
  498. var records models.MonitoringRecord
  499. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1 and monitoring_date = ?", patientID, orgID, recordDate).Order("operate_time asc").First(&records).Error
  500. if err != nil {
  501. return nil, err
  502. }
  503. return &records, nil
  504. }
  505. func MobileGetLastMonitorRecord(orgID int64, patientID int64, beforeDate int64) (*models.MonitoringRecord, error) {
  506. var record models.MonitoringRecord
  507. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1 and monitoring_date = ?", patientID, orgID, beforeDate).Order("operate_time desc").First(&record).Error
  508. if err != nil {
  509. if err == gorm.ErrRecordNotFound {
  510. return nil, nil
  511. } else {
  512. return nil, err
  513. }
  514. }
  515. return &record, nil
  516. }
  517. // 透后评估
  518. func MobileGetAssessmentAfterDislysis(orgID int64, patientID int64, recordDate int64) (*models.AssessmentAfterDislysis, error) {
  519. var record models.AssessmentAfterDislysis
  520. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  521. if err != nil {
  522. if err == gorm.ErrRecordNotFound {
  523. return nil, nil
  524. } else {
  525. return nil, err
  526. }
  527. }
  528. return &record, nil
  529. }
  530. // 获取 maxDate 之前一次的透后评估记录
  531. func MobileGetLastTimeAssessmentAfterDislysis(orgID int64, patientID int64, maxDate int64) (*models.AssessmentAfterDislysis, error) {
  532. var record models.AssessmentAfterDislysis
  533. err := readDb.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
  534. if err != nil {
  535. if err == gorm.ErrRecordNotFound {
  536. return nil, nil
  537. } else {
  538. return nil, err
  539. }
  540. }
  541. return &record, nil
  542. }
  543. func MobileGetLast(orgID int64, patientID int64, maxDate int64) (models.AssessmentAfterDislysis, error) {
  544. dislysis := models.AssessmentAfterDislysis{}
  545. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&dislysis).Error
  546. return dislysis, err
  547. }
  548. // 治疗小结
  549. func MobileGetTreatmentSummary(orgID int64, patientID int64, recordDate int64) (*models.TreatmentSummary, error) {
  550. var record models.TreatmentSummary
  551. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  552. if err != nil {
  553. if err == gorm.ErrRecordNotFound {
  554. return nil, nil
  555. } else {
  556. return nil, err
  557. }
  558. }
  559. return &record, nil
  560. }
  561. // 透析处方
  562. func MobileGetDialysisPrescribe(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  563. var record models.DialysisPrescription
  564. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  565. if err != nil {
  566. if err == gorm.ErrRecordNotFound {
  567. return nil, nil
  568. } else {
  569. return nil, err
  570. }
  571. }
  572. return &record, nil
  573. }
  574. // 透析方案
  575. func MobileGetDialysisSolution(orgID int64, patientID int64) (*models.DialysisSolution, error) {
  576. var record models.DialysisSolution
  577. err := readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  578. if err != nil {
  579. if err == gorm.ErrRecordNotFound {
  580. return nil, nil
  581. } else {
  582. return nil, err
  583. }
  584. }
  585. return &record, nil
  586. }
  587. func MobileGetPatientById(orgID int64, patientID int64) (*models.Patients, error) {
  588. var patient models.Patients
  589. err := readDb.Model(&models.Patients{}).Where("id = ? and user_org_id = ? and status = 1", patientID, orgID).First(&patient).Error
  590. if err != nil {
  591. if err == gorm.ErrRecordNotFound {
  592. return nil, nil
  593. } else {
  594. return nil, err
  595. }
  596. }
  597. return &patient, nil
  598. }
  599. func DisableMonitor(orgID int64, patientID int64, recordID int64, admin_user_id int64) error {
  600. fmt.Println()
  601. tx := writeDb.Begin()
  602. updateTime := time.Now().Unix()
  603. err := tx.Model(&models.MonitoringRecord{}).Where("user_org_id = ? AND patient_id = ? AND id = ? AND status = 1 ", orgID, patientID, recordID).Updates(map[string]interface{}{"status": 0, "updated_time": updateTime, "modify": admin_user_id}).Error
  604. if err != nil {
  605. tx.Rollback()
  606. return err
  607. }
  608. tx.Commit()
  609. return nil
  610. }
  611. func GetMonitor(orgID int64, patientID int64, id int64) (*models.MonitoringRecord, error) {
  612. var monitor models.MonitoringRecord
  613. var err error
  614. err = readDb.Model(&models.MonitoringRecord{}).Where("id = ? AND user_org_id = ? AND patient_id = ? AND status = 1 ", id, orgID, patientID).Find(&monitor).Error
  615. if err == gorm.ErrRecordNotFound {
  616. return nil, nil
  617. }
  618. if err != nil {
  619. return nil, err
  620. }
  621. return &monitor, nil
  622. }
  623. type MScheduleDoctorAdviceVM struct {
  624. ID int64 `gorm:"column:id" json:"id"`
  625. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  626. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  627. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  628. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  629. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  630. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  631. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  632. Status int64 `gorm:"column:status" json:"status"`
  633. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:DialysisDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"dialysis_order"`
  634. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  635. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  636. DoctorAdvices []*MDoctorAdviceVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  637. }
  638. func (MScheduleDoctorAdviceVM) TableName() string {
  639. return "xt_schedule"
  640. }
  641. type MDoctorAdviceVM struct {
  642. ID int64 `gorm:"column:id" json:"id"`
  643. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  644. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  645. AdviceType int64 `gorm:"column:advice_type" json:"advice_type"`
  646. AdviceDate int64 `gorm:"column:advice_date" json:"advice_date"`
  647. StartTime int64 `gorm:"column:start_time" json:"start_time"`
  648. AdviceName string `gorm:"column:advice_name" json:"advice_name"`
  649. AdviceDesc string `gorm:"column:advice_desc" json:"advice_desc"`
  650. ReminderDate int64 `gorm:"column:reminder_date" json:"reminder_date"`
  651. SingleDose float64 `gorm:"column:single_dose" json:"single_dose"`
  652. SingleDoseUnit string `gorm:"column:single_dose_unit" json:"single_dose_unit"`
  653. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number"`
  654. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit"`
  655. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way"`
  656. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency"`
  657. AdviceDoctor int64 `gorm:"column:advice_doctor" json:"advice_doctor"`
  658. Status int64 `gorm:"column:status" json:"status"`
  659. CreatedTime int64 `gorm:"column:created_time" json:"created_time"`
  660. UpdatedTime int64 `gorm:"column:updated_time" json:"updated_time"`
  661. AdviceAffirm string `gorm:"column:advice_affirm" json:"advice_affirm"`
  662. Remark string `gorm:"column:remark" json:"remark"`
  663. StopTime int64 `gorm:"column:stop_time" json:"stop_time"`
  664. StopReason string `gorm:"column:stop_reason" json:"stop_reason"`
  665. StopDoctor int64 `gorm:"column:stop_doctor" json:"stop_doctor"`
  666. StopState int64 `gorm:"column:stop_state" json:"stop_state"`
  667. ParentId int64 `gorm:"column:parent_id" json:"parent_id"`
  668. ExecutionTime int64 `gorm:"column:execution_time" json:"execution_time"`
  669. ExecutionStaff int64 `gorm:"column:execution_staff" json:"execution_staff"`
  670. ExecutionState int64 `gorm:"column:execution_state" json:"execution_state"`
  671. Checker int64 `gorm:"column:checker" json:"checker"`
  672. RecordDate int64 `gorm:"column:record_date" json:"record_date"`
  673. DialysisOrderId int64 `gorm:"column:dialysis_order_id" json:"dialysis_order_id"`
  674. CheckTime int64 `gorm:"column:check_time" json:"check_time"`
  675. CheckState int64 `gorm:"column:check_state" json:"check_state"`
  676. DrugSpec float64 `gorm:"column:drug_spec" json:"drug_spec"`
  677. DrugSpecUnit string `gorm:"column:drug_spec_unit" json:"drug_spec_unit"`
  678. Groupno int64 `gorm:"column:groupno" json:"groupno"`
  679. RemindType int64 `gorm:"column:remind_type" json:"remind_type"`
  680. FrequencyType int64 `gorm:"column:frequency_type" json:"frequency_type"`
  681. DayCount int64 `gorm:"column:day_count" json:"day_count"`
  682. WeekDay string `gorm:"column:week_day" json:"week_day"`
  683. TemplateId string `gorm:"column:template_id" json:"template_id"`
  684. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  685. }
  686. func (MDoctorAdviceVM) TableName() string {
  687. return "xt_doctor_advice"
  688. }
  689. func MobileGetScheduleDoctorAdvices(orgID int64, scheduleDate int64, adviceType int, patientType int, adminUserId int64) ([]*MScheduleDoctorAdviceVM, error) {
  690. var vms []*MScheduleDoctorAdviceVM
  691. adviceWhere := ""
  692. fmt.Println("advicetype", adviceType)
  693. fmt.Println("patientType", patientType)
  694. fmt.Println("adminUserId", adminUserId)
  695. adviceCondition := []interface{}{}
  696. if adviceType == 0 {
  697. if patientType == 0 {
  698. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1)"
  699. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  700. } else if patientType == 1 {
  701. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1)"
  702. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  703. } else if patientType == 2 {
  704. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND execution_staff = 0 AND(advice_type = 3 OR advice_type = 1)"
  705. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  706. }
  707. } else if adviceType == 1 {
  708. if patientType == 0 {
  709. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? "
  710. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  711. } else if patientType == 1 {
  712. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? "
  713. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  714. } else if patientType == 2 {
  715. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0"
  716. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  717. }
  718. } else if adviceType == 3 {
  719. if patientType == 0 {
  720. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? "
  721. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  722. } else if patientType == 1 {
  723. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? "
  724. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  725. } else if patientType == 2 {
  726. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0"
  727. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  728. }
  729. } else if adviceType == 2 {
  730. if patientType == 0 {
  731. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? "
  732. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  733. } else if patientType == 1 {
  734. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? "
  735. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  736. } else if patientType == 2 {
  737. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0"
  738. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  739. }
  740. }
  741. db := readDb.
  742. Table("xt_schedule").
  743. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  744. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  745. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  746. }).
  747. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  748. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  749. Preload("DoctorAdvices", adviceCondition...).
  750. Where("status = 1 AND user_org_id = ?", orgID)
  751. if scheduleDate != 0 {
  752. db = db.Where("schedule_date = ?", scheduleDate)
  753. }
  754. err := db.Find(&vms).Error
  755. return vms, err
  756. }
  757. func MobileCreateDialysisOrder(orgID int64, patientID int64, order *models.DialysisOrder) error {
  758. now := time.Now()
  759. tx := writeDb.Begin()
  760. if createOrderErr := tx.Model(&models.DialysisOrder{}).Create(order).Error; createOrderErr != nil {
  761. tx.Rollback()
  762. return createOrderErr
  763. }
  764. // 更新透析记录 ID
  765. // 透析处方
  766. if err := tx.Model(&models.DialysisPrescription{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"record_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  767. tx.Rollback()
  768. return err
  769. }
  770. // 接诊评估
  771. if err := tx.Model(&models.ReceiveTreatmentAsses{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"record_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  772. tx.Rollback()
  773. return err
  774. }
  775. // 透前评估
  776. if err := tx.Model(&models.PredialysisEvaluation{}).Where("user_org_id = ? AND patient_id = ? AND assessment_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  777. tx.Rollback()
  778. return err
  779. }
  780. // 临时医嘱
  781. if err := tx.Model(&models.DoctorAdvice{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1 AND advice_type = 2", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  782. tx.Rollback()
  783. return err
  784. }
  785. // 双人核对
  786. if err := tx.Model(&models.DoubleCheck{}).Where("user_org_id = ? AND patient_id = ? AND check_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  787. tx.Rollback()
  788. return err
  789. }
  790. // 透后评估
  791. if err := tx.Model(&models.AssessmentAfterDislysis{}).Where("user_org_id = ? AND patient_id = ? AND assessment_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  792. tx.Rollback()
  793. return err
  794. }
  795. // 治疗小结
  796. if err := tx.Model(&models.TreatmentSummary{}).Where("user_org_id = ? AND patient_id = ? AND assessment_date = ? AND status = 1", orgID, patientID, order.DialysisDate).Updates(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  797. tx.Rollback()
  798. return err
  799. }
  800. // 透析监测
  801. if err := tx.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id=? and status=1 and monitoring_date = ?", patientID, orgID, order.DialysisDate).Update(map[string]interface{}{"dialysis_order_id": order.ID, "updated_time": now.Unix()}).Error; err != nil {
  802. tx.Rollback()
  803. return err
  804. }
  805. tx.Commit()
  806. return nil
  807. }
  808. type MobileUrgentSchedulePatientVM struct {
  809. ID int64 `gorm:"column:id" json:"id"`
  810. Name string `gorm:"column:name" json:"name"`
  811. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no"`
  812. }
  813. func (MobileUrgentSchedulePatientVM) TableName() string {
  814. return "xt_patients"
  815. }
  816. type MobileUrgentScheduleTreatmentModeVM struct {
  817. ID int64 `gorm:"column:id" json:"id"`
  818. Name string `gorm:"column:name" json:"name"`
  819. }
  820. func (MobileUrgentScheduleTreatmentModeVM) TableName() string {
  821. return "xt_treatment_mode"
  822. }
  823. func MobileGetAllPatientsForUrgentSchedule(orgID int64, record_date int64) ([]*MobileUrgentSchedulePatientVM, error) {
  824. var vms []*MobileUrgentSchedulePatientVM = make([]*MobileUrgentSchedulePatientVM, 0)
  825. rows, err := readDb.Raw("SELECT p.* FROM xt_patients as p WHERE (p.user_org_id = ? AND p.status = 1 AND p.lapseto = 1) AND NOT EXISTS (Select * FROM `xt_dialysis_order` as d Where d.`dialysis_date` = ? AND d.`status` = 1 AND d.`patient_id` = p.id AND d.user_org_id = p.user_org_id )", orgID, record_date).Rows()
  826. defer rows.Close()
  827. if err != nil {
  828. return nil, err
  829. }
  830. for rows.Next() {
  831. var vm MobileUrgentSchedulePatientVM
  832. readDb.ScanRows(rows, &vm)
  833. vms = append(vms, &vm)
  834. }
  835. return vms, nil
  836. }
  837. func MobileGetAllTrearmentModesForUrgentSchedule() ([]*MobileUrgentScheduleTreatmentModeVM, error) {
  838. var modes []*MobileUrgentScheduleTreatmentModeVM
  839. err := readDb.Model(&MobileUrgentScheduleTreatmentModeVM{}).Where("status = 1").Find(&modes).Error
  840. if err != nil {
  841. return nil, err
  842. }
  843. return modes, nil
  844. }
  845. type MobileUrgentScheduleScheduleListVM struct {
  846. ID int64 `gorm:"column:id" json:"id"`
  847. // ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  848. DeviceNumberID int64 `gorm:"column:bed_id" json:"bed_id"`
  849. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  850. ScheduleType int `gorm:"column:schedule_type" json:"schedule_type"`
  851. DeviceNumber *models.DeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  852. // DeviceZone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"device_zone"`
  853. }
  854. func (MobileUrgentScheduleScheduleListVM) TableName() string {
  855. return "xt_schedule"
  856. }
  857. func MobileGetSchedulesForUrgentSchedule(orgID int64, scheduleDate int64, schedule_type int) (schedules []*MobileUrgentScheduleScheduleListVM, err error) {
  858. db := readDb.
  859. Model(&MobileUrgentScheduleScheduleListVM{}).
  860. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  861. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND schedule_type = ? ", orgID, scheduleDate, schedule_type)
  862. err = db.Find(&schedules).Error
  863. if err != nil {
  864. return nil, err
  865. }
  866. return schedules, nil
  867. }
  868. func MobileGetOtherSchedulesForUrgentSchedule(orgID int64, scheduleDate int64, schedule_type int) ([]*MobileUrgentScheduleScheduleListVM, error) {
  869. var vms []*MobileUrgentScheduleScheduleListVM = make([]*MobileUrgentScheduleScheduleListVM, 0)
  870. rows, err := readDb.Raw("SELECT * FROM xt_schedule as s WHERE s.user_org_id = ? AND s.status = 1 AND s.schedule_date = ? ", orgID, scheduleDate).Rows()
  871. defer rows.Close()
  872. if err != nil {
  873. return nil, err
  874. }
  875. for rows.Next() {
  876. var vm MobileUrgentScheduleScheduleListVM
  877. readDb.ScanRows(rows, &vm)
  878. vms = append(vms, &vm)
  879. }
  880. return vms, nil
  881. }
  882. type MobileUrgentScheduleDeviceNumberVM struct {
  883. ID int64 `gorm:"column:id" json:"id"`
  884. Number string `gorm:"column:number" json:"number"`
  885. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  886. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  887. }
  888. func (MobileUrgentScheduleDeviceNumberVM) TableName() string {
  889. return "xt_device_number"
  890. }
  891. func MobileGetAllDeviceNumbersForUrgentSchedule(orgID int64, record_date int64, schedule_type int) ([]*DeviceNumberViewModel, error) {
  892. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  893. rows, err := readDb.Raw("SELECT n.*, z.name as zone_name, g.name as group_name FROM xt_device_number as n join xt_device_zone as z on z.id = n.zone_id join xt_device_group as g on g.id = n.group_id WHERE (n.org_id = ? AND n.status = 1) AND NOT EXISTS (Select * FROM xt_schedule as s Where s.`schedule_date` = ? AND s.user_org_id = n.org_id AND s.`bed_id` = n.id AND s.`schedule_type` = ? AND s.status = 1 )", orgID, record_date, schedule_type).Rows()
  894. defer rows.Close()
  895. if err != nil {
  896. return nil, err
  897. }
  898. for rows.Next() {
  899. var vm DeviceNumberViewModel
  900. readDb.ScanRows(rows, &vm)
  901. vms = append(vms, &vm)
  902. }
  903. return vms, nil
  904. //var deviceNumbers []*MobileUrgentScheduleDeviceNumberVM
  905. //db := readDb.
  906. // Model(&MobileUrgentScheduleDeviceNumberVM{}).
  907. // Preload("Zone", "status = 1 AND org_id = ?", orgID).
  908. // Where("status = 1 AND org_id = ?", orgID)
  909. //err := db.Order("zone_id asc").Find(&deviceNumbers).Error
  910. //if err != nil {
  911. // return nil, err
  912. //}
  913. //return deviceNumbers, nil
  914. }
  915. func GetValidScheduleMonitorRecordCount() (int64, error) {
  916. var total int64
  917. err := readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND operate_time = 0 AND monitoring_date <> 0").Count(&total).Error
  918. return total, err
  919. }
  920. func GetTop1000ValidScheduleMonitorRecord() ([]*models.MonitoringRecord, error) {
  921. var monitors []*models.MonitoringRecord
  922. err := readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND operate_time = 0 AND monitoring_date <> 0").Order("id asc").Limit(100).Find(&monitors).Error
  923. if err != nil {
  924. return nil, err
  925. }
  926. return monitors, nil
  927. }
  928. func BatchUpdateMonitors(monitors []*models.MonitoringRecord) error {
  929. tx := writeDb.Begin()
  930. for index := 0; index < len(monitors); index++ {
  931. tx.Save(monitors[index])
  932. }
  933. return tx.Commit().Error
  934. }
  935. func ModifyStartDialysisOrder(order *models.DialysisOrder) error {
  936. tx := writeDb.Begin()
  937. updateTime := time.Now().Unix()
  938. err := tx.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND id = ? AND status = 1 ", order.UserOrgId, order.ID).Updates(map[string]interface{}{"start_nurse": order.StartNurse, "updated_time": updateTime, "bed_id": order.BedID, "puncture_nurse": order.PunctureNurse, "start_time": order.StartTime, "modifier": order.Modifier, "schedual_type": order.SchedualType}).Error
  939. if err != nil {
  940. tx.Rollback()
  941. return err
  942. }
  943. tx.Commit()
  944. return err
  945. }
  946. func ModifyFinishDialysisOrder(order *models.DialysisOrder) error {
  947. tx := writeDb.Begin()
  948. updateTime := time.Now().Unix()
  949. err := tx.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND id = ? AND status = 1 ", order.UserOrgId, order.ID).Updates(map[string]interface{}{"finish_nurse": order.FinishNurse, "updated_time": updateTime, "end_time": order.EndTime, "finish_modifier": order.FinishModifier}).Error
  950. if err != nil {
  951. tx.Rollback()
  952. return err
  953. }
  954. tx.Commit()
  955. return err
  956. }
  957. func MobileGetLastDryWeight(orgID int64, patientID int64) (*models.SgjPatientDryweight, error) {
  958. var record models.SgjPatientDryweight
  959. err := readDb.Model(&models.SgjPatientDryweight{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  960. if err != nil {
  961. if err == gorm.ErrRecordNotFound {
  962. return nil, nil
  963. } else {
  964. return nil, err
  965. }
  966. }
  967. return &record, nil
  968. }
  969. // 透析方案
  970. func MobileGetDialysisSolutionByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  971. var record models.DialysisSolution
  972. err := readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  973. if err != nil {
  974. if err == gorm.ErrRecordNotFound {
  975. return nil, nil
  976. } else {
  977. return nil, err
  978. }
  979. }
  980. return &record, nil
  981. }
  982. // 透析处方
  983. func MobileGetDialysisPrescribeByModeId(orgID int64, patientID int64, recordDate int64, mode_id int64) (*models.DialysisPrescription, error) {
  984. var record models.DialysisPrescription
  985. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ? AND mode_id = ?", patientID, orgID, recordDate, mode_id).First(&record).Error
  986. if err != nil {
  987. if err == gorm.ErrRecordNotFound {
  988. return nil, nil
  989. } else {
  990. return nil, err
  991. }
  992. }
  993. return &record, nil
  994. }
  995. func MobileGetLastDialysisPrescribe(orgID int64, patientID int64) (*models.DialysisPrescription, error) {
  996. var record models.DialysisPrescription
  997. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 ", patientID, orgID).Last(&record).Error
  998. if err != nil {
  999. if err == gorm.ErrRecordNotFound {
  1000. return nil, nil
  1001. } else {
  1002. return nil, err
  1003. }
  1004. }
  1005. return &record, nil
  1006. }
  1007. func MobileGetLastDialysisPrescribeByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  1008. var record models.DialysisPrescription
  1009. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  1010. if err != nil {
  1011. if err == gorm.ErrRecordNotFound {
  1012. return nil, nil
  1013. } else {
  1014. return nil, err
  1015. }
  1016. }
  1017. return &record, nil
  1018. }
  1019. func GetAllAvaildDeviceNumbers(orgID int64, record_date int64, schedule_type int) ([]*DeviceNumberViewModel, error) {
  1020. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  1021. rows, err := readDb.Raw("SELECT n.*, z.name as zone_name, g.name as group_name FROM xt_device_number as n join xt_device_zone as z on z.id = n.zone_id join xt_device_group as g on g.id = n.group_id WHERE (n.org_id = ? AND n.status = 1) AND NOT EXISTS (Select * FROM xt_schedule as s Where s.`schedule_date` = ? AND s.user_org_id = n.org_id AND s.`bed_id` = n.id AND s.`schedule_type` = ? AND s.status = 1 )", orgID, record_date, schedule_type).Rows()
  1022. defer rows.Close()
  1023. if err != nil {
  1024. return nil, err
  1025. }
  1026. for rows.Next() {
  1027. var vm DeviceNumberViewModel
  1028. readDb.ScanRows(rows, &vm)
  1029. vms = append(vms, &vm)
  1030. }
  1031. return vms, nil
  1032. }
  1033. // 获取 maxDate 之前一次的透前评估记录
  1034. func GetLastTimePredialysisEvaluation(orgID int64, patientID int64, maxDate int64) (*models.PredialysisEvaluation, error) {
  1035. var record models.PredialysisEvaluation
  1036. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date < ?", patientID, orgID, maxDate).Order("assessment_date desc").First(&record).Error
  1037. if err != nil {
  1038. if err == gorm.ErrRecordNotFound {
  1039. return nil, nil
  1040. } else {
  1041. return nil, err
  1042. }
  1043. }
  1044. return &record, nil
  1045. }
  1046. func GetLastMonitorRecord(orgID int64, patientID int64, beforeDate int64) (*models.MonitoringRecord, error) {
  1047. var record models.MonitoringRecord
  1048. err := readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id = ? and status = 1 and monitoring_date = ?", patientID, orgID, beforeDate).Order("operate_time desc").First(&record).Error
  1049. if err != nil {
  1050. if err == gorm.ErrRecordNotFound {
  1051. return nil, nil
  1052. } else {
  1053. return nil, err
  1054. }
  1055. }
  1056. return &record, nil
  1057. }
  1058. // 获取 maxDate 之前一次的透后评估记录
  1059. func GetLastTimeAssessmentAfterDislysis(orgID int64, patientID int64, maxDate int64) (*models.AssessmentAfterDislysis, error) {
  1060. var record models.AssessmentAfterDislysis
  1061. err := readDb.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
  1062. if err != nil {
  1063. if err == gorm.ErrRecordNotFound {
  1064. return nil, nil
  1065. } else {
  1066. return nil, err
  1067. }
  1068. }
  1069. return &record, nil
  1070. }
  1071. // 透析处方
  1072. func GetDialysisPrescribe(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  1073. var record models.DialysisPrescription
  1074. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  1075. if err != nil {
  1076. if err == gorm.ErrRecordNotFound {
  1077. return nil, nil
  1078. } else {
  1079. return nil, err
  1080. }
  1081. }
  1082. return &record, nil
  1083. }
  1084. // 透析方案
  1085. func GetDialysisSolution(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  1086. var record models.DialysisSolution
  1087. err := readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  1088. if err != nil {
  1089. if err == gorm.ErrRecordNotFound {
  1090. return nil, nil
  1091. } else {
  1092. return nil, err
  1093. }
  1094. }
  1095. return &record, nil
  1096. }
  1097. func GetLastDialysisPrescribeByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  1098. var record models.DialysisPrescription
  1099. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ?", patientID, orgID, mode_id).Last(&record).Error
  1100. if err != nil {
  1101. if err == gorm.ErrRecordNotFound {
  1102. return nil, nil
  1103. } else {
  1104. return nil, err
  1105. }
  1106. }
  1107. return &record, nil
  1108. }
  1109. func GetLastDryWeight(orgID int64, patientID int64) (*models.SgjPatientDryweight, error) {
  1110. var record models.SgjPatientDryweight
  1111. err := readDb.Model(&models.SgjPatientDryweight{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  1112. if err != nil {
  1113. if err == gorm.ErrRecordNotFound {
  1114. return nil, nil
  1115. } else {
  1116. return nil, err
  1117. }
  1118. }
  1119. return &record, nil
  1120. }
  1121. func MobileGetSystemDialysisPrescribeByModeId(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  1122. var record models.SystemPrescription
  1123. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).First(&record).Error
  1124. if err != nil {
  1125. if err == gorm.ErrRecordNotFound {
  1126. return nil, nil
  1127. } else {
  1128. return nil, err
  1129. }
  1130. }
  1131. return &record, nil
  1132. }
  1133. func GetSystemDialysisPrescribeByModeId(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  1134. var record models.SystemPrescription
  1135. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).Last(&record).Error
  1136. if err != nil {
  1137. if err == gorm.ErrRecordNotFound {
  1138. return nil, nil
  1139. } else {
  1140. return nil, err
  1141. }
  1142. }
  1143. return &record, nil
  1144. }
  1145. func GetDialysisOrderCount(orgID int64, patient_id int64, recordDate int64) (count int64, err error) {
  1146. err = readDb.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
  1147. return
  1148. }
  1149. func GetFirstDateOfMonth(d time.Time) time.Time {
  1150. d = d.AddDate(0, 0, -d.Day()+1)
  1151. return GetZeroTime(d)
  1152. }
  1153. func GetZeroTime(d time.Time) time.Time {
  1154. return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
  1155. }
  1156. func GetDialysisCountByPatientId(startime int64, endtime int64, patientid int64, orgid int64) (order []*models.BloodDialysisOrderCount, err error) {
  1157. db := XTReadDB().Table("xt_dialysis_order as o")
  1158. err = db.Raw("select count(o.id) as count,o.patient_id from xt_dialysis_order as o left join xt_schedule AS x ON x.patient_id = o.patient_id where o.status = 1 and o.dialysis_date>=? and o.dialysis_date<=? and o.user_org_id = ? and x.schedule_date = o.dialysis_date and x.status = 1 and o.patient_id = ?", startime, endtime, orgid, patientid).Scan(&order).Group("o.patient_id").Error
  1159. return order, err
  1160. }