schedule_service.go 54KB

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