patient_service.go 106KB

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