patient_service.go 106KB

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