dialysis_service.go 50KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "time"
  6. "github.com/jinzhu/gorm"
  7. )
  8. //GetPatientList 返回患者的列表
  9. func GetSchedualPatientList(orgID int64, schedulDate int64, schedulType int64, partitionType int64, keywords string, page int64, limit int64) (schedule []*models.DialysisSchedule, err error) {
  10. var total int64
  11. //if len(keywords) > 0 {
  12. // likeKey := "%" + keywords + "%"
  13. // //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)
  14. // db = readDb.
  15. // Model(&models.MonitorDialysisSchedule{}).
  16. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  17. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  18. // Preload("TreatmentMode", "status = 1").
  19. // Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  20. // Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  21. // Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  22. // Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  23. // Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  24. // Preload("MonitorPatients", "status = 1 AND user_org_id = ? AND `name` LIKE ?", orgID,likeKey)
  25. //
  26. //
  27. //} else {
  28. //}
  29. db := readDb.
  30. Model(&models.MonitorDialysisSchedule{}).
  31. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  32. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  33. Preload("TreatmentMode", "status = 1").
  34. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  35. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  36. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  37. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  38. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  39. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  40. db = db.Where("status = 1")
  41. if schedulDate > 0 {
  42. db = db.Where("schedule_date = ?", schedulDate)
  43. }
  44. db = db.Count(&total)
  45. fmt.Println(total)
  46. if schedulType > 0 {
  47. db = db.Where("schedule_type = ?", schedulType)
  48. }
  49. if partitionType > 0 {
  50. db = db.Where("partition_id = ?", partitionType)
  51. }
  52. offset := (page - 1) * limit
  53. errors := db.Offset(offset).Limit(limit).Find(&schedule).Error
  54. return schedule, errors
  55. }
  56. //用户基本信息
  57. func FindPatientInfoById(orgID int64, id int64, time int64) (patient models.SchedualPatient2, err error) {
  58. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  59. 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)
  60. }).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  61. return
  62. }
  63. //透析处方
  64. func FindPatientPrescribeById(orgID int64, patientsId int64, recordDate int64) (patient models.DialysisPrescription, err error) {
  65. err = readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  66. return
  67. }
  68. //透析方案中的透析处方
  69. func FindDialysisSolution(orgID int64, patientsId int64) (patient models.DialysisSolution, err error) {
  70. err = readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id=? and status=1", patientsId, orgID).Last(&patient).Error
  71. return
  72. }
  73. //透析方案中的透析模式
  74. func FindTreatmentModeById(modeId int64) (mode models.TreatmentMode, err error) {
  75. err = readDb.Model(&models.TreatmentMode{}).Where("id = ? AND status=1", modeId).First(&mode).Error
  76. return
  77. }
  78. //接诊评估
  79. func FindReceiverTreatmentAccessRecordById(orgID int64, patientsId int64, recordDate int64) (receiveTreatmentAsses models.ReceiveTreatmentAsses, err error) {
  80. err = readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ?", patientsId, orgID, recordDate).First(&receiveTreatmentAsses).Error
  81. return
  82. }
  83. //透前评估
  84. func FindPredialysisEvaluationById(orgID int64, patientsId int64, recordDate int64) (patient models.PredialysisEvaluation, err error) {
  85. err = readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date = ?", patientsId, orgID, recordDate).First(&patient).Error
  86. return
  87. }
  88. func FindPredialysisName(id int64, orgid int64) (models.Dataconfig, error) {
  89. dataconfig := models.Dataconfig{}
  90. err := readDb.Where("value = ? and org_id =? and status = 1", id, orgid).Find(&dataconfig).Error
  91. return dataconfig, err
  92. }
  93. //临时医嘱
  94. func FindDoctorAdviceById(orgID int64, patientsId int64, recordDate int64) (patient []*models.DoctorAdvice, err error) {
  95. 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
  96. return
  97. }
  98. func FindDoctorAdviceByInfo(orgID int64, patientsId int64, recordDate int64) (advices []*models.XtDoctorAdvice, err error) {
  99. //db := readDb.Table("xt_doctor_advice as a").Where("a.status = ?", 1)
  100. //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
  101. //return
  102. err = readDb.Model(&models.DoctorAdvice{}).
  103. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? and advice_type = 2", patientsId, orgID, recordDate).
  104. 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").
  105. Find(&advices).Error
  106. return
  107. }
  108. func GetTotalDialysisCout(orgid int64, patientid int64) (order []*models.DialysisOrder, total int64, err error) {
  109. err = readDb.Model(&order).Where("user_org_id = ? and patient_id = ? and status = 1", orgid, patientid).Find(&order).Count(&total).Error
  110. return order, total, err
  111. }
  112. //临时医嘱
  113. func FindDoctorAdviceOrderById(orgID int64, patientsId int64, recordDate int64) (patient []*models.DoctorAdvice, err error) {
  114. err = readDb.Model(&models.DoctorAdvice{}).
  115. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? and advice_type = 2", patientsId, orgID, recordDate).
  116. 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").
  117. Find(&patient).Error
  118. return
  119. }
  120. //双人核对
  121. func FindDoubleCheckById(orgID int64, patientsId int64, recordDate int64) (patient models.DoubleCheck, err error) {
  122. err = readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? and user_org_id=? and status=1 and check_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  123. return
  124. }
  125. //透析监测
  126. func FindAllMonitorRecord(orgID int64, patientsId int64, recordDate int64) (record []*models.MonitoringRecord, err error) {
  127. 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
  128. return
  129. }
  130. func FindAllMonitor(orgID int64, patientsId int64, recordDate int64) (record []*models.MonitoringRecord, err error) {
  131. 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
  132. return
  133. }
  134. //透后评估
  135. func FindAssessmentAfterDislysisById(orgID int64, patientsId int64, recordDate int64) (patient models.AssessmentAfterDislysis, err error) {
  136. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date =?", patientsId, orgID, recordDate).First(&patient).Error
  137. return
  138. }
  139. //治疗小结
  140. func FindTreatmentSummaryById(orgID int64, patientsId int64, recordDate int64) (patient models.TreatmentSummary, err error) {
  141. err = readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  142. return
  143. }
  144. //所有设备信息
  145. func FindAllDeviceInfo(orgID int64) (device []*models.Device, err error) {
  146. err = readDb.Model(&models.Device{}).Where("org_id=? and status=1 and device_type = 1", orgID).Find(&device).Error
  147. return device, err
  148. }
  149. //所有设备区域
  150. func GetAllDeviceZone(orgId int64) (error, []*models.DeviceZone) {
  151. var zone []*models.DeviceZone
  152. err := readDb.Model(&zone).Where("org_id = ? AND status = 1", orgId).Find(&zone).Error
  153. return err, zone
  154. }
  155. //获取所有子药物
  156. func FindAllSubDoctorAdvice(id int64, parentId int64) (error, []models.DoctorAdvice) {
  157. var subAdvies []models.DoctorAdvice
  158. err := readDb.Model(&subAdvies).Where("patient_id = ? AND parent_id = ?", id, parentId).Find(&subAdvies).Error
  159. return err, subAdvies
  160. }
  161. func FindAdivceById(orgId int64, patientId int64, id int64) (error, models.DoctorAdvice) {
  162. advices := models.DoctorAdvice{}
  163. err := readDb.Model(&advices).Where("user_org_id = ? AND patient_id = ? AND id = ? AND status = 1", orgId, patientId, id).Find(&advices).Error
  164. return err, advices
  165. }
  166. func FindSubAdivceById(patientId int64, parentId int64) []*models.DoctorAdvice {
  167. var subAdvices []*models.DoctorAdvice
  168. readDb.Model(&subAdvices).Where("patient_id = ? AND parent_id = ?", patientId, parentId).Find(&subAdvices)
  169. return subAdvices
  170. }
  171. func AddSigleRecord(prescription *models.DialysisPrescription) (err error) {
  172. tx := writeDb.Begin()
  173. errors := tx.Model(&models.DialysisPrescription{}).Create(&prescription).Error
  174. if err != nil {
  175. tx.Rollback()
  176. }
  177. tx.Commit()
  178. return errors
  179. }
  180. func AddSigleSummaryRecord(summary *models.TreatmentSummary) (err error) {
  181. tx := writeDb.Begin()
  182. errors := tx.Model(&models.TreatmentSummary{}).Create(&summary).Error
  183. if err != nil {
  184. tx.Rollback()
  185. }
  186. tx.Commit()
  187. return errors
  188. }
  189. func AddSigleReceiveTreatmentAssesRecord(receiveTreatmentAsses *models.ReceiveTreatmentAsses) (err error) {
  190. tx := writeDb.Begin()
  191. errors := tx.Model(&models.ReceiveTreatmentAsses{}).Create(&receiveTreatmentAsses).Error
  192. if err != nil {
  193. tx.Rollback()
  194. }
  195. tx.Commit()
  196. return errors
  197. }
  198. func AddSigleAssessmentAfterDislysisRecord(assessmentAfterDislysis *models.AssessmentAfterDislysis) (err error) {
  199. tx := writeDb.Begin()
  200. errors := tx.Model(&models.AssessmentAfterDislysis{}).Create(&assessmentAfterDislysis).Error
  201. if err != nil {
  202. tx.Rollback()
  203. }
  204. tx.Commit()
  205. return errors
  206. }
  207. func AddSigleAssessmentBeforeDislysisRecord(assessmentBeforeDislysis *models.PredialysisEvaluation) (err error) {
  208. tx := writeDb.Begin()
  209. errors := tx.Model(&models.PredialysisEvaluation{}).Create(&assessmentBeforeDislysis).Error
  210. if err != nil {
  211. tx.Rollback()
  212. }
  213. tx.Commit()
  214. return errors
  215. }
  216. func AddSigleDoubleCheck(check *models.DoubleCheck) (err error) {
  217. tx := writeDb.Begin()
  218. errors := tx.Model(&models.PredialysisEvaluation{}).Create(&check).Error
  219. if err != nil {
  220. tx.Rollback()
  221. }
  222. tx.Commit()
  223. return errors
  224. }
  225. //修改透析处方
  226. func UpDateDialysisPrescription(dialysisPrescription *models.DialysisPrescription) error {
  227. err := writeDb.Save(&dialysisPrescription).Error
  228. return err
  229. }
  230. //修改接诊评估
  231. func UpadateReceiveTreatmentAsses(receiveTreatmentAsses *models.ReceiveTreatmentAsses) error {
  232. err := writeDb.Save(receiveTreatmentAsses).Error
  233. return err
  234. }
  235. //修改透前评估
  236. func UpadatePredialysisEvaluation(predialysisEvaluation *models.PredialysisEvaluation) error {
  237. err := writeDb.Save(&predialysisEvaluation).Error
  238. return err
  239. }
  240. //修改双人核对
  241. func UpdateDoubleCheck(check *models.DoubleCheck) error {
  242. err := writeDb.Save(&check).Error
  243. return err
  244. }
  245. //修改透后评估
  246. func UpdateAssessmentAfterDislysisRecord(assessmentAfterDislysis *models.AssessmentAfterDislysis) error {
  247. err := writeDb.Save(&assessmentAfterDislysis).Error
  248. return err
  249. }
  250. //修改
  251. func UpdateSummeRecord(treatmentSummary *models.TreatmentSummary) error {
  252. err := writeDb.Save(&treatmentSummary).Error
  253. return err
  254. }
  255. // func UpdateDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  256. // tx := writeDb.Begin()
  257. // 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
  258. // if err != nil {
  259. // tx.Rollback()
  260. // }
  261. // tx.Commit()
  262. // return err
  263. // }
  264. func ExceDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  265. 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
  266. return err
  267. }
  268. func CheckDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  269. 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
  270. return err
  271. }
  272. func CreateMonitor(m *models.MonitoringRecord) (err error) {
  273. return writeDb.Create(m).Error
  274. }
  275. func UpdateMonitor(m *models.MonitoringRecord) (err error) {
  276. return writeDb.Save(m).Error
  277. }
  278. func DelMonitor(patientId int64, recordId int64) error {
  279. tx := writeDb.Begin()
  280. updateTime := time.Now().Unix()
  281. 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
  282. if err != nil {
  283. tx.Rollback()
  284. }
  285. tx.Commit()
  286. return err
  287. }
  288. //修改长期处方
  289. func UpdateSolution(solution *models.DialysisSolution) {
  290. writeDb.Save(&solution)
  291. }
  292. //创建长期处方
  293. func CreateSolution(solution *models.DialysisSolution) {
  294. writeDb.Create(&solution)
  295. }
  296. func SavePrescriptionAndCreateSolution(solution *models.DialysisSolution, prescription *models.DialysisPrescription) (err error) {
  297. tx := writeDb.Begin()
  298. if prescription.ID > 0 {
  299. err = tx.Save(&prescription).Error
  300. } else {
  301. err = tx.Model(&models.DialysisPrescription{}).Create(&prescription).Error
  302. }
  303. if err != nil {
  304. tx.Rollback()
  305. return
  306. }
  307. err = tx.Create(&solution).Error
  308. if err != nil {
  309. tx.Rollback()
  310. return
  311. }
  312. tx.Commit()
  313. return
  314. }
  315. //透析处方
  316. func FindDialysisPrescriptionByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DialysisPrescription) {
  317. dialysisPrescription := models.DialysisPrescription{}
  318. err := readDb.Model(&dialysisPrescription).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ? ", id, recordDate, orgId).Find(&dialysisPrescription).Error
  319. return err, &dialysisPrescription
  320. }
  321. func FindReceiveTreatmentAssesByReordDate(id int64, recordDate int64, orgId int64) (error, *models.ReceiveTreatmentAsses) {
  322. receiveTreatmentAsses := models.ReceiveTreatmentAsses{}
  323. err := readDb.Model(&receiveTreatmentAsses).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).Find(&receiveTreatmentAsses).Error
  324. return err, &receiveTreatmentAsses
  325. }
  326. //透前评估
  327. func FindPredialysisEvaluationByReordDate(id int64, recordDate int64, orgId int64) (error, *models.PredialysisEvaluation) {
  328. dialysisEvaluation := models.PredialysisEvaluation{}
  329. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&dialysisEvaluation).Error
  330. return err, &dialysisEvaluation
  331. }
  332. //双人核对
  333. func FindDoubleCheckByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoubleCheck) {
  334. check := models.DoubleCheck{}
  335. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? AND check_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&check).Error
  336. return err, &check
  337. }
  338. func FindAssessmentAfterDislysisByReordDate(id int64, recordDate int64, orgId int64) (error, *models.AssessmentAfterDislysis) {
  339. assessmentAfterDislysis := models.AssessmentAfterDislysis{}
  340. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&assessmentAfterDislysis).Error
  341. return err, &assessmentAfterDislysis
  342. }
  343. func FindTreatmentSummaryByReordDate(id int64, recordDate int64, orgId int64) (error, *models.TreatmentSummary) {
  344. summary := models.TreatmentSummary{}
  345. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&summary).Error
  346. return err, &summary
  347. }
  348. func FindDoctorAdviceByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoctorAdvice) {
  349. advice := models.DoctorAdvice{}
  350. 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
  351. return err, &advice
  352. }
  353. func FindDialysisRecordById(orgId int64, id int64, recordDate int64) (error, *models.DialysisOrder) {
  354. var count int64
  355. dialysisRecord := models.DialysisOrder{}
  356. err := readDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgId, id, recordDate).Find(&dialysisRecord).Count(&count).Error
  357. if count <= 0 {
  358. return err, nil
  359. }
  360. return err, &dialysisRecord
  361. }
  362. func FindDialysisSchedualById(orgId int64, id int64, recordDate int64) (error, *models.Schedule) {
  363. var count int64
  364. schedule := models.Schedule{}
  365. err := readDb.Model(&models.Schedule{}).Where("user_org_id = ? AND patient_id = ? AND schedule_date = ?", orgId, id, recordDate).Find(&schedule).Count(&count).Error
  366. if count <= 0 {
  367. return err, nil
  368. }
  369. return err, &schedule
  370. }
  371. func CreateDialysisRecord(id int64, orgId int64, record *models.DialysisOrder) error {
  372. err := writeDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? ", orgId, id).Create(&record).Error
  373. if err != nil {
  374. return err
  375. }
  376. dialysisPrescriptionErr := UpDateRecordIdToDialysisPrescription(id, orgId, record.DialysisDate, record.ID)
  377. if dialysisPrescriptionErr != nil {
  378. return dialysisPrescriptionErr
  379. }
  380. receiveTreatmentAssesErr := UpDateRecordIdToReceiveTreatmentAsses(id, orgId, record.DialysisDate, record.ID)
  381. if receiveTreatmentAssesErr != nil {
  382. return receiveTreatmentAssesErr
  383. }
  384. predialysisEvaluationErr := UpDateRecordIdToPredialysisEvaluation(id, orgId, record.DialysisDate, record.ID)
  385. if predialysisEvaluationErr != nil {
  386. return predialysisEvaluationErr
  387. }
  388. treatmentSummaryErr := UpDateRecordIdToTreatmentSummary(id, orgId, record.DialysisDate, record.ID)
  389. if treatmentSummaryErr != nil {
  390. return treatmentSummaryErr
  391. }
  392. doubleCheckErr := UpDateRecordIdToDoubleCheck(id, orgId, record.DialysisDate, record.ID)
  393. if doubleCheckErr != nil {
  394. return doubleCheckErr
  395. }
  396. adviceErrr := UpDateRecordIdToDoctorAdvice(id, orgId, record.DialysisDate, record.ID)
  397. if adviceErrr != nil {
  398. return adviceErrr
  399. }
  400. //透析监测
  401. records, err := FindAllMonitorRecord(orgId, id, record.DialysisDate)
  402. if err != nil {
  403. return err
  404. }
  405. var Ids []int64
  406. for _, item := range records {
  407. Ids = append(Ids, item.ID)
  408. }
  409. monitorErr := UpDateRecordIdToMonitor(Ids, record.ID)
  410. if monitorErr != nil {
  411. return monitorErr
  412. }
  413. return nil
  414. }
  415. func ModifyDialysisRecord(id int64, finishNurse int64, end_time int64, admin_user_id int64) error {
  416. 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
  417. return err
  418. }
  419. func UpdateSolutionByPatientId(id int64) error {
  420. err := writeDb.Model(&models.Patients{}).Where("id = ? AND status = 1 ", id).UpdateColumn("total_dialysis", gorm.Expr("total_dialysis + ?", 1)).Error
  421. return err
  422. }
  423. //更新透析处方记录ID
  424. func UpDateRecordIdToDialysisPrescription(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  425. 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
  426. return errors
  427. }
  428. func UpDateRecordIdToReceiveTreatmentAsses(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  429. 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
  430. return errors
  431. }
  432. func UpDateRecordIdToPredialysisEvaluation(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  433. 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
  434. return errors
  435. }
  436. func UpDateRecordIdToAssessmentAfterDislysis(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  437. 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
  438. return errors
  439. }
  440. func UpDateRecordIdToTreatmentSummary(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  441. 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
  442. return errors
  443. }
  444. func UpDateRecordIdToMonitor(Ids []int64, recordId int64) error {
  445. errors := writeDb.Model(&models.MonitoringRecord{}).Where("id IN (?)", Ids).Update(map[string]interface{}{"dialysis_order_id": recordId}).Error
  446. return errors
  447. }
  448. func UpDateRecordIdToDoubleCheck(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  449. 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
  450. return errors
  451. }
  452. func UpDateRecordIdToDoctorAdvice(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  453. 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
  454. return errors
  455. }
  456. //透析处方
  457. func FindPatientPrescribeWidyDevideById(orgID int64, patientsId int64, recordDate int64) (patient models.DialysisPrescriptionAddtional, err error) {
  458. err = readDb.Preload("Device", "status = 1 AND org_id = ?", orgID).
  459. Model(&models.DialysisPrescriptionAddtional{}).
  460. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? ", patientsId, orgID, recordDate).
  461. First(&patient).Error
  462. return
  463. }
  464. func FindDialysisMode(orgID int64, patientId int64, scheduleDate int64) (models.XtSchedule, error) {
  465. schedule := models.XtSchedule{}
  466. err = readDb.Where("user_org_id = ? and patient_id = ? and schedule_date = ? ", orgID, patientId, scheduleDate).Find(&schedule).Error
  467. return schedule, err
  468. }
  469. func FindDialysisMacher(orgid int64, patientid int64, dialysisDate int64) (models.XtDialysisOrder, error) {
  470. orders := models.XtDialysisOrder{}
  471. db := readDb.Table("xt_dialysis_order as o").Where("o.status = 1")
  472. table := readDb.Table("xt_device_number as n")
  473. fmt.Print("table", table)
  474. 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
  475. return orders, err
  476. }
  477. func FindPunctureNurse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  478. role := models.App_Roles{}
  479. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  480. //return role, err
  481. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status =1")
  482. 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
  483. return role, err
  484. }
  485. func FindeStartNuse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  486. role := models.App_Roles{}
  487. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  488. 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
  489. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  490. return role, err
  491. }
  492. func FindDialysisWay(orgid int64, patientid int64, recorddate int64) (models.DialysisPrescription, error) {
  493. prescription := models.DialysisPrescription{}
  494. err := readDb.Where("user_org_id = ? and patient_id = ? and record_date = ? and status =1", orgid, patientid, recorddate).Find(&prescription).Error
  495. return prescription, err
  496. }
  497. func FindDoctorOrder(orgid int64, patientid int64, adviceDate int64) (models.XtDoctorAdvice, error) {
  498. advices := models.XtDoctorAdvice{}
  499. db := readDb.Table("xt_doctor_advice as a").Where("a.status = ?", 1)
  500. 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
  501. return advices, err
  502. }
  503. func FindDoctor(adminuserid int64, appid int64, orgid int64) (models.App_Role, error) {
  504. role := models.App_Role{}
  505. err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminuserid, appid, orgid).Find(&role).Error
  506. return role, err
  507. }
  508. func FindDoctorName(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  509. role := models.App_Roles{}
  510. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  511. 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
  512. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  513. return role, err
  514. }
  515. /////////////// 新 18-10-22
  516. func GetDialysisScheduals(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  517. var vms []*MDialysisScheduleVM
  518. db := readDb.
  519. Table("xt_schedule").
  520. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  521. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  522. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  523. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  524. Preload("TreatmentMode", "status = 1").
  525. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  526. // Preload("DialysisOrder.MonitoringRecords", "status = 1 AND user_org_id = ?", orgID).
  527. Where("status = 1 AND user_org_id = ?", orgID)
  528. if scheduleDate != 0 {
  529. db = db.Where("schedule_date = ?", scheduleDate)
  530. }
  531. err := db.Find(&vms).Error
  532. return vms, err
  533. }
  534. func GetLastAcceptsAssessment(patient int64, orgId int64) (error, models.ReceiveTreatmentAsses) {
  535. var receiveTreatmentAsses models.ReceiveTreatmentAsses
  536. err := readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient, orgId).Last(&receiveTreatmentAsses).Error
  537. return err, receiveTreatmentAsses
  538. }
  539. func GetDialysisWatch(orgID int64, schedulDate int64, schedulType int64, partitionType int64, page int64, limit int64) (schedule []*models.DialysisSchedule, err error, total int64) {
  540. db := readDb.
  541. Model(&models.MonitorDialysisSchedule{}).
  542. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  543. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  544. Preload("TreatmentMode", "status = 1").
  545. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  546. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  547. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  548. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  549. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  550. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  551. db = db.Where("xt_schedule.status = 1 AND user_org_id = ?", orgID)
  552. if schedulDate > 0 {
  553. db = db.Where("schedule_date = ?", schedulDate)
  554. }
  555. if schedulType > 0 {
  556. db = db.Where("schedule_type = ?", schedulType)
  557. }
  558. if partitionType > 0 {
  559. 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)
  560. // db = db.Where("partition_id = ?", partitionType)
  561. }
  562. offset := (page - 1) * limit
  563. err = db.Count(&total).Offset(offset).Limit(limit).Order("bed_id desc").Find(&schedule).Error
  564. return schedule, err, total
  565. }
  566. func GetDialysisWatchByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64) ([]*models.DialysisSchedule, error, int64) {
  567. var patients []*models.Patients
  568. 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
  569. if getPatientErr != nil {
  570. return nil, getPatientErr, 0
  571. }
  572. patientIDs := make([]int64, len(patients))
  573. for index, patient := range patients {
  574. patientIDs[index] = patient.ID
  575. }
  576. db := readDb.
  577. Model(&models.DialysisSchedule{}).
  578. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  579. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  580. Preload("TreatmentMode", "status = 1").
  581. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  582. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  583. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  584. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  585. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  586. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  587. db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
  588. if schedulType > 0 {
  589. db = db.Where("schedule_type = ?", schedulType)
  590. }
  591. if partitionType > 0 {
  592. 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)
  593. // db = db.Where("partition_id = ?", partitionType)
  594. }
  595. var schedules []*models.DialysisSchedule
  596. total := int64(0)
  597. err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  598. return schedules, err, total
  599. }
  600. func GetAdminUserES(orgID int64, appID int64, ids []int64) (es []*models.AdminUserElectronicSignature, err error) {
  601. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id=? and creator in (?) and status=1", orgID, appID, ids).Find(&es).Error
  602. return
  603. }
  604. //
  605. //func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  606. //
  607. // db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  608. // table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  609. // fmt.Println("table", table)
  610. // 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
  611. // return admin, err
  612. //}
  613. func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  614. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status =1")
  615. table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  616. fmt.Println("table", table)
  617. 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
  618. return admin, err
  619. }
  620. func UpDateDialysisPrescriptionDoctorSign(patient int64, date int64, orgId int64, doctor_id int64) (err error) {
  621. 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
  622. return
  623. }
  624. func GetAllAdminUserES(orgID int64, appId int64) (es []*models.AdminUserElectronicSignature, err error) {
  625. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id = ? and status=1", orgID, appId).Find(&es).Error
  626. return
  627. }
  628. func GetAllStarfEs(orgid int64) (es []*models.SgjUserAdminRoles, err error) {
  629. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  630. table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  631. fmt.Println("table", table)
  632. 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
  633. return es, err
  634. }
  635. func FindLastMonitorRecord(patient_id int64, org_id int64) (record models.MonitoringRecord, err error) {
  636. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient_id, org_id).Last(&record).Error
  637. return
  638. }
  639. func FindLastMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  640. 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
  641. return
  642. }
  643. func FindFirstMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  644. 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
  645. return
  646. }
  647. func ExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  648. ut := writeDb.Begin()
  649. 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
  650. if err != nil {
  651. ut.Rollback()
  652. return
  653. }
  654. ut.Commit()
  655. return err
  656. }
  657. func ExceOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  658. ut := writeDb.Begin()
  659. 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
  660. if err != nil {
  661. ut.Rollback()
  662. return
  663. }
  664. 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
  665. if err != nil {
  666. ut.Rollback()
  667. return
  668. }
  669. ut.Commit()
  670. return err
  671. }
  672. func CheckDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  673. ut := writeDb.Begin()
  674. 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
  675. if err != nil {
  676. ut.Rollback()
  677. return
  678. }
  679. ut.Commit()
  680. return err
  681. }
  682. func CheckOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  683. ut := writeDb.Begin()
  684. 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
  685. if err != nil {
  686. ut.Rollback()
  687. return
  688. }
  689. 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
  690. if err != nil {
  691. ut.Rollback()
  692. return
  693. }
  694. ut.Commit()
  695. return err
  696. }
  697. func DeleteDoctorAdviceByGroupNo(m *models.DoctorAdvice) (err error) {
  698. ut := writeDb.Begin()
  699. 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
  700. if err != nil {
  701. ut.Rollback()
  702. return
  703. }
  704. ut.Commit()
  705. return
  706. }
  707. func GetDialysisBoard(orgID int64, scheduleDate int64, offset, limit int64) ([]*models.DialysisBoardVM, error) {
  708. var boards []*models.DialysisBoardVM
  709. db := readDb.
  710. Table("xt_schedule").
  711. Preload("Patient", "status = 1 AND user_org_id = ?", orgID).
  712. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  713. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  714. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  715. Preload("SigninAndWeigh", "status = 1 AND user_org_id = ?", orgID).
  716. Preload("NextSchedules", func(db *gorm.DB) *gorm.DB {
  717. return db.Where("user_org_id = ? AND schedule_date > ? AND status = 1", orgID, scheduleDate).
  718. Order("schedule_date asc, id asc")
  719. }).
  720. Where("status = 1 AND user_org_id = ?", orgID)
  721. if scheduleDate != 0 {
  722. db = db.Where("schedule_date = ?", scheduleDate)
  723. }
  724. err := db.Offset(offset).Limit(limit).Find(&boards).Error
  725. return boards, err
  726. }
  727. func GetOneDialysisOrder(orgID, orderData int64, patient_id int64) (*models.DialysisOrder, error) {
  728. var order models.DialysisOrder
  729. var err error
  730. 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
  731. if err == gorm.ErrRecordNotFound {
  732. return nil, nil
  733. }
  734. if err != nil {
  735. return nil, err
  736. }
  737. return &order, nil
  738. }
  739. func GetOrgInfoTemplate(orgID int64) (models.GobalTemplate, error) {
  740. var templateInfo models.GobalTemplate
  741. var err error
  742. err = readDb.Model(&models.GobalTemplate{}).Where("org_id=? and status=1", orgID).First(&templateInfo).Error
  743. return templateInfo, err
  744. }
  745. func FindAdminRoleTypeById(orgId int64, adminUserId int64, appId int64) (models.App_Role, error) {
  746. var appRole models.App_Role
  747. var err error
  748. 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
  749. return appRole, err
  750. }
  751. func BatchExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  752. ut := writeDb.Begin()
  753. 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
  754. if err != nil {
  755. ut.Rollback()
  756. return
  757. }
  758. 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
  759. if err != nil {
  760. ut.Rollback()
  761. return
  762. }
  763. ut.Commit()
  764. return err
  765. }
  766. func BatchCheckOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  767. ut := writeDb.Begin()
  768. err = writeDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?)", ids).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  769. if err != nil {
  770. ut.Rollback()
  771. return
  772. }
  773. err = writeDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?)", ids).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 ModifyScheduleMode(mode_id int64, patient_id int64, recordDate int64, org_id int64) {
  782. ut := writeDb.Begin()
  783. 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
  784. if err != nil {
  785. ut.Rollback()
  786. return
  787. }
  788. ut.Commit()
  789. }
  790. func FindDialysisOrderById(id int64) (error, *models.DialysisOrder) {
  791. dialysisRecord := models.DialysisOrder{}
  792. err := readDb.Model(&models.DialysisOrder{}).Where("id = ? AND status = 1", id).Find(&dialysisRecord).Error
  793. return err, &dialysisRecord
  794. }
  795. func ModifyExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  796. ut := writeDb.Begin()
  797. 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
  798. if err != nil {
  799. ut.Rollback()
  800. return
  801. }
  802. ut.Commit()
  803. return err
  804. }
  805. func BatchModifyExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  806. ut := writeDb.Begin()
  807. 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
  808. if err != nil {
  809. ut.Rollback()
  810. return
  811. }
  812. 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
  813. if err != nil {
  814. ut.Rollback()
  815. return
  816. }
  817. ut.Commit()
  818. return err
  819. }
  820. func UpdateOrderCount(good_type_id int64, good_id int64, number string, org_id int64) {
  821. 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))
  822. }
  823. func UpdateOrderInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  824. 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})
  825. }
  826. func FindOrderInfoByGoodId(good_type_id int64, good_id int64, number string, org_id int64) (err error, out models.WarehouseOutInfo) {
  827. 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
  828. return
  829. }
  830. func FindGoodUserById(good_type_id int64, good_id int64, id int64, org_id int64, number string) (user models.AutomaticReduceDetail, err error) {
  831. err = readDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND good_type_id = ? AND patient_id = ? AND org_id = ? AND warehouse_out_order_number = ? ", good_id, good_type_id, id, org_id, number).First(&user).Error
  832. return
  833. }
  834. func FindOldOrderInfoByGoodId(time int64, org_id int64) (err error, out models.WarehouseOutInfo) {
  835. err = readDb.Model(&models.WarehouseOutInfo{}).Where(" org_id = ? AND status = 1 AND sys_record_time = ? ", org_id, time).First(&out).Error
  836. return
  837. }
  838. func FindStockOutOrderNumberByTime(time int64, org_id int64) (err error, out models.WarehouseOut) {
  839. err = readDb.Model(&models.WarehouseOut{}).Where(" org_id = ? AND status = 1 AND warehouse_out_time = ? ", org_id, time).First(&out).Error
  840. return
  841. }
  842. func UpdateDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  843. 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})
  844. }
  845. func FindPatientIsOpenRemindById(patient_id int64, org_id int64) (patients models.Patients, err error) {
  846. err = readDb.Model(&models.Patients{}).Where("status = 1 AND id = ? AND user_org_id = ?", patient_id, org_id).First(&patients).Error
  847. return
  848. }
  849. func GetMaxLongAdviceGroupID(orgId int64, patient_id int64) (group int64) {
  850. var advice models.DoctorAdvice
  851. 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
  852. if err != nil {
  853. fmt.Println(err)
  854. group = 0
  855. }
  856. group = advice.GroupNo
  857. return
  858. }
  859. func GetLastLongAdviceByGroupNo(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  860. 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", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  861. return
  862. }
  863. func GetLastLongAdviceByGroupNoThree(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  864. 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
  865. return
  866. }
  867. func FindAllDoctorAdviceByTime(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  868. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 2 AND frequency_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
  869. return
  870. }
  871. func UpdateScheduleModeId(patient_id int64, org_id int64, date int64, mode_id int64) (err error) {
  872. 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
  873. return
  874. }
  875. func UpdateAssessmentAfterDate(patient_id int64, org_id int64, date int64, actual_treatment_hour int64, actual_treatment_minute int64) (err error) {
  876. 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
  877. return
  878. }