patient_service.go 133KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294
  1. package service
  2. import (
  3. "XT_New/models"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/jinzhu/gorm"
  10. )
  11. // GetPatientList 返回患者的列表
  12. func GetPatientList(orgID int64, keywords string, page, limit, schedulType, bindingState, lapseto, source, startTime, endTime, contagion, reimbursementWay, isscheduling, isprescription int64, isStartTime, isEndTime bool, patientSoureType int64) (patients []*models.Patients, total int64, err error) {
  13. db := readDb.Table("xt_patients as p").Where("p.status=1")
  14. if orgID > 0 {
  15. db = db.Where("p.user_org_id=?", orgID)
  16. }
  17. if len(keywords) > 0 {
  18. likeKey := "%" + keywords + "%"
  19. db = db.Where("p.name LIKE ? OR p.dialysis_no LIKE ? OR p.first_letter LIKE ?", likeKey, likeKey, likeKey)
  20. }
  21. if schedulType > 0 {
  22. db = db.Joins("JOIN xt_schedule as s ON s.patient_id=p.id")
  23. db = db.Where("s.status=1 and s.schedule_type=?", schedulType)
  24. }
  25. if contagion > 0 {
  26. db = db.Joins("JOIN xt_patients_infectious_diseases as xpid ON xpid.patient_id=p.id")
  27. db = db.Where("xpid.disease_id=? and xpid.status=1", contagion)
  28. }
  29. if isscheduling == 1 {
  30. db = db.Where("EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.status=1").QueryExpr())
  31. } else if isscheduling == 2 {
  32. db = db.Where("NOT EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.status=1").QueryExpr())
  33. }
  34. if isprescription == 1 {
  35. db = db.Where("EXISTS (?)", readDb.Table("xt_dialysis_prescription as xdp").Where("xdp.patient_id=p.id and xdp.status=1").QueryExpr())
  36. } else if isprescription == 2 {
  37. db = db.Where("NOT EXISTS (?)", readDb.Table("xt_dialysis_prescription as xdp").Where("xdp.patient_id=p.id and xdp.status=1").QueryExpr())
  38. }
  39. if bindingState > 0 {
  40. db = db.Where("p.binding_state=?", bindingState)
  41. }
  42. if lapseto > 0 {
  43. db = db.Where("p.lapseto=?", lapseto)
  44. }
  45. if source > 0 {
  46. db = db.Where("p.source=?", source)
  47. }
  48. if reimbursementWay > 0 {
  49. db = db.Where("p.reimbursement_way_id = ?", reimbursementWay)
  50. }
  51. if isStartTime {
  52. db = db.Where("p.created_time>=?", startTime)
  53. }
  54. if isEndTime {
  55. db = db.Where("p.created_time<=?", endTime)
  56. }
  57. if patientSoureType > 0 {
  58. db = db.Where("p.patient_source =?", patientSoureType)
  59. }
  60. offset := (page - 1) * limit
  61. if lapseto == 2 || lapseto == 3 {
  62. err = db.Order("p.death_time desc").Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time,p.user_sys_before_count,p.out_reason,p.death_time,p.is_infectious").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  63. }
  64. if lapseto != 2 && lapseto != 3 {
  65. if orgID != 10579 {
  66. err = db.Order("p.id desc").Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time,p.user_sys_before_count,p.out_reason,p.death_time,p.is_infectious").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  67. }
  68. if orgID == 10579 {
  69. err = db.Order("p.dialysis_no asc").Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time,p.user_sys_before_count,p.out_reason,p.death_time,p.is_infectious").Group("p.id").Count(&total).Offset(offset).Limit(limit).Find(&patients).Error
  70. }
  71. }
  72. return
  73. }
  74. // GetAllPatientList 返回全部患者的列表
  75. func GetAllPatientList(orgID int64) (patients []*models.Patients, total int64, err error) {
  76. db := readDb.Table("xt_patients as p").Where("p.user_org_id=? and p.status=1", orgID)
  77. err = db.Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time").Count(&total).Find(&patients).Error
  78. return
  79. }
  80. func GetAllPatientListTwenty(orgID int64) (patients []*models.Patients, total int64, err error) {
  81. db := readDb.Table("xt_patients as p").Where("p.user_org_id=? and p.status=1 and p.lapseto = 1", orgID)
  82. err = db.Select(" p.id, p.user_org_id, p.user_id, p.patient_type, p.dialysis_no, p.admission_number, p.source, p.lapseto, p.partition_id, p.bed_id, p.name, p.alias, p.gender, p.marital_status, p.id_card_no, p.birthday, p.reimbursement_way_id, p.health_care_type, p.health_care_no, p.health_care_due_date, p.height, p.blood_type, p.rh, p.health_care_due_alert_date, p.education_level, p.profession, p.phone, p.home_telephone, p.relative_phone, p.relative_relations, p.home_address, p.work_unit, p.unit_address, p.children, p.receiving_date, p.is_hospital_first_dialysis, p.first_dialysis_date, p.first_dialysis_hospital, p.induction_period, p.initial_dialysis, p.total_dialysis, p.attending_doctor_id, p.head_nurse_id, p.evaluate, p.diagnose, p.remark, p.registrars_id, p.registrars, p.qr_code, p.binding_state, p.status, p.created_time, p.updated_time").Count(&total).Find(&patients).Error
  83. return
  84. }
  85. func GetAllPatientListSix(orgID int64) (patients []*models.MSchedualPatientList, err error) {
  86. err = XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1", orgID).Find(&patients).Error
  87. return patients, err
  88. }
  89. // GetAllPatientList 返回全部患者的列表
  90. func GetAllPatientListByList(orgID int64) (patients []*models.MSchedualPatientList, err error) {
  91. redis := RedisClient()
  92. defer redis.Close()
  93. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  94. patient_info_str, _ := redis.Get(key).Result()
  95. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  96. err = readDb.Model(&models.MSchedualPatientList{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  97. if err != nil {
  98. if err == gorm.ErrRecordNotFound {
  99. if len(patients) <= 0 {
  100. redis.Set(key, "null", time.Second*60*60*18)
  101. }
  102. return nil, nil
  103. } else {
  104. return nil, err
  105. }
  106. } else {
  107. if len(patients) > 0 {
  108. //缓存数据
  109. patient_info_json, err := json.Marshal(&patients)
  110. if err == nil {
  111. redis.Set(key, patient_info_json, time.Second*60*60*18)
  112. }
  113. } else {
  114. redis.Set(key, "null", time.Second*60*60*18)
  115. }
  116. return patients, nil
  117. }
  118. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  119. if patient_info_str == "null" {
  120. return nil, nil
  121. } else {
  122. json.Unmarshal([]byte(patient_info_str), &patients)
  123. return patients, nil
  124. }
  125. }
  126. }
  127. func GetAllDevicetByListSix(orgID int64) (devices []*models.MDeviceNumberForList, err error) {
  128. err = readDb.Model(&models.MDeviceNumberForList{}).Preload("Zone", "status = 1 AND org_id = ?", orgID).Where("org_id=?", orgID).Find(&devices).Error
  129. return devices, err
  130. }
  131. func GetMonitorByListSix(orgID int64, monitor_date int64) (monitor []models.VMMonitoringRecord, err error) {
  132. err = readDb.Where("user_org_id = ? and monitor_date = ? and status =1", orgID, monitor_date).Find(&monitor).Error
  133. return monitor, err
  134. }
  135. func GetAllDevicetByList(orgID int64) (devices []*models.MDeviceNumberForList, err error) {
  136. redis := RedisClient()
  137. defer redis.Close()
  138. key := strconv.FormatInt(orgID, 10) + ":" + ":device_list_all"
  139. device_list_str, _ := redis.Get(key).Result()
  140. if len(device_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  141. err = readDb.Model(&models.MDeviceNumberForList{}).Preload("Zone", "status = 1 AND org_id = ?", orgID).Where("org_id=? and status=1 ", orgID).Find(&devices).Error
  142. if err != nil {
  143. if err == gorm.ErrRecordNotFound {
  144. if len(devices) <= 0 {
  145. redis.Set(key, "null", time.Second*60*60*18)
  146. }
  147. return nil, nil
  148. } else {
  149. return nil, err
  150. }
  151. } else {
  152. if len(devices) > 0 {
  153. //缓存数据
  154. device_list_str, err := json.Marshal(&devices)
  155. if err == nil {
  156. redis.Set(key, device_list_str, time.Second*60*60*18)
  157. }
  158. } else {
  159. redis.Set(key, "null", time.Second*60*60*18)
  160. }
  161. return devices, nil
  162. }
  163. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  164. if device_list_str == "null" {
  165. return nil, nil
  166. } else {
  167. json.Unmarshal([]byte(device_list_str), &devices)
  168. return devices, nil
  169. }
  170. }
  171. }
  172. func GetAllAdvicestByList(orgID int64, scheduleDate int64) (advices []models.VMDoctorAdviceForList, err error) {
  173. redis := RedisClient()
  174. defer redis.Close()
  175. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":advice_list_all"
  176. advice_list_str, _ := redis.Get(key).Result()
  177. if len(advice_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  178. err = readDb.Model(&models.VMDoctorAdviceForList{}).Where("status = 1 AND user_org_id = ? AND advice_type = 2 AND advice_date = ? ", orgID, scheduleDate).Find(&advices).Error
  179. if err != nil {
  180. if err == gorm.ErrRecordNotFound {
  181. if len(advices) <= 0 {
  182. redis.Set(key, "null", time.Second*60*60*18)
  183. }
  184. return nil, nil
  185. } else {
  186. return nil, err
  187. }
  188. } else {
  189. if len(advices) > 0 {
  190. //缓存数据
  191. advice_list_str, err := json.Marshal(&advices)
  192. if err == nil {
  193. redis.Set(key, advice_list_str, time.Second*60*60*18)
  194. }
  195. } else {
  196. redis.Set(key, "null", time.Second*60*60*18)
  197. }
  198. return advices, nil
  199. }
  200. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  201. if advice_list_str == "null" {
  202. return nil, nil
  203. } else {
  204. json.Unmarshal([]byte(advice_list_str), &advices)
  205. return advices, nil
  206. }
  207. }
  208. }
  209. func GetAllPrescriptionsByList(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescriptionList, err error) {
  210. err = readDb.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  211. return prescriptions, err
  212. }
  213. //func GetAllPrescriptionsByList(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescriptionList, err error) {
  214. // redis := RedisClient()
  215. // defer redis.Close()
  216. // key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  217. // prescriptions_list_all, _ := redis.Get(key).Result()
  218. // redis.Set(prescriptions_list_all, "", time.Second)
  219. // if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  220. // err = readDb.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  221. // if err != nil {
  222. // if err == gorm.ErrRecordNotFound {
  223. // if len(prescriptions) <= 0 {
  224. // redis.Set(key, "null", time.Second*60*60*18)
  225. // }
  226. // return nil, nil
  227. // } else {
  228. // return nil, err
  229. // }
  230. // } else {
  231. // if len(prescriptions) > 0 {
  232. // //缓存数据
  233. // prescriptions_list_all, err := json.Marshal(&prescriptions)
  234. // if err == nil {
  235. // redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  236. // }
  237. // } else {
  238. // redis.Set(key, "null", time.Second*60*60*18)
  239. // }
  240. // return prescriptions, nil
  241. // }
  242. // } else { //缓存数据了数据,将redis缓存的json字符串转为map
  243. // if prescriptions_list_all == "null" {
  244. // return nil, nil
  245. // } else {
  246. // json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  247. // return prescriptions, nil
  248. // }
  249. //
  250. // }
  251. //}
  252. func GetAllAssessmentBeforesByList(orgID int64, scheduleDate int64) (assessmentBefores []*models.PredialysisEvaluationList, err error) {
  253. redis := RedisClient()
  254. defer redis.Close()
  255. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_befores_list_all"
  256. assessment_befores_list_all, _ := redis.Get(key).Result()
  257. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  258. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  259. if err != nil {
  260. if err == gorm.ErrRecordNotFound {
  261. if len(assessmentBefores) <= 0 {
  262. redis.Set(key, "null", time.Second*60*60*18)
  263. }
  264. return nil, nil
  265. } else {
  266. return nil, err
  267. }
  268. } else {
  269. if len(assessmentBefores) > 0 {
  270. //缓存数据
  271. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  272. if err == nil {
  273. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  274. }
  275. } else {
  276. redis.Set(key, "null", time.Second*60*60*18)
  277. }
  278. return assessmentBefores, nil
  279. }
  280. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  281. if assessment_befores_list_all == "null" {
  282. return nil, nil
  283. } else {
  284. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  285. return assessmentBefores, nil
  286. }
  287. }
  288. }
  289. func GetAllDialysisOrdersByList(orgID int64, scheduleDate int64) (dialysisOrders []*models.MDialysisOrderForList, err error) {
  290. err = readDb.Model(&models.MDialysisOrderForList{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  291. return dialysisOrders, err
  292. }
  293. //func GetAllDialysisOrdersByList(orgID int64, scheduleDate int64) (dialysisOrders []*models.MDialysisOrderForList, err error) {
  294. // redis := RedisClient()
  295. // defer redis.Close()
  296. // key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  297. // dialysis_orders_list_all, _ := redis.Get(key).Result()
  298. // redis.Set(dialysis_orders_list_all, "", time.Second)
  299. // if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  300. // err = readDb.Model(&models.MDialysisOrderForList{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  301. //
  302. // if err != nil {
  303. // if err == gorm.ErrRecordNotFound {
  304. // return nil, nil
  305. // } else {
  306. // return nil, err
  307. // }
  308. // } else {
  309. // if len(dialysisOrders) > 0 {
  310. // //缓存数据
  311. // dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  312. // if err == nil {
  313. // redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  314. // }
  315. // } else {
  316. // redis.Set(key, "null", time.Second*60*60*18)
  317. // }
  318. // return dialysisOrders, nil
  319. // }
  320. // } else { //缓存数据了数据,将redis缓存的json字符串转为map
  321. // if dialysis_orders_list_all == "null" {
  322. // return nil, nil
  323. // } else {
  324. // json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  325. // return dialysisOrders, nil
  326. // }
  327. //
  328. // }
  329. //}
  330. func GetAllTreatmentSummarysByList(orgID int64, scheduleDate int64) (treatmentSummarys []*models.VMTreatmentSummaryForList, err error) {
  331. redis := RedisClient()
  332. defer redis.Close()
  333. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":treatment_summarys_list_all"
  334. treatment_summarys_all, _ := redis.Get(key).Result()
  335. if len(treatment_summarys_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  336. err = readDb.Model(&models.VMTreatmentSummaryForList{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&treatmentSummarys).Error
  337. if err != nil {
  338. if err == gorm.ErrRecordNotFound {
  339. if len(treatment_summarys_all) <= 0 {
  340. redis.Set(key, "null", time.Second*60*60*18)
  341. }
  342. return nil, nil
  343. } else {
  344. return nil, err
  345. }
  346. } else {
  347. if len(treatmentSummarys) > 0 {
  348. //缓存数据
  349. treatment_summarys_all, err := json.Marshal(&treatmentSummarys)
  350. if err == nil {
  351. redis.Set(key, treatment_summarys_all, time.Second*60*60*18)
  352. }
  353. } else {
  354. redis.Set(key, "null", time.Second*60*60*18)
  355. }
  356. return treatmentSummarys, nil
  357. }
  358. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  359. if treatment_summarys_all == "null" {
  360. return nil, nil
  361. } else {
  362. json.Unmarshal([]byte(treatment_summarys_all), &treatmentSummarys)
  363. return treatmentSummarys, nil
  364. }
  365. }
  366. }
  367. func GetAllAssessmentAfterDislysisByList(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  368. redis := RedisClient()
  369. defer redis.Close()
  370. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  371. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  372. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  373. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  374. if err != nil {
  375. if err == gorm.ErrRecordNotFound {
  376. if len(assessmentAfterDislysis) <= 0 {
  377. redis.Set(key, "null", time.Second*60*60*18)
  378. }
  379. return nil, nil
  380. } else {
  381. return nil, err
  382. }
  383. } else {
  384. if len(assessmentAfterDislysis) > 0 {
  385. //缓存数据
  386. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  387. if err == nil {
  388. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  389. }
  390. } else {
  391. redis.Set(key, "null", time.Second*60*60*18)
  392. }
  393. return assessmentAfterDislysis, nil
  394. }
  395. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  396. if assessment_after_dislysis__all == "null" {
  397. return nil, nil
  398. } else {
  399. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  400. return assessmentAfterDislysis, nil
  401. }
  402. }
  403. }
  404. func GetAllDobuleCheckList(orgID int64, scheduleDate int64) (dobulecheck []*models.DoubleCheck, err error) {
  405. err = XTReadDB().Where("check_date = ? and user_org_id = ? and status = 1", scheduleDate, orgID).Find(&dobulecheck).Error
  406. return dobulecheck, err
  407. }
  408. func GetAllHisAdvicesByList(orgID int64, scheduleDate int64) (hisAdvices []VMHisDoctorAdviceInfo, err error) {
  409. redis := RedisClient()
  410. defer redis.Close()
  411. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":his_advices_list_all"
  412. his_advices_all, _ := redis.Get(key).Result()
  413. if len(his_advices_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  414. err = readDb.Model(&VMHisDoctorAdviceInfo{}).Where("status = 1 AND user_org_id = ? AND advice_date = ?", orgID, scheduleDate).Find(&hisAdvices).Error
  415. if err != nil {
  416. if err == gorm.ErrRecordNotFound {
  417. if len(hisAdvices) <= 0 {
  418. redis.Set(key, "null", time.Second*60*60*18)
  419. }
  420. return nil, nil
  421. } else {
  422. return nil, err
  423. }
  424. } else {
  425. if len(hisAdvices) > 0 {
  426. //缓存数据
  427. his_advices_all, err := json.Marshal(&hisAdvices)
  428. if err == nil {
  429. redis.Set(key, his_advices_all, time.Second*60*60*18)
  430. }
  431. } else {
  432. redis.Set(key, "null", time.Second*60*60*18)
  433. }
  434. return hisAdvices, nil
  435. }
  436. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  437. if his_advices_all == "null" {
  438. return nil, nil
  439. } else {
  440. json.Unmarshal([]byte(his_advices_all), &hisAdvices)
  441. return hisAdvices, nil
  442. }
  443. }
  444. }
  445. func GetPatientListByUpdateTime(orgID int64, syncTime int64) (patients []*models.PatientListForFaceList, total int64, err error) {
  446. db := readDb.Model(&models.PatientListForFaceList{}).Where("user_org_id=? AND status=1 AND avatar <> 'https://images.shengws.com/201809182128222.png' AND avatar <> 'https://images.shengws.com/201809182128111.png' AND avatar <> ''", orgID)
  447. db = db.Where("updated_time >= ?", syncTime)
  448. err = db.Count(&total).Find(&patients).Error
  449. return
  450. }
  451. func GetPatientListBySchedules(orgID int64, syncTime int64) (patients []*models.SchedulePatients, total int64, err error) {
  452. db := readDb.Model(&models.SchedulePatients{}).Where("user_org_id=? AND status=1 AND schedule_date = ?", orgID, syncTime).Preload("Patient", "status = 1 AND user_org_id = ? ", orgID)
  453. err = db.Count(&total).Find(&patients).Error
  454. return
  455. }
  456. func GetPatientListBySchedulesFind(orgID int64, syncTime int64, patient_name string) (patients []*models.SchedulePatients, total int64, err error) {
  457. db := readDb.Model(&models.SchedulePatients{}).Where("user_org_id=? AND status=1 AND schedule_date = ?", orgID, syncTime).Preload("Patient", "status = 1 AND user_org_id = ? and name like ? ", orgID, "%"+patient_name+"%")
  458. err = db.Count(&total).Find(&patients).Error
  459. return
  460. }
  461. func GetPatientListById(orgID int64, patientId int64) (patients models.PatientListForFace, err error) {
  462. err = readDb.Model(&models.PatientListForFaceList{}).Where("user_org_id=? and id = ? and status=1", orgID, patientId).First(&patients).Error
  463. return
  464. }
  465. func GetPatientCount(orgID int64) (total int64) {
  466. readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Count(&total)
  467. return
  468. }
  469. func GetLapsetoPatientCount(orgID int64, lapseto int64) (total int64) {
  470. readDb.Model(&models.Patients{}).Where("user_org_id=? and lapseto=? and status=1", orgID, lapseto).Count(&total)
  471. return
  472. }
  473. func ChechLastDialysisNo(orgID int64) (dialysisNo int64) {
  474. var patient models.Patients
  475. if orgID == 10278 {
  476. err := readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=?", orgID).Order("id desc").First(&patient).Error
  477. if err != nil {
  478. return
  479. }
  480. } else {
  481. err := readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=?", orgID).Order("dialysis_no desc").First(&patient).Error
  482. if err != nil {
  483. return
  484. }
  485. }
  486. if patient.ID == 0 {
  487. return
  488. }
  489. dialysisNo, _ = strconv.ParseInt(patient.DialysisNo, 10, 64)
  490. return
  491. }
  492. func FindPatientByDialysisNo(orgID int64, dialysisNo string) (patient models.Patients, err error) {
  493. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and dialysis_no=? and lapseto = 1", orgID, dialysisNo).First(&patient).Error
  494. return
  495. }
  496. func FindPatientByIdCardNo(orgID int64, idCardNo string) (patient models.Patients, err error) {
  497. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and id_card_no=?", orgID, idCardNo).First(&patient).Error
  498. return
  499. }
  500. func FindPatientByMobile(orgID int64, mobile string) (patient models.Patients, err error) {
  501. err = readDb.Model(&models.Patients{}).Where("phone=? and user_org_id=? and status=1", mobile, orgID).First(&patient).Error
  502. return
  503. }
  504. func FindPatientById(orgID int64, id int64) (patient models.Patients, err error) {
  505. err = readDb.Model(&models.Patients{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  506. return
  507. }
  508. func FindPatientByIdTwo(orgID int64, id int64) (patient models.XtPatientsNew, err error) {
  509. err = readDb.Model(&models.XtPatientsNew{}).Where("blood_id = ? and user_org_id = ? and status =1", id, orgID).First(&patient).Error
  510. return
  511. }
  512. // func FindPatientByIdWithDiseases(orgID int64, id int64) (patient models.Patients, err error) {
  513. // err = readDb.Model(&models.Patients{}).Preload("Contagions", "status = 1").Preload("Diseases", "status = 1").Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  514. // return
  515. // }
  516. func FindPatientByIdWithDiseases(orgID int64, id int64) (patient models.Patients, err error) {
  517. redis := RedisClient()
  518. defer redis.Close()
  519. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(id, 10) + ":patient_info"
  520. patient_info_str, _ := redis.Get(key).Result()
  521. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  522. err = readDb.Model(&models.Patients{}).Preload("Contagions", "status = 1").Preload("Diseases", "status = 1").Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  523. if err != nil {
  524. if err == gorm.ErrRecordNotFound {
  525. return patient, nil
  526. } else {
  527. return patient, err
  528. }
  529. } else {
  530. if patient.ID > 0 {
  531. //缓存数据
  532. patient_info_json, err := json.Marshal(&patient)
  533. if err == nil {
  534. redis.Set(key, patient_info_json, time.Second*60*60*18)
  535. }
  536. } else {
  537. redis.Set(key, "null", time.Second*60*60*18)
  538. }
  539. return patient, nil
  540. }
  541. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  542. if patient_info_str == "null" {
  543. return patient, nil
  544. } else {
  545. json.Unmarshal([]byte(patient_info_str), &patient)
  546. return patient, nil
  547. }
  548. }
  549. }
  550. func FindPatientWithDeviceByNo(orgID int64, no string, time int64) (patient models.SchedualPatient2, err error) {
  551. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  552. return db.Preload("DeviceNumber", "org_id = ?", orgID).
  553. Preload("DeviceZone", "org_id = ?", orgID).
  554. Where("user_org_id = ? AND schedule_date = ? ", orgID, time)
  555. }).Preload("InfectiousDiseases", "status = 1").Where("user_org_id=? and dialysis_no = ? and status = 1 ", orgID, no).First(&patient).Error
  556. return
  557. }
  558. func FindPatientWithDeviceByPatientId(orgID int64, id int64, time int64) (patient models.SchedualPatient2, err error) {
  559. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  560. return db.Preload("DeviceNumber", "org_id = ?", orgID).
  561. Preload("DeviceZone", "org_id = ?", orgID).
  562. Where("user_org_id = ? AND schedule_date = ? ", orgID, time)
  563. }).Preload("InfectiousDiseases", "status = 1").Where("user_org_id=? and id = ? and status = 1 ", orgID, id).First(&patient).Error
  564. return
  565. }
  566. func CreatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  567. user, _ := GetSgjUserByMobild(patient.Phone)
  568. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  569. utx := writeDb.Begin()
  570. btx := writeUserDb.Begin()
  571. if user.ID == 0 {
  572. user.Mobile = patient.Phone
  573. user.Avatar = patient.Avatar
  574. user.AvatarThumb = patient.Avatar
  575. user.Birthday = patient.Birthday
  576. user.Username = patient.Name
  577. user.Gender = patient.Gender
  578. user.Sources = 11
  579. user.Introduce = patient.Remark
  580. user.Status = 1
  581. user.UpdatedTime = patient.UpdatedTime
  582. user.CreatedTime = patient.CreatedTime
  583. err = btx.Create(&user).Error
  584. if err != nil {
  585. utx.Rollback()
  586. btx.Rollback()
  587. return
  588. }
  589. }
  590. patient.UserId = user.ID
  591. if customer == nil {
  592. err = btx.Create(&models.SgjCustomer{
  593. UserOrgId: patient.UserOrgId,
  594. UserId: user.ID,
  595. Mobile: patient.Phone,
  596. Name: patient.Name,
  597. Gender: patient.Gender,
  598. Birthday: patient.Birthday,
  599. Sources: 11,
  600. Status: 1,
  601. CreatedTime: patient.CreatedTime,
  602. UpdatedTime: patient.UpdatedTime,
  603. Avatar: patient.Avatar,
  604. Remark: patient.Remark,
  605. }).Error
  606. if err != nil {
  607. utx.Rollback()
  608. btx.Rollback()
  609. return
  610. }
  611. }
  612. err = utx.Create(patient).Error
  613. if err != nil {
  614. utx.Rollback()
  615. btx.Rollback()
  616. return
  617. }
  618. var lapseto models.PatientLapseto
  619. lapseto.PatientId = patient.ID
  620. lapseto.LapsetoType = patient.Lapseto
  621. lapseto.CreatedTime = patient.CreatedTime
  622. lapseto.UpdatedTime = patient.CreatedTime
  623. lapseto.Status = 1
  624. lapseto.LapsetoTime = patient.CreatedTime
  625. err = utx.Create(&lapseto).Error
  626. if err != nil {
  627. utx.Rollback()
  628. btx.Rollback()
  629. return
  630. }
  631. if len(contagions) > 0 {
  632. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  633. insertParams := make([]string, 0)
  634. insertData := make([]interface{}, 0)
  635. for _, contagion := range contagions {
  636. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  637. insertData = append(insertData, patient.ID)
  638. insertData = append(insertData, contagion)
  639. insertData = append(insertData, 1)
  640. insertData = append(insertData, patient.CreatedTime)
  641. insertData = append(insertData, patient.UpdatedTime)
  642. }
  643. thisSQL += strings.Join(insertParams, ", ")
  644. err = utx.Exec(thisSQL, insertData...).Error
  645. if err != nil {
  646. utx.Rollback()
  647. btx.Rollback()
  648. return
  649. }
  650. }
  651. if len(diseases) > 0 {
  652. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  653. insertParams := make([]string, 0)
  654. insertData := make([]interface{}, 0)
  655. for _, disease := range diseases {
  656. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  657. insertData = append(insertData, patient.ID)
  658. insertData = append(insertData, disease)
  659. insertData = append(insertData, 1)
  660. insertData = append(insertData, patient.CreatedTime)
  661. insertData = append(insertData, patient.UpdatedTime)
  662. }
  663. thisSQL += strings.Join(insertParams, ", ")
  664. err = utx.Exec(thisSQL, insertData...).Error
  665. if err != nil {
  666. utx.Rollback()
  667. btx.Rollback()
  668. return
  669. }
  670. }
  671. utx.Commit()
  672. btx.Commit()
  673. return
  674. }
  675. func GetLastPatientData(orgid int64) (models.Patients, error) {
  676. patients := models.Patients{}
  677. err := XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1", orgid).Last(&patients).Error
  678. return patients, err
  679. }
  680. func CreatePatientsNew(patientsNew *models.XtPatientsNew) error {
  681. err := XTWriteDB().Model(&patientsNew).Create(&patientsNew).Error
  682. return err
  683. }
  684. func CreateAllergic(allergic models.XtPatientAllergic) error {
  685. err := XTWriteDB().Create(&allergic).Error
  686. return err
  687. }
  688. func EditPatientLapseto(patient *models.Patients, lapseto *models.PatientLapseto) (err error) {
  689. utx := writeDb.Begin()
  690. err = utx.Model(&models.Patients{}).Where("id=?", patient.ID).Update(map[string]interface{}{"lapseto": patient.Lapseto, "out_reason": patient.OutReason, "death_time": patient.DeathTime, "patient_start_time": patient.PatientStartTime, "patient_end_time": patient.PatientEndTime, "patient_address": patient.PatientAddress}).Error
  691. //err = utx.Model(&models.PatientLapseto{}).Where("id=?", patient.ID).Update(map[string]interface{}{"lapseto_type": patient.Lapseto, "lapseto_time": time.Now().Unix(), "updated_time": time.Now().Unix()}).Error
  692. if err != nil {
  693. utx.Rollback()
  694. return
  695. }
  696. err = utx.Create(lapseto).Error
  697. if err != nil {
  698. utx.Rollback()
  699. return
  700. }
  701. // 删除排班和排班模板信息
  702. if lapseto.LapsetoType == 2 || lapseto.LapsetoType == 3 {
  703. now := time.Now()
  704. deleteScheduleErr := utx.Model(&models.PatientSchedule{}).Where("patient_id = ? AND schedule_date >= ? AND status = 1", patient.ID, lapseto.LapsetoTime).Updates(map[string]interface{}{
  705. "status": 0,
  706. "updated_time": now.Unix(),
  707. }).Error
  708. if deleteScheduleErr != nil {
  709. utx.Rollback()
  710. err = deleteScheduleErr
  711. return
  712. }
  713. deleteSchTempItemErr := utx.Model(&models.PatientScheduleTemplateItem{}).Where("patient_id = ? AND status = 1", patient.ID).Updates(map[string]interface{}{
  714. "status": 0,
  715. "mtime": now.Unix(),
  716. }).Error
  717. if deleteSchTempItemErr != nil {
  718. utx.Rollback()
  719. err = deleteSchTempItemErr
  720. return
  721. }
  722. }
  723. utx.Commit()
  724. return
  725. }
  726. func UpdatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  727. // if len(contagions) > 0 || len(diseases) > 0 {
  728. utx := writeDb.Begin()
  729. err = utx.Save(patient).Error
  730. if err != nil {
  731. utx.Rollback()
  732. return
  733. }
  734. err = utx.Model(&models.InfectiousDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  735. fmt.Println("err", err)
  736. if err != nil {
  737. utx.Rollback()
  738. return
  739. }
  740. if len(contagions) > 0 && patient.IsInfectious == 2 {
  741. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  742. insertParams := make([]string, 0)
  743. insertData := make([]interface{}, 0)
  744. for _, contagion := range contagions {
  745. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  746. insertData = append(insertData, patient.ID)
  747. insertData = append(insertData, contagion)
  748. insertData = append(insertData, 1)
  749. insertData = append(insertData, time.Now().Unix())
  750. insertData = append(insertData, time.Now().Unix())
  751. }
  752. thisSQL += strings.Join(insertParams, ", ")
  753. err = utx.Exec(thisSQL, insertData...).Error
  754. if err != nil {
  755. utx.Rollback()
  756. return
  757. }
  758. }
  759. err = utx.Model(&models.ChronicDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  760. if err != nil {
  761. utx.Rollback()
  762. return
  763. }
  764. if len(diseases) > 0 {
  765. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  766. insertParams := make([]string, 0)
  767. insertData := make([]interface{}, 0)
  768. for _, disease := range diseases {
  769. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  770. insertData = append(insertData, patient.ID)
  771. insertData = append(insertData, disease)
  772. insertData = append(insertData, 1)
  773. insertData = append(insertData, time.Now().Unix())
  774. insertData = append(insertData, time.Now().Unix())
  775. }
  776. thisSQL += strings.Join(insertParams, ", ")
  777. err = utx.Exec(thisSQL, insertData...).Error
  778. if err != nil {
  779. utx.Rollback()
  780. return
  781. }
  782. }
  783. utx.Commit()
  784. // } else {
  785. // err = writeDb.Save(patient).Error
  786. // }
  787. return
  788. }
  789. func UpdatePatientLapseto(patientid int64, lapseto models.PatientLapseto) error {
  790. err := XTWriteDB().Model(&lapseto).Where("patient_id = ?", patientid).Updates(map[string]interface{}{"lapseto_type": lapseto.LapsetoType, "lapseto_time": time.Now().Unix(), "updated_time": time.Now().Unix()}).Error
  791. return err
  792. }
  793. func UpdatepatientTwo(patientsNew *models.XtPatientsNew, id int64) error {
  794. err := XTWriteDB().Model(&patientsNew).Where("blood_id = ?", id).Update(map[string]interface{}{"user_org_id": patientsNew.UserOrgId, "user_id": patientsNew.UserId, "avatar": patientsNew.Avatar, "patient_type": patientsNew.Avatar, "dialysis_no": patientsNew.DialysisNo, "admission_number": patientsNew.AdmissionNumber, "source": patientsNew.Source, "lapseto": patientsNew.Lapseto, "partition_id": patientsNew.PartitionId, "bed_id": patientsNew.BedId, "name": patientsNew.Name, "alias": patientsNew.Alias, "gender": patientsNew.Gender, "marital_status": patientsNew.MaritalStatus, "id_card_no": patientsNew.IdCardNo, "birthday": patientsNew.Birthday, "reimbursement_way_id": patientsNew.ReimbursementWayId, "health_care_type": patientsNew.HealthCareType, "health_care_no": patientsNew.HealthCareType, "health_care_due_date": patientsNew.HealthCareType, "height": patientsNew.Height, "blood_type": patientsNew.BloodType, "rh": patientsNew.Rh, "health_care_due_alert_date": patientsNew.HealthCareDueAlertDate, "education_level": patientsNew.EducationLevel, "profession": patientsNew.Profession, "phone": patientsNew.Phone, "home_telephone": patientsNew.HomeTelephone, "relative_phone": patientsNew.RelativePhone, "relative_relations": patientsNew.RelativeRelations, "home_address": patientsNew.HomeAddress, "work_unit": patientsNew.WorkUnit, "unit_address": patientsNew.UnitAddress, "children": patientsNew.Children, "receiving_date": patientsNew.ReceivingDate, "is_hospital_first_dialysis": patientsNew.IsHospitalFirstDialysis, "first_dialysis_date": patientsNew.FirstDialysisDate, "first_dialysis_hospital": patientsNew.FirstDialysisHospital, "predialysis_condition": patientsNew.PredialysisCondition, "pre_hospital_dialysis_frequency": patientsNew.PreHospitalDialysisFrequency, "pre_hospital_dialysis_times": patientsNew.PreHospitalDialysisFrequency, "hospital_first_dialysis_date": patientsNew.HospitalFirstDialysisDate, "induction_period": patientsNew.InductionPeriod, "initial_dialysis": patientsNew.InitialDialysis, "total_dialysis": patientsNew.TotalDialysis, "attending_doctor_id": patientsNew.AttendingDoctorId, "head_nurse_id": patientsNew.HeadNurseId, "evaluate": patientsNew.Evaluate, "diagnose": patientsNew.Diagnose, "remark": patientsNew.Remark, "registrars_id": patientsNew.RegistrarsId, "registrars": patientsNew.Registrars, "qr_code": patientsNew.QrCode, "binding_state": patientsNew.BindingState, "patient_complains": patientsNew.PatientComplains, "present_history": patientsNew.PresentHistory, "past_history": patientsNew.PastHistory, "temperature": patientsNew.Temperature,
  795. "pulse": patientsNew.Pulse, "respiratory": patientsNew.Respiratory, "sbp": patientsNew.Sbp, "dbp": patientsNew.Dbp, "nation": patientsNew.Nation, "native_place": patientsNew.NativePlace, "age": patientsNew.Age, "infectious_next_record_time": patientsNew.InfectiousNextRecordTime, "is_infectious": patientsNew.IsInfectious, "remind_cycle": patientsNew.RemindCycle, "response_result": patientsNew.ResponseResult, "is_open_remind": patientsNew.IsOpenRemind, "first_treatment_date": patientsNew.FirstTreatmentDate, "dialysis_age": patientsNew.DialysisAge, "expense_kind": patientsNew.ExpenseKind, "tell_phone": patientsNew.ExpenseKind, "contact_name": patientsNew.ContactName, "blood_patients": patientsNew.BloodPatients, "slow_patients": patientsNew.SlowPatients, "member_patients": patientsNew.MemberPatients, "ecommer_patients": patientsNew.EcommerPatients, "troble_shoot": patientsNew.TrobleShoot, "sch_remark": patientsNew.SchRemark, "treatment_plan": patientsNew.TreatmentPlan, "record_number": patientsNew.RecordNumber, "patient_start_time": patientsNew.PatientStartTime, "patient_end_time": patientsNew.PatientEndTime, "infectious_remark": patientsNew.InfectiousRemark, "allergic_history": patientsNew.AllergicHistory, "patient_address": patientsNew.PatientAddress}).Error
  796. return err
  797. }
  798. func GetLastInfectionRecord(id int64, org_id int64, project_id int64, date int64) (inspection models.Inspection, err error) {
  799. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and status=1 and org_id = ? and project_id = ? AND inspect_date = ? ", id, org_id, project_id, date).Last(&inspection).Error
  800. return
  801. }
  802. func GetAllInfectionRecord(date int64, org_id int64, patient_id int64, project_id int64) (inspection []*models.Inspection, err error) {
  803. err = readDb.Model(&models.Inspection{}).Where("patient_id=? and status=1 and org_id = ? and project_id = ? and inspect_date = ?", patient_id, org_id, project_id, date).Find(&inspection).Error
  804. return
  805. }
  806. func GetPatientDiseases(id int64) []int64 {
  807. var dis []models.ChronicDiseases
  808. ids := make([]int64, 0)
  809. err := readDb.Model(&models.ChronicDiseases{}).Where("patient_id=? and status=1", id).Find(&dis).Error
  810. if err != nil || len(dis) == 0 {
  811. return ids
  812. }
  813. for _, item := range dis {
  814. ids = append(ids, item.DiseaseId)
  815. }
  816. return ids
  817. }
  818. func GetPatientContagions(id int64) []int64 {
  819. var cis []models.InfectiousDiseases
  820. ids := make([]int64, 0)
  821. err := readDb.Model(&models.InfectiousDiseases{}).Where("patient_id=? and status=1", id).Find(&cis).Error
  822. if err != nil || len(cis) == 0 {
  823. return ids
  824. }
  825. for _, item := range cis {
  826. ids = append(ids, item.DiseaseId)
  827. }
  828. return ids
  829. }
  830. func FindPatientDialysisSolutionByMode(orgID int64, patientID, modeId int64) (solution models.DialysisSolution, err error) {
  831. err = readDb.Model(&models.DialysisSolution{}).Where("user_org_id=? and patient_id=? and mode_id=? and parent_id=0 and status=1", orgID, patientID, modeId).First(&solution).Error
  832. return
  833. }
  834. func FindPatientDialysisSolution(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  835. err = readDb.Model(&models.DialysisSolution{}).Where("id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  836. return
  837. }
  838. func FindPatientDialysisSolutionChild(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  839. err = readDb.Model(&models.DialysisSolution{}).Where("parent_id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  840. return
  841. }
  842. func CreatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  843. err = writeDb.Create(solution).Error
  844. return
  845. }
  846. func UpdatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  847. err = writeDb.Save(solution).Error
  848. return
  849. }
  850. // GetPatientDialysisSolutionList 返回患者透析方案的列表
  851. func GetPatientDialysisSolutionList(orgID int64, patientID int64, page, limit int64) (solutions []*models.DialysisSolution, total int64, err error) {
  852. offset := (page - 1) * limit
  853. db := readDb.Table("xt_dialysis_solution as ds").Where("ds.status=1")
  854. if orgID > 0 {
  855. db = db.Where("ds.user_org_id=?", orgID)
  856. }
  857. if patientID > 0 {
  858. db = db.Where("ds.patient_id=?", patientID)
  859. }
  860. db = db.Count(&total).Offset(offset).Limit(limit)
  861. err = db.Order("id desc").Find(&solutions).Error
  862. if err != nil {
  863. return
  864. }
  865. if len(solutions) > 0 {
  866. nilNameIds := make([]int64, 0)
  867. for _, solution := range solutions {
  868. if len(solution.ModeName) == 0 {
  869. nilNameIds = append(nilNameIds, solution.ModeId)
  870. }
  871. }
  872. if len(nilNameIds) > 0 {
  873. var modes []*models.TreatmentMode
  874. err = readDb.Model(&models.TreatmentMode{}).Where("id IN (?)", nilNameIds).Find(&modes).Error
  875. if err != nil {
  876. return
  877. }
  878. modesMap := make(map[int64]models.TreatmentMode, 0)
  879. for _, mode := range modes {
  880. modesMap[mode.ID] = *mode
  881. }
  882. for index, solution := range solutions {
  883. if _, exixt := modesMap[solution.ModeId]; exixt && len(solution.ModeName) == 0 {
  884. solutions[index].ModeName = modesMap[solution.ModeId].Name
  885. }
  886. }
  887. }
  888. }
  889. return
  890. }
  891. // GetPatientDryWeightAdjustList 返回患者调整干体重的列表
  892. func GetPatientDryWeightAdjustList(orgID int64, patientID int64, page int64, limit int64) (weights []*models.DryWeightAdjust, total int64, err error) {
  893. db := readDb.Table("xt_dry_weight_adjust as dwa").Where("dwa.status=1")
  894. if orgID > 0 {
  895. db = db.Where("dwa.user_org_id=?", orgID)
  896. }
  897. if patientID > 0 {
  898. db = db.Where("dwa.patient_id=?", patientID)
  899. }
  900. db = db.Select("dwa.id, dwa.user_org_id, dwa.patient_id, dwa.weight, dwa.adjusted_value, dwa.doctor, dwa.registrars_id, dwa.remark, dwa.status, dwa.created_time, dwa.updated_time").Count(&total).Order("id desc")
  901. if page > 0 && limit > 0 {
  902. offset := (page - 1) * limit
  903. db = db.Offset(offset).Limit(limit)
  904. }
  905. err = db.Find(&weights).Error
  906. return
  907. }
  908. func CreateDryWeightAdjust(m *models.DryWeightAdjust) (err error) {
  909. err = writeDb.Create(m).Error
  910. return
  911. }
  912. func FindPatientLastDryWeightAdjust(orgID int64, id int64) (weight models.DryWeightAdjust, err error) {
  913. err = readDb.Model(&models.DryWeightAdjust{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  914. return
  915. }
  916. func UpdateDoctorAdviceById(id int64, prescribing_number float64, single_dose float64) error {
  917. err := XTWriteDB().Model(&models.DoctorAdvice{}).Where("id=? and status =1", id).Updates(map[string]interface{}{"prescribing_number": prescribing_number, "single_dose": single_dose}).Error
  918. return err
  919. }
  920. func CreateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  921. return writeDb.Create(m).Error
  922. }
  923. func GetMaxAdviceGroupID(orgId int64) (group int64) {
  924. var advice models.DoctorAdvice
  925. err := readDb.Table("xt_doctor_advice").Where("user_org_id=?", orgId).Select("max(groupno) as groupno").First(&advice).Error
  926. if err != nil {
  927. fmt.Println(err)
  928. group = 0
  929. }
  930. group = advice.GroupNo
  931. return
  932. }
  933. func CreateGroupAdvice(orgId int64, group int64, advices []*models.GroupAdvice) (err error) {
  934. if group == 0 {
  935. group = GetMaxAdviceGroupID(orgId) + 1
  936. }
  937. tx := writeDb.Begin()
  938. defer func() {
  939. if r := recover(); r != nil {
  940. tx.Rollback()
  941. }
  942. }()
  943. for _, advice := range advices {
  944. advice.GroupNo = group
  945. if err = tx.Create(advice).Error; err != nil {
  946. tx.Rollback()
  947. return
  948. }
  949. }
  950. tx.Commit()
  951. return
  952. }
  953. func CreateMGroupAdvice(orgId int64, advices []*models.GroupAdvice, groupNo int64) (list []*models.GroupAdvice, err error) {
  954. if groupNo <= 0 {
  955. group := GetMaxAdviceGroupID(orgId)
  956. groupNo = group + 1
  957. }
  958. tx := writeDb.Begin()
  959. defer func() {
  960. if r := recover(); r != nil {
  961. tx.Rollback()
  962. }
  963. }()
  964. for _, advice := range advices {
  965. advice.GroupNo = groupNo
  966. if err = tx.Create(advice).Error; err != nil {
  967. tx.Rollback()
  968. return
  969. }
  970. list = append(list, advice)
  971. if len(advice.Children) > 0 {
  972. for _, child := range advice.Children {
  973. child.GroupNo = groupNo
  974. child.ParentId = advice.ID
  975. fmt.Println(child)
  976. if err = tx.Create(&child).Error; err != nil {
  977. tx.Rollback()
  978. return
  979. }
  980. list = append(list, child)
  981. }
  982. }
  983. }
  984. tx.Commit()
  985. return
  986. }
  987. func UpdateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  988. return writeDb.Save(m).Error
  989. }
  990. func StopGroupAdvice(orgId int64, groupNo int64, m *models.DoctorAdvice) (err error) {
  991. err = writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? and groupno=?", orgId, groupNo).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "StopState": 1, "StopReason": m.StopReason, "StopDoctor": m.StopDoctor, "StopTime": m.StopTime}).Error
  992. if err != nil {
  993. return
  994. }
  995. return
  996. }
  997. func StopDoctorAdvice(m *models.DoctorAdvice) (err error) {
  998. ut := writeDb.Begin()
  999. err = ut.Save(m).Error
  1000. if err != nil {
  1001. ut.Rollback()
  1002. return
  1003. }
  1004. err = ut.Model(&models.DoctorAdvice{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "StopState": 1, "StopReason": m.StopReason, "StopDoctor": m.StopDoctor, "StopTime": m.StopTime, "Modifier": m.Modifier}).Error
  1005. if err != nil {
  1006. ut.Rollback()
  1007. return
  1008. }
  1009. ut.Commit()
  1010. return
  1011. }
  1012. func DeleteSolution(m *models.DialysisSolution) (err error) {
  1013. if m.ParentId > 0 {
  1014. return writeDb.Save(m).Error
  1015. } else {
  1016. ut := writeDb.Begin()
  1017. err = ut.Save(m).Error
  1018. if err != nil {
  1019. ut.Rollback()
  1020. return
  1021. }
  1022. err = ut.Model(&models.DialysisSolution{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0}).Error
  1023. if err != nil {
  1024. ut.Rollback()
  1025. return
  1026. }
  1027. ut.Commit()
  1028. }
  1029. return
  1030. }
  1031. func DeleteDoctorAdvice(m *models.DoctorAdvice) (err error) {
  1032. if m.ParentId > 0 {
  1033. return writeDb.Save(m).Error
  1034. } else {
  1035. ut := writeDb.Begin()
  1036. err = ut.Save(m).Error
  1037. if err != nil {
  1038. ut.Rollback()
  1039. return
  1040. }
  1041. err = ut.Model(&models.DoctorAdvice{}).Where("parent_id=? AND user_org_id = ?", m.ID, m.UserOrgId).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0, "Modifier": m.Modifier}).Error
  1042. if err != nil {
  1043. ut.Rollback()
  1044. return
  1045. }
  1046. ut.Commit()
  1047. }
  1048. return
  1049. }
  1050. func DeleteGroupAdvice(orgId int64, groupNo int64, admin_user_id int64) (err error) {
  1051. err = writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? and groupno = ?", orgId, groupNo).Update(map[string]interface{}{"UpdatedTime": time.Now().Unix(), "Status": 0, "Modifier": admin_user_id}).Error
  1052. if err != nil {
  1053. return
  1054. }
  1055. return
  1056. }
  1057. func FindDoctorAdvice(orgID, id int64) (advice models.DoctorAdvice, err error) {
  1058. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  1059. return
  1060. }
  1061. func FindDoctorAdviceOne(orgID, patient_id int64) (advice models.DoctorAdvice, err error) {
  1062. err = readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id=? and status = 1", patient_id, orgID).First(&advice).Error
  1063. return
  1064. }
  1065. func FindDoctorAdviceTwo(ids []string, orgID int64) (advice []*models.DoctorAdvice, err error) {
  1066. err = readDb.Model(&models.DoctorAdvice{}).Where("id in(?) and user_org_id=? and status = 1", ids, orgID).First(&advice).Error
  1067. return
  1068. }
  1069. func FindHisDoctorAdvice(orgID, id int64) (advice models.HisDoctorAdviceInfo, err error) {
  1070. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  1071. return
  1072. }
  1073. func FindHisDoctorAdviceTwo(ids []string, orgID int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  1074. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id in(?) and user_org_id=? and status = 1", ids, orgID).First(&advice).Error
  1075. return
  1076. }
  1077. func FindHisDoctorAdviceOne(orgID, patient_id int64) (advice models.HisDoctorAdviceInfo, err error) {
  1078. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("patient_id = ? and user_org_id=? and status = 1", patient_id, orgID).First(&advice).Error
  1079. return
  1080. }
  1081. func FindDoctorAdviceByGroupNo(orgID, groupNo int64) (advice models.DoctorAdvice, err error) {
  1082. err = readDb.Model(&models.DoctorAdvice{}).Where("groupno = ? and user_org_id=? and status = 1", groupNo, orgID).First(&advice).Error
  1083. return
  1084. }
  1085. func GetDoctorAdviceList(orgID, patientID, advice_type, stop, start, end int64, keywords string) (advices []*models.DoctorAdvices, total int64, err error) {
  1086. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1087. table := UserReadDB().Table("sgj_user_admin_role as r")
  1088. fmt.Print("table", table)
  1089. if orgID > 0 {
  1090. db = db.Where("x.user_org_id=?", orgID)
  1091. }
  1092. if patientID > 0 {
  1093. db = db.Where("x.patient_id = ?", patientID)
  1094. }
  1095. if advice_type > 0 {
  1096. db = db.Where("x.advice_type = ?", advice_type)
  1097. } else if advice_type == 0 {
  1098. db = db.Where("x.advice_type in (?)", []int{1, 3})
  1099. }
  1100. if stop == 1 {
  1101. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1102. } else if stop == 2 {
  1103. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1104. }
  1105. if start != 0 {
  1106. db = db.Where("x.start_time>=?", start)
  1107. }
  1108. if end != 0 {
  1109. db = db.Where("start_time<=?", end)
  1110. }
  1111. if len(keywords) > 0 {
  1112. likeKey := "%" + keywords + "%"
  1113. db = db.Where("x.advice_name LIKE ?", likeKey)
  1114. }
  1115. err = db.Group("x.id").Count(&total).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, 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.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  1116. fmt.Print("err", err)
  1117. return
  1118. }
  1119. func GetLongDoctorAdviceList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64) (advices []*models.DoctorAdvices, total int64, err error) {
  1120. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1121. table := UserReadDB().Table("sgj_user_admin_role as r")
  1122. fmt.Print("table", table)
  1123. if orgID > 0 {
  1124. db = db.Where("x.user_org_id=?", orgID)
  1125. }
  1126. if patientID > 0 {
  1127. db = db.Where("x.patient_id = ?", patientID)
  1128. }
  1129. if advice_type == 1 && advice_type > 0 {
  1130. db = db.Where("x.advice_type = ?", advice_type)
  1131. }
  1132. if advice_type == 3 && advice_type > 0 {
  1133. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1134. }
  1135. if advice_type == 4 && advice_type > 0 {
  1136. db = db.Where("x.advice_type = 4")
  1137. }
  1138. if stop == 1 {
  1139. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1140. } else if stop == 2 {
  1141. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1142. }
  1143. if start != 0 {
  1144. db = db.Where("x.start_time>=?", start)
  1145. }
  1146. if end != 0 {
  1147. db = db.Where("x.start_time<=?", end)
  1148. }
  1149. if len(keywords) > 0 {
  1150. likeKey := "%" + keywords + "%"
  1151. db = db.Where("x.advice_name LIKE ?", likeKey)
  1152. }
  1153. offset := (page - 1) * limit
  1154. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, 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.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,x.push_start_time,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  1155. fmt.Print("err", err)
  1156. return
  1157. }
  1158. func GetDoctorAdviceListOne(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64) (advices []*models.DoctorAdvices, total int64, err error) {
  1159. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1160. table := UserReadDB().Table("sgj_user_admin_role as r")
  1161. fmt.Print("table", table)
  1162. if orgID > 0 {
  1163. db = db.Where("x.user_org_id=?", orgID)
  1164. }
  1165. if patientID > 0 {
  1166. db = db.Where("x.patient_id = ?", patientID)
  1167. }
  1168. if advice_type == 1 && advice_type > 0 {
  1169. db = db.Where("x.advice_type = ?", advice_type)
  1170. }
  1171. if advice_type == 3 && advice_type > 0 {
  1172. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1173. }
  1174. if advice_type == 4 && advice_type > 0 {
  1175. db = db.Where("x.advice_type = 4")
  1176. }
  1177. if stop == 1 {
  1178. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1179. } else if stop == 2 {
  1180. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1181. }
  1182. if start != 0 {
  1183. db = db.Where("x.start_time>=?", start)
  1184. }
  1185. if end != 0 {
  1186. db = db.Where("x.start_time<=?", end)
  1187. }
  1188. if len(keywords) > 0 {
  1189. likeKey := "%" + keywords + "%"
  1190. db = db.Where("x.advice_name LIKE ?", likeKey)
  1191. }
  1192. offset := (page - 1) * limit
  1193. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, 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.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,x.push_start_time,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  1194. fmt.Print("err", err)
  1195. return
  1196. }
  1197. func GetDoctorAdviceListTwo(orgID int64, patientID int64, advice_type int64, stop int64, start int64, end int64, keywords string, limit int64, page int64) (advices []*models.DoctorAdvices, total int64, err error) {
  1198. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1199. table := UserReadDB().Table("sgj_user_admin_role as r")
  1200. fmt.Println(table)
  1201. if orgID > 0 {
  1202. db = db.Where("x.user_org_id=?", orgID)
  1203. }
  1204. if patientID > 0 {
  1205. db = db.Where("x.patient_id = ?", patientID)
  1206. }
  1207. if advice_type == 1 {
  1208. db = db.Where("x.advice_type = ?", advice_type)
  1209. }
  1210. if advice_type == 3 && advice_type > 0 {
  1211. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1212. }
  1213. if advice_type == 4 && advice_type > 0 {
  1214. db = db.Where("x.advice_type = 4")
  1215. }
  1216. if stop == 1 {
  1217. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1218. } else if stop == 2 {
  1219. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1220. }
  1221. if start != 0 {
  1222. db = db.Where("x.start_time>=?", start)
  1223. }
  1224. if end != 0 {
  1225. db = db.Where("x.start_time<=?", end)
  1226. }
  1227. if len(keywords) > 0 {
  1228. likeKey := "%" + keywords + "%"
  1229. db = db.Where("x.advice_name LIKE ?", likeKey)
  1230. }
  1231. offset := (page - 1) * limit
  1232. err = db.Order("x.start_time desc").Group("x.start_time").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, 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.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,x.push_start_time,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Scan(&advices).Error
  1233. fmt.Print("错误是什么", err)
  1234. return
  1235. }
  1236. func GetDoctorAdvicePageList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page, limit int64) (advices []*models.DoctorAdvice, total int64, err error) {
  1237. offset := (page - 1) * limit
  1238. db := readDb.Model(&models.DoctorAdvice{}).Where("status=1")
  1239. if orgID > 0 {
  1240. db = db.Where("user_org_id=?", orgID)
  1241. }
  1242. if patientID > 0 {
  1243. db = db.Where("patient_id = ?", patientID)
  1244. }
  1245. if advice_type > 0 {
  1246. db = db.Where("advice_type = ?", advice_type)
  1247. }
  1248. if stop == 1 {
  1249. db = db.Where("(stop_state=? or execution_state=?) and parent_id=0", stop, stop)
  1250. } else if stop == 2 {
  1251. db = db.Where("stop_state=? and execution_state=?", stop, stop)
  1252. }
  1253. if start != 0 {
  1254. db = db.Where("start_time>=?", start)
  1255. }
  1256. if end != 0 {
  1257. db = db.Where("start_time<=?", end)
  1258. }
  1259. if len(keywords) > 0 {
  1260. likeKey := "%" + keywords + "%"
  1261. db = db.Where("advice_name LIKE ?", likeKey)
  1262. }
  1263. err = db.Count(&total).Select("id,user_org_id,patient_id,advice_type,advice_date,start_time,advice_name,advice_desc,reminder_date,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 IF(parent_id>0, parent_id, id) as advice_order").Order("advice_order desc, id").Offset(offset).Limit(limit).Scan(&advices).Error
  1264. return
  1265. }
  1266. func CreateSubDoctorAdvice(advices []*models.DoctorAdvice) (err error) {
  1267. if len(advices) > 0 {
  1268. utx := writeDb.Begin()
  1269. if len(advices) > 0 {
  1270. thisSQL := "INSERT INTO xt_doctor_advice (single_dose_unit, prescribing_number, prescribing_number_unit, advice_name, advice_desc,single_dose,created_time,updated_time,patient_id,parent_id,user_org_id,record_date,advice_type,drug_spec_unit) VALUES "
  1271. insertParams := make([]string, 0)
  1272. insertData := make([]interface{}, 0)
  1273. for _, advice := range advices {
  1274. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1275. insertData = append(insertData, advice.SingleDoseUnit)
  1276. insertData = append(insertData, advice.PrescribingNumber)
  1277. insertData = append(insertData, advice.PrescribingNumberUnit)
  1278. insertData = append(insertData, advice.AdviceName)
  1279. insertData = append(insertData, advice.AdviceDesc)
  1280. insertData = append(insertData, advice.SingleDose)
  1281. insertData = append(insertData, advice.CreatedTime)
  1282. insertData = append(insertData, advice.UpdatedTime)
  1283. insertData = append(insertData, advice.PatientId)
  1284. insertData = append(insertData, advice.ParentId)
  1285. insertData = append(insertData, advice.UserOrgId)
  1286. insertData = append(insertData, advice.RecordDate)
  1287. insertData = append(insertData, 2)
  1288. insertData = append(insertData, advice.DrugSpecUnit)
  1289. }
  1290. thisSQL += strings.Join(insertParams, ", ")
  1291. err = utx.Exec(thisSQL, insertData...).Error
  1292. if err != nil {
  1293. utx.Rollback()
  1294. return
  1295. }
  1296. }
  1297. utx.Commit()
  1298. }
  1299. return
  1300. }
  1301. func GetPatientDialysisRecord(orgID, patientID int64, page, limit, start, end, mode_id int64) ([]*models.PatientDialysisRecord, int64, error) {
  1302. offset := (page - 1) * limit
  1303. var total int64
  1304. var err error
  1305. var orders []*models.PatientDialysisRecord
  1306. // err = readDb.Table("xt_dialysis_order as do").
  1307. // Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1308. // Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1309. // Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1310. // Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1311. // Joins("JOIN xt_schedule as s ON s.patient_id=? and FROM_UNIXTIME(s.schedule_date, '%Y-%m-%d')=FROM_UNIXTIME(do.dialysis_date, '%Y-%m-%d')", patientID).
  1312. // Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1313. // Where("do.patient_id=? and do.user_org_id=? and do.stage = 2 and do.status=1", patientID, orgID).Count(&total).Offset(offset).Limit(limit).Order("do.dialysis_date desc").Select(" do.id, do.dialysis_date, do.user_org_id, do.patient_id, do.prescription_id, do.stage, do.remark, do.status, do.created_time, do.updated_time, s.schedule_type, s.partition_id, dz.name as partition_name").Find(&orders).Error
  1314. db := readDb.Table("xt_dialysis_order as do").
  1315. Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1316. Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1317. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  1318. return readDb.Where("patient_id=? and user_org_id=? and status=1", patientID, orgID).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1319. return readUserDb.Where("status = 1")
  1320. })
  1321. }).
  1322. Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1323. Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1324. Preload("Device", "org_id=? and status=1", orgID).
  1325. Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1326. return readUserDb.Where("org_id=?", orgID)
  1327. }).
  1328. Joins("JOIN xt_schedule as s ON s.patient_id=? and FROM_UNIXTIME(s.schedule_date, '%Y-%m-%d')=FROM_UNIXTIME(do.dialysis_date, '%Y-%m-%d')", patientID).
  1329. Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1330. Where("do.patient_id=? and do.user_org_id=? and do.status=1", patientID, orgID).Group("s.schedule_date")
  1331. if start != 0 {
  1332. db = db.Where("do.dialysis_date>=?", start)
  1333. }
  1334. if end != 0 {
  1335. db = db.Where("do.dialysis_date<=?", end)
  1336. }
  1337. if mode_id > 0 {
  1338. db = db.Joins("JOIN xt_dialysis_prescription as dp ON dp.patient_id=do.patient_id and dp.user_org_id = do.user_org_id and dp.record_date = do.dialysis_date")
  1339. db = db.Where("dp.mode_id=?", mode_id)
  1340. }
  1341. err = db.Count(&total).Offset(offset).Limit(limit).Order("do.dialysis_date desc").Select("do.bed_id, do.id, do.dialysis_date, do.user_org_id, do.patient_id, do.prescription_id, do.stage, do.remark, do.status, do.created_time, do.updated_time,do.start_nurse,do.finish_nurse ,s.schedule_type, s.partition_id, dz.name as partition_name").Find(&orders).Error
  1342. if len(orders) > 0 {
  1343. ids := make([]int64, 0)
  1344. for _, order := range orders {
  1345. dialyzer := order.DialysisPrescription.Dialyzer
  1346. ids = append(ids, dialyzer)
  1347. }
  1348. if len(ids) > 0 {
  1349. var dialyzers []*models.DeviceNumber
  1350. err = readDb.Model(&models.DeviceNumber{}).Where("id IN (?) and org_id=? and status=1", ids, orgID).Find(&dialyzers).Error
  1351. if err != nil {
  1352. return nil, 0, err
  1353. }
  1354. dialyzerMap := make(map[int64]models.DeviceNumber, 0)
  1355. for _, item := range dialyzers {
  1356. dialyzerMap[item.ID] = *item
  1357. }
  1358. for orderIndex, order := range orders {
  1359. if _, exist := dialyzerMap[order.DialysisPrescription.Dialyzer]; exist {
  1360. orders[orderIndex].DeviceNumber = dialyzerMap[order.DialysisPrescription.Dialyzer].Number
  1361. }
  1362. }
  1363. }
  1364. }
  1365. return orders, total, err
  1366. }
  1367. func GetPatientTreatmentSummaryList(orgID, patientID, page, limit, start, end int64) (list []*models.TreatmentSummary, total int, err error) {
  1368. offset := (page - 1) * limit
  1369. fmt.Println(offset)
  1370. fmt.Println(limit)
  1371. db := readDb.Model(&models.TreatmentSummary{}).Where("user_org_id = ? and patient_id=?", orgID, patientID)
  1372. if start != 0 {
  1373. db = db.Where("assessment_date >= ?", start)
  1374. }
  1375. if end != 0 {
  1376. db = db.Where("assessment_date <= ?", end)
  1377. }
  1378. db = db.Where("status=1")
  1379. err = db.Count(&total).Offset(offset).Limit(limit).Order("assessment_date desc, id desc").Find(&list).Error
  1380. return
  1381. }
  1382. func GetPatientScheduleList(orgID int64, patientID int64, page int64, limit int64, start int64) (schedules []*models.PatientSchedule, err error) {
  1383. offset := (page - 1) * limit
  1384. err = readDb.Table("xt_schedule as s").
  1385. Preload("DeviceZone", "org_id=? and status=1", orgID).
  1386. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  1387. Preload("TreatmentMode", "status=1").
  1388. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, start).
  1389. Limit(limit).Offset(offset).Find(&schedules).Error
  1390. return
  1391. }
  1392. func GetMonitorRecord(orgID int64, date int64, partition int64, patientId int64) ([]*models.NewVMMonitorDialysisSchedule, error) {
  1393. var mds []*models.NewVMMonitorDialysisSchedule
  1394. db := readDb.Model(&models.NewVMMonitorDialysisSchedule{})
  1395. if date > 0 {
  1396. db = db.Where("schedule_date = ?", date)
  1397. }
  1398. if partition > 0 {
  1399. db = db.Where("partition_id = ?", partition)
  1400. }
  1401. if patientId > 0 {
  1402. db = db.Where("patient_id = ?", patientId)
  1403. }
  1404. err = db.Where("status = 1 and user_org_id = ?", orgID).Find(&mds).Error
  1405. return mds, err
  1406. }
  1407. func MobileGetMonitorsWithPatient(orgID int64, keyword string, page int) ([]*models.MonitorDialysisSchedule, error) {
  1408. var patients []*models.Patients
  1409. getPatientErr := readDb.Model(&models.Patients{}).Where("status = 1 AND user_org_id = ? AND (name like ? OR dialysis_no like ?)", orgID, "%"+keyword+"%", "%"+keyword+"%").Find(&patients).Error
  1410. if getPatientErr != nil {
  1411. return nil, getPatientErr
  1412. }
  1413. patientIDs := make([]int64, len(patients))
  1414. for index, patient := range patients {
  1415. patientIDs[index] = patient.ID
  1416. }
  1417. db := readDb.
  1418. Model(&models.MonitorDialysisSchedule{}).
  1419. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1420. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  1421. Preload("TreatmentMode", "status = 1").
  1422. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  1423. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  1424. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  1425. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  1426. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  1427. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  1428. Where("status = 1 AND user_org_id = ? AND patient_id in (?)", orgID, patientIDs)
  1429. var schedules []*models.MonitorDialysisSchedule
  1430. err := db.Offset(20 * (page - 1)).Limit(20).Order("schedule_date desc").Find(&schedules).Error
  1431. return schedules, err
  1432. }
  1433. func GetPatientByKeyWord(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1434. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1", orgID)
  1435. if len(keywords) > 0 {
  1436. likekey := "%" + keywords + "%"
  1437. err = db.Where("name LIKE ? OR dialysis_no LIKE ? OR first_letter LIKE ?", likekey, likekey, likekey).Find(&patient).Error
  1438. } else {
  1439. err = db.Find(&patient).Error
  1440. }
  1441. return
  1442. }
  1443. func FindDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice models.DoctorAdvice, err error) {
  1444. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).First(&advice).Error
  1445. return
  1446. }
  1447. func FindOldDoctorAdvice(orgID int64, advice_id int64) (advice models.DoctorAdvice, err error) {
  1448. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? AND user_org_id=? AND status = 1", advice_id, orgID).First(&advice).Error
  1449. return
  1450. }
  1451. func UpdateAdviceGroupStartTime(orgID int64, groupNO int64, startTime int64, admin_user_id int64) error {
  1452. now := time.Now().Unix()
  1453. err := writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? AND status = 1 AND execution_state <> 1 AND check_state <> 1 AND groupno = ? AND admin_user_id = ?", orgID, groupNO, admin_user_id).Updates(map[string]interface{}{
  1454. "start_time": startTime,
  1455. "updated_time": now,
  1456. "modifier": admin_user_id,
  1457. }).Error
  1458. return err
  1459. }
  1460. func QueryPatientById(id int64) *models.Patients {
  1461. var pat models.Patients
  1462. err := readDb.Model(&models.Patients{}).Where("id=?", id).First(&pat).Error
  1463. if err != nil {
  1464. }
  1465. return &pat
  1466. }
  1467. func FindAllDoctorAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1468. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1", orgID, groupno).Find(&advice).Error
  1469. return
  1470. }
  1471. func UpdateDoctorAdviceByGoroupNo(id int64, drug_id int64) error {
  1472. err := XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"drug_id": drug_id, "drug_name_id": drug_id, "way": 1}).Error
  1473. return err
  1474. }
  1475. func FindAllDoctorAdviceByGoroupNoTwety(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1476. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 and (drug_id >0 or drug_name_id >0)", orgID, groupno).Find(&advice).Error
  1477. return
  1478. }
  1479. func FindDoctorAdviceByIds(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1480. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  1481. return
  1482. }
  1483. func FindDoctorAdviceByIdsTwety(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1484. err = readDb.Model(&models.DoctorAdvice{}).Where("id IN (?) AND user_org_id = ? AND status = 1 and (drug_id >0 or drug_name_id >0)", ids, orgID).Find(&advice).Error
  1485. return
  1486. }
  1487. func GetBatchDoctorAdviceList(ids []string, user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  1488. err = XTReadDB().Where("id in(?) and user_org_id = ? and status =1", ids, user_org_id).Find(&advice).Error
  1489. return advice, err
  1490. }
  1491. func BatchDeleteDoctorAdvice(ids []string, user_id int64) (err error) {
  1492. ut := writeDb.Begin()
  1493. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix(), "modifier": user_id}).Error
  1494. if err != nil {
  1495. ut.Rollback()
  1496. return
  1497. }
  1498. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?)", ids).Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix(), "modifier": user_id}).Error
  1499. if err != nil {
  1500. ut.Rollback()
  1501. return
  1502. }
  1503. ut.Commit()
  1504. return err
  1505. }
  1506. func FindAdviceByGoroupNo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1507. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND parent_id = 0", orgID, groupno).Find(&advice).Error
  1508. return
  1509. }
  1510. func UpdateDoctorAdviceAndSubAdvice(m *models.DoctorAdvice) (err error) {
  1511. ut := writeDb.Begin()
  1512. err = ut.Save(m).Error
  1513. if err != nil {
  1514. ut.Rollback()
  1515. return
  1516. }
  1517. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?)", m.ID).Updates(map[string]interface{}{"start_time": m.StartTime, "groupno": m.GroupNo, "updated_time": time.Now().Unix()}).Error
  1518. if err != nil {
  1519. ut.Rollback()
  1520. return
  1521. }
  1522. ut.Commit()
  1523. return err
  1524. }
  1525. func GetAllWaitRemindPatient(org_id int64, page int64, limit int64) (total int64, patient []*models.Patients, err error) {
  1526. type Total struct {
  1527. Count int64
  1528. }
  1529. var totals Total
  1530. offset := (page - 1) * limit
  1531. err = readDb.Raw("select * from xt_patients where user_org_id = ? AND lapseto = 1 AND infectious_next_record_time > 0 AND status = 1 AND date_sub(DATE_FORMAT(date(from_unixtime(`xt_patients`.`infectious_next_record_time`)),'%Y-%m-%d'), interval 7 day) <= now() Order by infectious_next_record_time", org_id).Offset(offset).Limit(limit).Scan(&patient).Error
  1532. readDb.Raw("select Count(id) as count from xt_patients where user_org_id = ? AND lapseto = 1 AND infectious_next_record_time > 0 AND status = 1 AND date_sub(DATE_FORMAT(date(from_unixtime(`xt_patients`.`infectious_next_record_time`)),'%Y-%m-%d'), interval 7 day) <= now() Order by infectious_next_record_time", org_id).Scan(&totals)
  1533. return totals.Count, patient, err
  1534. }
  1535. func UpdatePatientRemindStatus(patient_id int64, remind int64, org_id int64) (err error) {
  1536. err = writeDb.Model(&models.Patients{}).Where("status = 1 AND id = ? AND user_org_id = ?", patient_id, org_id).Updates(map[string]interface{}{"is_open_remind": remind}).Error
  1537. return
  1538. }
  1539. func CreatePatientWeightAdjust(m *models.SgjPatientDryweight) (err error) {
  1540. err = writeDb.Create(m).Error
  1541. return
  1542. }
  1543. func FindLastDryWeightAdjust(orgID int64, id int64) (weight models.SgjPatientDryweight, err error) {
  1544. err = readDb.Model(&models.SgjPatientDryweight{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  1545. return
  1546. }
  1547. func GetSchedualPatientByKeyWord(orgID int64, keywords string, date int64) (patient []*models.Patients, err error) {
  1548. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1549. if len(keywords) > 0 {
  1550. likekey := "%" + keywords + "%"
  1551. err = db.Where("(name LIKE ? OR dialysis_no LIKE ?) AND NOT EXISTS (Select * FROM `xt_dialysis_order` as d Where d.`dialysis_date` = ? AND d.`status` = 1 AND d.`patient_id` = xt_patients.id AND d.user_org_id = xt_patients.user_org_id)", likekey, likekey, date).Find(&patient).Error
  1552. } else {
  1553. err = db.Find(&patient).Error
  1554. }
  1555. return
  1556. }
  1557. func GetPatientScheduleOne(patientid int64, nowdate int64, orgid int64) (models.XtSchedule, error) {
  1558. schedule := models.XtSchedule{}
  1559. err := XTReadDB().Model(&schedule).Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status =1", patientid, nowdate, orgid).Find(&schedule).Error
  1560. return schedule, err
  1561. }
  1562. func CreateExportPatient(patient *models.Patients, contagions []int64, org_creator int64) (err error) {
  1563. user, _ := GetSgjUserByMobild(patient.Phone)
  1564. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  1565. utx := writeDb.Begin()
  1566. btx := writeUserDb.Begin()
  1567. if user.ID == 0 {
  1568. user.Mobile = patient.Phone
  1569. user.Avatar = patient.Avatar
  1570. user.AvatarThumb = patient.Avatar
  1571. user.Birthday = patient.Birthday
  1572. user.Username = patient.Name
  1573. user.Gender = patient.Gender
  1574. user.Sources = 11
  1575. user.Introduce = patient.Remark
  1576. user.Status = 1
  1577. user.UpdatedTime = patient.UpdatedTime
  1578. user.CreatedTime = patient.CreatedTime
  1579. err = btx.Create(&user).Error
  1580. if err != nil {
  1581. utx.Rollback()
  1582. btx.Rollback()
  1583. return
  1584. }
  1585. }
  1586. patient.UserId = user.ID
  1587. if customer == nil {
  1588. err = btx.Create(&models.SgjCustomer{
  1589. UserOrgId: patient.UserOrgId,
  1590. UserId: user.ID,
  1591. Mobile: patient.Phone,
  1592. Name: patient.Name,
  1593. Gender: patient.Gender,
  1594. Birthday: patient.Birthday,
  1595. Sources: 11,
  1596. Status: 1,
  1597. CreatedTime: patient.CreatedTime,
  1598. UpdatedTime: patient.UpdatedTime,
  1599. Avatar: patient.Avatar,
  1600. Remark: patient.Remark,
  1601. }).Error
  1602. if err != nil {
  1603. utx.Rollback()
  1604. btx.Rollback()
  1605. return
  1606. }
  1607. }
  1608. err = utx.Create(patient).Error
  1609. if err != nil {
  1610. utx.Rollback()
  1611. btx.Rollback()
  1612. return
  1613. }
  1614. if patient.DryWeight > 0 {
  1615. var dryWeight models.SgjPatientDryweight
  1616. dryWeight.PatientId = patient.ID
  1617. dryWeight.UserOrgId = patient.UserOrgId
  1618. dryWeight.Status = 1
  1619. dryWeight.AdjustedValue = "/"
  1620. dryWeight.Creator = org_creator
  1621. dryWeight.UserId = org_creator
  1622. dryWeight.Ctime = time.Now().Unix()
  1623. dryWeight.Mtime = time.Now().Unix()
  1624. dryWeight.DryWeight = patient.DryWeight
  1625. dryWeight.Remakes = ""
  1626. err = utx.Create(&dryWeight).Error
  1627. }
  1628. var lapseto models.PatientLapseto
  1629. lapseto.PatientId = patient.ID
  1630. lapseto.LapsetoType = patient.Lapseto
  1631. lapseto.CreatedTime = patient.CreatedTime
  1632. lapseto.UpdatedTime = patient.CreatedTime
  1633. lapseto.Status = 1
  1634. lapseto.LapsetoTime = patient.CreatedTime
  1635. err = utx.Create(&lapseto).Error
  1636. if err != nil {
  1637. utx.Rollback()
  1638. btx.Rollback()
  1639. return
  1640. }
  1641. if len(contagions) > 0 {
  1642. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  1643. insertParams := make([]string, 0)
  1644. insertData := make([]interface{}, 0)
  1645. for _, contagion := range contagions {
  1646. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  1647. insertData = append(insertData, patient.ID)
  1648. insertData = append(insertData, contagion)
  1649. insertData = append(insertData, 1)
  1650. insertData = append(insertData, patient.CreatedTime)
  1651. insertData = append(insertData, patient.UpdatedTime)
  1652. }
  1653. thisSQL += strings.Join(insertParams, ", ")
  1654. err = utx.Exec(thisSQL, insertData...).Error
  1655. if err != nil {
  1656. utx.Rollback()
  1657. btx.Rollback()
  1658. return
  1659. }
  1660. }
  1661. patientsNew := models.XtPatientsNew{
  1662. Name: patient.Name,
  1663. Gender: patient.Gender,
  1664. Phone: patient.Phone,
  1665. IdCardNo: patient.IdCardNo,
  1666. FirstDialysisDate: patient.FirstDialysisDate,
  1667. Source: patient.Source,
  1668. Lapseto: patient.Lapseto,
  1669. IsInfectious: patient.IsInfectious,
  1670. DialysisNo: patient.DialysisNo,
  1671. Height: patient.Height,
  1672. HomeAddress: patient.HomeAddress,
  1673. IsExcelExport: 1,
  1674. BloodPatients: 1,
  1675. Status: 1,
  1676. CreatedTime: time.Now().Unix(),
  1677. UserOrgId: patient.UserOrgId,
  1678. BloodId: patient.ID,
  1679. Avatar: "https://images.shengws.com/201809182128111.png",
  1680. }
  1681. err = utx.Create(&patientsNew).Error
  1682. if err != nil {
  1683. utx.Rollback()
  1684. btx.Rollback()
  1685. return
  1686. }
  1687. utx.Commit()
  1688. btx.Commit()
  1689. return
  1690. }
  1691. func FindPatientPhoneIsExist(phone string, org_id int64) (count int64) {
  1692. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND phone = ? AND status = 1", org_id, phone).Count(&count)
  1693. return
  1694. }
  1695. func FindPatientIdCardNoIsExist(id_card_no string, org_id int64) (count int64) {
  1696. readDb.Model(&models.Patients{}).Where("user_org_id = ? AND id_card_no = ? AND status = 1", org_id, id_card_no).Count(&count)
  1697. return
  1698. }
  1699. func CreateExportErrLog(log *models.ExportErrLog) {
  1700. writeDb.Create(&log)
  1701. return
  1702. }
  1703. func FindPatientExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1704. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 1", org_id, export_time).Find(&errLogs).Error
  1705. return
  1706. }
  1707. func FindPatientExportLogOne(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1708. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 4", org_id, export_time).Find(&errLogs).Error
  1709. return
  1710. }
  1711. func FindPatientExportLogTwo(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1712. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 5", org_id, export_time).Find(&errLogs).Error
  1713. return
  1714. }
  1715. func FindPatientExportLogThree(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  1716. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 6", org_id, export_time).Find(&errLogs).Error
  1717. return
  1718. }
  1719. func CreateExportLog(log *models.ExportLog) {
  1720. writeDb.Create(&log)
  1721. }
  1722. func UpdateDoctorEditAdvice(advice models.XtDoctorAdvice, orgid int64, groupno int64, date int64, patientid int64) error {
  1723. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and groupno = ? and advice_date = ? and patient_id = ?", orgid, groupno, date, patientid).Update(map[string]interface{}{"start_time": advice.StartTime, "updated_time": advice.UpdatedTime}).Error
  1724. return err
  1725. }
  1726. func GetPatientsByKey(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1727. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1 and lapseto = 1 ", orgID)
  1728. if len(keywords) > 0 {
  1729. likekey := "%" + keywords + "%"
  1730. err = db.Where("(name LIKE ? OR dialysis_no LIKE ? OR first_letter LIKE ?)", likekey, likekey, likekey).Find(&patient).Error
  1731. } else {
  1732. err = db.Find(&patient).Error
  1733. }
  1734. return
  1735. }
  1736. func FindAllDoctorAdviceByGoroupNoTwo(orgID int64, groupno int64) (advice []models.DoctorAdvice, err error) {
  1737. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND groupno = ? AND status = 1 AND execution_state = 2", orgID, groupno).Find(&advice).Error
  1738. return
  1739. }
  1740. func FindDoctorAdviceByIdsTwo(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1741. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? AND status = 1 AND execution_state = 2 AND (id IN (?) OR parent_id IN (?))", orgID, ids, ids).Find(&advice).Error
  1742. return
  1743. }
  1744. func CreateHisGroupAdvice(orgId int64, advices []*models.HisGroupAdvice, groupNo int64) (list []*models.HisGroupAdvice, err error) {
  1745. if groupNo <= 0 {
  1746. group := GetMaxAdviceGroupID(orgId)
  1747. groupNo = group + 1
  1748. }
  1749. tx := writeDb.Begin()
  1750. defer func() {
  1751. if r := recover(); r != nil {
  1752. tx.Rollback()
  1753. }
  1754. }()
  1755. for _, advice := range advices {
  1756. advice.Groupno = groupNo
  1757. if err = tx.Create(advice).Error; err != nil {
  1758. tx.Rollback()
  1759. return
  1760. }
  1761. list = append(list, advice)
  1762. if len(advice.Children) > 0 {
  1763. for _, child := range advice.Children {
  1764. child.Groupno = groupNo
  1765. child.ParentId = advice.ID
  1766. fmt.Println(child)
  1767. if err = tx.Create(&child).Error; err != nil {
  1768. tx.Rollback()
  1769. return
  1770. }
  1771. list = append(list, child)
  1772. }
  1773. }
  1774. }
  1775. tx.Commit()
  1776. return
  1777. }
  1778. func GetDialysisCount(orgid int64, partitionid int64) (order []*models.BloodDialysisOrderCount, err error) {
  1779. db := XTReadDB().Table("xt_dialysis_order as o")
  1780. if partitionid == 0 {
  1781. 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.user_org_id = ? and x.schedule_date = o.dialysis_date and x.status = 1", orgid).Group("o.patient_id").Scan(&order)
  1782. }
  1783. if partitionid > 0 {
  1784. 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.user_org_id = ? and x.partition_id = ? and x.schedule_date = o.dialysis_date and x.status = 1", orgid, partitionid).Group("o.patient_id").Scan(&order)
  1785. }
  1786. return order, err
  1787. }
  1788. func UpdatePatientDialysisSolutionOne(patientid int64, orgid int64, prescription *models.DialysisPrescription, timenow int64) error {
  1789. err = XTWriteDB().Model(&prescription).Where("patient_id = ? and user_org_id = ? and record_date = ? ", patientid, orgid, timenow).Updates(map[string]interface{}{"dialyzer": prescription.Dialyzer, "anticoagulant": prescription.Anticoagulant, "mode_id": prescription.ModeId, "dialysis_duration_hour": prescription.DialysisDurationHour, "anticoagulant_shouji": prescription.AnticoagulantShouji, "anticoagulant_weichi": prescription.AnticoagulantWeichi, "anticoagulant_zongliang": prescription.AnticoagulantZongliang, "kalium": prescription.Kalium, "sodium": prescription.Sodium, "calcium": prescription.Calcium, "dialyzer_perfusion_apparatus": prescription.DialyzerPerfusionApparatus, "blood_access": prescription.BloodAccess, "dialysate_flow": prescription.DialysateFlow, "dialysate_temperature": prescription.DialysateTemperature, "dialysis_dialyszers": prescription.DialysisDialyszers, "dialysis_irrigation": prescription.DialysisIrrigation, "plasma_separator": prescription.PlasmaSeparator, "bilirubin_adsorption_column": prescription.BilirubinAdsorptionColumn, "oxygen_uptake": prescription.OxygenUptake, "oxygen_flow": prescription.OxygenFlow, "oxygen_time": prescription.OxygenTime, "dialysis_strainer": prescription.DialysisStrainer, "chaptalization": prescription.Chaptalization, "blood_access_part_id": prescription.BloodAccessPartId}).Error
  1790. return err
  1791. }
  1792. //func FindRemindAdvice(user_org_id int64, advice_name string, advice_desc string, template_id string, fre_type int64, patient_id int64, record_date int64) (advice models.DoctorAdvice, err error) {
  1793. // err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and record_date = ? and advice_name = ? and advice_desc = ? and template_id = ? and frequency_type = ? ", patient_id, user_org_id, record_date, advice_name, advice_desc, template_id, fre_type).First(&advice).Error
  1794. // return
  1795. //}
  1796. func FindRemindAdvice(user_org_id int64, advice_name string, advice_desc string, template_id string, fre_type int64, patient_id int64, record_date int64) (advice models.DoctorAdvice, err error) {
  1797. err = XTWriteDB().Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and record_date = ? and advice_name = ? and advice_desc = ? and template_id = ? and frequency_type = ? AND advice_type = 2 ", patient_id, user_org_id, record_date, advice_name, advice_desc, template_id, fre_type).First(&advice).Error
  1798. return
  1799. }
  1800. func UpdateScheduleByDeathTime(patientid int64, shcheduledate int64) error {
  1801. err := XTWriteDB().Model(models.XtSchedule{}).Where("patient_id = ? and schedule_date > ?", patientid, shcheduledate).Updates(map[string]interface{}{"status": 0}).Error
  1802. return err
  1803. }
  1804. func UpdateScheduleItemByPatientId(id int64) error {
  1805. err := XTWriteDB().Model(models.PatientScheduleTemplateItem{}).Where("patient_id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  1806. return err
  1807. }
  1808. func GetDoctorAdviceListThree(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64) (advices []*models.HisDoctorAdvice, total int64, err error) {
  1809. db := readDb.Table("his_doctor_advice_info as x").Where("x.status = 1")
  1810. table := UserReadDB().Table("sgj_user_admin_role as r")
  1811. fmt.Print("table", table)
  1812. if orgID > 0 {
  1813. db = db.Where("x.user_org_id=?", orgID)
  1814. }
  1815. if patientID > 0 {
  1816. db = db.Where("x.patient_id = ?", patientID)
  1817. }
  1818. if advice_type == 1 && advice_type > 0 {
  1819. db = db.Where("x.advice_type = ?", advice_type)
  1820. }
  1821. if advice_type == 3 && advice_type > 0 {
  1822. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1823. }
  1824. if advice_type == 4 && advice_type > 0 {
  1825. db = db.Where("x.advice_type = 4")
  1826. }
  1827. if stop == 1 {
  1828. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1829. } else if stop == 2 {
  1830. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1831. }
  1832. if start != 0 {
  1833. db = db.Where("x.start_time>=?", start)
  1834. }
  1835. if end != 0 {
  1836. db = db.Where("x.start_time<=?", end)
  1837. }
  1838. if len(keywords) > 0 {
  1839. likeKey := "%" + keywords + "%"
  1840. db = db.Where("x.advice_name LIKE ?", likeKey)
  1841. }
  1842. offset := (page - 1) * limit
  1843. err = db.Group("x.id").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, 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.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Order("start_time desc, groupno desc, advice_order desc, id asc").Scan(&advices).Error
  1844. fmt.Print("err", err)
  1845. return
  1846. }
  1847. func GetDoctorAdviceListFour(orgID int64, patientID int64, advice_type int64, stop int64, start int64, end int64, keywords string, limit int64, page int64) (advices []*models.HisDoctorAdvice, total int64, err error) {
  1848. db := readDb.Table("his_doctor_advice_info as x").Where("x.status = 1")
  1849. table := UserReadDB().Table("sgj_user_admin_role as r")
  1850. fmt.Println(table)
  1851. if orgID > 0 {
  1852. db = db.Where("x.user_org_id=?", orgID)
  1853. }
  1854. if patientID > 0 {
  1855. db = db.Where("x.patient_id = ?", patientID)
  1856. }
  1857. if advice_type == 1 {
  1858. db = db.Where("x.advice_type = ?", advice_type)
  1859. }
  1860. if advice_type == 3 && advice_type > 0 {
  1861. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1862. }
  1863. if advice_type == 4 && advice_type > 0 {
  1864. db = db.Where("x.advice_type = 4")
  1865. }
  1866. if stop == 1 {
  1867. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1868. } else if stop == 2 {
  1869. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1870. }
  1871. if start != 0 {
  1872. db = db.Where("x.start_time>=?", start)
  1873. }
  1874. if end != 0 {
  1875. db = db.Where("x.start_time<=?", end)
  1876. }
  1877. if len(keywords) > 0 {
  1878. likeKey := "%" + keywords + "%"
  1879. db = db.Where("x.advice_name LIKE ?", likeKey)
  1880. }
  1881. offset := (page - 1) * limit
  1882. err = db.Order("x.start_time desc").Group("x.start_time").Count(&total).Offset(offset).Limit(limit).Select("x.id, x.user_org_id, x.patient_id, x.advice_type, x.advice_date, x.record_date, x.start_time, x.advice_name,x.advice_desc, x.reminder_date, x.drug_spec, x.drug_spec_unit, x.single_dose, x.single_dose_unit, x.prescribing_number, x.prescribing_number_unit, x.delivery_way, x.execution_frequency, x.advice_doctor, x.status, x.created_time,x.updated_time, 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.execution_state, x.checker, x.check_state, x.check_time, x.groupno,x.remind_type,x.frequency_type,x.day_count,x.week_day,x.parent_id,r.user_name, IF(x.parent_id > 0, x.parent_id, x.id) as advice_order").Joins("Left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = x.advice_doctor").Scan(&advices).Error
  1883. fmt.Print("错误是什么", err)
  1884. return
  1885. }
  1886. func GetAllDeviceNumberByListSix(orgID int64) (number []*models.DeviceNumber, err error) {
  1887. err = readDb.Model(&models.DeviceNumber{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  1888. return number, err
  1889. }
  1890. func GetAllDeviceNumberByList(orgID int64) (number []*models.DeviceNumber, err error) {
  1891. redis := RedisClient()
  1892. defer redis.Close()
  1893. key := strconv.FormatInt(orgID, 10) + ":" + ":device_number_all"
  1894. number_info_str, _ := redis.Get(key).Result()
  1895. if len(number_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1896. err = readDb.Model(&models.DeviceNumber{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  1897. if err != nil {
  1898. if err == gorm.ErrRecordNotFound {
  1899. return nil, nil
  1900. } else {
  1901. return nil, err
  1902. }
  1903. } else {
  1904. if len(number) > 0 {
  1905. //缓存数据
  1906. device_number_json, err := json.Marshal(&number)
  1907. if err == nil {
  1908. redis.Set(key, device_number_json, time.Second*60*60*18)
  1909. }
  1910. }
  1911. return number, nil
  1912. }
  1913. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1914. json.Unmarshal([]byte(number_info_str), &number)
  1915. return number, nil
  1916. }
  1917. }
  1918. func GetAllPrescriptionByListTwo(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescription, err error) {
  1919. redis := RedisClient()
  1920. defer redis.Close()
  1921. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  1922. prescriptions_list_all, _ := redis.Get(key).Result()
  1923. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1924. if scheduleDate > 0 {
  1925. err = readDb.Model(&models.DialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  1926. } else {
  1927. err = readDb.Model(&models.DialysisPrescription{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&prescriptions).Error
  1928. }
  1929. if err != nil {
  1930. if err == gorm.ErrRecordNotFound {
  1931. return nil, nil
  1932. } else {
  1933. return nil, err
  1934. }
  1935. } else {
  1936. if len(prescriptions) > 0 {
  1937. //缓存数据
  1938. prescriptions_list_all, err := json.Marshal(&prescriptions)
  1939. if err == nil {
  1940. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  1941. }
  1942. }
  1943. return prescriptions, nil
  1944. }
  1945. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1946. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  1947. return prescriptions, nil
  1948. }
  1949. }
  1950. func GetAllAssessmentBeforesByListThree(orgID int64, scheduleDate int64) (assessmentBefores []*models.PredialysisEvaluation, err error) {
  1951. redis := RedisClient()
  1952. defer redis.Close()
  1953. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_befores_list_all"
  1954. assessment_befores_list_all, _ := redis.Get(key).Result()
  1955. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1956. if scheduleDate > 0 {
  1957. err = readDb.Model(&models.PredialysisEvaluation{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  1958. } else {
  1959. err = readDb.Model(&models.PredialysisEvaluation{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&assessmentBefores).Error
  1960. }
  1961. if err != nil {
  1962. if err == gorm.ErrRecordNotFound {
  1963. return nil, nil
  1964. } else {
  1965. return nil, err
  1966. }
  1967. } else {
  1968. if len(assessmentBefores) > 0 {
  1969. //缓存数据
  1970. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  1971. if err == nil {
  1972. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  1973. }
  1974. }
  1975. return assessmentBefores, nil
  1976. }
  1977. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1978. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  1979. return assessmentBefores, nil
  1980. }
  1981. }
  1982. func GetAllDialysisOrdersByListThree(orgID int64, scheduleDate int64) (dialysisOrders []*models.MonitorDialysisOrder, err error) {
  1983. redis := RedisClient()
  1984. defer redis.Close()
  1985. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  1986. dialysis_orders_list_all, _ := redis.Get(key).Result()
  1987. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1988. if scheduleDate > 0 {
  1989. err = readDb.Model(&models.MonitorDialysisOrder{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  1990. } else {
  1991. err = readDb.Model(&models.MonitorDialysisOrder{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ?", orgID).Find(&dialysisOrders).Error
  1992. }
  1993. if err != nil {
  1994. if err == gorm.ErrRecordNotFound {
  1995. return nil, nil
  1996. } else {
  1997. return nil, err
  1998. }
  1999. } else {
  2000. if len(dialysisOrders) > 0 {
  2001. //缓存数据
  2002. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2003. if err == nil {
  2004. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2005. }
  2006. }
  2007. return dialysisOrders, nil
  2008. }
  2009. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2010. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2011. return dialysisOrders, nil
  2012. }
  2013. }
  2014. func GetAllAssessmentAfterDislysisByListThree(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.AssessmentAfterDislysis, err error) {
  2015. redis := RedisClient()
  2016. defer redis.Close()
  2017. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  2018. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2019. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2020. if scheduleDate > 0 {
  2021. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2022. } else {
  2023. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&assessmentAfterDislysis).Error
  2024. }
  2025. if err != nil {
  2026. if err == gorm.ErrRecordNotFound {
  2027. return nil, nil
  2028. } else {
  2029. return nil, err
  2030. }
  2031. } else {
  2032. if len(assessmentAfterDislysis) > 0 {
  2033. //缓存数据
  2034. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2035. if err == nil {
  2036. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2037. }
  2038. }
  2039. return assessmentAfterDislysis, nil
  2040. }
  2041. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2042. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2043. return assessmentAfterDislysis, nil
  2044. }
  2045. }
  2046. func GetAllMonitorListThree(orgID int64, scheduleDate int64) (monitor []*models.MonitoringRecord, err error) {
  2047. redis := RedisClient()
  2048. defer redis.Close()
  2049. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":monitor_record_list_all"
  2050. monitor_record_list_all, _ := redis.Get(key).Result()
  2051. if len(monitor_record_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2052. if scheduleDate > 0 {
  2053. err = readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Find(&monitor).Error
  2054. } else {
  2055. err = readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND user_org_id = ?", orgID).Find(&monitor).Error
  2056. }
  2057. if err != nil {
  2058. if err == gorm.ErrRecordNotFound {
  2059. return nil, nil
  2060. } else {
  2061. return nil, err
  2062. }
  2063. } else {
  2064. if len(monitor) > 0 {
  2065. //缓存数据
  2066. assessment_after_dislysis__all, err := json.Marshal(&monitor)
  2067. if err == nil {
  2068. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2069. }
  2070. }
  2071. return monitor, nil
  2072. }
  2073. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2074. json.Unmarshal([]byte(monitor_record_list_all), &monitor)
  2075. return monitor, nil
  2076. }
  2077. }
  2078. func GetAllPatientListByListThree(orgID int64) (patients []*models.MonitorPatients, err error) {
  2079. redis := RedisClient()
  2080. defer redis.Close()
  2081. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2082. patient_info_str, _ := redis.Get(key).Result()
  2083. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2084. err = readDb.Model(&models.MonitorPatients{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  2085. if err != nil {
  2086. if err == gorm.ErrRecordNotFound {
  2087. return nil, nil
  2088. } else {
  2089. return nil, err
  2090. }
  2091. } else {
  2092. if len(patients) > 0 {
  2093. //缓存数据
  2094. patient_info_json, err := json.Marshal(&patients)
  2095. if err == nil {
  2096. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2097. }
  2098. }
  2099. return patients, nil
  2100. }
  2101. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2102. json.Unmarshal([]byte(patient_info_str), &patients)
  2103. return patients, nil
  2104. }
  2105. }
  2106. func GetAllPatientListByListSix(orgID int64, keywords string) (patients []*models.MonitorPatients, err error) {
  2107. db := readDb.Model(&models.MonitorPatients{}).Where("status = 1 and lapseto = 1")
  2108. likeKey := "%" + keywords + "%"
  2109. if orgID > 0 {
  2110. db = db.Where("user_org_id = ?", orgID)
  2111. }
  2112. if len(keywords) > 0 {
  2113. db = db.Where("name like ?", likeKey)
  2114. }
  2115. err = db.Find(&patients).Error
  2116. return patients, err
  2117. }
  2118. func GetAllDialysisOrdersByListTwo(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVM, err error) {
  2119. redis := RedisClient()
  2120. defer redis.Close()
  2121. key := strconv.FormatInt(orgID, 10) + ":" + ":dialysis_orders_list_all"
  2122. dialysis_orders_list_all, _ := redis.Get(key).Result()
  2123. if scheduleDate < 1669132800 {
  2124. fmt.Println("key233232232332323232", key)
  2125. redis.Set(key, "", time.Second)
  2126. }
  2127. fmt.Println("dialysis_orders_list_all", dialysis_orders_list_all)
  2128. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2129. err = readDb.Model(&MDialysisOrderVM{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  2130. if err != nil {
  2131. if err == gorm.ErrRecordNotFound {
  2132. return nil, nil
  2133. } else {
  2134. return nil, err
  2135. }
  2136. } else {
  2137. if len(dialysisOrders) > 0 {
  2138. //缓存数据
  2139. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2140. if err == nil {
  2141. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2142. }
  2143. }
  2144. return dialysisOrders, nil
  2145. }
  2146. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2147. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2148. return dialysisOrders, nil
  2149. }
  2150. }
  2151. func GetAllZoneByList(orgID int64) (zone []*models.DeviceZone, err error) {
  2152. redis := RedisClient()
  2153. defer redis.Close()
  2154. key := strconv.FormatInt(orgID, 10) + ":" + ":device_zone_all"
  2155. zone_info_str, _ := redis.Get(key).Result()
  2156. if len(zone_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2157. err = readDb.Model(&models.DeviceZone{}).Where("org_id=? and status=1", orgID).Find(&zone).Error
  2158. if err != nil {
  2159. if err == gorm.ErrRecordNotFound {
  2160. return nil, nil
  2161. } else {
  2162. return nil, err
  2163. }
  2164. } else {
  2165. if len(zone) > 0 {
  2166. //缓存数据
  2167. zone_number_json, err := json.Marshal(&zone)
  2168. if err == nil {
  2169. redis.Set(key, zone_number_json, time.Second*60*60*18)
  2170. }
  2171. }
  2172. return zone, nil
  2173. }
  2174. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2175. json.Unmarshal([]byte(zone_info_str), &zone)
  2176. return zone, nil
  2177. }
  2178. }
  2179. func GetAllDialysisOrdersByListNight(orgID int64, scheduleDate int64, pay_tyep int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  2180. if pay_tyep == 0 {
  2181. err = readDb.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  2182. }
  2183. //待下机
  2184. if pay_tyep == 3 {
  2185. err = readDb.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ? and end_time = 0", orgID, scheduleDate).Find(&dialysisOrders).Error
  2186. }
  2187. fmt.Println("pay_typeowowowoowwo", pay_tyep)
  2188. if pay_tyep == 7 {
  2189. err = readDb.Preload("DeviceNumber", "status = 1 AND org_id = ?'", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ? and url = ''", orgID, scheduleDate).Find(&dialysisOrders).Error
  2190. }
  2191. return dialysisOrders, err
  2192. }
  2193. func GetAllPrescriptionByListSix(orgID int64, scheduleDate int64) (prescriptions []*models.VMDialysisPrescription, err error) {
  2194. err = readDb.Model(&models.VMDialysisPrescription{}).Where("status= 1 and user_org_id = ? and record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2195. return prescriptions, err
  2196. //redis := RedisClient()
  2197. //defer redis.Close()
  2198. //key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2199. //redis.Set(key, "", time.Second)
  2200. //prescriptions_list_all, _ := redis.Get(key).Result()
  2201. //
  2202. //if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2203. // err = readDb.Model(&models.VMDialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2204. // if err != nil {
  2205. // if err == gorm.ErrRecordNotFound {
  2206. // return nil, nil
  2207. // } else {
  2208. // return nil, err
  2209. // }
  2210. // } else {
  2211. // if len(prescriptions) > 0 {
  2212. // //缓存数据
  2213. // prescriptions_list_all, err := json.Marshal(&prescriptions)
  2214. // if err == nil {
  2215. // redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2216. // }
  2217. // }
  2218. // return prescriptions, nil
  2219. // }
  2220. //} else { //缓存数据了数据,将redis缓存的json字符串转为map
  2221. // json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2222. // return prescriptions, nil
  2223. //}
  2224. }
  2225. func GetAllPrescriptionByList(orgID int64, scheduleDate int64) (prescriptions []*models.VMDialysisPrescription, err error) {
  2226. redis := RedisClient()
  2227. defer redis.Close()
  2228. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2229. redis.Set(key, "", time.Second)
  2230. prescriptions_list_all, _ := redis.Get(key).Result()
  2231. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2232. err = readDb.Model(&models.VMDialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2233. if err != nil {
  2234. if err == gorm.ErrRecordNotFound {
  2235. return nil, nil
  2236. } else {
  2237. return nil, err
  2238. }
  2239. } else {
  2240. if len(prescriptions) > 0 {
  2241. //缓存数据
  2242. prescriptions_list_all, err := json.Marshal(&prescriptions)
  2243. if err == nil {
  2244. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2245. }
  2246. }
  2247. return prescriptions, nil
  2248. }
  2249. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2250. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2251. return prescriptions, nil
  2252. }
  2253. }
  2254. func GetAllAssessmentBeforesByListOne(orgID int64, scheduleDate int64) (assessmentBefores []*models.VMPredialysisEvaluation, err error) {
  2255. redis := RedisClient()
  2256. defer redis.Close()
  2257. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_befores_list_all"
  2258. assessment_befores_list_all, _ := redis.Get(key).Result()
  2259. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2260. err = readDb.Model(&models.VMPredialysisEvaluation{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  2261. if err != nil {
  2262. if err == gorm.ErrRecordNotFound {
  2263. return nil, nil
  2264. } else {
  2265. return nil, err
  2266. }
  2267. } else {
  2268. if len(assessmentBefores) > 0 {
  2269. //缓存数据
  2270. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  2271. if err == nil {
  2272. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  2273. }
  2274. }
  2275. return assessmentBefores, nil
  2276. }
  2277. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2278. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  2279. return assessmentBefores, nil
  2280. }
  2281. }
  2282. func GetAllDialysisOrdersByListSix(orgID int64, scheduleDate int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  2283. err = readDb.Model(&models.VMMonitorDialysisOrder{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  2284. return dialysisOrders, err
  2285. }
  2286. func GetAllDialysisOrdersByListSeven(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVM, err error) {
  2287. err = readDb.Model(&MDialysisOrderVM{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  2288. return dialysisOrders, err
  2289. }
  2290. func GetAllDialysisOrdersByListOne(orgID int64, scheduleDate int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  2291. redis := RedisClient()
  2292. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  2293. fmt.Println("key22332323232323223", key)
  2294. dialysis_orders_list_all, _ := redis.Get(key).Result()
  2295. defer redis.Close()
  2296. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2297. err = readDb.Model(&models.VMMonitorDialysisOrder{}).Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  2298. if err != nil {
  2299. if err == gorm.ErrRecordNotFound {
  2300. return nil, nil
  2301. } else {
  2302. return nil, err
  2303. }
  2304. } else {
  2305. if len(dialysisOrders) > 0 {
  2306. //缓存数据
  2307. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2308. if err == nil {
  2309. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2310. }
  2311. }
  2312. return dialysisOrders, nil
  2313. }
  2314. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2315. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2316. return dialysisOrders, nil
  2317. }
  2318. }
  2319. func GetAllAssessmentAfterDislysisByListSix(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  2320. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status=1 and user_org_id = ? and assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2321. return assessmentAfterDislysis, err
  2322. //redis := RedisClient()
  2323. //defer redis.Close()
  2324. //key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  2325. //redis.Set(key, "", time.Second)
  2326. //assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2327. //
  2328. //if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2329. // err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2330. // if err != nil {
  2331. // if err == gorm.ErrRecordNotFound {
  2332. // return nil, nil
  2333. // } else {
  2334. // return nil, err
  2335. // }
  2336. // } else {
  2337. // if len(assessmentAfterDislysis) > 0 {
  2338. // //缓存数据
  2339. // assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2340. // if err == nil {
  2341. // redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2342. // }
  2343. // }
  2344. // return assessmentAfterDislysis, nil
  2345. // }
  2346. //} else { //缓存数据了数据,将redis缓存的json字符串转为map
  2347. // json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2348. // return assessmentAfterDislysis, nil
  2349. //}
  2350. }
  2351. func GetAllAssessmentAfterDislysisByListOne(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  2352. redis := RedisClient()
  2353. defer redis.Close()
  2354. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  2355. redis.Set(key, "", time.Second)
  2356. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2357. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2358. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2359. if err != nil {
  2360. if err == gorm.ErrRecordNotFound {
  2361. return nil, nil
  2362. } else {
  2363. return nil, err
  2364. }
  2365. } else {
  2366. if len(assessmentAfterDislysis) > 0 {
  2367. //缓存数据
  2368. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2369. if err == nil {
  2370. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2371. }
  2372. }
  2373. return assessmentAfterDislysis, nil
  2374. }
  2375. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2376. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2377. return assessmentAfterDislysis, nil
  2378. }
  2379. }
  2380. func GetAllMonitorList(orgID int64, scheduleDate int64) (monitor []*models.VMMonitoringRecord, err error) {
  2381. redis := RedisClient()
  2382. defer redis.Close()
  2383. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":monitor_record_list_all"
  2384. monitor_record_list_all, _ := redis.Get(key).Result()
  2385. fmt.Println("monitor_recorde_list", len(monitor_record_list_all))
  2386. if len(monitor_record_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2387. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2388. if err != nil {
  2389. if err == gorm.ErrRecordNotFound {
  2390. return nil, nil
  2391. } else {
  2392. return nil, err
  2393. }
  2394. } else {
  2395. if len(monitor) > 0 {
  2396. //缓存数据
  2397. assessment_after_dislysis__all, err := json.Marshal(&monitor)
  2398. if err == nil {
  2399. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2400. }
  2401. }
  2402. return monitor, nil
  2403. }
  2404. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2405. json.Unmarshal([]byte(monitor_record_list_all), &monitor)
  2406. return monitor, nil
  2407. }
  2408. }
  2409. func GetAllPatientListByListOne(orgID int64) (patients []*models.VMMonitorPatients, err error) {
  2410. redis := RedisClient()
  2411. defer redis.Close()
  2412. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2413. patient_info_str, _ := redis.Get(key).Result()
  2414. redis.Set(key, "", time.Second)
  2415. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2416. err = readDb.Model(&models.VMMonitorPatients{}).Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  2417. if err != nil {
  2418. if err == gorm.ErrRecordNotFound {
  2419. return nil, nil
  2420. } else {
  2421. return nil, err
  2422. }
  2423. } else {
  2424. if len(patients) > 0 {
  2425. //缓存数据
  2426. patient_info_json, err := json.Marshal(&patients)
  2427. if err == nil {
  2428. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2429. }
  2430. }
  2431. return patients, nil
  2432. }
  2433. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2434. json.Unmarshal([]byte(patient_info_str), &patients)
  2435. return patients, nil
  2436. }
  2437. }
  2438. func GetAllTreatModeByList(orgID int64) (treatement []*models.TreatmentMode, err error) {
  2439. redis := RedisClient()
  2440. defer redis.Close()
  2441. key := strconv.FormatInt(orgID, 10) + ":" + ":treatement_mode_all"
  2442. treatment_info_str, _ := redis.Get(key).Result()
  2443. if len(treatment_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2444. err = readDb.Model(&models.TreatmentMode{}).Where("status=1").Find(&treatement).Error
  2445. if err != nil {
  2446. if err == gorm.ErrRecordNotFound {
  2447. return nil, nil
  2448. } else {
  2449. return nil, err
  2450. }
  2451. } else {
  2452. if len(treatement) > 0 {
  2453. //缓存数据
  2454. patient_info_json, err := json.Marshal(&treatement)
  2455. if err == nil {
  2456. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2457. }
  2458. }
  2459. return treatement, nil
  2460. }
  2461. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2462. json.Unmarshal([]byte(treatment_info_str), &treatement)
  2463. return treatement, nil
  2464. }
  2465. }
  2466. func GetAllPatientListByListSeven(orgID int64) (patients []*MSchedualPatientVM, err error) {
  2467. err = readDb.Model(&MSchedualPatientVM{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  2468. return patients, nil
  2469. }
  2470. func GetAllPatientListByListTwo(orgID int64) (patients []*MSchedualPatientVM, err error) {
  2471. redis := RedisClient()
  2472. defer redis.Close()
  2473. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2474. patient_info_str, _ := redis.Get(key).Result()
  2475. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2476. err = readDb.Model(&MSchedualPatientVM{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  2477. if err != nil {
  2478. if err == gorm.ErrRecordNotFound {
  2479. return nil, nil
  2480. } else {
  2481. return nil, err
  2482. }
  2483. } else {
  2484. if len(patients) > 0 {
  2485. //缓存数据
  2486. patient_info_json, err := json.Marshal(&patients)
  2487. if err == nil {
  2488. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2489. }
  2490. }
  2491. return patients, nil
  2492. }
  2493. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2494. json.Unmarshal([]byte(patient_info_str), &patients)
  2495. return patients, nil
  2496. }
  2497. }
  2498. func GetAllDeviceNumberByListOne(orgID int64) (number []*MDeviceNumberVM, err error) {
  2499. redis := RedisClient()
  2500. defer redis.Close()
  2501. key := strconv.FormatInt(orgID, 10) + ":" + ":device_number_all"
  2502. number_info_str, _ := redis.Get(key).Result()
  2503. if len(number_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2504. err = readDb.Model(&MDeviceNumberVM{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  2505. if err != nil {
  2506. if err == gorm.ErrRecordNotFound {
  2507. return nil, nil
  2508. } else {
  2509. return nil, err
  2510. }
  2511. } else {
  2512. if len(number) > 0 {
  2513. //缓存数据
  2514. device_number_json, err := json.Marshal(&number)
  2515. if err == nil {
  2516. redis.Set(key, device_number_json, time.Second*60*60*18)
  2517. }
  2518. }
  2519. return number, nil
  2520. }
  2521. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2522. json.Unmarshal([]byte(number_info_str), &number)
  2523. return number, nil
  2524. }
  2525. }
  2526. func GetAllPrescriptionByListOne(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescriptionTwenty, err error) {
  2527. redis := RedisClient()
  2528. defer redis.Close()
  2529. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":prescriptions_list_all"
  2530. prescriptions_list_all, _ := redis.Get(key).Result()
  2531. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2532. err = readDb.Model(&models.DialysisPrescriptionTwenty{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2533. if err != nil {
  2534. if err == gorm.ErrRecordNotFound {
  2535. return nil, nil
  2536. } else {
  2537. return nil, err
  2538. }
  2539. } else {
  2540. if len(prescriptions) > 0 {
  2541. //缓存数据
  2542. prescriptions_list_all, err := json.Marshal(&prescriptions)
  2543. if err == nil {
  2544. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2545. }
  2546. }
  2547. return prescriptions, nil
  2548. }
  2549. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2550. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2551. return prescriptions, nil
  2552. }
  2553. }
  2554. func GetAllHisProjectPrescription(orgid int64, recorddate int64) (project []*models.HisPrescriptionProjectTwo, err error) {
  2555. err = XTReadDB().Where("user_org_id = ? and record_date = ? and status = 1", orgid, recorddate).Find(&project).Error
  2556. return project, err
  2557. }
  2558. func GetZoneCountList(orgid int64, schedule_date int64) (list []*models.XtScheduleSeven, err error) {
  2559. db := XTReadDB().Model(&list).Where("status = 1")
  2560. if orgid > 0 {
  2561. db = db.Where("user_org_id = ?", orgid)
  2562. }
  2563. if schedule_date > 0 {
  2564. db = db.Where("schedule_date =?", schedule_date)
  2565. }
  2566. err = db.Select("partition_id,count(id) as count").Group("partition_id").Scan(&list).Error
  2567. return list, err
  2568. }
  2569. func GetPatientDialysisLongSolution(patient_id int64, page int64, limit int64, orgid int64) (solution []*models.DialysisSolution, total int64, err error) {
  2570. db := XTReadDB().Model(&solution).Where("status = 1 and solution_status = 1")
  2571. if patient_id > 0 {
  2572. db = db.Where("patient_id = ?", patient_id)
  2573. }
  2574. if orgid > 0 {
  2575. db = db.Where("user_org_id = ?", orgid)
  2576. }
  2577. offset := (page - 1) * limit
  2578. err = db.Group("mode_id").Count(&total).Offset(offset).Limit(limit).Order("created_time desc").Find(&solution).Error
  2579. return solution, total, err
  2580. }
  2581. func GetDialysisSolutionDetailList(orgid int64, patient_id int64, mode_id int64, page int64, limit int64) (solution []*models.DialysisSolution, total int64, err error) {
  2582. offset := (page - 1) * limit
  2583. db := XTReadDB().Model(&solution).Where("status = 1")
  2584. if orgid > 0 {
  2585. db = db.Where("user_org_id = ?", orgid)
  2586. }
  2587. if patient_id > 0 {
  2588. db = db.Where("patient_id = ?", patient_id)
  2589. }
  2590. if mode_id > 0 {
  2591. db = db.Where("mode_id = ?", mode_id)
  2592. }
  2593. err = db.Count(&total).Offset(offset).Limit(limit).Find(&solution).Order("created_time desc").Error
  2594. return solution, total, err
  2595. }
  2596. func UpdateDialysisBefor(dislysis models.XtAssessmentBeforeDislysis, id int64) error {
  2597. err := XTWriteDB().Model(&models.XtAssessmentBeforeDislysis{}).Where("id = ? and status= 1", id).Update(map[string]interface{}{"systolic_blood_pressure": dislysis.SystolicBloodPressure, "diastolic_blood_pressure": dislysis.DiastolicBloodPressure}).Error
  2598. return err
  2599. }
  2600. func GetAllSchedulesByPatientInfo(org_id int64, patient_id int64, mode_id int64) ([]Schedule, error) {
  2601. var record []Schedule
  2602. loc, _ := time.LoadLocation("Local")
  2603. date, _ := time.ParseInLocation("2006-01-02 15:04:05", time.Now().Format("2006-01-02")+" 00:00:00", loc)
  2604. err := readDb.Model(&Schedule{}).Where("status = 1 AND schedule_date >= ? AND patient_id = ? AND user_org_id = ? AND mode_id= ?", date, patient_id, org_id, mode_id).Find(&record).Error
  2605. if err != nil {
  2606. if err == gorm.ErrRecordNotFound {
  2607. return nil, nil
  2608. } else {
  2609. return nil, err
  2610. }
  2611. }
  2612. return record, nil
  2613. }
  2614. func UpdateScheduleByExport(org_id int64, shcheduledate int64) error {
  2615. err := XTWriteDB().Model(models.XtSchedule{}).Where("user_org_id = ? and schedule_date > ?", org_id, shcheduledate).Updates(map[string]interface{}{"status": 0}).Error
  2616. return err
  2617. }
  2618. func GetContextScheduleListGroupPatientId(limit int64, page int64, startTime int64, endTime int64, org_id int64) (schedule []*models.ConScheduleList, total int64, err error) {
  2619. db := XTReadDB().Model(&schedule).Where("status=1")
  2620. offset := (page - 1) * limit
  2621. if startTime > 0 {
  2622. db = db.Where("schedule_date >=?", startTime)
  2623. }
  2624. if endTime > 0 {
  2625. db = db.Where("schedule_date<=?", endTime)
  2626. }
  2627. if org_id > 0 {
  2628. db = db.Where("user_org_id = ?", org_id)
  2629. }
  2630. err = db.Count(&total).Offset(offset).Limit(limit).Group("patient_id").Find(&schedule).Error
  2631. return schedule, total, err
  2632. }
  2633. func GetContextScheduleListPatientId(startTime int64, endTime int64, org_id int64) (schedule []*models.ConScheduleList, total int64, err error) {
  2634. db := XTReadDB().Model(&schedule).Where("status=1")
  2635. if startTime > 0 {
  2636. db = db.Where("schedule_date >=?", startTime)
  2637. }
  2638. if endTime > 0 {
  2639. db = db.Where("schedule_date<=?", endTime)
  2640. }
  2641. if org_id > 0 {
  2642. db = db.Where("user_org_id = ?", org_id)
  2643. }
  2644. err = db.Find(&schedule).Error
  2645. return schedule, total, err
  2646. }
  2647. func GetPatientAllagicList(patient_id int64, user_org_id int64) (list []*models.XtPatientAllergic, err error) {
  2648. err = XTReadDB().Where("patient_id = ? and user_org_id =? and status= 1", patient_id, user_org_id).Find(&list).Error
  2649. return list, err
  2650. }
  2651. func GetNewMonitorRecord(orgID int64, date int64, partition int64, patientId int64) ([]*models.NewMonitorDialysisScheduleList, error) {
  2652. var mds []*models.NewMonitorDialysisScheduleList
  2653. db := readDb.Model(&models.NewMonitorDialysisScheduleList{})
  2654. if date > 0 {
  2655. db = db.Where("schedule_date = ?", date)
  2656. }
  2657. if partition > 0 {
  2658. db = db.Where("partition_id = ?", partition)
  2659. }
  2660. if patientId > 0 {
  2661. db = db.Where("patient_id = ?", patientId)
  2662. }
  2663. err = db.Where("status = 1 and user_org_id = ?", orgID).Find(&mds).Error
  2664. return mds, err
  2665. }
  2666. func GetAdviceList(user_org_id int64, advice_date int64, pat_type int64) (advice []*models.VMDoctorAdviceForList, err error) {
  2667. if pat_type == 0 {
  2668. err = XTReadDB().Where("user_org_id = ? and advice_date = ? and status =7 and check_time = 0", user_org_id, advice_date).Find(&advice).Error
  2669. }
  2670. //待医嘱核对
  2671. if pat_type == 1 {
  2672. err = XTReadDB().Where("user_org_id = ? and advice_date = ? and status =1 and check_time = 0", user_org_id, advice_date).Find(&advice).Error
  2673. }
  2674. //待医嘱执行
  2675. if pat_type == 6 {
  2676. err = XTReadDB().Where("user_org_id = ? and advice_date = ? and status =1 and execution_state = 2", user_org_id, advice_date).Find(&advice).Error
  2677. }
  2678. return advice, err
  2679. }
  2680. func GetDobuleCheck(user_org_id int64, advice_date int64) (check []*models.VmDoubleCheck, err error) {
  2681. err = XTReadDB().Where("user_org_id = ? and check_date = ? and status = 1", user_org_id, advice_date).Find(&check).Error
  2682. return check, err
  2683. }
  2684. func GetTreatmentSummaryForList(user_org_id int64, advice_date int64) (summary []*models.VMTreatmentSummaryForList, err error) {
  2685. err = XTReadDB().Where("user_org_id =? and assessment_date = ? and status =1", user_org_id, advice_date).Find(&summary).Error
  2686. return summary, err
  2687. }
  2688. func GetDialysisInformationList(user_org_id int64, advice_date int64) (information []*models.NewDeviceInformation, err error) {
  2689. err = UserReadDB().Where("user_org_id =? and date = ? and status =1", user_org_id, advice_date).Find(&information).Error
  2690. return information, err
  2691. }
  2692. func GetNewAllMonitorList(orgID int64, scheduleDate int64, pat_type int64) (monitor []*models.VMMonitoringRecord, err error) {
  2693. if pat_type == 0 || pat_type == 9 {
  2694. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2695. }
  2696. if pat_type == 10 {
  2697. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ? AND accumulated_blood_volume = 0", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2698. }
  2699. if pat_type == 11 {
  2700. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ? AND symptom <> ''", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2701. }
  2702. if pat_type == 12 {
  2703. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ? AND dispose <> ''", orgID, scheduleDate).Order("monitoring_date asc").Find(&monitor).Error
  2704. }
  2705. return monitor, err
  2706. }
  2707. func GetBaseDrugByName(drug_name string, org_id int64) (models.BaseDrugLibSeven, error) {
  2708. libSeven := models.BaseDrugLibSeven{}
  2709. err := XTReadDB().Where("drug_name= ? and org_id = ? and status=1", drug_name, org_id).First(&libSeven).Error
  2710. return libSeven, err
  2711. }
  2712. func CreateDeviceValsualMap(valsualMap models.XtDeviceValsualMap) error {
  2713. err := XTWriteDB().Save(&valsualMap).Error
  2714. return err
  2715. }
  2716. func GetPatientSitemap(patient_id int64, user_org_id int64) (models.XtDeviceValsualMap, error) {
  2717. valsualMap := models.XtDeviceValsualMap{}
  2718. err := XTReadDB().Where("patient_id = ? and status =1 and user_org_id = ?", patient_id, user_org_id).Preload("XtDeviceValsualALeft", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualVLeft", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualMapFangxiangA", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualMapFangxiangV", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualFuzhuA", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualFuzhuV", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValsualTextArr", "status=1 and user_org_id =?", user_org_id).Preload("DeviceValusalMapArr", "status=1 and user_org_id =?", user_org_id).Preload("XtDeviceValusalMapVrr", "status=1 and user_org_id =?", user_org_id).Find(&valsualMap).Error
  2719. return valsualMap, err
  2720. }
  2721. func UpdateSiteMap(valsualMap models.XtDeviceValsualMap) error {
  2722. err := XTWriteDB().Save(&valsualMap).Error
  2723. return err
  2724. }
  2725. func CreateValsualAleft(left models.XtDeviceValsualALeft) error {
  2726. err := XTWriteDB().Create(&left).Error
  2727. return err
  2728. }
  2729. func UpdateValsualAleft(patient_id int64, user_org_id int64) error {
  2730. err := XTWriteDB().Model(&models.XtDeviceValsualALeft{}).Where("patient_id = ? and user_org_id = ? and status =1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2731. return err
  2732. }
  2733. func CreateValsualMapFangxiangA(a models.XtDeviceValsualMapFangxiangA) error {
  2734. err := XTWriteDB().Create(&a).Error
  2735. return err
  2736. }
  2737. func UpdateValsualMapFangxiangA(patient_id int64, user_org_id int64) error {
  2738. err := XTWriteDB().Model(&models.XtDeviceValsualMapFangxiangA{}).Where("patient_id = ? and user_org_id = ? and status =1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2739. return err
  2740. }
  2741. func CreateValsualMapFangxiangV(v models.XtDeviceValsualMapFangxiangV) error {
  2742. err := XTWriteDB().Create(&v).Error
  2743. return err
  2744. }
  2745. func UpdateValsualMapFangxiangV(patient_id int64, user_org_id int64) error {
  2746. err := XTWriteDB().Model(&models.XtDeviceValsualMapFangxiangV{}).Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2747. return err
  2748. }
  2749. func ValsualMapFangxiangA(patient_id int64, user_org_id int64) error {
  2750. err := XTWriteDB().Model(models.XtDeviceValsualMapFangxiangA{}).Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2751. return err
  2752. }
  2753. func CreateValSualFuzhA(a models.XtDeviceValsualFuzhuA) error {
  2754. err := XTReadDB().Create(&a).Error
  2755. return err
  2756. }
  2757. func UpdateValSualFuzhA(patient_id int64, user_org_id int64) error {
  2758. err := XTWriteDB().Model(&models.XtDeviceValsualFuzhuA{}).Where("patient_id =? and user_org_id = ? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2759. return err
  2760. }
  2761. func CreateVasuaLfuzhuV(v models.XtDeviceValsualFuzhuV) error {
  2762. err := XTReadDB().Create(&v).Error
  2763. return err
  2764. }
  2765. func UpdateVasuaLfuzhuV(patient_id int64, user_org_id int64) error {
  2766. err := XTWriteDB().Model(&models.XtDeviceValsualFuzhuV{}).Where("patient_id = ? and user_org_id =? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2767. return err
  2768. }
  2769. func CreateTextArr(arr models.XtDeviceValsualTextArr) error {
  2770. err := XTReadDB().Create(&arr).Error
  2771. return err
  2772. }
  2773. func UpdateTextArr(patient_id int64, user_org_id int64) error {
  2774. err := XTWriteDB().Model(&models.XtDeviceValsualTextArr{}).Where("patient_id = ? and user_org_id = ? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2775. return err
  2776. }
  2777. func CreateValsualVleft(left models.XtDeviceValsualVLeft) error {
  2778. err = XTReadDB().Create(&left).Error
  2779. return err
  2780. }
  2781. func UpdateValsualVleft(patient_id int64, user_org_id int64) error {
  2782. err := XTWriteDB().Model(&models.XtDeviceValsualVLeft{}).Where("patient_id = ? and user_org_id =? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2783. return err
  2784. }
  2785. func UpdateValusalMapArr(patient_id int64, user_org_id int64) error {
  2786. err := XTWriteDB().Model(&models.XtDeviceValusalMapArr{}).Where("patient_id =? and user_org_id =? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2787. return err
  2788. }
  2789. func CreateDeviceValusalMapArr(vrr models.XtDeviceValusalMapArr) error {
  2790. err := XTWriteDB().Create(&vrr).Error
  2791. return err
  2792. }
  2793. func UpdateDeviceValusalMapVrr(patient_id int64, user_org_id int64) error {
  2794. err := XTWriteDB().Model(&models.XtDeviceValusalMapVrr{}).Where("patient_id = ? and user_org_id =? and status=1", patient_id, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  2795. return err
  2796. }
  2797. func CreateDeviceValusalMapVrr(vrr models.XtDeviceValusalMapVrr) error {
  2798. err := XTWriteDB().Create(&vrr).Error
  2799. return err
  2800. }
  2801. func GetDialysisOrderNoExecution(start_nurse int64, user_org_id int64, timenow int64) (order []*models.XtDialysisOrder, err error) {
  2802. err = XTReadDB().Where("start_nurse = ? and user_org_id =? and status =1 and dialysis_date =?", start_nurse, user_org_id, timenow).Find(&order).Error
  2803. return order, err
  2804. }
  2805. func GetAdviceListNoExecution(advice_date int64, user_org_id int64, patientIds []int64) (advice []*models.NoDoctorAdvice, err error) {
  2806. db := XTReadDB().Model(advice).Where("status=1 and execution_state = 2 and advice_type = 2")
  2807. if advice_date > 0 {
  2808. db = db.Where("advice_date= ?", advice_date)
  2809. }
  2810. if user_org_id > 0 {
  2811. db = db.Where("user_org_id =?", user_org_id)
  2812. }
  2813. if len(patientIds) > 0 {
  2814. db = db.Where("patient_id in(?)", patientIds)
  2815. }
  2816. err = db.Select("patient_id,Count(id) as count,advice_date").Group("patient_id").Find(&advice).Error
  2817. return advice, err
  2818. }
  2819. func GetHisAdviceListNoExecution(advice_date int64, user_org_id int64, patientIds []int64) (advice []*models.HisNoDoctorAdvice, err error) {
  2820. db := XTReadDB().Model(advice).Where("status=1 and execution_state = 2")
  2821. if advice_date > 0 {
  2822. db = db.Where("advice_date= ?", advice_date)
  2823. }
  2824. if user_org_id > 0 {
  2825. db = db.Where("user_org_id =?", user_org_id)
  2826. }
  2827. if len(patientIds) > 0 {
  2828. db = db.Where("patient_id in(?)", patientIds)
  2829. }
  2830. err = db.Select("advice_date,patient_id,Count(id) as count").Group("patient_id").Find(&advice).Error
  2831. return advice, err
  2832. }
  2833. func GetProjectListNoExecution(record_date int64, user_org_id int64, patientIds []int64) (project []*models.NoHisPrescriptionProject, err error) {
  2834. db := XTReadDB().Model(&project).Where("status=1 and execution_state =0")
  2835. if record_date > 0 {
  2836. db = db.Where("record_date= ?", record_date)
  2837. }
  2838. if user_org_id > 0 {
  2839. db = db.Where("user_org_id =?", user_org_id)
  2840. }
  2841. if len(patientIds) > 0 {
  2842. db = db.Where("patient_id in(?)", patientIds)
  2843. }
  2844. err = db.Select("patient_id,Count(id) as count,record_date as advice_date").Group("patient_id").Find(&project).Error
  2845. return project, err
  2846. }
  2847. func GetPatientNoExecutionList(org_id int64, patientsIds []int64) (patient []*models.XtPatients, err error) {
  2848. err = XTReadDB().Where("user_org_id = ? and id in(?)", org_id, patientsIds).Find(&patient).Error
  2849. return patient, err
  2850. }
  2851. func FindHisConfig(org_id int64) (err error, config models.XtHisConfig) {
  2852. err = readDb.Model(&models.XtHisConfig{}).Where("status = 1 AND user_org_id = ?", org_id).First(&config).Error
  2853. return
  2854. }
  2855. func GetPatientDetailInformedConsent(patient_id int64, org_id int64) (models.XtPatients, error) {
  2856. patients := models.XtPatients{}
  2857. err := XTReadDB().Where("id = ? and status=1 and user_org_id =?", patient_id, org_id).Find(&patients).Error
  2858. return patients, err
  2859. }