123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package service
-
- import (
- "XT_New/models"
- "github.com/jinzhu/gorm"
- "time"
- )
-
- func GetSmartSchPatientByKeyWord(orgID int64, keywords string) (patient []*models.SmartSchPatient, err error) {
- db := readDb.Model(&models.SmartSchPatient{}).Where("user_org_id=? and status=1", orgID)
- if len(keywords) > 0 {
- likekey := "%" + keywords + "%"
- err = db.Where("name LIKE ? OR dialysis_no LIKE ? ", likekey, likekey).Find(&patient).Error
- } else {
- err = db.Find(&patient).Error
- }
-
- //for _, item := range patient {
- // var sch []*models.SmartSchedule
- // now := time.Now()
- //
- // offset := int(time.Monday - now.Weekday())
- // if offset > 0 {
- // offset = -6
- // }
- //
- // weekStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
- // readDb.Model(&models.SmartSchedule{}).Where("user_org_id = ? AND status = 1 AND patient_id = ? AND schedule_date >= ?", orgID, item.ID, weekStart.Unix()).
- // Preload("DeviceZone", "status = 1").
- // Preload("SmSchDialysisOrder", "status = 1").
- // Preload("DeviceNumber", "status = 1").Find(&sch)
- // item.SmartSchedule = sch
- //}
-
- return
- }
-
- func GetSmartSchPatientByPatientID(orgID int64, id int64) (sch []*models.SmartSchedule, err error) {
- now := time.Now()
- offset := int(time.Monday - now.Weekday())
- if offset > 0 {
- offset = -6
- }
- weekStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
- readDb.Model(&models.SmartSchedule{}).Where("user_org_id = ? AND status = 1 AND patient_id = ? AND schedule_date >= ?", orgID, id, weekStart.Unix()).
- Preload("DeviceZone", "status = 1").
- Preload("DeviceNumber", "status = 1").Find(&sch)
-
- for _, item := range sch {
- var order models.SmSchDialysisOrder
- readDb.Model(&models.SmSchDialysisOrder{}).Where("user_org_id = ? AND status = 1 AND patient_id = ? AND dialysis_date = ? AND bed_id = ?", orgID, item.PatientId, item.ScheduleDate, item.BedId).First(&order)
- item.SmSchDialysisOrder = order
- }
-
- return
- }
-
- func GetSmartSchTemplatePatientByKeyWord(orgID int64, keywords string) (patient []*models.SmartSchTemplatePatient, err error) {
- db := readDb.Model(&models.SmartSchTemplatePatient{}).Where("user_org_id=? and status=1", orgID)
- if len(keywords) > 0 {
- likekey := "%" + keywords + "%"
- err = db.Where("name LIKE ? OR dialysis_no LIKE ? ", likekey, likekey).Find(&patient).Error
- } else {
- err = db.Find(&patient).Error
- }
-
- for _, item := range patient {
- mode, _ := GetOrgPatientScheduleTemplateModeTwo(item.UserOrgId)
- if mode.ID > 0 {
- if mode.Mode == 1 {
- schs, _ := GetSmartSchTemplatePatientByID(item.UserOrgId, item.ID, 1)
- item.XtPatientScheduleTemplateId = schs
-
- } else if mode.Mode == 2 {
- schs, _ := GetSmartSchTemplatePatientByID(item.UserOrgId, item.ID, 2)
-
- item.XtPatientScheduleTemplateId = schs
-
- } else if mode.Mode == 3 {
- schs, _ := GetSmartSchTemplatePatientByID(item.UserOrgId, item.ID, 3)
- item.XtPatientScheduleTemplateId = schs
-
- } else if mode.Mode == 4 {
-
- schs, _ := GetSmartSchTemplatePatientByID(item.UserOrgId, item.ID, 4)
- item.XtPatientScheduleTemplateId = schs
-
- }
- }
-
- }
- return
- }
-
- func GetSmartSchPatientByID(orgID int64, patient_id int64) (schs []*models.SmartSchedule, err error) {
- now := time.Now()
- offset := int(time.Monday - now.Weekday())
- if offset > 0 {
- offset = -6
- }
- weekStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
- err = readDb.Model(&models.SmartSchedule{}).Where("user_org_id = ? AND status = 1 AND patient_id = ? AND schedule_date >= ?", orgID, patient_id, weekStart.Unix()).
- Preload("DeviceZone", "status = 1").
- Preload("DeviceNumber", "status = 1").Find(&schs).Error
-
- for _, item := range schs {
- var order models.SmSchDialysisOrder
- readDb.Model(&models.SmSchDialysisOrder{}).Where("user_org_id = ? AND status = 1 AND patient_id = ? AND dialysis_date = ? AND bed_id = ?", orgID, item.PatientId, item.ScheduleDate, item.BedId).First(&order)
- item.SmSchDialysisOrder = order
- }
- return
- }
-
- func GetSmartSchTemplatePatientByID(orgID int64, patient_id int64, count int) (schs []*models.XtPatientScheduleTemplateId, err error) {
- err = readDb.Model(&models.XtPatientScheduleTemplateId{}).Preload("XtPatientScheduleTemplateItem", func(db *gorm.DB) *gorm.DB {
- return db.Where("status = 1 AND patient_id = ?", patient_id).Order("weekday")
- }).Where("org_id = ? AND status = 1", orgID).Limit(count).Find(&schs).Error
- for _, items := range schs {
- for _, subItems := range items.XtPatientScheduleTemplateItem {
- number, _ := GetBedNumberById(subItems.DeviceNumberId, subItems.OrgId)
- subItems.ZoneId = number.ZoneID
- }
- }
- return
- }
-
- func GetAllAvaildDeviceNumbersByZone(orgID int64, record_date int64, schedule_type int, zone_id int64, patient_id int64) ([]*DeviceNumberViewModel, error) {
- var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
- rows, err := readDb.Raw("SELECT n.*, z.name as zone_name FROM xt_device_number as n join xt_device_zone as z on z.id = n.zone_id AND z.id = ? WHERE (n.org_id = ? AND n.status = 1) AND NOT EXISTS (Select * FROM xt_schedule as s Where s.`schedule_date` = ? AND s.patient_id <> ? AND s.user_org_id = n.org_id AND s.`bed_id` = n.id AND s.`schedule_type` = ? AND s.status = 1 )", zone_id, orgID, record_date, patient_id, schedule_type).Rows()
- defer rows.Close()
- if err != nil {
- return nil, err
- }
- for rows.Next() {
- var vm DeviceNumberViewModel
- readDb.ScanRows(rows, &vm)
- vms = append(vms, &vm)
- }
- return vms, nil
- }
-
- func GetScheduleByZoneAndBed(org_id int64, partitionId int64, bed_id int64, schedule_date int64, sch_type int64) (sch models.SmartSchedule, err error) {
- err = readDb.Model(&models.SmartSchedule{}).Where("bed_id = ? AND user_org_id = ? AND schedule_date = ? AND partition_id = ? AND schedule_type = ? AND status = 1", bed_id, org_id, schedule_date, partitionId, sch_type).First(&sch).Error
- return
- }
-
- func GetScheduleByDate(org_id int64, schedule_date int64, patient_id int64) (sch models.SmartSchedule, err error) {
- err = readDb.Model(&models.SmartSchedule{}).Where(" user_org_id = ? AND schedule_date = ? AND status = 1 AND patient_id = ?", org_id, schedule_date, patient_id).First(&sch).Error
- return
- }
- func SaveSmartSch(sch models.SmartSchedule) {
- writeDb.Save(&sch)
- return
- }
-
- func GetAllAvaildTemplateDeviceNumbersByZone(orgID int64, weekday int64, schedule_type int, zone_id int64, patient_id int64, template_id int64) ([]*DeviceNumberViewModel, error) {
- var vms []*DeviceNumberViewModel = make([]*DeviceNumberViewModel, 0)
- rows, err := readDb.Raw("SELECT n.*, z.name as zone_name FROM xt_device_number as n join xt_device_zone as z on z.id = n.zone_id AND z.id = ? WHERE (n.org_id = ? AND n.status = 1) AND NOT EXISTS (Select * FROM xt_patient_schedule_template_item as s Where s.`weekday` = ? AND s.patient_id <> ? AND s.org_id = n.org_id AND s.`device_number_id` = n.id AND s.`time_type` = ? AND s.status = 1 AND s.template_id = ?)", zone_id, orgID, weekday, patient_id, schedule_type, template_id).Rows()
- defer rows.Close()
- if err != nil {
- return nil, err
- }
- for rows.Next() {
- var vm DeviceNumberViewModel
- readDb.ScanRows(rows, &vm)
- vms = append(vms, &vm)
- }
- return vms, nil
- }
|