patient_service.go 95KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447
  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").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) + ":" + ":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) + ":" + ":prescriptions_list_all"
  183. prescriptions_list_all, _ := redis.Get(key).Result()
  184. fmt.Println("铲毒23322332323232323", len(prescriptions_list_all))
  185. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  186. err = readDb.Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  187. if err != nil {
  188. if err == gorm.ErrRecordNotFound {
  189. if len(prescriptions) <= 0 {
  190. redis.Set(key, "null", time.Second*60*60*18)
  191. }
  192. return nil, nil
  193. } else {
  194. return nil, err
  195. }
  196. } else {
  197. if len(prescriptions) > 0 {
  198. //缓存数据
  199. prescriptions_list_all, err := json.Marshal(&prescriptions)
  200. if err == nil {
  201. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  202. }
  203. } else {
  204. redis.Set(key, "null", time.Second*60*60*18)
  205. }
  206. return prescriptions, nil
  207. }
  208. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  209. if prescriptions_list_all == "null" {
  210. return nil, nil
  211. } else {
  212. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  213. return prescriptions, nil
  214. }
  215. }
  216. }
  217. func GetAllAssessmentBeforesByList(orgID int64, scheduleDate int64) (assessmentBefores []*models.PredialysisEvaluationList, err error) {
  218. redis := RedisClient()
  219. defer redis.Close()
  220. key := strconv.FormatInt(orgID, 10) + ":" + ":assessment_befores_list_all"
  221. assessment_befores_list_all, _ := redis.Get(key).Result()
  222. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  223. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  224. if err != nil {
  225. if err == gorm.ErrRecordNotFound {
  226. if len(assessmentBefores) <= 0 {
  227. redis.Set(key, "null", time.Second*60*60*18)
  228. }
  229. return nil, nil
  230. } else {
  231. return nil, err
  232. }
  233. } else {
  234. if len(assessmentBefores) > 0 {
  235. //缓存数据
  236. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  237. if err == nil {
  238. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  239. }
  240. } else {
  241. redis.Set(key, "null", time.Second*60*60*18)
  242. }
  243. return assessmentBefores, nil
  244. }
  245. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  246. if assessment_befores_list_all == "null" {
  247. fmt.Println("尽力2323232", assessment_befores_list_all)
  248. return nil, nil
  249. } else {
  250. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  251. return assessmentBefores, nil
  252. }
  253. }
  254. }
  255. func GetAllDialysisOrdersByList(orgID int64, scheduleDate int64) (dialysisOrders []*models.MDialysisOrderForList, err error) {
  256. redis := RedisClient()
  257. defer redis.Close()
  258. key := strconv.FormatInt(orgID, 10) + ":" + ":dialysis_orders_list_all"
  259. dialysis_orders_list_all, _ := redis.Get(key).Result()
  260. fmt.Println("dialysis_orders_list_all23323223232323323232332", len(dialysis_orders_list_all))
  261. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  262. 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
  263. if err != nil {
  264. if err == gorm.ErrRecordNotFound {
  265. return nil, nil
  266. } else {
  267. return nil, err
  268. }
  269. } else {
  270. if len(dialysisOrders) > 0 {
  271. //缓存数据
  272. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  273. if err == nil {
  274. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  275. }
  276. } else {
  277. redis.Set(key, "null", time.Second*60*60*18)
  278. }
  279. return dialysisOrders, nil
  280. }
  281. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  282. if dialysis_orders_list_all == "null" {
  283. return nil, nil
  284. } else {
  285. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  286. return dialysisOrders, nil
  287. }
  288. }
  289. }
  290. func GetAllTreatmentSummarysByList(orgID int64, scheduleDate int64) (treatmentSummarys []*models.VMTreatmentSummaryForList, err error) {
  291. redis := RedisClient()
  292. defer redis.Close()
  293. key := strconv.FormatInt(orgID, 10) + ":" + ":treatment_summarys_list_all"
  294. treatment_summarys_all, _ := redis.Get(key).Result()
  295. if len(treatment_summarys_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  296. err = readDb.Model(&models.VMTreatmentSummaryForList{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&treatmentSummarys).Error
  297. if err != nil {
  298. if err == gorm.ErrRecordNotFound {
  299. if len(treatment_summarys_all) <= 0 {
  300. redis.Set(key, "null", time.Second*60*60*18)
  301. }
  302. return nil, nil
  303. } else {
  304. return nil, err
  305. }
  306. } else {
  307. if len(treatmentSummarys) > 0 {
  308. //缓存数据
  309. treatment_summarys_all, err := json.Marshal(&treatmentSummarys)
  310. if err == nil {
  311. redis.Set(key, treatment_summarys_all, time.Second*60*60*18)
  312. }
  313. } else {
  314. redis.Set(key, "null", time.Second*60*60*18)
  315. }
  316. return treatmentSummarys, nil
  317. }
  318. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  319. if treatment_summarys_all == "null" {
  320. return nil, nil
  321. } else {
  322. json.Unmarshal([]byte(treatment_summarys_all), &treatmentSummarys)
  323. return treatmentSummarys, nil
  324. }
  325. }
  326. }
  327. func GetAllAssessmentAfterDislysisByList(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  328. redis := RedisClient()
  329. defer redis.Close()
  330. key := strconv.FormatInt(orgID, 10) + ":" + ":assessment_after_dislysis_list_all"
  331. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  332. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  333. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  334. if err != nil {
  335. if err == gorm.ErrRecordNotFound {
  336. if len(assessmentAfterDislysis) <= 0 {
  337. redis.Set(key, "null", time.Second*60*60*18)
  338. }
  339. return nil, nil
  340. } else {
  341. return nil, err
  342. }
  343. } else {
  344. if len(assessmentAfterDislysis) > 0 {
  345. //缓存数据
  346. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  347. if err == nil {
  348. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  349. }
  350. } else {
  351. redis.Set(key, "null", time.Second*60*60*18)
  352. }
  353. return assessmentAfterDislysis, nil
  354. }
  355. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  356. if assessment_after_dislysis__all == "null" {
  357. return nil, nil
  358. } else {
  359. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  360. return assessmentAfterDislysis, nil
  361. }
  362. }
  363. }
  364. func GetAllHisAdvicesByList(orgID int64, scheduleDate int64) (hisAdvices []VMHisDoctorAdviceInfo, err error) {
  365. redis := RedisClient()
  366. defer redis.Close()
  367. key := strconv.FormatInt(orgID, 10) + ":" + ":his_advices_list_all"
  368. his_advices_all, _ := redis.Get(key).Result()
  369. if len(his_advices_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  370. err = readDb.Model(&VMHisDoctorAdviceInfo{}).Where("status = 1 AND user_org_id = ? AND advice_date = ?", orgID, scheduleDate).Find(&hisAdvices).Error
  371. if err != nil {
  372. if err == gorm.ErrRecordNotFound {
  373. if len(hisAdvices) <= 0 {
  374. redis.Set(key, "null", time.Second*60*60*18)
  375. }
  376. return nil, nil
  377. } else {
  378. return nil, err
  379. }
  380. } else {
  381. if len(hisAdvices) > 0 {
  382. //缓存数据
  383. his_advices_all, err := json.Marshal(&hisAdvices)
  384. if err == nil {
  385. redis.Set(key, his_advices_all, time.Second*60*60*18)
  386. }
  387. } else {
  388. redis.Set(key, "null", time.Second*60*60*18)
  389. }
  390. return hisAdvices, nil
  391. }
  392. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  393. if his_advices_all == "null" {
  394. return nil, nil
  395. } else {
  396. json.Unmarshal([]byte(his_advices_all), &hisAdvices)
  397. return hisAdvices, nil
  398. }
  399. }
  400. }
  401. func GetPatientListByUpdateTime(orgID int64, syncTime int64) (patients []*models.PatientListForFaceList, total int64, err error) {
  402. 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)
  403. db = db.Where("updated_time >= ?", syncTime)
  404. err = db.Count(&total).Find(&patients).Error
  405. return
  406. }
  407. func GetPatientListBySchedules(orgID int64, syncTime int64) (patients []*models.SchedulePatients, total int64, err error) {
  408. 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)
  409. err = db.Count(&total).Find(&patients).Error
  410. return
  411. }
  412. func GetPatientListBySchedulesFind(orgID int64, syncTime int64, patient_name string) (patients []*models.SchedulePatients, total int64, err error) {
  413. db := readDb.Model(&models.SchedulePatients{}).Where("user_org_id=? AND status=1 AND schedule_date = ?", orgID, syncTime).Preload("Patient", "status = 1 AND user_org_id = ? and name like ? ", orgID, "%"+patient_name+"%")
  414. err = db.Count(&total).Find(&patients).Error
  415. return
  416. }
  417. func GetPatientListById(orgID int64, patientId int64) (patients models.PatientListForFace, err error) {
  418. err = readDb.Model(&models.PatientListForFaceList{}).Where("user_org_id=? and id = ? and status=1", orgID, patientId).First(&patients).Error
  419. return
  420. }
  421. func GetPatientCount(orgID int64) (total int64) {
  422. readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1", orgID).Count(&total)
  423. return
  424. }
  425. func GetLapsetoPatientCount(orgID int64, lapseto int64) (total int64) {
  426. readDb.Model(&models.Patients{}).Where("user_org_id=? and lapseto=? and status=1", orgID, lapseto).Count(&total)
  427. return
  428. }
  429. func ChechLastDialysisNo(orgID int64) (dialysisNo int64) {
  430. var patient models.Patients
  431. err := readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=?", orgID).Order("dialysis_no desc").First(&patient).Error
  432. if err != nil {
  433. return
  434. }
  435. if patient.ID == 0 {
  436. return
  437. }
  438. dialysisNo, _ = strconv.ParseInt(patient.DialysisNo, 10, 64)
  439. return
  440. }
  441. func FindPatientByDialysisNo(orgID int64, dialysisNo string) (patient models.Patients, err error) {
  442. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and dialysis_no=?", orgID, dialysisNo).First(&patient).Error
  443. return
  444. }
  445. func FindPatientByIdCardNo(orgID int64, idCardNo string) (patient models.Patients, err error) {
  446. err = readDb.Model(&models.Patients{}).Where("status=1 and user_org_id=? and id_card_no=?", orgID, idCardNo).First(&patient).Error
  447. return
  448. }
  449. func FindPatientByMobile(orgID int64, mobile string) (patient models.Patients, err error) {
  450. err = readDb.Model(&models.Patients{}).Where("phone=? and user_org_id=? and status=1", mobile, orgID).First(&patient).Error
  451. return
  452. }
  453. func FindPatientById(orgID int64, id int64) (patient models.Patients, err error) {
  454. err = readDb.Model(&models.Patients{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  455. return
  456. }
  457. func FindPatientByIdTwo(orgID int64, id int64) (patient models.XtPatientsNew, err error) {
  458. err = readDb.Model(&models.XtPatientsNew{}).Where("blood_id = ? and user_org_id = ? and status =1", id, orgID).First(&patient).Error
  459. return
  460. }
  461. func FindPatientByIdWithDiseases(orgID int64, id int64) (patient models.Patients, err error) {
  462. redis := RedisClient()
  463. defer redis.Close()
  464. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(id, 10) + ":patient_info"
  465. patient_info_str, _ := redis.Get(key).Result()
  466. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  467. err = readDb.Model(&models.Patients{}).Preload("Contagions", "status = 1").Preload("Diseases", "status = 1").Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  468. if err != nil {
  469. if err == gorm.ErrRecordNotFound {
  470. return patient, nil
  471. } else {
  472. return patient, err
  473. }
  474. } else {
  475. if patient.ID > 0 {
  476. //缓存数据
  477. patient_info_json, err := json.Marshal(&patient)
  478. if err == nil {
  479. redis.Set(key, patient_info_json, time.Second*60*60*18)
  480. }
  481. } else {
  482. redis.Set(key, "null", time.Second*60*60*18)
  483. }
  484. return patient, nil
  485. }
  486. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  487. if patient_info_str == "null" {
  488. return patient, nil
  489. } else {
  490. json.Unmarshal([]byte(patient_info_str), &patient)
  491. return patient, nil
  492. }
  493. }
  494. }
  495. func FindPatientWithDeviceByNo(orgID int64, no string, time int64) (patient models.SchedualPatient2, err error) {
  496. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  497. return db.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  498. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  499. Where("user_org_id = ? AND schedule_date = ? ", orgID, time)
  500. }).Preload("InfectiousDiseases", "status = 1").Where("user_org_id=? and dialysis_no = ? and status=1", orgID, no).First(&patient).Error
  501. return
  502. }
  503. func CreatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  504. user, _ := GetSgjUserByMobild(patient.Phone)
  505. customer, _ := GetSgjCoustomerByMobile(patient.UserOrgId, patient.Phone)
  506. utx := writeDb.Begin()
  507. btx := writeUserDb.Begin()
  508. if user.ID == 0 {
  509. user.Mobile = patient.Phone
  510. user.Avatar = patient.Avatar
  511. user.AvatarThumb = patient.Avatar
  512. user.Birthday = patient.Birthday
  513. user.Username = patient.Name
  514. user.Gender = patient.Gender
  515. user.Sources = 11
  516. user.Introduce = patient.Remark
  517. user.Status = 1
  518. user.UpdatedTime = patient.UpdatedTime
  519. user.CreatedTime = patient.CreatedTime
  520. err = btx.Create(&user).Error
  521. if err != nil {
  522. utx.Rollback()
  523. btx.Rollback()
  524. return
  525. }
  526. }
  527. patient.UserId = user.ID
  528. if customer == nil {
  529. err = btx.Create(&models.SgjCustomer{
  530. UserOrgId: patient.UserOrgId,
  531. UserId: user.ID,
  532. Mobile: patient.Phone,
  533. Name: patient.Name,
  534. Gender: patient.Gender,
  535. Birthday: patient.Birthday,
  536. Sources: 11,
  537. Status: 1,
  538. CreatedTime: patient.CreatedTime,
  539. UpdatedTime: patient.UpdatedTime,
  540. Avatar: patient.Avatar,
  541. Remark: patient.Remark,
  542. }).Error
  543. if err != nil {
  544. utx.Rollback()
  545. btx.Rollback()
  546. return
  547. }
  548. }
  549. err = utx.Create(patient).Error
  550. if err != nil {
  551. utx.Rollback()
  552. btx.Rollback()
  553. return
  554. }
  555. var lapseto models.PatientLapseto
  556. lapseto.PatientId = patient.ID
  557. lapseto.LapsetoType = patient.Lapseto
  558. lapseto.CreatedTime = patient.CreatedTime
  559. lapseto.UpdatedTime = patient.CreatedTime
  560. lapseto.Status = 1
  561. lapseto.LapsetoTime = patient.CreatedTime
  562. err = utx.Create(&lapseto).Error
  563. if err != nil {
  564. utx.Rollback()
  565. btx.Rollback()
  566. return
  567. }
  568. if len(contagions) > 0 {
  569. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  570. insertParams := make([]string, 0)
  571. insertData := make([]interface{}, 0)
  572. for _, contagion := range contagions {
  573. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  574. insertData = append(insertData, patient.ID)
  575. insertData = append(insertData, contagion)
  576. insertData = append(insertData, 1)
  577. insertData = append(insertData, patient.CreatedTime)
  578. insertData = append(insertData, patient.UpdatedTime)
  579. }
  580. thisSQL += strings.Join(insertParams, ", ")
  581. err = utx.Exec(thisSQL, insertData...).Error
  582. if err != nil {
  583. utx.Rollback()
  584. btx.Rollback()
  585. return
  586. }
  587. }
  588. if len(diseases) > 0 {
  589. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  590. insertParams := make([]string, 0)
  591. insertData := make([]interface{}, 0)
  592. for _, disease := range diseases {
  593. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  594. insertData = append(insertData, patient.ID)
  595. insertData = append(insertData, disease)
  596. insertData = append(insertData, 1)
  597. insertData = append(insertData, patient.CreatedTime)
  598. insertData = append(insertData, patient.UpdatedTime)
  599. }
  600. thisSQL += strings.Join(insertParams, ", ")
  601. err = utx.Exec(thisSQL, insertData...).Error
  602. if err != nil {
  603. utx.Rollback()
  604. btx.Rollback()
  605. return
  606. }
  607. }
  608. utx.Commit()
  609. btx.Commit()
  610. return
  611. }
  612. func GetLastPatientData(orgid int64) (models.Patients, error) {
  613. patients := models.Patients{}
  614. err := XTReadDB().Model(&patients).Where("user_org_id = ? and status = 1", orgid).Last(&patients).Error
  615. return patients, err
  616. }
  617. func CreatePatientsNew(patientsNew *models.XtPatientsNew) error {
  618. err := XTWriteDB().Model(&patientsNew).Create(&patientsNew).Error
  619. return err
  620. }
  621. func EditPatientLapseto(patient *models.Patients, lapseto *models.PatientLapseto) (err error) {
  622. utx := writeDb.Begin()
  623. 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
  624. //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
  625. if err != nil {
  626. utx.Rollback()
  627. return
  628. }
  629. err = utx.Create(lapseto).Error
  630. if err != nil {
  631. utx.Rollback()
  632. return
  633. }
  634. // 删除排班和排班模板信息
  635. if lapseto.LapsetoType == 2 {
  636. now := time.Now()
  637. deleteScheduleErr := utx.Model(&models.PatientSchedule{}).Where("patient_id = ? AND schedule_date >= ? AND status = 1", patient.ID, lapseto.LapsetoTime).Updates(map[string]interface{}{
  638. "status": 0,
  639. "updated_time": now.Unix(),
  640. }).Error
  641. if deleteScheduleErr != nil {
  642. utx.Rollback()
  643. err = deleteScheduleErr
  644. return
  645. }
  646. deleteSchTempItemErr := utx.Model(&models.PatientScheduleTemplateItem{}).Where("patient_id = ? AND status = 1", patient.ID).Updates(map[string]interface{}{
  647. "status": 0,
  648. "mtime": now.Unix(),
  649. }).Error
  650. if deleteSchTempItemErr != nil {
  651. utx.Rollback()
  652. err = deleteSchTempItemErr
  653. return
  654. }
  655. }
  656. utx.Commit()
  657. return
  658. }
  659. func UpdatePatient(patient *models.Patients, contagions []int64, diseases []int64) (err error) {
  660. // if len(contagions) > 0 || len(diseases) > 0 {
  661. utx := writeDb.Begin()
  662. err = utx.Save(patient).Error
  663. if err != nil {
  664. utx.Rollback()
  665. return
  666. }
  667. err = utx.Model(&models.InfectiousDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  668. fmt.Println("err", err)
  669. if err != nil {
  670. utx.Rollback()
  671. return
  672. }
  673. if len(contagions) > 0 && patient.IsInfectious == 2 {
  674. thisSQL := "INSERT INTO xt_patients_infectious_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  675. insertParams := make([]string, 0)
  676. insertData := make([]interface{}, 0)
  677. for _, contagion := range contagions {
  678. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  679. insertData = append(insertData, patient.ID)
  680. insertData = append(insertData, contagion)
  681. insertData = append(insertData, 1)
  682. insertData = append(insertData, time.Now().Unix())
  683. insertData = append(insertData, time.Now().Unix())
  684. }
  685. thisSQL += strings.Join(insertParams, ", ")
  686. err = utx.Exec(thisSQL, insertData...).Error
  687. if err != nil {
  688. utx.Rollback()
  689. return
  690. }
  691. }
  692. err = utx.Model(&models.ChronicDiseases{}).Where("patient_id=?", patient.ID).Update(map[string]interface{}{"Status": 2, "UpdatedTime": patient.UpdatedTime}).Error
  693. if err != nil {
  694. utx.Rollback()
  695. return
  696. }
  697. if len(diseases) > 0 {
  698. thisSQL := "INSERT INTO xt_patients_chronic_diseases (patient_id, disease_id, status, created_time, updated_time) VALUES "
  699. insertParams := make([]string, 0)
  700. insertData := make([]interface{}, 0)
  701. for _, disease := range diseases {
  702. insertParams = append(insertParams, "(?, ?, ?, ?, ?)")
  703. insertData = append(insertData, patient.ID)
  704. insertData = append(insertData, disease)
  705. insertData = append(insertData, 1)
  706. insertData = append(insertData, time.Now().Unix())
  707. insertData = append(insertData, time.Now().Unix())
  708. }
  709. thisSQL += strings.Join(insertParams, ", ")
  710. err = utx.Exec(thisSQL, insertData...).Error
  711. if err != nil {
  712. utx.Rollback()
  713. return
  714. }
  715. }
  716. utx.Commit()
  717. // } else {
  718. // err = writeDb.Save(patient).Error
  719. // }
  720. return
  721. }
  722. func UpdatePatientLapseto(patientid int64, lapseto models.PatientLapseto) error {
  723. 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
  724. return err
  725. }
  726. func UpdatepatientTwo(patientsNew *models.XtPatientsNew, id int64) error {
  727. 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,
  728. "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
  729. return err
  730. }
  731. func GetLastInfectionRecord(id int64, org_id int64, project_id int64, date int64) (inspection models.Inspection, err error) {
  732. 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
  733. return
  734. }
  735. func GetAllInfectionRecord(date int64, org_id int64, patient_id int64, project_id int64) (inspection []*models.Inspection, err error) {
  736. 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
  737. return
  738. }
  739. func GetPatientDiseases(id int64) []int64 {
  740. var dis []models.ChronicDiseases
  741. ids := make([]int64, 0)
  742. err := readDb.Model(&models.ChronicDiseases{}).Where("patient_id=? and status=1", id).Find(&dis).Error
  743. if err != nil || len(dis) == 0 {
  744. return ids
  745. }
  746. for _, item := range dis {
  747. ids = append(ids, item.DiseaseId)
  748. }
  749. return ids
  750. }
  751. func GetPatientContagions(id int64) []int64 {
  752. var cis []models.InfectiousDiseases
  753. ids := make([]int64, 0)
  754. err := readDb.Model(&models.InfectiousDiseases{}).Where("patient_id=? and status=1", id).Find(&cis).Error
  755. if err != nil || len(cis) == 0 {
  756. return ids
  757. }
  758. for _, item := range cis {
  759. ids = append(ids, item.DiseaseId)
  760. }
  761. return ids
  762. }
  763. func FindPatientDialysisSolutionByMode(orgID int64, patientID, modeId int64) (solution models.DialysisSolution, err error) {
  764. 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
  765. return
  766. }
  767. func FindPatientDialysisSolution(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  768. err = readDb.Model(&models.DialysisSolution{}).Where("id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  769. return
  770. }
  771. func FindPatientDialysisSolutionChild(orgID int64, id int64) (solution models.DialysisSolution, err error) {
  772. err = readDb.Model(&models.DialysisSolution{}).Where("parent_id = ? and status=1 and user_org_id=?", id, orgID).First(&solution).Error
  773. return
  774. }
  775. func CreatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  776. err = writeDb.Create(solution).Error
  777. return
  778. }
  779. func UpdatePatientDialysisSolution(solution *models.DialysisSolution) (err error) {
  780. err = writeDb.Save(solution).Error
  781. return
  782. }
  783. //GetPatientDialysisSolutionList 返回患者透析方案的列表
  784. func GetPatientDialysisSolutionList(orgID int64, patientID int64, page, limit int64) (solutions []*models.DialysisSolution, total int64, err error) {
  785. offset := (page - 1) * limit
  786. db := readDb.Table("xt_dialysis_solution as ds").Where("ds.status=1")
  787. if orgID > 0 {
  788. db = db.Where("ds.user_org_id=?", orgID)
  789. }
  790. if patientID > 0 {
  791. db = db.Where("ds.patient_id=?", patientID)
  792. }
  793. db = db.Count(&total).Offset(offset).Limit(limit)
  794. err = db.Order("id desc").Find(&solutions).Error
  795. if err != nil {
  796. return
  797. }
  798. if len(solutions) > 0 {
  799. nilNameIds := make([]int64, 0)
  800. for _, solution := range solutions {
  801. if len(solution.ModeName) == 0 {
  802. nilNameIds = append(nilNameIds, solution.ModeId)
  803. }
  804. }
  805. if len(nilNameIds) > 0 {
  806. var modes []*models.TreatmentMode
  807. err = readDb.Model(&models.TreatmentMode{}).Where("id IN (?)", nilNameIds).Find(&modes).Error
  808. if err != nil {
  809. return
  810. }
  811. modesMap := make(map[int64]models.TreatmentMode, 0)
  812. for _, mode := range modes {
  813. modesMap[mode.ID] = *mode
  814. }
  815. for index, solution := range solutions {
  816. if _, exixt := modesMap[solution.ModeId]; exixt && len(solution.ModeName) == 0 {
  817. solutions[index].ModeName = modesMap[solution.ModeId].Name
  818. }
  819. }
  820. }
  821. }
  822. return
  823. }
  824. //GetPatientDryWeightAdjustList 返回患者调整干体重的列表
  825. func GetPatientDryWeightAdjustList(orgID int64, patientID int64, page int64, limit int64) (weights []*models.DryWeightAdjust, total int64, err error) {
  826. db := readDb.Table("xt_dry_weight_adjust as dwa").Where("dwa.status=1")
  827. if orgID > 0 {
  828. db = db.Where("dwa.user_org_id=?", orgID)
  829. }
  830. if patientID > 0 {
  831. db = db.Where("dwa.patient_id=?", patientID)
  832. }
  833. 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")
  834. if page > 0 && limit > 0 {
  835. offset := (page - 1) * limit
  836. db = db.Offset(offset).Limit(limit)
  837. }
  838. err = db.Find(&weights).Error
  839. return
  840. }
  841. func CreateDryWeightAdjust(m *models.DryWeightAdjust) (err error) {
  842. err = writeDb.Create(m).Error
  843. return
  844. }
  845. func FindPatientLastDryWeightAdjust(orgID int64, id int64) (weight models.DryWeightAdjust, err error) {
  846. err = readDb.Model(&models.DryWeightAdjust{}).Where("user_org_id=? and patient_id=? and status=1", orgID, id).Order("id desc").First(&weight).Error
  847. return
  848. }
  849. func CreateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  850. return writeDb.Create(m).Error
  851. }
  852. func GetMaxAdviceGroupID(orgId int64) (group int64) {
  853. var advice models.DoctorAdvice
  854. err := readDb.Table("xt_doctor_advice").Where("user_org_id=?", orgId).Select("max(groupno) as groupno").First(&advice).Error
  855. if err != nil {
  856. fmt.Println(err)
  857. group = 0
  858. }
  859. group = advice.GroupNo
  860. return
  861. }
  862. func CreateGroupAdvice(orgId int64, group int64, advices []*models.GroupAdvice) (err error) {
  863. if group == 0 {
  864. group = GetMaxAdviceGroupID(orgId) + 1
  865. }
  866. tx := writeDb.Begin()
  867. defer func() {
  868. if r := recover(); r != nil {
  869. tx.Rollback()
  870. }
  871. }()
  872. for _, advice := range advices {
  873. advice.GroupNo = group
  874. if err = tx.Create(advice).Error; err != nil {
  875. tx.Rollback()
  876. return
  877. }
  878. }
  879. tx.Commit()
  880. return
  881. }
  882. func CreateMGroupAdvice(orgId int64, advices []*models.GroupAdvice, groupNo int64) (list []*models.GroupAdvice, err error) {
  883. if groupNo <= 0 {
  884. group := GetMaxAdviceGroupID(orgId)
  885. groupNo = group + 1
  886. }
  887. tx := writeDb.Begin()
  888. defer func() {
  889. if r := recover(); r != nil {
  890. tx.Rollback()
  891. }
  892. }()
  893. for _, advice := range advices {
  894. advice.GroupNo = groupNo
  895. if err = tx.Create(advice).Error; err != nil {
  896. tx.Rollback()
  897. return
  898. }
  899. list = append(list, advice)
  900. if len(advice.Children) > 0 {
  901. for _, child := range advice.Children {
  902. child.GroupNo = groupNo
  903. child.ParentId = advice.ID
  904. fmt.Println(child)
  905. if err = tx.Create(&child).Error; err != nil {
  906. tx.Rollback()
  907. return
  908. }
  909. list = append(list, child)
  910. }
  911. }
  912. }
  913. tx.Commit()
  914. return
  915. }
  916. func UpdateDoctorAdvice(m *models.DoctorAdvice) (err error) {
  917. return writeDb.Save(m).Error
  918. }
  919. func StopGroupAdvice(orgId int64, groupNo int64, m *models.DoctorAdvice) (err error) {
  920. 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
  921. if err != nil {
  922. return
  923. }
  924. return
  925. }
  926. func StopDoctorAdvice(m *models.DoctorAdvice) (err error) {
  927. ut := writeDb.Begin()
  928. err = ut.Save(m).Error
  929. if err != nil {
  930. ut.Rollback()
  931. return
  932. }
  933. 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
  934. if err != nil {
  935. ut.Rollback()
  936. return
  937. }
  938. ut.Commit()
  939. return
  940. }
  941. func DeleteSolution(m *models.DialysisSolution) (err error) {
  942. if m.ParentId > 0 {
  943. return writeDb.Save(m).Error
  944. } else {
  945. ut := writeDb.Begin()
  946. err = ut.Save(m).Error
  947. if err != nil {
  948. ut.Rollback()
  949. return
  950. }
  951. err = ut.Model(&models.DialysisSolution{}).Where("parent_id=?", m.ID).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0}).Error
  952. if err != nil {
  953. ut.Rollback()
  954. return
  955. }
  956. ut.Commit()
  957. }
  958. return
  959. }
  960. func DeleteDoctorAdvice(m *models.DoctorAdvice) (err error) {
  961. if m.ParentId > 0 {
  962. return writeDb.Save(m).Error
  963. } else {
  964. ut := writeDb.Begin()
  965. err = ut.Save(m).Error
  966. if err != nil {
  967. ut.Rollback()
  968. return
  969. }
  970. 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
  971. if err != nil {
  972. ut.Rollback()
  973. return
  974. }
  975. ut.Commit()
  976. }
  977. return
  978. }
  979. func DeleteGroupAdvice(orgId int64, groupNo int64, admin_user_id int64) (err error) {
  980. 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
  981. if err != nil {
  982. return
  983. }
  984. return
  985. }
  986. func FindDoctorAdvice(orgID, id int64) (advice models.DoctorAdvice, err error) {
  987. err = readDb.Model(&models.DoctorAdvice{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  988. return
  989. }
  990. func FindHisDoctorAdvice(orgID, id int64) (advice models.HisDoctorAdviceInfo, err error) {
  991. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id = ? and user_org_id=? and status = 1", id, orgID).First(&advice).Error
  992. return
  993. }
  994. func FindDoctorAdviceByGroupNo(orgID, groupNo int64) (advice models.DoctorAdvice, err error) {
  995. err = readDb.Model(&models.DoctorAdvice{}).Where("groupno = ? and user_org_id=? and status = 1", groupNo, orgID).First(&advice).Error
  996. return
  997. }
  998. func GetDoctorAdviceList(orgID, patientID, advice_type, stop, start, end int64, keywords string) (advices []*models.DoctorAdvices, total int64, err error) {
  999. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1000. table := UserReadDB().Table("sgj_user_admin_role as r")
  1001. fmt.Print("table", table)
  1002. if orgID > 0 {
  1003. db = db.Where("x.user_org_id=?", orgID)
  1004. }
  1005. if patientID > 0 {
  1006. db = db.Where("x.patient_id = ?", patientID)
  1007. }
  1008. if advice_type > 0 {
  1009. db = db.Where("x.advice_type = ?", advice_type)
  1010. } else if advice_type == 0 {
  1011. db = db.Where("x.advice_type in (?)", []int{1, 3})
  1012. }
  1013. if stop == 1 {
  1014. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1015. } else if stop == 2 {
  1016. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1017. }
  1018. if start != 0 {
  1019. db = db.Where("x.start_time>=?", start)
  1020. }
  1021. if end != 0 {
  1022. db = db.Where("start_time<=?", end)
  1023. }
  1024. if len(keywords) > 0 {
  1025. likeKey := "%" + keywords + "%"
  1026. db = db.Where("x.advice_name LIKE ?", likeKey)
  1027. }
  1028. 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
  1029. fmt.Print("err", err)
  1030. return
  1031. }
  1032. func GetDoctorAdviceListOne(orgID, patientID, advice_type, stop, start, end int64, keywords string, page int64, limit int64) (advices []*models.DoctorAdvices, total int64, err error) {
  1033. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1034. table := UserReadDB().Table("sgj_user_admin_role as r")
  1035. fmt.Print("table", table)
  1036. if orgID > 0 {
  1037. db = db.Where("x.user_org_id=?", orgID)
  1038. }
  1039. if patientID > 0 {
  1040. db = db.Where("x.patient_id = ?", patientID)
  1041. }
  1042. if advice_type == 1 && advice_type > 0 {
  1043. db = db.Where("x.advice_type = ?", advice_type)
  1044. }
  1045. if advice_type == 3 && advice_type > 0 {
  1046. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1047. }
  1048. if advice_type == 4 && advice_type > 0 {
  1049. db = db.Where("x.advice_type = 4")
  1050. }
  1051. if stop == 1 {
  1052. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1053. } else if stop == 2 {
  1054. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1055. }
  1056. if start != 0 {
  1057. db = db.Where("x.start_time>=?", start)
  1058. }
  1059. if end != 0 {
  1060. db = db.Where("start_time<=?", end)
  1061. }
  1062. if len(keywords) > 0 {
  1063. likeKey := "%" + keywords + "%"
  1064. db = db.Where("x.advice_name LIKE ?", likeKey)
  1065. }
  1066. offset := (page - 1) * limit
  1067. 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
  1068. fmt.Print("err", err)
  1069. return
  1070. }
  1071. 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) {
  1072. db := readDb.Table("xt_doctor_advice as x").Where("x.status = 1")
  1073. table := UserReadDB().Table("sgj_user_admin_role as r")
  1074. fmt.Println(table)
  1075. if orgID > 0 {
  1076. db = db.Where("x.user_org_id=?", orgID)
  1077. }
  1078. if patientID > 0 {
  1079. db = db.Where("x.patient_id = ?", patientID)
  1080. }
  1081. if advice_type == 1 {
  1082. db = db.Where("x.advice_type = ?", advice_type)
  1083. }
  1084. if advice_type == 3 && advice_type > 0 {
  1085. db = db.Where("x.advice_type = 2 or x.advice_type = 3")
  1086. }
  1087. if advice_type == 4 && advice_type > 0 {
  1088. db = db.Where("x.advice_type = 4")
  1089. }
  1090. if stop == 1 {
  1091. db = db.Where("(x.stop_state=? or x.execution_state=?)", stop, stop)
  1092. } else if stop == 2 {
  1093. db = db.Where("x.stop_state=? and x.execution_state=?", stop, stop)
  1094. }
  1095. if start != 0 {
  1096. db = db.Where("x.start_time>=?", start)
  1097. }
  1098. if end != 0 {
  1099. db = db.Where("start_time<=?", end)
  1100. }
  1101. if len(keywords) > 0 {
  1102. likeKey := "%" + keywords + "%"
  1103. db = db.Where("x.advice_name LIKE ?", likeKey)
  1104. }
  1105. offset := (page - 1) * limit
  1106. 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
  1107. fmt.Print("错误是什么", err)
  1108. return
  1109. }
  1110. func GetDoctorAdvicePageList(orgID, patientID, advice_type, stop, start, end int64, keywords string, page, limit int64) (advices []*models.DoctorAdvice, total int64, err error) {
  1111. offset := (page - 1) * limit
  1112. db := readDb.Model(&models.DoctorAdvice{}).Where("status=1")
  1113. if orgID > 0 {
  1114. db = db.Where("user_org_id=?", orgID)
  1115. }
  1116. if patientID > 0 {
  1117. db = db.Where("patient_id = ?", patientID)
  1118. }
  1119. if advice_type > 0 {
  1120. db = db.Where("advice_type = ?", advice_type)
  1121. }
  1122. if stop == 1 {
  1123. db = db.Where("(stop_state=? or execution_state=?) and parent_id=0", stop, stop)
  1124. } else if stop == 2 {
  1125. db = db.Where("stop_state=? and execution_state=?", stop, stop)
  1126. }
  1127. if start != 0 {
  1128. db = db.Where("start_time>=?", start)
  1129. }
  1130. if end != 0 {
  1131. db = db.Where("start_time<=?", end)
  1132. }
  1133. if len(keywords) > 0 {
  1134. likeKey := "%" + keywords + "%"
  1135. db = db.Where("advice_name LIKE ?", likeKey)
  1136. }
  1137. 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
  1138. return
  1139. }
  1140. func CreateSubDoctorAdvice(advices []*models.DoctorAdvice) (err error) {
  1141. if len(advices) > 0 {
  1142. utx := writeDb.Begin()
  1143. if len(advices) > 0 {
  1144. 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 "
  1145. insertParams := make([]string, 0)
  1146. insertData := make([]interface{}, 0)
  1147. for _, advice := range advices {
  1148. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  1149. insertData = append(insertData, advice.SingleDoseUnit)
  1150. insertData = append(insertData, advice.PrescribingNumber)
  1151. insertData = append(insertData, advice.PrescribingNumberUnit)
  1152. insertData = append(insertData, advice.AdviceName)
  1153. insertData = append(insertData, advice.AdviceDesc)
  1154. insertData = append(insertData, advice.SingleDose)
  1155. insertData = append(insertData, advice.CreatedTime)
  1156. insertData = append(insertData, advice.UpdatedTime)
  1157. insertData = append(insertData, advice.PatientId)
  1158. insertData = append(insertData, advice.ParentId)
  1159. insertData = append(insertData, advice.UserOrgId)
  1160. insertData = append(insertData, advice.RecordDate)
  1161. insertData = append(insertData, 2)
  1162. insertData = append(insertData, advice.DrugSpecUnit)
  1163. }
  1164. thisSQL += strings.Join(insertParams, ", ")
  1165. err = utx.Exec(thisSQL, insertData...).Error
  1166. if err != nil {
  1167. utx.Rollback()
  1168. return
  1169. }
  1170. }
  1171. utx.Commit()
  1172. }
  1173. return
  1174. }
  1175. func GetPatientDialysisRecord(orgID, patientID int64, page, limit, start, end, mode_id int64) ([]*models.PatientDialysisRecord, int64, error) {
  1176. offset := (page - 1) * limit
  1177. var total int64
  1178. var err error
  1179. var orders []*models.PatientDialysisRecord
  1180. // err = readDb.Table("xt_dialysis_order as do").
  1181. // Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1182. // Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1183. // Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1184. // Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1185. // 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).
  1186. // Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1187. // 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
  1188. db := readDb.Table("xt_dialysis_order as do").
  1189. Preload("DialysisPrescription", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1190. Preload("PredialysisEvaluation", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1191. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  1192. return readDb.Where("patient_id=? and user_org_id=? and status=1", patientID, orgID).Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1193. return readUserDb.Where("status = 1")
  1194. })
  1195. }).
  1196. Preload("AssessmentAfterDislysis", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1197. Preload("TreatmentSummary", "patient_id=? and user_org_id=? and status=1", patientID, orgID).
  1198. Preload("Device", "org_id=? and status=1", orgID).
  1199. Preload("UserAdminRole", func(db *gorm.DB) *gorm.DB {
  1200. return readUserDb.Where("org_id=? and status = 1", orgID)
  1201. }).
  1202. 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).
  1203. Joins("JOIN xt_device_zone as dz ON dz.org_id = ? and dz.id=s.partition_id", orgID).
  1204. Where("do.patient_id=? and do.user_org_id=? and do.stage = 2 and do.status=1", patientID, orgID).Group("s.schedule_date")
  1205. if start != 0 {
  1206. db = db.Where("do.dialysis_date>=?", start)
  1207. }
  1208. if end != 0 {
  1209. db = db.Where("do.dialysis_date<=?", end)
  1210. }
  1211. if mode_id > 0 {
  1212. 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")
  1213. db = db.Where("dp.mode_id=?", mode_id)
  1214. }
  1215. 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
  1216. if len(orders) > 0 {
  1217. ids := make([]int64, 0)
  1218. for _, order := range orders {
  1219. dialyzer := order.DialysisPrescription.Dialyzer
  1220. ids = append(ids, dialyzer)
  1221. }
  1222. if len(ids) > 0 {
  1223. var dialyzers []*models.DeviceNumber
  1224. err = readDb.Model(&models.DeviceNumber{}).Where("id IN (?) and org_id=? and status=1", ids, orgID).Find(&dialyzers).Error
  1225. if err != nil {
  1226. return nil, 0, err
  1227. }
  1228. dialyzerMap := make(map[int64]models.DeviceNumber, 0)
  1229. for _, item := range dialyzers {
  1230. dialyzerMap[item.ID] = *item
  1231. }
  1232. for orderIndex, order := range orders {
  1233. if _, exist := dialyzerMap[order.DialysisPrescription.Dialyzer]; exist {
  1234. orders[orderIndex].DeviceNumber = dialyzerMap[order.DialysisPrescription.Dialyzer].Number
  1235. }
  1236. }
  1237. }
  1238. }
  1239. return orders, total, err
  1240. }
  1241. func GetPatientTreatmentSummaryList(orgID, patientID, page, limit, start, end int64) (list []*models.TreatmentSummary, total int, err error) {
  1242. offset := (page - 1) * limit
  1243. fmt.Println(offset)
  1244. fmt.Println(limit)
  1245. db := readDb.Model(&models.TreatmentSummary{}).Where("user_org_id = ? and patient_id=?", orgID, patientID)
  1246. if start != 0 {
  1247. db = db.Where("assessment_date >= ?", start)
  1248. }
  1249. if end != 0 {
  1250. db = db.Where("assessment_date <= ?", end)
  1251. }
  1252. db = db.Where("status=1")
  1253. err = db.Count(&total).Offset(offset).Limit(limit).Order("assessment_date desc, id desc").Find(&list).Error
  1254. return
  1255. }
  1256. func GetPatientScheduleList(orgID int64, patientID int64, page int64, limit int64, start int64) (schedules []*models.PatientSchedule, err error) {
  1257. offset := (page - 1) * limit
  1258. err = readDb.Table("xt_schedule as s").
  1259. Preload("DeviceZone", "org_id=? and status=1", orgID).
  1260. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  1261. Preload("TreatmentMode", "status=1").
  1262. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, start).
  1263. Limit(limit).Offset(offset).Find(&schedules).Error
  1264. return
  1265. }
  1266. func GetMonitorRecord(orgID int64, date int64, partition int64, patientId int64) ([]*models.NewVMMonitorDialysisSchedule, error) {
  1267. var mds []*models.NewVMMonitorDialysisSchedule
  1268. db := readDb.Model(&models.NewVMMonitorDialysisSchedule{})
  1269. if date > 0 {
  1270. db = db.Where("schedule_date = ?", date)
  1271. }
  1272. if partition > 0 {
  1273. db = db.Where("partition_id = ?", partition)
  1274. }
  1275. if patientId > 0 {
  1276. db = db.Where("patient_id = ?", patientId)
  1277. }
  1278. err = db.Where("status = 1 and user_org_id = ?", orgID).Find(&mds).Error
  1279. return mds, err
  1280. }
  1281. func MobileGetMonitorsWithPatient(orgID int64, keyword string, page int) ([]*models.MonitorDialysisSchedule, error) {
  1282. var patients []*models.Patients
  1283. 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
  1284. if getPatientErr != nil {
  1285. return nil, getPatientErr
  1286. }
  1287. patientIDs := make([]int64, len(patients))
  1288. for index, patient := range patients {
  1289. patientIDs[index] = patient.ID
  1290. }
  1291. db := readDb.
  1292. Model(&models.MonitorDialysisSchedule{}).
  1293. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  1294. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  1295. Preload("TreatmentMode", "status = 1").
  1296. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  1297. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  1298. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  1299. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  1300. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  1301. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID).
  1302. Where("status = 1 AND user_org_id = ? AND patient_id in (?)", orgID, patientIDs)
  1303. var schedules []*models.MonitorDialysisSchedule
  1304. err := db.Offset(20 * (page - 1)).Limit(20).Order("schedule_date desc").Find(&schedules).Error
  1305. return schedules, err
  1306. }
  1307. func GetPatientByKeyWord(orgID int64, keywords string) (patient []*models.Patients, err error) {
  1308. db := readDb.Model(&models.Patients{}).Where("user_org_id=? and status=1", orgID)
  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, "mtime": 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 GetAllDeviceNumberByList(orgID int64) (number []*models.DeviceNumber, err error) {
  1667. redis := RedisClient()
  1668. defer redis.Close()
  1669. key := strconv.FormatInt(orgID, 10) + ":" + ":device_number_all"
  1670. number_info_str, _ := redis.Get(key).Result()
  1671. if len(number_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1672. err = readDb.Model(&models.DeviceNumber{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  1673. if err != nil {
  1674. if err == gorm.ErrRecordNotFound {
  1675. return nil, nil
  1676. } else {
  1677. return nil, err
  1678. }
  1679. } else {
  1680. if len(number) > 0 {
  1681. //缓存数据
  1682. device_number_json, err := json.Marshal(&number)
  1683. if err == nil {
  1684. redis.Set(key, device_number_json, time.Second*60*60*18)
  1685. }
  1686. }
  1687. return number, nil
  1688. }
  1689. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1690. json.Unmarshal([]byte(number_info_str), &number)
  1691. return number, nil
  1692. }
  1693. }
  1694. func GetAllZoneByList(orgID int64) (zone []*models.DeviceZone, err error) {
  1695. redis := RedisClient()
  1696. defer redis.Close()
  1697. key := strconv.FormatInt(orgID, 10) + ":" + ":device_zone_all"
  1698. zone_info_str, _ := redis.Get(key).Result()
  1699. if len(zone_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1700. err = readDb.Model(&models.DeviceZone{}).Where("org_id=? and status=1", orgID).Find(&zone).Error
  1701. if err != nil {
  1702. if err == gorm.ErrRecordNotFound {
  1703. return nil, nil
  1704. } else {
  1705. return nil, err
  1706. }
  1707. } else {
  1708. if len(zone) > 0 {
  1709. //缓存数据
  1710. zone_number_json, err := json.Marshal(&zone)
  1711. if err == nil {
  1712. redis.Set(key, zone_number_json, time.Second*60*60*18)
  1713. }
  1714. }
  1715. return zone, nil
  1716. }
  1717. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1718. json.Unmarshal([]byte(zone_info_str), &zone)
  1719. return zone, nil
  1720. }
  1721. }
  1722. func GetAllPrescriptionByList(orgID int64, scheduleDate int64) (prescriptions []*models.VMDialysisPrescription, err error) {
  1723. redis := RedisClient()
  1724. defer redis.Close()
  1725. key := strconv.FormatInt(orgID, 10) + ":" + ":prescriptions_list_all"
  1726. prescriptions_list_all, _ := redis.Get(key).Result()
  1727. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1728. err = readDb.Model(&models.VMDialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  1729. if err != nil {
  1730. if err == gorm.ErrRecordNotFound {
  1731. return nil, nil
  1732. } else {
  1733. return nil, err
  1734. }
  1735. } else {
  1736. if len(prescriptions) > 0 {
  1737. //缓存数据
  1738. prescriptions_list_all, err := json.Marshal(&prescriptions)
  1739. if err == nil {
  1740. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  1741. }
  1742. }
  1743. return prescriptions, nil
  1744. }
  1745. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1746. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  1747. return prescriptions, nil
  1748. }
  1749. }
  1750. func GetAllAssessmentBeforesByListOne(orgID int64, scheduleDate int64) (assessmentBefores []*models.VMPredialysisEvaluation, err error) {
  1751. redis := RedisClient()
  1752. defer redis.Close()
  1753. key := strconv.FormatInt(orgID, 10) + ":" + ":assessment_befores_list_all"
  1754. assessment_befores_list_all, _ := redis.Get(key).Result()
  1755. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1756. err = readDb.Model(&models.VMPredialysisEvaluation{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  1757. if err != nil {
  1758. if err == gorm.ErrRecordNotFound {
  1759. return nil, nil
  1760. } else {
  1761. return nil, err
  1762. }
  1763. } else {
  1764. if len(assessmentBefores) > 0 {
  1765. //缓存数据
  1766. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  1767. if err == nil {
  1768. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  1769. }
  1770. }
  1771. return assessmentBefores, nil
  1772. }
  1773. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1774. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  1775. return assessmentBefores, nil
  1776. }
  1777. }
  1778. func GetAllDialysisOrdersByListOne(orgID int64, scheduleDate int64) (dialysisOrders []*models.VMMonitorDialysisOrder, err error) {
  1779. redis := RedisClient()
  1780. defer redis.Close()
  1781. key := strconv.FormatInt(orgID, 10) + ":" + ":dialysis_orders_list_all"
  1782. dialysis_orders_list_all, _ := redis.Get(key).Result()
  1783. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1784. 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
  1785. if err != nil {
  1786. if err == gorm.ErrRecordNotFound {
  1787. return nil, nil
  1788. } else {
  1789. return nil, err
  1790. }
  1791. } else {
  1792. if len(dialysisOrders) > 0 {
  1793. //缓存数据
  1794. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  1795. if err == nil {
  1796. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  1797. }
  1798. }
  1799. return dialysisOrders, nil
  1800. }
  1801. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1802. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  1803. return dialysisOrders, nil
  1804. }
  1805. }
  1806. func GetAllAssessmentAfterDislysisByListOne(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.VMAssessmentAfterDislysis, err error) {
  1807. redis := RedisClient()
  1808. defer redis.Close()
  1809. key := strconv.FormatInt(orgID, 10) + ":" + ":assessment_after_dislysis_list_all"
  1810. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  1811. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1812. err = readDb.Model(&models.VMAssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  1813. if err != nil {
  1814. if err == gorm.ErrRecordNotFound {
  1815. return nil, nil
  1816. } else {
  1817. return nil, err
  1818. }
  1819. } else {
  1820. if len(assessmentAfterDislysis) > 0 {
  1821. //缓存数据
  1822. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  1823. if err == nil {
  1824. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  1825. }
  1826. }
  1827. return assessmentAfterDislysis, nil
  1828. }
  1829. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1830. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  1831. return assessmentAfterDislysis, nil
  1832. }
  1833. }
  1834. func GetAllMonitorList(orgID int64, scheduleDate int64) (monitor []*models.VMMonitoringRecord, err error) {
  1835. redis := RedisClient()
  1836. defer redis.Close()
  1837. key := strconv.FormatInt(orgID, 10) + ":" + ":monitor_record_list_all"
  1838. monitor_record_list_all, _ := redis.Get(key).Result()
  1839. fmt.Println("monitor_recorde_list", len(monitor_record_list_all))
  1840. if len(monitor_record_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1841. err = readDb.Model(&models.VMMonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Find(&monitor).Error
  1842. if err != nil {
  1843. if err == gorm.ErrRecordNotFound {
  1844. return nil, nil
  1845. } else {
  1846. return nil, err
  1847. }
  1848. } else {
  1849. if len(monitor) > 0 {
  1850. //缓存数据
  1851. assessment_after_dislysis__all, err := json.Marshal(&monitor)
  1852. if err == nil {
  1853. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  1854. }
  1855. }
  1856. return monitor, nil
  1857. }
  1858. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1859. json.Unmarshal([]byte(monitor_record_list_all), &monitor)
  1860. return monitor, nil
  1861. }
  1862. }
  1863. func GetAllPatientListByListOne(orgID int64) (patients []*models.VMMonitorPatients, err error) {
  1864. redis := RedisClient()
  1865. defer redis.Close()
  1866. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  1867. patient_info_str, _ := redis.Get(key).Result()
  1868. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1869. err = readDb.Model(&models.VMMonitorPatients{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  1870. if err != nil {
  1871. if err == gorm.ErrRecordNotFound {
  1872. return nil, nil
  1873. } else {
  1874. return nil, err
  1875. }
  1876. } else {
  1877. if len(patients) > 0 {
  1878. //缓存数据
  1879. patient_info_json, err := json.Marshal(&patients)
  1880. if err == nil {
  1881. redis.Set(key, patient_info_json, time.Second*60*60*18)
  1882. }
  1883. }
  1884. return patients, nil
  1885. }
  1886. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1887. json.Unmarshal([]byte(patient_info_str), &patients)
  1888. return patients, nil
  1889. }
  1890. }
  1891. func GetAllTreatModeByList(orgID int64) (treatement []*models.TreatmentMode, err error) {
  1892. redis := RedisClient()
  1893. defer redis.Close()
  1894. key := strconv.FormatInt(orgID, 10) + ":" + ":treatement_mode_all"
  1895. treatment_info_str, _ := redis.Get(key).Result()
  1896. if len(treatment_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1897. err = readDb.Model(&models.TreatmentMode{}).Where("status=1").Find(&treatement).Error
  1898. if err != nil {
  1899. if err == gorm.ErrRecordNotFound {
  1900. return nil, nil
  1901. } else {
  1902. return nil, err
  1903. }
  1904. } else {
  1905. if len(treatement) > 0 {
  1906. //缓存数据
  1907. patient_info_json, err := json.Marshal(&treatement)
  1908. if err == nil {
  1909. redis.Set(key, patient_info_json, time.Second*60*60*18)
  1910. }
  1911. }
  1912. return treatement, nil
  1913. }
  1914. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1915. json.Unmarshal([]byte(treatment_info_str), &treatement)
  1916. return treatement, nil
  1917. }
  1918. }
  1919. func GetAllPatientListByListTwo(orgID int64) (patients []*MSchedualPatientVM, err error) {
  1920. redis := RedisClient()
  1921. defer redis.Close()
  1922. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  1923. patient_info_str, _ := redis.Get(key).Result()
  1924. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1925. err = readDb.Model(&MSchedualPatientVM{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  1926. if err != nil {
  1927. if err == gorm.ErrRecordNotFound {
  1928. return nil, nil
  1929. } else {
  1930. return nil, err
  1931. }
  1932. } else {
  1933. if len(patients) > 0 {
  1934. //缓存数据
  1935. patient_info_json, err := json.Marshal(&patients)
  1936. if err == nil {
  1937. redis.Set(key, patient_info_json, time.Second*60*60*18)
  1938. }
  1939. }
  1940. return patients, nil
  1941. }
  1942. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1943. json.Unmarshal([]byte(patient_info_str), &patients)
  1944. return patients, nil
  1945. }
  1946. }
  1947. func GetAllDeviceNumberByListOne(orgID int64) (number []*MDeviceNumberVM, err error) {
  1948. redis := RedisClient()
  1949. defer redis.Close()
  1950. key := strconv.FormatInt(orgID, 10) + ":" + ":device_number_all"
  1951. number_info_str, _ := redis.Get(key).Result()
  1952. if len(number_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1953. err = readDb.Model(&MDeviceNumberVM{}).Where("org_id=? and status=1", orgID).Find(&number).Error
  1954. if err != nil {
  1955. if err == gorm.ErrRecordNotFound {
  1956. return nil, nil
  1957. } else {
  1958. return nil, err
  1959. }
  1960. } else {
  1961. if len(number) > 0 {
  1962. //缓存数据
  1963. device_number_json, err := json.Marshal(&number)
  1964. if err == nil {
  1965. redis.Set(key, device_number_json, time.Second*60*60*18)
  1966. }
  1967. }
  1968. return number, nil
  1969. }
  1970. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1971. json.Unmarshal([]byte(number_info_str), &number)
  1972. return number, nil
  1973. }
  1974. }
  1975. func GetAllPrescriptionByListOne(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescriptionList, err error) {
  1976. redis := RedisClient()
  1977. defer redis.Close()
  1978. key := strconv.FormatInt(orgID, 10) + ":" + ":prescriptions_list_all"
  1979. prescriptions_list_all, _ := redis.Get(key).Result()
  1980. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1981. err = readDb.Model(&models.DialysisPrescriptionList{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  1982. if err != nil {
  1983. if err == gorm.ErrRecordNotFound {
  1984. return nil, nil
  1985. } else {
  1986. return nil, err
  1987. }
  1988. } else {
  1989. if len(prescriptions) > 0 {
  1990. //缓存数据
  1991. prescriptions_list_all, err := json.Marshal(&prescriptions)
  1992. if err == nil {
  1993. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  1994. }
  1995. }
  1996. return prescriptions, nil
  1997. }
  1998. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1999. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2000. return prescriptions, nil
  2001. }
  2002. }
  2003. func GetAllDialysisOrdersByListTwo(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVM, err error) {
  2004. redis := RedisClient()
  2005. defer redis.Close()
  2006. key := strconv.FormatInt(orgID, 10) + ":" + ":dialysis_orders_list_all"
  2007. dialysis_orders_list_all, _ := redis.Get(key).Result()
  2008. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2009. 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
  2010. if err != nil {
  2011. if err == gorm.ErrRecordNotFound {
  2012. return nil, nil
  2013. } else {
  2014. return nil, err
  2015. }
  2016. } else {
  2017. if len(dialysisOrders) > 0 {
  2018. //缓存数据
  2019. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2020. if err == nil {
  2021. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2022. }
  2023. }
  2024. return dialysisOrders, nil
  2025. }
  2026. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2027. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2028. return dialysisOrders, nil
  2029. }
  2030. }
  2031. func GetAllPatientListByListThree(orgID int64) (patients []*models.MonitorPatients, err error) {
  2032. redis := RedisClient()
  2033. defer redis.Close()
  2034. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  2035. patient_info_str, _ := redis.Get(key).Result()
  2036. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2037. err = readDb.Model(&models.MonitorPatients{}).Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  2038. if err != nil {
  2039. if err == gorm.ErrRecordNotFound {
  2040. return nil, nil
  2041. } else {
  2042. return nil, err
  2043. }
  2044. } else {
  2045. if len(patients) > 0 {
  2046. //缓存数据
  2047. patient_info_json, err := json.Marshal(&patients)
  2048. if err == nil {
  2049. redis.Set(key, patient_info_json, time.Second*60*60*18)
  2050. }
  2051. }
  2052. return patients, nil
  2053. }
  2054. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2055. json.Unmarshal([]byte(patient_info_str), &patients)
  2056. return patients, nil
  2057. }
  2058. }
  2059. func GetAllPrescriptionByListTwo(orgID int64, scheduleDate int64) (prescriptions []*models.DialysisPrescription, err error) {
  2060. redis := RedisClient()
  2061. defer redis.Close()
  2062. key := strconv.FormatInt(orgID, 10) + ":" + ":prescriptions_list_all"
  2063. prescriptions_list_all, _ := redis.Get(key).Result()
  2064. if len(prescriptions_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2065. err = readDb.Model(&models.DialysisPrescription{}).Where("status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).Find(&prescriptions).Error
  2066. if err != nil {
  2067. if err == gorm.ErrRecordNotFound {
  2068. return nil, nil
  2069. } else {
  2070. return nil, err
  2071. }
  2072. } else {
  2073. if len(prescriptions) > 0 {
  2074. //缓存数据
  2075. prescriptions_list_all, err := json.Marshal(&prescriptions)
  2076. if err == nil {
  2077. redis.Set(key, prescriptions_list_all, time.Second*60*60*18)
  2078. }
  2079. }
  2080. return prescriptions, nil
  2081. }
  2082. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2083. json.Unmarshal([]byte(prescriptions_list_all), &prescriptions)
  2084. return prescriptions, nil
  2085. }
  2086. }
  2087. func GetAllAssessmentBeforesByListThree(orgID int64, scheduleDate int64) (assessmentBefores []*models.PredialysisEvaluation, err error) {
  2088. redis := RedisClient()
  2089. defer redis.Close()
  2090. key := strconv.FormatInt(orgID, 10) + ":" + ":assessment_befores_list_all"
  2091. assessment_befores_list_all, _ := redis.Get(key).Result()
  2092. if len(assessment_befores_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2093. err = readDb.Model(&models.PredialysisEvaluation{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).Find(&assessmentBefores).Error
  2094. if err != nil {
  2095. if err == gorm.ErrRecordNotFound {
  2096. return nil, nil
  2097. } else {
  2098. return nil, err
  2099. }
  2100. } else {
  2101. if len(assessmentBefores) > 0 {
  2102. //缓存数据
  2103. assessment_befores_list_all, err := json.Marshal(&assessmentBefores)
  2104. if err == nil {
  2105. redis.Set(key, assessment_befores_list_all, time.Second*60*60*18)
  2106. }
  2107. }
  2108. return assessmentBefores, nil
  2109. }
  2110. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2111. json.Unmarshal([]byte(assessment_befores_list_all), &assessmentBefores)
  2112. return assessmentBefores, nil
  2113. }
  2114. }
  2115. func GetAllDialysisOrdersByListThree(orgID int64, scheduleDate int64) (dialysisOrders []*models.MonitorDialysisOrder, err error) {
  2116. redis := RedisClient()
  2117. defer redis.Close()
  2118. key := strconv.FormatInt(orgID, 10) + ":" + ":dialysis_orders_list_all"
  2119. dialysis_orders_list_all, _ := redis.Get(key).Result()
  2120. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2121. 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
  2122. if err != nil {
  2123. if err == gorm.ErrRecordNotFound {
  2124. return nil, nil
  2125. } else {
  2126. return nil, err
  2127. }
  2128. } else {
  2129. if len(dialysisOrders) > 0 {
  2130. //缓存数据
  2131. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  2132. if err == nil {
  2133. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  2134. }
  2135. }
  2136. return dialysisOrders, nil
  2137. }
  2138. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2139. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  2140. return dialysisOrders, nil
  2141. }
  2142. }
  2143. func GetAllAssessmentAfterDislysisByListThree(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*models.AssessmentAfterDislysis, err error) {
  2144. redis := RedisClient()
  2145. defer redis.Close()
  2146. key := strconv.FormatInt(orgID, 10) + ":" + ":assessment_after_dislysis_list_all"
  2147. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  2148. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2149. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  2150. if err != nil {
  2151. if err == gorm.ErrRecordNotFound {
  2152. return nil, nil
  2153. } else {
  2154. return nil, err
  2155. }
  2156. } else {
  2157. if len(assessmentAfterDislysis) > 0 {
  2158. //缓存数据
  2159. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  2160. if err == nil {
  2161. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2162. }
  2163. }
  2164. return assessmentAfterDislysis, nil
  2165. }
  2166. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2167. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  2168. return assessmentAfterDislysis, nil
  2169. }
  2170. }
  2171. func GetAllMonitorListThree(orgID int64, scheduleDate int64) (monitor []*models.MonitoringRecord, err error) {
  2172. redis := RedisClient()
  2173. defer redis.Close()
  2174. key := strconv.FormatInt(orgID, 10) + ":" + ":monitor_record_list_all"
  2175. monitor_record_list_all, _ := redis.Get(key).Result()
  2176. if len(monitor_record_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  2177. err = readDb.Model(&models.MonitoringRecord{}).Where("status = 1 AND user_org_id = ? AND monitoring_date= ?", orgID, scheduleDate).Find(&monitor).Error
  2178. if err != nil {
  2179. if err == gorm.ErrRecordNotFound {
  2180. return nil, nil
  2181. } else {
  2182. return nil, err
  2183. }
  2184. } else {
  2185. if len(monitor) > 0 {
  2186. //缓存数据
  2187. assessment_after_dislysis__all, err := json.Marshal(&monitor)
  2188. if err == nil {
  2189. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  2190. }
  2191. }
  2192. return monitor, nil
  2193. }
  2194. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  2195. json.Unmarshal([]byte(monitor_record_list_all), &monitor)
  2196. return monitor, nil
  2197. }
  2198. }