package controllers import ( "XT_New/enums" "XT_New/models" "XT_New/service" "XT_New/utils" "encoding/json" "fmt" "reflect" "strconv" "time" "github.com/astaxie/beego" ) type ScheduleApiController struct { BaseAuthAPIController } func ScheduleApiRegistRouters() { beego.Router("/api/schedule/weekpanel", &ScheduleApiController{}, "Get:GetWeekPanels") beego.Router("/api/schedule/schedules", &ScheduleApiController{}, "Get:GetSchedules") beego.Router("/api/schedule/patients", &ScheduleApiController{}, "Get:GetPatients") beego.Router("/api/schedule/create", &ScheduleApiController{}, "Post:CreateSchedule") beego.Router("/api/schedule/delete", &ScheduleApiController{}, "Delete:DeleteSchedule") beego.Router("/api/schedule/change", &ScheduleApiController{}, "Put:ChangeSchedule") beego.Router("/api/schedule/urgentinit", &ScheduleApiController{}, "Get:UrgentScheduleData") beego.Router("/api/schedule/print/initdata", &ScheduleApiController{}, "get:PrintInitData") beego.Router("/api/schedule/search", &ScheduleApiController{}, "get:SearchSchedulePatients") beego.Router("/api/schedule/week", &ScheduleApiController{}, "get:GetWeekDaySchedule") } func (c *ScheduleApiController) GetWeekPanels() { data, _ := c.GetInt64("data", 1) adminInfo := c.GetAdminUserInfo() thisTime := time.Now() year, monthTime, day := thisTime.Date() month := int(monthTime) _, theWeek := thisTime.ISOWeek() weekDay := int(thisTime.Weekday()) if weekDay == 0 { weekDay = 7 } weekEnd := 7 - weekDay weekStart := weekEnd - 6 weekDays := make([]string, 0) for index := weekStart; index <= weekEnd; index++ { theDay := thisTime.AddDate(0, 0, index) indexYear, indexMonthTime, indexDay := theDay.Date() indexMonth := int(indexMonthTime) indexWeek := strconv.Itoa(indexYear) + "." + strconv.Itoa(indexMonth) + "." + strconv.Itoa(indexDay) weekDays = append(weekDays, indexWeek) } returnData := map[string]interface{}{ "year": year, "month": month, "day": day, "theWeek": theWeek, "weekDay": weekDay, "weekDays": weekDays, } if data == 1 { partitions, _ := service.GetSchedulePartitionPanel(adminInfo.CurrentOrgId) returnData["partitions"] = partitions } c.ServeSuccessJSON(returnData) return } func (c *ScheduleApiController) GetSchedules() { week, _ := c.GetInt64("weekTime", 0) //1:last, 2:this 3:next 4 nextTwo adminInfo := c.GetAdminUserInfo() thisTime := time.Now() today := thisTime.Format("2006-01-02") if week < 1 || week > 4 { week = 2 } if week == 1 { thisTime = thisTime.AddDate(0, 0, -7) } else if week == 3 { thisTime = thisTime.AddDate(0, 0, 7) } else if week == 4 { thisTime = thisTime.AddDate(0, 0, 14) } weekDay := int(thisTime.Weekday()) if weekDay == 0 { weekDay = 7 } weekEnd := 7 - weekDay weekStart := weekEnd - 6 weekTitle := make([]string, 0) days := make([]string, 0) for index := weekStart; index <= weekEnd; index++ { theDay := thisTime.AddDate(0, 0, index) indexYear, indexMonthTime, indexDay := theDay.Date() indexMonth := int(indexMonthTime) indexWeek := strconv.Itoa(indexYear) + "." + strconv.Itoa(indexMonth) + "." + strconv.Itoa(indexDay) weekTitle = append(weekTitle, indexWeek) days = append(days, theDay.Format("2006-01-02")) } weekStartDay := thisTime.AddDate(0, 0, weekStart) weekEndDay := thisTime.AddDate(0, 0, weekEnd) weekStartTime := weekStartDay.Format("2006-01-02") + " 00:00:00" weekEndTime := weekEndDay.Format("2006-01-02") + " 23:59:59" timeLayout := "2006-01-02 15:04:05" loc, _ := time.LoadLocation("Local") theStarTime, _ := time.ParseInLocation(timeLayout, weekStartTime, loc) theEndTime, _ := time.ParseInLocation(timeLayout, weekEndTime, loc) weekStartPoint := theStarTime.Unix() weekEndPoint := theEndTime.Unix() schdules, _ := service.GetWeekSchedule(adminInfo.CurrentOrgId, weekStartPoint, weekEndPoint) c.ServeSuccessJSON(map[string]interface{}{ "days": days, "weekTitle": weekTitle, "schdules": schdules, "today": today, }) return } func (c *ScheduleApiController) GetPatients() { keywords := c.GetString("keywords", "") schedule, _ := c.GetInt64("schedule", 0) //1已2未 contagion, _ := c.GetInt64("contagion", 0) adminInfo := c.GetAdminUserInfo() thisTime := time.Now() weekDay := int(thisTime.Weekday()) if weekDay == 0 { weekDay = 7 } thisWeekEnd := 7 - weekDay weekStartPoint := thisWeekEnd - 6 weekStartDay := thisTime.AddDate(0, 0, weekStartPoint) weekEndPoint := thisWeekEnd + 7 weekEndDay := thisTime.AddDate(0, 0, weekEndPoint) fmt.Println(weekStartPoint, weekStartDay, weekEndPoint, weekEndDay) weekStartTime := weekStartDay.Format("2006-01-02") + " 00:00:00" weekEndTime := weekEndDay.Format("2006-01-02") + " 23:59:59" fmt.Println(weekStartTime, weekEndTime) timeLayout := "2006-01-02 15:04:05" loc, _ := time.LoadLocation("Local") theStarTime, _ := time.ParseInLocation(timeLayout, weekStartTime, loc) theEndTime, _ := time.ParseInLocation(timeLayout, weekEndTime, loc) weekStart := theStarTime.Unix() weekEnd := theEndTime.Unix() patients, _ := service.GetPatientWithScheduleAndSolution(adminInfo.CurrentOrgId, keywords, weekStart, weekEnd, schedule, contagion) c.ServeSuccessJSON(map[string]interface{}{ "patients": patients, }) return } func (c *ScheduleApiController) CreateSchedule() { patientID, _ := c.GetInt64("patient_id", 0) if patientID <= 0 { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } adminUserInfo := c.GetAdminUserInfo() patientInfo, _ := service.FindPatientById(adminUserInfo.CurrentOrgId, patientID) if patientInfo.ID == 0 { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePatientNoExist) return } var schedule models.Schedule dataBody := make(map[string]interface{}, 0) err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } if dataBody["schedule_date"] == nil || reflect.TypeOf(dataBody["schedule_date"]).String() != "string" { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } scheduleDate, _ := dataBody["schedule_date"].(string) if len(scheduleDate) == 0 { utils.ErrorLog("len(schedule_date) == 0") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } timeLayout := "2006-01-02" loc, _ := time.LoadLocation("Local") theTime, err := time.ParseInLocation(timeLayout+" 15:04:05", scheduleDate+" 00:00:00", loc) if err != nil { utils.ErrorLog(err.Error()) c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } schedule.ScheduleDate = theTime.Unix() timeNow := time.Now().Format("2006-01-02") if timeNow > scheduleDate { utils.ErrorLog(timeNow) utils.ErrorLog(scheduleDate) c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCantSetScheduleBeforeNow) return } if dataBody["schedule_type"] == nil || reflect.TypeOf(dataBody["schedule_type"]).String() != "float64" { utils.ErrorLog("schedule_type") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } scheduleType := int64(dataBody["schedule_type"].(float64)) if scheduleType < 1 || scheduleType > 3 { utils.ErrorLog("scheduleType < 3") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } schedule.ScheduleType = scheduleType if dataBody["bed_id"] == nil || reflect.TypeOf(dataBody["bed_id"]).String() != "float64" { utils.ErrorLog("bed_id") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } bedId := int64(dataBody["bed_id"].(float64)) if bedId < 1 { utils.ErrorLog("bedId < 1") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } schedule.BedId = bedId if dataBody["partition_id"] == nil || reflect.TypeOf(dataBody["partition_id"]).String() != "float64" { utils.ErrorLog("partition_id") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } partitionId := int64(dataBody["partition_id"].(float64)) if partitionId < 1 { utils.ErrorLog("partitionId < 1") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } schedule.PartitionId = partitionId if dataBody["schedule_week"] == nil || reflect.TypeOf(dataBody["schedule_week"]).String() != "float64" { utils.ErrorLog("schedule_week") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } scheduleWeek := int64(dataBody["schedule_week"].(float64)) if scheduleWeek < 1 || scheduleWeek > 7 { utils.ErrorLog("scheduleWeek < 1") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } schedule.ScheduleWeek = scheduleWeek if dataBody["mode_id"] == nil || reflect.TypeOf(dataBody["mode_id"]).String() != "float64" { utils.ErrorLog("mode_id") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } modeId := int64(dataBody["mode_id"].(float64)) if modeId < 1 && modeId > 14 { utils.ErrorLog("modeId < 1") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } schedule.ModeId = modeId schedule.PatientId = patientID schedule.CreatedTime = time.Now().Unix() schedule.UpdatedTime = time.Now().Unix() schedule.Status = 1 schedule.UserOrgId = adminUserInfo.CurrentOrgId bed, _ := service.GetDeviceNumberByID(adminUserInfo.CurrentOrgId, schedule.BedId) if bed == nil { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotExist) return } if bed.ZoneID != schedule.PartitionId { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotTheZone) return } scheduleDateStart := scheduleDate + " 00:00:00" scheduleDateEnd := scheduleDate + " 23:59:59" timeLayout = "2006-01-02 15:04:05" theStartTime, _ := time.ParseInLocation(timeLayout, scheduleDateStart, loc) theEndTime, _ := time.ParseInLocation(timeLayout, scheduleDateEnd, loc) startTime := theStartTime.Unix() endTime := theEndTime.Unix() //一天只有排班一次 daySchedule, err := service.GetDaySchedule(adminUserInfo.CurrentOrgId, startTime, endTime, patientID) if daySchedule.ID > 0 { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCantSetScheduleAgainOneDay) return } //同天同位置只能排一个 pointSchedule, err := service.GetPointSchedule(adminUserInfo.CurrentOrgId, schedule.ScheduleDate, schedule.ScheduleWeek, schedule.ScheduleType, schedule.BedId) if err != nil { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } if pointSchedule.ID > 0 { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePointScheduleExist) return } err = service.CreateSchedule(&schedule) fmt.Println(err) if err != nil { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCreateScheduleFail) return } schedule.Patient = patientInfo.Name c.ServeSuccessJSON(map[string]interface{}{ "msg": "ok", "schedule": schedule, }) return } func (c *ScheduleApiController) DeleteSchedule() { id, _ := c.GetInt64("id", 0) if id <= 0 { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } adminINfo := c.GetAdminUserInfo() schedule, _ := service.GetSchedule(adminINfo.CurrentOrgId, id) if schedule == nil { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeScheduleNotExist) return } order, err := service.GetOneDialysisOrder(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.PatientId) if err != nil { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } if order != nil { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDelScheduleFailByDialysis) return } schedule.Status = 0 schedule.UpdatedTime = time.Now().Unix() err = service.UpdateSchedule(schedule) if err != nil { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteScheduleFail) return } c.ServeSuccessJSON(map[string]interface{}{ "msg": "ok", "schedule": &schedule, }) } func (c *ScheduleApiController) ChangeSchedule() { id, _ := c.GetInt64("id", 0) if id <= 0 { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } adminINfo := c.GetAdminUserInfo() schedule, _ := service.GetSchedule(adminINfo.CurrentOrgId, id) if schedule == nil { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeScheduleNotExist) return } dataBody := make(map[string]interface{}, 0) err := json.Unmarshal(c.Ctx.Input.RequestBody, &dataBody) if err != nil { utils.ErrorLog(err.Error()) c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } if dataBody["change_action"] == nil || reflect.TypeOf(dataBody["change_action"]).String() != "string" { utils.ErrorLog("change_action") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } changeAction := dataBody["change_action"].(string) if changeAction == "change_mode" { if dataBody["mode_id"] == nil || reflect.TypeOf(dataBody["mode_id"]).String() != "float64" { utils.ErrorLog("mode_id") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } modeId := int64(dataBody["mode_id"].(float64)) if modeId < 1 { utils.ErrorLog("modeId < 1") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } schedule.ModeId = modeId order, err := service.GetOneDialysisOrder(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.PatientId) if err != nil { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } if order != nil { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeChangeMode) return } } else if changeAction == "change_device" { order, err := service.GetOneDialysisOrder(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.PatientId) if err != nil { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } if order != nil { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeChangeDeviceNumber) return } if dataBody["schedule_type"] == nil || reflect.TypeOf(dataBody["schedule_type"]).String() != "float64" { utils.ErrorLog("schedule_type") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } scheduleType := int64(dataBody["schedule_type"].(float64)) if scheduleType < 1 || scheduleType > 3 { utils.ErrorLog("scheduleType < 3") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } schedule.ScheduleType = scheduleType if dataBody["bed_id"] == nil || reflect.TypeOf(dataBody["bed_id"]).String() != "float64" { utils.ErrorLog("bed_id") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } bedId := int64(dataBody["bed_id"].(float64)) if bedId < 1 { utils.ErrorLog("bedId < 1") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } schedule.BedId = bedId if dataBody["partition_id"] == nil || reflect.TypeOf(dataBody["partition_id"]).String() != "float64" { utils.ErrorLog("partition_id") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } partitionId := int64(dataBody["partition_id"].(float64)) if partitionId < 1 { utils.ErrorLog("partitionId < 1") c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } schedule.PartitionId = partitionId bed, _ := service.GetDeviceNumberByID(adminINfo.CurrentOrgId, schedule.BedId) if bed == nil { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotExist) return } if bed.ZoneID != schedule.PartitionId { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeviceNumberNotTheZone) return } startTime := schedule.ScheduleDate endTime := startTime + 86399 //一天只有排班一次 daySchedule, err := service.GetDaySchedule(adminINfo.CurrentOrgId, startTime, endTime, schedule.PatientId) if daySchedule.ID > 0 && daySchedule.ID != schedule.ID { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeCantSetScheduleAgainOneDay) return } //同天同位置只能排一个 pointSchedule, err := service.GetPointSchedule(adminINfo.CurrentOrgId, schedule.ScheduleDate, schedule.ScheduleWeek, schedule.ScheduleType, schedule.BedId) if err != nil { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } if pointSchedule.ID > 0 { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePointScheduleExist) return } } else { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } schedule.UpdatedTime = time.Now().Unix() err = service.UpdateSchedule(schedule) if err != nil { c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeChangeScheduleFail) return } c.ServeSuccessJSON(map[string]interface{}{ "msg": "ok", "schedule": &schedule, }) } // /api/schedule/print/initdata [get] // @param date:string yyyy-MM-dd 要打印的那一周中的任一天 func (this *ScheduleApiController) PrintInitData() { dateStr := this.GetString("date") var date *time.Time if len(dateStr) == 0 { now := time.Now() date = &now } else { var parseErr error date, parseErr = utils.ParseTimeStringToTime("2006-01-02", dateStr) if parseErr != nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } } adminUserInfo := this.GetAdminUserInfo() // 获取本周的排班 monday, sunday := utils.GetMondayAndSundayOfWeekDate(date) schedules, getScheduleErr := service.GetPrinitWeekSchedules(adminUserInfo.CurrentOrgId, monday.Unix(), sunday.Unix()) if getScheduleErr != nil { this.ErrorLog("获取周排班失败:%v", getScheduleErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } this.ServeSuccessJSON(map[string]interface{}{ "schedules": schedules, "monday": monday.Unix(), }) } func (this *ScheduleApiController) UrgentScheduleData() { schedule_type, _ := this.GetInt("type", 0) schedule_date := this.GetString("date") var date *time.Time if len(schedule_date) == 0 { now := time.Now() date = &now } else { var parseErr error date, parseErr = utils.ParseTimeStringToTime("2006-01-02", schedule_date) if parseErr != nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } } adminUserInfo := this.GetAdminUserInfo() deviceNumbers, getDeviceNumbersErr := service.MobileGetAllDeviceNumbersForUrgentSchedule(adminUserInfo.CurrentOrgId, date.Unix(), schedule_type) if getDeviceNumbersErr != nil { this.ErrorLog("获取所有床位失败:%v", getDeviceNumbersErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } // today := utils.ZeroHourTimeOfDay(time.Now()) schedules, getSchedulesErr := service.MobileGetOtherSchedulesForUrgentSchedule(adminUserInfo.CurrentOrgId, date.Unix(), schedule_type) if getSchedulesErr != nil { this.ErrorLog("获取所有排班失败:%v", getSchedulesErr) this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException) return } this.ServeSuccessJSON(map[string]interface{}{ "device_numbers": deviceNumbers, "schedules": schedules, }) } func (this *ScheduleApiController) SearchSchedulePatients() { keywords := this.GetString("keywords") adminUserInfo := this.GetAdminUserInfo() list, _ := service.GetSchedualPatientsByKeywords(keywords, adminUserInfo.CurrentOrgId) this.ServeSuccessJSON(map[string]interface{}{ "schdules": list, }) } type aa struct { b int64 c int64 } func (this *ScheduleApiController) GetWeekDaySchedule() { week_type, _ := this.GetInt64("week_type", -1) fmt.Println(week_type) thisTime := time.Now() weekDay := int(thisTime.Weekday()) if weekDay == 0 { weekDay = 7 } weekEnd := 7 - weekDay weekStart := weekEnd - 6 weekTitle := make([]string, 0) days := make([]string, 0) for index := weekStart; index <= weekEnd; index++ { theDay := thisTime.AddDate(0, 0, index) indexYear, indexMonthTime, indexDay := theDay.Date() indexMonth := int(indexMonthTime) indexWeek := strconv.Itoa(indexYear) + "." + strconv.Itoa(indexMonth) + "." + strconv.Itoa(indexDay) weekTitle = append(weekTitle, indexWeek) days = append(days, theDay.Format("2006-01-02")) } fmt.Println(days) var targetDayStr string switch week_type { case 1: targetDayStr = days[0] break case 2: targetDayStr = days[1] break case 3: targetDayStr = days[2] break case 4: targetDayStr = days[3] break case 5: targetDayStr = days[4] break case 6: targetDayStr = days[5] break case 7: targetDayStr = days[6] break } targetDay, parseErr := utils.ParseTimeStringToTime("2006-01-02", targetDayStr) if parseErr != nil { this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong) return } adminUserInfo := this.GetAdminUserInfo() list, _ := service.GetWeekDaySchedule(adminUserInfo.CurrentOrgId, targetDay.Unix()) this.ServeSuccessJSON(map[string]interface{}{ "schdules": list, "day": targetDayStr, }) } func Struct2Map(obj interface{}) map[string]interface{} { t := reflect.TypeOf(obj) v := reflect.ValueOf(obj) var data = make(map[string]interface{}) for i := 0; i < t.NumField(); i++ { data[t.Field(i).Name] = v.Field(i).Interface() } return data }