dialysis_service.go 73KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583
  1. package service
  2. import (
  3. "XT_New/models"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "strconv"
  8. "strings"
  9. "time"
  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", 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 AddSigleSummaryRecord(summary *models.TreatmentSummary) (err error) {
  194. tx := writeDb.Begin()
  195. errors := tx.Model(&models.TreatmentSummary{}).Create(&summary).Error
  196. if err != nil {
  197. tx.Rollback()
  198. }
  199. tx.Commit()
  200. return errors
  201. }
  202. func AddSigleReceiveTreatmentAssesRecord(receiveTreatmentAsses *models.ReceiveTreatmentAsses) (err error) {
  203. tx := writeDb.Begin()
  204. errors := tx.Model(&models.ReceiveTreatmentAsses{}).Create(&receiveTreatmentAsses).Error
  205. if err != nil {
  206. tx.Rollback()
  207. }
  208. tx.Commit()
  209. return errors
  210. }
  211. func AddSigleAssessmentAfterDislysisRecord(assessmentAfterDislysis *models.AssessmentAfterDislysis) (err error) {
  212. tx := writeDb.Begin()
  213. errors := tx.Model(&models.AssessmentAfterDislysis{}).Create(&assessmentAfterDislysis).Error
  214. if err != nil {
  215. tx.Rollback()
  216. }
  217. tx.Commit()
  218. return errors
  219. }
  220. func AddSigleAssessmentBeforeDislysisRecord(assessmentBeforeDislysis *models.PredialysisEvaluation) (err error) {
  221. tx := writeDb.Begin()
  222. errors := tx.Model(&models.PredialysisEvaluation{}).Create(&assessmentBeforeDislysis).Error
  223. if err != nil {
  224. tx.Rollback()
  225. }
  226. tx.Commit()
  227. return errors
  228. }
  229. func AddSigleDoubleCheck(check *models.DoubleCheck) (err error) {
  230. tx := writeDb.Begin()
  231. errors := tx.Model(&models.PredialysisEvaluation{}).Create(&check).Error
  232. if err != nil {
  233. tx.Rollback()
  234. }
  235. tx.Commit()
  236. return errors
  237. }
  238. //修改透析处方
  239. func UpDateDialysisPrescription(dialysisPrescription *models.DialysisPrescription) error {
  240. err := writeDb.Save(&dialysisPrescription).Error
  241. return err
  242. }
  243. //修改接诊评估
  244. func UpadateReceiveTreatmentAsses(receiveTreatmentAsses *models.ReceiveTreatmentAsses) error {
  245. err := writeDb.Save(receiveTreatmentAsses).Error
  246. return err
  247. }
  248. //修改透前评估
  249. func UpadatePredialysisEvaluation(predialysisEvaluation *models.PredialysisEvaluation) error {
  250. err := writeDb.Save(&predialysisEvaluation).Error
  251. return err
  252. }
  253. //修改双人核对
  254. func UpdateDoubleCheck(check *models.DoubleCheck) error {
  255. err := writeDb.Save(&check).Error
  256. return err
  257. }
  258. //修改透后评估
  259. func UpdateAssessmentAfterDislysisRecord(assessmentAfterDislysis *models.AssessmentAfterDislysis) error {
  260. err := writeDb.Save(&assessmentAfterDislysis).Error
  261. return err
  262. }
  263. func UpdatePredialysisEvaluation(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  264. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"systolic_blood_pressure": predialysisevaluation.SystolicBloodPressure}).Error
  265. return err
  266. }
  267. func UpdatePredialysisEvaluationTwo(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  268. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"diastolic_blood_pressure": predialysisevaluation.DiastolicBloodPressure}).Error
  269. return err
  270. }
  271. func UpdatePredialysisEvaluationThree(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  272. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"pulse_frequency": predialysisevaluation.PulseFrequency}).Error
  273. return err
  274. }
  275. //修改
  276. func UpdateSummeRecord(treatmentSummary *models.TreatmentSummary) error {
  277. err := writeDb.Save(&treatmentSummary).Error
  278. return err
  279. }
  280. // func UpdateDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  281. // tx := writeDb.Begin()
  282. // 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
  283. // if err != nil {
  284. // tx.Rollback()
  285. // }
  286. // tx.Commit()
  287. // return err
  288. // }
  289. func ExceDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  290. 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
  291. return err
  292. }
  293. func ExceHidDoctorAdviceById(m *models.HisDoctorAdviceInfo, id int64, patientId int64) error {
  294. 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}).Error
  295. return err
  296. }
  297. func CheckDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  298. 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
  299. return err
  300. }
  301. func CheckHisDoctorAdviceById(m *models.HisDoctorAdviceInfo, id int64, patientId int64) error {
  302. 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
  303. return err
  304. }
  305. func CreateMonitor(m *models.MonitoringRecord) (err error) {
  306. return writeDb.Create(m).Error
  307. }
  308. func UpdateMonitor(m *models.MonitoringRecord) (err error) {
  309. return writeDb.Save(m).Error
  310. }
  311. func DelMonitor(patientId int64, recordId int64) error {
  312. tx := writeDb.Begin()
  313. updateTime := time.Now().Unix()
  314. 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
  315. if err != nil {
  316. tx.Rollback()
  317. }
  318. tx.Commit()
  319. return err
  320. }
  321. //修改长期处方
  322. func UpdateSolution(solution *models.DialysisSolution) {
  323. writeDb.Save(&solution)
  324. }
  325. //创建长期处方
  326. func CreateSolution(solution *models.DialysisSolution) {
  327. writeDb.Create(&solution)
  328. }
  329. func SavePrescriptionAndCreateSolution(solution *models.DialysisSolution, prescription *models.DialysisPrescription) (err error) {
  330. tx := writeDb.Begin()
  331. if prescription.ID > 0 {
  332. err = tx.Save(&prescription).Error
  333. } else {
  334. err = tx.Model(&models.DialysisPrescription{}).Create(&prescription).Error
  335. }
  336. if err != nil {
  337. tx.Rollback()
  338. return
  339. }
  340. err = tx.Create(&solution).Error
  341. if err != nil {
  342. tx.Rollback()
  343. return
  344. }
  345. tx.Commit()
  346. return
  347. }
  348. //透析处方
  349. func FindDialysisPrescriptionByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DialysisPrescription) {
  350. dialysisPrescription := models.DialysisPrescription{}
  351. err := readDb.Model(&dialysisPrescription).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ? ", id, recordDate, orgId).Find(&dialysisPrescription).Error
  352. return err, &dialysisPrescription
  353. }
  354. func FindReceiveTreatmentAssesByReordDate(id int64, recordDate int64, orgId int64) (error, *models.ReceiveTreatmentAsses) {
  355. receiveTreatmentAsses := models.ReceiveTreatmentAsses{}
  356. err := readDb.Model(&receiveTreatmentAsses).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).Find(&receiveTreatmentAsses).Error
  357. return err, &receiveTreatmentAsses
  358. }
  359. //透前评估
  360. func FindPredialysisEvaluationByReordDate(id int64, recordDate int64, orgId int64) (error, *models.PredialysisEvaluation) {
  361. dialysisEvaluation := models.PredialysisEvaluation{}
  362. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&dialysisEvaluation).Error
  363. return err, &dialysisEvaluation
  364. }
  365. //双人核对
  366. func FindDoubleCheckByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoubleCheck) {
  367. check := models.DoubleCheck{}
  368. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? AND check_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&check).Error
  369. return err, &check
  370. }
  371. func FindAssessmentAfterDislysisByReordDate(id int64, recordDate int64, orgId int64) (error, *models.AssessmentAfterDislysis) {
  372. assessmentAfterDislysis := models.AssessmentAfterDislysis{}
  373. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&assessmentAfterDislysis).Error
  374. return err, &assessmentAfterDislysis
  375. }
  376. func FindTreatmentSummaryByReordDate(id int64, recordDate int64, orgId int64) (error, *models.TreatmentSummary) {
  377. summary := models.TreatmentSummary{}
  378. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&summary).Error
  379. return err, &summary
  380. }
  381. func FindDoctorAdviceByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoctorAdvice) {
  382. advice := models.DoctorAdvice{}
  383. 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
  384. return err, &advice
  385. }
  386. func FindDialysisRecordById(orgId int64, id int64, recordDate int64) (error, *models.DialysisOrder) {
  387. var count int64
  388. dialysisRecord := models.DialysisOrder{}
  389. err := readDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgId, id, recordDate).Find(&dialysisRecord).Count(&count).Error
  390. if count <= 0 {
  391. return err, nil
  392. }
  393. return err, &dialysisRecord
  394. }
  395. func FindDialysisSchedualById(orgId int64, id int64, recordDate int64) (error, *models.Schedule) {
  396. var count int64
  397. schedule := models.Schedule{}
  398. err := readDb.Model(&models.Schedule{}).Where("user_org_id = ? AND patient_id = ? AND schedule_date = ?", orgId, id, recordDate).Find(&schedule).Count(&count).Error
  399. if count <= 0 {
  400. return err, nil
  401. }
  402. return err, &schedule
  403. }
  404. func CreateDialysisRecord(id int64, orgId int64, record *models.DialysisOrder) error {
  405. err := writeDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? ", orgId, id).Create(&record).Error
  406. if err != nil {
  407. return err
  408. }
  409. dialysisPrescriptionErr := UpDateRecordIdToDialysisPrescription(id, orgId, record.DialysisDate, record.ID)
  410. if dialysisPrescriptionErr != nil {
  411. return dialysisPrescriptionErr
  412. }
  413. receiveTreatmentAssesErr := UpDateRecordIdToReceiveTreatmentAsses(id, orgId, record.DialysisDate, record.ID)
  414. if receiveTreatmentAssesErr != nil {
  415. return receiveTreatmentAssesErr
  416. }
  417. predialysisEvaluationErr := UpDateRecordIdToPredialysisEvaluation(id, orgId, record.DialysisDate, record.ID)
  418. if predialysisEvaluationErr != nil {
  419. return predialysisEvaluationErr
  420. }
  421. treatmentSummaryErr := UpDateRecordIdToTreatmentSummary(id, orgId, record.DialysisDate, record.ID)
  422. if treatmentSummaryErr != nil {
  423. return treatmentSummaryErr
  424. }
  425. doubleCheckErr := UpDateRecordIdToDoubleCheck(id, orgId, record.DialysisDate, record.ID)
  426. if doubleCheckErr != nil {
  427. return doubleCheckErr
  428. }
  429. adviceErrr := UpDateRecordIdToDoctorAdvice(id, orgId, record.DialysisDate, record.ID)
  430. if adviceErrr != nil {
  431. return adviceErrr
  432. }
  433. //透析监测
  434. records, err := FindAllMonitorRecord(orgId, id, record.DialysisDate)
  435. if err != nil {
  436. return err
  437. }
  438. var Ids []int64
  439. for _, item := range records {
  440. Ids = append(Ids, item.ID)
  441. }
  442. monitorErr := UpDateRecordIdToMonitor(Ids, record.ID)
  443. if monitorErr != nil {
  444. return monitorErr
  445. }
  446. return nil
  447. }
  448. func ModifyDialysisRecord(id int64, finishNurse int64, end_time int64, admin_user_id int64) error {
  449. 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
  450. return err
  451. }
  452. func UpdateSolutionByPatientId(id int64) error {
  453. err := writeDb.Model(&models.Patients{}).Where("id = ? AND status = 1 ", id).UpdateColumn("total_dialysis", gorm.Expr("total_dialysis + ?", 1)).Error
  454. return err
  455. }
  456. //更新透析处方记录ID
  457. func UpDateRecordIdToDialysisPrescription(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  458. 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
  459. return errors
  460. }
  461. func UpDateRecordIdToReceiveTreatmentAsses(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  462. 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
  463. return errors
  464. }
  465. func UpDateRecordIdToPredialysisEvaluation(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  466. 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
  467. return errors
  468. }
  469. func UpDateRecordIdToAssessmentAfterDislysis(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  470. 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
  471. return errors
  472. }
  473. func UpDateRecordIdToTreatmentSummary(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  474. 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
  475. return errors
  476. }
  477. func UpDateRecordIdToMonitor(Ids []int64, recordId int64) error {
  478. errors := writeDb.Model(&models.MonitoringRecord{}).Where("id IN (?)", Ids).Update(map[string]interface{}{"dialysis_order_id": recordId}).Error
  479. return errors
  480. }
  481. func UpDateRecordIdToDoubleCheck(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  482. 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
  483. return errors
  484. }
  485. func UpDateRecordIdToDoctorAdvice(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  486. 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
  487. return errors
  488. }
  489. //透析处方
  490. func FindPatientPrescribeWidyDevideById(orgID int64, patientsId int64, recordDate int64) (patient models.DialysisPrescriptionAddtional, err error) {
  491. err = readDb.Preload("Device", "status = 1 AND org_id = ?", orgID).
  492. Model(&models.DialysisPrescriptionAddtional{}).
  493. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? ", patientsId, orgID, recordDate).
  494. First(&patient).Error
  495. return
  496. }
  497. func FindDialysisMode(orgID int64, patientId int64, scheduleDate int64) (models.XtSchedule, error) {
  498. schedule := models.XtSchedule{}
  499. err = readDb.Where("user_org_id = ? and patient_id = ? and schedule_date = ? ", orgID, patientId, scheduleDate).Find(&schedule).Error
  500. return schedule, err
  501. }
  502. func FindDialysisMacher(orgid int64, patientid int64, dialysisDate int64) (models.XtDialysisOrders, error) {
  503. orders := models.XtDialysisOrders{}
  504. db := readDb.Table("xt_dialysis_order as o").Where("o.status = 1")
  505. table := readDb.Table("xt_device_number as n")
  506. fmt.Print("table", table)
  507. err := db.Select("o.id,o.dialysis_date,o.user_org_id,o.patient_id,o.prescription_id,o.stage,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,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
  508. return orders, err
  509. }
  510. func FindPunctureNurse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  511. role := models.App_Roles{}
  512. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  513. //return role, err
  514. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status =1")
  515. 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
  516. return role, err
  517. }
  518. func FindeStartNuse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  519. role := models.App_Roles{}
  520. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  521. 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
  522. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  523. return role, err
  524. }
  525. func FindDialysisWay(orgid int64, patientid int64, recorddate int64) (models.DialysisPrescription, error) {
  526. prescription := models.DialysisPrescription{}
  527. err := readDb.Where("user_org_id = ? and patient_id = ? and record_date = ? and status =1", orgid, patientid, recorddate).Find(&prescription).Error
  528. return prescription, err
  529. }
  530. func FindDoctorOrder(orgid int64, patientid int64, adviceDate int64) (models.XtDoctorAdvice, error) {
  531. advices := models.XtDoctorAdvice{}
  532. db := readDb.Table("xt_doctor_advice as a").Where("a.status = ?", 1)
  533. 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
  534. return advices, err
  535. }
  536. func FindDoctor(adminuserid int64, appid int64, orgid int64) (models.App_Role, error) {
  537. role := models.App_Role{}
  538. err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminuserid, appid, orgid).Find(&role).Error
  539. return role, err
  540. }
  541. func FindDoctorName(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  542. role := models.App_Roles{}
  543. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  544. 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
  545. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  546. return role, err
  547. }
  548. /////////////// 新 18-10-22
  549. func GetDialysisScheduals(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  550. var vms []*MDialysisScheduleVM
  551. db := readDb.
  552. Table("xt_schedule").
  553. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  554. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  555. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  556. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).
  557. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  558. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  559. Preload("TreatmentSummary", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  560. Preload("Advices", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  561. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  562. Where("status = 1 AND user_org_id = ?", orgID)
  563. if scheduleDate != 0 {
  564. db = db.Where("schedule_date = ?", scheduleDate)
  565. }
  566. err := db.Find(&vms).Error
  567. return vms, err
  568. }
  569. func GetDialysisSchedualsOne(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  570. var vms []*MDialysisScheduleVM
  571. db := readDb.
  572. Table("xt_schedule").
  573. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  574. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  575. //Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).
  576. //Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  577. //Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  578. //Preload("TreatmentSummary", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  579. //Preload("Advices", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  580. //Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  581. Where("status = 1 AND user_org_id = ?", orgID)
  582. if scheduleDate != 0 {
  583. db = db.Where("schedule_date = ?", scheduleDate)
  584. }
  585. err := db.Find(&vms).Error
  586. return vms, err
  587. }
  588. func GetLastAcceptsAssessment(patient int64, orgId int64) (error, models.ReceiveTreatmentAsses) {
  589. var receiveTreatmentAsses models.ReceiveTreatmentAsses
  590. err := readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient, orgId).Last(&receiveTreatmentAsses).Error
  591. return err, receiveTreatmentAsses
  592. }
  593. func GetDialysisWatch(orgID int64, schedulDate int64, schedulType int64, partitionType int64, page int64, limit int64) (schedule []*models.DialysisSchedule, err error, total int64) {
  594. db := readDb.
  595. Model(&models.MonitorDialysisSchedule{}).
  596. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  597. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  598. Preload("TreatmentMode", "status = 1").
  599. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  600. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  601. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  602. Preload("MonitoringRecord", func(db *gorm.DB) *gorm.DB {
  603. return db.Where("status = 1 AND user_org_id = ?", orgID).Order("operate_time asc")
  604. }).
  605. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  606. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  607. db = db.Where("xt_schedule.status = 1 AND user_org_id = ?", orgID)
  608. if schedulDate > 0 {
  609. db = db.Where("schedule_date = ?", schedulDate)
  610. }
  611. if schedulType > 0 {
  612. db = db.Where("schedule_type = ?", schedulType)
  613. }
  614. if partitionType > 0 {
  615. db = db.Joins("inner join xt_device_number on xt_device_number.id = xt_schedule.bed_id and xt_device_number.zone_id = ? and xt_device_number.status = 1", partitionType)
  616. // db = db.Where("partition_id = ?", partitionType)
  617. }
  618. offset := (page - 1) * limit
  619. err = db.Count(&total).Offset(offset).Limit(limit).Order("bed_id desc").Find(&schedule).Error
  620. return schedule, err, total
  621. }
  622. func GetDialysisWatchByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64) (schedules []*models.DialysisSchedule, err error, total int64) {
  623. //var patients []*models.Patients
  624. //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
  625. //if getPatientErr != nil {
  626. // return nil, getPatientErr, 0
  627. //}
  628. //patientIDs := make([]int64, len(patients))
  629. //for index, patient := range patients {
  630. // patientIDs[index] = patient.ID
  631. //}
  632. //
  633. //db := readDb.
  634. // Model(&models.DialysisSchedule{}).
  635. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  636. // Preload("TreatmentMode", "status = 1").
  637. // Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  638. // Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  639. // Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  640. // Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  641. // Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  642. // Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  643. //db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
  644. //if schedulType > 0 {
  645. // db = db.Where("schedule_type = ?", schedulType)
  646. //}
  647. //if partitionType > 0 {
  648. // db = db.Joins("inner join xt_device_number as d_n on d_n.id = xt_schedule.bed_id and d_n.zone_id = ? and d_n.status = 1", partitionType)
  649. //
  650. //}
  651. //var schedules []*models.DialysisSchedule
  652. //total := int64(0)
  653. //err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  654. //return schedules, err, total
  655. db := readDb.Model(&models.DialysisSchedule{}).Where("status = 1")
  656. if schedulType > 0 {
  657. db = db.Where("scheudle_type = ?", schedulType)
  658. }
  659. if partitionType > 0 {
  660. db = db.Where("partition_id = ?", partitionType)
  661. }
  662. err = db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  663. return schedules, err, total
  664. }
  665. func GetAdminUserES(orgID int64, appID int64, ids []int64) (es []*models.AdminUserElectronicSignature, err error) {
  666. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id=? and creator in (?) and status=1", orgID, appID, ids).Find(&es).Error
  667. return
  668. }
  669. //
  670. //func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  671. //
  672. // db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  673. // table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  674. // fmt.Println("table", table)
  675. // 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
  676. // return admin, err
  677. //}
  678. func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  679. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status =1")
  680. table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  681. fmt.Println("table", table)
  682. 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
  683. return admin, err
  684. }
  685. func UpDateDialysisPrescriptionDoctorSign(patient int64, date int64, orgId int64, doctor_id int64) (err error) {
  686. 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
  687. return
  688. }
  689. func GetAllAdminUserES(orgID int64, appId int64) (es []*models.AdminUserElectronicSignature, err error) {
  690. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id = ? and status=1", orgID, appId).Find(&es).Error
  691. return
  692. }
  693. func GetAllStarfEs(orgid int64) (es []*models.SgjUserAdminRoles, err error) {
  694. redis := RedisClient()
  695. defer redis.Close()
  696. // cur_date := time.Now().Format("2006-01-02")
  697. key := strconv.FormatInt(orgid, 10) + ":starfes"
  698. starfes_str, _ := redis.Get(key).Result()
  699. if len(starfes_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  700. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  701. 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
  702. if err != nil {
  703. if err == gorm.ErrRecordNotFound {
  704. if len(es) <= 0 {
  705. redis.Set(key, "starfes_str_null", time.Second*60*60*18)
  706. }
  707. return nil, nil
  708. } else {
  709. return nil, err
  710. }
  711. } else {
  712. if len(es) > 0 {
  713. //缓存数据
  714. starfes_str, err := json.Marshal(es)
  715. if err == nil {
  716. redis.Set(key, starfes_str, time.Second*60*60*18)
  717. return es, nil
  718. }
  719. } else {
  720. redis.Set(key, "starfes_str_null", time.Second*60*60*18)
  721. return es, nil
  722. }
  723. return es, nil
  724. }
  725. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  726. if starfes_str == "starfes_str_null" {
  727. return es, nil
  728. } else {
  729. json.Unmarshal([]byte(starfes_str), &es)
  730. return es, nil
  731. }
  732. }
  733. }
  734. func FindLastMonitorRecord(patient_id int64, org_id int64) (record models.MonitoringRecord, err error) {
  735. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient_id, org_id).Last(&record).Error
  736. return
  737. }
  738. func FindLastMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  739. 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
  740. return
  741. }
  742. func FindFirstMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  743. 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
  744. return
  745. }
  746. func ExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  747. ut := writeDb.Begin()
  748. err = ut.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
  749. if err != nil {
  750. ut.Rollback()
  751. return
  752. }
  753. ut.Commit()
  754. return err
  755. }
  756. func ExceOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  757. ut := writeDb.Begin()
  758. 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
  759. if err != nil {
  760. ut.Rollback()
  761. return
  762. }
  763. 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
  764. if err != nil {
  765. ut.Rollback()
  766. return
  767. }
  768. ut.Commit()
  769. return err
  770. }
  771. func CheckDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  772. ut := writeDb.Begin()
  773. 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
  774. if err != nil {
  775. ut.Rollback()
  776. return
  777. }
  778. ut.Commit()
  779. return err
  780. }
  781. func CheckOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  782. ut := writeDb.Begin()
  783. 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
  784. if err != nil {
  785. ut.Rollback()
  786. return
  787. }
  788. 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
  789. if err != nil {
  790. ut.Rollback()
  791. return
  792. }
  793. ut.Commit()
  794. return err
  795. }
  796. func DeleteDoctorAdviceByGroupNo(m *models.DoctorAdvice) (err error) {
  797. ut := writeDb.Begin()
  798. 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
  799. if err != nil {
  800. ut.Rollback()
  801. return
  802. }
  803. ut.Commit()
  804. return
  805. }
  806. func GetDialysisBoard(orgID int64, scheduleDate int64, offset, limit int64) ([]*models.DialysisBoardVM, error) {
  807. var boards []*models.DialysisBoardVM
  808. db := readDb.
  809. Table("xt_schedule").
  810. Preload("Patient", "status = 1 AND user_org_id = ?", orgID).
  811. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  812. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  813. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  814. Preload("SigninAndWeigh", "status = 1 AND user_org_id = ?", orgID).
  815. Preload("NextSchedules", func(db *gorm.DB) *gorm.DB {
  816. return db.Where("user_org_id = ? AND schedule_date > ? AND status = 1", orgID, scheduleDate).
  817. Order("schedule_date asc, id asc")
  818. }).
  819. Where("status = 1 AND user_org_id = ?", orgID)
  820. if scheduleDate != 0 {
  821. db = db.Where("schedule_date = ?", scheduleDate)
  822. }
  823. err := db.Offset(offset).Limit(limit).Find(&boards).Error
  824. return boards, err
  825. }
  826. func GetOneDialysisOrder(orgID, orderData int64, patient_id int64) (*models.DialysisOrder, error) {
  827. var order models.DialysisOrder
  828. var err error
  829. 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
  830. if err == gorm.ErrRecordNotFound {
  831. return nil, nil
  832. }
  833. if err != nil {
  834. return nil, err
  835. }
  836. return &order, nil
  837. }
  838. func GetOrgInfoTemplate(orgID int64) (models.GobalTemplate, error) {
  839. var templateInfo models.GobalTemplate
  840. var err error
  841. err = readDb.Model(&models.GobalTemplate{}).Where("org_id=? and status=1", orgID).First(&templateInfo).Error
  842. return templateInfo, err
  843. }
  844. func FindAdminRoleTypeById(orgId int64, adminUserId int64, appId int64) (models.App_Role, error) {
  845. var appRole models.App_Role
  846. var err error
  847. 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
  848. return appRole, err
  849. }
  850. func BatchExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  851. ut := writeDb.Begin()
  852. err = ut.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
  853. if err != nil {
  854. ut.Rollback()
  855. return
  856. }
  857. err = ut.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
  858. if err != nil {
  859. ut.Rollback()
  860. return
  861. }
  862. ut.Commit()
  863. return err
  864. }
  865. func BatchCheckOldDoctorAdvice(m *models.DoctorAdvice, ids []string, org_id int64, date int64) (err error) {
  866. ut := writeDb.Begin()
  867. 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
  868. if err != nil {
  869. ut.Rollback()
  870. return
  871. }
  872. 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
  873. if err != nil {
  874. ut.Rollback()
  875. return
  876. }
  877. ut.Commit()
  878. return err
  879. }
  880. func ModifyScheduleMode(mode_id int64, patient_id int64, recordDate int64, org_id int64) {
  881. ut := writeDb.Begin()
  882. 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}).Error
  883. if err != nil {
  884. ut.Rollback()
  885. return
  886. }
  887. ut.Commit()
  888. }
  889. func FindDialysisOrderById(id int64) (error, *models.DialysisOrder) {
  890. dialysisRecord := models.DialysisOrder{}
  891. err := readDb.Model(&models.DialysisOrder{}).Where("id = ? AND status = 1", id).Find(&dialysisRecord).Error
  892. return err, &dialysisRecord
  893. }
  894. func ModifyExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  895. ut := writeDb.Begin()
  896. 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
  897. if err != nil {
  898. ut.Rollback()
  899. return
  900. }
  901. ut.Commit()
  902. return err
  903. }
  904. func BatchModifyExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  905. ut := writeDb.Begin()
  906. 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
  907. if err != nil {
  908. ut.Rollback()
  909. return
  910. }
  911. 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
  912. if err != nil {
  913. ut.Rollback()
  914. return
  915. }
  916. ut.Commit()
  917. return err
  918. }
  919. func UpdateOrderCount(good_type_id int64, good_id int64, number string, org_id int64) {
  920. 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))
  921. }
  922. func UpdateOrderInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  923. 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})
  924. }
  925. func FindOrderInfoByGoodId(good_type_id int64, good_id int64, number string, org_id int64) (err error, out models.WarehouseOutInfo) {
  926. 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
  927. return
  928. }
  929. func FindGoodUserById(good_type_id int64, good_id int64, id int64, org_id int64, record_time int64) (user models.AutomaticReduceDetail, err error) {
  930. 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
  931. return
  932. }
  933. func FindOldOrderInfoByGoodId(time int64, org_id int64) (err error, out models.WarehouseOutInfo) {
  934. err = readDb.Model(&models.WarehouseOutInfo{}).Where(" org_id = ? AND status = 1 AND sys_record_time = ? ", org_id, time).First(&out).Error
  935. return
  936. }
  937. func FindStockOutOrderNumberByTime(time int64, org_id int64) (err error, out models.WarehouseOut) {
  938. err = readDb.Model(&models.WarehouseOut{}).Where(" org_id = ? AND status = 1 AND warehouse_out_time = ? ", org_id, time).First(&out).Error
  939. return
  940. }
  941. func UpdateDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  942. 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})
  943. }
  944. func FindPatientIsOpenRemindById(patient_id int64, org_id int64) (patients models.Patients, err error) {
  945. err = readDb.Model(&models.Patients{}).Where("status = 1 AND id = ? AND user_org_id = ?", patient_id, org_id).First(&patients).Error
  946. return
  947. }
  948. func GetMaxLongAdviceGroupID(orgId int64, patient_id int64) (group int64) {
  949. var advice models.DoctorAdvice
  950. 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
  951. if err != nil {
  952. fmt.Println(err)
  953. group = 0
  954. }
  955. group = advice.GroupNo
  956. return
  957. }
  958. func GetLastLongAdviceByGroupNo(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  959. 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", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  960. return
  961. }
  962. func GetLastLongAdviceByGroupNoOne(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  963. redis := RedisClient()
  964. defer redis.Close()
  965. key := strconv.FormatInt(orgId, 10) + ":" + strconv.FormatInt(patient_id, 10) + ":long_advice"
  966. long_advice_str, _ := redis.Get(key).Result()
  967. if len(long_advice_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  968. 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", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  969. if err != nil {
  970. if err == gorm.ErrRecordNotFound {
  971. return nil, nil
  972. } else {
  973. return nil, err
  974. }
  975. } else {
  976. if len(advice) > 0 {
  977. //缓存数据
  978. starfes_str, err := json.Marshal(advice)
  979. if err == nil {
  980. redis.Set(key, starfes_str, time.Second*60*60*18)
  981. return advice, nil
  982. }
  983. } else {
  984. redis.Set(key, "null", time.Second*60*60*18)
  985. return advice, nil
  986. }
  987. return advice, nil
  988. }
  989. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  990. if long_advice_str == "null" {
  991. return advice, nil
  992. } else {
  993. json.Unmarshal([]byte(long_advice_str), &advice)
  994. return advice, nil
  995. }
  996. }
  997. 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", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  998. return
  999. }
  1000. func GetLastLongAdviceByGroupNoThree(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  1001. 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
  1002. return
  1003. }
  1004. func GetLastLongAdviceByGroupNoFour(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  1005. //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
  1006. //return
  1007. redis := RedisClient()
  1008. defer redis.Close()
  1009. key := strconv.FormatInt(orgId, 10) + ":" + strconv.FormatInt(patient_id, 10) + ":long_advice"
  1010. long_advice_str, _ := redis.Get(key).Result()
  1011. if len(long_advice_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1012. 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
  1013. if err != nil {
  1014. if err == gorm.ErrRecordNotFound {
  1015. return nil, nil
  1016. } else {
  1017. return nil, err
  1018. }
  1019. } else {
  1020. if len(advice) > 0 {
  1021. //缓存数据
  1022. starfes_str, err := json.Marshal(advice)
  1023. if err == nil {
  1024. redis.Set(key, starfes_str, time.Second*60*60*18)
  1025. return advice, nil
  1026. }
  1027. } else {
  1028. redis.Set(key, "null", time.Second*60*60*18)
  1029. return advice, nil
  1030. }
  1031. return advice, nil
  1032. }
  1033. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1034. if long_advice_str == "null" {
  1035. return advice, nil
  1036. } else {
  1037. json.Unmarshal([]byte(long_advice_str), &advice)
  1038. return advice, nil
  1039. }
  1040. }
  1041. 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", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  1042. return
  1043. }
  1044. func FindAllDoctorAdviceByTime(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  1045. 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
  1046. return
  1047. }
  1048. func UpdateScheduleModeId(patient_id int64, org_id int64, date int64, mode_id int64) (err error) {
  1049. 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
  1050. return
  1051. }
  1052. func UpdateAssessmentAfterDate(patient_id int64, org_id int64, date int64, actual_treatment_hour int64, actual_treatment_minute int64) (err error) {
  1053. 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
  1054. return
  1055. }
  1056. func CreateDialysisBeforePrepare(beforePrepares []*models.DialysisBeforePrepare) (err error) {
  1057. if len(beforePrepares) > 0 {
  1058. utx := writeDb.Begin()
  1059. if len(beforePrepares) > 0 {
  1060. 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 "
  1061. insertParams := make([]string, 0)
  1062. insertData := make([]interface{}, 0)
  1063. for _, prepare := range beforePrepares {
  1064. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?)")
  1065. insertData = append(insertData, prepare.UserOrgId)
  1066. insertData = append(insertData, prepare.PatientId)
  1067. insertData = append(insertData, prepare.RecordDate)
  1068. insertData = append(insertData, prepare.GoodId)
  1069. insertData = append(insertData, prepare.GoodTypeId)
  1070. insertData = append(insertData, prepare.Count)
  1071. insertData = append(insertData, prepare.Ctime)
  1072. insertData = append(insertData, prepare.Mtime)
  1073. insertData = append(insertData, prepare.Creater)
  1074. insertData = append(insertData, prepare.Modifier)
  1075. insertData = append(insertData, 1)
  1076. insertData = append(insertData, prepare.CommdityCode)
  1077. }
  1078. thisSQL += strings.Join(insertParams, ", ")
  1079. err = utx.Exec(thisSQL, insertData...).Error
  1080. if err != nil {
  1081. utx.Rollback()
  1082. return
  1083. }
  1084. }
  1085. utx.Commit()
  1086. }
  1087. return
  1088. }
  1089. func UpdateDialysisBeforePrepare(info *models.DialysisBeforePrepare) (err error) {
  1090. err = writeDb.Save(&info).Error
  1091. return err
  1092. }
  1093. func UpdateDialysisBeforePrepareTwo(info *models.DialysisBeforePrepare) (err error) {
  1094. err = writeDb.Model(&models.DialysisBeforePrepare{}).
  1095. 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).
  1096. Updates(map[string]interface{}{"count": info.Count, "updated_time": time.Now().Unix(), "modifier": info.Modifier, "commdity_code": info.CommdityCode, "status": 1}).Error
  1097. return err
  1098. }
  1099. func UpdateAutoReduceDetailSix(detail *models.AutomaticReduceDetail) (err error) {
  1100. 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
  1101. return err
  1102. }
  1103. func UpdateOrderCountSub(good_type_id int64, good_id int64, number string, org_id int64, count int64) {
  1104. 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))
  1105. }
  1106. func UpdateOrderCountAdd(good_type_id int64, good_id int64, number string, org_id int64, count int64) {
  1107. 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))
  1108. }
  1109. func UpdateUserInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, count int64, out *models.WarehouseOutInfo) {
  1110. 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})
  1111. //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)
  1112. }
  1113. func UpdateDrugUserInfoDetails(drug_id int64, time int64, org_id int64, patient_id int64, count int64, out *models.DrugWarehouseOutInfo) {
  1114. 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})
  1115. //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)
  1116. }
  1117. func FindDialysisBeforePrepare(patient_id int64, good_id int64, good_type_id int64, user_org_id int64, record_time int64) (count int64) {
  1118. 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)
  1119. return
  1120. }
  1121. func GetExecutionDoctors(orgid int64, patientid int64, id int64) (doctorAdvice []*models.DoctorAdvice, err error) {
  1122. 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
  1123. return doctorAdvice, err
  1124. }
  1125. func GetHisExecutionDoctors(orgid int64, patientid int64, id int64) (doctorAdvice []*models.HisDoctorAdviceInfo, err error) {
  1126. 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
  1127. return doctorAdvice, err
  1128. }
  1129. func FindHisDoctorAdviceById(orgID int64, id int64) (advice models.HisDoctorAdviceInfo, err error) {
  1130. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id=? and status=1 and id = ?", orgID, id).First(&advice).Error
  1131. return
  1132. }
  1133. func FindHisDoctorAdviceByIds(orgID int64, ids []string) (advice []*models.HisDoctorAdviceInfo, err error) {
  1134. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id=? and status=1 and id in (?)", orgID, ids).Find(&advice).Error
  1135. return
  1136. }
  1137. func SaveHisDoctorAdvice(advice *models.HisDoctorAdviceInfo) (err error) {
  1138. err = writeDb.Save(&advice).Error
  1139. return
  1140. }
  1141. func GetGoodInfoMation(orgid int64) (goodinfo []*models.GoodInfo, err error) {
  1142. err = XTReadDB().Model(&goodinfo).Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodinfo).Error
  1143. return goodinfo, err
  1144. }
  1145. func GetDialysisBeforePrepare(goodTypeId int64, goodId int64, orgid int64, patientId int64) (*models.DialysisBeforePrepare, error) {
  1146. prepare := models.DialysisBeforePrepare{}
  1147. 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
  1148. if err == gorm.ErrRecordNotFound {
  1149. return nil, err
  1150. }
  1151. if err != nil {
  1152. return nil, err
  1153. }
  1154. return &prepare, nil
  1155. }
  1156. func CreateDialysisBeforePrepareOne(prepare *models.DialysisBeforePrepare) error {
  1157. err := XTReadDB().Create(&prepare).Error
  1158. return err
  1159. }
  1160. func ModifyExceDoctorAdviceById(m *models.HisDoctorAdviceInfo, ids []string) (err error) {
  1161. ut := writeDb.Begin()
  1162. 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
  1163. if err != nil {
  1164. ut.Rollback()
  1165. return
  1166. }
  1167. 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
  1168. if err != nil {
  1169. ut.Rollback()
  1170. return
  1171. }
  1172. ut.Commit()
  1173. return err
  1174. }
  1175. func FindHisProjectById(orgID int64, id int64) (advice models.HisPrescriptionProject, err error) {
  1176. err = readDb.Model(&models.HisPrescriptionProject{}).Where("user_org_id=? and status=1 and id = ?", orgID, id).First(&advice).Error
  1177. return
  1178. }
  1179. func SaveHisProject(advice *models.HisPrescriptionProject) (err error) {
  1180. err = writeDb.Save(&advice).Error
  1181. return
  1182. }
  1183. func FindAllHisProjectById(orgID int64, patient_id int64, record_time int64) (advice []*models.HisPrescriptionProject, err error) {
  1184. err = readDb.Model(&models.HisPrescriptionProject{}).Preload("HisProject", "status = 1").Preload("GoodInfo", "status = 1").Where("user_org_id=? and status=1 and patient_id = ? AND record_date = ?", orgID, patient_id, record_time).Find(&advice).Error
  1185. return
  1186. }
  1187. func GetAllPcPatientListByList(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1188. redis := RedisClient()
  1189. defer redis.Close()
  1190. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  1191. patient_info_str, _ := redis.Get(key).Result()
  1192. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1193. err = readDb.Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  1194. if err != nil {
  1195. if err == gorm.ErrRecordNotFound {
  1196. if len(patients) <= 0 {
  1197. redis.Set(key, "null", time.Second*60*60*18)
  1198. }
  1199. return nil, nil
  1200. } else {
  1201. return nil, err
  1202. }
  1203. } else {
  1204. if len(patients) > 0 {
  1205. //缓存数据
  1206. patient_info_json, err := json.Marshal(&patients)
  1207. if err == nil {
  1208. redis.Set(key, patient_info_json, time.Second*60*60*18)
  1209. }
  1210. } else {
  1211. redis.Set(key, "null", time.Second*60*60*18)
  1212. }
  1213. return patients, nil
  1214. }
  1215. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1216. if patient_info_str == "null" {
  1217. return nil, nil
  1218. } else {
  1219. json.Unmarshal([]byte(patient_info_str), &patients)
  1220. return patients, nil
  1221. }
  1222. }
  1223. }
  1224. func GetAllPcAdvicestByList(orgID int64, scheduleDate int64) (advices []*VMDoctorAdvice, err error) {
  1225. redis := RedisClient()
  1226. defer redis.Close()
  1227. key := strconv.FormatInt(orgID, 10) + ":" + ":advice_list_all"
  1228. advice_list_str, _ := redis.Get(key).Result()
  1229. if len(advice_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1230. err = readDb.Where("status = 1 AND user_org_id = ? AND advice_type = 2 AND advice_date = ? ", orgID, scheduleDate).Find(&advices).Error
  1231. if err != nil {
  1232. if err == gorm.ErrRecordNotFound {
  1233. if len(advices) <= 0 {
  1234. redis.Set(key, "null", time.Second*60*60*18)
  1235. }
  1236. return nil, nil
  1237. } else {
  1238. return nil, err
  1239. }
  1240. } else {
  1241. if len(advices) > 0 {
  1242. //缓存数据
  1243. advice_list_str, err := json.Marshal(&advices)
  1244. if err == nil {
  1245. redis.Set(key, advice_list_str, time.Second*60*60*18)
  1246. }
  1247. } else {
  1248. redis.Set(key, "null", time.Second*60*60*18)
  1249. }
  1250. return advices, nil
  1251. }
  1252. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1253. if advice_list_str == "null" {
  1254. return nil, nil
  1255. } else {
  1256. json.Unmarshal([]byte(advice_list_str), &advices)
  1257. return advices, nil
  1258. }
  1259. }
  1260. }
  1261. func GetAllPcDialysisOrdersByList(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVMList, err error) {
  1262. redis := RedisClient()
  1263. defer redis.Close()
  1264. key := strconv.FormatInt(orgID, 10) + ":" + ":dialysis_orders_list_all"
  1265. dialysis_orders_list_all, _ := redis.Get(key).Result()
  1266. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1267. 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
  1268. if err != nil {
  1269. if err == gorm.ErrRecordNotFound {
  1270. return nil, nil
  1271. } else {
  1272. return nil, err
  1273. }
  1274. } else {
  1275. if len(dialysisOrders) > 0 {
  1276. //缓存数据
  1277. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  1278. if err == nil {
  1279. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  1280. }
  1281. } else {
  1282. redis.Set(key, "null", time.Second*60*60*18)
  1283. }
  1284. return dialysisOrders, nil
  1285. }
  1286. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1287. if dialysis_orders_list_all == "null" {
  1288. return nil, nil
  1289. } else {
  1290. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  1291. return dialysisOrders, nil
  1292. }
  1293. }
  1294. }
  1295. func GetAllPcAssessmentAfterDislysisByList(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*AssessmentAfterDislysis, err error) {
  1296. redis := RedisClient()
  1297. defer redis.Close()
  1298. key := strconv.FormatInt(orgID, 10) + ":" + ":assessment_after_dislysis_list_all"
  1299. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  1300. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1301. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  1302. if err != nil {
  1303. if err == gorm.ErrRecordNotFound {
  1304. if len(assessmentAfterDislysis) <= 0 {
  1305. redis.Set(key, "null", time.Second*60*60*18)
  1306. }
  1307. return nil, nil
  1308. } else {
  1309. return nil, err
  1310. }
  1311. } else {
  1312. if len(assessmentAfterDislysis) > 0 {
  1313. //缓存数据
  1314. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  1315. if err == nil {
  1316. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  1317. }
  1318. } else {
  1319. redis.Set(key, "null", time.Second*60*60*18)
  1320. }
  1321. return assessmentAfterDislysis, nil
  1322. }
  1323. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1324. if assessment_after_dislysis__all == "null" {
  1325. return nil, nil
  1326. } else {
  1327. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  1328. return assessmentAfterDislysis, nil
  1329. }
  1330. }
  1331. }
  1332. func GetAllPcTreatmentSummarysByList(orgID int64, scheduleDate int64) (treatmentSummarys []*VMTreatmentSummary, err error) {
  1333. redis := RedisClient()
  1334. defer redis.Close()
  1335. key := strconv.FormatInt(orgID, 10) + ":" + ":treatment_summarys_list_all"
  1336. treatment_summarys_all, _ := redis.Get(key).Result()
  1337. if len(treatment_summarys_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1338. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&treatmentSummarys).Error
  1339. if err != nil {
  1340. if err == gorm.ErrRecordNotFound {
  1341. if len(treatment_summarys_all) <= 0 {
  1342. redis.Set(key, "null", time.Second*60*60*18)
  1343. }
  1344. return nil, nil
  1345. } else {
  1346. return nil, err
  1347. }
  1348. } else {
  1349. if len(treatmentSummarys) > 0 {
  1350. //缓存数据
  1351. treatment_summarys_all, err := json.Marshal(&treatmentSummarys)
  1352. if err == nil {
  1353. redis.Set(key, treatment_summarys_all, time.Second*60*60*18)
  1354. }
  1355. } else {
  1356. redis.Set(key, "null", time.Second*60*60*18)
  1357. }
  1358. return treatmentSummarys, nil
  1359. }
  1360. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1361. if treatment_summarys_all == "null" {
  1362. return nil, nil
  1363. } else {
  1364. json.Unmarshal([]byte(treatment_summarys_all), &treatmentSummarys)
  1365. return treatmentSummarys, nil
  1366. }
  1367. }
  1368. }