dialysis_service.go 82KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  1. package service
  2. import (
  3. "XT_New/models"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/jinzhu/gorm"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. //GetPatientList 返回患者的列表
  12. func GetSchedualPatientList(orgID int64, schedulDate int64, schedulType int64, partitionType int64, keywords string, page int64, limit int64) (schedule []*models.DialysisSchedule, err error) {
  13. var total int64
  14. //if len(keywords) > 0 {
  15. // likeKey := "%" + keywords + "%"
  16. // //db = db.Preload("MonitorPatients", "status = 1 AND user_org_id = ? AND `name` LIKE ? ", orgID, likeKey).Preload("DeviceNumber", "status = 1 AND org_id = ? ", orgID).Preload("PredialysisEvaluation", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, schedulDate).Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, schedulDate).Preload("MonitoringRecord", "status = 1 AND user_org_id = ? AND monitoring_date = ?", orgID, schedulDate).Where("status = 1 AND user_org_id = ? ", orgID)
  17. // db = readDb.
  18. // Model(&models.MonitorDialysisSchedule{}).
  19. // Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  20. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  21. // Preload("TreatmentMode", "status = 1").
  22. // Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  23. // Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  24. // Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  25. // Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  26. // Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  27. // Preload("MonitorPatients", "status = 1 AND user_org_id = ? AND `name` LIKE ?", orgID,likeKey)
  28. //
  29. //
  30. //} else {
  31. //}
  32. db := readDb.
  33. Model(&models.MonitorDialysisSchedule{}).
  34. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  35. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  36. Preload("TreatmentMode", "status = 1").
  37. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, schedulDate).
  38. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
  39. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, schedulDate).
  40. Preload("MonitoringRecord", "status = 1 AND user_org_id = ? AND monitoring_date = ?", orgID, schedulDate).
  41. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, schedulDate).
  42. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  43. db = db.Where("status = 1")
  44. if schedulDate > 0 {
  45. db = db.Where("schedule_date = ?", schedulDate)
  46. }
  47. db = db.Count(&total)
  48. fmt.Println(total)
  49. if schedulType > 0 {
  50. db = db.Where("schedule_type = ?", schedulType)
  51. }
  52. if partitionType > 0 {
  53. db = db.Where("partition_id = ?", partitionType)
  54. }
  55. offset := (page - 1) * limit
  56. errors := db.Offset(offset).Limit(limit).Find(&schedule).Error
  57. return schedule, errors
  58. }
  59. //用户基本信息
  60. func FindPatientInfoById(orgID int64, id int64, time int64) (patient models.SchedualPatient2, err error) {
  61. err = readDb.Preload("DialysisSchedule", func(db *gorm.DB) *gorm.DB {
  62. return db.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).Where("patient_id = ? AND user_org_id = ? AND schedule_date = ? ", id, orgID, time)
  63. }).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&patient).Error
  64. return
  65. }
  66. //透析处方
  67. func FindPatientPrescribeById(orgID int64, patientsId int64, recordDate int64) (patient models.DialysisPrescription, err error) {
  68. err = readDb.Model(&models.DialysisPrescription{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  69. return
  70. }
  71. //透析方案中的透析处方
  72. func FindDialysisSolution(orgID int64, patientsId int64) (patient models.DialysisSolution, err error) {
  73. err = readDb.Model(&models.DialysisSolution{}).Where("patient_id = ? and user_org_id=? and status=1", patientsId, orgID).Last(&patient).Error
  74. return
  75. }
  76. //透析方案中的透析模式
  77. func FindTreatmentModeById(modeId int64) (mode models.TreatmentMode, err error) {
  78. err = readDb.Model(&models.TreatmentMode{}).Where("id = ? AND status=1", modeId).First(&mode).Error
  79. return
  80. }
  81. //接诊评估
  82. func FindReceiverTreatmentAccessRecordById(orgID int64, patientsId int64, recordDate int64) (receiveTreatmentAsses models.ReceiveTreatmentAsses, err error) {
  83. err = readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ?", patientsId, orgID, recordDate).First(&receiveTreatmentAsses).Error
  84. return
  85. }
  86. //透前评估
  87. func FindPredialysisEvaluationById(orgID int64, patientsId int64, recordDate int64) (patient models.PredialysisEvaluation, err error) {
  88. err = readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date = ?", patientsId, orgID, recordDate).First(&patient).Error
  89. return
  90. }
  91. func FindPredialysisName(id int64, orgid int64) (models.Dataconfig, error) {
  92. dataconfig := models.Dataconfig{}
  93. err := readDb.Where("value = ? and org_id =? and status = 1", id, orgid).Find(&dataconfig).Error
  94. return dataconfig, err
  95. }
  96. //临时医嘱
  97. func FindDoctorAdviceById(orgID int64, patientsId int64, recordDate int64) (patient []*models.DoctorAdvice, err error) {
  98. err = readDb.Model(&models.DoctorAdvice{}).Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? AND advice_type = 2", patientsId, orgID, recordDate).Find(&patient).Error
  99. return
  100. }
  101. func FindDoctorAdviceByInfo(orgID int64, patientsId int64, recordDate int64) (advices []*models.XtDoctorAdvice, err error) {
  102. //db := readDb.Table("xt_doctor_advice as a").Where("a.status = ?", 1)
  103. //err = db.Group("a.advice_name").Select("a.id,a.user_org_id,a.patient_id,a.advice_type,a.advice_date,a.start_time,a.advice_name,a.advice_desc,a.reminder_date,a.single_dose,a.single_dose_unit,a.prescribing_number,a.prescribing_number_unit,a.delivery_way,a.execution_frequency,a.advice_doctor,a.status,a.created_time,a.updated_time,a.advice_affirm,a.remark,a.stop_time,a.stop_reason,a.stop_doctor,a.stop_state,a.parent_id,a.execution_time,a.execution_staff,a.execution_state,a.checker,a.record_date,a.dialysis_order_id,a.check_time,a.check_state,a.drug_spec,a.drug_spec_unit,a.groupno,a.remind_type,a.frequency_type,a.day_count,a.week_day,a.template_id,r.user_name,s.user_name").Joins(" left join sgj_users.sgj_user_admin_role as r on r.admin_user_id = a.execution_staff").Joins("left join sgj_users.sgj_user_admin_role as s on s.admin_user_id = a.checker").Where("a.user_org_id = ? and a.patient_id = ? and a.advice_date = ?", orgID, patientsId, recordDate).Find(&advices).Error
  104. //return
  105. err = readDb.Model(&models.DoctorAdvice{}).
  106. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? and advice_type = 2", patientsId, orgID, recordDate).
  107. Select("id, user_org_id, patient_id, advice_type, advice_date, record_date, start_time, advice_name,advice_desc, reminder_date, drug_spec, drug_spec_unit, single_dose, single_dose_unit, prescribing_number, prescribing_number_unit, delivery_way, execution_frequency, advice_doctor, status, created_time,updated_time, advice_affirm, remark, stop_time, stop_reason, stop_doctor, stop_state, parent_id, execution_time, execution_staff, execution_state, checker, check_state, check_time, groupno, IF(parent_id > 0, parent_id, id) as advice_order").Order("start_time asc, groupno desc, advice_order desc, id asc").
  108. Find(&advices).Error
  109. return
  110. }
  111. func GetTotalDialysisCout(orgid int64, patientid int64) (order []*models.DialysisOrder, total int64, err error) {
  112. err = readDb.Model(&order).Where("user_org_id = ? and patient_id = ? and status = 1", orgid, patientid).Find(&order).Count(&total).Error
  113. return order, total, err
  114. }
  115. //临时医嘱
  116. func FindDoctorAdviceOrderById(orgID int64, patientsId int64, recordDate int64) (patient []*models.DoctorAdvice, err error) {
  117. err = readDb.Model(&models.DoctorAdvice{}).
  118. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? and advice_type = 2", patientsId, orgID, recordDate).
  119. Select("id, user_org_id, patient_id, advice_type, advice_date, record_date, start_time, advice_name,advice_desc, reminder_date, drug_spec, drug_spec_unit, single_dose, single_dose_unit, prescribing_number, prescribing_number_unit, delivery_way, execution_frequency, advice_doctor, status, created_time,updated_time, advice_affirm, remark, stop_time, stop_reason, stop_doctor, stop_state, parent_id, execution_time, execution_staff, execution_state, checker, check_state, check_time, groupno, IF(parent_id > 0, parent_id, id) as advice_order").Order("start_time asc, groupno desc, advice_order desc, id asc").
  120. Find(&patient).Error
  121. return
  122. }
  123. //双人核对
  124. func FindDoubleCheckById(orgID int64, patientsId int64, recordDate int64) (patient models.DoubleCheck, err error) {
  125. err = readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? and user_org_id=? and status=1 and check_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  126. return
  127. }
  128. //透析监测
  129. func FindAllMonitorRecord(orgID int64, patientsId int64, recordDate int64) (record []*models.MonitoringRecord, err error) {
  130. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id=? and status=1 and monitoring_date = ?", patientsId, orgID, recordDate).Order("operate_time ASC").Find(&record).Error
  131. return
  132. }
  133. func FindAllMonitor(orgID int64, patientsId int64, recordDate int64) (record []*models.MonitoringRecord, err error) {
  134. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? and user_org_id=? and status=1 and monitoring_date = ?", patientsId, orgID, recordDate).Order("operate_time ASC").Find(&record).Error
  135. return
  136. }
  137. //透后评估
  138. func FindAssessmentAfterDislysisById(orgID int64, patientsId int64, recordDate int64) (patient models.AssessmentAfterDislysis, err error) {
  139. err = readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date =?", patientsId, orgID, recordDate).First(&patient).Error
  140. return
  141. }
  142. //治疗小结
  143. func FindTreatmentSummaryById(orgID int64, patientsId int64, recordDate int64) (patient models.TreatmentSummary, err error) {
  144. err = readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? and user_org_id=? and status=1 and assessment_date = ? ", patientsId, orgID, recordDate).First(&patient).Error
  145. return
  146. }
  147. //机构叫号配置
  148. func FindQueueCallInfo(orgID int64) (queueConfig models.QueueCallConfig, err error) {
  149. err = readDb.Model(&models.QueueCallConfig{}).Where("org_id=? ", orgID).First(&queueConfig).Error
  150. return
  151. }
  152. //修改机构叫号配置
  153. func UpDateQueueCallInfo(queueConfig models.QueueCallConfig) error {
  154. err := writeDb.Save(&queueConfig).Error
  155. return err
  156. }
  157. //所有设备信息
  158. func FindAllDeviceInfo(orgID int64) (device []*models.Device, err error) {
  159. err = readDb.Model(&models.Device{}).Where("org_id=? and status=1 and device_type = 1", orgID).Find(&device).Error
  160. return device, err
  161. }
  162. //所有设备区域
  163. func GetAllDeviceZone(orgId int64) (error, []*models.DeviceZone) {
  164. var zone []*models.DeviceZone
  165. err := readDb.Model(&zone).Where("org_id = ? AND status = 1", orgId).Find(&zone).Error
  166. return err, zone
  167. }
  168. //获取所有子药物
  169. func FindAllSubDoctorAdvice(id int64, parentId int64) (error, []models.DoctorAdvice) {
  170. var subAdvies []models.DoctorAdvice
  171. err := readDb.Model(&subAdvies).Where("patient_id = ? AND parent_id = ?", id, parentId).Find(&subAdvies).Error
  172. return err, subAdvies
  173. }
  174. func FindAdivceById(orgId int64, patientId int64, id int64) (error, models.DoctorAdvice) {
  175. advices := models.DoctorAdvice{}
  176. err := readDb.Model(&advices).Where("user_org_id = ? AND patient_id = ? AND id = ? AND status = 1", orgId, patientId, id).Find(&advices).Error
  177. return err, advices
  178. }
  179. func FindSubAdivceById(patientId int64, parentId int64) []*models.DoctorAdvice {
  180. var subAdvices []*models.DoctorAdvice
  181. readDb.Model(&subAdvices).Where("patient_id = ? AND parent_id = ?", patientId, parentId).Find(&subAdvices)
  182. return subAdvices
  183. }
  184. func AddSigleRecord(prescription *models.DialysisPrescription) (err error) {
  185. tx := writeDb.Begin()
  186. errors := tx.Model(&models.DialysisPrescription{}).Create(&prescription).Error
  187. if err != nil {
  188. tx.Rollback()
  189. }
  190. tx.Commit()
  191. return errors
  192. }
  193. func AddSigleSummaryRecord(summary *models.TreatmentSummary) (err error) {
  194. tx := writeDb.Begin()
  195. errors := tx.Model(&models.TreatmentSummary{}).Create(&summary).Error
  196. if err != nil {
  197. tx.Rollback()
  198. }
  199. tx.Commit()
  200. return errors
  201. }
  202. func AddSigleReceiveTreatmentAssesRecord(receiveTreatmentAsses *models.ReceiveTreatmentAsses) (err error) {
  203. tx := writeDb.Begin()
  204. errors := tx.Model(&models.ReceiveTreatmentAsses{}).Create(&receiveTreatmentAsses).Error
  205. if err != nil {
  206. tx.Rollback()
  207. }
  208. tx.Commit()
  209. return errors
  210. }
  211. func AddSigleAssessmentAfterDislysisRecord(assessmentAfterDislysis *models.AssessmentAfterDislysis) (err error) {
  212. tx := writeDb.Begin()
  213. errors := tx.Model(&models.AssessmentAfterDislysis{}).Create(&assessmentAfterDislysis).Error
  214. if err != nil {
  215. tx.Rollback()
  216. }
  217. tx.Commit()
  218. return errors
  219. }
  220. func AddSigleAssessmentBeforeDislysisRecord(assessmentBeforeDislysis *models.PredialysisEvaluation) (err error) {
  221. tx := writeDb.Begin()
  222. errors := tx.Model(&models.PredialysisEvaluation{}).Create(&assessmentBeforeDislysis).Error
  223. if err != nil {
  224. tx.Rollback()
  225. }
  226. tx.Commit()
  227. return errors
  228. }
  229. func AddSigleDoubleCheck(check *models.DoubleCheck) (err error) {
  230. tx := writeDb.Begin()
  231. errors := tx.Model(&models.DoubleCheck{}).Create(&check).Error
  232. if err != nil {
  233. tx.Rollback()
  234. }
  235. tx.Commit()
  236. return errors
  237. }
  238. //修改透析处方
  239. func UpDateDialysisPrescription(dialysisPrescription *models.DialysisPrescription) error {
  240. tx := writeDb.Begin()
  241. err := writeDb.Save(&dialysisPrescription).Error
  242. if err != nil {
  243. tx.Rollback()
  244. }
  245. tx.Commit()
  246. return err
  247. }
  248. //修改接诊评估
  249. func UpadateReceiveTreatmentAsses(receiveTreatmentAsses *models.ReceiveTreatmentAsses) error {
  250. err := writeDb.Save(receiveTreatmentAsses).Error
  251. return err
  252. }
  253. //修改透前评估
  254. func UpadatePredialysisEvaluation(predialysisEvaluation *models.PredialysisEvaluation) error {
  255. err := writeDb.Save(&predialysisEvaluation).Error
  256. return err
  257. }
  258. //修改双人核对
  259. func UpdateDoubleCheck(check *models.DoubleCheck) error {
  260. err := writeDb.Save(&check).Error
  261. return err
  262. }
  263. //修改透后评估
  264. func UpdateAssessmentAfterDislysisRecord(assessmentAfterDislysis *models.AssessmentAfterDislysis) error {
  265. err := writeDb.Save(&assessmentAfterDislysis).Error
  266. return err
  267. }
  268. func UpdatePredialysisEvaluation(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  269. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"systolic_blood_pressure": predialysisevaluation.SystolicBloodPressure}).Error
  270. return err
  271. }
  272. func UpdatePredialysisEvaluationTwo(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  273. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"diastolic_blood_pressure": predialysisevaluation.DiastolicBloodPressure}).Error
  274. return err
  275. }
  276. func UpdatePredialysisEvaluationThree(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  277. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"pulse_frequency": predialysisevaluation.PulseFrequency}).Error
  278. return err
  279. }
  280. func UpdatePredialysisEvaluationFour(predialysisevaluation *models.PredialysisEvaluation, id int64) error {
  281. err := writeDb.Model(&predialysisevaluation).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"temperature": predialysisevaluation.Temperature}).Error
  282. return err
  283. }
  284. //修改
  285. func UpdateSummeRecord(treatmentSummary *models.TreatmentSummary) error {
  286. err := writeDb.Save(&treatmentSummary).Error
  287. return err
  288. }
  289. // func UpdateDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  290. // tx := writeDb.Begin()
  291. // 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
  292. // if err != nil {
  293. // tx.Rollback()
  294. // }
  295. // tx.Commit()
  296. // return err
  297. // }
  298. func ExceDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  299. 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
  300. return err
  301. }
  302. func ExceHidDoctorAdviceById(m *models.HisDoctorAdviceInfo, id int64, patientId int64) error {
  303. err := writeDb.Model(&models.HisDoctorAdviceInfo{}).Where("patient_id = ? AND (id = ? Or parent_id=?) AND status = 1 ", patientId, id, id).Updates(map[string]interface{}{"execution_time": m.ExecutionTime, "execution_staff": m.ExecutionStaff, "execution_state": 1}).Error
  304. return err
  305. }
  306. func CheckDoctorAdviceById(m *models.DoctorAdvice, id int64, patientId int64) error {
  307. 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
  308. return err
  309. }
  310. func CheckHisDoctorAdviceById(m *models.HisDoctorAdviceInfo, id int64, patientId int64) error {
  311. err := writeDb.Model(&models.HisDoctorAdviceInfo{}).Where("patient_id = ? AND (id = ? Or parent_id=?) AND status = 1 ", patientId, id, id).Updates(map[string]interface{}{"check_time": m.CheckTime, "checker": m.Checker, "check_state": 1}).Error
  312. return err
  313. }
  314. func CreateMonitor(m *models.MonitoringRecord) (err error) {
  315. return writeDb.Create(m).Error
  316. }
  317. func UpdateMonitor(m *models.MonitoringRecord) (err error) {
  318. return writeDb.Save(m).Error
  319. }
  320. func DelMonitor(patientId int64, recordId int64) error {
  321. tx := writeDb.Begin()
  322. updateTime := time.Now().Unix()
  323. 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
  324. if err != nil {
  325. tx.Rollback()
  326. }
  327. tx.Commit()
  328. return err
  329. }
  330. //修改长期处方
  331. func UpdateSolution(solution *models.DialysisSolution) {
  332. writeDb.Save(&solution)
  333. }
  334. //创建长期处方
  335. func CreateSolution(solution *models.DialysisSolution) {
  336. writeDb.Create(&solution)
  337. }
  338. func SavePrescriptionAndCreateSolution(solution *models.DialysisSolution, prescription *models.DialysisPrescription) (err error) {
  339. tx := writeDb.Begin()
  340. if prescription.ID > 0 {
  341. err = tx.Save(&prescription).Error
  342. } else {
  343. err = tx.Model(&models.DialysisPrescription{}).Create(&prescription).Error
  344. }
  345. if err != nil {
  346. tx.Rollback()
  347. return
  348. }
  349. err = tx.Create(&solution).Error
  350. if err != nil {
  351. tx.Rollback()
  352. return
  353. }
  354. tx.Commit()
  355. return
  356. }
  357. //透析处方
  358. func FindDialysisPrescriptionByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DialysisPrescription) {
  359. dialysisPrescription := models.DialysisPrescription{}
  360. err := readDb.Model(&dialysisPrescription).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ? ", id, recordDate, orgId).Find(&dialysisPrescription).Error
  361. return err, &dialysisPrescription
  362. }
  363. func FindReceiveTreatmentAssesByReordDate(id int64, recordDate int64, orgId int64) (error, *models.ReceiveTreatmentAsses) {
  364. receiveTreatmentAsses := models.ReceiveTreatmentAsses{}
  365. err := readDb.Model(&receiveTreatmentAsses).Where("patient_id = ? AND record_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).Find(&receiveTreatmentAsses).Error
  366. return err, &receiveTreatmentAsses
  367. }
  368. //透前评估
  369. func FindPredialysisEvaluationByReordDate(id int64, recordDate int64, orgId int64) (error, *models.PredialysisEvaluation) {
  370. dialysisEvaluation := models.PredialysisEvaluation{}
  371. err := readDb.Model(&models.PredialysisEvaluation{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&dialysisEvaluation).Error
  372. return err, &dialysisEvaluation
  373. }
  374. //双人核对
  375. func FindDoubleCheckByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoubleCheck) {
  376. check := models.DoubleCheck{}
  377. err := readDb.Model(&models.DoubleCheck{}).Where("patient_id = ? AND check_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&check).Error
  378. return err, &check
  379. }
  380. func FindAssessmentAfterDislysisByReordDate(id int64, recordDate int64, orgId int64) (error, *models.AssessmentAfterDislysis) {
  381. assessmentAfterDislysis := models.AssessmentAfterDislysis{}
  382. err := readDb.Model(&models.AssessmentAfterDislysis{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&assessmentAfterDislysis).Error
  383. return err, &assessmentAfterDislysis
  384. }
  385. func FindTreatmentSummaryByReordDate(id int64, recordDate int64, orgId int64) (error, *models.TreatmentSummary) {
  386. summary := models.TreatmentSummary{}
  387. err := readDb.Model(&models.TreatmentSummary{}).Where("patient_id = ? AND assessment_date = ? AND status = 1 AND user_org_id = ?", id, recordDate, orgId).First(&summary).Error
  388. return err, &summary
  389. }
  390. func FindDoctorAdviceByReordDate(id int64, recordDate int64, orgId int64) (error, *models.DoctorAdvice) {
  391. advice := models.DoctorAdvice{}
  392. 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
  393. return err, &advice
  394. }
  395. func FindDialysisRecordById(orgId int64, id int64, recordDate int64) (error, *models.DialysisOrder) {
  396. var count int64
  397. dialysisRecord := models.DialysisOrder{}
  398. err := readDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? AND dialysis_date = ?", orgId, id, recordDate).Find(&dialysisRecord).Count(&count).Error
  399. if count <= 0 {
  400. return err, nil
  401. }
  402. return err, &dialysisRecord
  403. }
  404. func FindDialysisSchedualById(orgId int64, id int64, recordDate int64) (error, *models.Schedule) {
  405. var count int64
  406. schedule := models.Schedule{}
  407. err := readDb.Model(&models.Schedule{}).Where("user_org_id = ? AND patient_id = ? AND schedule_date = ?", orgId, id, recordDate).Find(&schedule).Count(&count).Error
  408. if count <= 0 {
  409. return err, nil
  410. }
  411. return err, &schedule
  412. }
  413. func CreateDialysisRecord(id int64, orgId int64, record *models.DialysisOrder) error {
  414. err := writeDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND patient_id = ? ", orgId, id).Create(&record).Error
  415. if err != nil {
  416. return err
  417. }
  418. dialysisPrescriptionErr := UpDateRecordIdToDialysisPrescription(id, orgId, record.DialysisDate, record.ID)
  419. if dialysisPrescriptionErr != nil {
  420. return dialysisPrescriptionErr
  421. }
  422. receiveTreatmentAssesErr := UpDateRecordIdToReceiveTreatmentAsses(id, orgId, record.DialysisDate, record.ID)
  423. if receiveTreatmentAssesErr != nil {
  424. return receiveTreatmentAssesErr
  425. }
  426. predialysisEvaluationErr := UpDateRecordIdToPredialysisEvaluation(id, orgId, record.DialysisDate, record.ID)
  427. if predialysisEvaluationErr != nil {
  428. return predialysisEvaluationErr
  429. }
  430. treatmentSummaryErr := UpDateRecordIdToTreatmentSummary(id, orgId, record.DialysisDate, record.ID)
  431. if treatmentSummaryErr != nil {
  432. return treatmentSummaryErr
  433. }
  434. doubleCheckErr := UpDateRecordIdToDoubleCheck(id, orgId, record.DialysisDate, record.ID)
  435. if doubleCheckErr != nil {
  436. return doubleCheckErr
  437. }
  438. adviceErrr := UpDateRecordIdToDoctorAdvice(id, orgId, record.DialysisDate, record.ID)
  439. if adviceErrr != nil {
  440. return adviceErrr
  441. }
  442. //透析监测
  443. records, err := FindAllMonitorRecord(orgId, id, record.DialysisDate)
  444. if err != nil {
  445. return err
  446. }
  447. var Ids []int64
  448. for _, item := range records {
  449. Ids = append(Ids, item.ID)
  450. }
  451. monitorErr := UpDateRecordIdToMonitor(Ids, record.ID)
  452. if monitorErr != nil {
  453. return monitorErr
  454. }
  455. return nil
  456. }
  457. func ModifyDialysisRecord(id int64, finishNurse int64, end_time int64, admin_user_id int64, puncture_point_haematoma int64, internal_fistula string, catheter string, cruor string) error {
  458. 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, "puncture_point_haematoma": puncture_point_haematoma, "blood_access_internal_fistula": internal_fistula, "catheter": catheter, "cruor": cruor}).Error
  459. return err
  460. }
  461. //func ModifyDialysisRecord(id int64, finishNurse int64, end_time int64, admin_user_id int64) error {
  462. // 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
  463. // return err
  464. //
  465. //}
  466. func UpdateSolutionByPatientId(id int64) error {
  467. err := writeDb.Model(&models.Patients{}).Where("id = ? AND status = 1 ", id).UpdateColumn("total_dialysis", gorm.Expr("total_dialysis + ?", 1)).Error
  468. return err
  469. }
  470. //更新透析处方记录ID
  471. func UpDateRecordIdToDialysisPrescription(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  472. 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
  473. return errors
  474. }
  475. func UpDateRecordIdToReceiveTreatmentAsses(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  476. 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
  477. return errors
  478. }
  479. func UpDateRecordIdToPredialysisEvaluation(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  480. 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
  481. return errors
  482. }
  483. func UpDateRecordIdToAssessmentAfterDislysis(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  484. 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
  485. return errors
  486. }
  487. func UpDateRecordIdToTreatmentSummary(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  488. 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
  489. return errors
  490. }
  491. func UpDateRecordIdToMonitor(Ids []int64, recordId int64) error {
  492. errors := writeDb.Model(&models.MonitoringRecord{}).Where("id IN (?)", Ids).Update(map[string]interface{}{"dialysis_order_id": recordId}).Error
  493. return errors
  494. }
  495. func UpDateRecordIdToDoubleCheck(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  496. 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
  497. return errors
  498. }
  499. func UpDateRecordIdToDoctorAdvice(patientId int64, orgId int64, recordTime int64, recordId int64) error {
  500. 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
  501. return errors
  502. }
  503. //透析处方
  504. func FindPatientPrescribeWidyDevideById(orgID int64, patientsId int64, recordDate int64) (patient models.DialysisPrescriptionAddtional, err error) {
  505. err = readDb.Preload("Device", "status = 1 AND org_id = ?", orgID).
  506. Model(&models.DialysisPrescriptionAddtional{}).
  507. Where("patient_id = ? and user_org_id=? and status=1 and record_date = ? ", patientsId, orgID, recordDate).
  508. First(&patient).Error
  509. return
  510. }
  511. func FindDialysisMode(orgID int64, patientId int64, scheduleDate int64) (models.XtSchedule, error) {
  512. schedule := models.XtSchedule{}
  513. err = readDb.Where("user_org_id = ? and patient_id = ? and schedule_date = ? ", orgID, patientId, scheduleDate).Find(&schedule).Error
  514. return schedule, err
  515. }
  516. func FindDialysisMacher(orgid int64, patientid int64, dialysisDate int64) (models.XtDialysisOrders, error) {
  517. orders := models.XtDialysisOrders{}
  518. db := readDb.Table("xt_dialysis_order as o").Where("o.status = 1")
  519. table := readDb.Table("xt_device_number as n")
  520. fmt.Print("table", table)
  521. 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
  522. return orders, err
  523. }
  524. func FindPunctureNurse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  525. role := models.App_Roles{}
  526. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  527. //return role, err
  528. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status =1")
  529. 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
  530. return role, err
  531. }
  532. func FindeStartNuse(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  533. role := models.App_Roles{}
  534. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  535. 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
  536. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  537. return role, err
  538. }
  539. func FindDialysisWay(orgid int64, patientid int64, recorddate int64) (models.DialysisPrescription, error) {
  540. prescription := models.DialysisPrescription{}
  541. err := readDb.Where("user_org_id = ? and patient_id = ? and record_date = ? and status =1", orgid, patientid, recorddate).Find(&prescription).Error
  542. return prescription, err
  543. }
  544. func FindDoctorOrder(orgid int64, patientid int64, adviceDate int64) (models.XtDoctorAdvice, error) {
  545. advices := models.XtDoctorAdvice{}
  546. db := readDb.Table("xt_doctor_advice as a").Where("a.status = ?", 1)
  547. 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
  548. return advices, err
  549. }
  550. func FindDoctor(adminuserid int64, appid int64, orgid int64) (models.App_Role, error) {
  551. role := models.App_Role{}
  552. err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminuserid, appid, orgid).Find(&role).Error
  553. return role, err
  554. }
  555. func FindDoctorName(adminid int64, appid int64, orgid int64) (models.App_Roles, error) {
  556. role := models.App_Roles{}
  557. db := UserReadDB().Table("sgj_user_admin_role as r").Where("r.status = 1")
  558. 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
  559. //err := UserReadDB().Where("admin_user_id = ? and app_id = ? and org_id = ?", adminid, appid, orgid).Find(&role).Error
  560. return role, err
  561. }
  562. /////////////// 新 18-10-22
  563. func GetDialysisScheduals(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  564. var vms []*MDialysisScheduleVM
  565. db := readDb.
  566. Table("xt_schedule").
  567. Preload("SchedualPatient", "status = 1 AND user_org_id = ?", orgID).
  568. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  569. Preload("DeviceNumber.Zone", "status = 1 AND org_id = ?", orgID).
  570. Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).
  571. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  572. Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  573. Preload("TreatmentSummary", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  574. Preload("Advices", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  575. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  576. Where("status = 1 AND user_org_id = ?", orgID)
  577. if scheduleDate != 0 {
  578. db = db.Where("schedule_date = ?", scheduleDate)
  579. }
  580. err := db.Find(&vms).Error
  581. return vms, err
  582. }
  583. func GetDialysisSchedualsOne(orgID int64, scheduleDate int64) ([]*MDialysisScheduleVM, error) {
  584. var vms []*MDialysisScheduleVM
  585. db := readDb.
  586. Table("xt_schedule").
  587. Preload("DeviceNumber", "org_id = ?", orgID).
  588. Preload("DeviceNumber.Zone", "org_id = ?", orgID).
  589. //Preload("DialysisOrder", "status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).
  590. //Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).
  591. //Preload("Prescription", "status = 1 AND user_org_id = ? AND record_date = ?", orgID, scheduleDate).
  592. //Preload("TreatmentSummary", "status = 1 AND user_org_id = ? AND assessment_date = ? ", orgID, scheduleDate).
  593. //Preload("Advices", "status = 1 AND user_org_id = ? AND advice_date = ? ", orgID, scheduleDate).
  594. //Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date = ?", orgID, scheduleDate).
  595. Where("status = 1 AND user_org_id = ?", orgID)
  596. if scheduleDate != 0 {
  597. db = db.Where("schedule_date = ?", scheduleDate)
  598. }
  599. err := db.Find(&vms).Error
  600. return vms, err
  601. }
  602. func GetLastAcceptsAssessment(patient int64, orgId int64) (error, models.ReceiveTreatmentAsses) {
  603. var receiveTreatmentAsses models.ReceiveTreatmentAsses
  604. err := readDb.Model(&models.ReceiveTreatmentAsses{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient, orgId).Last(&receiveTreatmentAsses).Error
  605. return err, receiveTreatmentAsses
  606. }
  607. func GetDialysisWatch(orgID int64, schedulDate int64, schedulType int64, partitionType int64, page int64, limit int64, start int64, end int64) (schedule []*models.DialysisSchedule, err error, total int64) {
  608. db := readDb.Model(&models.MonitorDialysisSchedule{})
  609. if start > 0 && end > 0 {
  610. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  611. Preload("TreatmentMode", "status = 1").
  612. Preload("Prescription", "status = 1 AND user_org_id = ? and record_date>=? and record_date <=?", orgID, start, end).
  613. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date >=? and assessment_date <=?", orgID, start, end).
  614. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date >=? and assessment_date <=?", orgID, start, end).
  615. Preload("MonitoringRecord", func(db *gorm.DB) *gorm.DB {
  616. return db.Where("status = 1 AND user_org_id = ? and monitoring_date >=? and monitoring_date<=? ", orgID, start, end).Order("operate_time asc")
  617. }).
  618. Preload("DialysisOrder", "status = 1 AND user_org_id = ? and dialysis_date>=? and dialysis_date<=?", orgID, start, end).
  619. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  620. db = db.Where("xt_schedule.status = 1 AND user_org_id = ?", orgID)
  621. }
  622. if schedulDate > 0 {
  623. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  624. Preload("TreatmentMode", "status = 1").
  625. Preload("Prescription", "status = 1 AND user_org_id = ? and record_date>=? and record_date <=?", orgID, schedulDate, schedulDate).
  626. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date >=? and assessment_date <=?", orgID, schedulDate, schedulDate).
  627. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date >=? and assessment_date <=?", orgID, schedulDate, schedulDate).
  628. Preload("MonitoringRecord", func(db *gorm.DB) *gorm.DB {
  629. return db.Where("status = 1 AND user_org_id = ? and monitoring_date >=? and monitoring_date<=? ", orgID, schedulDate, schedulDate).Order("operate_time asc")
  630. }).
  631. Preload("DialysisOrder", "status = 1 AND user_org_id = ? and dialysis_date>=? and dialysis_date<=?", orgID, schedulDate, schedulDate).
  632. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  633. db = db.Where("xt_schedule.status = 1 AND user_org_id = ?", orgID)
  634. db = db.Where("schedule_date = ?", schedulDate)
  635. }
  636. if schedulType > 0 {
  637. db = db.Where("schedule_type = ?", schedulType)
  638. }
  639. if start > 0 {
  640. db = db.Where("schedule_date >= ?", start)
  641. }
  642. if end > 0 {
  643. db = db.Where("schedule_date <= ?", end)
  644. }
  645. if partitionType > 0 {
  646. db = db.Joins("inner join xt_device_number on xt_device_number.id = xt_schedule.bed_id and xt_device_number.zone_id = ?", partitionType)
  647. // db = db.Where("partition_id = ?", partitionType)
  648. }
  649. offset := (page - 1) * limit
  650. err = db.Count(&total).Offset(offset).Limit(limit).Order("bed_id desc").Find(&schedule).Error
  651. return schedule, err, total
  652. }
  653. //func GetDialysisWatchByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64) (schedules []*models.DialysisSchedule, err error, total int64) {
  654. // db := readDb.Model(&models.DialysisSchedule{}).Where("status = 1")
  655. //
  656. // if schedulType > 0 {
  657. // db = db.Where("scheudle_type = ?", schedulType)
  658. // }
  659. //
  660. // if partitionType > 0 {
  661. // db = db.Where("partition_id = ?", partitionType)
  662. // }
  663. //
  664. // err = db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  665. // return schedules, err, total
  666. //}
  667. func GetDialysisWatchByKeyword(orgID int64, keyword string, schedulType int64, partitionType int64, page int64, limit int64, start int64, end int64) ([]*models.DialysisSchedule, error, int64) {
  668. var patients []*models.Patients
  669. 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
  670. if getPatientErr != nil {
  671. return nil, getPatientErr, 0
  672. }
  673. patientIDs := make([]int64, len(patients))
  674. for index, patient := range patients {
  675. patientIDs[index] = patient.ID
  676. }
  677. db := readDb.Model(&models.DialysisSchedule{})
  678. if start > 0 && end > 0 {
  679. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  680. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  681. Preload("TreatmentMode", "status = 1").
  682. Preload("Prescription", "status = 1 AND user_org_id = ? and record_date>=? and record_date<=?", orgID, start, end).
  683. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ? and assessment_date>=? and assessment_date<=?", orgID, start, end).
  684. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ? and assessment_date>=? and assessment_date<=?", orgID, start, end).
  685. Preload("MonitoringRecord", "status = 1 AND user_org_id = ? and monitoring_date>=? and monitoring_date<=?", orgID, start, end).
  686. Preload("DialysisOrder", "status = 1 AND user_org_id = ? and dialysis_date >= ? and dialysis_date<=?", orgID, start, end).
  687. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  688. } else {
  689. db = db.Preload("DeviceNumber", "org_id = ?", orgID).
  690. // Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  691. Preload("TreatmentMode", "status = 1").
  692. Preload("Prescription", "status = 1 AND user_org_id = ?", orgID).
  693. Preload("AssessmentBeforeDislysis", "status = 1 AND user_org_id = ?", orgID).
  694. Preload("AssessmentAfterDislysis", "status = 1 AND user_org_id = ?", orgID).
  695. Preload("MonitoringRecord", "status = 1 AND user_org_id = ?", orgID).
  696. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  697. Preload("MonitorPatients", "status = 1 AND user_org_id = ?", orgID)
  698. }
  699. db = db.Where("xt_schedule.status = 1 AND patient_id in (?)", patientIDs)
  700. if schedulType > 0 {
  701. db = db.Where("schedule_type = ?", schedulType)
  702. }
  703. if start > 0 {
  704. db = db.Where("schedule_date >= ?", start)
  705. }
  706. if end > 0 {
  707. db = db.Where("schedule_date<=?", end)
  708. }
  709. if partitionType > 0 {
  710. db = db.Joins("inner join xt_device_number as d_n on d_n.id = xt_schedule.bed_id and d_n.zone_id = ?", partitionType)
  711. // db = db.Where("partition_id = ?", partitionType)
  712. }
  713. var schedules []*models.DialysisSchedule
  714. total := int64(0)
  715. err := db.Count(&total).Offset(limit * (page - 1)).Limit(limit).Order("schedule_date desc").Find(&schedules).Error
  716. return schedules, err, total
  717. }
  718. func GetAdminUserES(orgID int64, appID int64, ids []int64) (es []*models.AdminUserElectronicSignature, err error) {
  719. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id=? and creator in (?) and status=1", orgID, appID, ids).Find(&es).Error
  720. return
  721. }
  722. //
  723. //func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  724. //
  725. // db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  726. // table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  727. // fmt.Println("table", table)
  728. // 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
  729. // return admin, err
  730. //}
  731. func GetAdminUserEsOne(orgid int64) (admin []*models.UserAdminRoles, err error) {
  732. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status =1")
  733. table := readUserDb.Table("sgj_user_admin_electronic_signature as s")
  734. fmt.Println("table", table)
  735. 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
  736. return admin, err
  737. }
  738. func UpDateDialysisPrescriptionDoctorSign(patient int64, date int64, orgId int64, doctor_id int64) (err error) {
  739. 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
  740. return
  741. }
  742. func GetAllAdminUserES(orgID int64, appId int64) (es []*models.AdminUserElectronicSignature, err error) {
  743. err = readUserDb.Model(&models.AdminUserElectronicSignature{}).Where("org_id=? and app_id = ? and status=1", orgID, appId).Find(&es).Error
  744. return
  745. }
  746. func GetAllStarfEs(orgid int64) (es []*models.SgjUserAdminRoles, err error) {
  747. redis := RedisClient()
  748. defer redis.Close()
  749. // cur_date := time.Now().Format("2006-01-02")
  750. key := strconv.FormatInt(orgid, 10) + ":starfes"
  751. starfes_str, _ := redis.Get(key).Result()
  752. if len(starfes_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  753. db := readUserDb.Table("sgj_user_admin_role as x").Where("x.status = 1")
  754. 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
  755. if err != nil {
  756. if err == gorm.ErrRecordNotFound {
  757. if len(es) <= 0 {
  758. redis.Set(key, "starfes_str_null", time.Second*60*60*18)
  759. }
  760. return nil, nil
  761. } else {
  762. return nil, err
  763. }
  764. } else {
  765. if len(es) > 0 {
  766. //缓存数据
  767. starfes_str, err := json.Marshal(es)
  768. if err == nil {
  769. redis.Set(key, starfes_str, time.Second*60*60*18)
  770. return es, nil
  771. }
  772. } else {
  773. redis.Set(key, "starfes_str_null", time.Second*60*60*18)
  774. return es, nil
  775. }
  776. return es, nil
  777. }
  778. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  779. if starfes_str == "starfes_str_null" {
  780. return es, nil
  781. } else {
  782. json.Unmarshal([]byte(starfes_str), &es)
  783. return es, nil
  784. }
  785. }
  786. }
  787. func FindLastMonitorRecord(patient_id int64, org_id int64) (record models.MonitoringRecord, err error) {
  788. err = readDb.Model(&models.MonitoringRecord{}).Where("patient_id = ? AND status = 1 AND user_org_id = ?", patient_id, org_id).Last(&record).Error
  789. return
  790. }
  791. func FindLastMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  792. 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
  793. return
  794. }
  795. func FindFirstMonitorRecordToday(patient_id int64, org_id int64, monitoring_date int64) (record models.MonitoringRecord, err error) {
  796. 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
  797. return
  798. }
  799. func ExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  800. ut := writeDb.Begin()
  801. 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
  802. if err != nil {
  803. ut.Rollback()
  804. return
  805. }
  806. ut.Commit()
  807. return err
  808. }
  809. func ExceOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  810. ut := writeDb.Begin()
  811. 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
  812. if err != nil {
  813. ut.Rollback()
  814. return
  815. }
  816. 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
  817. if err != nil {
  818. ut.Rollback()
  819. return
  820. }
  821. ut.Commit()
  822. return err
  823. }
  824. func CheckDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  825. ut := writeDb.Begin()
  826. 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
  827. if err != nil {
  828. ut.Rollback()
  829. return
  830. }
  831. ut.Commit()
  832. return err
  833. }
  834. func CheckOldDoctorAdvice(m *models.DoctorAdvice, advice_id int64) (err error) {
  835. ut := writeDb.Begin()
  836. 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
  837. if err != nil {
  838. ut.Rollback()
  839. return
  840. }
  841. 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
  842. if err != nil {
  843. ut.Rollback()
  844. return
  845. }
  846. ut.Commit()
  847. return err
  848. }
  849. func DeleteDoctorAdviceByGroupNo(m *models.DoctorAdvice) (err error) {
  850. ut := writeDb.Begin()
  851. 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
  852. if err != nil {
  853. ut.Rollback()
  854. return
  855. }
  856. ut.Commit()
  857. return
  858. }
  859. func GetDialysisBoard(orgID int64, scheduleDate int64, offset, limit int64) ([]*models.DialysisBoardVM, error) {
  860. var boards []*models.DialysisBoardVM
  861. db := readDb.
  862. Table("xt_schedule").
  863. Preload("Patient", "status = 1 AND user_org_id = ?", orgID).
  864. Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).
  865. Preload("DeviceZone", "status = 1 AND org_id = ?", orgID).
  866. Preload("DialysisOrder", "status = 1 AND user_org_id = ?", orgID).
  867. Preload("SigninAndWeigh", "status = 1 AND user_org_id = ?", orgID).
  868. Preload("NextSchedules", func(db *gorm.DB) *gorm.DB {
  869. return db.Where("user_org_id = ? AND schedule_date > ? AND status = 1", orgID, scheduleDate).
  870. Order("schedule_date asc, id asc")
  871. }).
  872. Where("status = 1 AND user_org_id = ?", orgID)
  873. if scheduleDate != 0 {
  874. db = db.Where("schedule_date = ?", scheduleDate)
  875. }
  876. err := db.Offset(offset).Limit(limit).Find(&boards).Error
  877. return boards, err
  878. }
  879. func GetOneDialysisOrder(orgID, orderData int64, patient_id int64) (*models.DialysisOrder, error) {
  880. var order models.DialysisOrder
  881. var err error
  882. 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
  883. if err == gorm.ErrRecordNotFound {
  884. return nil, nil
  885. }
  886. if err != nil {
  887. return nil, err
  888. }
  889. return &order, nil
  890. }
  891. func GetOrgInfoTemplate(orgID int64) (models.GobalTemplate, error) {
  892. var templateInfo models.GobalTemplate
  893. var err error
  894. err = readDb.Model(&models.GobalTemplate{}).Where("org_id=? and status=1", orgID).First(&templateInfo).Error
  895. return templateInfo, err
  896. }
  897. func FindAdminRoleTypeById(orgId int64, adminUserId int64, appId int64) (models.App_Role, error) {
  898. var appRole models.App_Role
  899. var err error
  900. 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
  901. return appRole, err
  902. }
  903. func BatchExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  904. ut := writeDb.Begin()
  905. 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
  906. if err != nil {
  907. ut.Rollback()
  908. return
  909. }
  910. 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
  911. if err != nil {
  912. ut.Rollback()
  913. return
  914. }
  915. ut.Commit()
  916. return err
  917. }
  918. func BatchCheckOldDoctorAdvice(m *models.DoctorAdvice, ids []string, org_id int64, date int64) (err error) {
  919. ut := writeDb.Begin()
  920. 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
  921. if err != nil {
  922. ut.Rollback()
  923. return
  924. }
  925. 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
  926. if err != nil {
  927. ut.Rollback()
  928. return
  929. }
  930. ut.Commit()
  931. return err
  932. }
  933. func ModifyScheduleMode(mode_id int64, patient_id int64, recordDate int64, org_id int64) {
  934. ut := writeDb.Begin()
  935. 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
  936. if err != nil {
  937. ut.Rollback()
  938. return
  939. }
  940. ut.Commit()
  941. }
  942. func FindDialysisOrderById(id int64) (error, *models.DialysisOrder) {
  943. dialysisRecord := models.DialysisOrder{}
  944. err := readDb.Model(&models.DialysisOrder{}).Where("id = ? AND status = 1", id).Find(&dialysisRecord).Error
  945. return err, &dialysisRecord
  946. }
  947. func ModifyExceDoctorAdviceByGroupNo(m *models.DoctorAdvice, groupNo int64, org_id int64) (err error) {
  948. ut := writeDb.Begin()
  949. 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
  950. if err != nil {
  951. ut.Rollback()
  952. return
  953. }
  954. ut.Commit()
  955. return err
  956. }
  957. func BatchModifyExceOldDoctorAdvice(m *models.DoctorAdvice, ids []string) (err error) {
  958. ut := writeDb.Begin()
  959. 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
  960. if err != nil {
  961. ut.Rollback()
  962. return
  963. }
  964. 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
  965. if err != nil {
  966. ut.Rollback()
  967. return
  968. }
  969. ut.Commit()
  970. return err
  971. }
  972. func UpdateOrderCount(good_type_id int64, good_id int64, number string, org_id int64) {
  973. 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))
  974. }
  975. func UpdateOrderInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  976. 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})
  977. }
  978. func FindOrderInfoByGoodId(good_type_id int64, good_id int64, number string, org_id int64) (err error, out models.WarehouseOutInfo) {
  979. 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
  980. return
  981. }
  982. func FindGoodUserById(good_type_id int64, good_id int64, id int64, org_id int64, record_time int64) (user models.AutomaticReduceDetail, err error) {
  983. 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
  984. return
  985. }
  986. func FindOldOrderInfoByGoodId(time int64, org_id int64) (err error, out models.WarehouseOutInfo) {
  987. err = readDb.Model(&models.WarehouseOutInfo{}).Where(" org_id = ? AND status = 1 AND sys_record_time = ? ", org_id, time).First(&out).Error
  988. return
  989. }
  990. func FindStockOutOrderNumberByTime(time int64, org_id int64) (err error, out models.WarehouseOut) {
  991. err = readDb.Model(&models.WarehouseOut{}).Where(" org_id = ? AND status = 1 AND warehouse_out_time = ? ", org_id, time).First(&out).Error
  992. return
  993. }
  994. func UpdateDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, newNiprocart int64, out *models.WarehouseOutInfo) {
  995. 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})
  996. }
  997. func FindPatientIsOpenRemindById(patient_id int64, org_id int64) (patients models.Patients, err error) {
  998. err = readDb.Model(&models.Patients{}).Where("status = 1 AND id = ? AND user_org_id = ?", patient_id, org_id).First(&patients).Error
  999. return
  1000. }
  1001. func GetMaxLongAdviceGroupID(orgId int64, patient_id int64) (group int64) {
  1002. var advice models.DoctorAdvice
  1003. 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
  1004. if err != nil {
  1005. fmt.Println(err)
  1006. group = 0
  1007. }
  1008. group = advice.GroupNo
  1009. return
  1010. }
  1011. func GetLastLongAdviceByGroupNo(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  1012. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND stop_state = 2 AND parent_id = 0 AND frequency_type <> 0", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  1013. return
  1014. }
  1015. func GetLastLongAdviceByGroupNoOne(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  1016. redis := RedisClient()
  1017. defer redis.Close()
  1018. key := strconv.FormatInt(orgId, 10) + ":" + strconv.FormatInt(patient_id, 10) + ":long_advice"
  1019. long_advice_str, _ := redis.Get(key).Result()
  1020. if len(long_advice_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1021. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND stop_state = 2 AND parent_id = 0 AND frequency_type <> 0", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  1022. if err != nil {
  1023. if err == gorm.ErrRecordNotFound {
  1024. return nil, nil
  1025. } else {
  1026. return nil, err
  1027. }
  1028. } else {
  1029. if len(advice) > 0 {
  1030. //缓存数据
  1031. starfes_str, err := json.Marshal(advice)
  1032. if err == nil {
  1033. redis.Set(key, starfes_str, time.Second*60*60*18)
  1034. return advice, nil
  1035. }
  1036. } else {
  1037. redis.Set(key, "null", time.Second*60*60*18)
  1038. return advice, nil
  1039. }
  1040. return advice, nil
  1041. }
  1042. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1043. if long_advice_str == "null" {
  1044. return advice, nil
  1045. } else {
  1046. json.Unmarshal([]byte(long_advice_str), &advice)
  1047. return advice, nil
  1048. }
  1049. }
  1050. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND stop_state = 2 AND parent_id = 0 AND frequency_type <> 0", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  1051. return
  1052. }
  1053. func GetLastLongAdviceByGroupNoThree(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  1054. 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
  1055. return
  1056. }
  1057. func GetLastLongAdviceByGroupNoFour(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  1058. //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
  1059. //return
  1060. redis := RedisClient()
  1061. defer redis.Close()
  1062. key := strconv.FormatInt(orgId, 10) + ":" + strconv.FormatInt(patient_id, 10) + ":long_advice"
  1063. long_advice_str, _ := redis.Get(key).Result()
  1064. if len(long_advice_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1065. 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
  1066. if err != nil {
  1067. if err == gorm.ErrRecordNotFound {
  1068. return nil, nil
  1069. } else {
  1070. return nil, err
  1071. }
  1072. } else {
  1073. if len(advice) > 0 {
  1074. //缓存数据
  1075. starfes_str, err := json.Marshal(advice)
  1076. if err == nil {
  1077. redis.Set(key, starfes_str, time.Second*60*60*18)
  1078. return advice, nil
  1079. }
  1080. } else {
  1081. redis.Set(key, "null", time.Second*60*60*18)
  1082. return advice, nil
  1083. }
  1084. return advice, nil
  1085. }
  1086. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1087. if long_advice_str == "null" {
  1088. return advice, nil
  1089. } else {
  1090. json.Unmarshal([]byte(long_advice_str), &advice)
  1091. return advice, nil
  1092. }
  1093. }
  1094. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND stop_state = 2 AND parent_id = 0 AND frequency_type <> 0", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  1095. return
  1096. }
  1097. func FindAllDoctorAdviceByTime(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  1098. 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
  1099. return
  1100. }
  1101. func UpdateScheduleModeId(patient_id int64, org_id int64, date int64, mode_id int64) (err error) {
  1102. 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
  1103. return
  1104. }
  1105. func UpdateAssessmentAfterDate(patient_id int64, org_id int64, date int64, actual_treatment_hour int64, actual_treatment_minute int64) (err error) {
  1106. 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
  1107. return
  1108. }
  1109. func UpdateAssessmentAfterDateOne(patient_id int64, org_id int64, date int64, actual_treatment_hour int64, actual_treatment_minute int64, BreathingRate float64) (err error) {
  1110. 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, "breathing_rate": BreathingRate}).Error
  1111. return
  1112. }
  1113. func CreateDialysisBeforePrepare(beforePrepares []*models.DialysisBeforePrepare) (err error) {
  1114. if len(beforePrepares) > 0 {
  1115. utx := writeDb.Begin()
  1116. if len(beforePrepares) > 0 {
  1117. thisSQL := "INSERT INTO dialysis_before_prepare (user_org_id, patient_id, record_date, good_id, good_type_id,count,ctime,mtime,creater,modifier,status,commdity_code) VALUES "
  1118. insertParams := make([]string, 0)
  1119. insertData := make([]interface{}, 0)
  1120. for _, prepare := range beforePrepares {
  1121. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?)")
  1122. insertData = append(insertData, prepare.UserOrgId)
  1123. insertData = append(insertData, prepare.PatientId)
  1124. insertData = append(insertData, prepare.RecordDate)
  1125. insertData = append(insertData, prepare.GoodId)
  1126. insertData = append(insertData, prepare.GoodTypeId)
  1127. insertData = append(insertData, prepare.Count)
  1128. insertData = append(insertData, prepare.Ctime)
  1129. insertData = append(insertData, prepare.Mtime)
  1130. insertData = append(insertData, prepare.Creater)
  1131. insertData = append(insertData, prepare.Modifier)
  1132. insertData = append(insertData, 1)
  1133. insertData = append(insertData, prepare.CommdityCode)
  1134. }
  1135. thisSQL += strings.Join(insertParams, ", ")
  1136. err = utx.Exec(thisSQL, insertData...).Error
  1137. if err != nil {
  1138. utx.Rollback()
  1139. return
  1140. }
  1141. }
  1142. utx.Commit()
  1143. }
  1144. return
  1145. }
  1146. func UpdateDialysisBeforePrepare(info *models.DialysisBeforePrepare) (err error) {
  1147. err = writeDb.Save(&info).Error
  1148. return err
  1149. }
  1150. func UpdateDialysisBeforePrepareTwo(info *models.DialysisBeforePrepare) (err error) {
  1151. err = writeDb.Model(&models.DialysisBeforePrepare{}).
  1152. 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).
  1153. Updates(map[string]interface{}{"count": info.Count, "updated_time": time.Now().Unix(), "modifier": info.Modifier, "commdity_code": info.CommdityCode, "status": 1}).Error
  1154. return err
  1155. }
  1156. func UpdateAutoReduceDetailSix(detail *models.AutomaticReduceDetail) (err error) {
  1157. err = writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? and good_type_id = ? And patient_id = ? and org_id =? and record_time = ?", detail.GoodId, detail.GoodTypeId, detail.PatientId, detail.OrgId, detail.RecordTime).Updates(map[string]interface{}{"count": detail.Count, "mtime": detail.Mtime, "status": 1}).Error
  1158. return err
  1159. }
  1160. func UpdateOrderCountSub(good_type_id int64, good_id int64, number string, org_id int64, count int64) {
  1161. 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))
  1162. }
  1163. func UpdateOrderCountAdd(good_type_id int64, good_id int64, number string, org_id int64, count int64) {
  1164. 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))
  1165. }
  1166. func UpdateUserInfoDetails(good_type_id int64, good_id int64, time int64, org_id int64, patient_id int64, count int64, out *models.WarehouseOutInfo) {
  1167. writeDb.Model(&models.AutomaticReduceDetail{}).Where("good_id = ? AND good_type_id = ? AND org_id = ? AND status = 1 AND warehouse_out_order_number = ? AND patient_id = ?", good_id, good_type_id, org_id, out.WarehouseOutOrderNumber, patient_id).Updates(map[string]interface{}{"count": count, "status": 1})
  1168. //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)
  1169. }
  1170. func UpdateDrugUserInfoDetails(drug_id int64, time int64, org_id int64, patient_id int64, count int64, out *models.DrugWarehouseOutInfo) {
  1171. 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})
  1172. //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)
  1173. }
  1174. func FindDialysisBeforePrepare(patient_id int64, good_id int64, good_type_id int64, user_org_id int64, record_time int64) (count int64) {
  1175. readDb.Model(&models.DialysisBeforePrepare{}).Where("user_org_id = ? AND patient_id = ? AND good_id = ? AND good_type_id = ? AND record_date = ? AND status = 1 AND count<> 0", user_org_id, patient_id, good_id, good_type_id, record_time).Count(&count)
  1176. return
  1177. }
  1178. func GetExecutionDoctors(orgid int64, patientid int64, id int64) (doctorAdvice []*models.DoctorAdvice, err error) {
  1179. 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
  1180. return doctorAdvice, err
  1181. }
  1182. func GetHisExecutionDoctors(orgid int64, patientid int64, id int64) (doctorAdvice []*models.HisDoctorAdviceInfo, err error) {
  1183. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id = ? and patient_id = ? AND (id = ? Or parent_id=?) and status = 1", orgid, patientid, id, id).Find(&doctorAdvice).Error
  1184. return doctorAdvice, err
  1185. }
  1186. func FindHisDoctorAdviceById(orgID int64, id int64) (advice models.HisDoctorAdviceInfo, err error) {
  1187. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id=? and status=1 and id = ?", orgID, id).First(&advice).Error
  1188. return
  1189. }
  1190. func FindHisDoctorAdviceByIds(orgID int64, ids []string) (advice []*models.HisDoctorAdviceInfo, err error) {
  1191. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("user_org_id=? and status=1 and id in (?)", orgID, ids).Find(&advice).Error
  1192. return
  1193. }
  1194. func SaveHisDoctorAdvice(advice *models.HisDoctorAdviceInfo) (err error) {
  1195. err = writeDb.Save(&advice).Error
  1196. return
  1197. }
  1198. func GetGoodInfoMation(orgid int64) (goodinfo []*models.GoodInfo, err error) {
  1199. err = XTReadDB().Model(&goodinfo).Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Preload("GoodSotckInfo", "stock_count > 0 and status = 1").Find(&goodinfo).Error
  1200. //err = XTReadDB().Model(&goodinfo).Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Find(&goodinfo).Error
  1201. return goodinfo, err
  1202. }
  1203. func GetGoodInfoMationSix(orgid int64, storehouse_id int64) (goodinfo []*models.GoodInfo, err error) {
  1204. err = XTReadDB().Model(&goodinfo).Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Preload("GoodSotckInfo", "stock_count > 0 and status = 1 and storehouse_id = ?", storehouse_id).Find(&goodinfo).Error
  1205. return goodinfo, err
  1206. }
  1207. func GetGoodInfoMationSeven(orgid int64, storehouse_id int64) (goodinfo []*models.GoodInfo, err error) {
  1208. err = XTReadDB().Model(&goodinfo).Where("org_id = ? and status = 1 AND find_in_set('停用',good_status) = 0", orgid).Preload("GoodSotckInfo", "status = 1 and storehouse_id = ?", storehouse_id).Find(&goodinfo).Error
  1209. return goodinfo, err
  1210. }
  1211. func GetDialysisBeforePrepare(goodTypeId int64, goodId int64, orgid int64, patientId int64) (*models.DialysisBeforePrepare, error) {
  1212. prepare := models.DialysisBeforePrepare{}
  1213. err := XTReadDB().Where("good_type_id = ? and good_id = ? and user_org_id = ? and status =1 and patient_id = ?", goodTypeId, goodId, orgid, patientId).Last(&prepare).Error
  1214. if err == gorm.ErrRecordNotFound {
  1215. return nil, err
  1216. }
  1217. if err != nil {
  1218. return nil, err
  1219. }
  1220. return &prepare, nil
  1221. }
  1222. func CreateDialysisBeforePrepareOne(prepare *models.DialysisBeforePrepare) error {
  1223. err := XTReadDB().Create(&prepare).Error
  1224. return err
  1225. }
  1226. func ModifyExceDoctorAdviceById(m *models.HisDoctorAdviceInfo, ids []string) (err error) {
  1227. ut := writeDb.Begin()
  1228. err = ut.Model(&models.HisDoctorAdviceInfo{}).Where("status = 1 AND id IN (?) AND execution_state = 1", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  1229. if err != nil {
  1230. ut.Rollback()
  1231. return
  1232. }
  1233. err = ut.Model(&models.HisDoctorAdviceInfo{}).Where("status = 1 AND parent_id IN (?) AND execution_state = 1 ", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  1234. if err != nil {
  1235. ut.Rollback()
  1236. return
  1237. }
  1238. ut.Commit()
  1239. return err
  1240. }
  1241. func FindHisDoctorAdviceByIdOne(orgID int64, ids []string) (advice []models.HisDoctorAdviceInfo, err error) {
  1242. err = readDb.Model(&models.HisDoctorAdviceInfo{}).Where("id IN (?) AND user_org_id = ? AND status = 1", ids, orgID).Find(&advice).Error
  1243. return
  1244. }
  1245. func ModifyHisPrescriptionProject(m *models.HisPrescriptionProject, ids []string) (err error) {
  1246. ut := writeDb.Begin()
  1247. err = ut.Model(&models.HisPrescriptionProject{}).Where("status = 1 AND id IN (?) AND execution_state = 1", ids).Updates(map[string]interface{}{"execution_time": m.ExecutionTime}).Error
  1248. if err != nil {
  1249. ut.Rollback()
  1250. return
  1251. }
  1252. ut.Commit()
  1253. return err
  1254. }
  1255. func FindHisProjectById(orgID int64, id int64) (advice models.HisPrescriptionProject, err error) {
  1256. err = readDb.Model(&models.HisPrescriptionProject{}).Where("user_org_id=? and status=1 and id = ?", orgID, id).First(&advice).Error
  1257. return
  1258. }
  1259. func SaveHisProject(advice *models.HisPrescriptionProject) (err error) {
  1260. err = writeDb.Save(&advice).Error
  1261. return
  1262. }
  1263. func FindAllHisProjectById(orgID int64, patient_id int64, record_time int64) (advice []*models.HisPrescriptionProject, err error) {
  1264. err = readDb.Model(&models.HisPrescriptionProject{}).Preload("HisProject", "status = 1").Preload("GoodInfo", "status = 1").Where("user_org_id=? and status=1 and patient_id = ? AND record_date = ?", orgID, patient_id, record_time).Find(&advice).Error
  1265. return
  1266. }
  1267. func GetAllPcPatientListByListSeven(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1268. err = readDb.Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  1269. return patients, err
  1270. }
  1271. func GetAllPcPatientListByListEight(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1272. err = readDb.Where("user_org_id=? and status=1", orgID).Find(&patients).Error
  1273. return patients, err
  1274. }
  1275. func GetAllPcPatientListByList(orgID int64) (patients []*MSchedualPatientVMList, err error) {
  1276. redis := RedisClient()
  1277. defer redis.Close()
  1278. key := strconv.FormatInt(orgID, 10) + ":" + ":patient_list_all"
  1279. patient_info_str, _ := redis.Get(key).Result()
  1280. if len(patient_info_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1281. err = readDb.Where("user_org_id=? and status=1 and lapseto = 1", orgID).Find(&patients).Error
  1282. if err != nil {
  1283. if err == gorm.ErrRecordNotFound {
  1284. if len(patients) <= 0 {
  1285. redis.Set(key, "null", time.Second*60*60*18)
  1286. }
  1287. return nil, nil
  1288. } else {
  1289. return nil, err
  1290. }
  1291. } else {
  1292. if len(patients) > 0 {
  1293. //缓存数据
  1294. patient_info_json, err := json.Marshal(&patients)
  1295. if err == nil {
  1296. redis.Set(key, patient_info_json, time.Second*60*60*18)
  1297. }
  1298. } else {
  1299. redis.Set(key, "null", time.Second*60*60*18)
  1300. }
  1301. return patients, nil
  1302. }
  1303. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1304. if patient_info_str == "null" {
  1305. return nil, nil
  1306. } else {
  1307. json.Unmarshal([]byte(patient_info_str), &patients)
  1308. return patients, nil
  1309. }
  1310. }
  1311. }
  1312. func GetAllPcAdvicestByList(orgID int64, scheduleDate int64) (advices []*VMDoctorAdvice, err error) {
  1313. redis := RedisClient()
  1314. defer redis.Close()
  1315. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":advice_list_all"
  1316. advice_list_str, _ := redis.Get(key).Result()
  1317. if len(advice_list_str) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1318. err = readDb.Where("status = 1 AND user_org_id = ? AND advice_type = 2 AND advice_date = ? ", orgID, scheduleDate).Find(&advices).Error
  1319. if err != nil {
  1320. if err == gorm.ErrRecordNotFound {
  1321. if len(advices) <= 0 {
  1322. redis.Set(key, "null", time.Second*60*60*18)
  1323. }
  1324. return nil, nil
  1325. } else {
  1326. return nil, err
  1327. }
  1328. } else {
  1329. if len(advices) > 0 {
  1330. //缓存数据
  1331. advice_list_str, err := json.Marshal(&advices)
  1332. if err == nil {
  1333. redis.Set(key, advice_list_str, time.Second*60*60*18)
  1334. }
  1335. } else {
  1336. redis.Set(key, "null", time.Second*60*60*18)
  1337. }
  1338. return advices, nil
  1339. }
  1340. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1341. if advice_list_str == "null" {
  1342. return nil, nil
  1343. } else {
  1344. json.Unmarshal([]byte(advice_list_str), &advices)
  1345. return advices, nil
  1346. }
  1347. }
  1348. }
  1349. func GetAllPcDialysisOrdersByList(orgID int64, scheduleDate int64) (dialysisOrders []*MDialysisOrderVMList, err error) {
  1350. redis := RedisClient()
  1351. defer redis.Close()
  1352. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":dialysis_orders_list_all"
  1353. dialysis_orders_list_all, _ := redis.Get(key).Result()
  1354. if len(dialysis_orders_list_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1355. err = readDb.Preload("DeviceNumber", "status = 1 AND org_id = ?", orgID).Where("status = 1 AND user_org_id = ? AND dialysis_date = ?", orgID, scheduleDate).Find(&dialysisOrders).Error
  1356. if err != nil {
  1357. if err == gorm.ErrRecordNotFound {
  1358. return nil, nil
  1359. } else {
  1360. return nil, err
  1361. }
  1362. } else {
  1363. if len(dialysisOrders) > 0 {
  1364. //缓存数据
  1365. dialysis_orders_list_all, err := json.Marshal(&dialysisOrders)
  1366. if err == nil {
  1367. redis.Set(key, dialysis_orders_list_all, time.Second*60*60*18)
  1368. }
  1369. } else {
  1370. redis.Set(key, "null", time.Second*60*60*18)
  1371. }
  1372. return dialysisOrders, nil
  1373. }
  1374. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1375. if dialysis_orders_list_all == "null" {
  1376. return nil, nil
  1377. } else {
  1378. json.Unmarshal([]byte(dialysis_orders_list_all), &dialysisOrders)
  1379. return dialysisOrders, nil
  1380. }
  1381. }
  1382. }
  1383. func GetAllPcAssessmentAfterDislysisByList(orgID int64, scheduleDate int64) (assessmentAfterDislysis []*AssessmentAfterDislysis, err error) {
  1384. redis := RedisClient()
  1385. defer redis.Close()
  1386. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":assessment_after_dislysis_list_all"
  1387. assessment_after_dislysis__all, _ := redis.Get(key).Result()
  1388. if len(assessment_after_dislysis__all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1389. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&assessmentAfterDislysis).Error
  1390. if err != nil {
  1391. if err == gorm.ErrRecordNotFound {
  1392. if len(assessmentAfterDislysis) <= 0 {
  1393. redis.Set(key, "null", time.Second*60*60*18)
  1394. }
  1395. return nil, nil
  1396. } else {
  1397. return nil, err
  1398. }
  1399. } else {
  1400. if len(assessmentAfterDislysis) > 0 {
  1401. //缓存数据
  1402. assessment_after_dislysis__all, err := json.Marshal(&assessmentAfterDislysis)
  1403. if err == nil {
  1404. redis.Set(key, assessment_after_dislysis__all, time.Second*60*60*18)
  1405. }
  1406. } else {
  1407. redis.Set(key, "null", time.Second*60*60*18)
  1408. }
  1409. return assessmentAfterDislysis, nil
  1410. }
  1411. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1412. if assessment_after_dislysis__all == "null" {
  1413. return nil, nil
  1414. } else {
  1415. json.Unmarshal([]byte(assessment_after_dislysis__all), &assessmentAfterDislysis)
  1416. return assessmentAfterDislysis, nil
  1417. }
  1418. }
  1419. }
  1420. func GetAllPcTreatmentSummarysByList(orgID int64, scheduleDate int64) (treatmentSummarys []*VMTreatmentSummary, err error) {
  1421. redis := RedisClient()
  1422. defer redis.Close()
  1423. key := strconv.FormatInt(orgID, 10) + ":" + strconv.FormatInt(scheduleDate, 10) + ":treatment_summarys_list_all"
  1424. treatment_summarys_all, _ := redis.Get(key).Result()
  1425. if len(treatment_summarys_all) == 0 { //没有到缓存数据,从数据库中获取数据,进行缓存到redis
  1426. err = readDb.Where("status = 1 AND user_org_id = ? AND assessment_date = ?", orgID, scheduleDate).Find(&treatmentSummarys).Error
  1427. if err != nil {
  1428. if err == gorm.ErrRecordNotFound {
  1429. if len(treatment_summarys_all) <= 0 {
  1430. redis.Set(key, "null", time.Second*60*60*18)
  1431. }
  1432. return nil, nil
  1433. } else {
  1434. return nil, err
  1435. }
  1436. } else {
  1437. if len(treatmentSummarys) > 0 {
  1438. //缓存数据
  1439. treatment_summarys_all, err := json.Marshal(&treatmentSummarys)
  1440. if err == nil {
  1441. redis.Set(key, treatment_summarys_all, time.Second*60*60*18)
  1442. }
  1443. } else {
  1444. redis.Set(key, "null", time.Second*60*60*18)
  1445. }
  1446. return treatmentSummarys, nil
  1447. }
  1448. } else { //缓存数据了数据,将redis缓存的json字符串转为map
  1449. if treatment_summarys_all == "null" {
  1450. return nil, nil
  1451. } else {
  1452. json.Unmarshal([]byte(treatment_summarys_all), &treatmentSummarys)
  1453. return treatmentSummarys, nil
  1454. }
  1455. }
  1456. }
  1457. func UpdateTeamProjectExceStatus(ids []int64, admin_user_id int64, execution_time int64) (err error) {
  1458. ut := writeDb.Begin()
  1459. db := ut.Model(&models.HisPrescriptionProject{})
  1460. err = db.Where("status = 1 AND id IN (?)", ids).Updates(map[string]interface{}{"execution_time": execution_time, "execution_state": 1, "execution_staff": admin_user_id}).Error
  1461. if err != nil {
  1462. ut.Rollback()
  1463. return
  1464. }
  1465. ut.Commit()
  1466. return err
  1467. }
  1468. func UpdateTeamProjectCheckStatus(ids []int64, patient_id int64, record_date int64, user_org_id int64, admin_user_id int64, check_time int64) (err error) {
  1469. ut := writeDb.Begin()
  1470. err = ut.Model(&models.HisPrescriptionProject{}).Where("status = 1 AND id IN (?) AND patient_id = ? AND user_org_id = ? AND record_date = ?", ids, patient_id, user_org_id, record_date).Updates(map[string]interface{}{"check_time": check_time, "check_state": 1, "checker": admin_user_id}).Error
  1471. if err != nil {
  1472. ut.Rollback()
  1473. return
  1474. }
  1475. ut.Commit()
  1476. return err
  1477. }
  1478. func GetCheckTeamProject(team_id int64, patient_id int64, record_date int64, user_org_id int64) (ps []*models.HisPrescriptionProject, err error) {
  1479. err = readDb.Model(&models.HisPrescriptionProject{}).Joins("JOIN xt_his_project as p On p.id = his_prescription_project.project_id AND p.cost_classify = 3").Where("his_prescription_project.team_id IN (?) AND his_prescription_project.patient_id = ? AND his_prescription_project.user_org_id = ? AND his_prescription_project.record_date = ? AND his_prescription_project.status = 1", team_id, patient_id, user_org_id, record_date).Find(&ps).Error
  1480. return
  1481. }
  1482. func GetAdminUserRoleInformation(orgid int64) (models.SgjOrgFollow, error) {
  1483. follow := models.SgjOrgFollow{}
  1484. db := UserReadDB().Table("sgj_org_follow as f").Where("f.status = 1")
  1485. table := UserReadDB().Table("sgj_admin as a").Where("a.status = 1")
  1486. fmt.Println(table)
  1487. if orgid > 0 {
  1488. db = db.Where("org_id = ?", orgid)
  1489. }
  1490. if orgid == 0 {
  1491. db = db.Where("org_id = ?", 0)
  1492. }
  1493. err := db.Select("f.org_id,f.admin_user_id,a.name,a.phone,a.camera,a.wechat,a.qqchat,a.email,a.wechat_link").Joins("left join sgj_admin as a on a.id = f.admin_user_id").Scan(&follow).Error
  1494. return follow, err
  1495. }
  1496. func GetOrgFollowIsExist(orgid int64) (*models.SgjOrgFollowOne, error) {
  1497. follow := models.SgjOrgFollowOne{}
  1498. err := UserReadDB().Where("org_id = ? and status = 1", orgid).Find(&follow).Error
  1499. if err == gorm.ErrRecordNotFound {
  1500. return nil, err
  1501. }
  1502. if err != nil {
  1503. return nil, err
  1504. }
  1505. return &follow, nil
  1506. }