patient_service.go 100KB

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