mobile_dialysis_service.go 87KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  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 AND advice_date = ? ", orgID, scheduleDate).
  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 = ? AND dialysis_date = ?", orgID, scheduleDate).
  34. Preload("DialysisOrder.DeviceNumber", "status = 1 AND org_id = ?", orgID).
  35. Preload("TreatmentSummary", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  36. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  37. // Preload("DialysisOrder.MonitoringRecords", func(rdb *gorm.DB) *gorm.DB {
  38. // return rdb.Where("status = 1 AND user_org_id = ?", orgID).Order("operate_time DESC")
  39. // }).
  40. Where("sch.status = 1 AND sch.user_org_id = ?", orgID)
  41. if scheduleDate != 0 {
  42. db = db.Where("schedule_date = ?", scheduleDate)
  43. }
  44. if scheduleType != 0 {
  45. db = db.Where("schedule_type = ?", scheduleType)
  46. }
  47. err := db.Find(&vms).Error
  48. return vms, err
  49. }
  50. func MobileGetWaitingScheduals(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  51. var vms []*MDialysisScheduleVM
  52. db := readDb.
  53. Table("xt_schedule as sch").
  54. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  55. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  56. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  57. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  58. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  59. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  60. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  61. Preload("Advices", "status = 1 AND user_org_id = ? AND advice_type = 2 ", orgID).
  62. Preload("TreatmentMode", "status = 1").
  63. Preload("TreatmentSummary", "status = 1 AND user_org_id = ?", orgID).
  64. Where("sch.status = 1 AND sch.user_org_id = ?", orgID)
  65. if scheduleDate != 0 {
  66. db = db.Where("schedule_date = ?", scheduleDate)
  67. }
  68. err := db.Find(&vms).Error
  69. return vms, err
  70. }
  71. type VMTreatmentSummary struct {
  72. ID int64 `gorm:"column:id" json:"id"`
  73. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  74. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  75. AssessmentDate int64 `gorm:"column:assessment_date" json:"assessment_date"`
  76. DialysisSummary string `gorm:"column:dialysis_summary" json:"dialysis_summary" form:"dialysis_summary"`
  77. }
  78. func (VMTreatmentSummary) TableName() string {
  79. return "xt_treatment_summary"
  80. }
  81. type AssessmentAfterDislysis struct {
  82. ID int64 `gorm:"column:id" json:"id"`
  83. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  84. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  85. AssessmentDate int64 `gorm:"column:assessment_date" json:"assessment_date"`
  86. WeightAfter float64 `gorm:"column:weight_after" json:"weight_after"`
  87. Status int64 `gorm:"column:status" json:"status"`
  88. }
  89. func (AssessmentAfterDislysis) TableName() string {
  90. return "xt_assessment_after_dislysis"
  91. }
  92. type MDialysisScheduleVM struct {
  93. ID int64 `gorm:"column:id" json:"id"`
  94. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  95. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  96. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  97. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  98. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  99. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  100. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  101. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  102. Status int64 `gorm:"column:status" json:"status"`
  103. SchedualPatient *MSchedualPatientVMList `gorm:"ForeignKey:PatientId" json:"patient"`
  104. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  105. DialysisOrder *MDialysisOrderVMList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  106. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  107. AssessmentBeforeDislysis *models.PredialysisEvaluationList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_before_dislysis"`
  108. AssessmentAfterDislysis *AssessmentAfterDislysis `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_after_dislysis"`
  109. Advices []*VMDoctorAdvice `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  110. TreatmentSummary *VMTreatmentSummary `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"treatment_summary"`
  111. }
  112. func (MDialysisScheduleVM) TableName() string {
  113. return "xt_schedule"
  114. }
  115. type MDeviceNumberVM struct {
  116. models.DeviceNumber
  117. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  118. }
  119. func (MDeviceNumberVM) TableName() string {
  120. return "xt_device_number"
  121. }
  122. type MSchedualPatientVMList struct {
  123. ID int64 `gorm:"column:id" json:"id" form:"id"`
  124. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  125. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  126. PatientType int64 `gorm:"column:patient_type" json:"patient_type" form:"patient_type"`
  127. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  128. Avatar string `gorm:"column:avatar" json:"avatar" form:"avatar"`
  129. Gender int64 `gorm:"column:gender" json:"gender" form:"gender"`
  130. Birthday int64 `gorm:"column:birthday" json:"birthday" form:"birthday"`
  131. Age int64 `gorm:"column:age" json:"age"`
  132. Name string `gorm:"column:name" json:"name" form:"name"`
  133. IdCardNo string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
  134. UserSysBeforeCount int64 `gorm:"column:user_sys_before_count" json:"user_sys_before_count" form:"user_sys_before_count"`
  135. }
  136. func (MSchedualPatientVMList) TableName() string {
  137. return "xt_patients"
  138. }
  139. type MSchedualPatientVM struct {
  140. ID int64 `gorm:"column:id" json:"id" form:"id"`
  141. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  142. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  143. PatientType int64 `gorm:"column:patient_type" json:"patient_type" form:"patient_type"`
  144. Avatar string `gorm:"column:avatar" json:"avatar" form:"avatar"`
  145. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  146. AdmissionNumber string `gorm:"column:admission_number" json:"admission_number" form:"admission_number"`
  147. Source int64 `gorm:"column:source" json:"source" form:"source"`
  148. Lapseto int64 `gorm:"column:lapseto" json:"lapseto" form:"lapseto"`
  149. PartitionId int64 `gorm:"column:partition_id" json:"partition_id" form:"partition_id"`
  150. BedId int64 `gorm:"column:bed_id" json:"bed_id" form:"bed_id"`
  151. Name string `gorm:"column:name" json:"name" form:"name"`
  152. Alias string `gorm:"column:alias" json:"alias" form:"alias"`
  153. Gender int64 `gorm:"column:gender" json:"gender" form:"gender"`
  154. Nation string `gorm:"column:nation" json:"nation" form:"nation"`
  155. NativePlace string `gorm:"column:native_place" json:"native_place" form:"native_place"`
  156. MaritalStatus int64 `gorm:"column:marital_status" json:"marital_status" form:"marital_status"`
  157. IdCardNo string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
  158. Birthday int64 `gorm:"column:birthday" json:"birthday" form:"birthday"`
  159. ReimbursementWayId int64 `gorm:"column:reimbursement_way_id" json:"reimbursement_way_id" form:"reimbursement_way_id"`
  160. HealthCareType int64 `gorm:"column:health_care_type" json:"health_care_type" form:"health_care_type"`
  161. HealthCareNo string `gorm:"column:health_care_no" json:"health_care_no" form:"health_care_no"`
  162. HealthCareDueDate int64 `gorm:"column:health_care_due_date" json:"health_care_due_date" form:"health_care_due_date"`
  163. Height int64 `gorm:"column:height" json:"height" form:"height"`
  164. BloodType int64 `gorm:"column:blood_type" json:"blood_type" form:"blood_type"`
  165. Rh int64 `gorm:"column:rh" json:"rh" form:"rh"`
  166. HealthCareDueAlertDate int64 `gorm:"column:health_care_due_alert_date" json:"health_care_due_alert_date" form:"health_care_due_alert_date"`
  167. EducationLevel int64 `gorm:"column:education_level" json:"education_level" form:"education_level"`
  168. Profession int64 `gorm:"column:profession" json:"profession" form:"profession"`
  169. Phone string `gorm:"column:phone" json:"phone" form:"phone"`
  170. HomeTelephone string `gorm:"column:home_telephone" json:"home_telephone" form:"home_telephone"`
  171. RelativePhone string `gorm:"column:relative_phone" json:"relative_phone" form:"relative_phone"`
  172. RelativeRelations string `gorm:"column:relative_relations" json:"relative_relations" form:"relative_relations"`
  173. HomeAddress string `gorm:"column:home_address" json:"home_address" form:"home_address"`
  174. WorkUnit string `gorm:"column:work_unit" json:"work_unit" form:"work_unit"`
  175. UnitAddress string `gorm:"column:unit_address" json:"unit_address" form:"unit_address"`
  176. Children int64 `gorm:"column:children" json:"children" form:"children"`
  177. ReceivingDate int64 `gorm:"column:receiving_date" json:"receiving_date" form:"receiving_date"`
  178. IsHospitalFirstDialysis int64 `gorm:"column:is_hospital_first_dialysis" json:"is_hospital_first_dialysis" form:"is_hospital_first_dialysis"`
  179. FirstDialysisDate int64 `gorm:"column:first_dialysis_date" json:"first_dialysis_date" form:"first_dialysis_date"`
  180. FirstDialysisHospital string `gorm:"column:first_dialysis_hospital" json:"first_dialysis_hospital" form:"first_dialysis_hospital"`
  181. InductionPeriod int64 `gorm:"column:induction_period" json:"induction_period" form:"induction_period"`
  182. InitialDialysis int64 `gorm:"column:initial_dialysis" json:"initial_dialysis" form:"initial_dialysis"`
  183. TotalDialysis int64 `gorm:"column:total_dialysis" json:"total_dialysis" form:"total_dialysis"`
  184. AttendingDoctorId int64 `gorm:"column:attending_doctor_id" json:"attending_doctor_id" form:"attending_doctor_id"`
  185. HeadNurseId int64 `gorm:"column:head_nurse_id" json:"head_nurse_id" form:"head_nurse_id"`
  186. Evaluate string `gorm:"column:evaluate" json:"evaluate" form:"evaluate"`
  187. Diagnose string `gorm:"column:diagnose" json:"diagnose" form:"diagnose"`
  188. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  189. RegistrarsId int64 `gorm:"column:registrars_id" json:"registrars_id" form:"registrars_id"`
  190. Registrars string `gorm:"column:registrars" json:"registrars" form:"registrars"`
  191. QrCode string `gorm:"column:qr_code" json:"qr_code" form:"qr_code"`
  192. BindingState int64 `gorm:"column:binding_state" json:"binding_state" form:"binding_state"`
  193. PatientComplains string `gorm:"column:patient_complains" json:"patient_complains"` // 主诉
  194. PresentHistory string `gorm:"column:present_history" json:"present_history"` // 现病史
  195. PastHistory string `gorm:"column:past_history" json:"past_history"` // 既往史
  196. Temperature float64 `gorm:"column:temperature" json:"temperature"` // 体格检查-体温
  197. Pulse int64 `gorm:"column:pulse" json:"pulse"` // 体格检查-脉搏
  198. Respiratory int64 `gorm:"column:respiratory" json:"respiratory"` // 体格检查-呼吸频率
  199. SBP int64 `gorm:"column:sbp" json:"sbp"` // 体格检查-收缩压
  200. DBP int64 `gorm:"column:dbp" json:"dbp"` // 体格检查-舒张压
  201. Status int64 `gorm:"column:status" json:"status" form:"status"`
  202. Age int64 `gorm:"column:age" json:"age"`
  203. IsOpenRemind int64 `gorm:"column:is_open_remind" json:"is_open_remind"`
  204. DialysisAge int64 `gorm:"column:dialysis_age" json:"dialysis_age" form:"dialysis_age"`
  205. ExpenseKind int64 `gorm:"column:expense_kind" json:"expense_kind" form:"expense_kind"`
  206. TellPhone string `gorm:"column:tell_phone" json:"tell_phone" form:"tell_phone"`
  207. FirstTreatmentDate int64 `gorm:"column:first_treatment_date" json:"first_treatment_date" form:"first_treatment_date"`
  208. ContactName string `gorm:"column:contact_name" json:"contact_name" form:"contact_name"`
  209. UserSysBeforeCount int64 `gorm:"column:user_sys_before_count" json:"user_sys_before_count" form:"user_sys_before_count"`
  210. }
  211. func (MSchedualPatientVM) TableName() string {
  212. return "xt_patients"
  213. }
  214. type MDialysisOrderVM struct {
  215. ID int64 `gorm:"column:id" json:"id"`
  216. DialysisDate int64 `gorm:"column:dialysis_date" json:"dialysis_date"`
  217. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  218. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  219. // PrescriptionId int64 `gorm:"column:prescription_id" json:"prescription_id"`
  220. Stage int64 `gorm:"column:stage" json:"stage"`
  221. // Remark string `gorm:"column:remark" json:"remark"`
  222. BedID int64 `gorm:"column:bed_id" json:"bed_id"`
  223. StartNurse int64 `gorm:"column:start_nurse" json:"start_nurse"`
  224. FinishNurse int64 `gorm:"column:finish_nurse" json:"finish_nurse"`
  225. Status int64 `gorm:"column:status" json:"status"`
  226. PunctureNurse int64 `gorm:"column:puncture_nurse" json:"puncture_nurse"`
  227. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedID" json:"device_number"`
  228. MonitoringRecords []*models.MonitoringRecord `gorm:"ForeignKey:DialysisOrderId" json:"monitoring_records"`
  229. Creator int64 `gorm:"column:creator" json:"creator"`
  230. Modifier int64 `gorm:"column:modifier" json:"modifier"`
  231. FinishCreator int64 `gorm:"column:finish_creator" json:"finish_creator"`
  232. FinishModifier int64 `gorm:"column:finish_modifier" json:"finish_modifier"`
  233. SchedualType int64 `gorm:"column:schedual_type" json:"schedual_type"`
  234. WashpipeNurse int64 `gorm:"column:washpipe_nurse" json:"washpipe_nurse" form:"washpipe_nurse"`
  235. }
  236. func (MDialysisOrderVM) TableName() string {
  237. return "xt_dialysis_order"
  238. }
  239. type MDialysisOrderVMList struct {
  240. ID int64 `gorm:"column:id" json:"id"`
  241. DialysisDate int64 `gorm:"column:dialysis_date" json:"dialysis_date"`
  242. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  243. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  244. // PrescriptionId int64 `gorm:"column:prescription_id" json:"prescription_id"`
  245. Stage int64 `gorm:"column:stage" json:"stage"`
  246. // Remark string `gorm:"column:remark" json:"remark"`
  247. BedID int64 `gorm:"column:bed_id" json:"bed_id"`
  248. StartNurse int64 `gorm:"column:start_nurse" json:"start_nurse"`
  249. Status int64 `gorm:"column:status" json:"status"`
  250. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedID" json:"device_number"`
  251. Creator int64 `gorm:"column:creator" json:"creator"`
  252. WashpipeNurse int64 `gorm:"column:washpipe_nurse" json:"washpipe_nurse" form:"washpipe_nurse"`
  253. }
  254. func (MDialysisOrderVMList) TableName() string {
  255. return "xt_dialysis_order"
  256. }
  257. type VMDoctorAdvice struct {
  258. ID int64 `gorm:"column:id" json:"id" form:"id"`
  259. GroupNo int64 `gorm:"column:groupno" json:"groupno" form:"groupno"`
  260. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  261. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  262. AdviceDate int64 `gorm:"column:advice_date" json:"advice_date" form:"advice_date"`
  263. Status int64 `gorm:"column:status" json:"status" form:"status"`
  264. ExecutionState int64 `gorm:"column:execution_state" json:"execution_state" form:"execution_state"`
  265. RecordDate int64 `gorm:"column:record_date" json:"record_date"`
  266. CheckTime int64 `gorm:"column:check_time" json:"check_time" form:"check_time"`
  267. CheckState int64 `gorm:"column:check_state" json:"check_state" form:"check_state"`
  268. }
  269. func (VMDoctorAdvice) TableName() string {
  270. return "xt_doctor_advice"
  271. }
  272. // 获取透析记录
  273. func MobileGetDialysisRecord(orgID int64, patientID int64, recordDate int64) (*models.DialysisOrder, error) {
  274. var record models.DialysisOrder
  275. err := readDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgID, patientID, recordDate).First(&record).Error
  276. if err != nil {
  277. if err == gorm.ErrRecordNotFound {
  278. return nil, nil
  279. } else {
  280. return nil, err
  281. }
  282. }
  283. return &record, nil
  284. }
  285. // 用户基本信息
  286. func MobileGetPatientDetail(orgID int64, patientID int64) (*MPatient, error) {
  287. var patient MPatient
  288. err := readDb.Model(&MPatient{}).Where("status = 1 AND user_org_id = ? AND id = ?", orgID, patientID).First(&patient).Error
  289. if err != nil {
  290. if err == gorm.ErrRecordNotFound {
  291. return nil, nil
  292. } else {
  293. return nil, err
  294. }
  295. }
  296. return &patient, nil
  297. }
  298. // 用户排班信息
  299. func MobileGetSchedualDetail(orgID int64, patientID int64, schedualDate int64) (*MDialysisScheduleVM, error) {
  300. var vm MDialysisScheduleVM
  301. err := readDb.
  302. Table("xt_schedule").
  303. // Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  304. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  305. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND patient_id = ?", orgID, schedualDate, patientID).
  306. First(&vm).Error
  307. if err != nil {
  308. if err == gorm.ErrRecordNotFound {
  309. return nil, nil
  310. } else {
  311. return nil, err
  312. }
  313. }
  314. return &vm, err
  315. }
  316. // 用户排班信息
  317. func MobileGetPatientSchedual(orgID int64, patientID int64, schedualDate int64) (*models.Schedule, error) {
  318. var schedule models.Schedule
  319. err := readDb.
  320. Table("xt_schedule").
  321. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND patient_id = ?", orgID, schedualDate, patientID).
  322. First(&schedule).Error
  323. if err != nil {
  324. if err == gorm.ErrRecordNotFound {
  325. return nil, nil
  326. } else {
  327. return nil, err
  328. }
  329. }
  330. return &schedule, nil
  331. }
  332. type MPatient struct {
  333. ID int64 `gorm:"column:id" json:"id" form:"id"`
  334. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  335. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  336. PatientType int64 `gorm:"column:patient_type" json:"patient_type" form:"patient_type"`
  337. Avatar string `gorm:"column:avatar" json:"avatar" form:"avatar"`
  338. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  339. AdmissionNumber string `gorm:"column:admission_number" json:"admission_number" form:"admission_number"`
  340. Source int64 `gorm:"column:source" json:"source" form:"source"`
  341. Lapseto int64 `gorm:"column:lapseto" json:"lapseto" form:"lapseto"`
  342. PartitionId int64 `gorm:"column:partition_id" json:"partition_id" form:"partition_id"`
  343. BedId int64 `gorm:"column:bed_id" json:"bed_id" form:"bed_id"`
  344. Name string `gorm:"column:name" json:"name" form:"name"`
  345. Alias string `gorm:"column:alias" json:"alias" form:"alias"`
  346. Gender int64 `gorm:"column:gender" json:"gender" form:"gender"`
  347. Nation string `gorm:"column:nation" json:"nation" form:"nation"`
  348. NativePlace string `gorm:"column:native_place" json:"native_place" form:"native_place"`
  349. MaritalStatus int64 `gorm:"column:marital_status" json:"marital_status" form:"marital_status"`
  350. IdCardNo string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
  351. Birthday int64 `gorm:"column:birthday" json:"birthday" form:"birthday"`
  352. ReimbursementWayId int64 `gorm:"column:reimbursement_way_id" json:"reimbursement_way_id" form:"reimbursement_way_id"`
  353. HealthCareType int64 `gorm:"column:health_care_type" json:"health_care_type" form:"health_care_type"`
  354. HealthCareNo string `gorm:"column:health_care_no" json:"health_care_no" form:"health_care_no"`
  355. HealthCareDueDate int64 `gorm:"column:health_care_due_date" json:"health_care_due_date" form:"health_care_due_date"`
  356. Height int64 `gorm:"column:height" json:"height" form:"height"`
  357. BloodType int64 `gorm:"column:blood_type" json:"blood_type" form:"blood_type"`
  358. Rh int64 `gorm:"column:rh" json:"rh" form:"rh"`
  359. HealthCareDueAlertDate int64 `gorm:"column:health_care_due_alert_date" json:"health_care_due_alert_date" form:"health_care_due_alert_date"`
  360. EducationLevel int64 `gorm:"column:education_level" json:"education_level" form:"education_level"`
  361. Profession int64 `gorm:"column:profession" json:"profession" form:"profession"`
  362. Phone string `gorm:"column:phone" json:"phone" form:"phone"`
  363. HomeTelephone string `gorm:"column:home_telephone" json:"home_telephone" form:"home_telephone"`
  364. RelativePhone string `gorm:"column:relative_phone" json:"relative_phone" form:"relative_phone"`
  365. RelativeRelations string `gorm:"column:relative_relations" json:"relative_relations" form:"relative_relations"`
  366. HomeAddress string `gorm:"column:home_address" json:"home_address" form:"home_address"`
  367. WorkUnit string `gorm:"column:work_unit" json:"work_unit" form:"work_unit"`
  368. UnitAddress string `gorm:"column:unit_address" json:"unit_address" form:"unit_address"`
  369. Children int64 `gorm:"column:children" json:"children" form:"children"`
  370. ReceivingDate int64 `gorm:"column:receiving_date" json:"receiving_date" form:"receiving_date"`
  371. IsHospitalFirstDialysis int64 `gorm:"column:is_hospital_first_dialysis" json:"is_hospital_first_dialysis" form:"is_hospital_first_dialysis"`
  372. FirstDialysisDate int64 `gorm:"column:first_dialysis_date" json:"first_dialysis_date" form:"first_dialysis_date"`
  373. FirstDialysisHospital string `gorm:"column:first_dialysis_hospital" json:"first_dialysis_hospital" form:"first_dialysis_hospital"`
  374. InductionPeriod int64 `gorm:"column:induction_period" json:"induction_period" form:"induction_period"`
  375. InitialDialysis int64 `gorm:"column:initial_dialysis" json:"initial_dialysis" form:"initial_dialysis"`
  376. TotalDialysis int64 `gorm:"column:total_dialysis" json:"total_dialysis" form:"total_dialysis"`
  377. AttendingDoctorId int64 `gorm:"column:attending_doctor_id" json:"attending_doctor_id" form:"attending_doctor_id"`
  378. HeadNurseId int64 `gorm:"column:head_nurse_id" json:"head_nurse_id" form:"head_nurse_id"`
  379. Evaluate string `gorm:"column:evaluate" json:"evaluate" form:"evaluate"`
  380. Diagnose string `gorm:"column:diagnose" json:"diagnose" form:"diagnose"`
  381. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  382. RegistrarsId int64 `gorm:"column:registrars_id" json:"registrars_id" form:"registrars_id"`
  383. Registrars string `gorm:"column:registrars" json:"registrars" form:"registrars"`
  384. QrCode string `gorm:"column:qr_code" json:"qr_code" form:"qr_code"`
  385. BindingState int64 `gorm:"column:binding_state" json:"binding_state" form:"binding_state"`
  386. PatientComplains string `gorm:"column:patient_complains" json:"patient_complains"` // 主诉
  387. PresentHistory string `gorm:"column:present_history" json:"present_history"` // 现病史
  388. PastHistory string `gorm:"column:past_history" json:"past_history"` // 既往史
  389. Temperature float64 `gorm:"column:temperature" json:"temperature"` // 体格检查-体温
  390. Pulse int64 `gorm:"column:pulse" json:"pulse"` // 体格检查-脉搏
  391. Respiratory int64 `gorm:"column:respiratory" json:"respiratory"` // 体格检查-呼吸频率
  392. SBP int64 `gorm:"column:sbp" json:"sbp"` // 体格检查-收缩压
  393. DBP int64 `gorm:"column:dbp" json:"dbp"` // 体格检查-舒张压
  394. Status int64 `gorm:"column:status" json:"status" form:"status"`
  395. Age int64 `gorm:"column:age" json:"age"`
  396. IsOpenRemind int64 `gorm:"column:is_open_remind" json:"is_open_remind"`
  397. CreatedTime int64 `gorm:"column:created_time" json:"created_time" form:"created_time"`
  398. DialysisAge int64 `gorm:"column:dialysis_age" json:"dialysis_age" form:"dialysis_age"`
  399. ExpenseKind int64 `gorm:"column:expense_kind" json:"expense_kind" form:"expense_kind"`
  400. TellPhone string `gorm:"column:tell_phone" json:"tell_phone" form:"tell_phone"`
  401. FirstTreatmentDate int64 `gorm:"column:first_treatment_date" json:"first_treatment_date" form:"first_treatment_date"`
  402. ContactName string `gorm:"column:contact_name" json:"contact_name" form:"contact_name"`
  403. UserSysBeforeCount int64 `gorm:"column:user_sys_before_count" json:"user_sys_before_count" form:"user_sys_before_count"`
  404. }
  405. func (MPatient) TableName() string {
  406. return "xt_patients"
  407. }
  408. // 接诊评估
  409. func MobileGetReceiverTreatmentAccessRecord(orgID int64, patientID int64, recordDate int64) (*models.ReceiveTreatmentAsses, error) {
  410. var record models.ReceiveTreatmentAsses
  411. err = readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  412. if err != nil {
  413. if err == gorm.ErrRecordNotFound {
  414. return nil, nil
  415. } else {
  416. return nil, err
  417. }
  418. }
  419. return &record, nil
  420. }
  421. // 透前评估
  422. func MobileGetPredialysisEvaluation(orgID int64, patientID int64, recordDate int64) (*models.PredialysisEvaluation, error) {
  423. fmt.Println("recordDate", recordDate)
  424. var record models.PredialysisEvaluation
  425. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  426. if err != nil {
  427. if err == gorm.ErrRecordNotFound {
  428. return nil, nil
  429. } else {
  430. return nil, err
  431. }
  432. }
  433. return &record, nil
  434. }
  435. // 获取 maxDate 之前一次的透前评估记录
  436. func MobileGetLastTimePredialysisEvaluation(orgID int64, patientID int64, maxDate int64) (*models.PredialysisEvaluation, error) {
  437. var record models.PredialysisEvaluation
  438. 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
  439. if err != nil {
  440. if err == gorm.ErrRecordNotFound {
  441. return nil, nil
  442. } else {
  443. return nil, err
  444. }
  445. }
  446. return &record, nil
  447. }
  448. // 临时医嘱
  449. func MobileGetDoctorAdvices(orgID int64, patientID int64, recordDate int64) ([]*models.DoctorAdvice, error) {
  450. var records []*models.DoctorAdvice
  451. // err := readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).Find(&records).Error
  452. err := readDb.
  453. Model(&models.DoctorAdvice{}).
  454. Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ? and (advice_type = 2 or advice_type = 3)", patientID, orgID, recordDate).
  455. 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,way,drug_id,drug_name_id,IF(parent_id>0, parent_id, id) as advice_order,groupno").
  456. Order("start_time asc, groupno desc, advice_order desc, id").
  457. Scan(&records).Error
  458. if err != nil {
  459. return nil, err
  460. }
  461. return records, nil
  462. }
  463. func MobileGetDoctorAdvicesByGroups(orgID int64, patientID int64, recordDate int64) ([]*models.DoctorAdvice, error) {
  464. var records []*models.DoctorAdvice
  465. // err := readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).Find(&records).Error
  466. err := readDb.
  467. Model(&models.DoctorAdvice{}).
  468. Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ? and advice_type = 2", patientID, orgID, recordDate).
  469. 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,way,drug_id,drug_name_id, IF(parent_id > 0, parent_id, id) as advice_order").
  470. Order("start_time asc, groupno desc, advice_order desc, id asc").
  471. Scan(&records).Error
  472. if err != nil {
  473. return nil, err
  474. }
  475. return records, nil
  476. }
  477. // 透析记录
  478. func MobileGetSchedualDialysisRecord(orgID int64, patientID int64, recordDate int64) (*models.DialysisOrder, error) {
  479. var record models.DialysisOrder
  480. 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
  481. if err != nil {
  482. if err == gorm.ErrRecordNotFound {
  483. return nil, nil
  484. } else {
  485. return nil, err
  486. }
  487. }
  488. return &record, nil
  489. }
  490. // 双人核对
  491. func MobileGetDoubleCheck(orgID int64, patientID int64, recordDate int64) (*models.DoubleCheck, error) {
  492. var record models.DoubleCheck
  493. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? and user_org_id = ? and status = 1 and check_date = ?", patientID, orgID, recordDate).First(&record).Error
  494. if err != nil {
  495. if err == gorm.ErrRecordNotFound {
  496. return nil, nil
  497. } else {
  498. return nil, err
  499. }
  500. }
  501. return &record, nil
  502. }
  503. // 透析监测记录
  504. func MobileGetMonitorRecords(orgID int64, patientID int64, recordDate int64) ([]*models.MonitoringRecord, error) {
  505. var records []*models.MonitoringRecord
  506. 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
  507. if err != nil {
  508. return nil, err
  509. }
  510. return records, nil
  511. }
  512. func MobileGetMonitorRecordFirst(orgID int64, patientID int64, recordDate int64) (*models.MonitoringRecord, error) {
  513. var records models.MonitoringRecord
  514. 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
  515. if err != nil {
  516. return nil, err
  517. }
  518. return &records, nil
  519. }
  520. func MobileGetLastMonitorRecord(orgID int64, patientID int64, beforeDate int64) (*models.MonitoringRecord, error) {
  521. var record models.MonitoringRecord
  522. 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
  523. if err != nil {
  524. if err == gorm.ErrRecordNotFound {
  525. return nil, nil
  526. } else {
  527. return nil, err
  528. }
  529. }
  530. return &record, nil
  531. }
  532. // 透后评估
  533. func MobileGetAssessmentAfterDislysis(orgID int64, patientID int64, recordDate int64) (*models.AssessmentAfterDislysis, error) {
  534. var record models.AssessmentAfterDislysis
  535. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  536. if err != nil {
  537. if err == gorm.ErrRecordNotFound {
  538. return nil, nil
  539. } else {
  540. return nil, err
  541. }
  542. }
  543. return &record, nil
  544. }
  545. // 获取 maxDate 之前一次的透后评估记录
  546. func MobileGetLastTimeAssessmentAfterDislysis(orgID int64, patientID int64, maxDate int64) (*models.AssessmentAfterDislysis, error) {
  547. var record models.AssessmentAfterDislysis
  548. 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
  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. func MobileGetLast(orgID int64, patientID int64, maxDate int64) (models.AssessmentAfterDislysis, error) {
  559. dislysis := models.AssessmentAfterDislysis{}
  560. 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
  561. return dislysis, err
  562. }
  563. // 治疗小结
  564. func MobileGetTreatmentSummary(orgID int64, patientID int64, recordDate int64) (*models.TreatmentSummary, error) {
  565. var record models.TreatmentSummary
  566. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  567. if err != nil {
  568. if err == gorm.ErrRecordNotFound {
  569. return nil, nil
  570. } else {
  571. return nil, err
  572. }
  573. }
  574. return &record, nil
  575. }
  576. // 透析处方
  577. func MobileGetDialysisPrescribe(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  578. var record models.DialysisPrescription
  579. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  580. if err != nil {
  581. if err == gorm.ErrRecordNotFound {
  582. return nil, nil
  583. } else {
  584. return nil, err
  585. }
  586. }
  587. return &record, nil
  588. }
  589. func MobileGetDialysisSchedual(orgID int64, patientID int64, scheduleDate int64) (*models.XtSchedule, error) {
  590. var schedule models.XtSchedule
  591. err := readDb.Model(&models.XtSchedule{}).Where("patient_id = ? and user_org_id = ? and status = 1 and schedule_date = ?", patientID, orgID, scheduleDate).First(&schedule).Error
  592. if err != nil {
  593. if err == gorm.ErrRecordNotFound {
  594. return nil, nil
  595. } else {
  596. return nil, err
  597. }
  598. }
  599. return &schedule, nil
  600. }
  601. // 透析方案
  602. func MobileGetDialysisSolution(orgID int64, patientID int64) (*models.DialysisSolution, error) {
  603. var record models.DialysisSolution
  604. err := readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  605. if err != nil {
  606. if err == gorm.ErrRecordNotFound {
  607. return nil, nil
  608. } else {
  609. return nil, err
  610. }
  611. }
  612. return &record, nil
  613. }
  614. func MobileGetPatientById(orgID int64, patientID int64) (*models.Patients, error) {
  615. var patient models.Patients
  616. err := readDb.Model(&models.Patients{}).Where("id = ? and user_org_id = ? and status = 1", patientID, orgID).First(&patient).Error
  617. if err != nil {
  618. if err == gorm.ErrRecordNotFound {
  619. return nil, nil
  620. } else {
  621. return nil, err
  622. }
  623. }
  624. return &patient, nil
  625. }
  626. func DisableMonitor(orgID int64, patientID int64, recordID int64, admin_user_id int64) error {
  627. fmt.Println()
  628. tx := writeDb.Begin()
  629. updateTime := time.Now().Unix()
  630. 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
  631. if err != nil {
  632. tx.Rollback()
  633. return err
  634. }
  635. tx.Commit()
  636. return nil
  637. }
  638. func GetMonitor(orgID int64, patientID int64, id int64) (*models.MonitoringRecord, error) {
  639. var monitor models.MonitoringRecord
  640. var err error
  641. err = readDb.Model(&models.MonitoringRecord{}).Where("id = ? AND user_org_id = ? AND patient_id = ? AND status = 1 ", id, orgID, patientID).Find(&monitor).Error
  642. if err == gorm.ErrRecordNotFound {
  643. return nil, nil
  644. }
  645. if err != nil {
  646. return nil, err
  647. }
  648. return &monitor, nil
  649. }
  650. type MScheduleDoctorAdviceVM struct {
  651. ID int64 `gorm:"column:id" json:"id"`
  652. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  653. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  654. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  655. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  656. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  657. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  658. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  659. Status int64 `gorm:"column:status" json:"status"`
  660. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:DialysisDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"dialysis_order"`
  661. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  662. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  663. DoctorAdvices []*MDoctorAdviceVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  664. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  665. DialysisAssesmentBefor *models.DialysisAssesmentBefor `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assesmentbefor"`
  666. }
  667. func (MScheduleDoctorAdviceVM) TableName() string {
  668. return "xt_schedule"
  669. }
  670. type MDoctorAdviceVM struct {
  671. ID int64 `gorm:"column:id" json:"id"`
  672. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  673. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  674. AdviceType int64 `gorm:"column:advice_type" json:"advice_type"`
  675. AdviceDate int64 `gorm:"column:advice_date" json:"advice_date"`
  676. StartTime int64 `gorm:"column:start_time" json:"start_time"`
  677. AdviceName string `gorm:"column:advice_name" json:"advice_name"`
  678. AdviceDesc string `gorm:"column:advice_desc" json:"advice_desc"`
  679. ReminderDate int64 `gorm:"column:reminder_date" json:"reminder_date"`
  680. SingleDose float64 `gorm:"column:single_dose" json:"single_dose"`
  681. SingleDoseUnit string `gorm:"column:single_dose_unit" json:"single_dose_unit"`
  682. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number"`
  683. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit"`
  684. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way"`
  685. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency"`
  686. AdviceDoctor int64 `gorm:"column:advice_doctor" json:"advice_doctor"`
  687. Status int64 `gorm:"column:status" json:"status"`
  688. CreatedTime int64 `gorm:"column:created_time" json:"created_time"`
  689. UpdatedTime int64 `gorm:"column:updated_time" json:"updated_time"`
  690. AdviceAffirm string `gorm:"column:advice_affirm" json:"advice_affirm"`
  691. Remark string `gorm:"column:remark" json:"remark"`
  692. StopTime int64 `gorm:"column:stop_time" json:"stop_time"`
  693. StopReason string `gorm:"column:stop_reason" json:"stop_reason"`
  694. StopDoctor int64 `gorm:"column:stop_doctor" json:"stop_doctor"`
  695. StopState int64 `gorm:"column:stop_state" json:"stop_state"`
  696. ParentId int64 `gorm:"column:parent_id" json:"parent_id"`
  697. ExecutionTime int64 `gorm:"column:execution_time" json:"execution_time"`
  698. ExecutionStaff int64 `gorm:"column:execution_staff" json:"execution_staff"`
  699. ExecutionState int64 `gorm:"column:execution_state" json:"execution_state"`
  700. Checker int64 `gorm:"column:checker" json:"checker"`
  701. RecordDate int64 `gorm:"column:record_date" json:"record_date"`
  702. DialysisOrderId int64 `gorm:"column:dialysis_order_id" json:"dialysis_order_id"`
  703. CheckTime int64 `gorm:"column:check_time" json:"check_time"`
  704. CheckState int64 `gorm:"column:check_state" json:"check_state"`
  705. DrugSpec float64 `gorm:"column:drug_spec" json:"drug_spec"`
  706. DrugSpecUnit string `gorm:"column:drug_spec_unit" json:"drug_spec_unit"`
  707. Groupno int64 `gorm:"column:groupno" json:"groupno"`
  708. RemindType int64 `gorm:"column:remind_type" json:"remind_type"`
  709. FrequencyType int64 `gorm:"column:frequency_type" json:"frequency_type"`
  710. DayCount int64 `gorm:"column:day_count" json:"day_count"`
  711. WeekDay string `gorm:"column:week_day" json:"week_day"`
  712. TemplateId string `gorm:"column:template_id" json:"template_id"`
  713. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  714. }
  715. func (MDoctorAdviceVM) TableName() string {
  716. return "xt_doctor_advice"
  717. }
  718. func MobileGetScheduleDoctorAdvices(orgID int64, scheduleDate int64, adviceType int, patientType int, adminUserId int64, deliverWay string) ([]*MScheduleDoctorAdviceVM, error) {
  719. var vms []*MScheduleDoctorAdviceVM
  720. adviceWhere := ""
  721. fmt.Println("advicetype", adviceType)
  722. fmt.Println("patientType", patientType)
  723. fmt.Println("adminUserId", adminUserId)
  724. adviceCondition := []interface{}{}
  725. if adviceType == 0 {
  726. if patientType == 0 {
  727. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1)"
  728. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  729. } else if patientType == 1 {
  730. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1)"
  731. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  732. } else if patientType == 2 {
  733. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND execution_staff = 0 AND(advice_type = 3 OR advice_type = 1)"
  734. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  735. }
  736. } else if adviceType == 1 {
  737. if patientType == 0 {
  738. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? "
  739. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  740. } else if patientType == 1 {
  741. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? "
  742. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  743. } else if patientType == 2 {
  744. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0"
  745. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  746. }
  747. } else if adviceType == 3 {
  748. if patientType == 0 {
  749. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? "
  750. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  751. } else if patientType == 1 {
  752. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? "
  753. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  754. } else if patientType == 2 {
  755. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0"
  756. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  757. }
  758. } else if adviceType == 2 && len(deliverWay) > 0 {
  759. if patientType == 0 {
  760. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ?"
  761. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  762. } else if patientType == 1 {
  763. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and delivery_way = ? "
  764. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay)
  765. } else if patientType == 2 {
  766. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and delivery_way = ?"
  767. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  768. }
  769. } else if adviceType == 2 && len(deliverWay) <= 0 {
  770. if patientType == 0 {
  771. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ?"
  772. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  773. } else if patientType == 1 {
  774. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ?"
  775. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  776. } else if patientType == 2 {
  777. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0"
  778. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  779. }
  780. }
  781. db := readDb.
  782. Table("xt_schedule").
  783. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  784. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  785. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  786. }).
  787. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  788. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  789. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  790. Preload("DialysisAssesmentBefor", "status =1 AND user_org_id = ? and assessment_date =?", orgID, scheduleDate).
  791. Preload("DoctorAdvices", adviceCondition...).
  792. Where("status = 1 AND user_org_id = ?", orgID)
  793. if scheduleDate != 0 {
  794. db = db.Where("schedule_date = ?", scheduleDate)
  795. }
  796. err := db.Find(&vms).Error
  797. return vms, err
  798. }
  799. func GetHisDoctorAdvices(orgID int64, scheduleDate int64, deliverWay string) ([]*HisMScheduleDoctorAdviceVM, error) {
  800. var vms []*HisMScheduleDoctorAdviceVM
  801. if len(deliverWay) > 0 {
  802. db := readDb.
  803. Table("xt_schedule").
  804. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  805. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  806. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  807. }).
  808. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  809. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  810. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  811. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ?", orgID, scheduleDate, deliverWay).
  812. Where("status = 1 AND user_org_id = ?", orgID)
  813. if scheduleDate != 0 {
  814. db = db.Where("schedule_date = ?", scheduleDate)
  815. }
  816. err = db.Find(&vms).Error
  817. } else {
  818. db := readDb.
  819. Table("xt_schedule").
  820. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  821. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  822. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  823. }).
  824. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  825. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  826. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  827. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ?", orgID, scheduleDate).
  828. Where("status = 1 AND user_org_id = ?", orgID)
  829. if scheduleDate != 0 {
  830. db = db.Where("schedule_date = ?", scheduleDate)
  831. }
  832. err = db.Find(&vms).Error
  833. }
  834. return vms, err
  835. }
  836. func MobileCreateDialysisOrder(orgID int64, patientID int64, order *models.DialysisOrder) error {
  837. now := time.Now()
  838. tx := writeDb.Begin()
  839. if createOrderErr := tx.Model(&models.DialysisOrder{}).Create(order).Error; createOrderErr != nil {
  840. tx.Rollback()
  841. return createOrderErr
  842. }
  843. // 更新透析记录 ID
  844. // 透析处方
  845. 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 {
  846. tx.Rollback()
  847. return err
  848. }
  849. // 接诊评估
  850. 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 {
  851. tx.Rollback()
  852. return err
  853. }
  854. // 透前评估
  855. 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 {
  856. tx.Rollback()
  857. return err
  858. }
  859. // 临时医嘱
  860. 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 {
  861. tx.Rollback()
  862. return err
  863. }
  864. // 双人核对
  865. 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 {
  866. tx.Rollback()
  867. return err
  868. }
  869. // 透后评估
  870. 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 {
  871. tx.Rollback()
  872. return err
  873. }
  874. // 治疗小结
  875. 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 {
  876. tx.Rollback()
  877. return err
  878. }
  879. // 透析监测
  880. 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 {
  881. tx.Rollback()
  882. return err
  883. }
  884. tx.Commit()
  885. return nil
  886. }
  887. type MobileUrgentSchedulePatientVM struct {
  888. ID int64 `gorm:"column:id" json:"id"`
  889. Name string `gorm:"column:name" json:"name"`
  890. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no"`
  891. }
  892. func (MobileUrgentSchedulePatientVM) TableName() string {
  893. return "xt_patients"
  894. }
  895. type MobileUrgentScheduleTreatmentModeVM struct {
  896. ID int64 `gorm:"column:id" json:"id"`
  897. Name string `gorm:"column:name" json:"name"`
  898. }
  899. func (MobileUrgentScheduleTreatmentModeVM) TableName() string {
  900. return "xt_treatment_mode"
  901. }
  902. func MobileGetAllPatientsForUrgentSchedule(orgID int64, record_date int64) ([]*MobileUrgentSchedulePatientVM, error) {
  903. var vms []*MobileUrgentSchedulePatientVM = make([]*MobileUrgentSchedulePatientVM, 0)
  904. 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()
  905. defer rows.Close()
  906. if err != nil {
  907. return nil, err
  908. }
  909. for rows.Next() {
  910. var vm MobileUrgentSchedulePatientVM
  911. readDb.ScanRows(rows, &vm)
  912. vms = append(vms, &vm)
  913. }
  914. return vms, nil
  915. }
  916. func MobileGetAllTrearmentModesForUrgentSchedule() ([]*MobileUrgentScheduleTreatmentModeVM, error) {
  917. var modes []*MobileUrgentScheduleTreatmentModeVM
  918. err := readDb.Model(&MobileUrgentScheduleTreatmentModeVM{}).Where("status = 1").Find(&modes).Error
  919. if err != nil {
  920. return nil, err
  921. }
  922. return modes, nil
  923. }
  924. type MobileUrgentScheduleScheduleListVM struct {
  925. ID int64 `gorm:"column:id" json:"id"`
  926. // ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  927. DeviceNumberID int64 `gorm:"column:bed_id" json:"bed_id"`
  928. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  929. ScheduleType int `gorm:"column:schedule_type" json:"schedule_type"`
  930. DeviceNumber *models.DeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  931. // DeviceZone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"device_zone"`
  932. }
  933. func (MobileUrgentScheduleScheduleListVM) TableName() string {
  934. return "xt_schedule"
  935. }
  936. func MobileGetSchedulesForUrgentSchedule(orgID int64, scheduleDate int64, schedule_type int) (schedules []*MobileUrgentScheduleScheduleListVM, err error) {
  937. db := readDb.
  938. Model(&MobileUrgentScheduleScheduleListVM{}).
  939. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  940. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND schedule_type = ? ", orgID, scheduleDate, schedule_type)
  941. err = db.Find(&schedules).Error
  942. if err != nil {
  943. return nil, err
  944. }
  945. return schedules, nil
  946. }
  947. func MobileGetOtherSchedulesForUrgentSchedule(orgID int64, scheduleDate int64, schedule_type int) ([]*MobileUrgentScheduleScheduleListVM, error) {
  948. var vms []*MobileUrgentScheduleScheduleListVM = make([]*MobileUrgentScheduleScheduleListVM, 0)
  949. 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()
  950. defer rows.Close()
  951. if err != nil {
  952. return nil, err
  953. }
  954. for rows.Next() {
  955. var vm MobileUrgentScheduleScheduleListVM
  956. readDb.ScanRows(rows, &vm)
  957. vms = append(vms, &vm)
  958. }
  959. return vms, nil
  960. }
  961. type MobileUrgentScheduleDeviceNumberVM struct {
  962. ID int64 `gorm:"column:id" json:"id"`
  963. Number string `gorm:"column:number" json:"number"`
  964. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  965. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  966. }
  967. func (MobileUrgentScheduleDeviceNumberVM) TableName() string {
  968. return "xt_device_number"
  969. }
  970. func MobileGetAllDeviceNumbersForUrgentSchedule(orgID int64, record_date int64, schedule_type int) ([]*DeviceNumberViewModel, error) {
  971. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  972. 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()
  973. defer rows.Close()
  974. if err != nil {
  975. return nil, err
  976. }
  977. for rows.Next() {
  978. var vm DeviceNumberViewModel
  979. readDb.ScanRows(rows, &vm)
  980. vms = append(vms, &vm)
  981. }
  982. return vms, nil
  983. //var deviceNumbers []*MobileUrgentScheduleDeviceNumberVM
  984. //db := readDb.
  985. // Model(&MobileUrgentScheduleDeviceNumberVM{}).
  986. // Preload("Zone", "status = 1 AND org_id = ?", orgID).
  987. // Where("status = 1 AND org_id = ?", orgID)
  988. //err := db.Order("zone_id asc").Find(&deviceNumbers).Error
  989. //if err != nil {
  990. // return nil, err
  991. //}
  992. //return deviceNumbers, nil
  993. }
  994. func GetValidScheduleMonitorRecordCount() (int64, error) {
  995. var total int64
  996. err := readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND operate_time = 0 AND monitoring_date <> 0").Count(&total).Error
  997. return total, err
  998. }
  999. func GetTop1000ValidScheduleMonitorRecord() ([]*models.MonitoringRecord, error) {
  1000. var monitors []*models.MonitoringRecord
  1001. err := readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND operate_time = 0 AND monitoring_date <> 0").Order("id asc").Limit(100).Find(&monitors).Error
  1002. if err != nil {
  1003. return nil, err
  1004. }
  1005. return monitors, nil
  1006. }
  1007. func BatchUpdateMonitors(monitors []*models.MonitoringRecord) error {
  1008. tx := writeDb.Begin()
  1009. for index := 0; index < len(monitors); index++ {
  1010. tx.Save(monitors[index])
  1011. }
  1012. return tx.Commit().Error
  1013. }
  1014. func ModifyStartDialysisOrder(order *models.DialysisOrder) error {
  1015. tx := writeDb.Begin()
  1016. updateTime := time.Now().Unix()
  1017. 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, "washpipe_nurse": order.WashpipeNurse, "change_nurse": order.ChangeNurse}).Error
  1018. if err != nil {
  1019. tx.Rollback()
  1020. return err
  1021. }
  1022. tx.Commit()
  1023. return err
  1024. }
  1025. func ModifyFinishDialysisOrder(order *models.DialysisOrder) error {
  1026. tx := writeDb.Begin()
  1027. updateTime := time.Now().Unix()
  1028. 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
  1029. if err != nil {
  1030. tx.Rollback()
  1031. return err
  1032. }
  1033. tx.Commit()
  1034. return err
  1035. }
  1036. func MobileGetLastDryWeight(orgID int64, patientID int64) (*models.SgjPatientDryweight, error) {
  1037. var record models.SgjPatientDryweight
  1038. err := readDb.Model(&models.SgjPatientDryweight{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  1039. if err != nil {
  1040. if err == gorm.ErrRecordNotFound {
  1041. return nil, nil
  1042. } else {
  1043. return nil, err
  1044. }
  1045. }
  1046. return &record, nil
  1047. }
  1048. // 透析方案
  1049. func MobileGetDialysisSolutionByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  1050. var record models.DialysisSolution
  1051. 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
  1052. if err != nil {
  1053. if err == gorm.ErrRecordNotFound {
  1054. return nil, nil
  1055. } else {
  1056. return nil, err
  1057. }
  1058. }
  1059. return &record, nil
  1060. }
  1061. // 透析处方
  1062. func MobileGetDialysisPrescribeByModeId(orgID int64, patientID int64, recordDate int64, mode_id int64) (*models.DialysisPrescription, error) {
  1063. var record models.DialysisPrescription
  1064. 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
  1065. if err != nil {
  1066. if err == gorm.ErrRecordNotFound {
  1067. return nil, nil
  1068. } else {
  1069. return nil, err
  1070. }
  1071. }
  1072. return &record, nil
  1073. }
  1074. func MobileGetLastDialysisPrescribe(orgID int64, patientID int64) (*models.DialysisPrescription, error) {
  1075. var record models.DialysisPrescription
  1076. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 ", patientID, orgID).Last(&record).Error
  1077. if err != nil {
  1078. if err == gorm.ErrRecordNotFound {
  1079. return nil, nil
  1080. } else {
  1081. return nil, err
  1082. }
  1083. }
  1084. return &record, nil
  1085. }
  1086. func MobileGetLastDialysisPrescribeByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  1087. var record models.DialysisPrescription
  1088. 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
  1089. if err != nil {
  1090. if err == gorm.ErrRecordNotFound {
  1091. return nil, nil
  1092. } else {
  1093. return nil, err
  1094. }
  1095. }
  1096. return &record, nil
  1097. }
  1098. func GetAllAvaildDeviceNumbers(orgID int64, record_date int64, schedule_type int) ([]*DeviceNumberViewModel, error) {
  1099. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  1100. 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()
  1101. defer rows.Close()
  1102. if err != nil {
  1103. return nil, err
  1104. }
  1105. for rows.Next() {
  1106. var vm DeviceNumberViewModel
  1107. readDb.ScanRows(rows, &vm)
  1108. vms = append(vms, &vm)
  1109. }
  1110. return vms, nil
  1111. }
  1112. // 获取 maxDate 之前一次的透前评估记录
  1113. func GetLastTimePredialysisEvaluation(orgID int64, patientID int64, maxDate int64) (*models.PredialysisEvaluation, error) {
  1114. var record models.PredialysisEvaluation
  1115. 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
  1116. if err != nil {
  1117. if err == gorm.ErrRecordNotFound {
  1118. return nil, nil
  1119. } else {
  1120. return nil, err
  1121. }
  1122. }
  1123. return &record, nil
  1124. }
  1125. func GetLastTimeOrder(orgID int64, patientID int64, maxDate int64) (*models.DialysisOrder, error) {
  1126. var record models.DialysisOrder
  1127. err := readDb.Model(&models.DialysisOrder{}).Where("patient_id = ? and user_org_id = ? and status = 1 and dialysis_date < ?", patientID, orgID, maxDate).Order("dialysis_date desc").First(&record).Error
  1128. if err != nil {
  1129. if err == gorm.ErrRecordNotFound {
  1130. return nil, nil
  1131. } else {
  1132. return nil, err
  1133. }
  1134. }
  1135. return &record, nil
  1136. }
  1137. func GetLastMonitorRecord(orgID int64, patientID int64, beforeDate int64) (*models.MonitoringRecord, error) {
  1138. var record models.MonitoringRecord
  1139. 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
  1140. if err != nil {
  1141. if err == gorm.ErrRecordNotFound {
  1142. return nil, nil
  1143. } else {
  1144. return nil, err
  1145. }
  1146. }
  1147. return &record, nil
  1148. }
  1149. // 获取 maxDate 之前一次的透后评估记录
  1150. func GetLastTimeAssessmentAfterDislysis(orgID int64, patientID int64, maxDate int64) (*models.AssessmentAfterDislysis, error) {
  1151. var record models.AssessmentAfterDislysis
  1152. 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
  1153. if err != nil {
  1154. if err == gorm.ErrRecordNotFound {
  1155. return nil, nil
  1156. } else {
  1157. return nil, err
  1158. }
  1159. }
  1160. return &record, nil
  1161. }
  1162. // 透析处方
  1163. func GetDialysisPrescribe(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  1164. var record models.DialysisPrescription
  1165. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  1166. if err != nil {
  1167. if err == gorm.ErrRecordNotFound {
  1168. return nil, nil
  1169. } else {
  1170. return nil, err
  1171. }
  1172. }
  1173. return &record, nil
  1174. }
  1175. // 透析方案
  1176. func GetDialysisSolution(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  1177. var record models.DialysisSolution
  1178. 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
  1179. if err != nil {
  1180. if err == gorm.ErrRecordNotFound {
  1181. return nil, nil
  1182. } else {
  1183. return nil, err
  1184. }
  1185. }
  1186. return &record, nil
  1187. }
  1188. func GetLastDialysisPrescribeByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  1189. var record models.DialysisPrescription
  1190. 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
  1191. if err != nil {
  1192. if err == gorm.ErrRecordNotFound {
  1193. return nil, nil
  1194. } else {
  1195. return nil, err
  1196. }
  1197. }
  1198. return &record, nil
  1199. }
  1200. func GetLastDryWeight(orgID int64, patientID int64) (*models.SgjPatientDryweight, error) {
  1201. var record models.SgjPatientDryweight
  1202. err := readDb.Model(&models.SgjPatientDryweight{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  1203. if err != nil {
  1204. if err == gorm.ErrRecordNotFound {
  1205. return nil, nil
  1206. } else {
  1207. return nil, err
  1208. }
  1209. }
  1210. return &record, nil
  1211. }
  1212. func MobileGetSystemDialysisPrescribeByModeId(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  1213. var record models.SystemPrescription
  1214. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).First(&record).Error
  1215. if err != nil {
  1216. if err == gorm.ErrRecordNotFound {
  1217. return nil, nil
  1218. } else {
  1219. return nil, err
  1220. }
  1221. }
  1222. return &record, nil
  1223. }
  1224. func GetSystemDialysisPrescribeByModeId(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  1225. var record models.SystemPrescription
  1226. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).Last(&record).Error
  1227. if err != nil {
  1228. if err == gorm.ErrRecordNotFound {
  1229. return nil, nil
  1230. } else {
  1231. return nil, err
  1232. }
  1233. }
  1234. return &record, nil
  1235. }
  1236. func GetDialysisOrderCount(orgID int64, patient_id int64, recordDate int64) (count int64, err error) {
  1237. 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
  1238. return
  1239. }
  1240. func GetFirstDateOfMonth(d time.Time) time.Time {
  1241. d = d.AddDate(0, 0, -d.Day()+1)
  1242. return GetZeroTime(d)
  1243. }
  1244. func GetZeroTime(d time.Time) time.Time {
  1245. return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
  1246. }
  1247. func GetDialysisCountByPatientId(startime int64, endtime int64, patientid int64, orgid int64) (order []*models.BloodDialysisOrderCount, err error) {
  1248. db := XTReadDB().Table("xt_dialysis_order as o")
  1249. err = db.Raw("select count(o.id) as count,o.patient_id from xt_dialysis_order as o left join xt_schedule AS x ON x.patient_id = o.patient_id where o.status = 1 and o.dialysis_date>=? and o.dialysis_date<=? and o.user_org_id = ? and x.schedule_date = o.dialysis_date and x.status = 1 and o.patient_id = ?", startime, endtime, orgid, patientid).Scan(&order).Group("o.patient_id").Error
  1250. return order, err
  1251. }
  1252. func FindConsumablesByDate(orgID int64, patient_id int64, recordDate int64) (consumables []*models.DialysisBeforePrepare, err error) {
  1253. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1 AND count<>0", orgID, patient_id, recordDate).Find(&consumables).Error
  1254. return
  1255. }
  1256. //func FindLastConsumables(orgID int64, patient_id int64, recordDate int64) (consumables []*models.DialysisBeforePrepare, err error){
  1257. // err = readDb.Model(&models.DialysisBeforePrepare{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? AND status = 1", orgID, patient_id,recordDate).Find(&consumables).Error
  1258. // return
  1259. //}
  1260. //
  1261. func GetLastTimeConsumables(orgID int64, patientID int64, maxDate int64) (prepare []*models.DialysisBeforePrepare, err error) {
  1262. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date < ?", patientID, orgID, maxDate).Order("record_date desc").Find(&prepare).Error
  1263. return
  1264. }
  1265. type GoodsType struct {
  1266. ID int64 `gorm:"column:id" json:"id"`
  1267. TypeName string `gorm:"column:type_name" json:"type_name"`
  1268. Remark string `gorm:"column:remark" json:"remark"`
  1269. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  1270. Status int64 `gorm:"column:status" json:"status"`
  1271. Type int64 `gorm:"column:type" json:"type"`
  1272. }
  1273. func (GoodsType) TableName() string {
  1274. return "xt_goods_type"
  1275. }
  1276. type VMGoodInfo struct {
  1277. ID int64 `gorm:"column:id" json:"id"`
  1278. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  1279. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  1280. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  1281. }
  1282. func (VMGoodInfo) TableName() string {
  1283. return "xt_good_information"
  1284. }
  1285. type AutomaticReduceDetail struct {
  1286. ID int64 `gorm:"column:id" json:"id"`
  1287. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  1288. Status int64 `gorm:"column:status" json:"status"`
  1289. RecordTime int64 `gorm:"column:record_time" json:"record_time"`
  1290. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  1291. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  1292. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  1293. VMGoodInfo VMGoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"info"`
  1294. GoodsType GoodsType `gorm:"ForeignKey:GoodTypeId;AssociationForeignKey:ID" json:"type"`
  1295. Count int64 `gorm:"column:count" json:"count"`
  1296. }
  1297. func (AutomaticReduceDetail) TableName() string {
  1298. return "xt_automatic_reduce_detail"
  1299. }
  1300. type DialysisBeforePrepare struct {
  1301. ID int64 `gorm:"column:id" json:"id" form:"id"`
  1302. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  1303. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  1304. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  1305. GoodId int64 `gorm:"column:good_id" json:"good_id" form:"good_id"`
  1306. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id" form:"good_type_id"`
  1307. Count int64 `gorm:"column:count" json:"count" form:"count"`
  1308. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  1309. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  1310. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  1311. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  1312. VMGoodInfo VMGoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"info"`
  1313. GoodsType GoodsType `gorm:"ForeignKey:GoodTypeId;AssociationForeignKey:ID" json:"type"`
  1314. }
  1315. func (DialysisBeforePrepare) TableName() string {
  1316. return "dialysis_before_prepare"
  1317. }
  1318. type MDialysisGoodsVM struct {
  1319. ID int64 `gorm:"column:id" json:"id"`
  1320. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  1321. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  1322. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  1323. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  1324. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  1325. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  1326. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  1327. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  1328. Status int64 `gorm:"column:status" json:"status"`
  1329. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  1330. SchedualPatient *MSchedualPatientVMList `gorm:"ForeignKey:PatientId" json:"patient"`
  1331. AutomaticReduceDetail []*AutomaticReduceDetail `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"good_user"`
  1332. LastAutomaticReduceDetail []*AutomaticReduceDetail `gorm:"-" json:"last_good_user"`
  1333. DialysisBeforePrepare []*DialysisBeforePrepare `gorm:"ForeignKey:PatientId,ScheduleDate;AssociationForeignKey:PatientId,RecordDate" json:"good_user_detail"`
  1334. LastDialysisBeforePrepare []*DialysisBeforePrepare `gorm:"-" json:"last_good_user_detail"`
  1335. }
  1336. func (MDialysisGoodsVM) TableName() string {
  1337. return "xt_schedule"
  1338. }
  1339. type VMWarehouseOutInfo struct {
  1340. ID int64 `gorm:"column:id" json:"id"`
  1341. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id"`
  1342. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  1343. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  1344. Count int64 `gorm:"column:count" json:"count"`
  1345. Price float64 `gorm:"column:price" json:"price"`
  1346. TotalPrice float64 `gorm:"column:total_price" json:"total_price"`
  1347. Status int64 `gorm:"column:status" json:"status"`
  1348. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  1349. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number"`
  1350. GoodInfo VMGoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  1351. GoodsType GoodsType `gorm:"ForeignKey:GoodTypeId;AssociationForeignKey:ID" json:"good_type"`
  1352. IsSys int64 `gorm:"column:is_sys" json:"is_sys"`
  1353. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time"`
  1354. Mtime int64 `gorm:"column:mtime" json:"mtime"`
  1355. Ctime int64 `gorm:"column:ctime" json:"ctime"`
  1356. }
  1357. func (VMWarehouseOutInfo) TableName() string {
  1358. return "xt_warehouse_out_info"
  1359. }
  1360. type DialysisGoodsDate struct {
  1361. RecordDate int64
  1362. }
  1363. type DialysisGoodsDetailDate struct {
  1364. RecordTime int64
  1365. }
  1366. func MobileGetDialysisGoods(orgID int64, scheduleDate int64, schedule_type int64, partition_id int64, page int, limit int, patient_id int64, keywords string, end_time int64) ([]*MDialysisGoodsVM, error, int64) {
  1367. var vms []*MDialysisGoodsVM
  1368. var total int64
  1369. db := readDb.
  1370. Table("xt_schedule as sch").
  1371. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1372. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1373. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1374. Preload("DialysisBeforePrepare", func(db *gorm.DB) *gorm.DB {
  1375. return db.Preload("VMGoodInfo", "status = 1 AND org_id = ?", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Where("status = 1 AND user_org_id = ? AND record_date = ? AND count > 0 ", orgID, scheduleDate)
  1376. }).
  1377. Preload("AutomaticReduceDetail", func(db *gorm.DB) *gorm.DB {
  1378. return db.Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Where("status = 1 AND org_id = ? AND count > 0 AND record_time >= ? AND record_time <= ? ", orgID, scheduleDate, end_time).Group("patient_id,good_id")
  1379. }).Where("sch.status = 1 AND sch.user_org_id = ?", orgID)
  1380. if scheduleDate != 0 {
  1381. db = db.Where("schedule_date = ?", scheduleDate)
  1382. }
  1383. if schedule_type != 0 {
  1384. db = db.Where("schedule_type = ?", schedule_type)
  1385. }
  1386. if partition_id != 0 {
  1387. db = db.Where("partition_id = ?", partition_id)
  1388. }
  1389. if patient_id != 0 {
  1390. db = db.Where("patient_id = ?", patient_id)
  1391. }
  1392. //db = db.Count(&total)
  1393. //offset := (page - 1) * limit
  1394. //
  1395. //db = db.Offset(offset).Limit(limit)
  1396. err := db.Find(&vms).Error
  1397. return vms, err, total
  1398. }
  1399. func GetLastDialysisGoods(patient_id int64, orgID int64, record_time int64) (goodUser []*AutomaticReduceDetail, err error) {
  1400. var Id []*DialysisGoodsDetailDate
  1401. err = readDb.Model(&AutomaticReduceDetail{}).Where("patient_id = ? AND org_id = ? AND status=1 AND record_time < ? AND count > 0", patient_id, orgID, record_time).Select("record_time").Group("record_time").Order("record_time asc").Scan(&Id).Error
  1402. if len(Id) > 0 {
  1403. err = readDb.Model(&AutomaticReduceDetail{}).Where("patient_id = ? AND org_id = ? AND status=1 AND record_time = ? AND count > 0", patient_id, orgID, Id[len(Id)-1].RecordTime).Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Find(&goodUser).Error
  1404. }
  1405. return
  1406. }
  1407. func GetLastDialysisBeforePrepare(patient_id int64, orgID int64, record_time int64) (goodUser []*DialysisBeforePrepare, err error) {
  1408. var Id []*DialysisGoodsDate
  1409. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date < ? AND count > 0", patient_id, orgID, record_time).Select("record_date").Group("record_date").Order("record_date asc").Scan(&Id).Error
  1410. if len(Id) > 0 {
  1411. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date = ? AND count > 0", patient_id, orgID, Id[len(Id)-1].RecordDate).Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Find(&goodUser).Error
  1412. }
  1413. return
  1414. }
  1415. func MobileGetGoodsStatistics(orgID int64, start_time int64, end_time int64) (list []*models.StockInfo, err error) {
  1416. db := readDb.Model(&models.StockInfo{})
  1417. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgID)
  1418. db = db.Joins("JOIN xt_warehouse_out_info AS info ON info.good_id=xt_good_information.id AND info.status = 1 AND info.org_id = ?", orgID).Group("xt_good_information.id")
  1419. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  1420. return db.Where("xt_warehouse_out_info.org_id = ? AND xt_warehouse_out_info.status = 1 AND xt_warehouse_out_info.sys_record_time >= ? AND xt_warehouse_out_info.sys_record_time <= ?", orgID, start_time, end_time)
  1421. })
  1422. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgID)
  1423. err = db.Order("ctime desc").Find(&list).Error
  1424. return
  1425. }
  1426. func PCGetDialysisGoods(orgID int64, scheduleDate int64, schedule_type int64, partition_id int64, page int, limit int, patient_id int64, keywords string, end_time int64) ([]*MDialysisGoodsVM, error, int64) {
  1427. var vms []*MDialysisGoodsVM
  1428. var total int64
  1429. db := readDb.
  1430. Model(&models.Schedule{}).
  1431. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1432. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1433. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1434. Preload("DialysisBeforePrepare", func(db *gorm.DB) *gorm.DB {
  1435. return db.Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Where("status = 1 AND user_org_id = ? AND record_date = ? AND count > 0 ", orgID, scheduleDate)
  1436. }).
  1437. Preload("AutomaticReduceDetail", func(db *gorm.DB) *gorm.DB {
  1438. return db.Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Where("status = 1 AND org_id = ? AND record_time >= ? AND record_time <=? AND count > 0", orgID, scheduleDate, end_time)
  1439. }).Where("xt_schedule.status = 1 AND xt_schedule.user_org_id = ?", orgID)
  1440. if scheduleDate != 0 {
  1441. db = db.Where("schedule_date = ?", scheduleDate)
  1442. }
  1443. if patient_id != 0 {
  1444. db = db.Where("patient_id = ?", patient_id)
  1445. }
  1446. if len(keywords) != 0 {
  1447. keywords = "%" + keywords + "%"
  1448. db = db.Joins("JOIN xt_patients AS patient ON patient.id=xt_schedule.patient_id AND patient.status = 1 AND patient.user_org_id = ? AND patient.name Like ?", orgID, keywords)
  1449. } else {
  1450. if schedule_type != 0 {
  1451. db = db.Where("schedule_type = ?", schedule_type)
  1452. }
  1453. if partition_id != 0 {
  1454. db = db.Where("partition_id = ?", partition_id)
  1455. }
  1456. db = db.Count(&total)
  1457. offset := (page - 1) * limit
  1458. db = db.Offset(offset).Limit(limit)
  1459. }
  1460. err := db.Find(&vms).Error
  1461. return vms, err, total
  1462. }
  1463. func GetLastDialysisGoodsTwo(patient_id int64, orgID int64, record_time int64) (goodUser []*AutomaticReduceDetail, err error) {
  1464. var Id []*DialysisGoodsDetailDate
  1465. err = readDb.Model(&AutomaticReduceDetail{}).Where("patient_id = ? AND org_id = ? AND status=1 AND record_time < ? AND count > 0", patient_id, orgID, record_time).Select("record_time").Group("record_time").Order("record_time asc").Scan(&Id).Error
  1466. if len(Id) > 0 {
  1467. err = readDb.Model(&AutomaticReduceDetail{}).Where("patient_id = ? AND org_id = ? AND status=1 AND record_time = ? AND count > 0", patient_id, orgID, Id[len(Id)-1].RecordTime).Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Find(&goodUser).Error
  1468. }
  1469. return
  1470. }
  1471. func GetLastDialysisBeforePrepareTwo(patient_id int64, orgID int64, record_time int64) (goodUser []*DialysisBeforePrepare, err error) {
  1472. var Id []*DialysisGoodsDate
  1473. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date < ? AND count > 0", patient_id, orgID, record_time).Select("record_date").Group("record_date").Order("record_date asc").Scan(&Id).Error
  1474. if len(Id) > 0 {
  1475. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date = ? AND count > 0", patient_id, orgID, Id[len(Id)-1].RecordDate).Preload("VMGoodInfo", "status = 1 AND org_id = ? ", orgID).Preload("GoodsType", "status = 1 AND (org_id = ? OR org_id = 0) ", orgID).Find(&goodUser).Error
  1476. }
  1477. return
  1478. }
  1479. func GetAssessmentBefor(orgid int64, patientid int64, recorddate int64) (*models.PredialysisEvaluation, error) {
  1480. evaluation := models.PredialysisEvaluation{}
  1481. err := XTReadDB().Model(&evaluation).Where("user_org_id = ? and patient_id = ? and assessment_date = ? and status = 1", orgid, patientid, recorddate).Find(&evaluation).Error
  1482. if err == gorm.ErrRecordNotFound {
  1483. return nil, err
  1484. }
  1485. if err != nil {
  1486. return nil, err
  1487. }
  1488. return &evaluation, nil
  1489. }
  1490. func GetAllHisDoctorAdvice(orgid int64, patientid int64, recorddate int64) (his []*models.HisDoctorAdviceInfo, err error) {
  1491. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date = ?", patientid, orgid, recorddate).Find(&his).Error
  1492. return
  1493. }
  1494. func GetLastDialysisPrescriptionByPatientId(orgid int64, patientid int64, recorddate int64) (*models.DialysisPrescription, error) {
  1495. prescription := models.DialysisPrescription{}
  1496. err := readDb.Model(&models.DialysisPrescription{}).Where("user_org_id = ? and patient_id = ? and record_date = ? and status = 1", orgid, patientid, recorddate).Find(&prescription).Error
  1497. if err == gorm.ErrRecordNotFound {
  1498. return nil, err
  1499. }
  1500. if err != nil {
  1501. return nil, err
  1502. }
  1503. return &prescription, nil
  1504. }
  1505. type HisMScheduleDoctorAdviceVM struct {
  1506. ID int64 `gorm:"column:id" json:"id"`
  1507. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  1508. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  1509. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  1510. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  1511. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  1512. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  1513. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  1514. Status int64 `gorm:"column:status" json:"status"`
  1515. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:DialysisDate,PatientId;AssociationForeignKey:ScheduleDate,PatientId" json:"dialysis_order"`
  1516. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  1517. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  1518. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  1519. HisDoctorAdviceInfo []*models.HisDoctorAdviceInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  1520. }
  1521. func (HisMScheduleDoctorAdviceVM) TableName() string {
  1522. return "xt_schedule"
  1523. }
  1524. func GetHisDoctorConfig(orgid int64) (models.XtHisConfig, error) {
  1525. config := models.XtHisConfig{}
  1526. err := XTReadDB().Model(&config).Where("user_org_id = ? and status =1", orgid).Find(&config).Error
  1527. return config, err
  1528. }
  1529. func GetHisDoctorPatientById(orgid int64, patientid int64, recordtime int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  1530. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  1531. if orgid > 0 {
  1532. db = db.Where("x.user_org_id = ?", orgid)
  1533. }
  1534. if patientid > 0 {
  1535. db = db.Where("x.patient_id =?", patientid)
  1536. }
  1537. if recordtime > 0 {
  1538. db = db.Where("x.advice_date = ?", recordtime)
  1539. }
  1540. err = db.Select("x.id,x.user_org_id,x.patient_id,x.his_patient_id,x.advice_type,x.advice_date,x.start_time,x.advice_name,x.advice_desc,x.reminder_date,x.single_dose,x.single_dose_unit,x.single_dose_unit,x.prescribing_number_unit,x.prescribing_number,x.delivery_way,x.execution_frequency,x.advice_doctor,x.status,x.advice_affirm,x.remark,x.stop_time,x.stop_reason,x.stop_doctor,x.stop_state,x.parent_id,x.execution_time,x.execution_staff,x.checker,x.record_date,x.dialysis_order_id,x.check_time,x.check_state,x.drug_spec,x.drug_spec_unit,x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.template_id,x.modifier,x.drug_id,x.price,x.prescription_id,x.med_list_codg,x.feedetl_sn,x.day").Find(&advice).Error
  1541. return advice, err
  1542. }