schedule_service.go 48KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163
  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 int64) (partitions []*models.Partition, err error) {
  16. if partionId > 0 {
  17. err = readDb.Model(&models.DeviceZone{}).Preload("Jihaos", "org_id=? and status=1 and zone_id = ?", orgID, partionId).Where("org_id=? and status=1 and id = ?", orgID, partionId).Find(&partitions).Error
  18. }
  19. if 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", "org_id=? and status=1 and zone_id in(?)", orgID, schIDs).Where("org_id=? and status=1 and id in(?)", orgID, schIDs).Find(&partitions).Error
  30. }
  31. return
  32. }
  33. func GetPatientWithScheduleAndSolution(orgID int64, keywords string, weekStart, weekEnd, schedule, contagion int64) (patients []*models.Patients, err error) {
  34. db := readDb.Table("xt_patients as p").
  35. Preload("DialysisSolutions", "user_org_id=? and initiate_mode=1 and affirm_state=1 and parent_id=0 and status=1", orgID).
  36. Preload("Schedules", "user_org_id=? and schedule_date>=? and schedule_date<= ? and status=1", orgID, weekStart, weekEnd).
  37. Preload("Contagions", "status=1").
  38. Where("p.user_org_id=? and p.lapseto=1 and p.status=1", orgID)
  39. if len(keywords) > 0 {
  40. likeKey := "%" + keywords + "%"
  41. db = db.Where("p.name LIKE ?", likeKey)
  42. }
  43. if schedule == 1 {
  44. 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())
  45. } else if schedule == 2 {
  46. 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())
  47. }
  48. if contagion > 0 {
  49. 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())
  50. }
  51. err = db.Find(&patients).Error
  52. return
  53. }
  54. func GetWeekSchedule(orgID int64, start, end int64) (schedules []*models.Schedule, err error) {
  55. err = readDb.Table("xt_schedule as s").Preload("PatientInfectiousDiseases", "status = 1 ").
  56. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  57. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  58. 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").
  59. Order("updated_time asc").
  60. Find(&schedules).Error
  61. return
  62. }
  63. func GetWeekScheduleOne(orgID int64, start, end int64, partionid int64, scheduletype int64) (schedules []*models.Schedule, err error) {
  64. db := readDb.Table("xt_schedule as s")
  65. if partionid > 0 {
  66. db = db.Where("s.partition_id = ?", partionid)
  67. }
  68. if scheduletype > 0 {
  69. db = db.Where("s.schedule_type = ?", scheduletype)
  70. }
  71. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  72. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  73. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  74. 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").
  75. Order("updated_time asc").
  76. Find(&schedules).Error
  77. return
  78. }
  79. func GetWeekScheduleTwo(orgID int64, start, end int64, schIds []string, scheduletype int64) (schedules []*models.Schedule, err error) {
  80. db := readDb.Table("xt_schedule as s")
  81. fmt.Println("schIds2222222")
  82. if len(schIds) > 0 {
  83. db = db.Where("s.partition_id in (?)", schIds)
  84. }
  85. if scheduletype > 0 {
  86. db = db.Where("s.schedule_type = ?", scheduletype)
  87. }
  88. err = db.Preload("PatientInfectiousDiseases", "status = 1 ").
  89. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  90. Where("s.user_org_id=? and s.schedule_date>=? and s.schedule_date<=? and s.status=1", orgID, start, end).
  91. 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").
  92. Order("updated_time asc").
  93. Find(&schedules).Error
  94. return
  95. }
  96. func GetDaySchedule(orgID, start, end, patientID int64) (schedule models.Schedule, err error) {
  97. 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
  98. return
  99. }
  100. func GetDayScheduleTwo(orgID, start, patientID int64) (schedule models.Schedule, err error) {
  101. 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
  102. return
  103. }
  104. func GetDayScheduleByBedid(orgID, start, bed_id int64, schedule_type int64) (schedule models.Schedule, err error) {
  105. 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
  106. return
  107. }
  108. func GetPatientScheduleFormDay(orgID, start, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  109. err = readDb.Table("xt_schedule as s").
  110. Preload("DeviceZone", "org_id=? and status=1", orgID).
  111. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  112. 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).
  113. Find(&schedules).Error
  114. return
  115. }
  116. func GetNextSchedule(orgID, end, patientID int64) (schedules []*models.PatientSchedule, err error) {
  117. err = readDb.Table("xt_schedule as s").
  118. Preload("DeviceZone", "org_id=? and status=1", orgID).
  119. Preload("DeviceNumber", "org_id=? and status=1", orgID).
  120. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", patientID, orgID, end).
  121. Find(&schedules).Error
  122. return
  123. }
  124. func GetPointSchedule(orgID, scheduleDate, scheduleWeek, scheduleType, bedId int64) (schedule models.Schedule, err error) {
  125. 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
  126. if err == gorm.ErrRecordNotFound {
  127. err = nil
  128. return
  129. }
  130. return
  131. }
  132. func CreateSchedule(m *models.Schedule) error {
  133. utx := XTWriteDB().Begin()
  134. err := utx.Create(&m).Error
  135. utx.Commit()
  136. return err
  137. }
  138. func DeleteScheduleExist(scheduleData int64, user_org_id int64) (models.XtSchedule, error) {
  139. schedule := models.XtSchedule{}
  140. 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
  141. return schedule, err
  142. }
  143. func GetTodayScheduleIsExist(patientid int64, schedudate int64, orgid int64, mode_id int64, schedule_type int64) (*models.XtSchedule, error) {
  144. var scheudle models.XtSchedule
  145. var err error
  146. 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
  147. if err == gorm.ErrRecordNotFound {
  148. return nil, err
  149. }
  150. if err != nil {
  151. return nil, err
  152. }
  153. return &scheudle, nil
  154. }
  155. func GetTodayScheduleIsExistOne(patientid int64, schedudate int64, orgid int64) (*models.XtSchedule, error) {
  156. var scheudle models.XtSchedule
  157. var err error
  158. err = XTReadDB().Model(&scheudle).Where("patient_id = ? and schedule_date= ? and user_org_id = ? and status = 1", patientid, schedudate, orgid).Find(&scheudle).Error
  159. if err == gorm.ErrRecordNotFound {
  160. return nil, err
  161. }
  162. if err != nil {
  163. return nil, err
  164. }
  165. return &scheudle, nil
  166. }
  167. func GetPatientByBed(schedule_date int64, bed_id int64, scheduletype int64, orgid int64) (*models.XtSchedule, error) {
  168. var scheudle models.XtSchedule
  169. var err error
  170. 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
  171. if err == gorm.ErrRecordNotFound {
  172. return nil, err
  173. }
  174. if err != nil {
  175. return nil, err
  176. }
  177. return &scheudle, nil
  178. }
  179. func ModeFyScheduleById(schedule_date int64, bedid int64, scheduletype int64, orgid int64) error {
  180. schedule := models.XtSchedule{}
  181. 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
  182. return err
  183. }
  184. func ModeScheduleByTime(startime int64, endtime int64, orgid int64) error {
  185. schedule := models.XtSchedule{}
  186. 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
  187. return err
  188. }
  189. func ModeScheduleByTimeOne(startime int64, endtime int64, orgid int64) error {
  190. schedule := models.XtSchedule{}
  191. 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
  192. return err
  193. }
  194. func UpdateScheduleByOrgId(patientid int64, scheduledate int64, orgid int64, mode_id int64, schedule_type int64, sch *models.XtSchedule) error {
  195. 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
  196. return err
  197. }
  198. func UpdateScheduleByOrgIdOne(patientid int64, scheduledate int64, orgid int64, sch *models.XtSchedule) error {
  199. 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()}).Error
  200. return err
  201. }
  202. func GetSchedule(orgID, id int64) (*models.Schedule, error) {
  203. var schedule models.Schedule
  204. var err error
  205. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=? and status=1", id, orgID).First(&schedule).Error
  206. if err != nil {
  207. return nil, err
  208. }
  209. return &schedule, nil
  210. }
  211. func GetScheduleNight(orgID, id int64) (*models.Schedule, error) {
  212. var schedule models.Schedule
  213. var err error
  214. err = readDb.Model(&models.Schedule{}).Where("id = ? and user_org_id=?", id, orgID).First(&schedule).Error
  215. if err != nil {
  216. return nil, err
  217. }
  218. return &schedule, nil
  219. }
  220. func UpdateSchedule(m *models.Schedule) error {
  221. utx := XTWriteDB().Begin()
  222. err := utx.Save(&m).Error
  223. utx.Commit()
  224. return err
  225. }
  226. func UpdateScheduleOne(id int64, schedule models.Schedule) error {
  227. utx := XTWriteDB().Begin()
  228. 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
  229. utx.Commit()
  230. return err
  231. }
  232. func GetPatientScheduleFormToday(orgId, id, start int64) (schedules []*models.PatientSchedule, err error) {
  233. err = readDb.Table("xt_schedule as s").
  234. Preload("DeviceZone", "org_id=? and status=1", orgId).
  235. Preload("DeviceNumber", "org_id=? and status=1", orgId).
  236. Where("s.patient_id =? and s.user_org_id=? and s.schedule_date>=? and s.status=1", id, orgId, start).
  237. Find(&schedules).Error
  238. return
  239. }
  240. type WeekSchedulePrintVM struct {
  241. ID int64 `gorm:"column:id" json:"id"`
  242. ZoneID int64 `gorm:"column:partition_id" json:"zone_id"`
  243. DeviceNumberID int64 `gorm:"column:bed_id" json:"device_number_id"`
  244. PatientID int64 `gorm:"column:patient_id" json:"patient_id"`
  245. ScheduleDate int64 `gorm:"column:schedule_date" json:"schedule_date"`
  246. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  247. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  248. ModeID int64 `gorm:"column:mode_id" json:"mode_id"`
  249. Patient *models.Patients `gorm:"ForeignKey:PatientID" json:"patient"`
  250. DeviceNumber *models.DeviceNumber `gorm:"ForeignKey:DeviceNumberID" json:"device_number"`
  251. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  252. Mode *models.TreatmentMode `gorm:"ForeignKey:ModeID" json:"mode"`
  253. }
  254. type PrintCount struct {
  255. ScheduleType int64 `gorm:"column:schedule_type" json:"schedule_type"`
  256. ScheduleWeek int64 `gorm:"column:schedule_week" json:"schedule_week"`
  257. Count int64
  258. }
  259. func GetPrinitWeekSchedules(orgID int64, start, end int64) ([]*WeekSchedulePrintVM, error) {
  260. var schedules []*WeekSchedulePrintVM
  261. err := readDb.Table("xt_schedule as s").
  262. Preload("Patient", "status = 1 and user_org_id = ?", orgID).
  263. Preload("DeviceNumber", "status = 1 and org_id = ?", orgID).
  264. Preload("Zone", "status = 1 and org_id = ?", orgID).
  265. Preload("Mode", "status = 1").
  266. // Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  267. Where("s.user_org_id = ? and s.schedule_date >= ? and s.schedule_date <= ? and s.status = 1", orgID, start, end).
  268. // 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").
  269. Find(&schedules).Error
  270. return schedules, err
  271. }
  272. func GetPrinitWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  273. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  274. if orgid > 0 {
  275. db = db.Where("x.user_org_id = ?", orgid)
  276. }
  277. if startime > 0 {
  278. db = db.Where("x.schedule_date >=?", startime)
  279. }
  280. if endtime > 0 {
  281. db = db.Where("x.schedule_date <=?", endtime)
  282. }
  283. 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
  284. return print, err
  285. }
  286. func GetBloodPrintWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  287. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  288. if orgid > 0 {
  289. db = db.Where("x.user_org_id = ?", orgid)
  290. }
  291. if startime > 0 {
  292. db = db.Where("x.schedule_date >=?", startime)
  293. }
  294. if endtime > 0 {
  295. db = db.Where("x.schedule_date <=?", endtime)
  296. }
  297. db = db.Where("x.mode_id = 2")
  298. 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
  299. return print, err
  300. }
  301. func GetPerfusionWeekTotal(orgid int64, startime int64, endtime int64) (print []*PrintCount, err error) {
  302. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  303. if orgid > 0 {
  304. db = db.Where("x.user_org_id = ?", orgid)
  305. }
  306. if startime > 0 {
  307. db = db.Where("x.schedule_date >=?", startime)
  308. }
  309. if endtime > 0 {
  310. db = db.Where("x.schedule_date <=?", endtime)
  311. }
  312. db = db.Where("x.mode_id = 3")
  313. 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
  314. return print, err
  315. }
  316. // func ExchangeScheduleTimeWithWeekSchedules(orgID int64, nextWeekSchs []*models.Schedule, nextTwoWeekSchs []*models.Schedule) error {
  317. // templateMode, getTemplateModeErr := GetOrgPatientScheduleTemplateMode(orgID)
  318. // if getTemplateModeErr != nil {
  319. // return getTemplateModeErr
  320. // }
  321. // if templateMode == nil {
  322. // return errors.New("模板模式不存在")
  323. // }
  324. // tx := writeDb.Begin()
  325. // updateTime := time.Now().Unix()
  326. // for _, sch := range nextWeekSchs {
  327. // sch.PatientInfectiousDiseases = nil
  328. // date := time.Unix(sch.ScheduleDate, 0)
  329. // sch.ScheduleDate = date.AddDate(0, 0, 7).Unix()
  330. // sch.UpdatedTime = updateTime
  331. // updateSchErr := tx.Save(sch).Error
  332. // if updateSchErr != nil {
  333. // tx.Rollback()
  334. // return updateSchErr
  335. // }
  336. // }
  337. // for _, sch := range nextTwoWeekSchs {
  338. // sch.PatientInfectiousDiseases = nil
  339. // date := time.Unix(sch.ScheduleDate, 0)
  340. // sch.ScheduleDate = date.AddDate(0, 0, -7).Unix()
  341. // sch.UpdatedTime = updateTime
  342. // updateSchErr := tx.Save(sch).Error
  343. // if updateSchErr != nil {
  344. // tx.Rollback()
  345. // return updateSchErr
  346. // }
  347. // }
  348. // templateMode.ModifyTime = updateTime
  349. // templateMode.ExecuteTimes += 1
  350. // updateModeErr := tx.Save(templateMode).Error
  351. // if updateModeErr != nil {
  352. // tx.Rollback()
  353. // return updateModeErr
  354. // }
  355. // tx.Commit()
  356. // return nil
  357. // }
  358. func GetSchedualPatientsByKeywordsAndWeek(keywords string, org_id int64, start_time int64, end_time int64) (schedule []*models.Schedule, err error) {
  359. likeKey := "%" + keywords + "%"
  360. err = readDb.
  361. Table("xt_schedule as s").
  362. Preload("PatientInfectiousDiseases", "status = 1 ").
  363. Preload("DeviceZone", "status = 1 ").
  364. Preload("DeviceNumber", "status = 1 ").
  365. Preload("TreatmentMode", "status = 1 ").
  366. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  367. Where("s.user_org_id=? and s.schedule_date >= ? and s.schedule_date <= ? and s.status=1", org_id, start_time, end_time).
  368. 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").
  369. Order("schedule_date asc").
  370. Find(&schedule).Error
  371. return
  372. }
  373. func GetSchedualPatientsByKeywords(keywords string, org_id int64, startime int64, endtime int64) (schedule []*models.Schedule, err error) {
  374. likeKey := "%" + keywords + "%"
  375. err = readDb.
  376. Table("xt_schedule as s").
  377. Preload("PatientInfectiousDiseases", "status = 1 ").
  378. Preload("DeviceZone", "status = 1 ").
  379. Preload("DeviceNumber", "status = 1 ").
  380. Preload("TreatmentMode", "status = 1 ").
  381. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  382. Where("s.user_org_id=? and s.schedule_date >= ? and s.schedule_date <= ? and s.status=1", org_id, startime, endtime).
  383. 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").
  384. Order("schedule_date asc").
  385. Find(&schedule).Error
  386. return
  387. }
  388. func GetWeekDayScheduleTwo(orgID int64, scheduleDate int64, scheduleDates *time.Time, weektime int64) (schedules []*models.WeekSchedule, err error) {
  389. db := readDb.Table("xt_schedule as s ").Where("s.status =1")
  390. if orgID > 0 {
  391. db = db.Where("s.user_org_id = ?", orgID)
  392. }
  393. if scheduleDate > 0 {
  394. db = db.Where("s.schedule_date = ?", scheduleDate)
  395. }
  396. if weektime > 0 {
  397. db = db.Where("s.schedule_type = ?", weektime)
  398. }
  399. err = db.
  400. Preload("DeviceZone", "status = 1 ").
  401. Preload("DeviceNumber", "status = 1 ").
  402. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  403. return db.Where("status = 1").Order("id asc")
  404. }).
  405. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  406. 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").
  407. Order("s.partition_id asc").
  408. Find(&schedules).Error
  409. for _, item := range schedules {
  410. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  411. if config.IsOpenRemind > 0 {
  412. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  413. var advice_three []*models.DoctorAdvice
  414. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  415. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  416. for _, advice := range advices {
  417. advice.IsCheck = 1
  418. if advice.FrequencyType == 3 {
  419. week := int(scheduleDates.Weekday())
  420. switch week {
  421. case 1:
  422. if strings.Index(advice.WeekDay, "周一") > -1 {
  423. advice_three = append(advice_three, advice)
  424. }
  425. break
  426. case 2:
  427. if strings.Index(advice.WeekDay, "周二") > -1 {
  428. advice_three = append(advice_three, advice)
  429. }
  430. break
  431. case 3:
  432. if strings.Index(advice.WeekDay, "周三") > -1 {
  433. advice_three = append(advice_three, advice)
  434. }
  435. break
  436. case 4:
  437. if strings.Index(advice.WeekDay, "周四") > -1 {
  438. advice_three = append(advice_three, advice)
  439. }
  440. break
  441. case 5:
  442. if strings.Index(advice.WeekDay, "周五") > -1 {
  443. advice_three = append(advice_three, advice)
  444. }
  445. break
  446. case 6:
  447. if strings.Index(advice.WeekDay, "周六") > -1 {
  448. advice_three = append(advice_three, advice)
  449. }
  450. break
  451. case 0:
  452. if strings.Index(advice.WeekDay, "周日") > -1 {
  453. advice_three = append(advice_three, advice)
  454. }
  455. break
  456. }
  457. } else if advice.FrequencyType == 1 {
  458. advice_three = append(advice_three, advice)
  459. }
  460. }
  461. for _, advice := range advices_two {
  462. now := scheduleDates.Unix()
  463. dayStr := strconv.FormatInt(advice.DayCount, 10)
  464. dayStr2 := "-" + dayStr
  465. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  466. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  467. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  468. if len(advices) == 0 {
  469. advice_three = append(advice_three, advice)
  470. }
  471. }
  472. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  473. }
  474. }
  475. return
  476. }
  477. func GetWeekDaySchedule(orgID int64, scheduleDate int64, scheduleDates *time.Time) (schedules []*models.WeekSchedule, err error) {
  478. err = readDb.
  479. Table("xt_schedule as s").
  480. Preload("DeviceZone", "status = 1 ").
  481. Preload("DeviceNumber", "status = 1 ").
  482. Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  483. return db.Where("status = 1").Order("id asc")
  484. }).
  485. Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  486. Where("s.user_org_id=? and s.schedule_date = ? and s.status=1", orgID, scheduleDate).
  487. 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").
  488. Order("schedule_type").
  489. Find(&schedules).Error
  490. for _, item := range schedules {
  491. _, config := FindDoctorAdviceRecordByOrgId(item.UserOrgId)
  492. if config.IsOpenRemind > 0 {
  493. //开启推送提醒逻辑 提交长期处方的时候,弹起长期医嘱推送
  494. var advice_three []*models.DoctorAdvice
  495. advices, _ := GetLastLongAdviceByGroupNoOther(item.UserOrgId, item.PatientId)
  496. advices_two, _ := GetLastLongAdviceByGroupNoThreeOther(item.UserOrgId, item.PatientId)
  497. for _, advice := range advices {
  498. advice.IsCheck = 1
  499. if advice.FrequencyType == 3 {
  500. week := int(scheduleDates.Weekday())
  501. fmt.Println(advice.WeekDay)
  502. switch week {
  503. case 1:
  504. if strings.Index(advice.WeekDay, "周一") > -1 {
  505. advice_three = append(advice_three, advice)
  506. }
  507. break
  508. case 2:
  509. if strings.Index(advice.WeekDay, "周二") > -1 {
  510. advice_three = append(advice_three, advice)
  511. }
  512. break
  513. case 3:
  514. if strings.Index(advice.WeekDay, "周三") > -1 {
  515. advice_three = append(advice_three, advice)
  516. }
  517. break
  518. case 4:
  519. if strings.Index(advice.WeekDay, "周四") > -1 {
  520. advice_three = append(advice_three, advice)
  521. }
  522. break
  523. case 5:
  524. if strings.Index(advice.WeekDay, "周五") > -1 {
  525. advice_three = append(advice_three, advice)
  526. }
  527. break
  528. case 6:
  529. if strings.Index(advice.WeekDay, "周六") > -1 {
  530. advice_three = append(advice_three, advice)
  531. }
  532. break
  533. case 0:
  534. if strings.Index(advice.WeekDay, "周日") > -1 {
  535. advice_three = append(advice_three, advice)
  536. }
  537. break
  538. }
  539. } else if advice.FrequencyType == 1 {
  540. advice_three = append(advice_three, advice)
  541. }
  542. }
  543. for _, advice := range advices_two {
  544. now := scheduleDates.Unix()
  545. dayStr := strconv.FormatInt(advice.DayCount, 10)
  546. dayStr2 := "-" + dayStr
  547. count, _ := strconv.ParseInt(dayStr2, 10, 64)
  548. oldTime := scheduleDates.AddDate(0, 0, int(count)).Unix()
  549. advices, _ := FindAllDoctorAdviceByTimeOther(now, oldTime, item.PatientId, item.UserOrgId, advice.TemplateId)
  550. if len(advices) == 0 {
  551. advice_three = append(advice_three, advice)
  552. }
  553. }
  554. item.DoctorAdvice = append(item.DoctorAdvice, advice_three...)
  555. }
  556. }
  557. return
  558. }
  559. func GetWeekDayScheduleById(orgid int64, scheduleDate int64, scheduleType int64, zone int64) (list []*models.VmSchedulesRemind, err error) {
  560. db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
  561. if orgid > 0 {
  562. db = db.Where("x.user_org_id = ?", orgid)
  563. }
  564. if scheduleDate > 0 {
  565. db = db.Where("x.schedule_date = ?", scheduleDate)
  566. }
  567. if scheduleType > 0 {
  568. db = db.Where("x.schedule_type = ?", scheduleType)
  569. }
  570. if zone > 0 {
  571. db = db.Where("x.partition_id = ?", zone)
  572. }
  573. 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 {
  574. return db.Where("status =1 and parent_id = 0").Preload("ChildDoctorAdvice", "status = 1")
  575. }).Preload("HisDoctorAdviceInfo", "status = 1").Preload("DialysisPrescription", "status = 1").Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  576. return db.Where("status =1").Order("id asc")
  577. }).Find(&list).Error
  578. return list, err
  579. }
  580. func GetWeekDayScheduleByIdThee(orgid int64, scheduleDate int64, scheduleType int64, zone []string) (list []*models.VmSchedulesRemind, err error) {
  581. db := readDb2.Table("xt_schedule as x").Where("x.status = 1")
  582. if orgid > 0 {
  583. db = db.Where("x.user_org_id = ?", orgid)
  584. }
  585. if scheduleDate > 0 {
  586. db = db.Where("x.schedule_date = ?", scheduleDate)
  587. }
  588. if scheduleType > 0 {
  589. db = db.Where("x.schedule_type = ?", scheduleType)
  590. }
  591. if len(zone) > 0 {
  592. db = db.Where("x.partition_id in (?)", zone)
  593. }
  594. 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 {
  595. return db.Where("status =1 and parent_id = 0 and advice_date = ?", scheduleDate).Preload("ChildDoctorAdvice", "status = 1")
  596. }).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 {
  597. return db.Where("status =1").Order("id asc")
  598. }).Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  599. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgid, scheduleDate)
  600. }).Find(&list).Error
  601. return list, err
  602. }
  603. func GetWeekDayScheduleByIdTwo(orgid int64, scheduleDate int64, scheduleType int64, startDate int64, zone []string) (list []*models.VmSchedulesRemind, err error) {
  604. db := readDb2.Table("xt_schedule as x").Where("x.status = 1")
  605. if orgid > 0 {
  606. db = db.Where("x.user_org_id = ?", orgid)
  607. }
  608. if scheduleDate > 0 {
  609. db = db.Where("x.schedule_date >=? and x.schedule_date<=?", startDate, scheduleDate)
  610. }
  611. if scheduleType > 0 {
  612. db = db.Where("x.schedule_type = ?", scheduleType)
  613. }
  614. if len(zone) > 0 {
  615. db = db.Where("x.partition_id in (?)", zone)
  616. }
  617. 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 {
  618. return db.Where("status =1 and parent_id = 0 and advice_date = ?", scheduleDate).Preload("ChildDoctorAdvice", "status = 1")
  619. }).Preload("HisDoctorAdviceInfo", "status = 1 and advice_date = ?", scheduleDate).Preload("DialysisPrescription", "status = 1 and record_date = ?", scheduleDate).Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  620. return db.Where("status =1").Order("id asc")
  621. }).Preload("LastAfterWeight", func(db *gorm.DB) *gorm.DB {
  622. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgid, scheduleDate)
  623. }).Find(&list).Error
  624. return list, err
  625. }
  626. func RemoveRepeatedElement(arr []*models.DoctorAdvice) (newArr []*models.DoctorAdvice) {
  627. newArr = make([]*models.DoctorAdvice, 0)
  628. for i := 0; i < len(arr); i++ {
  629. repeat := false
  630. for j := i + 1; j < len(arr); j++ {
  631. if arr[i].ID == arr[j].ID {
  632. repeat = true
  633. break
  634. }
  635. }
  636. if !repeat {
  637. newArr = append(newArr, arr[i])
  638. }
  639. }
  640. return
  641. }
  642. func GetLastLongAdviceByGroupNoOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  643. 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
  644. return
  645. }
  646. func GetLastLongAdviceByGroupNoThreeOther(orgId int64, patient_id int64) (advice []*models.DoctorAdvice, err error) {
  647. 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
  648. return
  649. }
  650. func FindAllDoctorAdviceByTimeOther(now int64, end int64, patient_id int64, orgId int64, temp_id string) (advice []*models.DoctorAdvice, err error) {
  651. 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
  652. return
  653. }
  654. func FindSchPatientNameByName(name string, user_org_id int64) (patient []*models.Patients, err error) {
  655. err = readDb.Model(&models.Patients{}).Where("user_org_id = ? AND name = ? AND status = 1", user_org_id, name).Find(&patient).Error
  656. return
  657. }
  658. func UpdateScheduleStatus(date int64, org_id int64) (err error) {
  659. 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
  660. return
  661. }
  662. func FindDeviceZoneByName(name string, org_id int64) (zone models.DeviceZone, err error) {
  663. err = readDb.Model(&models.DeviceZone{}).Where("org_id = ? AND name = ? AND status = 1", org_id, name).First(&zone).Error
  664. return
  665. }
  666. func FindDeviceByName(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  667. 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
  668. return
  669. }
  670. func FindSchedualExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  671. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 2", org_id, export_time).Find(&errLogs).Error
  672. return
  673. }
  674. func FindDeviceByNameTwo(device_name string, org_id int64, zone_id int64) (devices []*models.DeviceNumber, err error) {
  675. 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
  676. return
  677. }
  678. func UpdateScheduleTemplateStatus(org_id int64) (err error) {
  679. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND status = 1", org_id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  680. return
  681. }
  682. func CreateScheduleTemplate(m *models.PatientScheduleTemplateItem) error {
  683. return writeDb.Model(&models.PatientScheduleTemplateItem{}).Create(&m).Error
  684. }
  685. func FindSchedualTemplateExportLog(org_id int64, export_time int64) (errLogs []*models.ExportErrLog, err error) {
  686. err = readDb.Model(&models.ExportErrLog{}).Where("user_org_id = ? AND export_time = ? AND log_type = 3", org_id, export_time).Find(&errLogs).Error
  687. return
  688. }
  689. func GetDialysisOrderByBedId(orgID, start, bed_id int64, schedule_type int64) (*models.DialysisOrder, error) {
  690. order := models.DialysisOrder{}
  691. 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
  692. if err == gorm.ErrRecordNotFound {
  693. return nil, err
  694. }
  695. if err != nil {
  696. return nil, err
  697. }
  698. return &order, nil
  699. }
  700. type VMDeviceNumber struct {
  701. ID int64 `gorm:"column:id" json:"id"`
  702. OrgID int64 `gorm:"column:org_id" json:"-"`
  703. Number string `gorm:"column:number" json:"number"`
  704. GroupID int64 `gorm:"column:group_id" json:"group_id"`
  705. ZoneID int64 `gorm:"column:zone_id" json:"zone_id"`
  706. Status int8 `json:"-"`
  707. CreateTime int64 `gorm:"column:ctime" json:"-"`
  708. ModifyTime int64 `gorm:"column:mtime" json:"-"`
  709. DeviceZone models.DeviceZone `json:"zone" gorm:"foreignkey:ID;AssociationForeignKey:ZoneID;"`
  710. }
  711. func (VMDeviceNumber) TableName() string {
  712. return "xt_device_number"
  713. }
  714. type VMPatientScheduleTemplateItem struct {
  715. ID int64 `gorm:"column:id" json:"id" form:"id"`
  716. OrgID int64 `gorm:"column:org_id" json:"-" form:"org_id"`
  717. TemplateID int64 `gorm:"column:template_id" json:"template_id" form:"template_id"`
  718. DeviceNumberID int64 `gorm:"column:device_number_id" json:"device_number_id" form:"device_number_id"`
  719. PatientID int64 `gorm:"column:patient_id" json:"patient_id" form:"patient_id"`
  720. TreatMode int64 `gorm:"column:treat_mode" json:"treat_mode" form:"treat_mode"` // 治疗模式
  721. Weekday int8 `gorm:"column :weekday" json:"weekday" form:"weekday"` // 周一-周日 1-7
  722. TimeType int8 `gorm:"column:time_type" json:"time_type" form:"time_type"` // 上下晚 123
  723. Status int8 `gorm:"column:status" json:"status" form:"status"`
  724. CreateTime int64 `gorm:"column:ctime" json:"-" form:"ctime"`
  725. ModifyTime int64 `gorm:"column:mtime" json:"-" form:"mtime"`
  726. IsExport int64 `gorm:"column:is_export" json:"is_export" form:"is_export"`
  727. Patient string `gorm:"-" json:"patient" form:"patient"`
  728. DeviceNumber VMDeviceNumber `json:"number" gorm:"foreignkey:ID;AssociationForeignKey:DeviceNumberID;"`
  729. PatientInfectiousDiseases []*models.InfectiousDiseases `json:"patient_contagions" gorm:"foreignkey:PatientId;AssociationForeignKey:PatientId;"`
  730. }
  731. func (VMPatientScheduleTemplateItem) TableName() string {
  732. return "xt_patient_schedule_template_item"
  733. }
  734. func GetTemplateSchedualPatientsByKeywords(keywords string, org_id int64, template_id int64) (schedule []*VMPatientScheduleTemplateItem, err error) {
  735. likeKey := "%" + keywords + "%"
  736. err = readDb.
  737. Table("xt_patient_schedule_template_item as s").
  738. Preload("PatientInfectiousDiseases", "status = 1 ").
  739. Preload("DeviceNumber", func(db *gorm.DB) *gorm.DB {
  740. return db.Where("org_id = ? AND status = 1", org_id).Preload("DeviceZone", "org_id = ? AND status = 1", org_id)
  741. }).
  742. Joins("JOIN xt_patients as p ON p.id = s.patient_id AND (p.name LIKE ? OR p.dialysis_no LIKE ?)", likeKey, likeKey).
  743. Where("s.template_id = ? AND s.org_id=? and s.status=1", template_id, org_id).
  744. 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").
  745. Order("weekday asc").
  746. Find(&schedule).Error
  747. return
  748. }
  749. func UpdateNewScheduleTemplateStatus(id int64, org_id int64) (err error) {
  750. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND id = ?", org_id, id).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  751. return
  752. }
  753. func GetNextWeekDaySchedule(weektype int64, weektime int64, startime int64, endtime int64, orgID int64, zone []string, record_date int64) (schedules []*models.WeekScheduleSix, err error) {
  754. db := readDb.Table("xt_schedule as s ").Where("s.status =1")
  755. if orgID > 0 {
  756. db = db.Where("s.user_org_id = ?", orgID)
  757. }
  758. if startime > 0 {
  759. db = db.Where("s.schedule_date >= ?", startime)
  760. }
  761. if endtime > 0 {
  762. db = db.Where("s.schedule_date <= ?", endtime)
  763. }
  764. if weektime > 0 {
  765. db = db.Where("s.schedule_type = ?", weektime)
  766. }
  767. if weektype > 0 {
  768. db = db.Where("s.schedule_week = ?", weektype)
  769. }
  770. if len(zone) > 0 {
  771. db = db.Where("s.partition_id in (?)", zone)
  772. }
  773. err = db.
  774. Preload("DeviceZone", "status = 1 ").
  775. Preload("DeviceNumber", "status = 1 ").
  776. Preload("XtAssessmentBeforeDislysis", func(db *gorm.DB) *gorm.DB {
  777. return db.Where("status = 1 and assessment_date = ?", record_date)
  778. }).Preload("DialysisPrescription", func(db *gorm.DB) *gorm.DB {
  779. return db.Where("status = 1").Order("id asc")
  780. }).Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  781. return db.Where("status = 1").Order("id asc")
  782. }).Preload("DoctorAdvice", func(db *gorm.DB) *gorm.DB {
  783. return db.Where("status =1 and parent_id = 0 and advice_date = ?", record_date).Preload("ChildDoctorAdvice", "status = 1 and advice_date = ?", record_date)
  784. }).Preload("HisDoctorAdviceInfo", "status = 1 and advice_date = ?", record_date).Joins("JOIN xt_patients as p ON p.id = s.patient_id").
  785. 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").
  786. Order("s.partition_id desc").
  787. Find(&schedules).Error
  788. return schedules, err
  789. }
  790. func GetThreeWeekList(startime int64, endtime int64, orgid int64, scheduletype int64, partionid int64) (schedule []*models.VmSchedule, err error) {
  791. db := XTReadDB().Table("xt_schedule as x").Where("x.status =1")
  792. if startime > 0 {
  793. db = db.Where("x.schedule_date >=?", startime)
  794. }
  795. if endtime > 0 {
  796. db = db.Where("x.schedule_date <=?", endtime)
  797. }
  798. if orgid > 0 {
  799. db = db.Where("x.user_org_id = ?", orgid)
  800. }
  801. if scheduletype > 0 {
  802. db = db.Where("x.schedule_type = ?", scheduletype)
  803. }
  804. if partionid > 0 {
  805. db = db.Where("x.partition_id = ?", partionid)
  806. }
  807. err = db.Select("x.schedule_date,x.schedule_type,x.schedule_week,x.bed_id,x.partition_id,t.name,n.number,z.name").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
  808. return schedule, err
  809. }
  810. func UpdatedDialysisPrescription(prescription *models.DialysisPrescription, recordDate int64, patientid int64, orgid int64) error {
  811. 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
  812. return err
  813. }
  814. func GetWeekSchedules(orgid int64, startime int64, endtime int64) (scheudle []*models.XtSchedule, err error) {
  815. err = XTReadDB().Model(&scheudle).Where("user_org_id = ? and status = 1 and schedule_date>=? and schedule_date<=?", orgid, startime, endtime).Find(&scheudle).Error
  816. return scheudle, err
  817. }
  818. func GetLastWeekSchedule(orgid int64, startime int64, endtime int64) (*models.XtSchedule, error) {
  819. schedule := models.XtSchedule{}
  820. err := XTReadDB().Where("user_org_id = ? and status =1 and schedule_date>=? and schedule_date<=?", orgid, startime, endtime).Find(&schedule).Error
  821. if err == gorm.ErrRecordNotFound {
  822. return nil, err
  823. }
  824. if err != nil {
  825. return nil, err
  826. }
  827. return &schedule, nil
  828. }
  829. func AddPatientSchedule(schedule *models.XtSchedule) error {
  830. utx := XTWriteDB().Begin()
  831. err := utx.Create(&schedule).Error
  832. utx.Commit()
  833. return err
  834. }
  835. func UpdatePatientSchedule(orgid int64, startime int64, endtime int64) error {
  836. schedule := models.XtSchedule{}
  837. 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
  838. return err
  839. }
  840. func GetSettingById(id int64) (*models.XtRemindPrintSetting, error) {
  841. setting := models.XtRemindPrintSetting{}
  842. err := XTReadDB().Model(&setting).Where("id = ? and status = 1", id).Find(&setting).Error
  843. if err == gorm.ErrRecordNotFound {
  844. return nil, err
  845. }
  846. if err != nil {
  847. return nil, err
  848. }
  849. return &setting, nil
  850. }
  851. func CreateSetting(setting *models.XtRemindPrintSetting) error {
  852. err := XTReadDB().Create(&setting).Error
  853. return err
  854. }
  855. func UpdatedRemindPrint(set *models.XtRemindPrintSetting, id int64) error {
  856. 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
  857. return err
  858. }
  859. func GetRemindPrintList(id int64) (models.XtRemindPrintSetting, error) {
  860. setting := models.XtRemindPrintSetting{}
  861. err := XTReadDB().Model(&setting).Where("user_org_id = ? and status =1", id).Find(&setting).Error
  862. return setting, err
  863. }
  864. func GetScheudleTemplateById(orgid int64) (*models.ScheduleTemplate, error) {
  865. schedule := models.ScheduleTemplate{}
  866. err := XTReadDB().Model(&schedule).Where("user_org_id = ? and status = 1", orgid).Find(&schedule).Error
  867. if err == gorm.ErrRecordNotFound {
  868. return nil, err
  869. }
  870. if err != nil {
  871. return nil, err
  872. }
  873. return &schedule, nil
  874. }
  875. func CreateXtscheduleTemplate(template *models.ScheduleTemplate) error {
  876. err := XTWriteDB().Create(&template).Error
  877. return err
  878. }
  879. func UpdateXtScheduleTemplate(template *models.ScheduleTemplate, orgid int64) error {
  880. err := XTWriteDB().Model(&template).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"template_id": template.TemplateId}).Error
  881. return err
  882. }
  883. func GetScheduleTemplate(orgid int64) (models.ScheduleTemplate, error) {
  884. template := models.ScheduleTemplate{}
  885. err := XTReadDB().Where("user_org_id = ? and status = 1", orgid).Find(&template).Error
  886. return template, err
  887. }
  888. func GetDataPrintByOrgId(orgid int64) (*models.XtDataPrint, error) {
  889. dataPrint := models.XtDataPrint{}
  890. err := XTReadDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Find(&dataPrint).Error
  891. if err == gorm.ErrRecordNotFound {
  892. return nil, err
  893. }
  894. if err != nil {
  895. return nil, err
  896. }
  897. return &dataPrint, nil
  898. }
  899. func CreateDataPrint(dataPrint *models.XtDataPrint) error {
  900. err := XTWriteDB().Create(&dataPrint).Error
  901. return err
  902. }
  903. func UpdteDataPrint(orgid int64, dataPrint *models.XtDataPrint) error {
  904. err := XTWriteDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Updates(map[string]interface{}{"is_open": dataPrint.IsOpen}).Error
  905. return err
  906. }
  907. func GetDataPrint(orgid int64) (models.XtDataPrint, error) {
  908. dataPrint := models.XtDataPrint{}
  909. err := XTReadDB().Model(&dataPrint).Where("user_org_id = ? and status = 1", orgid).Find(&dataPrint).Error
  910. return dataPrint, err
  911. }
  912. func GetNextWeekPanels(startime int64, endtime int64, orgid int64) (schedule []*models.XtSchedule, err error) {
  913. err = readDb2.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 {
  914. return db.Where("user_org_id = ? and status = 1 and assessment_date < ?", orgid, endtime)
  915. }).Find(&schedule).Error
  916. return schedule, err
  917. }
  918. func GetTemplateListByTemplateId(id int64, orgid int64) (list []*models.VmPatientScheduleTemplateItem, err error) {
  919. err = XTReadDB().Where("template_id =? and org_id = ? and status = 1", id, orgid).Find(&list).Error
  920. return list, err
  921. }
  922. func DeletePatientSchedule(startime int64, endtime int64, org_id int64) (models.XtSchedule, error) {
  923. schedule := models.XtSchedule{}
  924. 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
  925. return schedule, err
  926. }
  927. func DeletePatientScheduleToday(startime int64, endtime int64, org_id int64) (models.XtSchedule, error) {
  928. schedule := models.XtSchedule{}
  929. 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
  930. return schedule, err
  931. }
  932. func GetBedNumberById(id int64, org_id int64) (models.DeviceNumber, error) {
  933. number := models.DeviceNumber{}
  934. err := XTReadDB().Where("id = ? and status = 1 and org_id =?", id, org_id).Find(&number).Error
  935. return number, err
  936. }
  937. func CreatePatientSchedule(schedule *models.XtSchedule) error {
  938. utx := XTWriteDB().Begin()
  939. err := utx.Create(&schedule).Error
  940. utx.Commit()
  941. return err
  942. }
  943. func GetPatientScheduleTempalate(orgid int64) (models.PatientScheduleTemplateMode, error) {
  944. mode := models.PatientScheduleTemplateMode{}
  945. err := XTReadDB().Where("org_id = ? and status =1", orgid).Find(&mode).Error
  946. return mode, err
  947. }
  948. func GetSolutionSchedule(orgid int64) (solution []*models.VmDialysisSolution, err error) {
  949. 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 {
  950. return db.Where("user_org_id = ? and status = 1", orgid).Order("id asc")
  951. }).Find(&solution).Error
  952. return solution, err
  953. }
  954. func GetSolutionScheduleBySchedule(orgid int64, zonid int64, classtype int64, schedule_date int64) (schedule []*models.XtScheduleList, err error) {
  955. db := XTReadDB().Model(&schedule).Where("status = 1")
  956. if orgid > 0 {
  957. db = db.Where("user_org_id =?", orgid)
  958. }
  959. if zonid > 0 {
  960. db = db.Where("partition_id = ?", zonid)
  961. }
  962. if classtype > 0 {
  963. db = db.Where("schedule_type = ?", classtype)
  964. }
  965. if schedule_date > 0 {
  966. db = db.Where("schedule_date = ?", schedule_date)
  967. }
  968. err = db.Preload("DialysisSolution", func(db *gorm.DB) *gorm.DB {
  969. 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 {
  970. return db.Where("user_org_id = ? and status = 1", orgid).Order("id asc")
  971. })
  972. }).Find(&schedule).Error
  973. return schedule, err
  974. }
  975. func GetDeathPatient(orgid int64) (patient []*models.XtPatients, err error) {
  976. err = XTReadDB().Where("user_org_id = ? and (lapseto = 2 || lapseto = 3) and status = 1", orgid).Find(&patient).Error
  977. return patient, err
  978. }