dialysis_service.go 48KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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. func UpDateDialysisPrescriptionDoctorSign(patient int64, date int64, orgId int64, doctor_id int64) (err error) {
  605. 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
  606. return
  607. }
  608. func GetAllAdminUserES(orgID int64, appId int64) (es []*models.AdminUserElectronicSignature, err error) {
  609. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id = ? and status=1", orgID, appId).Find(&es).Error
  610. return
  611. }
  612. func FindLastMonitorRecord(patient_id int64, org_id int64) (record models.MonitoringRecord, err error) {
  613. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient_id, org_id).Last(&record).Error
  614. return
  615. }
  616. func FindLastMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  617. 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
  618. return
  619. }
  620. func FindFirstMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  621. 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
  622. return
  623. }
  624. func ExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  625. ut := writeDb.Begin()
  626. 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
  627. if err != nil {
  628. ut.Rollback()
  629. return
  630. }
  631. ut.Commit()
  632. return err
  633. }
  634. func ExceOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  635. ut := writeDb.Begin()
  636. 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
  637. if err != nil {
  638. ut.Rollback()
  639. return
  640. }
  641. 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
  642. if err != nil {
  643. ut.Rollback()
  644. return
  645. }
  646. ut.Commit()
  647. return err
  648. }
  649. func CheckDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  650. ut := writeDb.Begin()
  651. 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
  652. if err != nil {
  653. ut.Rollback()
  654. return
  655. }
  656. ut.Commit()
  657. return err
  658. }
  659. func CheckOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  660. ut := writeDb.Begin()
  661. 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
  662. if err != nil {
  663. ut.Rollback()
  664. return
  665. }
  666. 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
  667. if err != nil {
  668. ut.Rollback()
  669. return
  670. }
  671. ut.Commit()
  672. return err
  673. }
  674. func DeleteDoctorAdviceByGroupNo(m *models.DoctorAdvice) (err error) {
  675. ut := writeDb.Begin()
  676. err = ut.Model(&models.DoctorAdvice{}).Where("groupno = ?", m.GroupNo).Update(map[string]interface{}{"UpdatedTime": m.UpdatedTime, "Status": 0}).Error
  677. if err != nil {
  678. ut.Rollback()
  679. return
  680. }
  681. ut.Commit()
  682. return
  683. }
  684. func GetDialysisBoard(orgID int64, scheduleDate int64, offset, limit int64) ([]*models.DialysisBoardVM, error) {
  685. var boards []*models.DialysisBoardVM
  686. db := readDb.
  687. Table("xt_schedule").
  688. Preload("Patient", "status = 1 AND user_org_id = ?", orgID).
  689. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  690. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  691. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  692. Preload("SigninAndWeigh", "status = 1 AND user_org_id = ?", orgID).
  693. Preload("NextSchedules", func(db *gorm.DB) *gorm.DB {
  694. return db.Where("user_org_id = ? AND schedule_date > ? AND status = 1", orgID, scheduleDate).
  695. Order("schedule_date asc, id asc")
  696. }).
  697. Where("status = 1 AND user_org_id = ?", orgID)
  698. if scheduleDate != 0 {
  699. db = db.Where("schedule_date = ?", scheduleDate)
  700. }
  701. err := db.Offset(offset).Limit(limit).Find(&boards).Error
  702. return boards, err
  703. }
  704. func GetOneDialysisOrder(orgID, orderData int64, patient_id int64) (*models.DialysisOrder, error) {
  705. var order models.DialysisOrder
  706. var err error
  707. 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
  708. if err == gorm.ErrRecordNotFound {
  709. return nil, nil
  710. }
  711. if err != nil {
  712. return nil, err
  713. }
  714. return &order, nil
  715. }
  716. func GetOrgInfoTemplate(orgID int64) (models.GobalTemplate, error) {
  717. var templateInfo models.GobalTemplate
  718. var err error
  719. err = readDb.Model(&models.GobalTemplate{}).Where("org_id=? and status=1", orgID).First(&templateInfo).Error
  720. return templateInfo, err
  721. }
  722. func FindAdminRoleTypeById(orgId int64, adminUserId int64, appId int64) (models.App_Role, error) {
  723. var appRole models.App_Role
  724. var err error
  725. 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
  726. return appRole, err
  727. }
  728. func BatchExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  729. ut := writeDb.Begin()
  730. 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
  731. if err != nil {
  732. ut.Rollback()
  733. return
  734. }
  735. 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
  736. if err != nil {
  737. ut.Rollback()
  738. return
  739. }
  740. ut.Commit()
  741. return err
  742. }
  743. func BatchCheckOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  744. ut := writeDb.Begin()
  745. 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
  746. if err != nil {
  747. ut.Rollback()
  748. return
  749. }
  750. 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
  751. if err != nil {
  752. ut.Rollback()
  753. return
  754. }
  755. ut.Commit()
  756. return err
  757. }
  758. func ModifyScheduleMode(mode_id int64, patient_id int64, recordDate int64, org_id int64) {
  759. ut := writeDb.Begin()
  760. 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
  761. if err != nil {
  762. ut.Rollback()
  763. return
  764. }
  765. ut.Commit()
  766. }
  767. func FindDialysisOrderById(id int64) (error, *models.DialysisOrder) {
  768. dialysisRecord := models.DialysisOrder{}
  769. err := readDb.Model(&models.DialysisOrder{}).Where("id = ? AND status = 1", id).Find(&dialysisRecord).Error
  770. return err, &dialysisRecord
  771. }
  772. func ModifyExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  773. ut := writeDb.Begin()
  774. 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
  775. if err != nil {
  776. ut.Rollback()
  777. return
  778. }
  779. ut.Commit()
  780. return err
  781. }
  782. func BatchModifyExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  783. ut := writeDb.Begin()
  784. 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
  785. if err != nil {
  786. ut.Rollback()
  787. return
  788. }
  789. 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
  790. if err != nil {
  791. ut.Rollback()
  792. return
  793. }
  794. ut.Commit()
  795. return err
  796. }
  797. func UpdateOrderCount(good_type_id int64, good_id int64, number string, org_id int64) {
  798. 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))
  799. }
  800. func UpdateOrderInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  801. 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})
  802. }
  803. func FindOrderInfoByGoodId(good_type_id int64, good_id int64, number string, org_id int64) (err error, out models.WarehouseOutInfo) {
  804. 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
  805. return
  806. }
  807. func FindGoodUserById(good_type_id int64, good_id int64, id int64, org_id int64, number string) (user models.AutomaticReduceDetail, err error) {
  808. 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
  809. return
  810. }
  811. func FindOldOrderInfoByGoodId(time int64, org_id int64) (err error, out models.WarehouseOutInfo) {
  812. err = readDb.Model(&models.WarehouseOutInfo{}).Where(" org_id = ? AND status = 1 AND sys_record_time = ? ", org_id, time).First(&out).Error
  813. return
  814. }
  815. func FindStockOutOrderNumberByTime(time int64, org_id int64) (err error, out models.WarehouseOut) {
  816. err = readDb.Model(&models.WarehouseOut{}).Where(" org_id = ? AND status = 1 AND warehouse_out_time = ? ", org_id, time).First(&out).Error
  817. return
  818. }
  819. func UpdateDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  820. 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})
  821. }
  822. func FindPatientIsOpenRemindById(patient_id int64, org_id int64) (patients models.Patients, err error) {
  823. err = readDb.Model(&models.Patients{}).Where("status = 1 AND id = ? AND user_org_id = ?", patient_id, org_id).First(&patients).Error
  824. return
  825. }
  826. func GetMaxLongAdviceGroupID(orgId int64, patient_id int64) (group int64) {
  827. var advice models.DoctorAdvice
  828. 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
  829. if err != nil {
  830. fmt.Println(err)
  831. group = 0
  832. }
  833. group = advice.GroupNo
  834. return
  835. }
  836. func GetLastLongAdviceByGroupNo(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  837. 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
  838. return
  839. }
  840. func GetLastLongAdviceByGroupNoThree(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  841. 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
  842. return
  843. }
  844. func FindAllDoctorAdviceByTime(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  845. 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
  846. return
  847. }
  848. func UpdateScheduleModeId(patient_id int64, org_id int64, date int64, mode_id int64) (err error) {
  849. 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
  850. return
  851. }
  852. func UpdateAssessmentAfterDate(patient_id int64, org_id int64, date int64, actual_treatment_hour int64, actual_treatment_minute int64) (err error) {
  853. 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
  854. return
  855. }