schedule_service.go 53KB

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