dialysis_service.go 122KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729
  1. package service
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "XT_New/models"
  9. "github.com/jinzhu/gorm"
  10. )
  11. // GetPatientList 返回患者的列表
  12. func GetSchedualPatientList(orgID int64, schedulDate int64, schedulType int64, partitionType int64, keywords string, page int64, limit int64) (schedule []*models.DialysisSchedule, err error) {
  13. var total int64
  14. //if len(keywords) > 0 {
  15. // likeKey := "%" + keywords + "%"
  16. // //db = db.Preload("MonitorPatients", "status = 1 AND user_org_id = ? AND `name` LIKE ? ", orgID, likeKey).Preload("DeviceNumber", "status = 1 AND org_id = ? ", orgID).Preload("PredialysisEvaluation", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, schedulDate).Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, schedulDate).Preload("MonitoringRecord", "status = 1 AND user_org_id = ? AND monitoring_date = ?", orgID, schedulDate).Where("status = 1 AND user_org_id = ? ", orgID)
  17. // db = readDb.
  18. // Model(&models.MonitorDialysisSchedule{}).
  19. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  20. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  21. // Preload("TreatmentMode", "status = 1").
  22. // Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  23. // Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  24. // Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  25. // Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  26. // Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  27. // Preload("MonitorPatients", "status = 1 AND user_org_id = ? AND `name` LIKE ?", orgID,likeKey)
  28. //
  29. //
  30. //} else {
  31. //}
  32. db := readDb.
  33. Model(&models.MonitorDialysisSchedule{}).
  34. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  35. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  36. Preload("TreatmentMode", "status = 1").
  37. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, schedulDate).
  38. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
  39. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
  40. Preload("MonitoringRecord", "status = 1 AND user_org_id = ? AND monitoring_date = ?", orgID, schedulDate).
  41. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, schedulDate).
  42. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  43. db = db.Where("status = 1")
  44. if schedulDate > 0 {
  45. db = db.Where("schedule_date = ?", schedulDate)
  46. }
  47. db = db.Count(&total)
  48. fmt.Println(total)
  49. if schedulType > 0 {
  50. db = db.Where("schedule_type = ?", schedulType)
  51. }
  52. if partitionType > 0 {
  53. db = db.Where("partition_id = ?", partitionType)
  54. }
  55. offset := (page - 1) * limit
  56. errors := db.Offset(offset).Limit(limit).Find(&schedule).Error
  57. return schedule, errors
  58. }
  59. // 用户基本信息
  60. func FindPatientInfoById(orgID int64, id int64, time int64) (patient models.SchedualPatient2, err error) {
  61. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  62. return db.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).Where("patient_id = ? AND user_org_id = ? AND schedule_date = ? ", id, orgID, time)
  63. }).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  64. return
  65. }
  66. // 透析处方
  67. func FindPatientPrescribeById(orgID int64, patientsId int64, recordDate int64) (patient models.DialysisPrescription, err error) {
  68. err = readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  69. return
  70. }
  71. // 透析方案中的透析处方
  72. func FindDialysisSolution(orgID int64, patientsId int64) (patient models.DialysisSolution, err error) {
  73. err = readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id=? and status=1", patientsId, orgID).Last(&patient).Error
  74. return
  75. }
  76. // 透析方案中的透析模式
  77. func FindTreatmentModeById(modeId int64) (mode models.TreatmentMode, err error) {
  78. err = readDb.Model(&models.TreatmentMode{}).Where("id = ? AND status=1", modeId).First(&mode).Error
  79. return
  80. }
  81. // 接诊评估
  82. func FindReceiverTreatmentAccessRecordById(orgID int64, patientsId int64, recordDate int64) (receiveTreatmentAsses models.ReceiveTreatmentAsses, err error) {
  83. err = readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ?", patientsId, orgID, recordDate).First(&receiveTreatmentAsses).Error
  84. return
  85. }
  86. // 透前评估
  87. func FindPredialysisEvaluationById(orgID int64, patientsId int64, recordDate int64) (patient models.PredialysisEvaluation, err error) {
  88. err = readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date = ?", patientsId, orgID, recordDate).First(&patient).Error
  89. return
  90. }
  91. func FindPredialysisName(id int64, orgid int64) (models.Dataconfig, error) {
  92. dataconfig := models.Dataconfig{}
  93. err := readDb.Where("value = ? and org_id =? and status = 1", id, orgid).Find(&dataconfig).Error
  94. return dataconfig, err
  95. }
  96. // 临时医嘱
  97. func FindDoctorAdviceById(orgID int64, patientsId int64, recordDate int64) (patient []*models.DoctorAdvice, err error) {
  98. err = readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? AND advice_type = 2", patientsId, orgID, recordDate).Find(&patient).Error
  99. return
  100. }
  101. func FindDoctorAdviceByInfo(orgID int64, patientsId int64, recordDate int64) (advices []*models.XtDoctorAdvice, err error) {
  102. //db := readDb.Table("xt_doctor_advice as a").Where("a.status = ?", 1)
  103. //err = db.Group("a.advice_name").Select("a.id,a.user_org_id,a.patient_id,a.advice_type,a.advice_date,a.start_time,a.advice_name,a.advice_desc,a.reminder_date,a.single_dose,a.single_dose_unit,a.prescribing_number,a.prescribing_number_unit,a.delivery_way,a.execution_frequency,a.advice_doctor,a.status,a.created_time,a.updated_time,a.advice_affirm,a.remark,a.stop_time,a.stop_reason,a.stop_doctor,a.stop_state,a.parent_id,a.execution_time,a.execution_staff,a.execution_state,a.checker,a.record_date,a.dialysis_order_id,a.check_time,a.check_state,a.drug_spec,a.drug_spec_unit,a.groupno,a.remind_type,a.frequency_type,a.day_count,a.week_day,a.template_id,r.user_name,s.user_name").Joins(" left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = a.execution_staff").Joins("left join sgj_users.sgj_user_admin_role as s on s.admin_user_id = a.checker").Where("a.user_org_id = ? and a.patient_id = ? and a.advice_date = ?", orgID, patientsId, recordDate).Find(&advices).Error
  104. //return
  105. err = readDb.Model(&models.DoctorAdvice{}).
  106. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? and advice_type = 2", patientsId, orgID, recordDate).
  107. Select("id, user_org_id, patient_id, advice_type, advice_date, record_date, start_time, advice_name,advice_desc, reminder_date, drug_spec, drug_spec_unit, 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, check_state, check_time, groupno, IF(parent_id > 0, parent_id, id) as advice_order").Order("start_time asc, groupno desc, advice_order desc, id asc").
  108. Find(&advices).Error
  109. return
  110. }
  111. func GetTotalDialysisCout(orgid int64, patientid int64) (order []*models.DialysisOrder, total int64, err error) {
  112. err = readDb.Model(&order).Where("user_org_id = ? and patient_id = ? and status = 1", orgid, patientid).Find(&order).Count(&total).Error
  113. return order, total, err
  114. }
  115. // 临时医嘱
  116. func FindDoctorAdviceOrderById(orgID int64, patientsId int64, recordDate int64) (patient []*models.DoctorAdvice, err error) {
  117. err = readDb.Model(&models.DoctorAdvice{}).
  118. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? and (advice_type = 2 or advice_type = 3)", patientsId, orgID, recordDate).
  119. Select("id, user_org_id, patient_id, advice_type, advice_date, record_date, start_time, advice_name,advice_desc, reminder_date, drug_spec, drug_spec_unit, 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, check_state, check_time, groupno, IF(parent_id > 0, parent_id, id) as advice_order").Order("start_time asc, groupno desc, advice_order desc, id asc").
  120. Find(&patient).Error
  121. return
  122. }
  123. // 双人核对
  124. func FindDoubleCheckById(orgID int64, patientsId int64, recordDate int64) (patient models.DoubleCheck, err error) {
  125. err = readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? and user_org_id=? and status=1 and check_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  126. return
  127. }
  128. // 透析监测
  129. func FindAllMonitorRecord(orgID int64, patientsId int64, recordDate int64) (record []*models.MonitoringRecord, err error) {
  130. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id=? and status=1 and monitoring_date = ?", patientsId, orgID, recordDate).Order("operate_time ASC").Find(&record).Error
  131. return
  132. }
  133. func FindAllMonitor(orgID int64, patientsId int64, recordDate int64) (record []*models.MonitoringRecord, err error) {
  134. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id=? and status=1 and monitoring_date = ?", patientsId, orgID, recordDate).Order("operate_time ASC").Find(&record).Error
  135. return
  136. }
  137. // 透后评估
  138. func FindAssessmentAfterDislysisById(orgID int64, patientsId int64, recordDate int64) (patient models.AssessmentAfterDislysis, err error) {
  139. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date =?", patientsId, orgID, recordDate).First(&patient).Error
  140. return
  141. }
  142. // 治疗小结
  143. func FindTreatmentSummaryById(orgID int64, patientsId int64, recordDate int64) (patient models.TreatmentSummary, err error) {
  144. err = readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  145. return
  146. }
  147. // 机构叫号配置
  148. func FindQueueCallInfo(orgID int64) (queueConfig models.QueueCallConfig, err error) {
  149. err = readDb.Model(&models.QueueCallConfig{}).Where("org_id=? ", orgID).First(&queueConfig).Error
  150. return
  151. }
  152. // 修改机构叫号配置
  153. func UpDateQueueCallInfo(queueConfig models.QueueCallConfig) error {
  154. err := writeDb.Save(&queueConfig).Error
  155. return err
  156. }
  157. // 所有设备信息
  158. func FindAllDeviceInfo(orgID int64) (device []*models.Device, err error) {
  159. err = readDb.Model(&models.Device{}).Where("org_id=? and status=1 and device_type = 1", orgID).Find(&device).Error
  160. return device, err
  161. }
  162. // 所有设备区域
  163. func GetAllDeviceZone(orgId int64) (error, []*models.DeviceZone) {
  164. var zone []*models.DeviceZone
  165. err := readDb.Model(&zone).Where("org_id = ? AND status = 1", orgId).Find(&zone).Error
  166. return err, zone
  167. }
  168. // 获取所有子药物
  169. func FindAllSubDoctorAdvice(id int64, parentId int64) (error, []models.DoctorAdvice) {
  170. var subAdvies []models.DoctorAdvice
  171. err := readDb.Model(&subAdvies).Where("patient_id = ? AND parent_id = ?", id, parentId).Find(&subAdvies).Error
  172. return err, subAdvies
  173. }
  174. func FindAdivceById(orgId int64, patientId int64, id int64) (error, models.DoctorAdvice) {
  175. advices := models.DoctorAdvice{}
  176. err := readDb.Model(&advices).Where("user_org_id = ? AND patient_id = ? AND id = ? AND status = 1", orgId, patientId, id).Find(&advices).Error
  177. return err, advices
  178. }
  179. func FindSubAdivceById(patientId int64, parentId int64) []*models.DoctorAdvice {
  180. var subAdvices []*models.DoctorAdvice
  181. readDb.Model(&subAdvices).Where("patient_id = ? AND parent_id = ?", patientId, parentId).Find(&subAdvices)
  182. return subAdvices
  183. }
  184. func AddSigleRecord(prescription *models.DialysisPrescription) (err error) {
  185. tx := writeDb.Begin()
  186. errors := tx.Model(&models.DialysisPrescription{}).Create(&prescription).Error
  187. if err != nil {
  188. tx.Rollback()
  189. }
  190. tx.Commit()
  191. return errors
  192. }
  193. func AddSigleRecordOne(prescription *models.DialysisPrescription) (err error) {
  194. errors := XTWriteDB().Model(&models.DialysisPrescription{}).Create(&prescription).Error
  195. return errors
  196. }
  197. func UpdateAddSigleRecord(id int64, target_ultrafiltration float64) error {
  198. err := XTWriteDB().Model(&models.DialysisPrescription{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"target_ultrafiltration": target_ultrafiltration, "prescription_water": target_ultrafiltration}).Error
  199. return err
  200. }
  201. func AddSigleSummaryRecord(summary *models.TreatmentSummary) (err error) {
  202. tx := writeDb.Begin()
  203. errors := tx.Model(&models.TreatmentSummary{}).Create(&summary).Error
  204. if err != nil {
  205. tx.Rollback()
  206. }
  207. tx.Commit()
  208. return errors
  209. }
  210. func AddSigleReceiveTreatmentAssesRecord(receiveTreatmentAsses *models.ReceiveTreatmentAsses) (err error) {
  211. tx := writeDb.Begin()
  212. errors := tx.Model(&models.ReceiveTreatmentAsses{}).Create(&receiveTreatmentAsses).Error
  213. if err != nil {
  214. tx.Rollback()
  215. }
  216. tx.Commit()
  217. return errors
  218. }
  219. func AddSigleAssessmentAfterDislysisRecord(assessmentAfterDislysis *models.AssessmentAfterDislysis) (err error) {
  220. tx := writeDb.Begin()
  221. errors := tx.Model(&models.AssessmentAfterDislysis{}).Create(&assessmentAfterDislysis).Error
  222. if err != nil {
  223. tx.Rollback()
  224. }
  225. tx.Commit()
  226. return errors
  227. }
  228. func GetSigleAssessmentAfterDislysisRecord(patient_id int64, assesment_date int64, user_org_id int64) (models.AssessmentAfterDislysis, error) {
  229. afterDislysis := models.AssessmentAfterDislysis{}
  230. err := XTReadDB().Where("patient_id=? and assessment_date = ? and user_org_id = ? and status=1", patient_id, assesment_date, user_org_id).Find(&afterDislysis).Error
  231. return afterDislysis, err
  232. }
  233. func AddSigleAssessmentBeforeDislysisRecord(assessmentBeforeDislysis *models.PredialysisEvaluation) (err error) {
  234. tx := writeDb.Begin()
  235. errors := tx.Model(&models.PredialysisEvaluation{}).Create(&assessmentBeforeDislysis).Error
  236. if err != nil {
  237. tx.Rollback()
  238. }
  239. tx.Commit()
  240. return errors
  241. }
  242. func AddSigleDoubleCheck(check *models.DoubleCheck) (err error) {
  243. tx := writeDb.Begin()
  244. errors := tx.Model(&models.DoubleCheck{}).Create(&check).Error
  245. if err != nil {
  246. tx.Rollback()
  247. }
  248. tx.Commit()
  249. return errors
  250. }
  251. // 修改透析处方
  252. func UpDateDialysisPrescription(dialysisPrescription *models.DialysisPrescription) error {
  253. tx := writeDb.Begin()
  254. err := writeDb.Save(&dialysisPrescription).Error
  255. if err != nil {
  256. tx.Rollback()
  257. }
  258. tx.Commit()
  259. return err
  260. }
  261. // 修改接诊评估
  262. func UpadateReceiveTreatmentAsses(receiveTreatmentAsses *models.ReceiveTreatmentAsses) error {
  263. err := writeDb.Save(receiveTreatmentAsses).Error
  264. return err
  265. }
  266. func GetPredialysisEvaluation(patient_id int64, org_id int64) (models.PredialysisEvaluation, error) {
  267. evaluation := models.PredialysisEvaluation{}
  268. err := XTReadDB().Where("patient_id = ? and user_org_id = ? and status =1 and symptom_before_dialysis!=''", patient_id, org_id).Last(&evaluation).Error
  269. return evaluation, err
  270. }
  271. // 修改透前评估
  272. func UpadatePredialysisEvaluation(predialysisEvaluation *models.PredialysisEvaluation) error {
  273. err := writeDb.Save(&predialysisEvaluation).Error
  274. return err
  275. }
  276. // 修改双人核对
  277. func UpdateDoubleCheck(check *models.DoubleCheck) error {
  278. err := writeDb.Save(&check).Error
  279. return err
  280. }
  281. // 修改透后评估
  282. func UpdateAssessmentAfterDislysisRecord(assessmentAfterDislysis *models.AssessmentAfterDislysis) error {
  283. err := writeDb.Save(&assessmentAfterDislysis).Error
  284. return err
  285. }
  286. func UpdatePredialysisEvaluation(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  287. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"systolic_blood_pressure": predialysisevaluation.SystolicBloodPressure}).Error
  288. return err
  289. }
  290. func UpdatePredialysisEvaluationTwo(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  291. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"diastolic_blood_pressure": predialysisevaluation.DiastolicBloodPressure}).Error
  292. return err
  293. }
  294. func UpdatePredialysisEvaluationThree(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  295. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"pulse_frequency": predialysisevaluation.PulseFrequency}).Error
  296. return err
  297. }
  298. func UpdatePredialysisEvaluationFour(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  299. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"temperature": predialysisevaluation.Temperature}).Error
  300. return err
  301. }
  302. // 修改
  303. func UpdateSummeRecord(treatmentSummary *models.TreatmentSummary) error {
  304. err := writeDb.Save(&treatmentSummary).Error
  305. return err
  306. }
  307. // func UpdateDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  308. // tx := writeDb.Begin()
  309. // err := tx.Model(&models.DoctorAdvice{}).Where("patient_id = ? AND id = ? AND status = 1 ", patientId, id).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1, "checker": m.Checker}).Error
  310. // if err != nil {
  311. // tx.Rollback()
  312. // }
  313. // tx.Commit()
  314. // return err
  315. // }
  316. func ExceDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  317. err := writeDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? AND (id = ? Or parent_id=?) AND status = 1 ", patientId, id, id).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1}).Error
  318. return err
  319. }
  320. func ExecBatchDoctorAdvice(patient_id int64, orgid int64, ids []string, creater int64) error {
  321. err := writeDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? AND user_org_id = ? and id in(?) AND status = 1 ", patient_id, orgid, ids).Updates(map[string]interface{}{"execution_time": time.Now().Unix(), "execution_staff": creater, "execution_state": 1}).Error
  322. return err
  323. }
  324. func ExceHidDoctorAdviceById(m *models.HisDoctorAdviceInfo, id int64, patientId int64) error {
  325. err := writeDb.Model(&models.HisDoctorAdviceInfo{}).Where("patient_id = ? AND (id = ? Or parent_id=?) AND status = 1 ", patientId, id, id).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1, "is_mobile": 2}).Error
  326. return err
  327. }
  328. func ExceHisBatchDotorAdvice(patientid int64, orgid int64, ids []string, creater int64) error {
  329. err := writeDb.Model(&models.HisDoctorAdviceInfo{}).Where("patient_id = ? AND user_org_id = ? and id in(?) AND status = 1 ", patientid, orgid, ids).Updates(map[string]interface{}{"execution_time": time.Now().Unix(), "execution_staff": creater, "execution_state": 1}).Error
  330. return err
  331. }
  332. func CheckDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  333. err := writeDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? AND (id = ? Or parent_id=?) AND status = 1 ", patientId, id, id).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  334. return err
  335. }
  336. func CheckHisBathDoctorAdviceById(patientId int64, orgid int64, creater int64, ids []string) error {
  337. err := writeDb.Model(&models.HisDoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and id in(?)", patientId, orgid, ids).Updates(map[string]interface{}{"check_time": time.Now().Unix(), "checker": creater, "check_state": 1}).Error
  338. return err
  339. }
  340. func CheckBatchDoctorAdviceById(patientId int64, ids []string, orgid int64, creater int64) error {
  341. err := writeDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id = ? and id in(?)", patientId, orgid, ids).Updates(map[string]interface{}{"check_time": time.Now().Unix(), "checker": creater, "check_state": 1}).Error
  342. return err
  343. }
  344. func CheckHisDoctorAdviceById(m *models.HisDoctorAdviceInfo, id int64, patientId int64) error {
  345. err := writeDb.Model(&models.HisDoctorAdviceInfo{}).Where("patient_id = ? AND (id = ? Or parent_id=?) AND status = 1 ", patientId, id, id).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  346. return err
  347. }
  348. func CreateMonitor(m *models.MonitoringRecord) (err error) {
  349. return writeDb.Create(m).Error
  350. }
  351. func UpdateMonitor(m *models.MonitoringRecord) (err error) {
  352. return writeDb.Save(m).Error
  353. }
  354. func DelMonitor(patientId int64, recordId int64) error {
  355. tx := writeDb.Begin()
  356. updateTime := time.Now().Unix()
  357. err := tx.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND id = ? AND status = 1 ", patientId, recordId).Updates(map[string]interface{}{"status": 0, "updated_time": updateTime}).Error
  358. if err != nil {
  359. tx.Rollback()
  360. }
  361. tx.Commit()
  362. return err
  363. }
  364. //修改长期处方
  365. func UpdateSolution(solution *models.DialysisSolution) {
  366. writeDb.Save(&solution)
  367. }
  368. // 创建长期处方
  369. func CreateSolution(solution *models.DialysisSolution) {
  370. writeDb.Create(&solution)
  371. }
  372. func SavePrescriptionAndCreateSolution(solution *models.DialysisSolution, prescription *models.DialysisPrescription) (err error) {
  373. tx := writeDb.Begin()
  374. if prescription.ID > 0 {
  375. err = tx.Save(&prescription).Error
  376. } else {
  377. err = tx.Model(&models.DialysisPrescription{}).Create(&prescription).Error
  378. }
  379. if err != nil {
  380. tx.Rollback()
  381. return
  382. }
  383. err = tx.Create(&solution).Error
  384. if err != nil {
  385. tx.Rollback()
  386. return
  387. }
  388. tx.Commit()
  389. return
  390. }
  391. // 透析处方
  392. func FindDialysisPrescriptionByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DialysisPrescription) {
  393. dialysisPrescription := models.DialysisPrescription{}
  394. err := readDb.Model(&dialysisPrescription).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ? ", id, recordDate, orgId).Find(&dialysisPrescription).Error
  395. return err, &dialysisPrescription
  396. }
  397. func FindReceiveTreatmentAssesByReordDate(id int64, recordDate int64, orgId int64) (error, *models.ReceiveTreatmentAsses) {
  398. receiveTreatmentAsses := models.ReceiveTreatmentAsses{}
  399. err := readDb.Model(&receiveTreatmentAsses).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).Find(&receiveTreatmentAsses).Error
  400. return err, &receiveTreatmentAsses
  401. }
  402. // 透前评估
  403. func FindPredialysisEvaluationByReordDate(id int64, recordDate int64, orgId int64) (error, *models.PredialysisEvaluation) {
  404. dialysisEvaluation := models.PredialysisEvaluation{}
  405. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&dialysisEvaluation).Error
  406. return err, &dialysisEvaluation
  407. }
  408. // 双人核对
  409. func FindDoubleCheckByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoubleCheck) {
  410. check := models.DoubleCheck{}
  411. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? AND check_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&check).Error
  412. return err, &check
  413. }
  414. func FindAssessmentAfterDislysisByReordDate(id int64, recordDate int64, orgId int64) (error, *models.AssessmentAfterDislysis) {
  415. assessmentAfterDislysis := models.AssessmentAfterDislysis{}
  416. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&assessmentAfterDislysis).Error
  417. return err, &assessmentAfterDislysis
  418. }
  419. func FindTreatmentSummaryByReordDate(id int64, recordDate int64, orgId int64) (error, *models.TreatmentSummary) {
  420. summary := models.TreatmentSummary{}
  421. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&summary).Error
  422. return err, &summary
  423. }
  424. func FindDoctorAdviceByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoctorAdvice) {
  425. advice := models.DoctorAdvice{}
  426. err := readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? AND record_date = ? AND parent_id = 0 AND status = 1 AND user_org_id = ? ", id, recordDate, orgId).First(&advice).Error
  427. return err, &advice
  428. }
  429. func FindDialysisRecordById(orgId int64, id int64, recordDate int64) (error, *models.DialysisOrder) {
  430. var count int64
  431. dialysisRecord := models.DialysisOrder{}
  432. err := readDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgId, id, recordDate).Find(&dialysisRecord).Count(&count).Error
  433. if count <= 0 {
  434. return err, nil
  435. }
  436. return err, &dialysisRecord
  437. }
  438. func FindDialysisSchedualById(orgId int64, id int64, recordDate int64) (error, *models.Schedule) {
  439. var count int64
  440. schedule := models.Schedule{}
  441. err := readDb.Model(&models.Schedule{}).Where("user_org_id = ? AND patient_id = ? AND schedule_date = ?", orgId, id, recordDate).Find(&schedule).Count(&count).Error
  442. if count <= 0 {
  443. return err, nil
  444. }
  445. return err, &schedule
  446. }
  447. func CreateDialysisRecord(id int64, orgId int64, record *models.DialysisOrder) error {
  448. err := writeDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? ", orgId, id).Create(&record).Error
  449. if err != nil {
  450. return err
  451. }
  452. dialysisPrescriptionErr := UpDateRecordIdToDialysisPrescription(id, orgId, record.DialysisDate, record.ID)
  453. if dialysisPrescriptionErr != nil {
  454. return dialysisPrescriptionErr
  455. }
  456. receiveTreatmentAssesErr := UpDateRecordIdToReceiveTreatmentAsses(id, orgId, record.DialysisDate, record.ID)
  457. if receiveTreatmentAssesErr != nil {
  458. return receiveTreatmentAssesErr
  459. }
  460. predialysisEvaluationErr := UpDateRecordIdToPredialysisEvaluation(id, orgId, record.DialysisDate, record.ID)
  461. if predialysisEvaluationErr != nil {
  462. return predialysisEvaluationErr
  463. }
  464. treatmentSummaryErr := UpDateRecordIdToTreatmentSummary(id, orgId, record.DialysisDate, record.ID)
  465. if treatmentSummaryErr != nil {
  466. return treatmentSummaryErr
  467. }
  468. doubleCheckErr := UpDateRecordIdToDoubleCheck(id, orgId, record.DialysisDate, record.ID)
  469. if doubleCheckErr != nil {
  470. return doubleCheckErr
  471. }
  472. adviceErrr := UpDateRecordIdToDoctorAdvice(id, orgId, record.DialysisDate, record.ID)
  473. if adviceErrr != nil {
  474. return adviceErrr
  475. }
  476. //透析监测
  477. records, err := FindAllMonitorRecord(orgId, id, record.DialysisDate)
  478. if err != nil {
  479. return err
  480. }
  481. var Ids []int64
  482. for _, item := range records {
  483. Ids = append(Ids, item.ID)
  484. }
  485. monitorErr := UpDateRecordIdToMonitor(Ids, record.ID)
  486. if monitorErr != nil {
  487. return monitorErr
  488. }
  489. return nil
  490. }
  491. func ModifyDialysisRecord(id int64, finishNurse int64, end_time int64, admin_user_id int64, puncture_point_haematoma int64, internal_fistula string, catheter string, cruor string, mission string, condenser string) error {
  492. err := writeDb.Model(&models.DialysisOrder{}).Where("id = ? AND status = 1 ", id).Updates(map[string]interface{}{"updated_time": time.Now().Unix(), "stage": 2, "finish_nurse": finishNurse, "end_time": end_time, "finish_creator": admin_user_id, "finish_modifier": admin_user_id, "puncture_point_haematoma": puncture_point_haematoma, "blood_access_internal_fistula": internal_fistula, "catheter": catheter, "cruor": cruor, "mission": mission, "condenser": condenser}).Error
  493. return err
  494. }
  495. //func ModifyDialysisRecord(id int64, finishNurse int64, end_time int64, admin_user_id int64) error {
  496. // err := writeDb.Model(&models.DialysisOrder{}).Where("id = ? AND status = 1 ", id).Updates(map[string]interface{}{"updated_time": time.Now().Unix(), "stage": 2, "finish_nurse": finishNurse, "end_time": end_time, "finish_creator": admin_user_id, "finish_modifier": admin_user_id}).Error
  497. // return err
  498. //
  499. //}
  500. func UpdateSolutionByPatientId(id int64) error {
  501. err := writeDb.Model(&models.Patients{}).Where("id = ? AND status = 1 ", id).UpdateColumn("total_dialysis", gorm.Expr("total_dialysis + ?", 1)).Error
  502. return err
  503. }
  504. // 更新透析处方记录ID
  505. func UpDateRecordIdToDialysisPrescription(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  506. errors := writeDb.Model(&models.DialysisPrescription{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? ", orgId, patientId, recordTime).Updates(map[string]interface{}{"record_id": recordId}).Error
  507. return errors
  508. }
  509. func UpDateRecordIdToReceiveTreatmentAsses(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  510. errors := writeDb.Model(&models.ReceiveTreatmentAsses{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? ", orgId, patientId, recordTime).Updates(map[string]interface{}{"record_id": recordId}).Error
  511. return errors
  512. }
  513. func UpDateRecordIdToPredialysisEvaluation(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  514. errors := writeDb.Model(&models.PredialysisEvaluation{}).Where("user_org_id = ? AND patient_id = ? AND assessment_date = ? ", orgId, patientId, recordTime).Updates(map[string]interface{}{"dialysis_order_id": recordId}).Error
  515. return errors
  516. }
  517. func UpDateRecordIdToAssessmentAfterDislysis(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  518. errors := writeDb.Model(&models.AssessmentAfterDislysis{}).Where("user_org_id = ? AND patient_id = ? AND assessment_date = ? ", orgId, patientId, recordTime).Updates(map[string]interface{}{"dialysis_order_id": recordId}).Error
  519. return errors
  520. }
  521. func UpDateRecordIdToTreatmentSummary(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  522. errors := writeDb.Model(&models.TreatmentSummary{}).Where("user_org_id = ? AND patient_id = ? AND assessment_date = ? ", orgId, patientId, recordTime).Updates(map[string]interface{}{"dialysis_order_id": recordId}).Error
  523. return errors
  524. }
  525. func UpDateRecordIdToMonitor(Ids []int64, recordId int64) error {
  526. errors := writeDb.Model(&models.MonitoringRecord{}).Where("id IN (?)", Ids).Update(map[string]interface{}{"dialysis_order_id": recordId}).Error
  527. return errors
  528. }
  529. func UpDateRecordIdToDoubleCheck(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  530. errors := writeDb.Model(&models.DoubleCheck{}).Where("user_org_id = ? AND patient_id = ? AND check_date = ? ", orgId, patientId, recordTime).Updates(map[string]interface{}{"dialysis_order_id": recordId}).Error
  531. return errors
  532. }
  533. func UpDateRecordIdToDoctorAdvice(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  534. errors := writeDb.Model(&models.DoctorAdvice{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? ", orgId, patientId, recordTime).Updates(map[string]interface{}{"dialysis_order_id": recordId}).Error
  535. return errors
  536. }
  537. // 透析处方
  538. func FindPatientPrescribeWidyDevideById(orgID int64, patientsId int64, recordDate int64) (patient models.DialysisPrescriptionAddtional, err error) {
  539. err = readDb.Preload("Device", "status = 1 AND org_id = ?", orgID).
  540. Model(&models.DialysisPrescriptionAddtional{}).
  541. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? ", patientsId, orgID, recordDate).
  542. First(&patient).Error
  543. return
  544. }
  545. func FindDialysisMode(orgID int64, patientId int64, scheduleDate int64) (models.XtSchedule, error) {
  546. schedule := models.XtSchedule{}
  547. err = readDb.Where("user_org_id = ? and patient_id = ? and schedule_date = ? ", orgID, patientId, scheduleDate).Find(&schedule).Error
  548. return schedule, err
  549. }
  550. func FindDialysisMacher(orgid int64, patientid int64, dialysisDate int64) (models.XtDialysisOrders, error) {
  551. orders := models.XtDialysisOrders{}
  552. db := readDb.Table("xt_dialysis_order as o").Where("o.status = 1")
  553. table := readDb.Table("xt_device_number as n")
  554. fmt.Print("table", table)
  555. err := db.Select("o.id,o.dialysis_date,o.user_org_id,o.patient_id,o.prescription_id,o.stage,o.remark,o.bed_id,o.start_nurse,o.finish_nurse,o.status,o.created_time,o.updated_time,o.start_time,o.end_time,o.puncture_nurse,o.creator,o.modifier,o.finish_creator,o.finish_modifier,o.schedual_type,o.quality_nurse_id,o.blood_drawing,n.number,r.user_name").Joins("left join xt_device_number as n on n.id = o.bed_id").Joins("left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = o.start_nurse").Where("o.user_org_id = ? and o.patient_id = ? and o.dialysis_date =?", orgid, patientid, dialysisDate).Scan(&orders).Error
  556. return orders, err
  557. }
  558. func FindPunctureNurse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  559. role := models.App_Roles{}
  560. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  561. //return role, err
  562. db := UserReadDB().Table("sgj_user_admin_role as r")
  563. err := db.Select("r.id,r.admin_user_id,r.org_id,r.role_id,r.user_name,r.avatar,r.user_type,r.user_title,r.intro,r.status,r.ctime,e.url").Joins("left join sgj_user_admin_electronic_signature as e on e.creator = r.admin_user_id").Where("r.admin_user_id = ? and r.app_id = ? and r.org_id = ?", adminid, appid, orgid).Find(&role).Error
  564. return role, err
  565. }
  566. func FindeStartNuse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  567. role := models.App_Roles{}
  568. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  569. err := db.Select("r.id,r.admin_user_id,r.org_id,r.role_id,r.user_name,r.avatar,r.user_type,r.user_title,r.intro,r.status,r.ctime,e.url").Joins("left join sgj_user_admin_electronic_signature as e on e.creator = r.admin_user_id").Where("r.admin_user_id = ? and r.app_id = ? and r.org_id = ?", adminid, appid, orgid).Find(&role).Error
  570. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  571. return role, err
  572. }
  573. func FindeQualityNurse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  574. role := models.App_Roles{}
  575. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  576. err := db.Select("r.id,r.admin_user_id,r.org_id,r.role_id,r.user_name,r.avatar,r.user_type,r.user_title,r.intro,r.status,r.ctime,e.url").Joins("left join sgj_user_admin_electronic_signature as e on e.creator = r.admin_user_id").Where("r.admin_user_id = ? and r.app_id = ? and r.org_id = ?", adminid, appid, orgid).Find(&role).Error
  577. return role, err
  578. }
  579. func FindDialysisWay(orgid int64, patientid int64, recorddate int64) (models.DialysisPrescription, error) {
  580. prescription := models.DialysisPrescription{}
  581. err := readDb.Where("user_org_id = ? and patient_id = ? and record_date = ? and status =1", orgid, patientid, recorddate).Find(&prescription).Error
  582. return prescription, err
  583. }
  584. func FindDoctorOrder(orgid int64, patientid int64, adviceDate int64) (models.XtDoctorAdvice, error) {
  585. advices := models.XtDoctorAdvice{}
  586. db := readDb.Table("xt_doctor_advice as a").Where("a.status = ?", 1)
  587. err := db.Select("a.id,a.user_org_id,a.patient_id,a.advice_type,a.advice_date,a.start_time,a.advice_name,a.advice_desc,a.reminder_date,a.single_dose,a.single_dose_unit,a.prescribing_number,a.prescribing_number_unit,a.delivery_way,a.execution_frequency,a.advice_doctor,a.status,a.created_time,a.updated_time,a.advice_affirm,a.remark,a.stop_time,a.stop_reason,a.stop_doctor,a.stop_state,a.parent_id,a.execution_time,a.execution_staff,a.execution_state,a.checker,a.record_date,a.dialysis_order_id,a.check_time,a.check_state,a.drug_spec,a.drug_spec_unit,a.groupno,a.remind_type,a.frequency_type,a.day_count,a.week_day,a.template_id").Where("a.user_org_id = ? and a.patient_id = ? and a.advice_date = ?", orgid, patientid, adviceDate).Find(&advices).Error
  588. return advices, err
  589. }
  590. func FindDoctor(adminuserid int64, appid int64, orgid int64) (models.App_Role, error) {
  591. role := models.App_Role{}
  592. err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminuserid, appid, orgid).Find(&role).Error
  593. return role, err
  594. }
  595. func FindDoctorName(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  596. role := models.App_Roles{}
  597. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  598. err := db.Select("r.id,r.admin_user_id,r.org_id,r.role_id,r.user_name,r.avatar,r.user_type,r.user_title,r.intro,r.status,r.ctime,e.url").Joins("left join sgj_user_admin_electronic_signature as e on e.creator = r.admin_user_id").Where("r.admin_user_id = ? and r.app_id = ? and r.org_id = ?", adminid, appid, orgid).Find(&role).Error
  599. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  600. return role, err
  601. }
  602. // ///////////// 新 18-10-22
  603. func GetDialysisScheduals(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  604. var vms []*MDialysisScheduleVM
  605. db := readDb.
  606. Table("xt_schedule").
  607. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  608. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  609. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  610. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).
  611. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  612. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  613. Preload("TreatmentSummary", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  614. Preload("Advices", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  615. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  616. Where("status = 1 AND user_org_id = ?", orgID)
  617. if scheduleDate != 0 {
  618. db = db.Where("schedule_date = ?", scheduleDate)
  619. }
  620. err := db.Find(&vms).Error
  621. return vms, err
  622. }
  623. func GetDialysisSchedualsOne(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  624. var vms []*MDialysisScheduleVM
  625. db := readDb.
  626. Table("xt_schedule").
  627. Preload("DeviceNumber", "org_id = ?", orgID).
  628. Preload("DeviceNumber.Zone", "org_id = ?", orgID).
  629. //Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).
  630. //Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  631. //Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  632. //Preload("TreatmentSummary", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  633. //Preload("Advices", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  634. //Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  635. Where("status = 1 AND user_org_id = ?", orgID)
  636. if scheduleDate != 0 {
  637. db = db.Where("schedule_date = ?", scheduleDate)
  638. }
  639. err := db.Find(&vms).Error
  640. return vms, err
  641. }
  642. func GetLastAcceptsAssessment(patient int64, orgId int64) (error, models.ReceiveTreatmentAsses) {
  643. var receiveTreatmentAsses models.ReceiveTreatmentAsses
  644. err := readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient, orgId).Last(&receiveTreatmentAsses).Error
  645. return err, receiveTreatmentAsses
  646. }
  647. func GetDialysisWatch(orgID int64, schedulDate int64, schedulType int64, partitionType int64, page int64, limit int64, start int64, end int64) (schedule []*models.DialysisSchedule, err error, total int64) {
  648. db := readDb.Model(&models.MonitorDialysisSchedule{})
  649. if start > 0 && end > 0 {
  650. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  651. Preload("TreatmentMode", "status = 1").
  652. Preload("MonitoringRecord", func(db *gorm.DB) *gorm.DB {
  653. return db.Where("status = 1 AND user_org_id = ? and monitoring_date >=? and monitoring_date<=? ", orgID, start, end).Order("operate_time asc")
  654. })
  655. db = db.Where("xt_schedule.status = 1")
  656. }
  657. if schedulDate > 0 {
  658. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  659. Preload("TreatmentMode", "status = 1").
  660. Preload("MonitoringRecord", func(db *gorm.DB) *gorm.DB {
  661. return db.Where("status = 1 AND user_org_id = ? and monitoring_date >=? and monitoring_date<=? ", orgID, schedulDate, schedulDate).Order("operate_time asc")
  662. })
  663. db = db.Where("xt_schedule.status = 1")
  664. db = db.Where("schedule_date = ?", schedulDate)
  665. }
  666. if schedulType > 0 {
  667. db = db.Where("schedule_type = ?", schedulType)
  668. }
  669. if start > 0 {
  670. db = db.Where("schedule_date >= ?", start)
  671. }
  672. if end > 0 {
  673. db = db.Where("schedule_date <= ?", end)
  674. }
  675. if orgID > 0 {
  676. db = db.Where("user_org_id = ?", orgID)
  677. }
  678. if partitionType > 0 {
  679. db = db.Joins("inner join xt_device_number on xt_device_number.id = xt_schedule.bed_id and xt_device_number.zone_id = ?", partitionType)
  680. }
  681. offset := (page - 1) * limit
  682. err = db.Count(&total).Offset(offset).Limit(limit).Order("bed_id desc").Find(&schedule).Error
  683. return schedule, err, total
  684. }
  685. //func GetDialysisWatchByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64) (schedules []*models.DialysisSchedule, err error, total int64) {
  686. // db := readDb.Model(&models.DialysisSchedule{}).Where("status = 1")
  687. //
  688. // if schedulType > 0 {
  689. // db = db.Where("scheudle_type = ?", schedulType)
  690. // }
  691. //
  692. // if partitionType > 0 {
  693. // db = db.Where("partition_id = ?", partitionType)
  694. // }
  695. //
  696. // err = db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  697. // return schedules, err, total
  698. //}
  699. func GetDialysisWatchByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64, start int64, end int64) ([]*models.DialysisSchedule, error, int64) {
  700. var patients []*models.Patients
  701. 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
  702. if getPatientErr != nil {
  703. return nil, getPatientErr, 0
  704. }
  705. patientIDs := make([]int64, len(patients))
  706. for index, patient := range patients {
  707. patientIDs[index] = patient.ID
  708. }
  709. db := readDb.Model(&models.DialysisSchedule{})
  710. if start > 0 && end > 0 {
  711. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  712. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  713. Preload("TreatmentMode", "status = 1").
  714. Preload("Prescription", "status = 1 AND user_org_id = ? and record_date>=? and record_date<=?", orgID, start, end).
  715. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date>=? and assessment_date<=?", orgID, start, end).
  716. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date>=? and assessment_date<=?", orgID, start, end).
  717. Preload("MonitoringRecord", "status = 1 AND user_org_id = ? and monitoring_date>=? and monitoring_date<=?", orgID, start, end).
  718. Preload("DialysisOrder", "status = 1 AND user_org_id = ? and dialysis_date >= ? and dialysis_date<=?", orgID, start, end).
  719. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  720. } else {
  721. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  722. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  723. Preload("TreatmentMode", "status = 1").
  724. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  725. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  726. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  727. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  728. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  729. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  730. }
  731. db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
  732. if schedulType > 0 {
  733. db = db.Where("schedule_type = ?", schedulType)
  734. }
  735. if start > 0 {
  736. db = db.Where("schedule_date >= ?", start)
  737. }
  738. if end > 0 {
  739. db = db.Where("schedule_date<=?", end)
  740. }
  741. if partitionType > 0 {
  742. db = db.Joins("inner join xt_device_number as d_n on d_n.id = xt_schedule.bed_id and d_n.zone_id = ?", partitionType)
  743. // db = db.Where("partition_id = ?", partitionType)
  744. }
  745. var schedules []*models.DialysisSchedule
  746. total := int64(0)
  747. err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  748. return schedules, err, total
  749. }
  750. func GetAdminUserES(orgID int64, appID int64, ids []int64) (es []*models.AdminUserElectronicSignature, err error) {
  751. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id=? and creator in (?) and status=1", orgID, appID, ids).Find(&es).Error
  752. return
  753. }
  754. //
  755. //func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  756. //
  757. // db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  758. // table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  759. // fmt.Println("table", table)
  760. // err = db.Select("x.id,x.admin_user_id,x.user_name,x.org_id,s.creator,s.url,s.hash").Joins("left join sgj_user_admin_electronic_signature as s on s.creator = x.admin_user_id").Where("x.org_id = ?", orgid).Scan(&admin).Error
  761. // return admin, err
  762. //}
  763. func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  764. db := readUserDb.Table("sgj_user_admin_role as x")
  765. table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  766. fmt.Println("table", table)
  767. err = db.Select("x.id,x.admin_user_id,x.user_name,x.org_id,s.creator,s.url,s.hash").Joins("left join sgj_user_admin_electronic_signature as s on s.creator = x.admin_user_id").Where("x.org_id = ?", orgid).Scan(&admin).Error
  768. return admin, err
  769. }
  770. func GetAdminUserEsTwo(orgid int64, aapid int64) (admin []*models.UserAdminRoles, err error) {
  771. db := readUserDb.Table("sgj_user_admin_role as x")
  772. table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  773. fmt.Println("table", table)
  774. err = db.Select("x.id,x.admin_user_id,x.user_name,x.org_id,s.creator,s.url,s.hash").Joins("left join sgj_user_admin_electronic_signature as s on s.creator = x.admin_user_id").Where("x.org_id = ? and x.app_id =? and x.status=1", orgid, aapid).Group("user_name").Scan(&admin).Error
  775. return admin, err
  776. }
  777. func UpDateDialysisPrescriptionDoctorSign(patient int64, date int64, orgId int64, doctor_id int64) (err error) {
  778. err = writeDb.Model(&models.DialysisPrescription{}).Where("user_org_id = ? AND patient_id = ? AND record_date = ? ", orgId, patient, date).Updates(map[string]interface{}{"prescription_doctor": doctor_id}).Error
  779. return
  780. }
  781. func GetAllAdminUserES(orgID int64, appId int64) (es []*models.AdminUserElectronicSignature, err error) {
  782. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id = ? and status=1", orgID, appId).Find(&es).Error
  783. return
  784. }
  785. func GetAllStarfEs(orgid int64) (es []*models.SgjUserAdminRoles, err error) {
  786. redis := RedisClient()
  787. defer redis.Close()
  788. // cur_date := time.Now().Format("2006-01-02")
  789. key := strconv.FormatInt(orgid, 10) + ":starfes"
  790. starfes_str, _ := redis.Get(key).Result()
  791. redis.Set(key, "", time.Second)
  792. if len(starfes_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  793. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  794. err = db.Select("x.id,x.admin_user_id,x.org_id,x.app_id,x.role_id,x.user_name,x.avatar,x.user_type,s.creator,s.url,s.hash").Joins("left join sgj_user_admin_electronic_signature as s on s.creator = x.admin_user_id and s.status = 1").Where("x.org_id = ?", orgid).Scan(&es).Error
  795. if err != nil {
  796. if err == gorm.ErrRecordNotFound {
  797. if len(es) <= 0 {
  798. redis.Set(key, "starfes_str_null", time.Second*60*60*18)
  799. }
  800. return nil, nil
  801. } else {
  802. return nil, err
  803. }
  804. } else {
  805. if len(es) > 0 {
  806. //缓存数据
  807. starfes_str, err := json.Marshal(es)
  808. if err == nil {
  809. redis.Set(key, starfes_str, time.Second*60*60*18)
  810. return es, nil
  811. }
  812. } else {
  813. redis.Set(key, "starfes_str_null", time.Second*60*60*18)
  814. return es, nil
  815. }
  816. return es, nil
  817. }
  818. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  819. if starfes_str == "starfes_str_null" {
  820. return es, nil
  821. } else {
  822. json.Unmarshal([]byte(starfes_str), &es)
  823. return es, nil
  824. }
  825. }
  826. }
  827. func FindLastMonitorRecord(patient_id int64, org_id int64) (record models.MonitoringRecord, err error) {
  828. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient_id, org_id).Last(&record).Error
  829. return
  830. }
  831. func FindLastMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  832. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND status = 1 AND user_org_id = ? AND monitoring_date = ?", patient_id, org_id, monitoring_date).Last(&record).Error
  833. return
  834. }
  835. func FindFirstMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  836. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND status = 1 AND user_org_id = ? AND monitoring_date = ?", patient_id, org_id, monitoring_date).First(&record).Error
  837. return
  838. }
  839. func ExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  840. err = writeDb.Model(&models.DoctorAdvice{}).Where("groupno = ? AND user_org_id = ? AND status = 1 AND execution_state = 2", groupNo, org_id).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1}).Error
  841. return err
  842. }
  843. func GetExceDoctorAdviceGroupNo(groupNo int64, org_id int64) (advice []*models.DoctorAdvice, err error) {
  844. err = XTReadDB().Where("groupno = ? AND user_org_id = ? AND status = 1", groupNo, org_id).Find(&advice).Error
  845. return advice, err
  846. }
  847. func ExceOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  848. ut := writeDb.Begin()
  849. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND id = ?", advice_id).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1}).Error
  850. if err != nil {
  851. ut.Rollback()
  852. return
  853. }
  854. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id = ?", advice_id).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1}).Error
  855. if err != nil {
  856. ut.Rollback()
  857. return
  858. }
  859. ut.Commit()
  860. return err
  861. }
  862. func CheckDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  863. ut := writeDb.Begin()
  864. err = ut.Model(&models.DoctorAdvice{}).Where("groupno = ? AND user_org_id = ? AND status = 1 AND check_state = 0", groupNo, org_id).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  865. if err != nil {
  866. ut.Rollback()
  867. return
  868. }
  869. ut.Commit()
  870. return err
  871. }
  872. func CheckDoctorAdviceByGroupNoOne(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  873. ut := writeDb.Begin()
  874. err = ut.Model(&models.DoctorAdvice{}).Where("groupno = ? AND user_org_id = ? AND status = 1", groupNo, org_id).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  875. if err != nil {
  876. ut.Rollback()
  877. return
  878. }
  879. ut.Commit()
  880. return err
  881. }
  882. func CheckOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  883. ut := writeDb.Begin()
  884. err = writeDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND id = ?", advice_id).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  885. if err != nil {
  886. ut.Rollback()
  887. return
  888. }
  889. err = writeDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id = ?", advice_id).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  890. if err != nil {
  891. ut.Rollback()
  892. return
  893. }
  894. ut.Commit()
  895. return err
  896. }
  897. func DeleteDoctorAdviceByGroupNo(m *models.DoctorAdvice) (err error) {
  898. ut := writeDb.Begin()
  899. err = ut.Model(&models.DoctorAdvice{}).Where("groupno = ? AND user_org_id = ?", m.GroupNo, m.UserOrgId).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0, "modifier": m.Modifier}).Error
  900. if err != nil {
  901. ut.Rollback()
  902. return
  903. }
  904. ut.Commit()
  905. return
  906. }
  907. func GetDialysisBoard(orgID int64, scheduleDate int64, offset, limit int64) ([]*models.DialysisBoardVM, error) {
  908. var boards []*models.DialysisBoardVM
  909. db := readDb.
  910. Table("xt_schedule").
  911. Preload("Patient", "status = 1 AND user_org_id = ?", orgID).
  912. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  913. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  914. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  915. Preload("SigninAndWeigh", "status = 1 AND user_org_id = ?", orgID).
  916. Preload("NextSchedules", func(db *gorm.DB) *gorm.DB {
  917. return db.Where("user_org_id = ? AND schedule_date > ? AND status = 1", orgID, scheduleDate).
  918. Order("schedule_date asc, id asc")
  919. }).
  920. Where("status = 1 AND user_org_id = ?", orgID)
  921. if scheduleDate != 0 {
  922. db = db.Where("schedule_date = ?", scheduleDate)
  923. }
  924. err := db.Offset(offset).Limit(limit).Find(&boards).Error
  925. return boards, err
  926. }
  927. func GetOneDialysisOrder(orgID, orderData int64, patient_id int64) (*models.DialysisOrder, error) {
  928. var order models.DialysisOrder
  929. var err error
  930. err = readDb.Model(&models.DialysisOrder{}).Where("user_org_id=? and dialysis_date=? and status=1 and patient_id = ?", orgID, orderData, patient_id).First(&order).Error
  931. if err == gorm.ErrRecordNotFound {
  932. return nil, nil
  933. }
  934. if err != nil {
  935. return nil, err
  936. }
  937. return &order, nil
  938. }
  939. func GetOneDialysisOrderOne(orgID, orderData int64, patient_id int64) (models.DialysisOrder, error) {
  940. order := models.DialysisOrder{}
  941. err := readDb.Where("user_org_id=? and dialysis_date=? and status=1 and patient_id = ?", orgID, orderData, patient_id).Find(&order).Error
  942. return order, err
  943. }
  944. func GetPatientScheduleByPatientId(patient_id int64, user_org_id int64, schedule_date int64) (models.XtSchedule, error) {
  945. schedule := models.XtSchedule{}
  946. err := XTReadDB().Where("patient_id = ? and user_org_id = ? and schedule_date = ? and status=1", patient_id, user_org_id, schedule_date).Find(&schedule).Error
  947. return schedule, err
  948. }
  949. func GetPscheduleDate(schedule_date int64, patient_id int64, user_org_id int64) (models.XtSchedule, error) {
  950. schedule := models.XtSchedule{}
  951. err := XTReadDB().Where("schedule_date = ? and patient_id = ? and user_org_id =? and status =1", schedule_date, patient_id, user_org_id).Find(&schedule).Error
  952. return schedule, err
  953. }
  954. func UpdateDialysiOrderByPatientId(id int64, schedule_type int64, partion_id int64, bed_id int64) error {
  955. err := XTWriteDB().Model(&models.DialysisOrder{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"schedual_type": schedule_type, "zone_id": partion_id, "bed_id": bed_id}).Error
  956. return err
  957. }
  958. func GetOrgInfoTemplate(orgID int64) (models.GobalTemplate, error) {
  959. var templateInfo models.GobalTemplate
  960. var err error
  961. err = readDb.Model(&models.GobalTemplate{}).Where("org_id=? and status=1", orgID).First(&templateInfo).Error
  962. return templateInfo, err
  963. }
  964. func FindAdminRoleTypeById(orgId int64, adminUserId int64, appId int64) (models.App_Role, error) {
  965. var appRole models.App_Role
  966. var err error
  967. err = readUserDb.Model(&models.App_Role{}).Where("org_id =? and app_id = ? and admin_user_id = ? and status=1", orgId, appId, adminUserId).First(&appRole).Error
  968. return appRole, err
  969. }
  970. func BatchExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  971. err = writeDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?) AND execution_state = 2", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1}).Error
  972. err = writeDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?) AND execution_state = 2 ", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1}).Error
  973. return err
  974. }
  975. func BatchCheckOldDoctorAdvice(m *models.DoctorAdvice, ids []string, org_id int64, date int64) (err error) {
  976. err = writeDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?) AND user_org_id = ? AND advice_date = ?", ids, org_id, date).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  977. err = writeDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?) AND user_org_id = ? AND advice_date = ?", ids, org_id, date).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  978. return err
  979. }
  980. func ModifyScheduleMode(mode_id int64, patient_id int64, recordDate int64, org_id int64, dialysis_machine_name string) {
  981. ut := writeDb.Begin()
  982. err = writeDb.Model(&models.Schedule{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND schedule_date = ?", org_id, patient_id, recordDate).Updates(map[string]interface{}{"updated_time": time.Now().Unix(), "mode_id": mode_id, "dialysis_machine_name": dialysis_machine_name}).Error
  983. if err != nil {
  984. ut.Rollback()
  985. return
  986. }
  987. ut.Commit()
  988. }
  989. func ModifyScheduleModeOne(mode_id int64, patient_id int64, recordDate int64, org_id int64, dialysis_machine_name string) {
  990. ut := writeDb.Begin()
  991. err = writeDb.Model(&models.Schedule{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND schedule_date >= ? mode_id= ?", org_id, patient_id, recordDate, mode_id).Updates(map[string]interface{}{"updated_time": time.Now().Unix(), "dialysis_machine_name": dialysis_machine_name}).Error
  992. if err != nil {
  993. ut.Rollback()
  994. return
  995. }
  996. ut.Commit()
  997. }
  998. func FindDialysisOrderById(id int64) (error, *models.DialysisOrder) {
  999. dialysisRecord := models.DialysisOrder{}
  1000. err := readDb.Model(&models.DialysisOrder{}).Where("id = ? AND status = 1", id).Find(&dialysisRecord).Error
  1001. return err, &dialysisRecord
  1002. }
  1003. func ModifyExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  1004. ut := writeDb.Begin()
  1005. err = ut.Model(&models.DoctorAdvice{}).Where("groupno = ? AND user_org_id = ? AND status = 1 AND execution_state = 1", groupNo, org_id).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  1006. if err != nil {
  1007. ut.Rollback()
  1008. return
  1009. }
  1010. ut.Commit()
  1011. return err
  1012. }
  1013. func ModifyExceDoctorAdviceByGroupNoOne(m *models.DoctorAdvice, ids []string) (err error) {
  1014. ut := writeDb.Begin()
  1015. err = ut.Model(&models.DoctorAdvice{}).Where("id in(?) and status =1", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  1016. if err != nil {
  1017. ut.Rollback()
  1018. return
  1019. }
  1020. ut.Commit()
  1021. return err
  1022. }
  1023. func BatchModifyExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  1024. ut := writeDb.Begin()
  1025. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?) AND execution_state = 1", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  1026. if err != nil {
  1027. ut.Rollback()
  1028. return
  1029. }
  1030. err = ut.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?) AND execution_state = 1 ", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  1031. if err != nil {
  1032. ut.Rollback()
  1033. return
  1034. }
  1035. ut.Commit()
  1036. return err
  1037. }
  1038. func UpdateOrderCount(good_type_id int64, good_id int64, number string, org_id int64) {
  1039. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND is_sys = 1 AND warehouse_out_order_number = ? ", good_id, good_type_id, org_id, number).UpdateColumn("count", gorm.Expr("count - ?", 1))
  1040. }
  1041. func UpdateOrderInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  1042. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND warehouse_out_order_number = ? AND patient_id = ?", good_id, good_type_id, org_id, out.WarehouseOutOrderNumber, patient_id).Updates(map[string]interface{}{"good_id": newNiprocart, "warehouse_out_id": out.ID, "warehouse_out_order_number": out.WarehouseOutOrderNumber})
  1043. }
  1044. func FindOrderInfoByGoodId(good_type_id int64, good_id int64, number string, org_id int64) (err error, out models.WarehouseOutInfo) {
  1045. err = writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND warehouse_out_order_number = ? ", good_id, good_type_id, org_id, number).Find(&out).Error
  1046. return
  1047. }
  1048. func FindGoodUserById(good_type_id int64, good_id int64, id int64, org_id int64, record_time int64) (user models.AutomaticReduceDetail, err error) {
  1049. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND good_type_id = ? AND patient_id = ? AND org_id = ? AND record_time = ? ", good_id, good_type_id, id, org_id, record_time).First(&user).Error
  1050. return
  1051. }
  1052. func FindOldOrderInfoByGoodId(time int64, org_id int64) (err error, out models.WarehouseOutInfo) {
  1053. err = readDb.Model(&models.WarehouseOutInfo{}).Where(" org_id = ? AND status = 1 AND sys_record_time = ? ", org_id, time).First(&out).Error
  1054. return
  1055. }
  1056. func FindStockOutOrderNumberByTime(time int64, org_id int64) (err error, out models.WarehouseOut) {
  1057. err = readDb.Model(&models.WarehouseOut{}).Where(" org_id = ? AND status = 1 AND warehouse_out_time = ? ", org_id, time).First(&out).Error
  1058. return
  1059. }
  1060. func UpdateDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  1061. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND warehouse_out_order_number = ? AND patient_id = ?", good_id, good_type_id, org_id, out.WarehouseOutOrderNumber, patient_id).Updates(map[string]interface{}{"good_id": newNiprocart, "warehouse_out_id": out.ID, "warehouse_out_order_number": out.WarehouseOutOrderNumber})
  1062. }
  1063. func FindPatientIsOpenRemindById(patient_id int64, org_id int64) (patients models.Patients, err error) {
  1064. err = readDb.Model(&models.Patients{}).Where("status = 1 AND id = ? AND user_org_id = ?", patient_id, org_id).First(&patients).Error
  1065. return
  1066. }
  1067. func GetMaxLongAdviceGroupID(orgId int64, patient_id int64) (group int64) {
  1068. var advice models.DoctorAdvice
  1069. err := readDb.Table("xt_doctor_advice").Where("user_org_id=? AND patient_id = ? AND status = 1 AND advice_type = 1 AND frequency_type > 0", orgId, patient_id).Select("max(groupno) as groupno").First(&advice).Error
  1070. if err != nil {
  1071. fmt.Println(err)
  1072. group = 0
  1073. }
  1074. group = advice.GroupNo
  1075. return
  1076. }
  1077. func GetLastLongAdviceByGroupNo(orgId int64, patient_id int64, advice_date int64) (advice []*models.DoctorAdvice, err error) {
  1078. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND stop_state = 2 AND parent_id = 0 AND frequency_type <> 0 and (push_start_time >=? or push_start_time = 0) ", orgId, patient_id, advice_date).Preload("ChildDoctorAdvice", "status = 1 and (push_start_time >=? or push_start_time = 0)", advice_date).Find(&advice).Error
  1079. return
  1080. }
  1081. func GetLastLongAdviceByGroupNoOne(orgId int64, patient_id int64, advice_date int64) (advice []*models.DoctorAdvice, err error) {
  1082. redis := RedisClient()
  1083. defer redis.Close()
  1084. key := strconv.FormatInt(orgId, 10) + ":" + strconv.FormatInt(patient_id, 10) + ":long_advice"
  1085. long_advice_str, _ := redis.Get(key).Result()
  1086. if len(long_advice_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1087. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND stop_state = 2 AND parent_id = 0 AND frequency_type <> 0 and (push_start_time >=? or push_start_time = 0) ", orgId, patient_id, advice_date).Preload("ChildDoctorAdvice", "status = 1 and (push_start_time >=? or push_start_time = 0) ", advice_date).Find(&advice).Error
  1088. if err != nil {
  1089. if err == gorm.ErrRecordNotFound {
  1090. return nil, nil
  1091. } else {
  1092. return nil, err
  1093. }
  1094. } else {
  1095. if len(advice) > 0 {
  1096. //缓存数据
  1097. starfes_str, err := json.Marshal(advice)
  1098. if err == nil {
  1099. redis.Set(key, starfes_str, time.Second*60*60*18)
  1100. return advice, nil
  1101. }
  1102. } else {
  1103. redis.Set(key, "null", time.Second*60*60*18)
  1104. return advice, nil
  1105. }
  1106. return advice, nil
  1107. }
  1108. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1109. if long_advice_str == "null" {
  1110. return advice, nil
  1111. } else {
  1112. json.Unmarshal([]byte(long_advice_str), &advice)
  1113. return advice, nil
  1114. }
  1115. }
  1116. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND stop_state = 2 AND parent_id = 0 AND frequency_type <> 0 and (push_start_time >=? or push_start_time = 0) ", orgId, patient_id, advice_date).Preload("ChildDoctorAdvice", "status = 1 and (push_start_time >=? or push_start_time = 0) ", advice_date).Find(&advice).Error
  1117. return
  1118. }
  1119. func GetLastLongAdviceByGroupNoThree(orgId int64, patient_id int64, advice_date int64) (advice []*models.DoctorAdvice, err error) {
  1120. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND frequency_type = 2 AND stop_state = 2 AND parent_id = 0 and (push_start_time>=? or push_start_time = 0) ", orgId, patient_id, advice_date).Preload("ChildDoctorAdvice", "status = 1 and (push_start_time>=? or push_start_time = 0) ", advice_date).Find(&advice).Error
  1121. return
  1122. }
  1123. func GetLastLongAdviceByGroupNoFour(orgId int64, patient_id int64, advice_date int64) (advice []*models.DoctorAdvice, err error) {
  1124. //err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND frequency_type = 2 AND stop_state = 2 AND parent_id = 0 ", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  1125. //return
  1126. redis := RedisClient()
  1127. defer redis.Close()
  1128. key := strconv.FormatInt(orgId, 10) + ":" + strconv.FormatInt(patient_id, 10) + ":long_advice"
  1129. long_advice_str, _ := redis.Get(key).Result()
  1130. if len(long_advice_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1131. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND frequency_type = 2 AND stop_state = 2 AND parent_id = 0 and (push_start_time >=? or push_start_time = 0)", orgId, patient_id, advice_date).Preload("ChildDoctorAdvice", "status = 1 and (push_start_time >=? or push_start_time = 0)", advice_date).Find(&advice).Error
  1132. if err != nil {
  1133. if err == gorm.ErrRecordNotFound {
  1134. return nil, nil
  1135. } else {
  1136. return nil, err
  1137. }
  1138. } else {
  1139. if len(advice) > 0 {
  1140. //缓存数据
  1141. starfes_str, err := json.Marshal(advice)
  1142. if err == nil {
  1143. redis.Set(key, starfes_str, time.Second*60*60*18)
  1144. return advice, nil
  1145. }
  1146. } else {
  1147. redis.Set(key, "null", time.Second*60*60*18)
  1148. return advice, nil
  1149. }
  1150. return advice, nil
  1151. }
  1152. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1153. if long_advice_str == "null" {
  1154. return advice, nil
  1155. } else {
  1156. json.Unmarshal([]byte(long_advice_str), &advice)
  1157. return advice, nil
  1158. }
  1159. }
  1160. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND stop_state = 2 AND parent_id = 0 AND frequency_type <> 0 and (push_start_time >=? or push_start_time = 0)", orgId, patient_id, advice_date).Preload("ChildDoctorAdvice", "status = 1 and (push_start_time >=? or push_start_time = 0)", advice_date).Find(&advice).Error
  1161. return
  1162. }
  1163. func FindAllDoctorAdviceByTime(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  1164. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 2 AND advice_date > ? AND advice_date <= ? AND template_id = ? ", orgId, patient_id, end, now, temp_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  1165. return
  1166. }
  1167. func UpdateScheduleModeId(patient_id int64, org_id int64, date int64, mode_id int64) (err error) {
  1168. err = writeDb.Model(&models.Schedule{}).Where("status =1 AND user_org_id = ? AND patient_id = ? AND schedule_date = ?", org_id, patient_id, date).Updates(map[string]interface{}{"mode_id": mode_id, "updated_time": time.Now().Unix()}).Error
  1169. return
  1170. }
  1171. func UpdateAssessmentAfterDate(patient_id int64, org_id int64, date int64, actual_treatment_hour int64, actual_treatment_minute int64) (err error) {
  1172. err = writeDb.Model(&models.AssessmentAfterDislysis{}).Where("status =1 AND user_org_id = ? AND patient_id = ? AND assessment_date = ?", org_id, patient_id, date).Updates(map[string]interface{}{"actual_treatment_hour": actual_treatment_hour, "actual_treatment_minute": actual_treatment_minute}).Error
  1173. return
  1174. }
  1175. func UpdateAssessmentAfterDateOne(patient_id int64, org_id int64, date int64, actual_treatment_hour int64, actual_treatment_minute int64, BreathingRate string) (err error) {
  1176. err = writeDb.Model(&models.AssessmentAfterDislysis{}).Where("status =1 AND user_org_id = ? AND patient_id = ? AND assessment_date = ?", org_id, patient_id, date).Updates(map[string]interface{}{"actual_treatment_hour": actual_treatment_hour, "actual_treatment_minute": actual_treatment_minute, "breathing_rate": BreathingRate}).Error
  1177. return
  1178. }
  1179. func CreateDialysisBeforePrepare(beforePrepares []*models.DialysisBeforePrepare) (err error) {
  1180. if len(beforePrepares) > 0 {
  1181. utx := writeDb.Begin()
  1182. if len(beforePrepares) > 0 {
  1183. thisSQL := "INSERT INTO dialysis_before_prepare (user_org_id, patient_id, record_date, good_id, good_type_id,count,ctime,mtime,creater,modifier,status,commdity_code) VALUES "
  1184. insertParams := make([]string, 0)
  1185. insertData := make([]interface{}, 0)
  1186. for _, prepare := range beforePrepares {
  1187. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?)")
  1188. insertData = append(insertData, prepare.UserOrgId)
  1189. insertData = append(insertData, prepare.PatientId)
  1190. insertData = append(insertData, prepare.RecordDate)
  1191. insertData = append(insertData, prepare.GoodId)
  1192. insertData = append(insertData, prepare.GoodTypeId)
  1193. insertData = append(insertData, prepare.Count)
  1194. insertData = append(insertData, prepare.Ctime)
  1195. insertData = append(insertData, prepare.Mtime)
  1196. insertData = append(insertData, prepare.Creater)
  1197. insertData = append(insertData, prepare.Modifier)
  1198. insertData = append(insertData, 1)
  1199. insertData = append(insertData, prepare.CommdityCode)
  1200. }
  1201. thisSQL += strings.Join(insertParams, ", ")
  1202. err = utx.Exec(thisSQL, insertData...).Error
  1203. if err != nil {
  1204. utx.Rollback()
  1205. return
  1206. }
  1207. }
  1208. utx.Commit()
  1209. }
  1210. return
  1211. }
  1212. func UpdateDialysisBeforePrepare(info *models.DialysisBeforePrepare) (err error) {
  1213. err = writeDb.Save(&info).Error
  1214. return err
  1215. }
  1216. func UpdateDialysisBeforePrepareTwo(info *models.DialysisBeforePrepare) (err error) {
  1217. err = writeDb.Model(&models.DialysisBeforePrepare{}).
  1218. Where("status = 1 and good_id = ? AND good_type_id = ? AND patient_id = ? AND user_org_id = ? AND record_date = ?", info.GoodId, info.GoodTypeId, info.PatientId, info.UserOrgId, info.RecordDate).
  1219. Updates(map[string]interface{}{"count": info.Count, "updated_time": time.Now().Unix(), "modifier": info.Modifier, "commdity_code": info.CommdityCode, "status": 1}).Error
  1220. return err
  1221. }
  1222. func UpdateAutoReduceDetailSix(detail *models.AutomaticReduceDetail) (err error) {
  1223. err = writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? and good_type_id = ? And patient_id = ? and org_id =? and record_time = ?", detail.GoodId, detail.GoodTypeId, detail.PatientId, detail.OrgId, detail.RecordTime).Updates(map[string]interface{}{"count": detail.Count, "mtime": detail.Mtime, "status": 1}).Error
  1224. return err
  1225. }
  1226. func UpdateOrderCountSub(good_type_id int64, good_id int64, number string, org_id int64, count int64) {
  1227. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND is_sys = 1 AND warehouse_out_order_number = ? ", good_id, good_type_id, org_id, number).UpdateColumn("count", gorm.Expr("count - ?", count))
  1228. }
  1229. func UpdateOrderCountAdd(good_type_id int64, good_id int64, number string, org_id int64, count int64) {
  1230. writeDb.Model(&models.WarehouseOutInfo{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND is_sys = 1 AND warehouse_out_order_number = ? ", good_id, good_type_id, org_id, number).UpdateColumn("count", gorm.Expr("count + ?", count))
  1231. }
  1232. func UpdateUserInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, count int64, out *models.WarehouseOutInfo) {
  1233. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND warehouse_out_order_number = ? AND patient_id = ?", good_id, good_type_id, org_id, out.WarehouseOutOrderNumber, patient_id).Updates(map[string]interface{}{"count": count, "status": 1})
  1234. //writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND is_sys = 1 AND warehouse_out_order_number = ? ", good_id, good_type_id, org_id, out.WarehouseOutOrderNumber).UpdateColumn("count", count)
  1235. }
  1236. func UpdateDrugUserInfoDetails(drug_id int64, time int64, org_id int64, patient_id int64, count int64, out *models.DrugWarehouseOutInfo) {
  1237. writeDb.Model(&models.DrugAutomaticReduceDetail{}).Where("drug_id = ? AND org_id = ? AND status = 1 AND warehouse_out_order_number = ? AND patient_id = ?", drug_id, org_id, out.WarehouseOutOrderNumber, patient_id).Updates(map[string]interface{}{"count": count})
  1238. //writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND is_sys = 1 AND warehouse_out_order_number = ? ", good_id, good_type_id, org_id, out.WarehouseOutOrderNumber).UpdateColumn("count", count)
  1239. }
  1240. func FindDialysisBeforePrepare(patient_id int64, good_id int64, good_type_id int64, user_org_id int64, record_time int64) (count int64) {
  1241. readDb.Model(&models.DialysisBeforePrepare{}).Where("user_org_id = ? AND patient_id = ? AND good_id = ? AND good_type_id = ? AND record_date = ? AND status = 1 AND count<> 0", user_org_id, patient_id, good_id, good_type_id, record_time).Count(&count)
  1242. return
  1243. }
  1244. func GetExecutionDoctors(orgid int64, patientid int64, id int64) (doctorAdvice []*models.DoctorAdvice, err error) {
  1245. err = readDb.Model(&doctorAdvice).Where("user_org_id = ? and patient_id = ? AND (id = ? Or parent_id=?) and status = 1", orgid, patientid, id, id).Find(&doctorAdvice).Error
  1246. return doctorAdvice, err
  1247. }
  1248. func GetHisExecutionDoctors(orgid int64, patientid int64, id int64) (doctorAdvice []*models.HisDoctorAdviceInfo, err error) {
  1249. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id = ? and patient_id = ? AND (id = ? Or parent_id=?) and status = 1", orgid, patientid, id, id).Find(&doctorAdvice).Error
  1250. return doctorAdvice, err
  1251. }
  1252. func GetHisExecutionDoctorsTwo(orgid int64, patientid int64, id int64) (doctorAdvice []*models.HisDoctorAdviceInfo, err error) {
  1253. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id = ? and patient_id = ? AND id = ? and status = 1 and execution_state = 1", orgid, patientid, id).Find(&doctorAdvice).Error
  1254. return doctorAdvice, err
  1255. }
  1256. func FindHisDoctorAdviceById(orgID int64, id int64) (advice models.HisDoctorAdviceInfo, err error) {
  1257. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id=? and status=1 and id = ?", orgID, id).First(&advice).Error
  1258. return
  1259. }
  1260. func FindHisDoctorAdviceByIdTwo(orgID int64, id int64, tx *gorm.DB) (advice models.HisDoctorAdviceInfo, err error) {
  1261. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id=? and status=1 and id = ?", orgID, id).First(&advice).Error
  1262. if err != gorm.ErrRecordNotFound {
  1263. if err != nil {
  1264. tx.Rollback()
  1265. return
  1266. }
  1267. }
  1268. return
  1269. }
  1270. func FindHisDoctorAdviceByIds(orgID int64, ids []string) (advice []*models.HisDoctorAdviceInfo, err error) {
  1271. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id=? and status=1 and id in (?)", orgID, ids).Find(&advice).Error
  1272. return
  1273. }
  1274. func SaveHisDoctorAdvice(advice *models.HisDoctorAdviceInfo) (err error) {
  1275. err = writeDb.Save(&advice).Error
  1276. return
  1277. }
  1278. func SaveHisDoctorAdviceOne(advice *models.HisDoctorAdviceInfo, tx *gorm.DB) (err error) {
  1279. err = tx.Save(&advice).Error
  1280. if err != nil {
  1281. tx.Rollback()
  1282. return
  1283. }
  1284. return
  1285. }
  1286. func GetGoodInfoMation(orgid int64) (goodinfo []*models.GoodInfo, err error) {
  1287. err = XTReadDB().Model(&goodinfo).Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Preload("GoodSotckInfo", "stock_count > 0 and status = 1").Find(&goodinfo).Error
  1288. //err = XTReadDB().Model(&goodinfo).Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodinfo).Error
  1289. return goodinfo, err
  1290. }
  1291. func GetGoodInfoMationSix(orgid int64, storehouse_id int64) (goodinfo []*models.GoodInfo, err error) {
  1292. err = XTReadDB().Model(&goodinfo).Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Preload("GoodSotckInfo", "stock_count > 0 and status = 1 and storehouse_id = ?", storehouse_id).Order("sort asc").Find(&goodinfo).Error
  1293. return goodinfo, err
  1294. }
  1295. func GetGoodInfoMationSeven(orgid int64, storehouse_id int64) (goodinfo []*models.GoodInfo, err error) {
  1296. err = XTReadDB().Model(&goodinfo).Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Preload("GoodSotckInfo", "status = 1 and storehouse_id = ? and stock_count>0", storehouse_id).Find(&goodinfo).Error
  1297. return goodinfo, err
  1298. }
  1299. func GetDialysisBeforePrepare(goodTypeId int64, goodId int64, orgid int64, patientId int64) (*models.DialysisBeforePrepare, error) {
  1300. prepare := models.DialysisBeforePrepare{}
  1301. err := XTReadDB().Where("good_type_id = ? and good_id = ? and user_org_id = ? and status =1 and patient_id = ?", goodTypeId, goodId, orgid, patientId).Last(&prepare).Error
  1302. if err == gorm.ErrRecordNotFound {
  1303. return nil, err
  1304. }
  1305. if err != nil {
  1306. return nil, err
  1307. }
  1308. return &prepare, nil
  1309. }
  1310. func CreateDialysisBeforePrepareOne(prepare *models.DialysisBeforePrepare) error {
  1311. err := XTWriteDB().Create(&prepare).Error
  1312. return err
  1313. }
  1314. func ModifyExceDoctorAdviceById(m *models.HisDoctorAdviceInfo, ids []string) (err error) {
  1315. ut := writeDb.Begin()
  1316. err = ut.Model(&models.HisDoctorAdviceInfo{}).Where("status = 1 AND id IN (?) AND execution_state = 1", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  1317. if err != nil {
  1318. ut.Rollback()
  1319. return
  1320. }
  1321. err = ut.Model(&models.HisDoctorAdviceInfo{}).Where("status = 1 AND parent_id IN (?) AND execution_state = 1 ", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  1322. if err != nil {
  1323. ut.Rollback()
  1324. return
  1325. }
  1326. ut.Commit()
  1327. return err
  1328. }
  1329. func FindHisDoctorAdviceByIdOne(orgID int64, ids []string) (advice []models.HisDoctorAdviceInfo, err error) {
  1330. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  1331. return
  1332. }
  1333. func ModifyHisPrescriptionProject(m *models.HisPrescriptionProject, ids []string) (err error) {
  1334. ut := writeDb.Begin()
  1335. err = ut.Model(&models.HisPrescriptionProject{}).Where("status = 1 AND id IN (?) AND execution_state = 1", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  1336. if err != nil {
  1337. ut.Rollback()
  1338. return
  1339. }
  1340. ut.Commit()
  1341. return err
  1342. }
  1343. func FindHisProjectById(orgID int64, id int64) (advice models.HisPrescriptionProject, err error) {
  1344. err = readDb.Model(&models.HisPrescriptionProject{}).Where("user_org_id=? and status=1 and id = ?", orgID, id).First(&advice).Error
  1345. return
  1346. }
  1347. func SaveHisProject(advice *models.HisPrescriptionProject) (err error) {
  1348. err = writeDb.Save(&advice).Error
  1349. return
  1350. }
  1351. func FindAllHisProjectById(orgID int64, patient_id int64, record_time int64) (advice []*models.HisPrescriptionProject, err error) {
  1352. err = readDb.Model(&models.HisPrescriptionProject{}).Preload("HisProject", "status = 1 and user_org_id = ?", orgID).Preload("XtHisProjectTeam", "user_org_id=? and status=1", orgID).Preload("GoodInfo", "status = 1 and org_id = ?", orgID).Where("user_org_id=? and status=1 and patient_id = ? AND record_date = ?", orgID, patient_id, record_time).Find(&advice).Error
  1353. return
  1354. }
  1355. func GetAllPcPatientListByListSeven(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1356. err = readDb.Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  1357. return patients, err
  1358. }
  1359. func GetAllPcPatientListByListEight(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1360. err = readDb.Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  1361. return patients, err
  1362. }
  1363. func GetAllPcPatientListByList(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1364. redis := RedisClient()
  1365. defer redis.Close()
  1366. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  1367. patient_info_str, _ := redis.Get(key).Result()
  1368. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1369. err = readDb.Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  1370. if err != nil {
  1371. if err == gorm.ErrRecordNotFound {
  1372. if len(patients) <= 0 {
  1373. redis.Set(key, "null", time.Second*60*60*18)
  1374. }
  1375. return nil, nil
  1376. } else {
  1377. return nil, err
  1378. }
  1379. } else {
  1380. if len(patients) > 0 {
  1381. //缓存数据
  1382. patient_info_json, err := json.Marshal(&patients)
  1383. if err == nil {
  1384. redis.Set(key, patient_info_json, time.Second*60*60*18)
  1385. }
  1386. } else {
  1387. redis.Set(key, "null", time.Second*60*60*18)
  1388. }
  1389. return patients, nil
  1390. }
  1391. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1392. if patient_info_str == "null" {
  1393. return nil, nil
  1394. } else {
  1395. json.Unmarshal([]byte(patient_info_str), &patients)
  1396. return patients, nil
  1397. }
  1398. }
  1399. }
  1400. func GetAllPcAdvicestByList(orgID int64, scheduleDate int64) (advices []*VMDoctorAdvice, err error) {
  1401. redis := RedisClient()
  1402. defer redis.Close()
  1403. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":advice_list_all"
  1404. advice_list_str, _ := redis.Get(key).Result()
  1405. if len(advice_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1406. err = readDb.Where("status = 1 AND user_org_id = ? AND advice_type = 2 AND advice_date = ?", orgID, scheduleDate).Find(&advices).Error
  1407. if err != nil {
  1408. if err == gorm.ErrRecordNotFound {
  1409. if len(advices) <= 0 {
  1410. redis.Set(key, "null", time.Second*60*60*18)
  1411. }
  1412. return nil, nil
  1413. } else {
  1414. return nil, err
  1415. }
  1416. } else {
  1417. if len(advices) > 0 {
  1418. //缓存数据
  1419. advice_list_str, err := json.Marshal(&advices)
  1420. if err == nil {
  1421. redis.Set(key, advice_list_str, time.Second*60*60*18)
  1422. }
  1423. } else {
  1424. redis.Set(key, "null", time.Second*60*60*18)
  1425. }
  1426. return advices, nil
  1427. }
  1428. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1429. if advice_list_str == "null" {
  1430. return nil, nil
  1431. } else {
  1432. json.Unmarshal([]byte(advice_list_str), &advices)
  1433. return advices, nil
  1434. }
  1435. }
  1436. }
  1437. func GetAllPcDialysisOrdersByList(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVMList, err error) {
  1438. redis := RedisClient()
  1439. defer redis.Close()
  1440. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  1441. dialysis_orders_list_all, _ := redis.Get(key).Result()
  1442. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1443. err = readDb.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  1444. if err != nil {
  1445. if err == gorm.ErrRecordNotFound {
  1446. return nil, nil
  1447. } else {
  1448. return nil, err
  1449. }
  1450. } else {
  1451. if len(dialysisOrders) > 0 {
  1452. //缓存数据
  1453. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  1454. if err == nil {
  1455. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  1456. }
  1457. } else {
  1458. redis.Set(key, "null", time.Second*60*60*18)
  1459. }
  1460. return dialysisOrders, nil
  1461. }
  1462. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1463. if dialysis_orders_list_all == "null" {
  1464. return nil, nil
  1465. } else {
  1466. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  1467. return dialysisOrders, nil
  1468. }
  1469. }
  1470. }
  1471. func GetAllPcAssessmentAfterDislysisByList(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*AssessmentAfterDislysis, err error) {
  1472. redis := RedisClient()
  1473. defer redis.Close()
  1474. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  1475. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  1476. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1477. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  1478. if err != nil {
  1479. if err == gorm.ErrRecordNotFound {
  1480. if len(assessmentAfterDislysis) <= 0 {
  1481. redis.Set(key, "null", time.Second*60*60*18)
  1482. }
  1483. return nil, nil
  1484. } else {
  1485. return nil, err
  1486. }
  1487. } else {
  1488. if len(assessmentAfterDislysis) > 0 {
  1489. //缓存数据
  1490. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  1491. if err == nil {
  1492. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  1493. }
  1494. } else {
  1495. redis.Set(key, "null", time.Second*60*60*18)
  1496. }
  1497. return assessmentAfterDislysis, nil
  1498. }
  1499. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1500. if assessment_after_dislysis__all == "null" {
  1501. return nil, nil
  1502. } else {
  1503. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  1504. return assessmentAfterDislysis, nil
  1505. }
  1506. }
  1507. }
  1508. func GetAllPcTreatmentSummarysByList(orgID int64, scheduleDate int64) (treatmentSummarys []*VMTreatmentSummary, err error) {
  1509. redis := RedisClient()
  1510. defer redis.Close()
  1511. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":treatment_summarys_list_all"
  1512. treatment_summarys_all, _ := redis.Get(key).Result()
  1513. if len(treatment_summarys_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1514. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&treatmentSummarys).Error
  1515. if err != nil {
  1516. if err == gorm.ErrRecordNotFound {
  1517. if len(treatment_summarys_all) <= 0 {
  1518. redis.Set(key, "null", time.Second*60*60*18)
  1519. }
  1520. return nil, nil
  1521. } else {
  1522. return nil, err
  1523. }
  1524. } else {
  1525. if len(treatmentSummarys) > 0 {
  1526. //缓存数据
  1527. treatment_summarys_all, err := json.Marshal(&treatmentSummarys)
  1528. if err == nil {
  1529. redis.Set(key, treatment_summarys_all, time.Second*60*60*18)
  1530. }
  1531. } else {
  1532. redis.Set(key, "null", time.Second*60*60*18)
  1533. }
  1534. return treatmentSummarys, nil
  1535. }
  1536. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1537. if treatment_summarys_all == "null" {
  1538. return nil, nil
  1539. } else {
  1540. json.Unmarshal([]byte(treatment_summarys_all), &treatmentSummarys)
  1541. return treatmentSummarys, nil
  1542. }
  1543. }
  1544. }
  1545. func UpdateTeamProjectExceStatus(ids []int64, admin_user_id int64, execution_time int64) (err error) {
  1546. ut := writeDb.Begin()
  1547. db := ut.Model(&models.HisPrescriptionProject{})
  1548. err = db.Where("status = 1 AND id IN (?)", ids).Updates(map[string]interface{}{"execution_time": execution_time, "execution_state": 1, "execution_staff": admin_user_id}).Error
  1549. if err != nil {
  1550. ut.Rollback()
  1551. return
  1552. }
  1553. ut.Commit()
  1554. return err
  1555. }
  1556. func UpdateTeamProjectCheckStatus(ids []int64, patient_id int64, record_date int64, user_org_id int64, admin_user_id int64, check_time int64) (err error) {
  1557. ut := writeDb.Begin()
  1558. err = ut.Model(&models.HisPrescriptionProject{}).Where("status = 1 AND id IN (?) AND patient_id = ? AND user_org_id = ? AND record_date = ?", ids, patient_id, user_org_id, record_date).Updates(map[string]interface{}{"check_time": check_time, "check_state": 1, "checker": admin_user_id}).Error
  1559. if err != nil {
  1560. ut.Rollback()
  1561. return
  1562. }
  1563. ut.Commit()
  1564. return err
  1565. }
  1566. func GetCheckTeamProject(team_id int64, patient_id int64, record_date int64, user_org_id int64) (ps []*models.HisPrescriptionProject, err error) {
  1567. err = readDb.Model(&models.HisPrescriptionProject{}).Joins("JOIN xt_his_project as p On p.id = his_prescription_project.project_id AND p.cost_classify = 3").Where("his_prescription_project.team_id IN (?) AND his_prescription_project.patient_id = ? AND his_prescription_project.user_org_id = ? AND his_prescription_project.record_date = ? AND his_prescription_project.status = 1", team_id, patient_id, user_org_id, record_date).Find(&ps).Error
  1568. return
  1569. }
  1570. func GetAdminUserRoleInformation(orgid int64) (models.SgjOrgFollow, error) {
  1571. follow := models.SgjOrgFollow{}
  1572. db := UserReadDB().Table("sgj_org_follow as f").Where("f.status = 1")
  1573. table := UserReadDB().Table("sgj_admin as a").Where("a.status = 1")
  1574. fmt.Println(table)
  1575. if orgid > 0 {
  1576. db = db.Where("org_id = ?", orgid)
  1577. }
  1578. if orgid == 0 {
  1579. db = db.Where("org_id = ?", 0)
  1580. }
  1581. err := db.Select("f.org_id,f.admin_user_id,a.name,a.phone,a.camera,a.wechat,a.qqchat,a.email,a.wechat_link").Joins("left join sgj_admin as a on a.id = f.admin_user_id").Scan(&follow).Error
  1582. return follow, err
  1583. }
  1584. func GetOrgFollowIsExist(orgid int64) (*models.SgjOrgFollowOne, error) {
  1585. follow := models.SgjOrgFollowOne{}
  1586. err := UserReadDB().Where("org_id = ? and status = 1", orgid).Find(&follow).Error
  1587. if err == gorm.ErrRecordNotFound {
  1588. return nil, err
  1589. }
  1590. if err != nil {
  1591. return nil, err
  1592. }
  1593. return &follow, nil
  1594. }
  1595. func UpdatePatientSign(patient_id int64, dialysis_date int64, order models.DialysisOrder, orgid int64) error {
  1596. err := XTWriteDB().Model(&order).Where("patient_id = ? and dialysis_date= ? and status = 1 and user_org_id = ?", patient_id, dialysis_date, orgid).Update(map[string]interface{}{"url": order.Url, "hash": order.Hash}).Error
  1597. return err
  1598. }
  1599. func GetPatientSign(patient_id int64, dialysis_date int64, orgid int64) (models.DialysisOrder, error) {
  1600. order := models.DialysisOrder{}
  1601. err := XTReadDB().Where("patient_id=? and dialysis_date = ? and user_org_id = ? and status = 1", patient_id, dialysis_date, orgid).Find(&order).Error
  1602. return order, err
  1603. }
  1604. func GetScheduleByPatient(patient_id int64, schedule_date int64, orgid int64) (models.XtScheduleSix, error) {
  1605. schedule := models.XtScheduleSix{}
  1606. err := XTReadDB().Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status = 1", patient_id, schedule_date, orgid).Find(&schedule).Error
  1607. return schedule, err
  1608. }
  1609. func GetSchedulePrintList(page int64, limit int64, schedulType int64, partitionType int64, keywords string, schedule_date int64, orgID int64) (list []*models.DialysisScheduleSix, total int64, err error) {
  1610. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  1611. table := XTReadDB().Table("xt_patients as p").Where("p.status = 1")
  1612. fmt.Println("table", table)
  1613. if schedulType > 0 {
  1614. db = db.Where("x.schedule_type = ?", schedulType)
  1615. }
  1616. if partitionType > 0 {
  1617. db = db.Where("x.partition_id = ?", partitionType)
  1618. }
  1619. if orgID > 0 {
  1620. db = db.Where("x.user_org_id = ?", orgID)
  1621. }
  1622. if schedule_date > 0 {
  1623. db = db.Where("x.schedule_date = ?", schedule_date)
  1624. }
  1625. db = db.Joins("left join xt_patients as p on p.id = x.patient_id")
  1626. likeKey := "%" + keywords + "%"
  1627. if len(keywords) > 0 {
  1628. db = db.Where("p.name like ?", likeKey)
  1629. }
  1630. err = db.Count(&total).Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,x.status,p.name").Offset(limit * (page - 1)).Limit(limit).Order("x.schedule_date desc").Find(&list).Error
  1631. return list, total, err
  1632. }
  1633. func GetSchedulePrintListOne(page int64, limit int64, schedulType int64, partitionType int64, keywords string, schedule_date int64, orgID int64) (list []*models.DialysisScheduleSeven, err error) {
  1634. db := XTReadDB().Model(&list).Where("status = 1")
  1635. if orgID > 0 {
  1636. db = db.Where("user_org_id = ?", orgID)
  1637. }
  1638. if schedulType > 0 {
  1639. db = db.Where("schedule_type = ?", schedulType)
  1640. }
  1641. if partitionType > 0 {
  1642. db = db.Where("partition_id = ?", partitionType)
  1643. }
  1644. err = db.Group("schedule_type,partition_id").Find(&list).Error
  1645. return list, err
  1646. }
  1647. func GetDialysisRecordOrder(org_id int64, patient_id int64, dialysis_date int64) (models.MDialysisOrderForListSix, error) {
  1648. order := models.MDialysisOrderForListSix{}
  1649. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and dialysis_date = ? and status =1 ", org_id, patient_id, dialysis_date).Find(&order).Error
  1650. return order, err
  1651. }
  1652. func GetSolutionListByOrgId(orgid int64) (list []*models.DialysisSolution, err error) {
  1653. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Group("patient_id,mode_id").Order("id desc").Find(&list).Error
  1654. return list, err
  1655. }
  1656. func GetScheduleTypeById(org_id int64, schedule_date int64, schedule_type int64) (list []*models.XtScheduleSeven, err error) {
  1657. db := XTReadDB().Model(&list).Where("status = 1")
  1658. if org_id > 0 {
  1659. db = db.Where("user_org_id = ?", org_id)
  1660. }
  1661. if schedule_date > 0 {
  1662. db = db.Where("schedule_date = ?", schedule_date)
  1663. }
  1664. if schedule_type > 0 {
  1665. db = db.Where("schedule_type = ?", schedule_type)
  1666. }
  1667. err = db.Select("partition_id,count(id) as count").Group("partition_id").Scan(&list).Error
  1668. return list, err
  1669. }
  1670. func GetHisExcutionDoctorAdviceListGroupList(patients []string, advice_date int64, user_org_id int64) (advice []*models.HisDoctorAdvice, err error) {
  1671. err = XTReadDB().Where("patient_id in(?) and advice_date = ? and user_org_id = ? and status = 1 and execution_state = 2", patients, advice_date, user_org_id).Group("drug_id").Find(&advice).Error
  1672. return advice, err
  1673. }
  1674. func GetHisExcutionDoctorAdviceList(patients []string, advice_date int64, user_org_id int64) (advice []*models.HisDoctorAdvice, err error) {
  1675. err = XTReadDB().Where("patient_id in(?) and advice_date = ? and user_org_id = ? and status = 1 and execution_state = 2", patients, advice_date, user_org_id).Find(&advice).Error
  1676. return advice, err
  1677. }
  1678. func UpdateHisDoctorAdviceExecution(info models.HisDoctorAdviceInfo, id int64) error {
  1679. err := XTWriteDB().Model(&info).Where("id = ? and status = 1", id).Update(map[string]interface{}{"execution_staff": info.ExecutionStaff, "execution_state": info.ExecutionState, "execution_time": info.ExecutionTime, "updated_time": info.ExecutionTime}).Error
  1680. return err
  1681. }
  1682. func UpdateDoctorAdviceExecution(info models.DoctorAdvice, id int64) error {
  1683. err := XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? and status = 1", id).Update(map[string]interface{}{"execution_staff": info.ExecutionStaff, "execution_state": info.ExecutionState, "execution_time": info.ExecutionTime, "updated_time": info.ExecutionTime}).Error
  1684. return err
  1685. }
  1686. func GetExcutionDoctorAdviceGroupList(patients []string, advice_date int64, user_org_id int64) (advice []*models.DoctorAdviceThrity, err error) {
  1687. err = XTReadDB().Where("patient_id in(?) and advice_date = ? and user_org_id = ? and status = 1 and execution_state = 2", patients, advice_date, user_org_id).Group("drug_id").Find(&advice).Error
  1688. return advice, err
  1689. }
  1690. func GetExcutionDoctorAdviceList(patients []string, advice_date int64, user_org_id int64) (advice []*models.DoctorAdviceThrity, err error) {
  1691. err = XTReadDB().Where("patient_id in(?) and advice_date = ? and user_org_id = ? and status = 1 and execution_state = 2", patients, advice_date, user_org_id).Find(&advice).Error
  1692. return advice, err
  1693. }
  1694. func GetHisCheckDoctorAdvice(orgid int64, patinentids []string, advice_date int64) (advice []*models.HisDoctorAdviceThirty, err error) {
  1695. err = XTReadDB().Where("user_org_id = ? and patient_id in(?) and advice_date = ? and status =1", orgid, patinentids, advice_date).Find(&advice).Error
  1696. return advice, err
  1697. }
  1698. func CheckDoctorAdvice(id int64, creater int64) (models.DoctorAdviceThrity, error) {
  1699. doctoradvice := models.DoctorAdviceThrity{}
  1700. err := XTWriteDB().Model(&doctoradvice).Where("id = ? and status = 1", id).Update(map[string]interface{}{"checker": creater, "check_time": time.Now().Unix()}).Error
  1701. return doctoradvice, err
  1702. }
  1703. func CheckHisDoctorAdvice(id int64, creater int64) (models.HisDoctorAdviceThirty, error) {
  1704. doctoradvice := models.HisDoctorAdviceThirty{}
  1705. err := XTWriteDB().Model(&doctoradvice).Where("id = ? and status = 1", id).Update(map[string]interface{}{"checker": creater, "check_time": time.Now().Unix()}).Error
  1706. return doctoradvice, err
  1707. }
  1708. func GetCheckDoctorAdvice(orgid int64, patinentids []string, advice_date int64) (advice []*models.DoctorAdviceThrity, err error) {
  1709. err = XTReadDB().Where("user_org_id = ? and patient_id in(?) and advice_date = ? and status=1", orgid, patinentids, advice_date).Find(&advice).Error
  1710. return advice, err
  1711. }
  1712. func SettleHisNewDoctorAdvice(patinentids []string, advice_date int64, orgid int64) (advice []*models.HisDoctorAdviceThirty, err error) {
  1713. err = XTWriteDB().Model(&advice).Where("patient_id in(?) and advice_date = ? and user_org_id = ? and status = 1", patinentids, advice_date, orgid).Update(map[string]interface{}{"is_settle": 1}).Error
  1714. return advice, err
  1715. }
  1716. func SettleNewDoctorAdvice(patinentids []string, advice_date int64, orgid int64) (advice []*models.DoctorAdviceThrity, err error) {
  1717. err = XTWriteDB().Model(&advice).Where("patient_id in(?) and advice_date = ? and user_org_id = ? and status = 1", patinentids, advice_date, orgid).Update(map[string]interface{}{"is_settle": 1}).Error
  1718. return advice, err
  1719. }
  1720. func GetHisMobileAdviceGroupList(orgid int64, ids []string) (advice []*models.HisDoctorAdvice, err error) {
  1721. err = XTReadDB().Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Group("drug_id").Find(&advice).Error
  1722. return advice, err
  1723. }
  1724. func GetHisMobileAdviceList(orgid int64, ids []string) (advice []*models.HisDoctorAdvice, err error) {
  1725. err = XTReadDB().Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Find(&advice).Error
  1726. return advice, err
  1727. }
  1728. func ExectionMobileAdvice(orgid int64, ids []string, execution_time int64, execution_staff int64) error {
  1729. advice := models.HisDoctorAdvice{}
  1730. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Updates(map[string]interface{}{"execution_time": execution_time, "execution_staff": execution_staff, "execution_state": 1, "is_mobile": 3}).Error
  1731. return err
  1732. }
  1733. func GetBloodMobileAdviceGroupList(orgid int64, ids []string) (advice []*models.DoctorAdviceThrity, err error) {
  1734. err = XTReadDB().Model(&advice).Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Group("drug_id").Find(&advice).Error
  1735. return advice, err
  1736. }
  1737. func GetBloodMobileAdviceList(orgid int64, ids []string) (advice []*models.DoctorAdviceThrity, err error) {
  1738. err = XTReadDB().Model(&advice).Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Find(&advice).Error
  1739. return advice, err
  1740. }
  1741. func FindAllDoctorAdviceByIds(orgID int64, ids []string) (advice []models.DoctorAdvice, err error) {
  1742. err = readDb.Model(&models.DoctorAdvice{}).Where("user_org_id=? AND id in(?) AND status = 1", orgID, ids).Find(&advice).Error
  1743. return
  1744. }
  1745. func ExectionBloodMobileAdvice(orgid int64, ids []string, execution_time int64, execution_staff int64) error {
  1746. advice := models.DoctorAdviceThrity{}
  1747. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Updates(map[string]interface{}{"execution_time": execution_time, "execution_staff": execution_staff, "execution_state": 1}).Error
  1748. return err
  1749. }
  1750. func UpdateSettleMobileHisAdvice(orgid int64, ids []string) error {
  1751. advice := models.HisDoctorAdviceThirty{}
  1752. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and id in(?) and status =1", orgid, ids).Updates(map[string]interface{}{"is_settle": 1}).Error
  1753. return err
  1754. }
  1755. func UpdateSettleMobileBloodAdvice(orgid int64, ids []string) error {
  1756. advice := models.DoctorAdvice{}
  1757. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and id in(?) and status =1", orgid, ids).Updates(map[string]interface{}{"is_settle": 1}).Error
  1758. return err
  1759. }
  1760. func CheckBloodDoctorAdvice(orgid int64, ids []string, advice models.DoctorAdvice) error {
  1761. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Updates(map[string]interface{}{"check_time": advice.CheckTime, "checker": advice.Checker, "check_state": 1}).Error
  1762. return err
  1763. }
  1764. func CheckHisMobileDoctorAdvice(orgid int64, ids []string, advice models.HisDoctorAdviceThirty) error {
  1765. err := XTWriteDB().Model(&advice).Where("user_org_id = ? and id in(?) and status = 1", orgid, ids).Updates(map[string]interface{}{"check_time": advice.CheckTime, "checker": advice.Checker, "check_state": 1}).Error
  1766. return err
  1767. }
  1768. func GetLastOrder(user_org_id int64, patient_id int64, dialysis_date int64) (models.DialysisOrderTwenty, error) {
  1769. twenty := models.DialysisOrderTwenty{}
  1770. err := XTReadDB().Where("user_org_id = ? and patient_id =? and dialysis_date < ? and status = 1", user_org_id, patient_id, dialysis_date).Preload("DeviceNumber", func(db *gorm.DB) *gorm.DB {
  1771. return db.Preload("DeviceZone", "status =1").Where("org_id = ? AND status = 1", user_org_id)
  1772. }).Order("id desc").First(&twenty).Error
  1773. return twenty, err
  1774. }
  1775. func UpdateDoctorSix(start_time int64, patient_id int64, dialysis_date int64, user_org_id int64) (models.DoctorAdvice, error) {
  1776. advice := models.DoctorAdvice{}
  1777. err := XTWriteDB().Model(&advice).Where("patient_id = ? and advice_date=? and user_org_id =? and status=1 and advice_name ='低分子肝素钠注射液'", patient_id, dialysis_date, user_org_id).Updates(map[string]interface{}{"execution_time": start_time}).Error
  1778. return advice, err
  1779. }
  1780. func GetTodayInforMation(orgid int64, record_date int64) (list []*NewDeviceInformation, err error) {
  1781. err = UserReadDB().Where("user_org_id = ? and date = ? and status = 1", orgid, record_date).Find(&list).Error
  1782. return list, err
  1783. }
  1784. func GetFiledConfig(orgid int64) (models.FiledConfig, error) {
  1785. config := models.FiledConfig{}
  1786. err := XTReadDB().Where("filed_name = 'dialyzer_perfusion_apparatus' and org_id = ? and module =1 and is_show = 1", orgid).First(&config).Error
  1787. return config, err
  1788. }
  1789. func GetDoctorAdviceCheckList(patient_id int64, advice_date int64, user_org_id int64) (doctor []*models.DoctorAdvice, err error) {
  1790. err = XTReadDB().Where("patient_id = ? and advice_date = ? and user_org_id =? and status= 1 and checker= 0", patient_id, advice_date, user_org_id).Find(&doctor).Error
  1791. return doctor, err
  1792. }
  1793. func UpdateDoctorAdviceList(id int64, checker int64) error {
  1794. advice := models.DoctorAdvice{}
  1795. err := XTWriteDB().Model(&advice).Where("id = ? and status =1", id).Updates(map[string]interface{}{"checker": checker, "check_state": 1, "check_time": time.Now().Unix()}).Error
  1796. return err
  1797. }
  1798. func UpdateLastMonitorRecordToday(accumulated_blood_volume float64, id int64) error {
  1799. err := XTWriteDB().Model(&models.MonitoringRecord{}).Where(" id = ? and status= 1", id).Update(map[string]interface{}{"accumulated_blood_volume": accumulated_blood_volume}).Error
  1800. return err
  1801. }
  1802. func GetDialysisFinish(user_org_id int64, record_date int64, module int64, patient_id int64) (models.XtDialysisFinish, error) {
  1803. finish := models.XtDialysisFinish{}
  1804. err := XTReadDB().Where("user_org_id = ? and record_date = ? and status =1 and module = ? and patient_id = ?", user_org_id, record_date, module, patient_id).Find(&finish).Error
  1805. return finish, err
  1806. }
  1807. func CreateDialysisFinish(finish models.XtDialysisFinish) error {
  1808. err := XTWriteDB().Create(&finish).Error
  1809. return err
  1810. }
  1811. func GetDialysisInformationSetting(user_org_id int64) (models.XtDialysisInformaitonSetting, error) {
  1812. setting := models.XtDialysisInformaitonSetting{}
  1813. err := XTReadDB().Where("user_org_id =? and status = 1", user_org_id).Find(&setting).Error
  1814. return setting, err
  1815. }
  1816. func CreatedDialysisInformation(information models.XtDialysisInformation) error {
  1817. err := XTWriteDB().Create(&information).Error
  1818. return err
  1819. }
  1820. func GetDialysisInoformationById(patient_id int64, record_date int64, user_org_id int64, module int64) (models.XtDialysisInformation, error) {
  1821. information := models.XtDialysisInformation{}
  1822. err := XTReadDB().Where("patient_id = ? and record_date = ? and user_org_id = ? and module = ? and status = 1", patient_id, record_date, user_org_id, module).Find(&information).Error
  1823. return information, err
  1824. }
  1825. func UpdateDialysisInformationById(patient_id int64, record_date int64, user_org_id int64, module int64, advice_date int64, remark string) error {
  1826. information := models.XtDialysisInformation{}
  1827. err := XTWriteDB().Model(&information).Where("patient_id = ? and record_date = ? and user_org_id =? and module = ? and status=1", patient_id, record_date, user_org_id, module).Update(map[string]interface{}{"application_date": advice_date, "remark": remark}).Error
  1828. return err
  1829. }
  1830. func UpdateDoctorAdviceByExcecute(id int64, user_org_id int64) error {
  1831. err := XTWriteDB().Model(models.DoctorAdvice{}).Where("id = ? and user_org_id = ? and status =1", id, user_org_id).Updates(map[string]interface{}{"execution_time": 0, "execution_staff": 0, "execution_state": 2}).Error
  1832. return err
  1833. }
  1834. func UpdateInformationSettingById(orgid int64, week_day int64) error {
  1835. err := XTWriteDB().Model(&models.XtDialysisInformaitonSetting{}).Where("user_org_id = ? and status =1", orgid).Updates(map[string]interface{}{"week_day": week_day}).Error
  1836. return err
  1837. }
  1838. func CreateInformationSetting(setting models.XtDialysisInformaitonSetting) error {
  1839. err := XTWriteDB().Create(&setting).Error
  1840. return err
  1841. }
  1842. func GetInformationSettingByOrgId(user_org_id int64) (models.XtDialysisInformaitonSetting, error) {
  1843. setting := models.XtDialysisInformaitonSetting{}
  1844. err := XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&setting).Error
  1845. return setting, err
  1846. }
  1847. func GetDialysisInformationSettingList(user_org_id int64) (infor []*models.XtDialysisInformaitonSetting, err error) {
  1848. err = XTReadDB().Where("user_org_id = ? and status =1", user_org_id).Find(&infor).Error
  1849. return infor, err
  1850. }
  1851. func GetDialysisInformationIsNoCheck(user_org_id int64, limit int64, page int64, application_status int64) (infor []*models.DialysisInformation, total int64, err error) {
  1852. db := XTReadDB().Model(&infor).Where("status= 1")
  1853. if application_status > 0 {
  1854. db = db.Where("application_status = ?", application_status)
  1855. }
  1856. if user_org_id > 0 {
  1857. db = db.Where("user_org_id = ?", user_org_id)
  1858. }
  1859. err = db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Find(&infor).Error
  1860. return infor, total, err
  1861. }
  1862. func GetDialysisInformationIsNoCheckOne(user_org_id int64, limit int64, page int64, application_status int64) (infor []*models.DialysisInformation, total int64, err error) {
  1863. db := XTReadDB().Model(&infor).Where("status= 1")
  1864. if application_status > 0 {
  1865. db = db.Where("application_status = ? or application_status= 3", application_status)
  1866. }
  1867. if user_org_id > 0 {
  1868. db = db.Where("user_org_id = ?", user_org_id)
  1869. }
  1870. err = db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Find(&infor).Error
  1871. return infor, total, err
  1872. }
  1873. func CheckDialysisInformation(id int64, application_status int64, timenow int64, checker int64) (models.DialysisInformation, error) {
  1874. information := models.DialysisInformation{}
  1875. err := XTWriteDB().Model(&information).Where("id=? and status = 1", id).Updates(map[string]interface{}{"application_status": application_status, "check_time": timenow, "checker": checker}).Error
  1876. return information, err
  1877. }
  1878. func GetDialysisWatchByKeywordFlow(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64, start int64, end int64) ([]*models.DialysisScheduleFlow, error, int64) {
  1879. var patients []*models.Patients
  1880. 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
  1881. if getPatientErr != nil {
  1882. return nil, getPatientErr, 0
  1883. }
  1884. patientIDs := make([]int64, len(patients))
  1885. for index, patient := range patients {
  1886. patientIDs[index] = patient.ID
  1887. }
  1888. db := readDb.Model(&models.DialysisScheduleFlow{})
  1889. if start > 0 && end > 0 {
  1890. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  1891. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  1892. Preload("TreatmentMode", "status = 1").
  1893. Preload("DialysisFinish", "status = 1 AND user_org_id = ? and record_date >= ? and record_date<=?", orgID, start, end).
  1894. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  1895. } else {
  1896. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  1897. Preload("TreatmentMode", "status = 1").
  1898. Preload("DialysisFinish", "status = 1 AND user_org_id = ?", orgID).
  1899. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  1900. }
  1901. db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
  1902. if schedulType > 0 {
  1903. db = db.Where("schedule_type = ?", schedulType)
  1904. }
  1905. if start > 0 {
  1906. db = db.Where("schedule_date >= ?", start)
  1907. }
  1908. if end > 0 {
  1909. db = db.Where("schedule_date<=?", end)
  1910. }
  1911. if partitionType > 0 {
  1912. db = db.Joins("inner join xt_device_number as d_n on d_n.id = xt_schedule.bed_id and d_n.zone_id = ?", partitionType)
  1913. }
  1914. var schedules []*models.DialysisScheduleFlow
  1915. total := int64(0)
  1916. err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  1917. return schedules, err, total
  1918. }
  1919. func GetDialysisWatchFlow(orgID int64, schedulDate int64, schedulType int64, partitionType int64, page int64, limit int64, start int64, end int64) (schedule []*models.DialysisScheduleFlow, err error, total int64) {
  1920. db := readDb.Model(&models.DialysisScheduleFlow{})
  1921. if schedulDate > 0 {
  1922. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  1923. Preload("TreatmentMode", "status = 1").
  1924. Preload("DialysisFinish", "status = 1 AND user_org_id = ? and record_date =?", orgID, schedulDate).
  1925. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  1926. db = db.Where("xt_schedule.status = 1")
  1927. db = db.Where("schedule_date = ?", schedulDate)
  1928. }
  1929. if schedulType > 0 {
  1930. db = db.Where("schedule_type = ?", schedulType)
  1931. }
  1932. if start > 0 {
  1933. db = db.Where("schedule_date >= ?", start)
  1934. }
  1935. if end > 0 {
  1936. db = db.Where("schedule_date <= ?", end)
  1937. }
  1938. if orgID > 0 {
  1939. db = db.Where("user_org_id = ?", orgID)
  1940. }
  1941. if partitionType > 0 {
  1942. db = db.Joins("inner join xt_device_number on xt_device_number.id = xt_schedule.bed_id and xt_device_number.zone_id = ?", partitionType)
  1943. }
  1944. offset := (page - 1) * limit
  1945. err = db.Count(&total).Offset(offset).Limit(limit).Order("bed_id desc").Find(&schedule).Error
  1946. return schedule, err, total
  1947. }
  1948. func GetPrescriptionInfo(user_org_id int64, patient_id int64, record_date int64) (models.HisPrescriptionInfo, error) {
  1949. info := models.HisPrescriptionInfo{}
  1950. err := XTReadDB().Where("user_org_id = ? and patient_id =? and record_date = ? and status=1", user_org_id, patient_id, record_date).Find(&info).Error
  1951. return info, err
  1952. }
  1953. func FindLastSchedule(user_org_id int64, patient_id int64, schedule_date int64) (models.XtSchedules, error) {
  1954. schedules := models.XtSchedules{}
  1955. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and schedule_date < ? and status =1", user_org_id, patient_id, schedule_date).Order("schedule_date desc").First(&schedules).Error
  1956. return schedules, err
  1957. }
  1958. func GetMonitorPatients(patient_id int64) (*models.MonitorPatients, error) {
  1959. patients := models.MonitorPatients{}
  1960. err := XTReadDB().Where("id = ? and status = 1", patient_id).Find(&patients).Error
  1961. return &patients, err
  1962. }
  1963. func GetMonitorDialysisOrderObj(patient_id int64, schedule_date int64) (*models.MonitorDialysisOrder, error) {
  1964. order := models.MonitorDialysisOrder{}
  1965. err := XTReadDB().Where("patient_id = ? and dialysis_date = ? and status = 1", patient_id, schedule_date).Find(&order).Error
  1966. return &order, err
  1967. }
  1968. func GetMonitorDialysisPrescription(patient_id int64, record_date int64) (*models.DialysisPrescription, error) {
  1969. prescription := models.DialysisPrescription{}
  1970. err := XTReadDB().Where("patient_id = ? and status =1 and record_date = ?", patient_id, record_date).Find(&prescription).Error
  1971. return &prescription, err
  1972. }
  1973. func GetMonitorPredialysisEvaluation(patient_id int64, record_date int64) (*models.PredialysisEvaluation, error) {
  1974. evaluation := models.PredialysisEvaluation{}
  1975. err := XTReadDB().Where("patient_id = ? and status =1 and assessment_date = ?", patient_id, record_date).Find(&evaluation).Error
  1976. return &evaluation, err
  1977. }
  1978. func GetMonitorAssessmentAfterDislysis(patient_id int64, record_date int64) (*models.AssessmentAfterDislysis, error) {
  1979. dislysis := models.AssessmentAfterDislysis{}
  1980. err = XTReadDB().Where("patient_id = ? and status =1 and assessment_date = ?", patient_id, record_date).Find(&dislysis).Error
  1981. return &dislysis, err
  1982. }
  1983. func GetMonitoringRecordList(patient_id int64, record_date int64) (record []*models.MonitoringRecord, err error) {
  1984. err = XTReadDB().Where("patient_id = ? and monitoring_date=? and status =1", patient_id, record_date).Find(&record).Error
  1985. return record, err
  1986. }
  1987. func GetDoctorAdviceListNoExecution(patient_id int64, dialysis_date int64, user_org_id int64) (doctor []*models.XtDoctorAdvice, err error) {
  1988. err = XTReadDB().Where("patient_id = ? and advice_date = ? and user_org_id = ? and execution_state = 2 and status =1", patient_id, dialysis_date, user_org_id).Find(&doctor).Error
  1989. return doctor, err
  1990. }
  1991. func UpdateDoctorAdviceNoExecution(id int64, creator int64, end_time int64) error {
  1992. err := XTWriteDB().Model(&models.XtDoctorAdvice{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"execution_state": 1, "execution_staff": creator, "execution_time": end_time}).Error
  1993. return err
  1994. }
  1995. func CreatePrescriptionLog(log models.XtDialysisPrescriptionLog) error {
  1996. ut := writeDb.Begin()
  1997. err := ut.Create(&log).Error
  1998. if err != nil {
  1999. ut.Rollback()
  2000. return err
  2001. }
  2002. ut.Commit()
  2003. return err
  2004. }
  2005. func CreateBeforLog(log models.XtAssessmentBeforeDislysisLog) error {
  2006. err := XTWriteDB().Create(&log).Error
  2007. return err
  2008. }
  2009. func CreateAfterDialysisLog(log models.XtAssessmentAfterDialysisLog) error {
  2010. err := XTWriteDB().Create(&log).Error
  2011. return err
  2012. }
  2013. func CreateDoctorAdviceLog(log models.XtDoctorAdviceLog) error {
  2014. err := XTWriteDB().Create(&log).Error
  2015. return err
  2016. }
  2017. func CreateMonitorRecordLog(log models.XtMonitorRecordLog) error {
  2018. err := XTWriteDB().Create(&log).Error
  2019. return err
  2020. }
  2021. func GetAllHisAdviceList(patient_id int64, advice_date int64, user_org_id int64) (advice []*models.HisDoctorAdvice, err error) {
  2022. err = XTReadDB().Where("patient_id = ? and advice_date =? and user_org_id = ? and status =1", patient_id, advice_date, user_org_id).Find(&advice).Error
  2023. return advice, err
  2024. }
  2025. func BatchCheckHisAdvice(id int64, creater int64) error {
  2026. err := XTWriteDB().Model(&models.HisDoctorAdvice{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"checker": creater, "check_state": 1, "check_time": time.Now().Unix()}).Error
  2027. return err
  2028. }
  2029. func BatchAdviceList(id int64, creater int64) error {
  2030. err := XTWriteDB().Model(&models.DoctorAdvice{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"checker": creater, "check_state": 1, "check_time": time.Now().Unix()}).Error
  2031. return err
  2032. }
  2033. func GetMobileProjectList(patient_id int64, advice_date int64, user_org_id int64) (project []*models.HisPrescriptionProject, err error) {
  2034. err = XTReadDB().Where("patient_id = ? and status=1 and record_date = ? and user_org_id = ?", patient_id, advice_date, user_org_id).Find(&project).Error
  2035. return project, err
  2036. }
  2037. func BatchCheckProject(id int64, creater int64) error {
  2038. err = XTWriteDB().Model(&models.HisPrescriptionProject{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"checker": creater, "check_state": 1, "check_time": time.Now().Unix()}).Error
  2039. return err
  2040. }
  2041. func GetAllAdviceList(patient_id int64, advice_date int64, user_org_id int64) (advice []*models.DoctorAdvice, err error) {
  2042. err = XTReadDB().Where("patient_id = ? and advice_date = ? and user_org_id = ? and status=1", patient_id, advice_date, user_org_id).Find(&advice).Error
  2043. return advice, err
  2044. }
  2045. func GetHistoryWarehouseOutInfo(patient_id int64, user_org_id int64, record_time int64, good_id int64, tx *gorm.DB) (outInfo []*models.WarehouseOutInfo, err error) {
  2046. err = tx.Where("patient_id = ? and org_id = ? and sys_record_time = ? and status =1 and good_id = ?", patient_id, user_org_id, record_time, good_id).Find(&outInfo).Error
  2047. if err != gorm.ErrRecordNotFound {
  2048. if err != nil {
  2049. tx.Rollback()
  2050. return
  2051. }
  2052. }
  2053. return outInfo, err
  2054. }
  2055. func GetDialysisAutoGoodInfo(patient_id int64, record_time int64, org_id int64, tx *gorm.DB, good_id int64) (detail []*models.AutomaticReduceDetail, err error) {
  2056. err = tx.Where("patient_id = ? and record_time=? and org_id=? and status= 1 and good_id = ?", patient_id, record_time, org_id, good_id).Find(&detail).Error
  2057. if err != gorm.ErrRecordNotFound {
  2058. if err != nil {
  2059. tx.Commit()
  2060. return
  2061. }
  2062. }
  2063. return detail, err
  2064. }
  2065. func CreateDialysisPrepare(prepare models.DialysisBeforePrepare, tx *gorm.DB) error {
  2066. err := tx.Create(&prepare).Error
  2067. if err != nil {
  2068. tx.Commit()
  2069. return err
  2070. }
  2071. return err
  2072. }
  2073. func ModfiyAddDialysisAuto(patient_id int64, record_date int64, count int64, good_id int64, tx *gorm.DB) error {
  2074. err = tx.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? and record_date = ? and good_id = ? and status = 1", patient_id, record_date, good_id).UpdateColumn("count", gorm.Expr("count + ?", count)).Error
  2075. if err != nil {
  2076. tx.Rollback()
  2077. return err
  2078. }
  2079. return err
  2080. }
  2081. func ModfiyReduceDialysisAuto(patient_id int64, record_date int64, count int64, good_id int64, tx *gorm.DB) error {
  2082. err = tx.Model(&models.DialysisBeforePrepare{}).Where("patient_id = ? and record_date = ? and good_id = ? and status = 1", patient_id, record_date, good_id).UpdateColumn("count", gorm.Expr("count - ?", count)).Error
  2083. if err != nil {
  2084. tx.Rollback()
  2085. return err
  2086. }
  2087. return err
  2088. }
  2089. func GetHisExecutionDoctorsThree(orgid int64, patientid int64, id int64, tx *gorm.DB) (doctorAdvice []*models.HisDoctorAdviceInfo, err error) {
  2090. err = tx.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id = ? and patient_id = ? AND id = ? and status = 1 and execution_state = 1", orgid, patientid, id).Find(&doctorAdvice).Error
  2091. if err != gorm.ErrRecordNotFound {
  2092. if err != nil {
  2093. tx.Rollback()
  2094. return doctorAdvice, err
  2095. }
  2096. }
  2097. return doctorAdvice, err
  2098. }
  2099. func CreateMobileAdivce(advice models.DoctorAdvice) error {
  2100. err := XTWriteDB().Create(&advice).Error
  2101. return err
  2102. }
  2103. func CreateMobileAdivceOne(advice models.XtDoctorAdviceOne) error {
  2104. err := XTWriteDB().Create(&advice).Error
  2105. return err
  2106. }
  2107. func GetLongDoctorAdviceByPatient(patient_id int64, org_id int64) (advice []*models.XtDoctorAdvice, err error) {
  2108. err = XTReadDB().Where("patient_id = ? and user_org_id = ? and status=1 and advice_type =1", patient_id, org_id).Order("start_time asc").Find(&advice).Error
  2109. return advice, err
  2110. }
  2111. func GetLastLongAdviceList(user_org_id int64) (models.XtDoctorAdvice, error) {
  2112. advice := models.XtDoctorAdvice{}
  2113. err := XTReadDB().Where("user_org_id =? and status=1 and advice_type =1", user_org_id).Last(&advice).Error
  2114. return advice, err
  2115. }
  2116. func DeleteSelfAdviceSubAdvice(id int64) (models.XtDoctorAdviceOne, error) {
  2117. adviceOne := models.XtDoctorAdviceOne{}
  2118. err2 := XTWriteDB().Model(&adviceOne).Where("id = ? and status=1", id).Updates(map[string]interface{}{"status": 0}).Error
  2119. return adviceOne, err2
  2120. }
  2121. func UpdateMobileDoctorAdviceById(id int64, advice models.XtDoctorAdviceOne) error {
  2122. err := XTWriteDB().Model(&models.XtDoctorAdviceOne{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"advice_name": advice.AdviceName, "advice_date": advice.AdviceDate, "start_time": advice.StartTime, "advice_desc": advice.AdviceDesc, "single_dose": advice.SingleDose, "single_dose_unit": advice.SingleDoseUnit, "prescribing_number": advice.PrescribingNumber, "prescribing_number_unit": advice.PrescribingNumberUnit, "delivery_way": advice.DeliveryWay, "execution_frequency": advice.ExecutionFrequency, "remark": advice.Remark}).Error
  2123. return err
  2124. }
  2125. func UpdateMobileDoctorAdviceByIdOne(id int64, advice models.XtDoctorAdviceOne) error {
  2126. err := XTWriteDB().Model(&models.XtDoctorAdviceOne{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"advice_name": advice.AdviceName, "advice_date": advice.AdviceDate, "start_time": advice.StartTime, "advice_desc": advice.AdviceDesc, "single_dose": advice.SingleDose, "single_dose_unit": advice.SingleDoseUnit, "prescribing_number": advice.PrescribingNumber, "prescribing_number_unit": advice.PrescribingNumberUnit, "delivery_way": advice.DeliveryWay, "execution_frequency": advice.ExecutionFrequency, "remark": advice.Remark, "frequency_type": advice.FrequencyType, "week_day": advice.WeekDay, "day_count": advice.DayCount}).Error
  2127. return err
  2128. }
  2129. func CreateMobileMGroupAdvice(orgId int64, advices []*models.GroupAdvice, groupNo int64) (list []*models.GroupAdvice, err error) {
  2130. if groupNo <= 0 {
  2131. group := GetMaxAdviceGroupID(orgId)
  2132. groupNo = group + 1
  2133. }
  2134. tx := writeDb.Begin()
  2135. defer func() {
  2136. if r := recover(); r != nil {
  2137. tx.Rollback()
  2138. }
  2139. }()
  2140. for _, advice := range advices {
  2141. advice.GroupNo = groupNo
  2142. advice.Status = 1
  2143. template_id := strconv.FormatInt(groupNo, 10)
  2144. advice.TemplateId = "M" + template_id
  2145. if err = tx.Create(advice).Error; err != nil {
  2146. tx.Rollback()
  2147. return
  2148. }
  2149. list = append(list, advice)
  2150. if len(advice.Children) > 0 {
  2151. for _, child := range advice.Children {
  2152. child.GroupNo = groupNo
  2153. child.ParentId = advice.ID
  2154. fmt.Println(child)
  2155. if err = tx.Create(&child).Error; err != nil {
  2156. tx.Rollback()
  2157. return
  2158. }
  2159. list = append(list, child)
  2160. }
  2161. }
  2162. }
  2163. tx.Commit()
  2164. return
  2165. }
  2166. func GetTodayPrescription(patient_id int64, user_org_id int64, record_date int64) (models.DialysisPrescription, error) {
  2167. prescription := models.DialysisPrescription{}
  2168. err := XTReadDB().Where("patient_id = ? and user_org_id = ? and record_date =? and status=1", patient_id, user_org_id, record_date).Find(&prescription).Error
  2169. return prescription, err
  2170. }
  2171. func GetMobilePatientMonitor(patient_id int64, record_date int64, user_org_id int64) (monitor []*models.MonitoringRecord, err error) {
  2172. err = XTReadDB().Where("patient_id = ? and monitoring_date = ? and user_org_id =? and status=1", patient_id, record_date, user_org_id).Order("operate_time asc").Find(&monitor).Error
  2173. return monitor, err
  2174. }
  2175. func ModifyDialysisPatientMonitor(id int64, ultrafiltration_rate float64) error {
  2176. err := XTWriteDB().Model(&models.MonitoringRecord{}).Where("id =? and status=1", id).Updates(map[string]interface{}{"ultrafiltration_rate": ultrafiltration_rate}).Error
  2177. return err
  2178. }
  2179. func ModifyDialysisPatientReplascementRate(id int64, replacement_rate float64) error {
  2180. err := XTWriteDB().Model(&models.MonitoringRecord{}).Where("id =? and status=1", id).Updates(map[string]interface{}{"replacement_rate": replacement_rate}).Error
  2181. return err
  2182. }
  2183. func ModifyPatientUltrafiltrationVolume(id int64, ultrafiltration_volume float64) error {
  2184. err := XTWriteDB().Model(&models.MonitoringRecord{}).Where("id =? and status=1", id).Updates(map[string]interface{}{"ultrafiltration_volume": ultrafiltration_volume}).Error
  2185. return err
  2186. }
  2187. func ModifyPatientDisplacementQuantity(id int64, displacement_quantity float64) error {
  2188. err := XTWriteDB().Model(&models.MonitoringRecord{}).Where("id = ? and status=1", id).Updates(map[string]interface{}{"displacement_quantity": displacement_quantity}).Error
  2189. return err
  2190. }