patient_service.go 117KB

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