schedule_service.go 72KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  1. package service
  2. import (
  3. "XT_New/models"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/jinzhu/gorm"
  9. )
  10. // GetSchedulePartitionPanel 取出排班面板,分区-机号
  11. func GetSchedulePartitionPanel(orgID int64) (partitions []*models.Partition, err error) {
  12. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1", orgID).Where("org_id=? and status=1", orgID).Order("").Find(&partitions).Error
  13. return
  14. }
  15. func GetSchedulePartitionPanelOne(orgID int64, partionId []string) (partitions []*models.Partition, err error) {
  16. //if len(partionId) > 0 {
  17. // err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1 and zone_id in (?)", orgID, partionId).Where("org_id=? and status=1 and id ?", orgID, partionId).Find(&partitions).Error
  18. //}
  19. //if len(partionId) == 0 {
  20. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1", orgID).Where("org_id=? and status=1", orgID).Find(&partitions).Error
  21. //}
  22. return
  23. }
  24. func GetSchedulePartitionPanelTwo(orgID int64, schIDs []string) (partitions []*models.Partition, err error) {
  25. if len(schIDs) == 0 {
  26. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1", orgID).Where("org_id=? and status=1", orgID).Find(&partitions).Error
  27. }
  28. if len(schIDs) > 0 {
  29. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", func(db *gorm.DB) *gorm.DB {
  30. return db.Where("org_id=? and status=1 and zone_id in (?)", orgID, schIDs).Order("sort desc")
  31. }).Where("org_id=? and status=1 and id in(?)", orgID, schIDs).Find(&partitions).Error
  32. }
  33. return
  34. }
  35. func GetPatientWithScheduleAndSolution(orgID int64, keywords string, weekStart, weekEnd, schedule, contagion int64) (patients []*models.Patients, err error) {
  36. db := readDb.Table("xt_patients as p").
  37. Preload("DialysisSolutions", "user_org_id=? and initiate_mode=1 and affirm_state=1 and parent_id=0 and status=1", orgID).
  38. Preload("Schedules", "user_org_id=? and schedule_date>=? and schedule_date<= ? and status=1", orgID, weekStart, weekEnd).
  39. Preload("Contagions", "status=1").
  40. Where("p.user_org_id=? and p.lapseto=1 and p.status=1", orgID)
  41. if len(keywords) > 0 {
  42. likeKey := "%" + keywords + "%"
  43. db = db.Where("p.name LIKE ? or p.first_letter LIKE ?", likeKey, likeKey)
  44. }
  45. if schedule == 1 {
  46. db = db.Where("EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.schedule_date>=? and iss.schedule_date<= ? and iss.status=1", weekStart, weekEnd).QueryExpr())
  47. } else if schedule == 2 {
  48. db = db.Where("NOT EXISTS (?)", readDb.Table("xt_schedule as iss").Where("iss.patient_id=p.id and iss.schedule_date>=? and iss.schedule_date<= ? and iss.status=1", weekStart, weekEnd).QueryExpr())
  49. }
  50. if contagion > 0 {
  51. db = db.Where("EXISTS (?)", readDb.Table("xt_patients_infectious_diseases as pid").Where("pid.patient_id=p.id and pid.disease_id=? and pid.status=1", contagion).QueryExpr())
  52. }
  53. err = db.Find(&patients).Error
  54. return
  55. }
  56. func GetPatientWithScheduleAndSolutionTwo(orgID int64, keywords string, weekStart, weekEnd, schedule, contagion int64) (patients []*models.Patients, err error) {
  57. db := readDb.Table("xt_patients as p").
  58. Preload("DialysisSolutions", "user_org_id=? and initiate_mode=1 and affirm_state=1 and parent_id=0 and status=1", orgID).
  59. //Preload("Schedules", "user_org_id=? and schedule_date>=? and schedule_date<= ? and status=1", orgID, weekStart, weekEnd).
  60. Where("p.user_org_id=? and p.lapseto=1 and p.status=1", orgID)
  61. if len(keywords) > 0 {
  62. likeKey := "%" + keywords + "%"
  63. db = db.Where("p.name LIKE ?", likeKey)
  64. }
  65. err = db.Find(&patients).Error
  66. return
  67. }
  68. func GetWeekSchedule(orgID int64, start, end int64) (schedules []*models.Schedule, err error) {
  69. err = readDb.Table("xt_schedule as s").Preload("PatientInfectiousDiseases", "status = 1 ").
  70. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  71. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  72. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time,s.dialysis_machine_name, p.name as patient").
  73. Order("updated_time asc").
  74. Find(&schedules).Error
  75. return
  76. }
  77. func GetWeekScheduleOne(orgID int64, start, end int64, partionid int64, scheduletype int64) (schedules []*models.Schedule, err error) {
  78. db := readDb.Table("xt_schedule as s")
  79. if partionid > 0 {
  80. db = db.Where("s.partition_id = ?", partionid)
  81. }
  82. if scheduletype > 0 {
  83. db = db.Where("s.schedule_type = ?", scheduletype)
  84. }
  85. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  86. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  87. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  88. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time,s.dialysis_machine_name, s.updated_time, p.name as patient").
  89. Order("updated_time asc").
  90. Find(&schedules).Error
  91. return
  92. }
  93. func GetWeekScheduleTwo(orgID int64, start, end int64, schIds []string, scheduletype int64) (schedules []*models.ScheduleTwo, err error) {
  94. db := readDb.Table("xt_schedule as s")
  95. if len(schIds) > 0 {
  96. db = db.Where("s.partition_id in (?)", schIds)
  97. }
  98. if scheduletype > 0 {
  99. db = db.Where("s.schedule_type = ?", scheduletype)
  100. }
  101. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  102. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  103. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  104. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time,s.dialysis_machine_name, p.name as patient").
  105. Order("updated_time asc").
  106. Find(&schedules).Error
  107. return
  108. }
  109. func GetScheduleTotalCount(orgID int64, start, end int64, schIds []string, scheduletype int64) (schedules []*models.MySchedule, err error) {
  110. db := readDb.Table("xt_schedule as s").Where("status= 1 and (mode_id = 2 or mode_id= 3)")
  111. if len(schIds) > 0 {
  112. db = db.Where("s.partition_id in (?)", schIds)
  113. }
  114. if scheduletype > 0 {
  115. db = db.Where("s.schedule_type = ?", scheduletype)
  116. }
  117. if orgID > 0 {
  118. db = db.Where("s.user_org_id = ?", orgID)
  119. }
  120. if start > 0 {
  121. db = db.Where("s.schedule_date>=?", start)
  122. }
  123. if end > 0 {
  124. db = db.Where("s.schedule_date<=?", end)
  125. }
  126. err = db.Select("Count(id) as count,schedule_date,mode_id,user_org_id,schedule_week,schedule_type").Group("schedule_date,schedule_type,mode_id").Find(&schedules).Error
  127. return schedules, err
  128. }
  129. func GetWeekScheduleThree(orgID int64, start, end int64, schIds []string, scheduletype int64, WeekDayIds []string) (schedules []*models.ScheduleTwo, err error) {
  130. db := readDb.Table("xt_schedule as s")
  131. if len(schIds) > 0 {
  132. db = db.Where("s.partition_id in (?)", schIds)
  133. }
  134. if scheduletype > 0 {
  135. db = db.Where("s.schedule_type = ?", scheduletype)
  136. }
  137. if len(WeekDayIds) > 0 {
  138. db = db.Where("s.schedule_week in(?)", WeekDayIds)
  139. }
  140. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  141. Preload("DialysisSolution", "status= 1 and solution_status = 1").
  142. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  143. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  144. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time,s.dialysis_machine_name, p.name as patient").
  145. Order("updated_time asc").
  146. Find(&schedules).Error
  147. return
  148. }
  149. func GetWeekScheduleFour(orgID int64, start, end int64, schIds []string, scheduletype int64, WeekDayIds []string) (schedules []*models.ScheduleThree, err error) {
  150. db := readDb.Table("xt_schedule as s")
  151. if len(schIds) > 0 {
  152. db = db.Where("s.partition_id in (?)", schIds)
  153. }
  154. if scheduletype > 0 {
  155. db = db.Where("s.schedule_type = ?", scheduletype)
  156. }
  157. if len(WeekDayIds) > 0 {
  158. db = db.Where("s.schedule_week in(?)", WeekDayIds)
  159. }
  160. err = db.Preload("DeviceNumber", "status = 1 and org_id =?", orgID).
  161. Preload("DialysisSolution", "status= 1 and solution_status = 1").
  162. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  163. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  164. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time,s.dialysis_machine_name, p.name as patient").
  165. Order("updated_time asc").
  166. Find(&schedules).Error
  167. return
  168. }
  169. func GetDaySchedule(orgID, start, end, patientID int64) (schedule models.Schedule, err error) {
  170. err = readDb.Model(&models.Schedule{}).Where("user_org_id=? and patient_id=? and schedule_date>=? and schedule_date<=? and status=1", orgID, patientID, start, end).First(&schedule).Error
  171. return
  172. }
  173. func GetDayScheduleTwo(orgID, start, patientID int64) (schedule models.Schedule, err error) {
  174. err = readDb.Model(&models.Schedule{}).Where("user_org_id=? and patient_id=? and schedule_date = ? and status=1", orgID, patientID, start).Preload("DialysisOrder", "status = 1").First(&schedule).Error
  175. return
  176. }
  177. func GetDayScheduleByBedid(orgID, start, bed_id int64, schedule_type int64) (schedule models.Schedule, err error) {
  178. err = readDb.Model(&models.Schedule{}).Where("user_org_id=? and schedule_date=? and bed_id=? and status=1 AND schedule_type = ?", orgID, start, bed_id, schedule_type).Preload("DialysisOrder", "status = 1").First(&schedule).Error
  179. return
  180. }
  181. func GetSourceSch(orgId int64, patient_id int64, record_date int64) (schedule models.Schedule, err error) {
  182. err = readDb.Model(&models.Schedule{}).Where("user_org_id=? and schedule_date=? and patient_id =? and status=1", orgId, record_date, patient_id).First(&schedule).Error
  183. return
  184. }
  185. func GetPatientScheduleFormDay(orgID, start, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  186. err = readDb.Table("xt_schedule as s").
  187. Preload("DeviceZone", "org_id=? and status=1", orgID).
  188. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  189. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.schedule_date < ? and s.status=1", patientID, orgID, start, end).
  190. Find(&schedules).Error
  191. return
  192. }
  193. func GetNextSchedule(orgID, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  194. err = readDb.Table("xt_schedule as s").
  195. Preload("DeviceZone", "org_id=? and status=1", orgID).
  196. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  197. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, end).
  198. Find(&schedules).Error
  199. return
  200. }
  201. func GetNextScheduleByList(patient_id int64, schedule_date int64, user_org_id int64) (models.SchedulePatientsOne, error) {
  202. patientsOne := models.SchedulePatientsOne{}
  203. err := XTReadDB().Where("patient_id = ? and schedule_date >? and user_org_id = ? and status=1", patient_id, schedule_date, user_org_id).Order("schedule_date asc").First(&patientsOne).Error
  204. return patientsOne, err
  205. }
  206. func GetPointSchedule(orgID, scheduleDate, scheduleWeek, scheduleType, bedId int64) (schedule models.Schedule, err error) {
  207. err = readDb.Model(&models.Schedule{}).Where("user_org_id=? and schedule_date=? and bed_id=? and schedule_week=? AND schedule_type=? and status=1", orgID, scheduleDate, bedId, scheduleWeek, scheduleType).First(&schedule).Error
  208. if err == gorm.ErrRecordNotFound {
  209. err = nil
  210. return
  211. }
  212. return
  213. }
  214. func CreateSchedule(m *models.Schedule, id int64) (err error) {
  215. utx := XTWriteDB().Begin()
  216. schedule := models.XtSchedule{}
  217. err = utx.Model(&schedule).Where("id=? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  218. if err != nil {
  219. utx.Rollback()
  220. return err
  221. }
  222. err = utx.Create(&m).Error
  223. if err != nil {
  224. utx.Rollback()
  225. return err
  226. }
  227. utx.Commit()
  228. return err
  229. }
  230. func DeleteScheduleExist(scheduleData int64, user_org_id int64) (models.XtSchedule, error) {
  231. schedule := models.XtSchedule{}
  232. err := XTWriteDB().Model(&schedule).Where("schedule_date= ? and user_org_id = ? and status = 1", scheduleData, user_org_id).Updates(map[string]interface{}{"status": 0}).Error
  233. return schedule, err
  234. }
  235. func GetTodayScheduleIsExist(patientid int64, schedudate int64, orgid int64, mode_id int64, schedule_type int64) (*models.XtSchedule, error) {
  236. var scheudle models.XtSchedule
  237. var err error
  238. err = XTReadDB().Model(&scheudle).Where("patient_id = ? and schedule_date= ? and user_org_id = ? and status = 1 and mode_id = ? and schedule_type = ?", patientid, schedudate, orgid, mode_id, schedule_type).Find(&scheudle).Error
  239. if err == gorm.ErrRecordNotFound {
  240. return nil, err
  241. }
  242. if err != nil {
  243. return nil, err
  244. }
  245. return &scheudle, nil
  246. }
  247. func GetTodayScheduleIsExistOne(patientid int64, schedudate int64, orgid int64) (*models.XtSchedule, error) {
  248. var scheudle models.XtSchedule
  249. var err error
  250. err = XTReadDB().Model(&scheudle).Where("patient_id = ? and schedule_date= ? and user_org_id = ? and status = 1", patientid, schedudate, orgid).Find(&scheudle).Error
  251. if err == gorm.ErrRecordNotFound {
  252. return nil, err
  253. }
  254. if err != nil {
  255. return nil, err
  256. }
  257. return &scheudle, nil
  258. }
  259. func GetPatientByBed(schedule_date int64, bed_id int64, scheduletype int64, orgid int64) (*models.XtSchedule, error) {
  260. var scheudle models.XtSchedule
  261. var err error
  262. err = XTReadDB().Model(&scheudle).Where(" schedule_date= ? and bed_id = ? and schedule_type = ? and user_org_id = ? and status = 1", schedule_date, bed_id, scheduletype, orgid).Find(&scheudle).Error
  263. if err == gorm.ErrRecordNotFound {
  264. return nil, err
  265. }
  266. if err != nil {
  267. return nil, err
  268. }
  269. return &scheudle, nil
  270. }
  271. func ModeFyScheduleById(schedule_date int64, bedid int64, scheduletype int64, orgid int64) error {
  272. schedule := models.XtSchedule{}
  273. err := XTWriteDB().Model(&schedule).Where("schedule_date = ? and bed_id =? and schedule_type = ? and status = 1 and user_org_id = ?", schedule_date, bedid, scheduletype, orgid).Updates(map[string]interface{}{"status": 0}).Error
  274. return err
  275. }
  276. func ModeScheduleByTime(startime int64, endtime int64, orgid int64) error {
  277. schedule := models.XtSchedule{}
  278. err := XTWriteDB().Model(&schedule).Where("schedule_date >? and schedule_date<=? and status = 1 and user_org_id = ?", startime, endtime, orgid).Updates(map[string]interface{}{"status": 0}).Error
  279. return err
  280. }
  281. func ModeScheduleByTimeOne(startime int64, endtime int64, orgid int64) error {
  282. schedule := models.XtSchedule{}
  283. err := XTWriteDB().Model(&schedule).Where("schedule_date >=? and schedule_date<=? and status = 1 and user_org_id = ?", startime, endtime, orgid).Updates(map[string]interface{}{"status": 0}).Error
  284. return err
  285. }
  286. func UpdateScheduleByOrgId(patientid int64, scheduledate int64, orgid int64, mode_id int64, schedule_type int64, sch *models.XtSchedule) error {
  287. err := XTWriteDB().Model(&sch).Where("patient_id = ? and schedule_date= ? and user_org_id = ? and status = 1 and mode_id = ? and schedule_type = ?", patientid, scheduledate, orgid, mode_id, schedule_type).Updates(map[string]interface{}{"partition_id": sch.PartitionId, "bed_id": sch.BedId, "schedule_type": sch.ScheduleType, "schedule_week": sch.ScheduleWeek, "mode_id": sch.ModeId, "updated_time": time.Now().Unix()}).Error
  288. return err
  289. }
  290. func UpdateScheduleByOrgIdOne(patientid int64, scheduledate int64, orgid int64, sch *models.XtSchedule) error {
  291. err := XTWriteDB().Model(&sch).Where("patient_id = ? and schedule_date= ? and user_org_id = ? and status = 1", patientid, scheduledate, orgid).Updates(map[string]interface{}{"partition_id": sch.PartitionId, "bed_id": sch.BedId, "schedule_type": sch.ScheduleType, "schedule_week": sch.ScheduleWeek, "mode_id": sch.ModeId, "updated_time": time.Now().Unix(), "dialysis_machine_name": sch.DialysisMachineName}).Error
  292. return err
  293. }
  294. func GetSchedule(orgID, id int64) (*models.Schedule, error) {
  295. var schedule models.Schedule
  296. var err error
  297. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  298. if err != nil {
  299. return nil, err
  300. }
  301. return &schedule, nil
  302. }
  303. func GetScheduleNight(orgID, id int64) (*models.Schedule, error) {
  304. var schedule models.Schedule
  305. var err error
  306. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=?", id, orgID).First(&schedule).Error
  307. if err != nil {
  308. return nil, err
  309. }
  310. return &schedule, nil
  311. }
  312. func UpdateSchedule(m *models.Schedule) error {
  313. utx := XTWriteDB().Begin()
  314. err := utx.Save(&m).Error
  315. utx.Commit()
  316. return err
  317. }
  318. func UpdateScheduleOne(id int64, schedule models.Schedule) error {
  319. utx := XTWriteDB().Begin()
  320. err := XTWriteDB().Model(&schedule).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"partition_id": schedule.PartitionId, "bed_id": schedule.BedId, "schedule_type": schedule.ScheduleType}).Error
  321. utx.Commit()
  322. return err
  323. }
  324. // 参数一:当前操作的患者的排班 参数二被替换床位对应的排班
  325. func UpdateScheduleTwo(schone models.Schedule, schtwo models.Schedule) (err error) {
  326. utx := XTWriteDB().Begin()
  327. //床位
  328. var tempBedID int64
  329. var tempBedIDTwo int64
  330. //分區
  331. var tempPID int64
  332. var tempPIDTwo int64
  333. //上下午
  334. var tempTypeID int64
  335. var tempTypeTwo int64
  336. tempBedID = schone.BedId
  337. tempBedIDTwo = schtwo.BedId
  338. tempPID = schone.PartitionId
  339. tempPIDTwo = schtwo.PartitionId
  340. tempTypeID = schone.ScheduleType
  341. tempTypeTwo = schtwo.ScheduleType
  342. schone.BedId = tempBedIDTwo
  343. schtwo.BedId = tempBedID
  344. schone.PartitionId = tempPIDTwo
  345. schtwo.PartitionId = tempPID
  346. schone.ScheduleType = tempTypeTwo
  347. schtwo.ScheduleType = tempTypeID
  348. if err = utx.Model(&models.Schedule{}).Where("id = ?", schone.ID).Updates(map[string]interface{}{"partition_id": schone.PartitionId, "bed_id": schone.BedId, "schedule_type": schone.ScheduleType, "is_export": 90000}).Error; err != nil {
  349. utx.Rollback()
  350. return err
  351. }
  352. if err = utx.Model(&models.Schedule{}).Where("id = ?", schtwo.ID).Updates(map[string]interface{}{"partition_id": schtwo.PartitionId, "bed_id": schtwo.BedId, "schedule_type": schtwo.ScheduleType, "is_export": 90000}).Error; err != nil {
  353. utx.Rollback()
  354. return err
  355. }
  356. utx.Commit()
  357. return err
  358. }
  359. func UpdateScheduleThree(schone *models.Schedule, schtwo *models.Schedule) (err error) {
  360. utx := XTWriteDB().Begin()
  361. //床位
  362. var tempBedID int64
  363. var tempBedIDTwo int64
  364. //分區
  365. var tempPID int64
  366. var tempPIDTwo int64
  367. //上下午
  368. var tempTypeID int64
  369. var tempTypeTwo int64
  370. //日期
  371. var sch_date_one int64
  372. var sch_date_two int64
  373. //周数
  374. var sch_week_one int64
  375. var sch_week_two int64
  376. //原数据
  377. tempBedID = schone.BedId
  378. tempBedIDTwo = schtwo.BedId
  379. tempPID = schone.PartitionId
  380. tempPIDTwo = schtwo.PartitionId
  381. tempTypeID = schone.ScheduleType
  382. tempTypeTwo = schtwo.ScheduleType
  383. sch_date_one = schone.ScheduleDate
  384. sch_date_two = schtwo.ScheduleDate
  385. sch_week_one = schone.ScheduleWeek
  386. sch_week_two = schtwo.ScheduleWeek
  387. //交换数据
  388. schone.BedId = tempBedIDTwo
  389. schtwo.BedId = tempBedID
  390. schone.PartitionId = tempPIDTwo
  391. schtwo.PartitionId = tempPID
  392. schone.ScheduleType = tempTypeTwo
  393. schtwo.ScheduleType = tempTypeID
  394. schone.ScheduleDate = sch_date_two
  395. schtwo.ScheduleDate = sch_date_one
  396. schone.ScheduleWeek = sch_week_two
  397. schtwo.ScheduleWeek = sch_week_one
  398. if err = utx.Model(&models.Schedule{}).Where("id = ?", schone.ID).Updates(map[string]interface{}{"partition_id": schone.PartitionId, "bed_id": schone.BedId, "schedule_type": schone.ScheduleType, "schedule_date": schone.ScheduleDate, "schedule_week": schone.ScheduleWeek, "is_export": 80000}).Error; err != nil {
  399. utx.Rollback()
  400. return err
  401. }
  402. if err = utx.Model(&models.Schedule{}).Where("id = ?", schtwo.ID).Updates(map[string]interface{}{"partition_id": schtwo.PartitionId, "bed_id": schtwo.BedId, "schedule_type": schtwo.ScheduleType, "schedule_date": schtwo.ScheduleDate, "schedule_week": schtwo.ScheduleWeek, "is_export": 80000}).Error; err != nil {
  403. utx.Rollback()
  404. return err
  405. }
  406. utx.Commit()
  407. return err
  408. }
  409. func GetPatientScheduleFormToday(orgId, id, start int64) (schedules []*models.PatientSchedule, err error) {
  410. err = readDb.Table("xt_schedule as s").
  411. Preload("DeviceZone", "org_id=? and status=1", orgId).
  412. Preload("DeviceNumber", "org_id=? and status=1", orgId).
  413. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", id, orgId, start).
  414. Find(&schedules).Error
  415. return
  416. }
  417. type WeekSchedulePrintVM struct {
  418. ID int64 `gorm:"column:id" json:"id"`
  419. ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  420. DeviceNumberID int64 `gorm:"column:bed_id" json:"device_number_id"`
  421. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  422. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  423. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  424. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  425. ModeID int64 `gorm:"column:mode_id" json:"mode_id"`
  426. Patient *models.Patients `gorm:"ForeignKey:PatientID" json:"patient"`
  427. DeviceNumber *models.DeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  428. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  429. Mode *models.TreatmentMode `gorm:"ForeignKey:ModeID" json:"mode"`
  430. }
  431. type WeekSchedulePrintPatients struct {
  432. ID int64 `gorm:"column:id" json:"id" form:"id"`
  433. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  434. Name string `gorm:"column:name" json:"name" form:"name"`
  435. Status int64 `gorm:"column:status" json:"status" form:"status"`
  436. }
  437. func (WeekSchedulePrintPatients) TableName() string {
  438. return "xt_patients"
  439. }
  440. type WeekSchedulePrintDeviceNumber struct {
  441. ID int64 `gorm:"column:id" json:"id"`
  442. OrgID int64 `gorm:"column:org_id" json:"-"`
  443. Number string `gorm:"column:number" json:"number"`
  444. GroupID int64 `gorm:"column:group_id" json:"group_id"`
  445. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  446. Status int64 `gorm:"column:status" json:"status" form:"status"`
  447. CreateTime int64 `gorm:"column:ctime" json:"-"`
  448. ModifyTime int64 `gorm:"column:mtime" json:"-"`
  449. Sort int64 `gorm:"column:sort" json:"sort" form:"sort"`
  450. }
  451. func (WeekSchedulePrintDeviceNumber) TableName() string {
  452. return "xt_device_number"
  453. }
  454. type WeekSchedulePrintVMTwo struct {
  455. ID int64 `gorm:"column:id" json:"id"`
  456. ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  457. DeviceNumberID int64 `gorm:"column:bed_id" json:"device_number_id"`
  458. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  459. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  460. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  461. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  462. ModeID int64 `gorm:"column:mode_id" json:"mode_id"`
  463. Patient *WeekSchedulePrintPatients `gorm:"ForeignKey:PatientID" json:"patient"`
  464. DeviceNumber *WeekSchedulePrintDeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  465. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  466. Mode *models.TreatmentMode `gorm:"ForeignKey:ModeID" json:"mode"`
  467. }
  468. type PrintCount struct {
  469. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  470. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  471. Count int64
  472. }
  473. func GetPrinitWeekSchedules(orgID int64, start, end int64) ([]*WeekSchedulePrintVMTwo, error) {
  474. var schedules []*WeekSchedulePrintVMTwo
  475. err := readDb.Table("xt_schedule as s").
  476. Preload("Patient", "status = 1 and user_org_id = ?", orgID).
  477. Preload("DeviceNumber", "status = 1 and org_id = ?", orgID).
  478. Preload("Zone", "status = 1 and org_id = ?", orgID).
  479. Preload("Mode", "status = 1").
  480. // Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  481. Where("s.user_org_id = ? and s.schedule_date >= ? and s.schedule_date <= ? and s.status = 1", orgID, start, end).
  482. // Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id").
  483. Find(&schedules).Error
  484. return schedules, err
  485. }
  486. func GetPrinitWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  487. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  488. if orgid > 0 {
  489. db = db.Where("x.user_org_id = ?", orgid)
  490. }
  491. if startime > 0 {
  492. db = db.Where("x.schedule_date >=?", startime)
  493. }
  494. if endtime > 0 {
  495. db = db.Where("x.schedule_date <=?", endtime)
  496. }
  497. err = db.Select("x.schedule_type,x.schedule_week,count(x.patient_id) as count").Group("x.schedule_date,x.schedule_type").Scan(&print).Error
  498. return print, err
  499. }
  500. func GetBloodPrintWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  501. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  502. if orgid > 0 {
  503. db = db.Where("x.user_org_id = ?", orgid)
  504. }
  505. if startime > 0 {
  506. db = db.Where("x.schedule_date >=?", startime)
  507. }
  508. if endtime > 0 {
  509. db = db.Where("x.schedule_date <=?", endtime)
  510. }
  511. db = db.Where("x.mode_id = 2")
  512. err = db.Select("x.schedule_type,x.schedule_week,count(x.patient_id) as count").Group("x.schedule_date,x.schedule_type").Scan(&print).Error
  513. return print, err
  514. }
  515. func GetPerfusionWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  516. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  517. if orgid > 0 {
  518. db = db.Where("x.user_org_id = ?", orgid)
  519. }
  520. if startime > 0 {
  521. db = db.Where("x.schedule_date >=?", startime)
  522. }
  523. if endtime > 0 {
  524. db = db.Where("x.schedule_date <=?", endtime)
  525. }
  526. db = db.Where("x.mode_id = 3")
  527. err = db.Select("x.schedule_type,x.schedule_week,count(x.patient_id) as count").Group("x.schedule_date,x.schedule_type").Scan(&print).Error
  528. return print, err
  529. }
  530. // func ExchangeScheduleTimeWithWeekSchedules(orgID int64, nextWeekSchs []*models.Schedule, nextTwoWeekSchs []*models.Schedule) error {
  531. // templateMode, getTemplateModeErr := GetOrgPatientScheduleTemplateMode(orgID)
  532. // if getTemplateModeErr != nil {
  533. // return getTemplateModeErr
  534. // }
  535. // if templateMode == nil {
  536. // return errors.New("模板模式不存在")
  537. // }
  538. // tx := writeDb.Begin()
  539. // updateTime := time.Now().Unix()
  540. // for _, sch := range nextWeekSchs {
  541. // sch.PatientInfectiousDiseases = nil
  542. // date := time.Unix(sch.ScheduleDate, 0)
  543. // sch.ScheduleDate = date.AddDate(0, 0, 7).Unix()
  544. // sch.UpdatedTime = updateTime
  545. // updateSchErr := tx.Save(sch).Error
  546. // if updateSchErr != nil {
  547. // tx.Rollback()
  548. // return updateSchErr
  549. // }
  550. // }
  551. // for _, sch := range nextTwoWeekSchs {
  552. // sch.PatientInfectiousDiseases = nil
  553. // date := time.Unix(sch.ScheduleDate, 0)
  554. // sch.ScheduleDate = date.AddDate(0, 0, -7).Unix()
  555. // sch.UpdatedTime = updateTime
  556. // updateSchErr := tx.Save(sch).Error
  557. // if updateSchErr != nil {
  558. // tx.Rollback()
  559. // return updateSchErr
  560. // }
  561. // }
  562. // templateMode.ModifyTime = updateTime
  563. // templateMode.ExecuteTimes += 1
  564. // updateModeErr := tx.Save(templateMode).Error
  565. // if updateModeErr != nil {
  566. // tx.Rollback()
  567. // return updateModeErr
  568. // }
  569. // tx.Commit()
  570. // return nil
  571. // }
  572. func GetSchedualPatientsByKeywordsAndWeek(keywords string, org_id int64, start_time int64, end_time int64) (schedule []*models.Schedule, err error) {
  573. likeKey := "%" + keywords + "%"
  574. err = readDb.
  575. Table("xt_schedule as s").
  576. Preload("PatientInfectiousDiseases", "status = 1 ").
  577. Preload("DeviceZone", "status = 1 ").
  578. Preload("DeviceNumber", "status = 1 ").
  579. Preload("TreatmentMode", "status = 1 ").
  580. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ? OR p.first_letter LIKE ?)", likeKey, likeKey, likeKey).
  581. Where("s.user_org_id=? and s.schedule_date >= ? and s.schedule_date <= ? and s.status=1", org_id, start_time, end_time).
  582. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time, p.name as patient").
  583. Order("schedule_date asc").
  584. Find(&schedule).Error
  585. return
  586. }
  587. func GetSchedualPatientsByKeywords(keywords string, org_id int64, startime int64, endtime int64) (schedule []*models.Schedule, err error) {
  588. likeKey := "%" + keywords + "%"
  589. err = readDb.
  590. Table("xt_schedule as s").
  591. Preload("PatientInfectiousDiseases", "status = 1 ").
  592. Preload("DeviceZone", "status = 1 ").
  593. Preload("DeviceNumber", "status = 1 ").
  594. Preload("TreatmentMode", "status = 1 ").
  595. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ? OR p.first_letter LIKE ?)", likeKey, likeKey, likeKey).
  596. Where("s.user_org_id=? and s.schedule_date >= ? and s.schedule_date <= ? and s.status=1", org_id, startime, endtime).
  597. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time, p.name as patient").
  598. Order("schedule_date asc").
  599. Find(&schedule).Error
  600. return
  601. }
  602. func GetWeekDayScheduleTwo(orgID int64, scheduleDate int64, scheduleDates *time.Time, weektime int64) (schedules []*models.WeekSchedule, err error) {
  603. db := readDb.Table("xt_schedule as s ").Where("s.status =1")
  604. if orgID > 0 {
  605. db = db.Where("s.user_org_id = ?", orgID)
  606. }
  607. if scheduleDate > 0 {
  608. db = db.Where("s.schedule_date = ?", scheduleDate)
  609. }
  610. if weektime > 0 {
  611. db = db.Where("s.schedule_type = ?", weektime)
  612. }
  613. err = db.
  614. Preload("DeviceZone", "status = 1 ").
  615. Preload("DeviceNumber", "status = 1 ").
  616. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  617. return db.Where("status = 1").Order("id asc")
  618. }).
  619. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  620. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time, p.name as patient").
  621. Order("s.partition_id asc").
  622. Find(&schedules).Error
  623. for _, item := range schedules {
  624. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  625. if config.IsOpenRemind > 0 {
  626. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  627. var advice_three []*models.DoctorAdvice
  628. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  629. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  630. for _, advice := range advices {
  631. advice.IsCheck = 1
  632. if advice.FrequencyType == 3 {
  633. week := int(scheduleDates.Weekday())
  634. switch week {
  635. case 1:
  636. if strings.Index(advice.WeekDay, "周一") > -1 {
  637. advice_three = append(advice_three, advice)
  638. }
  639. break
  640. case 2:
  641. if strings.Index(advice.WeekDay, "周二") > -1 {
  642. advice_three = append(advice_three, advice)
  643. }
  644. break
  645. case 3:
  646. if strings.Index(advice.WeekDay, "周三") > -1 {
  647. advice_three = append(advice_three, advice)
  648. }
  649. break
  650. case 4:
  651. if strings.Index(advice.WeekDay, "周四") > -1 {
  652. advice_three = append(advice_three, advice)
  653. }
  654. break
  655. case 5:
  656. if strings.Index(advice.WeekDay, "周五") > -1 {
  657. advice_three = append(advice_three, advice)
  658. }
  659. break
  660. case 6:
  661. if strings.Index(advice.WeekDay, "周六") > -1 {
  662. advice_three = append(advice_three, advice)
  663. }
  664. break
  665. case 0:
  666. if strings.Index(advice.WeekDay, "周日") > -1 {
  667. advice_three = append(advice_three, advice)
  668. }
  669. break
  670. }
  671. } else if advice.FrequencyType == 1 {
  672. advice_three = append(advice_three, advice)
  673. }
  674. }
  675. for _, advice := range advices_two {
  676. now := scheduleDates.Unix()
  677. dayStr := strconv.FormatInt(advice.DayCount, 10)
  678. dayStr2 := "-" + dayStr
  679. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  680. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  681. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  682. if len(advices) == 0 {
  683. advice_three = append(advice_three, advice)
  684. }
  685. }
  686. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  687. }
  688. }
  689. return
  690. }
  691. func GetWeekDaySchedule(orgID int64, scheduleDate int64, scheduleDates *time.Time) (schedules []*models.WeekSchedule, err error) {
  692. err = readDb.
  693. Table("xt_schedule as s").
  694. Preload("DeviceZone", "status = 1 ").
  695. Preload("DeviceNumber", "status = 1 ").
  696. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  697. return db.Where("status = 1").Order("id asc")
  698. }).
  699. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  700. Where("s.user_org_id=? and s.schedule_date = ? and s.status=1", orgID, scheduleDate).
  701. Select("s.id, s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time, p.name as patient").
  702. Order("schedule_type").
  703. Find(&schedules).Error
  704. for _, item := range schedules {
  705. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  706. if config.IsOpenRemind > 0 {
  707. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  708. var advice_three []*models.DoctorAdvice
  709. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  710. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  711. for _, advice := range advices {
  712. advice.IsCheck = 1
  713. if advice.FrequencyType == 3 {
  714. week := int(scheduleDates.Weekday())
  715. fmt.Println(advice.WeekDay)
  716. switch week {
  717. case 1:
  718. if strings.Index(advice.WeekDay, "周一") > -1 {
  719. advice_three = append(advice_three, advice)
  720. }
  721. break
  722. case 2:
  723. if strings.Index(advice.WeekDay, "周二") > -1 {
  724. advice_three = append(advice_three, advice)
  725. }
  726. break
  727. case 3:
  728. if strings.Index(advice.WeekDay, "周三") > -1 {
  729. advice_three = append(advice_three, advice)
  730. }
  731. break
  732. case 4:
  733. if strings.Index(advice.WeekDay, "周四") > -1 {
  734. advice_three = append(advice_three, advice)
  735. }
  736. break
  737. case 5:
  738. if strings.Index(advice.WeekDay, "周五") > -1 {
  739. advice_three = append(advice_three, advice)
  740. }
  741. break
  742. case 6:
  743. if strings.Index(advice.WeekDay, "周六") > -1 {
  744. advice_three = append(advice_three, advice)
  745. }
  746. break
  747. case 0:
  748. if strings.Index(advice.WeekDay, "周日") > -1 {
  749. advice_three = append(advice_three, advice)
  750. }
  751. break
  752. }
  753. } else if advice.FrequencyType == 1 {
  754. advice_three = append(advice_three, advice)
  755. }
  756. }
  757. for _, advice := range advices_two {
  758. now := scheduleDates.Unix()
  759. dayStr := strconv.FormatInt(advice.DayCount, 10)
  760. dayStr2 := "-" + dayStr
  761. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  762. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  763. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  764. if len(advices) == 0 {
  765. advice_three = append(advice_three, advice)
  766. }
  767. }
  768. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  769. }
  770. }
  771. return
  772. }
  773. func GetWeekDayScheduleById(orgid int64, scheduleDate int64, scheduleType int64, zone int64) (list []*models.VmSchedulesRemind, err error) {
  774. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  775. if orgid > 0 {
  776. db = db.Where("x.user_org_id = ?", orgid)
  777. }
  778. if scheduleDate > 0 {
  779. db = db.Where("x.schedule_date = ?", scheduleDate)
  780. }
  781. if scheduleType > 0 {
  782. db = db.Where("x.schedule_type = ?", scheduleType)
  783. }
  784. if zone > 0 {
  785. db = db.Where("x.partition_id = ?", zone)
  786. }
  787. err = db.Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,x.is_export").Preload("XtPatients", "status = 1").Preload("DeviceZone", "status = 1").Preload("DeviceNumber", "status = 1").Preload("DialysisOrder", "status = 1").Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  788. return db.Where("status =1 and parent_id = 0").Preload("ChildDoctorAdvice", "status = 1")
  789. }).Preload("HisDoctorAdviceInfo", "status = 1").Preload("DialysisPrescription", "status = 1").Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  790. return db.Where("status =1 and solution_status =1").Order("id asc")
  791. }).Find(&list).Error
  792. return list, err
  793. }
  794. func GetWeekDayScheduleByIdThee(orgid int64, scheduleDate int64, scheduleType int64, zone []string) (list []*models.VmSchedulesRemind, err error) {
  795. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  796. if orgid > 0 {
  797. db = db.Where("x.user_org_id = ?", orgid)
  798. }
  799. if scheduleDate > 0 {
  800. db = db.Where("x.schedule_date = ?", scheduleDate)
  801. }
  802. if scheduleType > 0 {
  803. db = db.Where("x.schedule_type = ?", scheduleType)
  804. }
  805. if len(zone) > 0 {
  806. db = db.Where("x.partition_id in (?)", zone)
  807. }
  808. err = db.Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,x.is_export").Preload("XtPatients", "status = 1").Preload("DeviceZone", "status = 1").Preload("DeviceNumber", "status = 1").Preload("DialysisOrder", "status = 1 and dialysis_date = ?", scheduleDate).Preload("XtAssessmentBeforeDislysis", "status = 1 and assessment_date =?", scheduleDate).Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  809. return db.Where("status =1 and parent_id = 0 and advice_date = ?", scheduleDate).Preload("ChildDoctorAdvice", "status = 1")
  810. }).Preload("HisDoctorAdviceInfo", "status = 1 and advice_date = ? and user_org_id = ? ", scheduleDate, orgid).Preload("DialysisPrescription", "status = 1 and record_date =? and user_org_id = ?", scheduleDate, orgid).Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  811. return db.Where("status =1 and solution_status = 1").Order("id asc")
  812. }).Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  813. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgid, scheduleDate)
  814. }).Find(&list).Error
  815. return list, err
  816. }
  817. func GetWeekDayScheduleByIdTwo(orgid int64, scheduleDate int64, scheduleType int64, startDate int64, zone []string) (list []*models.VmSchedulesRemind, err error) {
  818. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  819. if orgid > 0 {
  820. db = db.Where("x.user_org_id = ?", orgid)
  821. }
  822. if scheduleDate > 0 {
  823. db = db.Where("x.schedule_date >=? and x.schedule_date<=?", startDate, scheduleDate)
  824. }
  825. if scheduleType > 0 {
  826. db = db.Where("x.schedule_type = ?", scheduleType)
  827. }
  828. if len(zone) > 0 {
  829. db = db.Where("x.partition_id in (?)", zone)
  830. }
  831. err = db.Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,x.is_export,x.mode_id").Preload("XtPatients", "status = 1").Preload("DeviceZone", "status = 1").Preload("DeviceNumber", "status = 1").Preload("DialysisOrder", "status = 1 and dialysis_date =?", scheduleDate).Preload("XtAssessmentBeforeDislysis", "status = 1 and assessment_date =?", scheduleDate).Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  832. return db.Where("status =1 and parent_id = 0 and advice_date = ?", scheduleDate).Preload("ChildDoctorAdvice", "status = 1")
  833. }).Preload("HisDoctorAdviceInfo", "status = 1 and advice_date = ?", scheduleDate).Preload("DialysisPrescription", "status = 1 and record_date = ?", scheduleDate).Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  834. return db.Where("status =1 and solution_status =1").Order("id asc")
  835. }).Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  836. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgid, scheduleDate)
  837. }).Find(&list).Error
  838. return list, err
  839. }
  840. func RemoveRepeatedElement(arr []*models.DoctorAdvice) (newArr []*models.DoctorAdvice) {
  841. newArr = make([]*models.DoctorAdvice, 0)
  842. for i := 0; i < len(arr); i++ {
  843. repeat := false
  844. for j := i + 1; j < len(arr); j++ {
  845. if arr[i].ID == arr[j].ID {
  846. repeat = true
  847. break
  848. }
  849. }
  850. if !repeat {
  851. newArr = append(newArr, arr[i])
  852. }
  853. }
  854. return
  855. }
  856. func GetLastLongAdviceByGroupNoOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  857. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 1 AND stop_state = 2 AND parent_id = 0", orgId, patient_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  858. return
  859. }
  860. func GetLastLongAdviceByGroupNoThreeOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  861. 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
  862. return
  863. }
  864. func FindAllDoctorAdviceByTimeOther(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  865. err = readDb.Model(&models.DoctorAdvice{}).Where("status = 1 AND user_org_id = ? AND patient_id = ? AND advice_type = 2 AND frequency_type = 2 AND advice_date > ? AND advice_date < ? AND template_id = ? ", orgId, patient_id, end, now, temp_id).Preload("ChildDoctorAdvice", "status = 1").Find(&advice).Error
  866. return
  867. }
  868. func FindSchPatientNameByName(name string, user_org_id int64) (patient []*models.Patients, err error) {
  869. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? AND name = ? AND status = 1", user_org_id, name).Find(&patient).Error
  870. return
  871. }
  872. func UpdateScheduleStatus(date int64, org_id int64) (err error) {
  873. err = readDb.Model(&models.Schedule{}).Where("user_org_id = ? AND schedule_date > ? AND status = 1", org_id, date).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  874. return
  875. }
  876. func FindDeviceZoneByName(name string, org_id int64) (zone models.DeviceZone, err error) {
  877. err = readDb.Model(&models.DeviceZone{}).Where("org_id = ? AND name = ? AND status = 1", org_id, name).First(&zone).Error
  878. return
  879. }
  880. func FindDeviceByName(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  881. err = readDb.Model(&models.DeviceNumber{}).Where("org_id = ? AND number = ? AND zone_id = ? AND status = 1", org_id, device_name, zone_id).Find(&devices).Error
  882. return
  883. }
  884. func FindSchedualExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  885. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 2", org_id, export_time).Find(&errLogs).Error
  886. return
  887. }
  888. func FindDeviceByNameTwo(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  889. err = readDb.Model(&models.DeviceNumber{}).Where("org_id = ? AND number = ? AND status = 1 AND zone_id = ?", org_id, device_name, zone_id).Find(&devices).Error
  890. return
  891. }
  892. func UpdateScheduleTemplateStatus(org_id int64) (err error) {
  893. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND status = 1", org_id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  894. return
  895. }
  896. func CreateScheduleTemplate(m *models.PatientScheduleTemplateItem) error {
  897. return writeDb.Model(&models.PatientScheduleTemplateItem{}).Create(&m).Error
  898. }
  899. func ExportScheduleTemplate(m *models.PatientScheduleTemplateItem) (total int64) {
  900. readDb.Model(&models.PatientScheduleTemplateItem{}).Where("template_id = ? AND patient_id = ? AND weekday = ? AND org_id = ? and status =1", m.TemplateID, m.PatientID, m.Weekday, m.OrgID).Count(&total)
  901. return
  902. }
  903. func FindSchedualTemplateExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  904. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 3", org_id, export_time).Find(&errLogs).Error
  905. return
  906. }
  907. func GetDialysisOrderByBedId(orgID, start, bed_id int64, schedule_type int64) (*models.DialysisOrder, error) {
  908. order := models.DialysisOrder{}
  909. err = readDb.Model(&order).Where("user_org_id=? and dialysis_date=? and bed_id=? and status=1 AND schedual_type = ?", orgID, start, bed_id, schedule_type).Find(&order).Error
  910. if err == gorm.ErrRecordNotFound {
  911. return nil, err
  912. }
  913. if err != nil {
  914. return nil, err
  915. }
  916. return &order, nil
  917. }
  918. type VMDeviceNumber struct {
  919. ID int64 `gorm:"column:id" json:"id"`
  920. OrgID int64 `gorm:"column:org_id" json:"-"`
  921. Number string `gorm:"column:number" json:"number"`
  922. GroupID int64 `gorm:"column:group_id" json:"group_id"`
  923. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  924. Status int8 `json:"-"`
  925. CreateTime int64 `gorm:"column:ctime" json:"-"`
  926. ModifyTime int64 `gorm:"column:mtime" json:"-"`
  927. DeviceZone models.DeviceZone `json:"zone" gorm:"foreignkey:ID;AssociationForeignKey:ZoneID;"`
  928. }
  929. func (VMDeviceNumber) TableName() string {
  930. return "xt_device_number"
  931. }
  932. type VMPatientScheduleTemplateItem struct {
  933. ID int64 `gorm:"column:id" json:"id" form:"id"`
  934. OrgID int64 `gorm:"column:org_id" json:"-" form:"org_id"`
  935. TemplateID int64 `gorm:"column:template_id" json:"template_id" form:"template_id"`
  936. DeviceNumberID int64 `gorm:"column:device_number_id" json:"device_number_id" form:"device_number_id"`
  937. PatientID int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  938. TreatMode int64 `gorm:"column:treat_mode" json:"treat_mode" form:"treat_mode"` // 治疗模式
  939. Weekday int8 `gorm:"column :weekday" json:"weekday" form:"weekday"` // 周一-周日 1-7
  940. TimeType int8 `gorm:"column:time_type" json:"time_type" form:"time_type"` // 上下晚 123
  941. Status int8 `gorm:"column:status" json:"status" form:"status"`
  942. CreateTime int64 `gorm:"column:ctime" json:"-" form:"ctime"`
  943. ModifyTime int64 `gorm:"column:mtime" json:"-" form:"mtime"`
  944. IsExport int64 `gorm:"column:is_export" json:"is_export" form:"is_export"`
  945. Patient string `gorm:"-" json:"patient" form:"patient"`
  946. DeviceNumber VMDeviceNumber `json:"number" gorm:"foreignkey:ID;AssociationForeignKey:DeviceNumberID;"`
  947. PatientInfectiousDiseases []*models.InfectiousDiseases `json:"patient_contagions" gorm:"foreignkey:PatientId;AssociationForeignKey:PatientId;"`
  948. }
  949. func (VMPatientScheduleTemplateItem) TableName() string {
  950. return "xt_patient_schedule_template_item"
  951. }
  952. func GetTemplateSchedualPatientsByKeywords(keywords string, org_id int64, template_id int64) (schedule []*VMPatientScheduleTemplateItem, err error) {
  953. likeKey := "%" + keywords + "%"
  954. err = readDb.
  955. Table("xt_patient_schedule_template_item as s").
  956. Preload("PatientInfectiousDiseases", "status = 1 ").
  957. Preload("DeviceNumber", func(db *gorm.DB) *gorm.DB {
  958. return db.Where("org_id = ? AND status = 1", org_id).Preload("DeviceZone", "org_id = ? AND status = 1", org_id)
  959. }).
  960. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  961. Where("s.template_id = ? AND s.org_id=? and s.status=1", template_id, org_id).
  962. Select("s.id, s.org_id, s.template_id, s.device_number_id, s.patient_id, s.treat_mode, s.weekday, s.time_type, s.status, s.ctime, s.mtime, p.name as patient").
  963. Order("weekday asc").
  964. Find(&schedule).Error
  965. return
  966. }
  967. func UpdateNewScheduleTemplateStatus(id int64, org_id int64) (err error) {
  968. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND id = ?", org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  969. return
  970. }
  971. func GetNextWeekDaySchedule(weektype int64, weektime int64, startime int64, endtime int64, orgID int64, zone []string, record_date int64) (schedules []*models.WeekScheduleSix, err error) {
  972. db := XTReadDB().Table("xt_schedule as s ").Where("s.status =1")
  973. if orgID > 0 {
  974. db = db.Where("s.user_org_id = ?", orgID)
  975. }
  976. if weektype == 0 {
  977. if startime > 0 {
  978. db = db.Where("s.schedule_date >= ?", startime)
  979. }
  980. if endtime > 0 {
  981. db = db.Where("s.schedule_date <= ?", endtime)
  982. }
  983. }
  984. if weektype > 0 {
  985. if record_date > 0 {
  986. db = db.Where("s.schedule_date = ?", record_date)
  987. }
  988. }
  989. if weektime > 0 {
  990. db = db.Where("s.schedule_type = ?", weektime)
  991. }
  992. if weektype > 0 {
  993. db = db.Where("s.schedule_week = ?", weektype)
  994. }
  995. if len(zone) > 0 {
  996. db = db.Where("s.partition_id in (?)", zone)
  997. }
  998. err = db.
  999. Preload("DeviceZone", "status = 1 ").
  1000. Preload("DeviceNumber", "status = 1 ").Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  1001. Select("s.id,s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time, p.name as patient,p.id_card_no as id_card_no,p.dialysis_no as dialysis_no,p.gender as gender,p.phone as phone").
  1002. Order("s.partition_id desc").
  1003. Find(&schedules).Error
  1004. return schedules, err
  1005. }
  1006. func GetThreeWeekList(startime int64, endtime int64, orgid int64, scheduletype int64, partionid int64) (schedule []*models.VmSchedule, err error) {
  1007. db := XTReadDB().Table("xt_schedule as x").Where("x.status =1")
  1008. if startime > 0 {
  1009. db = db.Where("x.schedule_date >=?", startime)
  1010. }
  1011. if endtime > 0 {
  1012. db = db.Where("x.schedule_date <=?", endtime)
  1013. }
  1014. if orgid > 0 {
  1015. db = db.Where("x.user_org_id = ?", orgid)
  1016. }
  1017. if scheduletype > 0 {
  1018. db = db.Where("x.schedule_type = ?", scheduletype)
  1019. }
  1020. if partionid > 0 {
  1021. db = db.Where("x.partition_id = ?", partionid)
  1022. }
  1023. err = db.Select("x.schedule_date,x.schedule_type,x.schedule_week,x.bed_id,x.partition_id,t.name,n.number,z.name,x.dialysis_machine_name,x.mode_id").Joins("left join xt_patients as t on t.id = x.patient_id and t.user_org_id = ? and t.status = 1", orgid).Joins("left join xt_device_zone as z on z.id = x.partition_id and z.org_id = ? and z.status =1", orgid).Joins("left join xt_device_number as n on n.id = x.bed_id and n.org_id = ? and n.status =1", orgid).Scan(&schedule).Error
  1024. return schedule, err
  1025. }
  1026. func UpdatedDialysisPrescription(prescription *models.DialysisPrescription, recordDate int64, patientid int64, orgid int64) error {
  1027. err := XTWriteDB().Model(&prescription).Where("record_date = ? and patient_id = ? and user_org_id = ?", recordDate, patientid, orgid).Update(map[string]interface{}{"mode_id": prescription.ModeId, "updated_time": time.Now().Unix()}).Error
  1028. return err
  1029. }
  1030. func GetWeekSchedules(orgid int64, startime int64, endtime int64) (scheudle []*models.XtSchedule, err error) {
  1031. err = XTReadDB().Model(&scheudle).Where("user_org_id = ? and status = 1 and schedule_date>=? and schedule_date<=?", orgid, startime, endtime).Find(&scheudle).Error
  1032. return scheudle, err
  1033. }
  1034. func GetLastWeekSchedule(orgid int64, startime int64, endtime int64) (*models.XtSchedule, error) {
  1035. schedule := models.XtSchedule{}
  1036. err := XTReadDB().Where("user_org_id = ? and status =1 and schedule_date>=? and schedule_date<=?", orgid, startime, endtime).Find(&schedule).Error
  1037. if err == gorm.ErrRecordNotFound {
  1038. return nil, err
  1039. }
  1040. if err != nil {
  1041. return nil, err
  1042. }
  1043. return &schedule, nil
  1044. }
  1045. func AddPatientSchedule(schedule *models.XtSchedule) error {
  1046. utx := XTWriteDB().Begin()
  1047. err := utx.Create(&schedule).Error
  1048. utx.Commit()
  1049. return err
  1050. }
  1051. func UpdatePatientSchedule(orgid int64, startime int64, endtime int64) error {
  1052. schedule := models.XtSchedule{}
  1053. err := XTWriteDB().Model(&schedule).Where("user_org_id = ? and schedule_date>=? and schedule_date<=? and status =1", orgid, startime, endtime).Updates(map[string]interface{}{"status": 0}).Error
  1054. return err
  1055. }
  1056. func GetSettingById(id int64) (*models.XtRemindPrintSetting, error) {
  1057. setting := models.XtRemindPrintSetting{}
  1058. err := XTReadDB().Model(&setting).Where("id = ? and status = 1", id).Find(&setting).Error
  1059. if err == gorm.ErrRecordNotFound {
  1060. return nil, err
  1061. }
  1062. if err != nil {
  1063. return nil, err
  1064. }
  1065. return &setting, nil
  1066. }
  1067. func CreateSetting(setting *models.XtRemindPrintSetting) error {
  1068. err := XTWriteDB().Create(&setting).Error
  1069. return err
  1070. }
  1071. func UpdatedRemindPrint(set *models.XtRemindPrintSetting, id int64) error {
  1072. err := XTWriteDB().Model(&set).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"prescription_status": set.PrescriptionStatus, "week": set.Week, "name": set.Name, "zone": set.Zone, "classes": set.Classes, "number": set.Number, "mode": set.Mode, "dialyzers": set.Dialyzers, "perfusion_apparatus": set.PerfusionApparatus, "anticoagulant": set.Anticoagulant, "doctor_advice": set.DoctorAdvice, "anticoagulant_zongliang": set.AnticoagulantZongliang}).Error
  1073. return err
  1074. }
  1075. func GetRemindPrintList(id int64) (models.XtRemindPrintSetting, error) {
  1076. setting := models.XtRemindPrintSetting{}
  1077. err := XTReadDB().Model(&setting).Where("user_org_id = ? and status =1", id).Find(&setting).Error
  1078. return setting, err
  1079. }
  1080. func GetScheudleTemplateById(orgid int64) (*models.ScheduleTemplate, error) {
  1081. schedule := models.ScheduleTemplate{}
  1082. err := XTReadDB().Model(&schedule).Where("user_org_id = ? and status = 1", orgid).Find(&schedule).Error
  1083. if err == gorm.ErrRecordNotFound {
  1084. return nil, err
  1085. }
  1086. if err != nil {
  1087. return nil, err
  1088. }
  1089. return &schedule, nil
  1090. }
  1091. func CreateXtscheduleTemplate(template *models.ScheduleTemplate) error {
  1092. err := XTWriteDB().Create(&template).Error
  1093. return err
  1094. }
  1095. func UpdateXtScheduleTemplate(template *models.ScheduleTemplate, orgid int64) error {
  1096. err := XTWriteDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"template_id": template.TemplateId}).Error
  1097. return err
  1098. }
  1099. func GetScheduleTemplate(orgid int64) (models.ScheduleTemplate, error) {
  1100. template := models.ScheduleTemplate{}
  1101. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  1102. return template, err
  1103. }
  1104. func GetDataPrintByOrgId(orgid int64) (*models.XtDataPrint, error) {
  1105. dataPrint := models.XtDataPrint{}
  1106. err := XTReadDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Find(&dataPrint).Error
  1107. if err == gorm.ErrRecordNotFound {
  1108. return nil, err
  1109. }
  1110. if err != nil {
  1111. return nil, err
  1112. }
  1113. return &dataPrint, nil
  1114. }
  1115. func CreateDataPrint(dataPrint *models.XtDataPrint) error {
  1116. err := XTWriteDB().Create(&dataPrint).Error
  1117. return err
  1118. }
  1119. func UpdteDataPrint(orgid int64, dataPrint *models.XtDataPrint) error {
  1120. err := XTWriteDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"is_open": dataPrint.IsOpen}).Error
  1121. return err
  1122. }
  1123. func GetDataPrint(orgid int64) (models.XtDataPrint, error) {
  1124. dataPrint := models.XtDataPrint{}
  1125. err := XTReadDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Find(&dataPrint).Error
  1126. return dataPrint, err
  1127. }
  1128. func GetNextWeekPanels(startime int64, endtime int64, orgid int64) (schedule []*models.XtSchedule, err error) {
  1129. err = XTReadDB().Model(&schedule).Where("schedule_date >=? and schedule_date<=? and status = 1 and user_org_id = ?", startime, endtime, orgid).Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  1130. return db.Where("user_org_id = ? and status = 1 and assessment_date>=1672502400 and assessment_date < ?", orgid, endtime)
  1131. }).Find(&schedule).Error
  1132. return schedule, err
  1133. }
  1134. func GetTemplateListByTemplateId(id int64, orgid int64) (list []*models.VmPatientScheduleTemplateItem, err error) {
  1135. err = XTReadDB().Where("template_id =? and org_id = ? and status = 1", id, orgid).Find(&list).Error
  1136. return list, err
  1137. }
  1138. func DeletePatientSchedule(startime int64, endtime int64, org_id int64) (models.XtSchedule, error) {
  1139. schedule := models.XtSchedule{}
  1140. err := XTWriteDB().Model(&schedule).Where("schedule_date>=? and schedule_date<=? and user_org_id = ? and status = 1", startime, endtime, org_id).Updates(map[string]interface{}{"status": 0}).Error
  1141. return schedule, err
  1142. }
  1143. func DeletePatientScheduleToday(startime int64, endtime int64, org_id int64) (models.XtSchedule, error) {
  1144. schedule := models.XtSchedule{}
  1145. err := XTWriteDB().Model(&schedule).Where("schedule_date>? and schedule_date<=? and user_org_id = ? and status = 1", startime, endtime, org_id).Updates(map[string]interface{}{"status": 0}).Error
  1146. return schedule, err
  1147. }
  1148. func GetBedNumberById(id int64, org_id int64) (models.DeviceNumber, error) {
  1149. number := models.DeviceNumber{}
  1150. err := XTReadDB().Where("id = ? and status = 1 and org_id =?", id, org_id).Find(&number).Error
  1151. return number, err
  1152. }
  1153. func CreatePatientSchedule(schedule *models.XtSchedule) error {
  1154. utx := XTWriteDB().Begin()
  1155. err := utx.Create(&schedule).Error
  1156. utx.Commit()
  1157. return err
  1158. }
  1159. func GetPatientScheduleTempalate(orgid int64) (models.PatientScheduleTemplateMode, error) {
  1160. mode := models.PatientScheduleTemplateMode{}
  1161. err := XTReadDB().Where("org_id = ? and status =1", orgid).Find(&mode).Error
  1162. return mode, err
  1163. }
  1164. func GetSolutionSchedule(orgid int64) (solution []*models.VmDialysisSolution, err error) {
  1165. err = XTReadDB().Where("user_org_id = ? and status = 1", orgid).Group("patient_id,mode_id").Order("created_time desc").Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  1166. return db.Where("user_org_id = ? and status = 1", orgid).Order("id asc")
  1167. }).Find(&solution).Error
  1168. return solution, err
  1169. }
  1170. func GetSolutionScheduleBySchedule(orgid int64, zonid int64, classtype int64, schedule_date int64) (schedule []*models.XtScheduleList, err error) {
  1171. db := XTReadDB().Model(&schedule).Where("status = 1")
  1172. if orgid > 0 {
  1173. db = db.Where("user_org_id =?", orgid)
  1174. }
  1175. if zonid > 0 {
  1176. db = db.Where("partition_id = ?", zonid)
  1177. }
  1178. if classtype > 0 {
  1179. db = db.Where("schedule_type = ?", classtype)
  1180. }
  1181. if schedule_date > 0 {
  1182. db = db.Where("schedule_date = ?", schedule_date)
  1183. }
  1184. err = db.Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  1185. return db.Where("user_org_id = ? and status = 1", orgid).Group("patient_id,mode_id").Order("created_time asc").Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  1186. return db.Where("user_org_id = ? and status = 1", orgid).Order("id asc")
  1187. })
  1188. }).Find(&schedule).Error
  1189. return schedule, err
  1190. }
  1191. func GetDeathPatient(orgid int64) (patient []*models.XtPatients, err error) {
  1192. err = XTReadDB().Where("user_org_id = ? and (lapseto = 2 || lapseto = 3) and status = 1", orgid).Find(&patient).Error
  1193. return patient, err
  1194. }
  1195. func GetInitPrintData(patient_id int64, scheduleDate int64, orgid int64) (list []*models.VmSchedulesRemind, err error) {
  1196. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  1197. if orgid > 0 {
  1198. db = db.Where("x.user_org_id = ?", orgid)
  1199. }
  1200. if patient_id > 0 {
  1201. db = db.Where("x.patient_id = ?", patient_id)
  1202. }
  1203. if scheduleDate > 0 {
  1204. db = db.Where("x.schedule_date >=? and x.schedule_date<=?", scheduleDate, scheduleDate)
  1205. }
  1206. err = db.Select("x.id,x.user_org_id,x.partition_id,x.bed_id,x.patient_id,x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,x.is_export").Preload("XtPatients", "status = 1").Preload("DeviceZone", "status = 1").Preload("DeviceNumber", "status = 1").Preload("XtAssessmentBeforeDislysis", "status = 1 and assessment_date =? and patient_id = ?", scheduleDate, patient_id).Preload("DialysisPrescription", "status = 1 and record_date = ? and patient_id = ?", scheduleDate, patient_id).Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  1207. return db.Where("status =1 and patient_id = ?", patient_id).Order("id asc")
  1208. }).Find(&list).Error
  1209. return list, err
  1210. }
  1211. func GetLastWeightAfter(patient_id int64, scheduleDate int64, orgid int64) (models.AssessmentAfterDislysis, error) {
  1212. dislysis := models.AssessmentAfterDislysis{}
  1213. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and assessment_date < ? and status = 1", orgid, patient_id, scheduleDate).Order("id desc").First(&dislysis).Error
  1214. return dislysis, err
  1215. }
  1216. func GetScheduleTwo(orgID, id int64) (models.Schedule, error) {
  1217. var schedule models.Schedule
  1218. var err error
  1219. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  1220. return schedule, err
  1221. }
  1222. func UpdateSchDName(sch models.Schedule) (err error) {
  1223. err = writeDb.Model(&models.Schedule{}).Where("status=1 AND id = ?", sch.ID).Updates(map[string]interface{}{"dialysis_machine_name": sch.DialysisMachineName}).Error
  1224. return
  1225. }
  1226. func GetDialysisOrderByBedIdTwo(orgID, start, bed_id int64, schedule_type int64) (models.DialysisOrder, error) {
  1227. order := models.DialysisOrder{}
  1228. err = readDb.Model(&order).Where("user_org_id=? and dialysis_date=? and bed_id=? and status=1 AND schedual_type = ?", orgID, start, bed_id, schedule_type).Find(&order).Error
  1229. return order, nil
  1230. }
  1231. func ChangeScheduleClass(schedule models.Schedule) error {
  1232. err := XTWriteDB().Model(&schedule).Where("patient_id = ? and schedule_date = ? and user_org_id = ? and status = 1", schedule.PatientId, schedule.ScheduleDate, schedule.UserOrgId).Update(map[string]interface{}{"schedule_type": schedule.ScheduleType}).Error
  1233. return err
  1234. }
  1235. func GetLastDialysisOrder(org_id int64, patient_id int64, dialysis_date int64) (models.XtDialysisOrderSix, error) {
  1236. order := models.XtDialysisOrderSix{}
  1237. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and dialysis_date < ? and status = 1", org_id, patient_id, dialysis_date).Order("id desc").First(&order).Error
  1238. return order, err
  1239. }
  1240. func GetLastDialysisSolution(org_id int64, patient_id int64, mode_id int64, schedule_date int64) (models.NewDialysisPrescription, error) {
  1241. dialysisprescription := models.NewDialysisPrescription{}
  1242. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and mode_id = ? and status = 1 and record_date>=1672502400 and record_date<?", org_id, patient_id, mode_id, schedule_date).Order("id desc").First(&dialysisprescription).Error
  1243. return dialysisprescription, err
  1244. }
  1245. func GetDoctorAdviceLastWeek(org_id int64, patient_id int64, schedule_date int64) (advice []*models.DoctorAdvice, err error) {
  1246. err = XTReadDB().Where("user_org_id = ? and patient_id = ? and status= 1 and parent_id = 0 and advice_date = ?", org_id, patient_id, schedule_date).Preload("ChildDoctorAdvice", "status = 1 and advice_date = ?", schedule_date).Find(&advice).Error
  1247. return advice, err
  1248. }
  1249. func GetHisDoctorAdviceListLastWeek(org_id int64, patient_id int64, schedule_date int64) (advice []*models.HisDoctorAdviceInfo, err error) {
  1250. err = XTReadDB().Where("user_org_id = ? and patient_id = ? and advice_date = ? and status= 1", org_id, patient_id, schedule_date).Find(&advice).Error
  1251. return advice, err
  1252. }
  1253. func GetDialysisSolutionLastWeek(user_org_id int64, patient_id int64, mode_id int64) (models.DialysisSolution, error) {
  1254. solution := models.DialysisSolution{}
  1255. err := XTReadDB().Where("user_org_id =? and status = 1 and patient_id = ? and mode_id = ? and solution_status = 1", user_org_id, patient_id, mode_id).Find(&solution).Error
  1256. return solution, err
  1257. }
  1258. func GetNextWeekDayScheduleSix(weektype int64, weektime int64, startime int64, endtime int64, orgID int64, zone []string, record_date int64) (schedules []*models.WeekScheduleSeven, err error) {
  1259. db := XTReadDB().Table("xt_schedule as s ").Where("s.status =1")
  1260. if orgID > 0 {
  1261. db = db.Where("s.user_org_id = ?", orgID)
  1262. }
  1263. if weektype == 0 {
  1264. if startime > 0 {
  1265. db = db.Where("s.schedule_date >= ?", startime)
  1266. }
  1267. if endtime > 0 {
  1268. db = db.Where("s.schedule_date <= ?", endtime)
  1269. }
  1270. }
  1271. if weektype > 0 {
  1272. if record_date > 0 {
  1273. db = db.Where("s.schedule_date = ?", record_date)
  1274. }
  1275. }
  1276. if weektime > 0 {
  1277. db = db.Where("s.schedule_type = ?", weektime)
  1278. }
  1279. if weektype > 0 {
  1280. db = db.Where("s.schedule_week = ?", weektype)
  1281. }
  1282. if len(zone) > 0 {
  1283. db = db.Where("s.partition_id in (?)", zone)
  1284. }
  1285. err = db.
  1286. Preload("DeviceZone", "status = 1 ").
  1287. Preload("DeviceNumber", "status = 1 ").
  1288. Preload("XtAssessmentBeforeDislysis", func(db *gorm.DB) *gorm.DB {
  1289. return db.Where("status = 1 and assessment_date = ?", record_date)
  1290. }).Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  1291. Select("s.id,s.user_org_id, s.partition_id, s.bed_id, s.patient_id, s.schedule_date, s.schedule_type, s.schedule_week, s.mode_id, s.status, s.created_time, s.updated_time, p.name as patient,p.id_card_no as id_card_no,p.dialysis_no as dialysis_no,p.gender as gender,p.phone as phone").
  1292. Order("s.partition_id desc").
  1293. Find(&schedules).Error
  1294. return schedules, err
  1295. }
  1296. func GetAssessmentBeforListLastWeek(schedule_date int64, patient_id int64, org_id int64) (models.XtAssessmentBeforeDislysis, error) {
  1297. dislysis := models.XtAssessmentBeforeDislysis{}
  1298. err := XTReadDB().Where("assessment_date = ? and patient_id = ? and user_org_id =? and status = 1", schedule_date, patient_id, org_id).Find(&dislysis).Error
  1299. return dislysis, err
  1300. }
  1301. func GetLastDialysisSolutionType(org_id int64, patient_id int64, mode_id int64) (models.NewDialysisSolutionOne, error) {
  1302. dialysisprescription := models.NewDialysisSolutionOne{}
  1303. err := XTReadDB().Where("user_org_id = ? and patient_id = ? and mode_id = ? and status = 1 and solution_status =1", org_id, patient_id, mode_id).First(&dialysisprescription).Error
  1304. return dialysisprescription, err
  1305. }
  1306. func GetScheduleTen(orgID, id int64) (models.Schedule, error) {
  1307. var schedule models.Schedule
  1308. var err error
  1309. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  1310. return schedule, err
  1311. }
  1312. func GetDialysisOrderByPatientAndBed(org_id int64, record_date int64, bed_id int64, patient_id int64) (order models.DialysisOrder, err error) {
  1313. err = readDb.Model(&models.DialysisOrder{}).Where("user_org_id = ? AND status = 1 AND dialysis_date = ? AND bed_id = ? AND patient_id = ?", org_id, record_date, bed_id, patient_id).First(&order).Error
  1314. return
  1315. }
  1316. func GetScheduleTemplateTen(orgID, id int64) (models.PatientScheduleTemplateItem, error) {
  1317. var schedule models.PatientScheduleTemplateItem
  1318. var err error
  1319. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  1320. return schedule, err
  1321. }
  1322. func GetScheduleTemplateCountByDate(org_id int64, week_day int8, patient_id int64, template_id int64) (total int64, err error) {
  1323. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where(" org_id = ? AND weekday = ? AND status = 1 AND patient_id = ? AND template_id = ?", org_id, week_day, patient_id, template_id).Count(&total).Error
  1324. return
  1325. }
  1326. func SaveSchTemplateTwo(sch models.PatientScheduleTemplateItem, sch_two models.PatientScheduleTemplateItem, new_sch models.PatientScheduleTemplateItem) (err error) {
  1327. utx := writeDb.Begin()
  1328. if err = utx.Model(&models.PatientScheduleTemplateItem{}).Where("status=1 AND id = ?", sch.ID).Updates(map[string]interface{}{"status": 0}).Error; err != nil {
  1329. utx.Rollback()
  1330. return err
  1331. }
  1332. if err = utx.Model(&models.PatientScheduleTemplateItem{}).Where("status=1 AND id = ?", sch_two.ID).Updates(map[string]interface{}{"status": 0}).Error; err != nil {
  1333. utx.Rollback()
  1334. return err
  1335. }
  1336. if err = utx.Save(&new_sch).Error; err != nil {
  1337. utx.Rollback()
  1338. return err
  1339. }
  1340. utx.Commit()
  1341. return err
  1342. }
  1343. func SaveSchTemplate(sch models.PatientScheduleTemplateItem, sch_two models.PatientScheduleTemplateItem) (err error) {
  1344. utx := writeDb.Begin()
  1345. if err = utx.Save(&sch).Error; err != nil {
  1346. utx.Rollback()
  1347. return err
  1348. }
  1349. if err = utx.Save(&sch_two).Error; err != nil {
  1350. utx.Rollback()
  1351. return err
  1352. }
  1353. utx.Commit()
  1354. return err
  1355. }
  1356. func UpdateScheduleTemplateThree(schone models.PatientScheduleTemplateItem, schtwo models.PatientScheduleTemplateItem) error {
  1357. utx := XTWriteDB().Begin()
  1358. //床位
  1359. var tempBedID int64
  1360. var tempBedIDTwo int64
  1361. //上下午
  1362. var tempTypeID int8
  1363. var tempTypeTwo int8
  1364. //周数
  1365. var sch_week_one int8
  1366. var sch_week_two int8
  1367. tempBedID = schone.DeviceNumberID
  1368. tempBedIDTwo = schtwo.DeviceNumberID
  1369. tempTypeID = schone.TimeType
  1370. tempTypeTwo = schtwo.TimeType
  1371. sch_week_one = schone.Weekday
  1372. sch_week_two = schtwo.Weekday
  1373. schone.DeviceNumberID = tempBedIDTwo
  1374. schtwo.DeviceNumberID = tempBedID
  1375. schone.TimeType = tempTypeTwo
  1376. schtwo.TimeType = tempTypeID
  1377. schone.Weekday = sch_week_two
  1378. schtwo.Weekday = sch_week_one
  1379. if err = utx.Model(&models.PatientScheduleTemplateItem{}).Where("id = ?", schone.ID).Updates(map[string]interface{}{"device_number_id": schone.DeviceNumberID, "time_type": schone.TimeType, "weekday": schone.Weekday, "is_export": 70000}).Error; err != nil {
  1380. utx.Rollback()
  1381. return err
  1382. }
  1383. if err = utx.Model(&models.PatientScheduleTemplateItem{}).Where("id = ?", schtwo.ID).Updates(map[string]interface{}{"device_number_id": schtwo.DeviceNumberID, "time_type": schtwo.TimeType, "weekday": schtwo.Weekday, "is_export": 70000}).Error; err != nil {
  1384. utx.Rollback()
  1385. return err
  1386. }
  1387. utx.Commit()
  1388. return err
  1389. }
  1390. func GetScheduleTemplateForDeviceNumberTwo(orgID int64, deviceNumberID int64, template_id int64, week_type int64, time_type int64, patient_id int64) (models.PatientScheduleTemplateItem, error) {
  1391. var item models.PatientScheduleTemplateItem
  1392. err := readDb.Model(&models.PatientScheduleTemplateItem{}).Where("device_number_id = ? AND org_id = ? AND status = 1 AND template_id = ? AND weekday = ? AND time_type = ? AND patient_id = ?", deviceNumberID, orgID, template_id, week_type, time_type, patient_id).First(&item).Error
  1393. return item, err
  1394. }
  1395. func ClearSchedules(user_org_id int64, start_time int64) (err error) {
  1396. err = writeDb.Table("xt_schedule as s").
  1397. Where("s.schedule_date > ? AND s.status = 1 AND "+
  1398. "s.user_org_id = ?", start_time, user_org_id).
  1399. Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  1400. return
  1401. }
  1402. func CreateScheduleTwo(m *models.Schedule) error {
  1403. utx := XTWriteDB().Begin()
  1404. err = utx.Create(&m).Error
  1405. if err != nil {
  1406. utx.Rollback()
  1407. return err
  1408. }
  1409. utx.Commit()
  1410. return err
  1411. }
  1412. func GetLongSolutionByModeId(patient_id int64, mode_id int64, user_org_id int64) (models.DialysisSolution, error) {
  1413. solution := models.DialysisSolution{}
  1414. err := XTReadDB().Where("patient_id = ? and mode_id = ? and user_org_id = ? and status= 1 and solution_status =1", patient_id, mode_id, user_org_id).Find(&solution).Error
  1415. return solution, err
  1416. }
  1417. func UpdateScheduleByDialysis(patient_id int64, mode_id int64, schedule_date int64, user_org_id int64, dialyzer_perfusion_apparatus string, schedule_type int64) error {
  1418. schedule := models.XtSchedule{}
  1419. err := XTWriteDB().Model(&schedule).Where("patient_id = ? and mode_id = ? and schedule_date = ? and user_org_id = ? and schedule_type = ?", patient_id, mode_id, schedule_date, user_org_id, schedule_type).Updates(map[string]interface{}{"dialysis_machine_name": dialyzer_perfusion_apparatus}).Error
  1420. return err
  1421. }
  1422. func GetScheduleByBedId(bed_id int64, schedule_type int64, user_org_id int64, schedule_date int64) (models.XtSchedule, error) {
  1423. schedule := models.XtSchedule{}
  1424. err := XTReadDB().Model(&schedule).Where("bed_id = ? and schedule_type =? and user_org_id = ? and schedule_date = ? and status =1", bed_id, schedule_type, user_org_id, schedule_date).Find(&schedule).Error
  1425. return schedule, err
  1426. }
  1427. func UpdateScheduleByScheduleId(id int64) error {
  1428. err := XTWriteDB().Model(&models.XtSchedule{}).Where("id = ? and status = 1", id).Updates(map[string]interface{}{"status": 0}).Error
  1429. return err
  1430. }
  1431. func UpdateScheduleSixTy(id int64, schedules models.XtSchedule) error {
  1432. err := XTWriteDB().Model(&models.XtSchedule{}).Where("id = ? and status =1", id).Updates(map[string]interface{}{"mode_id": schedules.ModeId, "partition_id": schedules.PartitionId, "bed_id": schedules.BedId, "patient_id": schedules.PatientId, "schedule_date": schedules.ScheduleDate, "schedule_type": schedules.ScheduleType, "schedule_week": schedules.ScheduleWeek}).Error
  1433. return err
  1434. }
  1435. func CreateNewSchedule(schedule models.XtSchedule) error {
  1436. err := XTWriteDB().Create(&schedule).Error
  1437. return err
  1438. }
  1439. func GetNumberListById(id int64, org_id int64) (number []*models.DeviceNumber, err error) {
  1440. err = XTReadDB().Where("zone_id = ? and org_id = ? and status= 1", id, org_id).Find(&number).Error
  1441. return number, err
  1442. }
  1443. func CreateScheduleLog(log models.XtScheduleLog) error {
  1444. err := XTWriteDB().Create(&log).Error
  1445. return err
  1446. }
  1447. func GetDoctorAdviceListTwenty(patient_id int64, schedule_date int64, user_org_id int64) (advice []*models.XtDoctorAdvice, err error) {
  1448. err = XTReadDB().Where("patient_id = ? and advice_date = ? and user_org_id = ? and status=1", patient_id, schedule_date, user_org_id).Find(&advice).Error
  1449. return advice, err
  1450. }
  1451. func UpdateAdviceObj(patient_id int64, schedule_date int64, user_org_id int64) (err error) {
  1452. err = XTWriteDB().Model(&models.XtDoctorAdvice{}).Where("patient_id = ? and advice_date =? and status=1 and user_org_id = ?", patient_id, schedule_date, user_org_id).Updates(map[string]interface{}{"status": 9}).Error
  1453. return err
  1454. }
  1455. func GetLastSchedule(patient_id int64, user_org_id int64) (models.XtSchedule, error) {
  1456. schedule := models.XtSchedule{}
  1457. err := XTReadDB().Where("patient_id = ? and status =1 and user_org_id = ?", patient_id, user_org_id).Find(&schedule).Error
  1458. return schedule, err
  1459. }
  1460. func UpdateRepeatSchStatus(user_org_id int64, schedule_date int64) {
  1461. var schedules []models.XtSchedule
  1462. // Find duplicate records based on patient_id, schedule_date, and user_org_id
  1463. XTReadDB().Where("user_org_id = ? AND schedule_date = ? AND status = ?", user_org_id, schedule_date, 1).
  1464. Group("patient_id, schedule_date").
  1465. Having("COUNT(*) > 1").
  1466. Select("patient_id, schedule_date, MAX(id) as max_id").
  1467. Find(&schedules)
  1468. for _, schedule := range schedules {
  1469. // Update records with status = 0 where id is less than max_id
  1470. XTReadDB().Model(&models.XtSchedule{}).
  1471. Where("patient_id = ? AND schedule_date = ? AND id < ?", schedule.PatientId, schedule.ScheduleDate, schedule.MaxId).
  1472. Update("status", 0)
  1473. }
  1474. }