123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783 |
- package controllers
-
- import (
- "XT_New/enums"
- "XT_New/models"
- "XT_New/service"
- "encoding/json"
- "fmt"
- "strconv"
- "strings"
- "time"
-
- "github.com/astaxie/beego"
- )
-
- func PatientScheduleTemplateAPIControllerRegistRouters() {
- beego.Router("/api/schtemp/p/initdata", &PatientScheduleTemplateAPIController{}, "get:InitData")
- beego.Router("/api/schtemp/p/setmode", &PatientScheduleTemplateAPIController{}, "post:SetMode")
- beego.Router("/api/schtemp/p/update_sch", &PatientScheduleTemplateAPIController{}, "post:UpdateSchedules")
-
- beego.Router("/api/patients/list", &PatientScheduleTemplateAPIController{}, "get:GetPatientList")
-
- beego.Router("/api/schedule/update_schtemplate", &PatientScheduleTemplateAPIController{}, "post:UpdateSchedulesTemplate")
- beego.Router("/api/schtemplate_device/get", &ScheduleApiController{}, "Get:GetTemplateDevicesNumbers")
-
- // beego.Router("/api/test", &TestCtr{}, "get:Test")
-
- }
- func (c *ScheduleApiController) GetTemplateDevicesNumbers() {
- sch_type, _ := c.GetInt("sch_type", 0)
- zone_id, _ := c.GetInt64("zone_id", 0)
- patient_id, _ := c.GetInt64("patient_id", 0)
- template_id, _ := c.GetInt64("template_id", 0)
-
- weekday, _ := c.GetInt64("weekday")
-
- deviceNumbers, getDeviceNumbersErr := service.GetAllAvaildTemplateDeviceNumbersByZone(c.GetAdminUserInfo().CurrentOrgId, weekday, sch_type, zone_id, patient_id, template_id)
- if getDeviceNumbersErr != nil {
- c.ErrorLog("获取所有床位失败:%v", getDeviceNumbersErr)
- c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- c.ServeSuccessJSON(map[string]interface{}{
- "devices": deviceNumbers,
- })
-
- }
-
- // type TestCtr struct {
- // BaseAPIController
- // }
-
- // func (this *TestCtr) Test() {
- // now := time.Now().AddDate(0, 0, 7)
-
- // weekday := int(now.Weekday())
- // if weekday == 0 {
- // weekday = 7
- // }
-
- // monday := now.AddDate(0, 0, 1-weekday)
- // sunday := now.AddDate(0, 0, 7-weekday)
-
- // // this.TraceLog("%v", weekday)
- // this.ServeSuccessJSON(map[string]interface{}{
- // "weekday": weekday,
- // "monday": monday.Format("2006-01-02"),
- // "sunday": sunday.Format("2006-01-02"),
- // })
- // }
-
- // ------- 患者排班模板 --------
- type PatientScheduleTemplateAPIController struct {
- BaseAuthAPIController
- }
-
- // /api/schtemp/p/initdata [get]
- func (this *PatientScheduleTemplateAPIController) InitData() {
- adminUserInfo := this.GetAdminUserInfo()
-
- patitionIdStr := this.GetString("patitionid")
- var ids []string
- if len(patitionIdStr) > 0 {
- ids = strings.Split(patitionIdStr, ",")
- }
-
- mode, getModeErr := service.GetOrgPatientScheduleTemplateMode(adminUserInfo.CurrentOrgId)
- if getModeErr != nil {
- this.ErrorLog("获取排班模板启用模式失败:%v", getModeErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- now := time.Now()
- if mode == nil {
- mode = &models.PatientScheduleTemplateMode{
- OrgID: adminUserInfo.CurrentOrgId,
- Mode: 0,
- ExecuteTimes: 0,
- Status: 1,
- CreateTime: now.Unix(),
- ModifyTime: now.Unix(),
- }
- createModeErr := service.SavePatientScheduleTemplateMode(mode)
- if createModeErr != nil {
- this.ErrorLog("创建排班模板启用模式失败:%v", createModeErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- }
-
- deviceNumbers, getDeviceNumbersErr := service.GetScheduleValidDeviceNumbers(adminUserInfo.CurrentOrgId, ids)
-
- fmt.Println("deviceNumbers233323232332232323223233223323232323", deviceNumbers)
- if getDeviceNumbersErr != nil {
- this.ErrorLog("获取床位号失败:%v", getDeviceNumbersErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- templates, getTemplatesErr := service.GetOrgPatientScheduleTemplateItems(adminUserInfo.CurrentOrgId)
- if getTemplatesErr != nil {
- this.ErrorLog("获取排班模板及其排班列表失败:%v", getTemplatesErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- if len(templates) == 0 {
- firstTemp, secondTemp, thirdTemp, fourTemp, createTempErr := service.CreateTwoPatientScheduleTemplates(adminUserInfo.CurrentOrgId)
- if createTempErr != nil {
- this.ErrorLog("创建4个排班模板失败:%v", createTempErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- templates = append(templates, firstTemp, secondTemp, thirdTemp, fourTemp)
-
- } else if len(templates) == 1 {
- temp, thirdTemp, fourTemp, createTempErr := service.CreatePatientScheduleTemplate(adminUserInfo.CurrentOrgId)
- if createTempErr != nil {
- this.ErrorLog("创建3个排班模板失败:%v", createTempErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- templates = append(templates, temp, thirdTemp, fourTemp)
- } else if len(templates) == 2 {
- thirdTemp, fourTemp, createTempErr := service.CreatePatientScheduleTemplateTwo(adminUserInfo.CurrentOrgId)
- if createTempErr != nil {
- this.ErrorLog("创建2个排班模板失败:%v", createTempErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- templates = append(templates, thirdTemp, fourTemp)
- } else if len(templates) == 3 {
- temp, createTempErr := service.CreatePatientScheduleTemplateThree(adminUserInfo.CurrentOrgId)
- if createTempErr != nil {
- this.ErrorLog("创建1个排班模板失败:%v", createTempErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- templates = append(templates, temp)
- }
-
- patients, getPatientsErr := service.PatientScheduleTemplateGetAllValidPatient(adminUserInfo.CurrentOrgId)
- if getPatientsErr != nil {
- this.ErrorLog("获取患者列表失败失败:%v", getPatientsErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- // 获取本周的排班
- //monday, sunday := utils.GetMondayAndSundayOfWeekDate(&now)
- //schedules, getScheduleErr := service.GetWeekSchedule(adminUserInfo.CurrentOrgId, monday.Unix(), sunday.Unix())
- //if getScheduleErr != nil {
- // this.ErrorLog("获取本周排班失败:%v", getScheduleErr)
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- // return
- //}
-
- this.ServeSuccessJSON(map[string]interface{}{
- "template_mode": mode,
- "device_numbers": deviceNumbers,
- "templates": templates,
- "patients": patients,
- //"schedules": schedules,
- })
- }
-
- // /api/schtemp/p/setmode [post]
- // @param mode:int 0.不启用模板 1.单周模板 2.双周模板 3.三周模版 4.四周模版
- func (this *PatientScheduleTemplateAPIController) SetMode() {
- mode, _ := this.GetInt8("mode")
- week_time, _ := this.GetInt8("week_time")
-
- if mode != 1 && mode != 2 && mode != 0 && mode != 3 && mode != 4 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- if mode > 0 {
- if week_time == 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- }
-
- adminUserInfo := this.GetAdminUserInfo()
-
- templateMode, getTemplateModeErr := service.GetOrgPatientScheduleTemplateMode(adminUserInfo.CurrentOrgId)
- if getTemplateModeErr != nil {
- this.ErrorLog("获取排班模板启用模式失败:%v", getTemplateModeErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- } else if templateMode == nil {
- this.ErrorLog("获取不到排班模板启用模式")
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- templateMode.Week = int64(week_time)
- templateMode.OriginalWeek = int64(week_time)
-
- if mode != templateMode.Mode {
- if templateMode.Mode == 0 { // 0 -> 1 或 0 -> 2 则直接清除下四周的排班
- templateMode.Mode = mode
- templateMode.ExecuteTimes = 0
- templateMode.ModifyTime = time.Now().Unix()
- updateErr := service.SavePatientScheduleTemplateModeAndClearNextTwoWeekSchedules(templateMode)
- if updateErr != nil {
- this.ErrorLog("更新排班模板启用方式(0 -> 1 或 0 -> 2)时失败:%v", updateErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- } else if mode == 0 { // 1 -> 0 或 2 -> 0 则不处理排班,但清除所有模板item
- templateMode.Mode = mode
- templateMode.ExecuteTimes = 0
- templateMode.ModifyTime = time.Now().Unix()
- updateErr := service.SavePatientScheduleTemplateModeAndClearAllTemplateItems(templateMode)
- if updateErr != nil {
- this.ErrorLog("更新排班模板启用方式(1 -> 0 或 2 -> 0)时失败:%v", updateErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- } else if templateMode.Mode == 1 && mode == 2 { // 1 -> 2 清除第二周排班
- templateMode.Mode = mode
- templateMode.ExecuteTimes = 0
- templateMode.ModifyTime = time.Now().Unix()
- updateErr := service.SavePatientScheduleTemplateModeAndClearNextTwoWeekSchedules(templateMode)
- if updateErr != nil {
- this.ErrorLog("更新排班模板启用方式(1 -> 2)时失败:%v", updateErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- } else if templateMode.Mode == 1 && mode == 3 {
- templateMode.Mode = mode
- templateMode.ExecuteTimes = 0
- templateMode.ModifyTime = time.Now().Unix()
- updateErr := service.SaveTemplateMode(templateMode)
- if updateErr != nil {
- this.ErrorLog("更新排班模板启用方式(1 -> 3)时失败:%v", updateErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- } else if templateMode.Mode == 1 && mode == 4 {
- templateMode.Mode = mode
- templateMode.ExecuteTimes = 0
- templateMode.ModifyTime = time.Now().Unix()
- updateErr := service.SaveTemplateMode(templateMode)
- if updateErr != nil {
- this.ErrorLog("更新排班模板启用方式(1 -> 4)时失败:%v", updateErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- } else if templateMode.Mode == 2 && mode == 1 { // 2 -> 1 清除第二周模板item,清除第二周排班
- templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
- if getTemplateIDsErr != nil {
- this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- firstWeekItems, getFirstWeekItemsErr := service.GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(adminUserInfo.CurrentOrgId, templateIDs[0].ID)
- if getFirstWeekItemsErr != nil {
- this.ErrorLog("获取排班模板排班失败:%v", getFirstWeekItemsErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- templateMode.Mode = mode
- templateMode.ExecuteTimes = 0
- templateMode.ModifyTime = time.Now().Unix()
- updateErr := service.SavePatientScheduleTemplateMode2To1(templateMode, templateIDs[1].ID, firstWeekItems)
- if updateErr != nil {
- this.ErrorLog("更新排班模板启用方式(1 -> 2)时失败:%v", updateErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- } else if templateMode.Mode == 2 && mode == 3 { //
- templateMode.Mode = mode
- templateMode.ExecuteTimes = 0
- templateMode.ModifyTime = time.Now().Unix()
- updateErr := service.SaveTemplateMode(templateMode)
- if updateErr != nil {
- this.ErrorLog("更新排班模板启用方式(1 -> 4)时失败:%v", updateErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- } else if templateMode.Mode == 2 && mode == 4 { //
- templateMode.Mode = mode
- templateMode.ExecuteTimes = 0
- templateMode.ModifyTime = time.Now().Unix()
- updateErr := service.SaveTemplateMode(templateMode)
- if updateErr != nil {
- this.ErrorLog("更新排班模板启用方式(1 -> 4)时失败:%v", updateErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- } else if templateMode.Mode == 3 && mode == 1 { // 3 -> 1 清除第三,二周模板item,清除第三,二周排班
- templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
- if getTemplateIDsErr != nil {
- this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- templateMode.Mode = mode
- templateMode.ExecuteTimes = 0
- templateMode.ModifyTime = time.Now().Unix()
- updateErr := service.SavePatientScheduleTemplateMode3To1(templateMode, templateIDs[1].ID, templateIDs[2].ID)
- if updateErr != nil {
- this.ErrorLog("更新排班模板启用方式(1 -> 3)时失败:%v", updateErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- } else if templateMode.Mode == 3 && mode == 2 { // 3 -> 1 清除第三周模板item,清除第三周排班,将第一周的排班重复到第二周
- templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
- if getTemplateIDsErr != nil {
- this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- templateMode.Mode = mode
- templateMode.ExecuteTimes = 0
- templateMode.ModifyTime = time.Now().Unix()
- updateErr := service.SavePatientScheduleTemplateMode3To2(templateMode, templateIDs[2].ID)
- if updateErr != nil {
- this.ErrorLog("更新排班模板启用方式(2 -> 3)时失败:%v", updateErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- } else if templateMode.Mode == 3 && mode == 4 { // 2 -> 1 清除第二周模板item,清除第二周排班
- templateMode.Mode = mode
- templateMode.ExecuteTimes = 0
- templateMode.ModifyTime = time.Now().Unix()
- updateErr := service.SaveTemplateMode(templateMode)
- if updateErr != nil {
- this.ErrorLog("更新排班模板启用方式(1 -> 4)时失败:%v", updateErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- } else if templateMode.Mode == 4 && mode == 1 { // 4 -> 1 清除第三周模板item,清除第三周排班,将第一周的排班重复到第二周
- templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
- if getTemplateIDsErr != nil {
- this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- templateMode.Mode = mode
- templateMode.ExecuteTimes = 0
- templateMode.ModifyTime = time.Now().Unix()
- updateErr := service.SavePatientScheduleTemplateMode4To1(templateMode, templateIDs[1].ID, templateIDs[2].ID, templateIDs[3].ID)
- if updateErr != nil {
- this.ErrorLog("更新排班模板启用方式(1 -> 4)时失败:%v", updateErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- } else if templateMode.Mode == 4 && mode == 2 { // 4 -> 2
- templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
- if getTemplateIDsErr != nil {
- this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- templateMode.Mode = mode
- templateMode.ExecuteTimes = 0
- templateMode.ModifyTime = time.Now().Unix()
- updateErr := service.SavePatientScheduleTemplateMode4To2(templateMode, templateIDs[2].ID, templateIDs[3].ID)
- if updateErr != nil {
- this.ErrorLog("更新排班模板启用方式(2 -> 4)时失败:%v", updateErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- } else if templateMode.Mode == 4 && mode == 3 { // 4 -> 3
- templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
- if getTemplateIDsErr != nil {
- this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- templateMode.Mode = mode
- templateMode.ExecuteTimes = 0
- templateMode.ModifyTime = time.Now().Unix()
- updateErr := service.SavePatientScheduleTemplateMode4To3(templateMode, templateIDs[3].ID)
- if updateErr != nil {
- this.ErrorLog("更新排班模板启用方式(3 -> 4)时失败:%v", updateErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- }
- templates, _ := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
- //处理第几周数问题
- if mode == 0 {
- service.UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, 0)
- service.UpdateTemplateItemWeek(templates[1].OrgID, templates[1].ID, 0)
- service.UpdateTemplateItemWeek(templates[2].OrgID, templates[2].ID, 0)
- service.UpdateTemplateItemWeek(templates[3].OrgID, templates[3].ID, 0)
-
- } else if mode == 1 {
- service.UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, week_time)
- service.UpdateTemplateItemWeek(templates[1].OrgID, templates[1].ID, 0)
- service.UpdateTemplateItemWeek(templates[2].OrgID, templates[2].ID, 0)
- service.UpdateTemplateItemWeek(templates[3].OrgID, templates[3].ID, 0)
-
- } else if mode == 2 {
- service.UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, week_time)
- service.UpdateTemplateItemWeek(templates[1].OrgID, templates[1].ID, week_time+1)
- service.UpdateTemplateItemWeek(templates[2].OrgID, templates[2].ID, 0)
- service.UpdateTemplateItemWeek(templates[3].OrgID, templates[3].ID, 0)
-
- } else if mode == 3 {
- service.UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, week_time)
- service.UpdateTemplateItemWeek(templates[1].OrgID, templates[1].ID, week_time+1)
- service.UpdateTemplateItemWeek(templates[2].OrgID, templates[2].ID, week_time+2)
- service.UpdateTemplateItemWeek(templates[3].OrgID, templates[3].ID, 0)
-
- } else if mode == 4 {
- service.UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, week_time)
- service.UpdateTemplateItemWeek(templates[1].OrgID, templates[1].ID, week_time+1)
- service.UpdateTemplateItemWeek(templates[2].OrgID, templates[2].ID, week_time+2)
- service.UpdateTemplateItemWeek(templates[3].OrgID, templates[3].ID, week_time+3)
- }
-
- } else {
- fmt.Println("~~~~~~~~")
- service.UpdateTemplateItemModeWeek(adminUserInfo.CurrentOrgId, templateMode.ID, templateMode.OriginalWeek)
-
- }
-
- this.ServeSuccessJSON(nil)
- }
-
- // /api/schtemp/p/update_sch [post]
- // @param template_id:int
- // @param add_schs:array'json ([ {device_number_id, patient_id, mode(treat_mode), weekday, time_type} ])
- // @param del_schs:array'json ([ {device_number_id, weekday, time_type} ])
- // @param change_schs:array'json ([ {device_number_id, patient_id, mode(treat_mode), weekday, time_type} ])
- func (this *PatientScheduleTemplateAPIController) UpdateSchedules() {
- templateID, _ := this.GetInt64("template_id")
- addSchedulesJSON := this.GetString("add_schs")
- delSchedulesJSON := this.GetString("del_schs")
- changeSchedulesJSON := this.GetString("change_schs")
- if len(addSchedulesJSON) == 0 || len(delSchedulesJSON) == 0 || len(changeSchedulesJSON) == 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- var addSchedules []*ParamModelSchedule
- var delSchedules []*ParamModelSchedule
- var changeSchedules []*ParamModelSchedule
- parseErr := json.Unmarshal([]byte(addSchedulesJSON), &addSchedules)
- if parseErr != nil {
- this.ErrorLog("解析增加排班的数组参数失败:%v", parseErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- parseErr = json.Unmarshal([]byte(delSchedulesJSON), &delSchedules)
- if parseErr != nil {
- this.ErrorLog("解析删除排班的数组参数失败:%v", parseErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- parseErr = json.Unmarshal([]byte(changeSchedulesJSON), &changeSchedules)
- if parseErr != nil {
- this.ErrorLog("解析修改排班的数组参数失败:%v", parseErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- adminUserInfo := this.GetAdminUserInfo()
- templateIDs, getTemplateIDsErr := service.GetOrgPatientScheduleTemplateIDs(adminUserInfo.CurrentOrgId)
- if getTemplateIDsErr != nil {
- this.ErrorLog("获取排班模板失败:%v", getTemplateIDsErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- } else {
- isTemplateIDExist := false
- for _, templateIDObject := range templateIDs {
- if templateIDObject.ID == templateID {
- isTemplateIDExist = true
- break
- }
- }
- if isTemplateIDExist == false {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeScheduleTemplateNotExist)
- return
- }
- }
-
- addTemplateItems := make([]*models.PatientScheduleTemplateItem, 0, len(addSchedules))
- delTemplateItems := make([]*models.PatientScheduleTemplateItem, 0, len(delSchedules))
- changeTemplateItems := make([]*models.PatientScheduleTemplateItem, 0, len(changeSchedules))
- for _, sch := range addSchedules {
- if sch.DeviceNumberID <= 0 || sch.PatientID <= 0 || sch.Weekday < 1 || sch.Weekday > 7 || sch.TimeType < 1 || sch.TimeType > 3 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- addTemplateItems = append(addTemplateItems, &models.PatientScheduleTemplateItem{
- DeviceNumberID: sch.DeviceNumberID,
- PatientID: sch.PatientID,
- TreatMode: sch.Mode,
- Weekday: sch.Weekday,
- TimeType: sch.TimeType,
- })
- }
- for _, sch := range delSchedules {
- if sch.DeviceNumberID <= 0 || sch.Weekday < 1 || sch.Weekday > 7 || sch.TimeType < 1 || sch.TimeType > 3 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- delTemplateItems = append(delTemplateItems, &models.PatientScheduleTemplateItem{
- DeviceNumberID: sch.DeviceNumberID,
- Weekday: sch.Weekday,
- TimeType: sch.TimeType,
- })
- }
- for _, sch := range changeSchedules {
- if sch.DeviceNumberID <= 0 || sch.PatientID <= 0 || sch.Weekday < 1 || sch.Weekday > 7 || sch.TimeType < 1 || sch.TimeType > 3 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- changeTemplateItems = append(changeTemplateItems, &models.PatientScheduleTemplateItem{
- DeviceNumberID: sch.DeviceNumberID,
- PatientID: sch.PatientID,
- TreatMode: sch.Mode,
- Weekday: sch.Weekday,
- TimeType: sch.TimeType,
- })
- }
-
- templateMode, getTemplateModeErr := service.GetOrgPatientScheduleTemplateMode(adminUserInfo.CurrentOrgId)
- if getTemplateModeErr != nil {
- this.ErrorLog("获取排班模板启用模式失败:%v", getTemplateModeErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- } else if templateMode == nil {
- this.ErrorLog("获取不到排班模板启用模式")
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- //shouldUpdateNextWeekSchedules := false
- //shouldUpdateNextSecondWeekSchedules := false
- //shouldUpdateNextThirdWeekSchedules := false
- //shouldUpdateNextFourWeekSchedules := false
- //
- if templateMode.Mode == 0 {
- this.ErrorLog("当前未开启排班模板,所以更新模板的操作将被无视")
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- //判断当前用的是模版几
- //templateIDIndex := 1
- //for index, templateIDObject := range templateIDs {
- // if templateIDObject.ID == templateID {
- // //templateIDIndex = index + 1
- // }
- //}
- //if templateMode.Mode == 1 {
- // if templateIDIndex == 1 {
- // shouldUpdateNextWeekSchedules = true
- // shouldUpdateNextSecondWeekSchedules = true
- // } else {
- // this.ErrorLog("当前开启的是单周模板,所以不会更新第二个模板")
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- // return
- // }
- //
- //} else if templateMode.Mode == 2 { // mode == 2 => 开启双周模板时,只需更新一周排班
- // if templateIDIndex == 1 {
- // if templateMode.ExecuteTimes%2 == 0 {
- // shouldUpdateNextWeekSchedules = true
- // shouldUpdateNextSecondWeekSchedules = false
- // } else {
- // shouldUpdateNextWeekSchedules = false
- // shouldUpdateNextSecondWeekSchedules = true
- // }
- //
- // } else {
- // if templateMode.ExecuteTimes%2 == 0 {
- // shouldUpdateNextWeekSchedules = false
- // shouldUpdateNextSecondWeekSchedules = true
- // } else {
- // shouldUpdateNextWeekSchedules = true
- // shouldUpdateNextSecondWeekSchedules = false
- // }
- //
- // }
- //} else if templateMode.Mode == 3 { // mode == 2 => 开启双周模板时,只需更新一周排班
- // if templateIDIndex == 1 { //当前修改的是第一个模版
- // if templateMode.ExecuteTimes%3 == 0 {
- // shouldUpdateNextWeekSchedules = true
- // shouldUpdateNextSecondWeekSchedules = false
- // shouldUpdateNextThirdWeekSchedules = false
- // } else if templateMode.ExecuteTimes%3 == 1 {
- // shouldUpdateNextWeekSchedules = false
- // shouldUpdateNextSecondWeekSchedules = true
- // shouldUpdateNextThirdWeekSchedules = false
- // } else {
- // shouldUpdateNextWeekSchedules = false
- // shouldUpdateNextSecondWeekSchedules = false
- // shouldUpdateNextThirdWeekSchedules = true
- // }
- // } else if templateIDIndex == 2 {
- // if templateMode.ExecuteTimes%2 == 0 {
- // shouldUpdateNextWeekSchedules = false
- // shouldUpdateNextSecondWeekSchedules = true
- // } else {
- // shouldUpdateNextWeekSchedules = true
- // shouldUpdateNextSecondWeekSchedules = false
- // }
- //
- // } else {
- //
- // }
- //}
-
- updateErr := service.UpdateScheduleTemplate(adminUserInfo.CurrentOrgId, templateID, addTemplateItems, delTemplateItems, changeTemplateItems, false, false)
- if updateErr != nil {
- this.ErrorLog("更新排班模板排班失败:%v", updateErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
- return
- }
-
- //newItems, getTemplateItemsErr := service.GetAllPatientScheduleTemplateItemsWithoutPatientByTemplateID(adminUserInfo.CurrentOrgId)
- templates, getTemplateItemsErr := service.GetOrgPatientScheduleTemplateItems(adminUserInfo.CurrentOrgId)
-
- //newItems, getTemplateItemsErr := service.GetAllPatientScheduleTemplateItemsWithoutPatientByTemplateID(adminUserInfo.CurrentOrgId)
- if getTemplateItemsErr != nil {
- this.ErrorLog("获取排班模板排班失败:%v", getTemplateItemsErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
- this.ServeSuccessJSON(map[string]interface{}{
- "items": templates,
- })
- }
-
- type ParamModelSchedule struct {
- DeviceNumberID int64 `json:"device_number_id"`
- PatientID int64 `json:"patient_id"`
- Mode int64 `json:"mode"`
- Weekday int8 `json:"weekday"`
- TimeType int8 `json:"time_type"`
- }
-
- func (this *PatientScheduleTemplateAPIController) GetPatientList() {
- patients, err := service.GetSchTemplatePatientList(this.GetAdminUserInfo().CurrentOrgId)
- if err != nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- }
- this.ServeSuccessJSON(map[string]interface{}{
- "list": patients,
- })
-
- }
-
- type ParamModelEditSchedule struct {
- ID int64 `json:"sch_id"`
- DeviceNumberID int64 `json:"jihao_id"`
- PatientID int64 `json:"patient_id"`
- Mode int64 `json:"mode_id"`
- Weekday int64 `json:"schedule_week"`
- TimeType int64 `json:"time_type"`
- TemplateID int64 `json:"t_id"`
- Title string `json:"title"`
- }
-
- func (this *PatientScheduleTemplateAPIController) UpdateSchedulesTemplate() {
- addOrEditSchedulesJSON := this.GetString("edit_schs")
- //delSchedulesJSON := this.GetString("del_schs")
-
- fmt.Println(addOrEditSchedulesJSON)
- var addOrEditSchedules []*ParamModelEditSchedule
- //var delSchedules []*ParamModelEditSchedule
-
- parseErr := json.Unmarshal([]byte(addOrEditSchedulesJSON), &addOrEditSchedules)
- //parseDelErr := json.Unmarshal([]byte(delSchedulesJSON), &delSchedules)
-
- fmt.Println(parseErr)
- if parseErr != nil {
- this.ErrorLog("解析增加编辑排班的数组参数失败:%v", parseErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
- //if parseDelErr != nil {
- // this.ErrorLog("解析删除排班的数组参数失败:%v", parseErr)
- // this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- // return
- //}
-
- for _, item := range addOrEditSchedules {
-
- if item.ID > 0 { //已经存在的数据
- src_template, _ := service.GetScheduleTemplateItem(item.ID)
- src_template.DeviceNumberID = item.DeviceNumberID
- src_template.TimeType = int8(item.TimeType)
- src_template.TreatMode = item.Mode
- service.SaveSchTemplateItem(src_template)
-
- } else {
- schItemTwo, _ := service.GetScheduleTemplateTwo(this.GetAdminUserInfo().CurrentOrgId, item.TemplateID, item.Weekday, item.PatientID)
- fmt.Println("222222")
-
- if schItemTwo.ID > 0 { //当天存在排班,存在则为修改
- fmt.Println("333333333")
- src_template, _ := service.GetScheduleTemplateItem(schItemTwo.ID)
- src_template.DeviceNumberID = item.DeviceNumberID
- src_template.TimeType = int8(item.TimeType)
- src_template.TreatMode = item.Mode
- service.SaveSchTemplateItem(src_template)
-
- } else {
- schItem, _ := service.GetScheduleTemplateOne(this.GetAdminUserInfo().CurrentOrgId, item.DeviceNumberID, item.TemplateID, item.Weekday, item.TimeType)
- if schItem.ID == 0 { //床位未被占用
- var template models.PatientScheduleTemplateItem
- template.TreatMode = item.Mode
- template.TemplateID = int64(item.TemplateID)
- template.TimeType = int8(item.TimeType)
- template.DeviceNumberID = item.DeviceNumberID
- template.CreateTime = time.Now().Unix()
- template.ModifyTime = time.Now().Unix()
- template.OrgID = this.GetAdminUserInfo().CurrentOrgId
- template.PatientID = item.PatientID
- template.Weekday = int8(item.Weekday)
- template.Status = 1
- service.SaveSchTemplateItem(template)
- } else { //床位被占用 1.被自己占用,2。被别人占用
- if schItem.PatientID != 0 && schItem.PatientID == item.PatientID { //当天该患者已经存在排班则设为修改操作
- src_template, _ := service.GetScheduleTemplateItem(schItem.ID)
- src_template.DeviceNumberID = item.DeviceNumberID
- src_template.TimeType = int8(item.TimeType)
- src_template.TreatMode = item.Mode
- service.SaveSchTemplateItem(src_template)
-
- } else if schItem.PatientID != 0 && schItem.PatientID != item.PatientID { //当天该床位被其他人占用,则无法保存
- //fmt.Println("7777777")
- json := make(map[string]interface{})
- json["msg"] = item.Title + "周" + strconv.FormatInt(item.Weekday, 10) + "的床位已经有人排班,无法保存"
- json["state"] = 0
- this.Data["json"] = json
- this.ServeJSON()
- break
- }
- }
-
- }
-
- }
- }
-
- templates, _ := service.GetOrgPatientScheduleTemplateItems(this.GetAdminUserInfo().CurrentOrgId)
- this.ServeSuccessJSON(map[string]interface{}{
- "items": templates,
- })
- }
|