mobile_dialysis_service.go 61KB

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