mobile_dialysis_service.go 62KB

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