schedule_service.go 54KB

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