dialysis_service.go 56KB

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