dialysis_service.go 50KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  527. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  528. Preload("TreatmentSummary", "status = 1 AND user_org_id = ?", orgID).
  529. // Preload("DialysisOrder.MonitoringRecords", "status = 1 AND user_org_id = ?", orgID).
  530. Where("status = 1 AND user_org_id = ?", orgID)
  531. if scheduleDate != 0 {
  532. db = db.Where("schedule_date = ?", scheduleDate)
  533. }
  534. err := db.Find(&vms).Error
  535. return vms, err
  536. }
  537. func GetLastAcceptsAssessment(patient int64, orgId int64) (error, models.ReceiveTreatmentAsses) {
  538. var receiveTreatmentAsses models.ReceiveTreatmentAsses
  539. err := readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient, orgId).Last(&receiveTreatmentAsses).Error
  540. return err, receiveTreatmentAsses
  541. }
  542. func GetDialysisWatch(orgID int64, schedulDate int64, schedulType int64, partitionType int64, page int64, limit int64) (schedule []*models.DialysisSchedule, err error, total int64) {
  543. db := readDb.
  544. Model(&models.MonitorDialysisSchedule{}).
  545. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  546. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  547. Preload("TreatmentMode", "status = 1").
  548. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  549. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  550. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  551. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  552. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  553. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  554. db = db.Where("xt_schedule.status = 1 AND user_org_id = ?", orgID)
  555. if schedulDate > 0 {
  556. db = db.Where("schedule_date = ?", schedulDate)
  557. }
  558. if schedulType > 0 {
  559. db = db.Where("schedule_type = ?", schedulType)
  560. }
  561. if partitionType > 0 {
  562. 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)
  563. // db = db.Where("partition_id = ?", partitionType)
  564. }
  565. offset := (page - 1) * limit
  566. err = db.Count(&total).Offset(offset).Limit(limit).Order("bed_id desc").Find(&schedule).Error
  567. return schedule, err, total
  568. }
  569. func GetDialysisWatchByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64) ([]*models.DialysisSchedule, error, int64) {
  570. var patients []*models.Patients
  571. 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
  572. if getPatientErr != nil {
  573. return nil, getPatientErr, 0
  574. }
  575. patientIDs := make([]int64, len(patients))
  576. for index, patient := range patients {
  577. patientIDs[index] = patient.ID
  578. }
  579. db := readDb.
  580. Model(&models.DialysisSchedule{}).
  581. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  582. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  583. Preload("TreatmentMode", "status = 1").
  584. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  585. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  586. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  587. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  588. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  589. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  590. db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
  591. if schedulType > 0 {
  592. db = db.Where("schedule_type = ?", schedulType)
  593. }
  594. if partitionType > 0 {
  595. 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)
  596. // db = db.Where("partition_id = ?", partitionType)
  597. }
  598. var schedules []*models.DialysisSchedule
  599. total := int64(0)
  600. err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  601. return schedules, err, total
  602. }
  603. func GetAdminUserES(orgID int64, appID int64, ids []int64) (es []*models.AdminUserElectronicSignature, err error) {
  604. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id=? and creator in (?) and status=1", orgID, appID, ids).Find(&es).Error
  605. return
  606. }
  607. //
  608. //func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  609. //
  610. // db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  611. // table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  612. // fmt.Println("table", table)
  613. // 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
  614. // return admin, err
  615. //}
  616. func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  617. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status =1")
  618. table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  619. fmt.Println("table", table)
  620. 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
  621. return admin, err
  622. }
  623. func UpDateDialysisPrescriptionDoctorSign(patient int64, date int64, orgId int64, doctor_id int64) (err error) {
  624. 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
  625. return
  626. }
  627. func GetAllAdminUserES(orgID int64, appId int64) (es []*models.AdminUserElectronicSignature, err error) {
  628. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id = ? and status=1", orgID, appId).Find(&es).Error
  629. return
  630. }
  631. func GetAllStarfEs(orgid int64) (es []*models.SgjUserAdminRoles, err error) {
  632. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  633. table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  634. fmt.Println("table", table)
  635. 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
  636. return es, err
  637. }
  638. func FindLastMonitorRecord(patient_id int64, org_id int64) (record models.MonitoringRecord, err error) {
  639. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient_id, org_id).Last(&record).Error
  640. return
  641. }
  642. func FindLastMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  643. 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
  644. return
  645. }
  646. func FindFirstMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  647. 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
  648. return
  649. }
  650. func ExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  651. ut := writeDb.Begin()
  652. 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
  653. if err != nil {
  654. ut.Rollback()
  655. return
  656. }
  657. ut.Commit()
  658. return err
  659. }
  660. func ExceOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  661. ut := writeDb.Begin()
  662. 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
  663. if err != nil {
  664. ut.Rollback()
  665. return
  666. }
  667. 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
  668. if err != nil {
  669. ut.Rollback()
  670. return
  671. }
  672. ut.Commit()
  673. return err
  674. }
  675. func CheckDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  676. ut := writeDb.Begin()
  677. 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
  678. if err != nil {
  679. ut.Rollback()
  680. return
  681. }
  682. ut.Commit()
  683. return err
  684. }
  685. func CheckOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  686. ut := writeDb.Begin()
  687. 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
  688. if err != nil {
  689. ut.Rollback()
  690. return
  691. }
  692. 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
  693. if err != nil {
  694. ut.Rollback()
  695. return
  696. }
  697. ut.Commit()
  698. return err
  699. }
  700. func DeleteDoctorAdviceByGroupNo(m *models.DoctorAdvice) (err error) {
  701. ut := writeDb.Begin()
  702. 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
  703. if err != nil {
  704. ut.Rollback()
  705. return
  706. }
  707. ut.Commit()
  708. return
  709. }
  710. func GetDialysisBoard(orgID int64, scheduleDate int64, offset, limit int64) ([]*models.DialysisBoardVM, error) {
  711. var boards []*models.DialysisBoardVM
  712. db := readDb.
  713. Table("xt_schedule").
  714. Preload("Patient", "status = 1 AND user_org_id = ?", orgID).
  715. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  716. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  717. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  718. Preload("SigninAndWeigh", "status = 1 AND user_org_id = ?", orgID).
  719. Preload("NextSchedules", func(db *gorm.DB) *gorm.DB {
  720. return db.Where("user_org_id = ? AND schedule_date > ? AND status = 1", orgID, scheduleDate).
  721. Order("schedule_date asc, id asc")
  722. }).
  723. Where("status = 1 AND user_org_id = ?", orgID)
  724. if scheduleDate != 0 {
  725. db = db.Where("schedule_date = ?", scheduleDate)
  726. }
  727. err := db.Offset(offset).Limit(limit).Find(&boards).Error
  728. return boards, err
  729. }
  730. func GetOneDialysisOrder(orgID, orderData int64, patient_id int64) (*models.DialysisOrder, error) {
  731. var order models.DialysisOrder
  732. var err error
  733. 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
  734. if err == gorm.ErrRecordNotFound {
  735. return nil, nil
  736. }
  737. if err != nil {
  738. return nil, err
  739. }
  740. return &order, nil
  741. }
  742. func GetOrgInfoTemplate(orgID int64) (models.GobalTemplate, error) {
  743. var templateInfo models.GobalTemplate
  744. var err error
  745. err = readDb.Model(&models.GobalTemplate{}).Where("org_id=? and status=1", orgID).First(&templateInfo).Error
  746. return templateInfo, err
  747. }
  748. func FindAdminRoleTypeById(orgId int64, adminUserId int64, appId int64) (models.App_Role, error) {
  749. var appRole models.App_Role
  750. var err error
  751. 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
  752. return appRole, err
  753. }
  754. func BatchExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  755. ut := writeDb.Begin()
  756. 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
  757. if err != nil {
  758. ut.Rollback()
  759. return
  760. }
  761. 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
  762. if err != nil {
  763. ut.Rollback()
  764. return
  765. }
  766. ut.Commit()
  767. return err
  768. }
  769. func BatchCheckOldDoctorAdvice(m *models.DoctorAdvice, ids []string, org_id int64, date int64) (err error) {
  770. ut := writeDb.Begin()
  771. err = writeDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND id IN (?) AND user_org_id = ? AND advice_date = ?", ids, org_id, date).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  772. if err != nil {
  773. ut.Rollback()
  774. return
  775. }
  776. err = writeDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND parent_id IN (?) AND user_org_id = ? AND advice_date = ?", ids, org_id, date).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  777. if err != nil {
  778. ut.Rollback()
  779. return
  780. }
  781. ut.Commit()
  782. return err
  783. }
  784. func ModifyScheduleMode(mode_id int64, patient_id int64, recordDate int64, org_id int64) {
  785. ut := writeDb.Begin()
  786. 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
  787. if err != nil {
  788. ut.Rollback()
  789. return
  790. }
  791. ut.Commit()
  792. }
  793. func FindDialysisOrderById(id int64) (error, *models.DialysisOrder) {
  794. dialysisRecord := models.DialysisOrder{}
  795. err := readDb.Model(&models.DialysisOrder{}).Where("id = ? AND status = 1", id).Find(&dialysisRecord).Error
  796. return err, &dialysisRecord
  797. }
  798. func ModifyExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  799. ut := writeDb.Begin()
  800. 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
  801. if err != nil {
  802. ut.Rollback()
  803. return
  804. }
  805. ut.Commit()
  806. return err
  807. }
  808. func BatchModifyExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  809. ut := writeDb.Begin()
  810. 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
  811. if err != nil {
  812. ut.Rollback()
  813. return
  814. }
  815. 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
  816. if err != nil {
  817. ut.Rollback()
  818. return
  819. }
  820. ut.Commit()
  821. return err
  822. }
  823. func UpdateOrderCount(good_type_id int64, good_id int64, number string, org_id int64) {
  824. 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))
  825. }
  826. func UpdateOrderInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  827. 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})
  828. }
  829. func FindOrderInfoByGoodId(good_type_id int64, good_id int64, number string, org_id int64) (err error, out models.WarehouseOutInfo) {
  830. 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
  831. return
  832. }
  833. func FindGoodUserById(good_type_id int64, good_id int64, id int64, org_id int64, number string) (user models.AutomaticReduceDetail, err error) {
  834. 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
  835. return
  836. }
  837. func FindOldOrderInfoByGoodId(time int64, org_id int64) (err error, out models.WarehouseOutInfo) {
  838. err = readDb.Model(&models.WarehouseOutInfo{}).Where(" org_id = ? AND status = 1 AND sys_record_time = ? ", org_id, time).First(&out).Error
  839. return
  840. }
  841. func FindStockOutOrderNumberByTime(time int64, org_id int64) (err error, out models.WarehouseOut) {
  842. err = readDb.Model(&models.WarehouseOut{}).Where(" org_id = ? AND status = 1 AND warehouse_out_time = ? ", org_id, time).First(&out).Error
  843. return
  844. }
  845. func UpdateDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  846. 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})
  847. }
  848. func FindPatientIsOpenRemindById(patient_id int64, org_id int64) (patients models.Patients, err error) {
  849. err = readDb.Model(&models.Patients{}).Where("status = 1 AND id = ? AND user_org_id = ?", patient_id, org_id).First(&patients).Error
  850. return
  851. }
  852. func GetMaxLongAdviceGroupID(orgId int64, patient_id int64) (group int64) {
  853. var advice models.DoctorAdvice
  854. 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
  855. if err != nil {
  856. fmt.Println(err)
  857. group = 0
  858. }
  859. group = advice.GroupNo
  860. return
  861. }
  862. func GetLastLongAdviceByGroupNo(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  863. 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
  864. return
  865. }
  866. func GetLastLongAdviceByGroupNoThree(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  867. 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
  868. return
  869. }
  870. func FindAllDoctorAdviceByTime(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  871. 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
  872. return
  873. }
  874. func UpdateScheduleModeId(patient_id int64, org_id int64, date int64, mode_id int64) (err error) {
  875. 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
  876. return
  877. }
  878. func UpdateAssessmentAfterDate(patient_id int64, org_id int64, date int64, actual_treatment_hour int64, actual_treatment_minute int64) (err error) {
  879. 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
  880. return
  881. }