patient_service.go 103KB

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