mobile_dialysis_service.go 145KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. "time"
  7. "encoding/json"
  8. "strconv"
  9. )
  10. // func GetSchedualPatients(orgID int64) ([]*MDialysisScheduleVM, error) {
  11. // var vms []*MDialysisScheduleVM
  12. // err := readDb.
  13. // Table("xt_schedule as sch").
  14. // Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  15. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  16. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  17. // Preload("TreatmentMode", "status = 1").
  18. // Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  19. // Preload("DialysisOrder.MonitoringRecords", "status = 1 AND user_org_id = ?", orgID).
  20. // Where("sch.status = 1 AND sch.user_org_id = ? AND schedule_date = 1536768000", orgID).
  21. // Find(&vms).
  22. // Error
  23. // return vms, err
  24. // }
  25. func MobileGetDialysisScheduals(orgID int64, scheduleDate int64, scheduleType int64) ([]*MDialysisScheduleVMForList, error) {
  26. var vms []*MDialysisScheduleVMForList
  27. db := readDb.
  28. Table("xt_schedule as sch").
  29. // Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  30. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  31. // Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  32. // Preload("Advices", "status = 1 AND user_org_id = ? AND advice_type = 2 AND advice_date = ? ", orgID, scheduleDate).
  33. // Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  34. // Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  35. // Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).
  36. // Preload("DialysisOrder.DeviceNumber", "status = 1 AND org_id = ?", orgID).
  37. // Preload("TreatmentSummary", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  38. // Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  39. // Preload("HisAdvices", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  40. // Preload("DialysisOrder.MonitoringRecords", func(rdb *gorm.DB) *gorm.DB {
  41. // return rdb.Where("status = 1 AND user_org_id = ?", orgID).Order("operate_time DESC")
  42. // }).
  43. Where("sch.status = 1 AND sch.user_org_id = ?", orgID)
  44. if scheduleDate != 0 {
  45. db = db.Where("schedule_date = ?", scheduleDate)
  46. }
  47. if scheduleType != 0 {
  48. db = db.Where("schedule_type = ?", scheduleType)
  49. }
  50. err := db.Find(&vms).Error
  51. return vms, err
  52. }
  53. func MobileGetWaitingScheduals(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  54. var vms []*MDialysisScheduleVM
  55. db := readDb.
  56. Table("xt_schedule as sch").
  57. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  58. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  59. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  60. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  61. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  62. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  63. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  64. Preload("Advices", "status = 1 AND user_org_id = ? AND advice_type = 2 ", orgID).
  65. Preload("TreatmentMode", "status = 1").
  66. Preload("TreatmentSummary", "status = 1 AND user_org_id = ?", orgID).
  67. Where("sch.status = 1 AND sch.user_org_id = ?", orgID)
  68. if scheduleDate != 0 {
  69. db = db.Where("schedule_date = ?", scheduleDate)
  70. }
  71. err := db.Find(&vms).Error
  72. return vms, err
  73. }
  74. type VMTreatmentSummary struct {
  75. ID int64 `gorm:"column:id" json:"id"`
  76. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  77. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  78. AssessmentDate int64 `gorm:"column:assessment_date" json:"assessment_date"`
  79. DialysisSummary string `gorm:"column:dialysis_summary" json:"dialysis_summary" form:"dialysis_summary"`
  80. }
  81. func (VMTreatmentSummary) TableName() string {
  82. return "xt_treatment_summary"
  83. }
  84. type AssessmentAfterDislysis struct {
  85. ID int64 `gorm:"column:id" json:"id"`
  86. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  87. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  88. AssessmentDate int64 `gorm:"column:assessment_date" json:"assessment_date"`
  89. WeightAfter float64 `gorm:"column:weight_after" json:"weight_after"`
  90. Status int64 `gorm:"column:status" json:"status"`
  91. }
  92. func (AssessmentAfterDislysis) TableName() string {
  93. return "xt_assessment_after_dislysis"
  94. }
  95. type MDialysisScheduleVM struct {
  96. ID int64 `gorm:"column:id" json:"id"`
  97. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  98. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  99. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  100. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  101. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  102. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  103. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  104. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  105. Status int64 `gorm:"column:status" json:"status"`
  106. SchedualPatient *MSchedualPatientVMList `gorm:"ForeignKey:PatientId" json:"patient"`
  107. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  108. DialysisOrder *MDialysisOrderVMList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  109. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  110. AssessmentBeforeDislysis *models.PredialysisEvaluationList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_before_dislysis"`
  111. AssessmentAfterDislysis *AssessmentAfterDislysis `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_after_dislysis"`
  112. HisAdvices []*VMHisDoctorAdviceInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"his_doctor_advice"`
  113. Advices []*VMDoctorAdvice `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  114. TreatmentSummary *VMTreatmentSummary `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"treatment_summary"`
  115. }
  116. func (MDialysisScheduleVM) TableName() string {
  117. return "xt_schedule"
  118. }
  119. type MDialysisScheduleVMForList struct {
  120. ID int64 `gorm:"column:id" json:"id"`
  121. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  122. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  123. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  124. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  125. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  126. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  127. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  128. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  129. Status int64 `gorm:"column:status" json:"status"`
  130. SchedualPatient *models.MSchedualPatientList `gorm:"ForeignKey:PatientId" json:"patient"`
  131. DeviceNumber *models.MDeviceNumberForList `gorm:"ForeignKey:BedId" json:"device_number"`
  132. DialysisOrder *models.MDialysisOrderForList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  133. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  134. AssessmentBeforeDislysis *models.PredialysisEvaluationList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_before_dislysis"`
  135. AssessmentAfterDislysis *models.VMAssessmentAfterDislysis `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"assessment_after_dislysis"`
  136. HisAdvices []VMHisDoctorAdviceInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"his_doctor_advice"`
  137. Advices []models.VMDoctorAdviceForList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  138. TreatmentSummary *models.VMTreatmentSummaryForList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"treatment_summary"`
  139. }
  140. func (MDialysisScheduleVMForList) TableName() string {
  141. return "xt_schedule"
  142. }
  143. type MDeviceNumberVM struct {
  144. models.DeviceNumber
  145. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  146. }
  147. func (MDeviceNumberVM) TableName() string {
  148. return "xt_device_number"
  149. }
  150. type MSchedualPatientVMList struct {
  151. ID int64 `gorm:"column:id" json:"id" form:"id"`
  152. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  153. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  154. PatientType int64 `gorm:"column:patient_type" json:"patient_type" form:"patient_type"`
  155. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  156. Avatar string `gorm:"column:avatar" json:"avatar" form:"avatar"`
  157. Gender int64 `gorm:"column:gender" json:"gender" form:"gender"`
  158. Birthday int64 `gorm:"column:birthday" json:"birthday" form:"birthday"`
  159. Age int64 `gorm:"column:age" json:"age"`
  160. Name string `gorm:"column:name" json:"name" form:"name"`
  161. IdCardNo string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
  162. UserSysBeforeCount int64 `gorm:"column:user_sys_before_count" json:"user_sys_before_count" form:"user_sys_before_count"`
  163. }
  164. func (MSchedualPatientVMList) TableName() string {
  165. return "xt_patients"
  166. }
  167. type MSchedualPatientVM struct {
  168. ID int64 `gorm:"column:id" json:"id" form:"id"`
  169. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  170. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  171. PatientType int64 `gorm:"column:patient_type" json:"patient_type" form:"patient_type"`
  172. Avatar string `gorm:"column:avatar" json:"avatar" form:"avatar"`
  173. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  174. AdmissionNumber string `gorm:"column:admission_number" json:"admission_number" form:"admission_number"`
  175. Source int64 `gorm:"column:source" json:"source" form:"source"`
  176. Lapseto int64 `gorm:"column:lapseto" json:"lapseto" form:"lapseto"`
  177. PartitionId int64 `gorm:"column:partition_id" json:"partition_id" form:"partition_id"`
  178. BedId int64 `gorm:"column:bed_id" json:"bed_id" form:"bed_id"`
  179. Name string `gorm:"column:name" json:"name" form:"name"`
  180. Alias string `gorm:"column:alias" json:"alias" form:"alias"`
  181. Gender int64 `gorm:"column:gender" json:"gender" form:"gender"`
  182. Nation string `gorm:"column:nation" json:"nation" form:"nation"`
  183. NativePlace string `gorm:"column:native_place" json:"native_place" form:"native_place"`
  184. MaritalStatus int64 `gorm:"column:marital_status" json:"marital_status" form:"marital_status"`
  185. IdCardNo string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
  186. Birthday int64 `gorm:"column:birthday" json:"birthday" form:"birthday"`
  187. ReimbursementWayId int64 `gorm:"column:reimbursement_way_id" json:"reimbursement_way_id" form:"reimbursement_way_id"`
  188. HealthCareType int64 `gorm:"column:health_care_type" json:"health_care_type" form:"health_care_type"`
  189. HealthCareNo string `gorm:"column:health_care_no" json:"health_care_no" form:"health_care_no"`
  190. HealthCareDueDate int64 `gorm:"column:health_care_due_date" json:"health_care_due_date" form:"health_care_due_date"`
  191. Height int64 `gorm:"column:height" json:"height" form:"height"`
  192. BloodType int64 `gorm:"column:blood_type" json:"blood_type" form:"blood_type"`
  193. Rh int64 `gorm:"column:rh" json:"rh" form:"rh"`
  194. HealthCareDueAlertDate int64 `gorm:"column:health_care_due_alert_date" json:"health_care_due_alert_date" form:"health_care_due_alert_date"`
  195. EducationLevel int64 `gorm:"column:education_level" json:"education_level" form:"education_level"`
  196. Profession int64 `gorm:"column:profession" json:"profession" form:"profession"`
  197. Phone string `gorm:"column:phone" json:"phone" form:"phone"`
  198. HomeTelephone string `gorm:"column:home_telephone" json:"home_telephone" form:"home_telephone"`
  199. RelativePhone string `gorm:"column:relative_phone" json:"relative_phone" form:"relative_phone"`
  200. RelativeRelations string `gorm:"column:relative_relations" json:"relative_relations" form:"relative_relations"`
  201. HomeAddress string `gorm:"column:home_address" json:"home_address" form:"home_address"`
  202. WorkUnit string `gorm:"column:work_unit" json:"work_unit" form:"work_unit"`
  203. UnitAddress string `gorm:"column:unit_address" json:"unit_address" form:"unit_address"`
  204. Children int64 `gorm:"column:children" json:"children" form:"children"`
  205. ReceivingDate int64 `gorm:"column:receiving_date" json:"receiving_date" form:"receiving_date"`
  206. IsHospitalFirstDialysis int64 `gorm:"column:is_hospital_first_dialysis" json:"is_hospital_first_dialysis" form:"is_hospital_first_dialysis"`
  207. FirstDialysisDate int64 `gorm:"column:first_dialysis_date" json:"first_dialysis_date" form:"first_dialysis_date"`
  208. FirstDialysisHospital string `gorm:"column:first_dialysis_hospital" json:"first_dialysis_hospital" form:"first_dialysis_hospital"`
  209. InductionPeriod int64 `gorm:"column:induction_period" json:"induction_period" form:"induction_period"`
  210. InitialDialysis int64 `gorm:"column:initial_dialysis" json:"initial_dialysis" form:"initial_dialysis"`
  211. TotalDialysis int64 `gorm:"column:total_dialysis" json:"total_dialysis" form:"total_dialysis"`
  212. AttendingDoctorId int64 `gorm:"column:attending_doctor_id" json:"attending_doctor_id" form:"attending_doctor_id"`
  213. HeadNurseId int64 `gorm:"column:head_nurse_id" json:"head_nurse_id" form:"head_nurse_id"`
  214. Evaluate string `gorm:"column:evaluate" json:"evaluate" form:"evaluate"`
  215. Diagnose string `gorm:"column:diagnose" json:"diagnose" form:"diagnose"`
  216. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  217. RegistrarsId int64 `gorm:"column:registrars_id" json:"registrars_id" form:"registrars_id"`
  218. Registrars string `gorm:"column:registrars" json:"registrars" form:"registrars"`
  219. QrCode string `gorm:"column:qr_code" json:"qr_code" form:"qr_code"`
  220. BindingState int64 `gorm:"column:binding_state" json:"binding_state" form:"binding_state"`
  221. PatientComplains string `gorm:"column:patient_complains" json:"patient_complains"` // 主诉
  222. PresentHistory string `gorm:"column:present_history" json:"present_history"` // 现病史
  223. PastHistory string `gorm:"column:past_history" json:"past_history"` // 既往史
  224. Temperature float64 `gorm:"column:temperature" json:"temperature"` // 体格检查-体温
  225. Pulse int64 `gorm:"column:pulse" json:"pulse"` // 体格检查-脉搏
  226. Respiratory int64 `gorm:"column:respiratory" json:"respiratory"` // 体格检查-呼吸频率
  227. SBP int64 `gorm:"column:sbp" json:"sbp"` // 体格检查-收缩压
  228. DBP int64 `gorm:"column:dbp" json:"dbp"` // 体格检查-舒张压
  229. Status int64 `gorm:"column:status" json:"status" form:"status"`
  230. Age int64 `gorm:"column:age" json:"age"`
  231. IsOpenRemind int64 `gorm:"column:is_open_remind" json:"is_open_remind"`
  232. DialysisAge int64 `gorm:"column:dialysis_age" json:"dialysis_age" form:"dialysis_age"`
  233. ExpenseKind int64 `gorm:"column:expense_kind" json:"expense_kind" form:"expense_kind"`
  234. TellPhone string `gorm:"column:tell_phone" json:"tell_phone" form:"tell_phone"`
  235. FirstTreatmentDate int64 `gorm:"column:first_treatment_date" json:"first_treatment_date" form:"first_treatment_date"`
  236. ContactName string `gorm:"column:contact_name" json:"contact_name" form:"contact_name"`
  237. UserSysBeforeCount int64 `gorm:"column:user_sys_before_count" json:"user_sys_before_count" form:"user_sys_before_count"`
  238. }
  239. func (MSchedualPatientVM) TableName() string {
  240. return "xt_patients"
  241. }
  242. type MDialysisOrderVM struct {
  243. ID int64 `gorm:"column:id" json:"id"`
  244. DialysisDate int64 `gorm:"column:dialysis_date" json:"dialysis_date"`
  245. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  246. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  247. // PrescriptionId int64 `gorm:"column:prescription_id" json:"prescription_id"`
  248. Stage int64 `gorm:"column:stage" json:"stage"`
  249. // Remark string `gorm:"column:remark" json:"remark"`
  250. BedID int64 `gorm:"column:bed_id" json:"bed_id"`
  251. StartNurse int64 `gorm:"column:start_nurse" json:"start_nurse"`
  252. FinishNurse int64 `gorm:"column:finish_nurse" json:"finish_nurse"`
  253. Status int64 `gorm:"column:status" json:"status"`
  254. PunctureNurse int64 `gorm:"column:puncture_nurse" json:"puncture_nurse"`
  255. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedID" json:"device_number"`
  256. MonitoringRecords []*models.MonitoringRecord `gorm:"ForeignKey:DialysisOrderId" json:"monitoring_records"`
  257. Creator int64 `gorm:"column:creator" json:"creator"`
  258. Modifier int64 `gorm:"column:modifier" json:"modifier"`
  259. FinishCreator int64 `gorm:"column:finish_creator" json:"finish_creator"`
  260. FinishModifier int64 `gorm:"column:finish_modifier" json:"finish_modifier"`
  261. SchedualType int64 `gorm:"column:schedual_type" json:"schedual_type"`
  262. WashpipeNurse int64 `gorm:"column:washpipe_nurse" json:"washpipe_nurse" form:"washpipe_nurse"`
  263. }
  264. func (MDialysisOrderVM) TableName() string {
  265. return "xt_dialysis_order"
  266. }
  267. type MDialysisOrderVMList struct {
  268. ID int64 `gorm:"column:id" json:"id"`
  269. DialysisDate int64 `gorm:"column:dialysis_date" json:"dialysis_date"`
  270. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  271. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  272. // PrescriptionId int64 `gorm:"column:prescription_id" json:"prescription_id"`
  273. Stage int64 `gorm:"column:stage" json:"stage"`
  274. // Remark string `gorm:"column:remark" json:"remark"`
  275. BedID int64 `gorm:"column:bed_id" json:"bed_id"`
  276. StartNurse int64 `gorm:"column:start_nurse" json:"start_nurse"`
  277. Status int64 `gorm:"column:status" json:"status"`
  278. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedID" json:"device_number"`
  279. Creator int64 `gorm:"column:creator" json:"creator"`
  280. WashpipeNurse int64 `gorm:"column:washpipe_nurse" json:"washpipe_nurse" form:"washpipe_nurse"`
  281. }
  282. func (MDialysisOrderVMList) TableName() string {
  283. return "xt_dialysis_order"
  284. }
  285. type VMDoctorAdvice struct {
  286. ID int64 `gorm:"column:id" json:"id" form:"id"`
  287. GroupNo int64 `gorm:"column:groupno" json:"groupno" form:"groupno"`
  288. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  289. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  290. AdviceDate int64 `gorm:"column:advice_date" json:"advice_date" form:"advice_date"`
  291. Status int64 `gorm:"column:status" json:"status" form:"status"`
  292. ExecutionState int64 `gorm:"column:execution_state" json:"execution_state" form:"execution_state"`
  293. RecordDate int64 `gorm:"column:record_date" json:"record_date"`
  294. CheckTime int64 `gorm:"column:check_time" json:"check_time" form:"check_time"`
  295. CheckState int64 `gorm:"column:check_state" json:"check_state" form:"check_state"`
  296. ParentId int64 `gorm:"column:parent_id" json:"parent_id" form:"parent_id"`
  297. }
  298. func (VMDoctorAdvice) TableName() string {
  299. return "xt_doctor_advice"
  300. }
  301. type VMHisDoctorAdviceInfo struct {
  302. ID int64 `gorm:"column:id" json:"id" form:"id"`
  303. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  304. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  305. HisPatientId int64 `gorm:"column:his_patient_id" json:"his_patient_id" form:"his_patient_id"`
  306. ExecutionTime int64 `gorm:"column:execution_time" json:"execution_time" form:"execution_time"`
  307. ExecutionStaff int64 `gorm:"column:execution_staff" json:"execution_staff" form:"execution_staff"`
  308. ExecutionState int64 `gorm:"column:execution_state" json:"execution_state" form:"execution_state"`
  309. }
  310. func (VMHisDoctorAdviceInfo) TableName() string {
  311. return "his_doctor_advice_info"
  312. }
  313. type VMAssessmentAfterDislysis struct {
  314. ID int64 `gorm:"column:id" json:"id"`
  315. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  316. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  317. AssessmentDate int64 `gorm:"column:assessment_date" json:"assessment_date"`
  318. WeightAfter float64 `gorm:"column:weight_after" json:"weight_after"`
  319. Status int64 `gorm:"column:status" json:"status"`
  320. }
  321. func (VMAssessmentAfterDislysis) TableName() string {
  322. return "xt_assessment_after_dislysis"
  323. }
  324. // 获取透析记录
  325. func MobileGetDialysisRecord(orgID int64, patientID int64, recordDate int64) (*models.DialysisOrder, error) {
  326. var record models.DialysisOrder
  327. err := readDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgID, patientID, recordDate).First(&record).Error
  328. if err != nil {
  329. if err == gorm.ErrRecordNotFound {
  330. return nil, nil
  331. } else {
  332. return nil, err
  333. }
  334. }
  335. return &record, nil
  336. }
  337. // 用户基本信息
  338. func MobileGetPatientDetail(orgID int64, patientID int64) (*MPatient, error) {
  339. var patient MPatient
  340. err := readDb.Model(&MPatient{}).Where("status = 1 AND user_org_id = ? AND id = ?", orgID, patientID).First(&patient).Error
  341. if err != nil {
  342. if err == gorm.ErrRecordNotFound {
  343. return nil, nil
  344. } else {
  345. return nil, err
  346. }
  347. }
  348. return &patient, nil
  349. }
  350. // 用户排班信息
  351. func MobileGetSchedualDetail(orgID int64, patientID int64, schedualDate int64) (*MDialysisScheduleVM, error) {
  352. redis := RedisClient()
  353. defer redis.Close()
  354. var vm MDialysisScheduleVM
  355. // cur_date := time.Now().Format("2006-01-02")
  356. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(schedualDate, 10) + ":schedual_detail"
  357. schedual_detail_str, _ := redis.Get(key).Result()
  358. if len(schedual_detail_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  359. err := readDb.
  360. Table("xt_schedule").
  361. // Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  362. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  363. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  364. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND patient_id = ?", orgID, schedualDate, patientID).
  365. First(&vm).Error
  366. if err != nil {
  367. if err == gorm.ErrRecordNotFound {
  368. return nil, nil
  369. } else {
  370. return nil, err
  371. }
  372. } else {
  373. if vm.ID > 0 {
  374. //缓存数据
  375. schedual_detail_str, err := json.Marshal(vm)
  376. if err == nil {
  377. redis.Set(key, schedual_detail_str, time.Second*60*60*18)
  378. }
  379. } else {
  380. redis.Set(key, " ", time.Second*60*60*18)
  381. }
  382. return &vm, nil
  383. }
  384. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  385. json.Unmarshal([]byte(schedual_detail_str), &vm)
  386. return &vm, nil
  387. }
  388. }
  389. // 用户排班信息
  390. func MobileGetPatientSchedual(orgID int64, patientID int64, schedualDate int64) (*models.Schedule, error) {
  391. var schedule models.Schedule
  392. err := readDb.
  393. Table("xt_schedule").
  394. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND patient_id = ?", orgID, schedualDate, patientID).
  395. First(&schedule).Error
  396. if err != nil {
  397. if err == gorm.ErrRecordNotFound {
  398. return nil, nil
  399. } else {
  400. return nil, err
  401. }
  402. }
  403. return &schedule, nil
  404. }
  405. type MPatient struct {
  406. ID int64 `gorm:"column:id" json:"id" form:"id"`
  407. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  408. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  409. PatientType int64 `gorm:"column:patient_type" json:"patient_type" form:"patient_type"`
  410. Avatar string `gorm:"column:avatar" json:"avatar" form:"avatar"`
  411. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  412. AdmissionNumber string `gorm:"column:admission_number" json:"admission_number" form:"admission_number"`
  413. Source int64 `gorm:"column:source" json:"source" form:"source"`
  414. Lapseto int64 `gorm:"column:lapseto" json:"lapseto" form:"lapseto"`
  415. PartitionId int64 `gorm:"column:partition_id" json:"partition_id" form:"partition_id"`
  416. BedId int64 `gorm:"column:bed_id" json:"bed_id" form:"bed_id"`
  417. Name string `gorm:"column:name" json:"name" form:"name"`
  418. Alias string `gorm:"column:alias" json:"alias" form:"alias"`
  419. Gender int64 `gorm:"column:gender" json:"gender" form:"gender"`
  420. Nation string `gorm:"column:nation" json:"nation" form:"nation"`
  421. NativePlace string `gorm:"column:native_place" json:"native_place" form:"native_place"`
  422. MaritalStatus int64 `gorm:"column:marital_status" json:"marital_status" form:"marital_status"`
  423. IdCardNo string `gorm:"column:id_card_no" json:"id_card_no" form:"id_card_no"`
  424. Birthday int64 `gorm:"column:birthday" json:"birthday" form:"birthday"`
  425. ReimbursementWayId int64 `gorm:"column:reimbursement_way_id" json:"reimbursement_way_id" form:"reimbursement_way_id"`
  426. HealthCareType int64 `gorm:"column:health_care_type" json:"health_care_type" form:"health_care_type"`
  427. HealthCareNo string `gorm:"column:health_care_no" json:"health_care_no" form:"health_care_no"`
  428. HealthCareDueDate int64 `gorm:"column:health_care_due_date" json:"health_care_due_date" form:"health_care_due_date"`
  429. Height int64 `gorm:"column:height" json:"height" form:"height"`
  430. BloodType int64 `gorm:"column:blood_type" json:"blood_type" form:"blood_type"`
  431. Rh int64 `gorm:"column:rh" json:"rh" form:"rh"`
  432. HealthCareDueAlertDate int64 `gorm:"column:health_care_due_alert_date" json:"health_care_due_alert_date" form:"health_care_due_alert_date"`
  433. EducationLevel int64 `gorm:"column:education_level" json:"education_level" form:"education_level"`
  434. Profession int64 `gorm:"column:profession" json:"profession" form:"profession"`
  435. Phone string `gorm:"column:phone" json:"phone" form:"phone"`
  436. HomeTelephone string `gorm:"column:home_telephone" json:"home_telephone" form:"home_telephone"`
  437. RelativePhone string `gorm:"column:relative_phone" json:"relative_phone" form:"relative_phone"`
  438. RelativeRelations string `gorm:"column:relative_relations" json:"relative_relations" form:"relative_relations"`
  439. HomeAddress string `gorm:"column:home_address" json:"home_address" form:"home_address"`
  440. WorkUnit string `gorm:"column:work_unit" json:"work_unit" form:"work_unit"`
  441. UnitAddress string `gorm:"column:unit_address" json:"unit_address" form:"unit_address"`
  442. Children int64 `gorm:"column:children" json:"children" form:"children"`
  443. ReceivingDate int64 `gorm:"column:receiving_date" json:"receiving_date" form:"receiving_date"`
  444. IsHospitalFirstDialysis int64 `gorm:"column:is_hospital_first_dialysis" json:"is_hospital_first_dialysis" form:"is_hospital_first_dialysis"`
  445. FirstDialysisDate int64 `gorm:"column:first_dialysis_date" json:"first_dialysis_date" form:"first_dialysis_date"`
  446. FirstDialysisHospital string `gorm:"column:first_dialysis_hospital" json:"first_dialysis_hospital" form:"first_dialysis_hospital"`
  447. InductionPeriod int64 `gorm:"column:induction_period" json:"induction_period" form:"induction_period"`
  448. InitialDialysis int64 `gorm:"column:initial_dialysis" json:"initial_dialysis" form:"initial_dialysis"`
  449. TotalDialysis int64 `gorm:"column:total_dialysis" json:"total_dialysis" form:"total_dialysis"`
  450. AttendingDoctorId int64 `gorm:"column:attending_doctor_id" json:"attending_doctor_id" form:"attending_doctor_id"`
  451. HeadNurseId int64 `gorm:"column:head_nurse_id" json:"head_nurse_id" form:"head_nurse_id"`
  452. Evaluate string `gorm:"column:evaluate" json:"evaluate" form:"evaluate"`
  453. Diagnose string `gorm:"column:diagnose" json:"diagnose" form:"diagnose"`
  454. Remark string `gorm:"column:remark" json:"remark" form:"remark"`
  455. RegistrarsId int64 `gorm:"column:registrars_id" json:"registrars_id" form:"registrars_id"`
  456. Registrars string `gorm:"column:registrars" json:"registrars" form:"registrars"`
  457. QrCode string `gorm:"column:qr_code" json:"qr_code" form:"qr_code"`
  458. BindingState int64 `gorm:"column:binding_state" json:"binding_state" form:"binding_state"`
  459. PatientComplains string `gorm:"column:patient_complains" json:"patient_complains"` // 主诉
  460. PresentHistory string `gorm:"column:present_history" json:"present_history"` // 现病史
  461. PastHistory string `gorm:"column:past_history" json:"past_history"` // 既往史
  462. Temperature float64 `gorm:"column:temperature" json:"temperature"` // 体格检查-体温
  463. Pulse int64 `gorm:"column:pulse" json:"pulse"` // 体格检查-脉搏
  464. Respiratory int64 `gorm:"column:respiratory" json:"respiratory"` // 体格检查-呼吸频率
  465. SBP int64 `gorm:"column:sbp" json:"sbp"` // 体格检查-收缩压
  466. DBP int64 `gorm:"column:dbp" json:"dbp"` // 体格检查-舒张压
  467. Status int64 `gorm:"column:status" json:"status" form:"status"`
  468. Age int64 `gorm:"column:age" json:"age"`
  469. IsOpenRemind int64 `gorm:"column:is_open_remind" json:"is_open_remind"`
  470. CreatedTime int64 `gorm:"column:created_time" json:"created_time" form:"created_time"`
  471. DialysisAge int64 `gorm:"column:dialysis_age" json:"dialysis_age" form:"dialysis_age"`
  472. ExpenseKind int64 `gorm:"column:expense_kind" json:"expense_kind" form:"expense_kind"`
  473. TellPhone string `gorm:"column:tell_phone" json:"tell_phone" form:"tell_phone"`
  474. FirstTreatmentDate int64 `gorm:"column:first_treatment_date" json:"first_treatment_date" form:"first_treatment_date"`
  475. ContactName string `gorm:"column:contact_name" json:"contact_name" form:"contact_name"`
  476. UserSysBeforeCount int64 `gorm:"column:user_sys_before_count" json:"user_sys_before_count" form:"user_sys_before_count"`
  477. }
  478. func (MPatient) TableName() string {
  479. return "xt_patients"
  480. }
  481. // 接诊评估
  482. func MobileGetReceiverTreatmentAccessRecord(orgID int64, patientID int64, recordDate int64) (*models.ReceiveTreatmentAsses, error) {
  483. var record models.ReceiveTreatmentAsses
  484. redis := RedisClient()
  485. defer redis.Close()
  486. // cur_date := time.Now().Format("2006-01-02")
  487. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":receive_treatment_asses"
  488. receive_treatment_asses_str, _ := redis.Get(key).Result()
  489. if len(receive_treatment_asses_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  490. err = readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  491. if err != nil {
  492. if err == gorm.ErrRecordNotFound {
  493. return nil, nil
  494. } else {
  495. return nil, err
  496. }
  497. } else {
  498. if record.ID > 0 {
  499. //缓存数据
  500. receive_treatment_asses_str, err := json.Marshal(record)
  501. if err == nil {
  502. redis.Set(key, receive_treatment_asses_str, time.Second*60*60*18)
  503. }
  504. } else {
  505. redis.Set(key, " ", time.Second*60*60*18)
  506. }
  507. return &record, nil
  508. }
  509. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  510. json.Unmarshal([]byte(receive_treatment_asses_str), &record)
  511. return &record, nil
  512. }
  513. }
  514. // 透前评估
  515. func MobileGetPredialysisEvaluation(orgID int64, patientID int64, recordDate int64) (*models.PredialysisEvaluation, error) {
  516. var record models.PredialysisEvaluation
  517. redis := RedisClient()
  518. defer redis.Close()
  519. // cur_date := time.Now().Format("2006-01-02")
  520. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":assessment_before_dislysis"
  521. assessment_before_dislysis_str, _ := redis.Get(key).Result()
  522. if len(assessment_before_dislysis_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  523. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  524. if err != nil {
  525. if err == gorm.ErrRecordNotFound {
  526. return nil, nil
  527. } else {
  528. return nil, err
  529. }
  530. } else {
  531. if record.ID > 0 {
  532. //缓存数据
  533. assessment_before_dislysis_str, err := json.Marshal(record)
  534. if err == nil {
  535. redis.Set(key, assessment_before_dislysis_str, time.Second*60*60*18)
  536. }
  537. } else {
  538. redis.Set(key, " ", time.Second*60*60*18)
  539. }
  540. return &record, nil
  541. }
  542. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  543. json.Unmarshal([]byte(assessment_before_dislysis_str), &record)
  544. return &record, nil
  545. }
  546. }
  547. // 获取 maxDate 之前一次的透前评估记录
  548. func MobileGetLastTimePredialysisEvaluation(orgID int64, patientID int64, maxDate int64) (*models.PredialysisEvaluation, error) {
  549. var record models.PredialysisEvaluation
  550. redis := RedisClient()
  551. defer redis.Close()
  552. // cur_date := time.Now().Format("2006-01-02")
  553. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(maxDate, 10) + ":assessment_before_dislysis_last"
  554. assessment_before_dislysis_last_str, _ := redis.Get(key).Result()
  555. if len(assessment_before_dislysis_last_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  556. 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
  557. if err != nil {
  558. if err == gorm.ErrRecordNotFound {
  559. return nil, nil
  560. } else {
  561. return nil, err
  562. }
  563. } else {
  564. if record.ID > 0 {
  565. //缓存数据
  566. assessment_before_dislysis_last_str, err := json.Marshal(record)
  567. if err == nil {
  568. redis.Set(key, assessment_before_dislysis_last_str, time.Second*60*60*4)
  569. }
  570. } else {
  571. redis.Set(key, " ", time.Second*60*60*18)
  572. }
  573. return &record, nil
  574. }
  575. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  576. json.Unmarshal([]byte(assessment_before_dislysis_last_str), &record)
  577. return &record, nil
  578. }
  579. }
  580. // 临时医嘱
  581. func MobileGetDoctorAdvices(orgID int64, patientID int64, recordDate int64) ([]*models.DoctorAdvice, error) {
  582. var records []*models.DoctorAdvice
  583. // err := readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).Find(&records).Error
  584. err := readDb.
  585. Model(&models.DoctorAdvice{}).
  586. Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ? and (advice_type = 2 or advice_type = 3)", patientID, orgID, recordDate).
  587. 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").
  588. Order("start_time asc, groupno desc, advice_order desc, id").
  589. Scan(&records).Error
  590. if err != nil {
  591. return nil, err
  592. }
  593. return records, nil
  594. }
  595. func MobileGetDoctorAdvicesByGroups(orgID int64, patientID int64, recordDate int64) ([]*models.DoctorAdvice, error) {
  596. var records []*models.DoctorAdvice
  597. redis := RedisClient()
  598. defer redis.Close()
  599. // cur_date := time.Now().Format("2006-01-02")
  600. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":doctor_advices"
  601. doctor_advices_str, _ := redis.Get(key).Result()
  602. if len(doctor_advices_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  603. err := readDb.
  604. Model(&models.DoctorAdvice{}).
  605. Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ? and (advice_type = 2 || advice_type = 3)", patientID, orgID, recordDate).
  606. 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").
  607. Order("start_time asc, groupno desc, advice_order desc, id asc").
  608. Scan(&records).Error
  609. if err != nil {
  610. if err == gorm.ErrRecordNotFound {
  611. return nil, nil
  612. } else {
  613. return nil, err
  614. }
  615. } else {
  616. if len(records) > 0 {
  617. //缓存数据
  618. doctor_advices_str, err := json.Marshal(records)
  619. if err == nil {
  620. redis.Set(key, doctor_advices_str, time.Second*60*60*18)
  621. }
  622. } else {
  623. redis.Set(key, " ", time.Second*60*60*18)
  624. }
  625. return records, nil
  626. }
  627. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  628. json.Unmarshal([]byte(doctor_advices_str), &records)
  629. return records, nil
  630. }
  631. }
  632. // 透析记录
  633. func MobileGetSchedualDialysisRecord(orgID int64, patientID int64, recordDate int64) (*models.DialysisOrder, error) {
  634. var record models.DialysisOrder
  635. redis := RedisClient()
  636. defer redis.Close()
  637. // cur_date := time.Now().Format("2006-01-02")
  638. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":dialysis_order"
  639. dialysis_order_str, _ := redis.Get(key).Result()
  640. if len(dialysis_order_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  641. 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
  642. if err != nil {
  643. if err == gorm.ErrRecordNotFound {
  644. return nil, nil
  645. } else {
  646. return nil, err
  647. }
  648. } else {
  649. if record.ID > 0 {
  650. //缓存数据
  651. dialysis_order_str, err := json.Marshal(record)
  652. if err == nil {
  653. redis.Set(key, dialysis_order_str, time.Second*60*60*18)
  654. }
  655. } else {
  656. redis.Set(key, " ", time.Second*60*60*18)
  657. }
  658. return &record, nil
  659. }
  660. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  661. json.Unmarshal([]byte(dialysis_order_str), &record)
  662. return &record, nil
  663. }
  664. }
  665. // 双人核对
  666. func MobileGetDoubleCheck(orgID int64, patientID int64, recordDate int64) (*models.DoubleCheck, error) {
  667. var record models.DoubleCheck
  668. redis := RedisClient()
  669. defer redis.Close()
  670. // cur_date := time.Now().Format("2006-01-02")
  671. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":double_check"
  672. double_check_str, _ := redis.Get(key).Result()
  673. if len(double_check_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  674. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? and user_org_id = ? and status = 1 and check_date = ?", patientID, orgID, recordDate).First(&record).Error
  675. if err != nil {
  676. if err == gorm.ErrRecordNotFound {
  677. return nil, nil
  678. } else {
  679. return nil, err
  680. }
  681. } else {
  682. if record.ID > 0 {
  683. //缓存数据
  684. double_check_str, err := json.Marshal(record)
  685. if err == nil {
  686. redis.Set(key, double_check_str, time.Second*60*60*18)
  687. }
  688. } else {
  689. redis.Set(key, " ", time.Second*60*60*18)
  690. }
  691. return &record, nil
  692. }
  693. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  694. json.Unmarshal([]byte(double_check_str), &record)
  695. return &record, nil
  696. }
  697. }
  698. // 透析监测记录
  699. func MobileGetMonitorRecords(orgID int64, patientID int64, recordDate int64) ([]*models.MonitoringRecord, error) {
  700. var records []*models.MonitoringRecord
  701. redis := RedisClient()
  702. defer redis.Close()
  703. // cur_date := time.Now().Format("2006-01-02")
  704. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":monitor_records"
  705. monitor_records_str, _ := redis.Get(key).Result()
  706. if len(monitor_records_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  707. 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
  708. if err != nil {
  709. if err == gorm.ErrRecordNotFound {
  710. return nil, nil
  711. } else {
  712. return nil, err
  713. }
  714. } else {
  715. if len(records) > 0 {
  716. //缓存数据
  717. monitor_records_str, err := json.Marshal(records)
  718. if err == nil {
  719. redis.Set(key, monitor_records_str, time.Second*60*60*18)
  720. }
  721. } else {
  722. redis.Set(key, " ", time.Second*60*60*18)
  723. }
  724. return records, nil
  725. }
  726. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  727. json.Unmarshal([]byte(monitor_records_str), &records)
  728. return records, nil
  729. }
  730. }
  731. func MobileGetMonitorRecordFirst(orgID int64, patientID int64, recordDate int64) (*models.MonitoringRecord, error) {
  732. var records models.MonitoringRecord
  733. 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
  734. if err != nil {
  735. return nil, err
  736. }
  737. return &records, nil
  738. }
  739. func MobileGetLastMonitorRecord(orgID int64, patientID int64, beforeDate int64) (*models.MonitoringRecord, error) {
  740. var record models.MonitoringRecord
  741. redis := RedisClient()
  742. defer redis.Close()
  743. // cur_date := time.Now().Format("2006-01-02")
  744. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(beforeDate, 10) + ":monitor_record_last"
  745. monitor_record_last_str, _ := redis.Get(key).Result()
  746. if len(monitor_record_last_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  747. 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
  748. if err != nil {
  749. if err == gorm.ErrRecordNotFound {
  750. return nil, nil
  751. } else {
  752. return nil, err
  753. }
  754. } else {
  755. if record.ID > 0 {
  756. //缓存数据
  757. monitor_record_last_str, err := json.Marshal(record)
  758. if err == nil {
  759. redis.Set(key, monitor_record_last_str, time.Second*60*60*18)
  760. }
  761. } else {
  762. redis.Set(key, " ", time.Second*60*60*18)
  763. }
  764. return &record, nil
  765. }
  766. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  767. json.Unmarshal([]byte(monitor_record_last_str), &record)
  768. return &record, nil
  769. }
  770. }
  771. // 透后评估
  772. func MobileGetAssessmentAfterDislysis(orgID int64, patientID int64, recordDate int64) (*models.AssessmentAfterDislysis, error) {
  773. var record models.AssessmentAfterDislysis
  774. redis := RedisClient()
  775. defer redis.Close()
  776. // cur_date := time.Now().Format("2006-01-02")
  777. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":assessment_after_dislysis"
  778. assessment_after_dislysis_str, _ := redis.Get(key).Result()
  779. if len(assessment_after_dislysis_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  780. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  781. if err != nil {
  782. if err == gorm.ErrRecordNotFound {
  783. return nil, nil
  784. } else {
  785. return nil, err
  786. }
  787. } else {
  788. if record.ID > 0 {
  789. //缓存数据
  790. assessment_after_dislysis_str, err := json.Marshal(record)
  791. if err == nil {
  792. redis.Set(key, assessment_after_dislysis_str, time.Second*60*60*18)
  793. }
  794. } else {
  795. redis.Set(key, " ", time.Second*60*60*18)
  796. }
  797. return &record, nil
  798. }
  799. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  800. json.Unmarshal([]byte(assessment_after_dislysis_str), &record)
  801. return &record, nil
  802. }
  803. }
  804. // 获取 maxDate 之前一次的透后评估记录
  805. func MobileGetLastTimeAssessmentAfterDislysis(orgID int64, patientID int64, maxDate int64) (*models.AssessmentAfterDislysis, error) {
  806. var record models.AssessmentAfterDislysis
  807. redis := RedisClient()
  808. defer redis.Close()
  809. // cur_date := time.Now().Format("2006-01-02")
  810. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(maxDate, 10) + ":assessment_after_dislysis_last"
  811. assessment_after_dislysis_last_str, _ := redis.Get(key).Result()
  812. if len(assessment_after_dislysis_last_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  813. 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
  814. if err != nil {
  815. if err == gorm.ErrRecordNotFound {
  816. return nil, nil
  817. } else {
  818. return nil, err
  819. }
  820. } else {
  821. if record.ID > 0 {
  822. //缓存数据
  823. assessment_after_dislysis_last_str, err := json.Marshal(record)
  824. if err == nil {
  825. redis.Set(key, assessment_after_dislysis_last_str, time.Second*60*60*18)
  826. }
  827. } else {
  828. redis.Set(key, " ", time.Second*60*60*18)
  829. }
  830. return &record, nil
  831. }
  832. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  833. json.Unmarshal([]byte(assessment_after_dislysis_last_str), &record)
  834. return &record, nil
  835. }
  836. }
  837. func MobileGetLast(orgID int64, patientID int64, maxDate int64) (models.AssessmentAfterDislysis, error) {
  838. dislysis := models.AssessmentAfterDislysis{}
  839. 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
  840. return dislysis, err
  841. }
  842. // 治疗小结
  843. func MobileGetTreatmentSummary(orgID int64, patientID int64, recordDate int64) (*models.TreatmentSummary, error) {
  844. var record models.TreatmentSummary
  845. redis := RedisClient()
  846. defer redis.Close()
  847. // cur_date := time.Now().Format("2006-01-02")
  848. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":treatment_summary"
  849. treatment_summary_str, _ := redis.Get(key).Result()
  850. if len(treatment_summary_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  851. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? and user_org_id = ? and status = 1 and assessment_date = ?", patientID, orgID, recordDate).First(&record).Error
  852. if err != nil {
  853. if err == gorm.ErrRecordNotFound {
  854. return nil, nil
  855. } else {
  856. return nil, err
  857. }
  858. } else {
  859. if record.ID > 0 {
  860. //缓存数据
  861. treatment_summary_str, err := json.Marshal(record)
  862. if err == nil {
  863. redis.Set(key, treatment_summary_str, time.Second*60*60*18)
  864. }
  865. } else {
  866. redis.Set(key, " ", time.Second*60*60*18)
  867. }
  868. return &record, nil
  869. }
  870. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  871. json.Unmarshal([]byte(treatment_summary_str), &record)
  872. return &record, nil
  873. }
  874. }
  875. // 透析处方
  876. func MobileGetDialysisPrescribe(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  877. var record models.DialysisPrescription
  878. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  879. if err != nil {
  880. if err == gorm.ErrRecordNotFound {
  881. return nil, nil
  882. } else {
  883. return nil, err
  884. }
  885. }
  886. return &record, nil
  887. }
  888. func MobileGetDialysisSchedual(orgID int64, patientID int64, scheduleDate int64) (*models.XtSchedule, error) {
  889. var schedule models.XtSchedule
  890. err := readDb.Model(&models.XtSchedule{}).Where("patient_id = ? and user_org_id = ? and status = 1 and schedule_date = ?", patientID, orgID, scheduleDate).First(&schedule).Error
  891. if err != nil {
  892. if err == gorm.ErrRecordNotFound {
  893. return nil, nil
  894. } else {
  895. return nil, err
  896. }
  897. }
  898. return &schedule, nil
  899. }
  900. // 透析方案
  901. func MobileGetDialysisSolution(orgID int64, patientID int64) (*models.DialysisSolution, error) {
  902. var record models.DialysisSolution
  903. err := readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  904. if err != nil {
  905. if err == gorm.ErrRecordNotFound {
  906. return nil, nil
  907. } else {
  908. return nil, err
  909. }
  910. }
  911. return &record, nil
  912. }
  913. func MobileGetPatientById(orgID int64, patientID int64) (*models.Patients, error) {
  914. var patient models.Patients
  915. err := readDb.Model(&models.Patients{}).Where("id = ? and user_org_id = ? and status = 1", patientID, orgID).First(&patient).Error
  916. if err != nil {
  917. if err == gorm.ErrRecordNotFound {
  918. return nil, nil
  919. } else {
  920. return nil, err
  921. }
  922. }
  923. return &patient, nil
  924. }
  925. func DisableMonitor(orgID int64, patientID int64, recordID int64, admin_user_id int64) error {
  926. fmt.Println()
  927. tx := writeDb.Begin()
  928. updateTime := time.Now().Unix()
  929. 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
  930. if err != nil {
  931. tx.Rollback()
  932. return err
  933. }
  934. tx.Commit()
  935. return nil
  936. }
  937. func GetMonitor(orgID int64, patientID int64, id int64) (*models.MonitoringRecord, error) {
  938. var monitor models.MonitoringRecord
  939. var err error
  940. err = readDb.Model(&models.MonitoringRecord{}).Where("id = ? AND user_org_id = ? AND patient_id = ? AND status = 1 ", id, orgID, patientID).Find(&monitor).Error
  941. if err == gorm.ErrRecordNotFound {
  942. return nil, nil
  943. }
  944. if err != nil {
  945. return nil, err
  946. }
  947. return &monitor, nil
  948. }
  949. type MScheduleDoctorAdviceVM struct {
  950. ID int64 `gorm:"column:id" json:"id"`
  951. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  952. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  953. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  954. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  955. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  956. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  957. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  958. Status int64 `gorm:"column:status" json:"status"`
  959. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  960. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  961. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  962. DoctorAdvices []*MDoctorAdviceVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  963. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  964. DialysisAssesmentBefor *models.DialysisAssesmentBefor `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysisassesmentbefor"`
  965. }
  966. func (MScheduleDoctorAdviceVM) TableName() string {
  967. return "xt_schedule"
  968. }
  969. type MScheduleDoctorAdviceVMOne struct {
  970. ID int64 `gorm:"column:id" json:"id"`
  971. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  972. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  973. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  974. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  975. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  976. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  977. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  978. Status int64 `gorm:"column:status" json:"status"`
  979. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  980. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  981. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  982. DoctorAdvices []*MDoctorAdviceVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  983. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  984. //DialysisAssesmentBefor *models.DialysisAssesmentBefor `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysisassesmentbefor"`
  985. }
  986. func (MScheduleDoctorAdviceVMOne) TableName() string {
  987. return "xt_schedule"
  988. }
  989. type MDoctorAdviceVM struct {
  990. ID int64 `gorm:"column:id" json:"id"`
  991. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  992. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  993. AdviceType int64 `gorm:"column:advice_type" json:"advice_type"`
  994. AdviceDate int64 `gorm:"column:advice_date" json:"advice_date"`
  995. StartTime int64 `gorm:"column:start_time" json:"start_time"`
  996. AdviceName string `gorm:"column:advice_name" json:"advice_name"`
  997. AdviceDesc string `gorm:"column:advice_desc" json:"advice_desc"`
  998. ReminderDate int64 `gorm:"column:reminder_date" json:"reminder_date"`
  999. SingleDose float64 `gorm:"column:single_dose" json:"single_dose"`
  1000. SingleDoseUnit string `gorm:"column:single_dose_unit" json:"single_dose_unit"`
  1001. PrescribingNumber float64 `gorm:"column:prescribing_number" json:"prescribing_number"`
  1002. PrescribingNumberUnit string `gorm:"column:prescribing_number_unit" json:"prescribing_number_unit"`
  1003. DeliveryWay string `gorm:"column:delivery_way" json:"delivery_way"`
  1004. ExecutionFrequency string `gorm:"column:execution_frequency" json:"execution_frequency"`
  1005. AdviceDoctor int64 `gorm:"column:advice_doctor" json:"advice_doctor"`
  1006. Status int64 `gorm:"column:status" json:"status"`
  1007. CreatedTime int64 `gorm:"column:created_time" json:"created_time"`
  1008. UpdatedTime int64 `gorm:"column:updated_time" json:"updated_time"`
  1009. AdviceAffirm string `gorm:"column:advice_affirm" json:"advice_affirm"`
  1010. Remark string `gorm:"column:remark" json:"remark"`
  1011. StopTime int64 `gorm:"column:stop_time" json:"stop_time"`
  1012. StopReason string `gorm:"column:stop_reason" json:"stop_reason"`
  1013. StopDoctor int64 `gorm:"column:stop_doctor" json:"stop_doctor"`
  1014. StopState int64 `gorm:"column:stop_state" json:"stop_state"`
  1015. ParentId int64 `gorm:"column:parent_id" json:"parent_id"`
  1016. ExecutionTime int64 `gorm:"column:execution_time" json:"execution_time"`
  1017. ExecutionStaff int64 `gorm:"column:execution_staff" json:"execution_staff"`
  1018. ExecutionState int64 `gorm:"column:execution_state" json:"execution_state"`
  1019. Checker int64 `gorm:"column:checker" json:"checker"`
  1020. RecordDate int64 `gorm:"column:record_date" json:"record_date"`
  1021. DialysisOrderId int64 `gorm:"column:dialysis_order_id" json:"dialysis_order_id"`
  1022. CheckTime int64 `gorm:"column:check_time" json:"check_time"`
  1023. CheckState int64 `gorm:"column:check_state" json:"check_state"`
  1024. DrugSpec float64 `gorm:"column:drug_spec" json:"drug_spec"`
  1025. DrugSpecUnit string `gorm:"column:drug_spec_unit" json:"drug_spec_unit"`
  1026. Groupno int64 `gorm:"column:groupno" json:"groupno"`
  1027. RemindType int64 `gorm:"column:remind_type" json:"remind_type"`
  1028. FrequencyType int64 `gorm:"column:frequency_type" json:"frequency_type"`
  1029. DayCount int64 `gorm:"column:day_count" json:"day_count"`
  1030. WeekDay string `gorm:"column:week_day" json:"week_day"`
  1031. TemplateId string `gorm:"column:template_id" json:"template_id"`
  1032. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  1033. }
  1034. func (MDoctorAdviceVM) TableName() string {
  1035. return "xt_doctor_advice"
  1036. }
  1037. func MobileGetScheduleDoctorAdvices(orgID int64, scheduleDate int64, adviceType int, patientType int, adminUserId int64, deliverWay string) ([]*MScheduleDoctorAdviceVM, error) {
  1038. var vms []*MScheduleDoctorAdviceVM
  1039. adviceWhere := ""
  1040. adviceCondition := []interface{}{}
  1041. if adviceType == 0 {
  1042. if patientType == 0 {
  1043. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1)"
  1044. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1045. } else if patientType == 1 {
  1046. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1)"
  1047. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1048. } else if patientType == 2 {
  1049. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND execution_staff = 0 AND(advice_type = 3 OR advice_type = 1)"
  1050. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1051. }
  1052. } else if adviceType == 1 {
  1053. if patientType == 0 {
  1054. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? "
  1055. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1056. } else if patientType == 1 {
  1057. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? "
  1058. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1059. } else if patientType == 2 {
  1060. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0"
  1061. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1062. }
  1063. } else if adviceType == 3 {
  1064. if patientType == 0 {
  1065. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? "
  1066. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1067. } else if patientType == 1 {
  1068. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? "
  1069. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1070. } else if patientType == 2 {
  1071. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0"
  1072. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1073. }
  1074. } else if adviceType == 2 && len(deliverWay) > 0 {
  1075. if patientType == 0 {
  1076. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ?"
  1077. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  1078. } else if patientType == 1 {
  1079. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and delivery_way = ? "
  1080. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay)
  1081. } else if patientType == 2 {
  1082. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and delivery_way = ?"
  1083. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  1084. }
  1085. } else if adviceType == 2 && len(deliverWay) <= 0 {
  1086. if patientType == 0 {
  1087. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ?"
  1088. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1089. } else if patientType == 1 {
  1090. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND (advice_doctor = ? or execution_staff = ?)"
  1091. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, adminUserId)
  1092. } else if patientType == 2 {
  1093. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0"
  1094. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1095. }
  1096. }
  1097. db := readDb.
  1098. Table("xt_schedule").
  1099. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1100. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1101. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1102. }).
  1103. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1104. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1105. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1106. Preload("DialysisAssesmentBefor", "status =1 AND user_org_id = ? and assessment_date =?", orgID, scheduleDate).
  1107. Preload("DoctorAdvices", adviceCondition...).
  1108. Where("status = 1 AND user_org_id = ?", orgID)
  1109. if scheduleDate != 0 {
  1110. db = db.Where("schedule_date = ?", scheduleDate)
  1111. }
  1112. err := db.Find(&vms).Error
  1113. return vms, err
  1114. }
  1115. func MobileGetScheduleDoctorAdvicesTwo(orgID int64, scheduleDate int64, adviceType int, patientType int, adminUserId int64, deliverWay string) ([]*MScheduleDoctorAdviceVMOne, error) {
  1116. var vms []*MScheduleDoctorAdviceVMOne
  1117. adviceWhere := ""
  1118. adviceCondition := []interface{}{}
  1119. if adviceType == 0 {
  1120. if patientType == 0 {
  1121. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1)"
  1122. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1123. } else if patientType == 1 {
  1124. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1)"
  1125. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1126. } else if patientType == 2 {
  1127. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND execution_staff = 0 AND(advice_type = 3 OR advice_type = 1)"
  1128. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1129. }
  1130. } else if adviceType == 1 {
  1131. if patientType == 0 {
  1132. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? "
  1133. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1134. } else if patientType == 1 {
  1135. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? "
  1136. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1137. } else if patientType == 2 {
  1138. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0"
  1139. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1140. }
  1141. } else if adviceType == 3 {
  1142. if patientType == 0 {
  1143. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? "
  1144. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1145. } else if patientType == 1 {
  1146. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? "
  1147. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  1148. } else if patientType == 2 {
  1149. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0"
  1150. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1151. }
  1152. } else if adviceType == 2 && len(deliverWay) > 0 {
  1153. if patientType == 0 {
  1154. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ?"
  1155. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  1156. } else if patientType == 1 {
  1157. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and delivery_way = ? "
  1158. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay)
  1159. } else if patientType == 2 {
  1160. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and delivery_way = ?"
  1161. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  1162. }
  1163. } else if adviceType == 2 && len(deliverWay) <= 0 {
  1164. if patientType == 0 {
  1165. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ?"
  1166. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1167. } else if patientType == 1 {
  1168. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND (advice_doctor = ? or execution_staff = ?)"
  1169. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, adminUserId)
  1170. } else if patientType == 2 {
  1171. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0"
  1172. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  1173. }
  1174. }
  1175. db := readDb.
  1176. Table("xt_schedule").
  1177. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1178. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1179. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1180. }).
  1181. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1182. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1183. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1184. //Preload("DialysisAssesmentBefor", "status =1 AND user_org_id = ? and assessment_date =?", orgID, scheduleDate).
  1185. Preload("DoctorAdvices", adviceCondition...).
  1186. Where("status = 1 AND user_org_id = ?", orgID)
  1187. if scheduleDate != 0 {
  1188. db = db.Where("schedule_date = ?", scheduleDate)
  1189. }
  1190. err := db.Find(&vms).Error
  1191. return vms, err
  1192. }
  1193. func GetHisDoctorAdvices(orgID int64, scheduleDate int64, deliverWay string, patientType int, adminUserId int64) ([]*HisMScheduleDoctorAdviceVM, error) {
  1194. var vms []*HisMScheduleDoctorAdviceVM
  1195. if len(deliverWay) > 0 {
  1196. if patientType == 0 {
  1197. db := readDb.
  1198. Table("xt_schedule").
  1199. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ?", orgID, scheduleDate, deliverWay).
  1200. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1201. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1202. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1203. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1204. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1205. }).
  1206. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1207. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1208. Where("status = 1 AND user_org_id = ?", orgID)
  1209. if scheduleDate != 0 {
  1210. db = db.Where("schedule_date = ?", scheduleDate)
  1211. }
  1212. err = db.Find(&vms).Error
  1213. }
  1214. if patientType > 0 {
  1215. db := readDb.
  1216. Table("xt_schedule").
  1217. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1218. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1219. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, deliverWay, adminUserId, adminUserId).
  1220. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1221. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1222. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1223. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1224. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1225. }).Where("status = 1 AND user_org_id = ?", orgID)
  1226. if scheduleDate != 0 {
  1227. db = db.Where("schedule_date = ?", scheduleDate)
  1228. }
  1229. err = db.Find(&vms).Error
  1230. }
  1231. } else {
  1232. if patientType == 0 {
  1233. db := readDb.
  1234. Table("xt_schedule").
  1235. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  1236. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1237. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1238. return db.Where("status = 1 AND user_org_id = ? and dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1239. }).
  1240. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1241. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1242. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1243. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1244. Where("status = 1 AND user_org_id = ?", orgID)
  1245. if scheduleDate != 0 {
  1246. db = db.Where("schedule_date = ?", scheduleDate)
  1247. }
  1248. err = db.Find(&vms).Error
  1249. }
  1250. if patientType > 0 {
  1251. db := readDb.
  1252. Table("xt_schedule").
  1253. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1254. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  1255. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1256. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1257. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1258. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1259. }).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1260. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1261. Where("status = 1 AND user_org_id = ?", orgID)
  1262. if scheduleDate != 0 {
  1263. db = db.Where("schedule_date = ?", scheduleDate)
  1264. }
  1265. err = db.Find(&vms).Error
  1266. }
  1267. }
  1268. return vms, err
  1269. }
  1270. func GetHisDoctorAdvicesTwo(orgID int64, scheduleDate int64, deliverWay string, patientType int, adminUserId int64) ([]*HisMScheduleDoctorAdviceVMOne, error) {
  1271. var vms []*HisMScheduleDoctorAdviceVMOne
  1272. if len(deliverWay) > 0 {
  1273. if patientType == 0 {
  1274. db := readDb.
  1275. Table("xt_schedule").
  1276. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ?", orgID, scheduleDate, deliverWay).
  1277. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1278. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1279. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1280. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1281. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1282. }).
  1283. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1284. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1285. Where("status = 1 AND user_org_id = ?", orgID)
  1286. if scheduleDate != 0 {
  1287. db = db.Where("schedule_date = ?", scheduleDate)
  1288. }
  1289. err = db.Find(&vms).Error
  1290. }
  1291. if patientType > 0 {
  1292. db := readDb.
  1293. Table("xt_schedule").
  1294. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1295. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1296. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, deliverWay, adminUserId, adminUserId).
  1297. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1298. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1299. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1300. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1301. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1302. }).Where("status = 1 AND user_org_id = ?", orgID)
  1303. if scheduleDate != 0 {
  1304. db = db.Where("schedule_date = ?", scheduleDate)
  1305. }
  1306. err = db.Find(&vms).Error
  1307. }
  1308. } else {
  1309. if patientType == 0 {
  1310. db := readDb.
  1311. Table("xt_schedule").
  1312. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  1313. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1314. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1315. return db.Where("status = 1 AND user_org_id = ? and dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1316. }).
  1317. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1318. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1319. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1320. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1321. Where("status = 1 AND user_org_id = ?", orgID)
  1322. if scheduleDate != 0 {
  1323. db = db.Where("schedule_date = ?", scheduleDate)
  1324. }
  1325. err = db.Find(&vms).Error
  1326. }
  1327. if patientType > 0 {
  1328. db := readDb.
  1329. Table("xt_schedule").
  1330. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1331. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  1332. Preload("HisPrescriptionProject", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1333. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1334. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1335. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1336. }).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1337. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1338. Where("status = 1 AND user_org_id = ?", orgID)
  1339. if scheduleDate != 0 {
  1340. db = db.Where("schedule_date = ?", scheduleDate)
  1341. }
  1342. err = db.Find(&vms).Error
  1343. }
  1344. }
  1345. return vms, err
  1346. }
  1347. func GetMobileHisPrescriptionProject(orgID int64, scheduleDate int64, deliverWay string, patientType int, adminUserId int64) ([]*HisMScheduleProjectVM, error) {
  1348. var vms []*HisMScheduleProjectVM
  1349. if patientType == 0 {
  1350. db := readDb.
  1351. Table("xt_schedule").
  1352. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1353. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1354. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1355. }).
  1356. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1357. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1358. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1359. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  1360. return db.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  1361. }).
  1362. Where("status = 1 AND user_org_id = ?", orgID)
  1363. if scheduleDate != 0 {
  1364. db = db.Where("schedule_date = ?", scheduleDate)
  1365. }
  1366. err = db.Find(&vms).Error
  1367. }
  1368. if patientType > 0 {
  1369. db := readDb.
  1370. Table("xt_schedule").
  1371. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  1372. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  1373. return db.Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  1374. }).
  1375. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1376. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  1377. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  1378. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  1379. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  1380. return db.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  1381. }).
  1382. Where("status = 1 AND user_org_id = ?", orgID)
  1383. if scheduleDate != 0 {
  1384. db = db.Where("schedule_date = ?", scheduleDate)
  1385. }
  1386. err = db.Find(&vms).Error
  1387. }
  1388. return vms, err
  1389. }
  1390. func MobileCreateDialysisOrder(orgID int64, patientID int64, order *models.DialysisOrder) error {
  1391. now := time.Now()
  1392. tx := writeDb.Begin()
  1393. if createOrderErr := tx.Model(&models.DialysisOrder{}).Create(order).Error; createOrderErr != nil {
  1394. tx.Rollback()
  1395. return createOrderErr
  1396. }
  1397. // 更新透析记录 ID
  1398. // 透析处方
  1399. 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 {
  1400. tx.Rollback()
  1401. return err
  1402. }
  1403. // 接诊评估
  1404. 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, "update_time": now.Unix()}).Error; err != nil {
  1405. tx.Rollback()
  1406. return err
  1407. }
  1408. // 透前评估
  1409. 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 {
  1410. tx.Rollback()
  1411. return err
  1412. }
  1413. // 临时医嘱
  1414. 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 {
  1415. tx.Rollback()
  1416. return err
  1417. }
  1418. // 双人核对
  1419. 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 {
  1420. tx.Rollback()
  1421. return err
  1422. }
  1423. // 透后评估
  1424. 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 {
  1425. tx.Rollback()
  1426. return err
  1427. }
  1428. // 治疗小结
  1429. 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 {
  1430. tx.Rollback()
  1431. return err
  1432. }
  1433. // 透析监测
  1434. 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 {
  1435. tx.Rollback()
  1436. return err
  1437. }
  1438. tx.Commit()
  1439. return nil
  1440. }
  1441. type MobileUrgentSchedulePatientVM struct {
  1442. ID int64 `gorm:"column:id" json:"id"`
  1443. Name string `gorm:"column:name" json:"name"`
  1444. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no"`
  1445. }
  1446. func (MobileUrgentSchedulePatientVM) TableName() string {
  1447. return "xt_patients"
  1448. }
  1449. type MobileUrgentScheduleTreatmentModeVM struct {
  1450. ID int64 `gorm:"column:id" json:"id"`
  1451. Name string `gorm:"column:name" json:"name"`
  1452. }
  1453. func (MobileUrgentScheduleTreatmentModeVM) TableName() string {
  1454. return "xt_treatment_mode"
  1455. }
  1456. func MobileGetAllPatientsForUrgentSchedule(orgID int64, record_date int64) ([]*MobileUrgentSchedulePatientVM, error) {
  1457. var vms []*MobileUrgentSchedulePatientVM = make([]*MobileUrgentSchedulePatientVM, 0)
  1458. 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()
  1459. defer rows.Close()
  1460. if err != nil {
  1461. return nil, err
  1462. }
  1463. for rows.Next() {
  1464. var vm MobileUrgentSchedulePatientVM
  1465. readDb.ScanRows(rows, &vm)
  1466. vms = append(vms, &vm)
  1467. }
  1468. return vms, nil
  1469. }
  1470. func MobileGetAllTrearmentModesForUrgentSchedule() ([]*MobileUrgentScheduleTreatmentModeVM, error) {
  1471. var modes []*MobileUrgentScheduleTreatmentModeVM
  1472. err := readDb.Model(&MobileUrgentScheduleTreatmentModeVM{}).Where("status = 1").Find(&modes).Error
  1473. if err != nil {
  1474. return nil, err
  1475. }
  1476. return modes, nil
  1477. }
  1478. type MobileUrgentScheduleScheduleListVM struct {
  1479. ID int64 `gorm:"column:id" json:"id"`
  1480. // ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  1481. DeviceNumberID int64 `gorm:"column:bed_id" json:"bed_id"`
  1482. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  1483. ScheduleType int `gorm:"column:schedule_type" json:"schedule_type"`
  1484. DeviceNumber *models.DeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  1485. // DeviceZone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"device_zone"`
  1486. }
  1487. func (MobileUrgentScheduleScheduleListVM) TableName() string {
  1488. return "xt_schedule"
  1489. }
  1490. func MobileGetSchedulesForUrgentSchedule(orgID int64, scheduleDate int64, schedule_type int) (schedules []*MobileUrgentScheduleScheduleListVM, err error) {
  1491. db := readDb.
  1492. Model(&MobileUrgentScheduleScheduleListVM{}).
  1493. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1494. Where("status = 1 AND user_org_id = ? AND schedule_date = ? AND schedule_type = ? ", orgID, scheduleDate, schedule_type)
  1495. err = db.Find(&schedules).Error
  1496. if err != nil {
  1497. return nil, err
  1498. }
  1499. return schedules, nil
  1500. }
  1501. func MobileGetOtherSchedulesForUrgentSchedule(orgID int64, scheduleDate int64, schedule_type int) ([]*MobileUrgentScheduleScheduleListVM, error) {
  1502. var vms []*MobileUrgentScheduleScheduleListVM = make([]*MobileUrgentScheduleScheduleListVM, 0)
  1503. 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()
  1504. defer rows.Close()
  1505. if err != nil {
  1506. return nil, err
  1507. }
  1508. for rows.Next() {
  1509. var vm MobileUrgentScheduleScheduleListVM
  1510. readDb.ScanRows(rows, &vm)
  1511. vms = append(vms, &vm)
  1512. }
  1513. return vms, nil
  1514. }
  1515. type MobileUrgentScheduleDeviceNumberVM struct {
  1516. ID int64 `gorm:"column:id" json:"id"`
  1517. Number string `gorm:"column:number" json:"number"`
  1518. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  1519. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  1520. }
  1521. func (MobileUrgentScheduleDeviceNumberVM) TableName() string {
  1522. return "xt_device_number"
  1523. }
  1524. func MobileGetAllDeviceNumbersForUrgentSchedule(orgID int64, record_date int64, schedule_type int) ([]*DeviceNumberViewModel, error) {
  1525. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  1526. 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()
  1527. defer rows.Close()
  1528. if err != nil {
  1529. return nil, err
  1530. }
  1531. for rows.Next() {
  1532. var vm DeviceNumberViewModel
  1533. readDb.ScanRows(rows, &vm)
  1534. vms = append(vms, &vm)
  1535. }
  1536. return vms, nil
  1537. //var deviceNumbers []*MobileUrgentScheduleDeviceNumberVM
  1538. //db := readDb.
  1539. // Model(&MobileUrgentScheduleDeviceNumberVM{}).
  1540. // Preload("Zone", "status = 1 AND org_id = ?", orgID).
  1541. // Where("status = 1 AND org_id = ?", orgID)
  1542. //err := db.Order("zone_id asc").Find(&deviceNumbers).Error
  1543. //if err != nil {
  1544. // return nil, err
  1545. //}
  1546. //return deviceNumbers, nil
  1547. }
  1548. func GetValidScheduleMonitorRecordCount() (int64, error) {
  1549. var total int64
  1550. err := readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND operate_time = 0 AND monitoring_date <> 0").Count(&total).Error
  1551. return total, err
  1552. }
  1553. func GetTop1000ValidScheduleMonitorRecord() ([]*models.MonitoringRecord, error) {
  1554. var monitors []*models.MonitoringRecord
  1555. err := readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND operate_time = 0 AND monitoring_date <> 0").Order("id asc").Limit(100).Find(&monitors).Error
  1556. if err != nil {
  1557. return nil, err
  1558. }
  1559. return monitors, nil
  1560. }
  1561. func BatchUpdateMonitors(monitors []*models.MonitoringRecord) error {
  1562. tx := writeDb.Begin()
  1563. for index := 0; index < len(monitors); index++ {
  1564. tx.Save(monitors[index])
  1565. }
  1566. return tx.Commit().Error
  1567. }
  1568. func ModifyStartDialysisOrder(order *models.DialysisOrder) error {
  1569. tx := writeDb.Begin()
  1570. updateTime := time.Now().Unix()
  1571. 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, "difficult_puncture_nurse": order.DifficultPunctureNurse, "new_fistula_nurse": order.NewFistulaNurse}).Error
  1572. if err != nil {
  1573. tx.Rollback()
  1574. return err
  1575. }
  1576. tx.Commit()
  1577. return err
  1578. }
  1579. func ModifyFinishDialysisOrder(order *models.DialysisOrder) error {
  1580. tx := writeDb.Begin()
  1581. updateTime := time.Now().Unix()
  1582. 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
  1583. if err != nil {
  1584. tx.Rollback()
  1585. return err
  1586. }
  1587. tx.Commit()
  1588. return err
  1589. }
  1590. func MobileGetLastDryWeight(orgID int64, patientID int64) (*models.SgjPatientDryweight, error) {
  1591. var record models.SgjPatientDryweight
  1592. redis := RedisClient()
  1593. defer redis.Close()
  1594. // cur_date := time.Now().Format("2006-01-02")
  1595. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":last_dry_weight"
  1596. last_dry_weight_str, _ := redis.Get(key).Result()
  1597. if len(last_dry_weight_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1598. err := readDb.Model(&models.SgjPatientDryweight{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  1599. if err != nil {
  1600. if err == gorm.ErrRecordNotFound {
  1601. return nil, nil
  1602. } else {
  1603. return nil, err
  1604. }
  1605. } else {
  1606. if record.ID > 0 {
  1607. //缓存数据
  1608. last_dry_weight_str, err := json.Marshal(record)
  1609. if err == nil {
  1610. redis.Set(key, last_dry_weight_str, time.Second*60*60*18)
  1611. }
  1612. } else {
  1613. redis.Set(key, " ", time.Second*60*60*18)
  1614. }
  1615. return &record, nil
  1616. }
  1617. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1618. json.Unmarshal([]byte(last_dry_weight_str), &record)
  1619. return &record, nil
  1620. }
  1621. }
  1622. // 透析方案
  1623. func MobileGetDialysisSolutionByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  1624. var record models.DialysisSolution
  1625. redis := RedisClient()
  1626. defer redis.Close()
  1627. // cur_date := time.Now().Format("2006-01-02")
  1628. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(mode_id, 10) + ":dialysis_solution"
  1629. dialysis_solution_str, _ := redis.Get(key).Result()
  1630. if len(dialysis_solution_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1631. 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
  1632. if err != nil {
  1633. if err == gorm.ErrRecordNotFound {
  1634. return nil, nil
  1635. } else {
  1636. return nil, err
  1637. }
  1638. } else {
  1639. if record.ID > 0 {
  1640. //缓存数据
  1641. dialysis_solution_str, err := json.Marshal(record)
  1642. if err == nil {
  1643. redis.Set(key, dialysis_solution_str, time.Second*60*60*18)
  1644. }
  1645. } else {
  1646. redis.Set(key, " ", time.Second*60*60*18)
  1647. }
  1648. return &record, nil
  1649. }
  1650. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1651. json.Unmarshal([]byte(dialysis_solution_str), &record)
  1652. return &record, nil
  1653. }
  1654. }
  1655. // 透析处方
  1656. func MobileGetDialysisPrescribeByModeId(orgID int64, patientID int64, recordDate int64, mode_id int64) (*models.DialysisPrescription, error) {
  1657. var record models.DialysisPrescription
  1658. redis := RedisClient()
  1659. defer redis.Close()
  1660. // cur_date := time.Now().Format("2006-01-02")
  1661. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":" + strconv.FormatInt(mode_id, 10) + ":dialysis_prescribe"
  1662. dialysis_prescribe_str, _ := redis.Get(key).Result()
  1663. if len(dialysis_prescribe_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1664. 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
  1665. if err != nil {
  1666. if err == gorm.ErrRecordNotFound {
  1667. return nil, nil
  1668. } else {
  1669. return nil, err
  1670. }
  1671. } else {
  1672. if record.ID > 0 {
  1673. //缓存数据
  1674. dialysis_prescribe_str, err := json.Marshal(record)
  1675. if err == nil {
  1676. redis.Set(key, dialysis_prescribe_str, time.Second*60*60*18)
  1677. }
  1678. } else {
  1679. redis.Set(key, " ", time.Second*60*60*18)
  1680. }
  1681. return &record, nil
  1682. }
  1683. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1684. json.Unmarshal([]byte(dialysis_prescribe_str), &record)
  1685. return &record, nil
  1686. }
  1687. }
  1688. func MobileGetDialysisPrescribeByModeIdOne(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  1689. var record models.DialysisPrescription
  1690. redis := RedisClient()
  1691. defer redis.Close()
  1692. // cur_date := time.Now().Format("2006-01-02")
  1693. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":dialysis_prescribe_by_mode"
  1694. dialysis_prescribe_by_mode_str, _ := redis.Get(key).Result()
  1695. if len(dialysis_prescribe_by_mode_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1696. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  1697. if err != nil {
  1698. if err == gorm.ErrRecordNotFound {
  1699. return nil, nil
  1700. } else {
  1701. return nil, err
  1702. }
  1703. } else {
  1704. if record.ID > 0 {
  1705. //缓存数据
  1706. dialysis_prescribe_by_mode_str, err := json.Marshal(record)
  1707. if err == nil {
  1708. redis.Set(key, dialysis_prescribe_by_mode_str, time.Second*60*60*18)
  1709. }
  1710. } else {
  1711. redis.Set(key, " ", time.Second*60*60*18)
  1712. }
  1713. return &record, nil
  1714. }
  1715. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1716. json.Unmarshal([]byte(dialysis_prescribe_by_mode_str), &record)
  1717. return &record, nil
  1718. }
  1719. }
  1720. func MobileGetLastDialysisPrescribe(orgID int64, patientID int64) (*models.DialysisPrescription, error) {
  1721. var record models.DialysisPrescription
  1722. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 ", patientID, orgID).Last(&record).Error
  1723. if err != nil {
  1724. if err == gorm.ErrRecordNotFound {
  1725. return nil, nil
  1726. } else {
  1727. return nil, err
  1728. }
  1729. }
  1730. return &record, nil
  1731. }
  1732. func MobileGetLastDialysisPrescribeByModeId(orgID int64, patientID int64, recordDate int64,mode_id int64) (*models.DialysisPrescription, error) {
  1733. var record models.DialysisPrescription
  1734. redis := RedisClient()
  1735. defer redis.Close()
  1736. // cur_date := time.Now().Format("2006-01-02")
  1737. key := strconv.FormatInt(orgID, 10) + ":"+ strconv.FormatInt(patientID, 10) + ":" + strconv.FormatInt(recordDate, 10) + ":" + strconv.FormatInt(mode_id, 10) + ":dialysis_prescribe_last"
  1738. dialysis_prescribe_last_str, _ := redis.Get(key).Result()
  1739. if len(dialysis_prescribe_last_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1740. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 AND mode_id = ? AND record_date < ?", patientID, orgID, mode_id,recordDate).Last(&record).Error
  1741. if err != nil {
  1742. if err == gorm.ErrRecordNotFound {
  1743. return nil, nil
  1744. } else {
  1745. return nil, err
  1746. }
  1747. } else {
  1748. if record.ID > 0 {
  1749. //缓存数据
  1750. dialysis_prescribe_last_str, err := json.Marshal(record)
  1751. if err == nil {
  1752. redis.Set(key, dialysis_prescribe_last_str, time.Second*60*60*18)
  1753. }
  1754. } else {
  1755. redis.Set(key, " ", time.Second*60*60*18)
  1756. }
  1757. return &record, nil
  1758. }
  1759. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1760. json.Unmarshal([]byte(dialysis_prescribe_last_str), &record)
  1761. return &record, nil
  1762. }
  1763. }
  1764. func GetAllAvaildDeviceNumbers(orgID int64, record_date int64, schedule_type int) ([]*DeviceNumberViewModel, error) {
  1765. var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
  1766. 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()
  1767. defer rows.Close()
  1768. if err != nil {
  1769. return nil, err
  1770. }
  1771. for rows.Next() {
  1772. var vm DeviceNumberViewModel
  1773. readDb.ScanRows(rows, &vm)
  1774. vms = append(vms, &vm)
  1775. }
  1776. return vms, nil
  1777. }
  1778. // 获取 maxDate 之前一次的透前评估记录
  1779. func GetLastTimePredialysisEvaluation(orgID int64, patientID int64, maxDate int64) (*models.PredialysisEvaluation, error) {
  1780. var record models.PredialysisEvaluation
  1781. 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
  1782. if err != nil {
  1783. if err == gorm.ErrRecordNotFound {
  1784. return nil, nil
  1785. } else {
  1786. return nil, err
  1787. }
  1788. }
  1789. return &record, nil
  1790. }
  1791. func GetLastTimeOrder(orgID int64, patientID int64, maxDate int64) (*models.DialysisOrder, error) {
  1792. var record models.DialysisOrder
  1793. 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
  1794. if err != nil {
  1795. if err == gorm.ErrRecordNotFound {
  1796. return nil, nil
  1797. } else {
  1798. return nil, err
  1799. }
  1800. }
  1801. return &record, nil
  1802. }
  1803. func GetLastMonitorRecord(orgID int64, patientID int64, beforeDate int64) (*models.MonitoringRecord, error) {
  1804. var record models.MonitoringRecord
  1805. 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
  1806. if err != nil {
  1807. if err == gorm.ErrRecordNotFound {
  1808. return nil, nil
  1809. } else {
  1810. return nil, err
  1811. }
  1812. }
  1813. return &record, nil
  1814. }
  1815. // 获取 maxDate 之前一次的透后评估记录
  1816. func GetLastTimeAssessmentAfterDislysis(orgID int64, patientID int64, maxDate int64) (*models.AssessmentAfterDislysis, error) {
  1817. var record models.AssessmentAfterDislysis
  1818. 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
  1819. if err != nil {
  1820. if err == gorm.ErrRecordNotFound {
  1821. return nil, nil
  1822. } else {
  1823. return nil, err
  1824. }
  1825. }
  1826. return &record, nil
  1827. }
  1828. // 透析处方
  1829. func GetDialysisPrescribe(orgID int64, patientID int64, recordDate int64) (*models.DialysisPrescription, error) {
  1830. var record models.DialysisPrescription
  1831. err := readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id = ? and status = 1 and record_date = ?", patientID, orgID, recordDate).First(&record).Error
  1832. if err != nil {
  1833. if err == gorm.ErrRecordNotFound {
  1834. return nil, nil
  1835. } else {
  1836. return nil, err
  1837. }
  1838. }
  1839. return &record, nil
  1840. }
  1841. // 透析方案
  1842. func GetDialysisSolution(orgID int64, patientID int64, mode_id int64) (*models.DialysisSolution, error) {
  1843. var record models.DialysisSolution
  1844. 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
  1845. if err != nil {
  1846. if err == gorm.ErrRecordNotFound {
  1847. return nil, nil
  1848. } else {
  1849. return nil, err
  1850. }
  1851. }
  1852. return &record, nil
  1853. }
  1854. func GetDialysisSolutionOne(orgID int64, mode_id int64) (*models.DialysisSolution, error) {
  1855. var record models.DialysisSolution
  1856. err := readDb.Model(&models.DialysisSolution{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).Last(&record).Error
  1857. if err != nil {
  1858. if err == gorm.ErrRecordNotFound {
  1859. return nil, nil
  1860. } else {
  1861. return nil, err
  1862. }
  1863. }
  1864. return &record, nil
  1865. }
  1866. func GetLastDialysisPrescribeByModeId(orgID int64, patientID int64, mode_id int64) (*models.DialysisPrescription, error) {
  1867. var record models.DialysisPrescription
  1868. 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
  1869. if err != nil {
  1870. if err == gorm.ErrRecordNotFound {
  1871. return nil, nil
  1872. } else {
  1873. return nil, err
  1874. }
  1875. }
  1876. return &record, nil
  1877. }
  1878. func GetLastDryWeight(orgID int64, patientID int64) (*models.SgjPatientDryweight, error) {
  1879. var record models.SgjPatientDryweight
  1880. err := readDb.Model(&models.SgjPatientDryweight{}).Where("patient_id = ? and user_org_id = ? and status = 1", patientID, orgID).Last(&record).Error
  1881. if err != nil {
  1882. if err == gorm.ErrRecordNotFound {
  1883. return nil, nil
  1884. } else {
  1885. return nil, err
  1886. }
  1887. }
  1888. return &record, nil
  1889. }
  1890. func MobileGetSystemDialysisPrescribeByModeId(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  1891. var record models.SystemPrescription
  1892. redis := RedisClient()
  1893. defer redis.Close()
  1894. // cur_date := time.Now().Format("2006-01-02")
  1895. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(mode_id, 10) + ":system_dialysis_prescribe"
  1896. system_dialysis_prescribe_str, _ := redis.Get(key).Result()
  1897. if len(system_dialysis_prescribe_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1898. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).First(&record).Error
  1899. if err != nil {
  1900. if err == gorm.ErrRecordNotFound {
  1901. return nil, nil
  1902. } else {
  1903. return nil, err
  1904. }
  1905. } else {
  1906. if record.ID > 0 {
  1907. //缓存数据
  1908. system_dialysis_prescribe_str, err := json.Marshal(record)
  1909. if err == nil {
  1910. redis.Set(key, system_dialysis_prescribe_str, time.Second*60*60*18)
  1911. }
  1912. } else {
  1913. redis.Set(key, " ", time.Second*60*60*18)
  1914. }
  1915. return &record, nil
  1916. }
  1917. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1918. json.Unmarshal([]byte(system_dialysis_prescribe_str), &record)
  1919. return &record, nil
  1920. }
  1921. }
  1922. func GetSystemDialysisPrescribeByModeId(orgID int64, mode_id int64) (*models.SystemPrescription, error) {
  1923. var record models.SystemPrescription
  1924. err := readDb.Model(&models.SystemPrescription{}).Where("user_org_id = ? and status = 1 AND mode_id = ?", orgID, mode_id).Last(&record).Error
  1925. if err != nil {
  1926. if err == gorm.ErrRecordNotFound {
  1927. return nil, nil
  1928. } else {
  1929. return nil, err
  1930. }
  1931. }
  1932. return &record, nil
  1933. }
  1934. func GetDialysisOrderCount(orgID int64, patient_id int64, recordDate int64) (count int64, err error) {
  1935. 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
  1936. return
  1937. }
  1938. func GetFirstDateOfMonth(d time.Time) time.Time {
  1939. d = d.AddDate(0, 0, -d.Day()+1)
  1940. return GetZeroTime(d)
  1941. }
  1942. func GetZeroTime(d time.Time) time.Time {
  1943. return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
  1944. }
  1945. func GetDialysisCountByPatientId(startime int64, endtime int64, patientid int64, orgid int64) (order []*models.BloodDialysisOrderCount, err error) {
  1946. db := XTReadDB().Table("xt_dialysis_order as o")
  1947. 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
  1948. return order, err
  1949. }
  1950. func FindConsumablesByDate(orgID int64, patient_id int64, recordDate int64) (consumables []*models.DialysisBeforePrepare, err error) {
  1951. 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
  1952. return
  1953. }
  1954. func FindConsumablesByDateTwo(orgID int64, patient_id int64, recordDate int64) (consumables []*models.DialysisBeforePrepare, err error) {
  1955. 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
  1956. return
  1957. }
  1958. func FindConsumablesByDateThree(orgID int64, patient_id int64, recordDate int64) (consumables []*models.BloodAutomaticReduceDetail, err error) {
  1959. err = readDb.Model(&models.BloodAutomaticReduceDetail{}).Where("org_id = ? AND patient_id = ? AND record_time = ? AND status = 1 AND count > 0", orgID, patient_id, recordDate).Find(&consumables).Error
  1960. return
  1961. }
  1962. //func FindLastConsumables(orgID int64, patient_id int64, recordDate int64) (consumables []*models.DialysisBeforePrepare, err error){
  1963. // 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
  1964. // return
  1965. //}
  1966. //
  1967. func GetLastTimeConsumables(orgID int64, patientID int64, maxDate int64) (prepare []*models.DialysisBeforePrepare, err error) {
  1968. 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
  1969. return
  1970. }
  1971. type GoodsType struct {
  1972. ID int64 `gorm:"column:id" json:"id"`
  1973. TypeName string `gorm:"column:type_name" json:"type_name"`
  1974. Remark string `gorm:"column:remark" json:"remark"`
  1975. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  1976. Status int64 `gorm:"column:status" json:"status"`
  1977. Type int64 `gorm:"column:type" json:"type"`
  1978. }
  1979. func (GoodsType) TableName() string {
  1980. return "xt_goods_type"
  1981. }
  1982. type VMGoodInfo struct {
  1983. ID int64 `gorm:"column:id" json:"id"`
  1984. SpecificationName string `gorm:"column:specification_name" json:"specification_name"`
  1985. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  1986. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  1987. GoodName string `gorm:"column:good_name" json:"good_name" form:"good_name"`
  1988. GoodUnit int64 `gorm:"column:good_unit" json:"good_unit" form:"good_unit"`
  1989. Total float64 `gorm:"column:total" json:"total" form:"total"`
  1990. }
  1991. func (VMGoodInfo) TableName() string {
  1992. return "xt_good_information"
  1993. }
  1994. type AutomaticReduceDetail struct {
  1995. ID int64 `gorm:"column:id" json:"id"`
  1996. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  1997. Status int64 `gorm:"column:status" json:"status"`
  1998. RecordTime int64 `gorm:"column:record_time" json:"record_time"`
  1999. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2000. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  2001. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  2002. VMGoodInfo VMGoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"info"`
  2003. GoodsType GoodsType `gorm:"ForeignKey:GoodTypeId;AssociationForeignKey:ID" json:"type"`
  2004. Count int64 `gorm:"column:count" json:"count"`
  2005. }
  2006. func (AutomaticReduceDetail) TableName() string {
  2007. return "xt_automatic_reduce_detail"
  2008. }
  2009. type DialysisBeforePrepare struct {
  2010. ID int64 `gorm:"column:id" json:"id" form:"id"`
  2011. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  2012. PatientId int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  2013. RecordDate int64 `gorm:"column:record_date" json:"record_date" form:"record_date"`
  2014. GoodId int64 `gorm:"column:good_id" json:"good_id" form:"good_id"`
  2015. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id" form:"good_type_id"`
  2016. Count int64 `gorm:"column:count" json:"count" form:"count"`
  2017. Ctime int64 `gorm:"column:ctime" json:"ctime" form:"ctime"`
  2018. Mtime int64 `gorm:"column:mtime" json:"mtime" form:"mtime"`
  2019. Creater int64 `gorm:"column:creater" json:"creater" form:"creater"`
  2020. Modifier int64 `gorm:"column:modifier" json:"modifier" form:"modifier"`
  2021. VMGoodInfo VMGoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"info"`
  2022. GoodsType GoodsType `gorm:"ForeignKey:GoodTypeId;AssociationForeignKey:ID" json:"type"`
  2023. }
  2024. func (DialysisBeforePrepare) TableName() string {
  2025. return "dialysis_before_prepare"
  2026. }
  2027. type MDialysisGoodsVM struct {
  2028. ID int64 `gorm:"column:id" json:"id"`
  2029. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  2030. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  2031. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  2032. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  2033. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  2034. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  2035. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  2036. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  2037. Status int64 `gorm:"column:status" json:"status"`
  2038. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  2039. SchedualPatient *MSchedualPatientVMList `gorm:"ForeignKey:PatientId" json:"patient"`
  2040. AutomaticReduceDetail []*AutomaticReduceDetail `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"good_user"`
  2041. LastAutomaticReduceDetail []*AutomaticReduceDetail `gorm:"-" json:"last_good_user"`
  2042. DialysisBeforePrepare []*DialysisBeforePrepare `gorm:"ForeignKey:PatientId,ScheduleDate;AssociationForeignKey:PatientId,RecordDate" json:"good_user_detail"`
  2043. LastDialysisBeforePrepare []*DialysisBeforePrepare `gorm:"-" json:"last_good_user_detail"`
  2044. Project []*models.HisPrescriptionProject `gorm:"-" json:"project"`
  2045. }
  2046. func (MDialysisGoodsVM) TableName() string {
  2047. return "xt_schedule"
  2048. }
  2049. type VMWarehouseOutInfo struct {
  2050. ID int64 `gorm:"column:id" json:"id"`
  2051. WarehouseOutId int64 `gorm:"column:warehouse_out_id" json:"warehouse_out_id"`
  2052. GoodId int64 `gorm:"column:good_id" json:"good_id"`
  2053. GoodTypeId int64 `gorm:"column:good_type_id" json:"good_type_id"`
  2054. Count int64 `gorm:"column:count" json:"count"`
  2055. Price float64 `gorm:"column:price" json:"price"`
  2056. TotalPrice float64 `gorm:"column:total_price" json:"total_price"`
  2057. Status int64 `gorm:"column:status" json:"status"`
  2058. OrgId int64 `gorm:"column:org_id" json:"org_id"`
  2059. WarehouseOutOrderNumber string `gorm:"column:warehouse_out_order_number" json:"warehouse_out_order_number"`
  2060. GoodInfo VMGoodInfo `gorm:"ForeignKey:GoodId;AssociationForeignKey:ID" json:"good_info"`
  2061. GoodsType GoodsType `gorm:"ForeignKey:GoodTypeId;AssociationForeignKey:ID" json:"good_type"`
  2062. IsSys int64 `gorm:"column:is_sys" json:"is_sys"`
  2063. SysRecordTime int64 `gorm:"column:sys_record_time" json:"sys_record_time"`
  2064. Mtime int64 `gorm:"column:mtime" json:"mtime"`
  2065. Ctime int64 `gorm:"column:ctime" json:"ctime"`
  2066. }
  2067. func (VMWarehouseOutInfo) TableName() string {
  2068. return "xt_warehouse_out_info"
  2069. }
  2070. type DialysisGoodsDate struct {
  2071. RecordDate int64
  2072. }
  2073. type DialysisGoodsDetailDate struct {
  2074. RecordTime int64
  2075. }
  2076. 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) {
  2077. var vms []*MDialysisGoodsVM
  2078. var total int64
  2079. db := readDb.
  2080. Table("xt_schedule as sch").
  2081. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  2082. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  2083. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  2084. Preload("DialysisBeforePrepare", func(db *gorm.DB) *gorm.DB {
  2085. 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)
  2086. }).
  2087. Preload("AutomaticReduceDetail", func(db *gorm.DB) *gorm.DB {
  2088. 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")
  2089. }).Where("sch.status = 1 AND sch.user_org_id = ?", orgID)
  2090. if scheduleDate != 0 {
  2091. db = db.Where("schedule_date = ?", scheduleDate)
  2092. }
  2093. if schedule_type != 0 {
  2094. db = db.Where("schedule_type = ?", schedule_type)
  2095. }
  2096. if partition_id != 0 {
  2097. db = db.Where("partition_id = ?", partition_id)
  2098. }
  2099. if patient_id != 0 {
  2100. db = db.Where("patient_id = ?", patient_id)
  2101. }
  2102. //db = db.Count(&total)
  2103. //offset := (page - 1) * limit
  2104. //
  2105. //db = db.Offset(offset).Limit(limit)
  2106. err := db.Find(&vms).Error
  2107. return vms, err, total
  2108. }
  2109. func GetLastDialysisGoods(patient_id int64, orgID int64, record_time int64) (goodUser []*AutomaticReduceDetail, err error) {
  2110. var Id []*DialysisGoodsDetailDate
  2111. 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
  2112. if len(Id) > 0 {
  2113. 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
  2114. }
  2115. return
  2116. }
  2117. func GetLastDialysisBeforePrepare(patient_id int64, orgID int64, record_time int64) (goodUser []*DialysisBeforePrepare, err error) {
  2118. var Id []*DialysisGoodsDate
  2119. 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
  2120. if len(Id) > 0 {
  2121. 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
  2122. }
  2123. return
  2124. }
  2125. func MobileGetGoodsStatistics(orgID int64, start_time int64, end_time int64) (list []*models.StockInfo, err error) {
  2126. db := readDb.Model(&models.StockInfo{})
  2127. db = db.Where("xt_good_information.org_id = ? AND xt_good_information.status = 1", orgID)
  2128. 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")
  2129. db = db.Preload("QueryWarehouseOutInfo", func(db *gorm.DB) *gorm.DB {
  2130. 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)
  2131. })
  2132. db = db.Preload("GoodsType", "org_id = ? AND status = 1", orgID)
  2133. err = db.Order("ctime desc").Find(&list).Error
  2134. return
  2135. }
  2136. 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) {
  2137. var vms []*MDialysisGoodsVM
  2138. var total int64
  2139. db := readDb.
  2140. Model(&models.Schedule{}).
  2141. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  2142. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  2143. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  2144. Preload("DialysisBeforePrepare", func(db *gorm.DB) *gorm.DB {
  2145. 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)
  2146. }).
  2147. Preload("AutomaticReduceDetail", func(db *gorm.DB) *gorm.DB {
  2148. 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)
  2149. }).Where("xt_schedule.status = 1 AND xt_schedule.user_org_id = ?", orgID)
  2150. if scheduleDate != 0 {
  2151. db = db.Where("schedule_date = ?", scheduleDate)
  2152. }
  2153. if patient_id != 0 {
  2154. db = db.Where("patient_id = ?", patient_id)
  2155. }
  2156. if len(keywords) != 0 {
  2157. keywords = "%" + keywords + "%"
  2158. 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)
  2159. } else {
  2160. if schedule_type != 0 {
  2161. db = db.Where("schedule_type = ?", schedule_type)
  2162. }
  2163. if partition_id != 0 {
  2164. db = db.Where("partition_id = ?", partition_id)
  2165. }
  2166. db = db.Count(&total)
  2167. offset := (page - 1) * limit
  2168. db = db.Offset(offset).Limit(limit)
  2169. }
  2170. err := db.Find(&vms).Error
  2171. return vms, err, total
  2172. }
  2173. func GetLastDialysisGoodsTwo(patient_id int64, orgID int64, record_time int64) (goodUser []*AutomaticReduceDetail, err error) {
  2174. var Id []*DialysisGoodsDetailDate
  2175. 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
  2176. if len(Id) > 0 {
  2177. 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
  2178. }
  2179. return
  2180. }
  2181. func GetLastDialysisBeforePrepareTwo(patient_id int64, orgID int64, record_time int64) (goodUser []*DialysisBeforePrepare, err error) {
  2182. var Id []*DialysisGoodsDate
  2183. 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
  2184. if len(Id) > 0 {
  2185. 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
  2186. }
  2187. return
  2188. }
  2189. func GetAssessmentBefor(orgid int64, patientid int64, recorddate int64) (*models.PredialysisEvaluation, error) {
  2190. evaluation := models.PredialysisEvaluation{}
  2191. err := XTReadDB().Model(&evaluation).Where("user_org_id = ? and patient_id = ? and assessment_date = ? and status = 1", orgid, patientid, recorddate).Find(&evaluation).Error
  2192. if err == gorm.ErrRecordNotFound {
  2193. return nil, err
  2194. }
  2195. if err != nil {
  2196. return nil, err
  2197. }
  2198. return &evaluation, nil
  2199. }
  2200. func GetAllHisDoctorAdvice(orgid int64, patientid int64, recorddate int64) (his []*models.HisDoctorAdviceInfo, err error) {
  2201. redis := RedisClient()
  2202. defer redis.Close()
  2203. key := strconv.FormatInt(orgid, 10) + ":"+ strconv.FormatInt(patientid, 10) + ":"+ strconv.FormatInt(recorddate, 10) + ":his_doctor_advice"
  2204. his_doctor_advice_str, _ := redis.Get(key).Result()
  2205. if len(his_doctor_advice_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2206. err = readDb.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? AND user_org_id = ? AND status=1 AND record_date = ?", patientid, orgid, recorddate).Find(&his).Error
  2207. if err != nil {
  2208. if err == gorm.ErrRecordNotFound {
  2209. return his, nil
  2210. } else {
  2211. return his, err
  2212. }
  2213. } else {
  2214. if len(his) > 0 {
  2215. //缓存数据
  2216. his_doctor_advice_str, err := json.Marshal(his)
  2217. if err == nil {
  2218. redis.Set(key, his_doctor_advice_str, time.Second*60*60*18)
  2219. }
  2220. } else {
  2221. redis.Set(key, " ", time.Second*60*60*18)
  2222. }
  2223. return his, nil
  2224. }
  2225. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2226. json.Unmarshal([]byte(his_doctor_advice_str), &his)
  2227. return his, nil
  2228. }
  2229. }
  2230. func GetLastDialysisPrescriptionByPatientId(orgid int64, patientid int64, recorddate int64) (*models.DialysisPrescription, error) {
  2231. prescription := models.DialysisPrescription{}
  2232. err := readDb.Model(&models.DialysisPrescription{}).Where("user_org_id = ? and patient_id = ? and record_date = ? and status = 1", orgid, patientid, recorddate).Find(&prescription).Error
  2233. if err == gorm.ErrRecordNotFound {
  2234. return nil, err
  2235. }
  2236. if err != nil {
  2237. return nil, err
  2238. }
  2239. return &prescription, nil
  2240. }
  2241. type HisMScheduleDoctorAdviceVM struct {
  2242. ID int64 `gorm:"column:id" json:"id"`
  2243. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  2244. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  2245. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  2246. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  2247. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  2248. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  2249. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  2250. Status int64 `gorm:"column:status" json:"status"`
  2251. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  2252. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  2253. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  2254. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  2255. HisDoctorAdviceInfo []*models.HisDoctorAdviceInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  2256. HisPrescriptionProject []*models.HisPrescriptionProject `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"project"`
  2257. }
  2258. func (HisMScheduleDoctorAdviceVM) TableName() string {
  2259. return "xt_schedule"
  2260. }
  2261. type HisMScheduleDoctorAdviceVMOne struct {
  2262. ID int64 `gorm:"column:id" json:"id"`
  2263. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  2264. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  2265. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  2266. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  2267. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  2268. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  2269. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  2270. Status int64 `gorm:"column:status" json:"status"`
  2271. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  2272. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  2273. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  2274. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  2275. HisDoctorAdviceInfo []*models.HisDoctorAdviceInfo `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"doctor_advice"`
  2276. HisPrescriptionProject []*models.HisPrescriptionProject `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"project"`
  2277. }
  2278. func (HisMScheduleDoctorAdviceVMOne) TableName() string {
  2279. return "xt_schedule"
  2280. }
  2281. type HisMScheduleProjectVM struct {
  2282. ID int64 `gorm:"column:id" json:"id"`
  2283. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id"`
  2284. PartitionId int64 `gorm:"column:partition_id" json:"partition_id"`
  2285. BedId int64 `gorm:"column:bed_id" json:"bed_id"`
  2286. PatientId int64 `gorm:"column:patient_id" json:"patient_id"`
  2287. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  2288. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  2289. ModeId int64 `gorm:"column:mode_id" json:"mode_id"`
  2290. Status int64 `gorm:"column:status" json:"status"`
  2291. DialysisOrder *MDialysisOrderVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"dialysis_order"`
  2292. SchedualPatient *MSchedualPatientVM `gorm:"ForeignKey:PatientId;AssociationForeignKey:ID" json:"patient"`
  2293. DeviceNumber *MDeviceNumberVM `gorm:"ForeignKey:BedId" json:"device_number"`
  2294. Prescription *models.DialysisPrescriptionList `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"prescription"`
  2295. HisPrescriptionProject []*models.HisPrescriptionProject `gorm:"ForeignKey:PatientId;AssociationForeignKey:PatientId" json:"project"`
  2296. }
  2297. func (HisMScheduleProjectVM) TableName() string {
  2298. return "xt_schedule"
  2299. }
  2300. func GetHisDoctorConfig(orgid int64) (models.XtHisConfig, error) {
  2301. config := models.XtHisConfig{}
  2302. err := XTReadDB().Model(&config).Where("user_org_id = ? and status =1", orgid).Find(&config).Error
  2303. return config, err
  2304. }
  2305. func GetHisDoctorPatientById(orgid int64, patientid int64, recordtime int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  2306. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1")
  2307. if orgid > 0 {
  2308. db = db.Where("x.user_org_id = ?", orgid)
  2309. }
  2310. if patientid > 0 {
  2311. db = db.Where("x.patient_id =?", patientid)
  2312. }
  2313. if recordtime > 0 {
  2314. db = db.Where("x.advice_date = ?", recordtime)
  2315. }
  2316. 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
  2317. return advice, err
  2318. }
  2319. func GetDoctorAdviceCount(startime int64, endtime int64, deliveway string, orgid int64) (advice []*models.BloodDoctorAdvice, err error) {
  2320. db := XTReadDB().Table("xt_doctor_advice as x").Where("x.status = 1 and x.advice_type =2")
  2321. if startime > 0 {
  2322. db = db.Where("x.advice_date >= ?", startime)
  2323. }
  2324. if endtime > 0 {
  2325. db = db.Where("x.advice_date<=?", endtime)
  2326. }
  2327. if orgid > 0 {
  2328. db = db.Where("x.user_org_id = ?", orgid)
  2329. }
  2330. if len(deliveway) > 0 {
  2331. db = db.Where("x.delivery_way = ?", deliveway)
  2332. }
  2333. err = db.Select("x.advice_name,x.advice_desc,x.delivery_way,count(x.id) as total").Group("x.advice_name,x.advice_desc").Scan(&advice).Error
  2334. return advice, err
  2335. }
  2336. func GetHisDoctorAdviceCount(startime int64, endtime int64, deliveway string, orgid int64) (advice []*models.BloodDoctorAdvice, err error) {
  2337. db := XTReadDB().Table("his_doctor_advice_info as x").Where("x.status = 1 and x.advice_type =2")
  2338. if startime > 0 {
  2339. db = db.Where("x.advice_date >= ?", startime)
  2340. }
  2341. if endtime > 0 {
  2342. db = db.Where("x.advice_date<=?", endtime)
  2343. }
  2344. if orgid > 0 {
  2345. db = db.Where("x.user_org_id = ?", orgid)
  2346. }
  2347. if len(deliveway) > 0 {
  2348. db = db.Where("x.delivery_way = ?", deliveway)
  2349. }
  2350. err = db.Select("x.advice_name,x.advice_desc,x.delivery_way,count(x.id) as total").Group("x.advice_name,x.advice_desc").Scan(&advice).Error
  2351. return advice, err
  2352. }
  2353. func MobileGetScheduleDoctorAdvicesOne(orgID int64, scheduleDate int64, adviceType int, patientType int, adminUserId int64, deliverWay string, scheduleType int64, partitonType int64) ([]*MScheduleDoctorAdviceVM, error) {
  2354. var vms []*MScheduleDoctorAdviceVM
  2355. adviceWhere := ""
  2356. adviceCondition := []interface{}{}
  2357. if adviceType == 0 {
  2358. if patientType == 0 {
  2359. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND (advice_type = 3 OR advice_type = 1)"
  2360. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2361. } else if patientType == 1 {
  2362. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_doctor = ? AND(advice_type = 3 OR advice_type = 1)"
  2363. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  2364. } else if patientType == 2 {
  2365. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND execution_staff = 0 AND(advice_type = 3 OR advice_type = 1)"
  2366. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2367. }
  2368. } else if adviceType == 1 {
  2369. if patientType == 0 {
  2370. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? "
  2371. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2372. } else if patientType == 1 {
  2373. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND advice_doctor = ? "
  2374. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  2375. } else if patientType == 2 {
  2376. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 1 AND record_date = ? AND execution_staff = 0"
  2377. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2378. }
  2379. } else if adviceType == 3 {
  2380. if patientType == 0 {
  2381. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 3 AND record_date = ? "
  2382. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2383. } else if patientType == 1 {
  2384. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND advice_doctor = ? "
  2385. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  2386. } else if patientType == 2 {
  2387. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 3 AND execution_staff = 0"
  2388. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2389. }
  2390. } else if adviceType == 2 && len(deliverWay) > 0 {
  2391. if patientType == 0 {
  2392. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ? and delivery_way = ?"
  2393. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  2394. } else if patientType == 1 {
  2395. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ? and delivery_way = ? "
  2396. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId, deliverWay)
  2397. } else if patientType == 2 {
  2398. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0 and delivery_way = ?"
  2399. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, deliverWay)
  2400. }
  2401. } else if adviceType == 2 && len(deliverWay) <= 0 {
  2402. if patientType == 0 {
  2403. adviceWhere = "status = 1 AND user_org_id = ? AND advice_type = 2 AND record_date = ?"
  2404. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2405. } else if patientType == 1 {
  2406. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND advice_doctor = ?"
  2407. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate, adminUserId)
  2408. } else if patientType == 2 {
  2409. adviceWhere = "status = 1 AND user_org_id = ? AND record_date = ? AND advice_type = 2 AND execution_staff = 0"
  2410. adviceCondition = append(adviceCondition, adviceWhere, orgID, scheduleDate)
  2411. }
  2412. }
  2413. db := readDb.Table("xt_schedule")
  2414. if scheduleType > 0 {
  2415. db = db.Where("schedule_type = ?", scheduleType)
  2416. }
  2417. if partitonType > 0 {
  2418. db = db.Where("partition_id = ?", partitonType)
  2419. }
  2420. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  2421. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  2422. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  2423. }).
  2424. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  2425. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  2426. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  2427. Preload("DialysisAssesmentBefor", "status =1 AND user_org_id = ? and assessment_date =?", orgID, scheduleDate).
  2428. Preload("DoctorAdvices", adviceCondition...).
  2429. Where("status = 1 AND user_org_id = ?", orgID)
  2430. if scheduleDate != 0 {
  2431. db = db.Where("schedule_date = ?", scheduleDate)
  2432. }
  2433. err := db.Find(&vms).Error
  2434. return vms, err
  2435. }
  2436. func GetHisDoctorAdvicesOne(orgID int64, scheduleDate int64, deliverWay string, scheduleType int64, partitionType int64) ([]*HisMScheduleDoctorAdviceVM, error) {
  2437. var vms []*HisMScheduleDoctorAdviceVM
  2438. if len(deliverWay) > 0 {
  2439. db := readDb.Table("xt_schedule")
  2440. if scheduleType > 0 {
  2441. db = db.Where("schedule_type = ?", scheduleType)
  2442. }
  2443. if partitionType > 0 {
  2444. db = db.Where("partition_id = ?", partitionType)
  2445. }
  2446. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  2447. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  2448. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  2449. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  2450. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and delivery_way = ?", orgID, scheduleDate, deliverWay).
  2451. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  2452. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  2453. }).
  2454. Where("status = 1 AND user_org_id = ?", orgID)
  2455. if scheduleDate != 0 {
  2456. db = db.Where("schedule_date = ?", scheduleDate)
  2457. }
  2458. err = db.Find(&vms).Error
  2459. } else {
  2460. db := readDb.Table("xt_schedule")
  2461. if scheduleType > 0 {
  2462. db = db.Where("schedule_type = ?", scheduleType)
  2463. }
  2464. if partitionType > 0 {
  2465. db = db.Where("partition_id = ?", partitionType)
  2466. }
  2467. db = db.Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  2468. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  2469. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  2470. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  2471. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ?", orgID, scheduleDate).
  2472. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  2473. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  2474. }).
  2475. Where("status = 1 AND user_org_id = ?", orgID)
  2476. if scheduleDate != 0 {
  2477. db = db.Where("schedule_date = ?", scheduleDate)
  2478. }
  2479. err = db.Find(&vms).Error
  2480. }
  2481. return vms, err
  2482. }
  2483. func BatchDeleteMonitor(ids []string) (err error) {
  2484. if len(ids) == 1 {
  2485. err = XTWriteDB().Model(&models.MonitoringRecord{}).Where("id=?", ids[0]).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  2486. } else {
  2487. err = XTWriteDB().Model(models.MonitoringRecord{}).Where("id IN(?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  2488. }
  2489. return
  2490. }
  2491. func GetPatientDialysisRecordList(patientid int64, startime int64, endtime int64) (order []*models.XtDialysisOrders, err error) {
  2492. db := XTReadDB().Table("xt_dialysis_order as x").Where("x.status = 1")
  2493. //table := XTReadDB().Table("xt_schedule as s")
  2494. //fmt.Println(table)
  2495. if patientid > 0 {
  2496. db = db.Where("x.patient_id = ?", patientid)
  2497. }
  2498. if startime > 0 {
  2499. db = db.Where("x.dialysis_date>=?", startime)
  2500. }
  2501. if endtime > 0 {
  2502. db = db.Where("x.dialysis_date <= ?", endtime)
  2503. }
  2504. err = db.Select("x.id,x.dialysis_date,x.schedual_type,s.mode_id").Joins("left join xt_schedule as s on x.patient_id = s.patient_id and x.dialysis_date = s.schedule_date ").Scan(&order).Error
  2505. return order, err
  2506. }
  2507. func BatchDeleteAdvice(ids []string) (err error) {
  2508. if len(ids) == 1 {
  2509. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("id=?", ids[0]).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  2510. } else {
  2511. err = XTWriteDB().Model(models.DoctorAdvice{}).Where("id IN(?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  2512. }
  2513. return
  2514. }
  2515. func BatchDeleteHisAdvice(ids []string) (err error) {
  2516. if len(ids) == 1 {
  2517. err = XTWriteDB().Model(&models.HisDoctorAdviceInfo{}).Where("id = ?", ids[0]).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  2518. } else {
  2519. err = XTWriteDB().Model(models.HisDoctorAdviceInfo{}).Where("id IN(?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  2520. }
  2521. return
  2522. }
  2523. func UpdateAutoReduceDetail(good_id int64, count int64, record_time int64, patient_id int64) (models.AutomaticReduceDetail, error) {
  2524. detail := models.AutomaticReduceDetail{}
  2525. err := XTWriteDB().Model(&detail).Where("good_id = ? and status = 1 and record_time = ? and patient_id = ?", good_id, record_time, patient_id).Updates(map[string]interface{}{"count": count}).Error
  2526. return detail, err
  2527. }
  2528. func DeleteAutoReduceDetail(good_id int64, record_time int64, patient_id int64) error {
  2529. detail := models.AutomaticReduceDetail{}
  2530. err := XTWriteDB().Model(&detail).Where("good_id = ? and status = 1 and record_time = ? and patient_id = ?", good_id, record_time, patient_id).Updates(map[string]interface{}{"status": 0}).Error
  2531. return err
  2532. }
  2533. func DeleteDialysisBeforOne(good_id int64, record_time int64, patient_id int64) error {
  2534. detail := models.DialysisBeforePrepare{}
  2535. err := XTWriteDB().Model(&detail).Where("good_id = ? and status = 1 and record_date = ? and patient_id = ?", good_id, record_time, patient_id).Updates(map[string]interface{}{"status": 0}).Error
  2536. return err
  2537. }
  2538. func BatchAdviceCheck(ids []string, creator int64) error {
  2539. advice := models.XtDoctorAdvice{}
  2540. err := XTWriteDB().Model(&advice).Where("id IN(?) and status = 1", ids).Updates(map[string]interface{}{"check_state": 1, "checker": creator, "check_time": time.Now().Unix()}).Error
  2541. return err
  2542. }
  2543. func BatchHisAdviceCheck(ids []string, creator int64) error {
  2544. advice := models.HisDoctorAdviceInfo{}
  2545. err := XTWriteDB().Model(&advice).Where("id IN(?) and status = 1", ids).Updates(map[string]interface{}{"check_state": 1, "checker": creator, "check_time": time.Now().Unix()}).Error
  2546. return err
  2547. }
  2548. func BatchAdviceExecution(ids []string, creator int64, execution_state int64) error {
  2549. advice := models.XtDoctorAdvice{}
  2550. err := XTWriteDB().Model(&advice).Where("id IN(?) and status = 1", ids).Updates(map[string]interface{}{"execution_state": 1, "execution_staff": creator, "execution_time": execution_state}).Error
  2551. return err
  2552. }
  2553. func BatchHisAdviceExecution(ids []string, creator int64, execution_state int64) error {
  2554. advice := models.HisDoctorAdviceInfo{}
  2555. err := XTWriteDB().Model(&advice).Where("id IN(?) and status = 1", ids).Updates(map[string]interface{}{"execution_state": 1, "execution_staff": creator, "execution_time": execution_state}).Error
  2556. return err
  2557. }
  2558. func GetAdviceExecutionById(ids []string) (doctor []*models.XtDoctorAdvice, err error) {
  2559. err = XTReadDB().Model(&doctor).Where("id IN(?) AND status = 1", ids).Find(&doctor).Error
  2560. return doctor, err
  2561. }
  2562. func GetHisAdviceExecutionById(ids []string) (doctor []*models.HisDoctorAdviceInfo, err error) {
  2563. err = XTReadDB().Model(&doctor).Where("id IN(?) AND status = 1", ids).Find(&doctor).Error
  2564. return doctor, err
  2565. }
  2566. func GetHisProjectConfig(orgid int64) (models.XtHisProjectConfig, error) {
  2567. config := models.XtHisProjectConfig{}
  2568. err := XTReadDB().Model(&config).Where("user_org_id = ? and status =1", orgid).Find(&config).Error
  2569. return config, err
  2570. }
  2571. func GetHisPrescriptionProject(org_id int64, patient_id int64, record_date int64) (project []*models.HisPrescriptionProject, err error) {
  2572. err = readDb.Model(&models.HisPrescriptionProject{}).Preload("GoodInfo", func(db *gorm.DB) *gorm.DB {
  2573. return db.Preload("GoodsType", "status = 1").Where("status = 1 ")
  2574. }).Where("user_org_id = ? AND status = 1 AND record_date = ? AND patient_id = ? AND type = 3", org_id, record_date, patient_id).Find(&project).Error
  2575. return
  2576. }
  2577. func GetPCHisPrescriptionProject(orgID int64, scheduleDate int64, deliverWay string, patientType int, adminUserId int64) ([]*HisMScheduleProjectVM, error) {
  2578. var vms []*HisMScheduleProjectVM
  2579. if patientType == 0 {
  2580. db := readDb.
  2581. Table("xt_schedule").
  2582. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  2583. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  2584. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  2585. }).
  2586. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  2587. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  2588. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  2589. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  2590. return db.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  2591. }).Where("status = 1 AND user_org_id = ?", orgID)
  2592. if scheduleDate != 0 {
  2593. db = db.Where("schedule_date = ?", scheduleDate)
  2594. }
  2595. err = db.Find(&vms).Error
  2596. }
  2597. if patientType > 0 {
  2598. db := readDb.
  2599. Table("xt_schedule").
  2600. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  2601. Preload("DialysisOrder", func(db *gorm.DB) *gorm.DB {
  2602. return db.Where("status = 1 AND user_org_id = ?", orgID).Preload("DeviceNumber", "status = 1 AND org_id= ?", orgID)
  2603. }).
  2604. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  2605. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  2606. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  2607. Preload("HisDoctorAdviceInfo", "status = 1 AND user_org_id = ? AND advice_date = ? and (advice_doctor = ? or execution_staff = ?) ", orgID, scheduleDate, adminUserId, adminUserId).
  2608. Preload("HisPrescriptionProject", func(db *gorm.DB) *gorm.DB {
  2609. return db.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Preload("HisProject").Preload("GoodInfo", "status=1")
  2610. }).
  2611. Where("status = 1 AND user_org_id = ?", orgID)
  2612. if scheduleDate != 0 {
  2613. db = db.Where("schedule_date = ?", scheduleDate)
  2614. }
  2615. err = db.Find(&vms).Error
  2616. }
  2617. return vms, err
  2618. }
  2619. func UpdateStockGoods(good_id int64, record_time int64, patient_id int64, count int64) error {
  2620. prepare := models.DialysisBeforePrepare{}
  2621. err := XTWriteDB().Model(&prepare).Where("good_id= ? and record_date = ? and patient_id = ? and count <> 0 and status = 1", good_id, record_time, patient_id).Updates(map[string]interface{}{"count": count}).Error
  2622. return err
  2623. }
  2624. func UPdateAutoStockGoods(good_id int64, record_time int64, patient_id int64, count int64) error {
  2625. detail := models.AutomaticReduceDetail{}
  2626. err := XTWriteDB().Model(&detail).Where("good_id = ? and record_time = ? and patient_id = ? and count <> 0 and status = 1", good_id, record_time, patient_id).Updates(map[string]interface{}{"count": count}).Error
  2627. return err
  2628. }