patient_service.go 117KB

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