patient_service.go 109KB

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