123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056 |
- package service
-
- import (
- "fmt"
- "time"
-
- "XT_New/models"
- "XT_New/utils"
-
- "github.com/jinzhu/gorm"
- "github.com/robfig/cron"
- )
-
- // cron表达式 https://www.cnblogs.com/zuxingyu/p/6023919.html
- var createWeekSchedulesCronJob *cron.Cron
-
- func init() {
- utils.InfoLog("开启自动排班定时任务")
- createWeekSchedulesCronJob = cron.New()
- spec := "0 0 0 ? * MON" // 每周一凌晨0点生成排班
- //spec := "0 */1 * * * ?"
- createWeekSchedulesCronJob.AddFunc(spec, func() {
- //AutoCreateWeekSchedules()
- NewAutoCreateWeekSchedules()
- })
- }
-
- func BeginAutoCreateWeekSchedulesJob() {
- createWeekSchedulesCronJob.Start()
- }
-
- func NewAutoCreateWeekSchedules() {
- utils.TraceLog("自动排班任务开始执行")
- var modes []*models.PatientScheduleTemplateMode
- getModesErr := readDb.Model(&models.PatientScheduleTemplateMode{}).Where("mode <> 0 AND status = 1 ").Find(&modes).Error
- if getModesErr != nil {
- utils.ErrorLog("自动排班-获取启用模板模式列表失败:%v", getModesErr)
- CreateDBErrorRecord(0, "自动排班-获取启用模板模式列表失败:%v", getModesErr)
- return
- }
-
- for _, mode := range modes {
- templates, getTemplatesErr := GetOrgPatientScheduleTemplateIDs(mode.OrgID)
- if getTemplatesErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取模板 ID 失败:%v", mode.OrgID, getTemplatesErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取模板 ID 失败:%v", mode.OrgID, getTemplatesErr)
- continue
- } else if len(templates) == 0 {
- utils.WarningLog("自动排班-org_id: %v :没有模板", mode.OrgID)
- continue
- }
-
- now := time.Now()
- nextSecWeek := now.AddDate(0, 0, 14)
- //nextThridWeek := now.AddDate(0, 0, 21)
- //nextFourWeek := now.AddDate(0, 0, 28)
- curWeek := now.AddDate(0, 0, 0)
- nextWeek := now.AddDate(0, 0, 7)
-
- tx := writeDb.Begin()
- thisTime := time.Now()
- _, theWeek := thisTime.ISOWeek()
- fmt.Println(theWeek)
-
- //mode.Week == 0 为了兼容开发新功能前的老数据
- //新功能加了,预约排班生成的,int64(theWeek) >= mode.Week 当判断当前周数是否小大于或者等于模版预约第几周生成的周数
- if mode.Week == 0 || int64(theWeek) >= mode.Week {
- if mode.Week > 0 { //新机构或者重新设置模版的机构需要帮他们根据选择的模版模式,来生成本周,下周,下下周数据
- if mode.Mode == 1 {
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
-
- }
- insertErr, count := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, curWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
-
- } else {
- //更新周次
- templates[0].Week = count
- UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, int8(templates[0].Week))
-
- }
-
- items2, getItemsErr2 := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
- if getItemsErr2 != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr2)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr2)
- continue
-
- }
- insertErr2, count2 := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextWeek, items2, nil)
- if insertErr2 != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr2)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr2)
- tx.Rollback()
- continue
-
- } else {
- //更新周次
- templates[0].Week = count2
- UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, int8(templates[0].Week))
-
- }
-
- items3, getItemsErr3 := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
- if getItemsErr3 != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr3)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr3)
- continue
-
- }
- insertErr3, count3 := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items3, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr3)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr3)
- tx.Rollback()
- continue
-
- } else {
- //更新周次
- templates[0].Week = count3
- UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, int8(templates[0].Week))
-
- }
-
- mode.Week = 0
- mode.ExecuteTimes += 3
- mode.ModifyTime = now.Unix()
- updateModeErr := tx.Save(mode).Error
- if updateModeErr != nil {
- utils.ErrorLog("自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- tx.Rollback()
- continue
-
- }
- tx.Commit()
- utils.SuccessLog("自动排班-插入排班成功:org_id: %v", mode.OrgID)
- time.Sleep(time.Second * 30)
-
- } else if mode.Mode == 2 {
-
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
-
- }
- insertErr, count := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, curWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
-
- } else {
- //更新周次
- templates[0].Week = count
- UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, int8(templates[0].Week))
- }
-
- items2, getItemsErr2 := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[1].ID)
- if getItemsErr2 != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr2)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr2)
- continue
-
- }
- insertErr2, count2 := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextWeek, items2, nil)
- if insertErr2 != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr2)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr2)
- tx.Rollback()
- continue
-
- } else {
- //更新周次
- templates[1].Week = count2
- UpdateTemplateItemWeek(templates[1].OrgID, templates[1].ID, int8(templates[1].Week))
-
- }
-
- items3, getItemsErr3 := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
- if getItemsErr3 != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr3)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr3)
- continue
-
- }
- insertErr3, count3 := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items3, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr3)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr3)
- tx.Rollback()
- continue
-
- } else {
- //更新周次
- templates[0].Week = count3
- UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, int8(templates[0].Week))
-
- }
-
- mode.Week = 0
- mode.ExecuteTimes += 3
- mode.ModifyTime = now.Unix()
- updateModeErr := tx.Save(mode).Error
- if updateModeErr != nil {
- utils.ErrorLog("自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- tx.Rollback()
- continue
-
- }
- tx.Commit()
- utils.SuccessLog("自动排班-插入排班成功:org_id: %v", mode.OrgID)
- time.Sleep(time.Second * 30)
-
- } else if mode.Mode == 3 || mode.Mode == 4 {
- fmt.Println("---------")
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
-
- }
- insertErr, count := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, curWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
-
- } else {
- //更新周次
- templates[0].Week = count
- UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, int8(templates[0].Week))
-
- }
-
- items12, getItemsErr12 := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[1].ID)
- fmt.Println("------22222")
- if getItemsErr12 != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr12)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr12)
- continue
-
- }
- insertErr12, count12 := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextWeek, items12, nil)
- if insertErr12 != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr12)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr12)
- tx.Rollback()
- continue
-
- } else {
- //更新周次
- templates[1].Week = count12
- UpdateTemplateItemWeek(templates[1].OrgID, templates[1].ID, int8(templates[1].Week))
-
- }
-
- items3, getItemsErr3 := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[2].ID)
- if getItemsErr3 != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr3)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr3)
- continue
-
- }
- insertErr3, count3 := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items3, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr3)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入本周排班失败:%v", mode.OrgID, insertErr3)
- tx.Rollback()
- continue
-
- } else {
- //更新周次
- templates[2].Week = count3
- UpdateTemplateItemWeek(templates[2].OrgID, templates[2].ID, int8(templates[2].Week))
-
- }
-
- mode.Week = 0
- mode.ExecuteTimes += 3
- mode.ModifyTime = now.Unix()
- updateModeErr := tx.Save(mode).Error
- if updateModeErr != nil {
- utils.ErrorLog("自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- tx.Rollback()
- continue
-
- }
- tx.Commit()
- utils.SuccessLog("自动排班-插入排班成功:org_id: %v", mode.OrgID)
- time.Sleep(time.Second * 30)
- }
- } else {
- if mode.Mode == 1 { // 用第一周模板生成下第二周的排班
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
- }
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
- } else {
- //thisTime := time.Now()
- //_, theWeek := thisTime.ISOWeek()
-
- //更新周次
- templates[0].Week = GetWeeks(nextSecWeek.Format("2006-01-02 15:04:05"))
- UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, int8(templates[0].Week))
- }
-
- mode.Week = 0
- mode.ExecuteTimes += 1
- mode.ModifyTime = now.Unix()
- updateModeErr := tx.Save(mode).Error
- if updateModeErr != nil {
- utils.ErrorLog("自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- tx.Rollback()
- continue
- }
- tx.Commit()
- utils.SuccessLog("自动排班-插入排班成功:org_id: %v", mode.OrgID)
- time.Sleep(time.Second * 30)
-
- } else if mode.Mode == 2 { // mode.ExecuteTimes % 2 的结果,如果为0,用第一周模板生成下第二周的排班,否则用第二周的模板生成排班
- if mode.ExecuteTimes%2 == 0 {
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
- }
-
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
- } else {
-
- templates[0].Week = GetWeeks(nextSecWeek.Format("2006-01-02 15:04:05"))
-
- UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, int8(templates[0].Week))
-
- }
-
- mode.Week = 0
- mode.ExecuteTimes += 1
- mode.ModifyTime = now.Unix()
- updateModeErr := tx.Save(mode).Error
- if updateModeErr != nil {
- utils.ErrorLog("自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- tx.Rollback()
- continue
- }
- tx.Commit()
- utils.SuccessLog("自动排班-插入排班成功:org_id: %v", mode.OrgID)
- time.Sleep(time.Second * 30)
-
- } else {
- if len(templates) < 2 {
- utils.WarningLog("自动排班-org_id: %v :没有第二周的模板")
- continue
- }
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[1].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第二周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第二周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
- }
-
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第二周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第二周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
- } else {
-
- //thisTime := time.Now()
- //_, week := thisTime.ISOWeek()
-
- //更新周次
- templates[1].Week = GetWeeks(nextSecWeek.Format("2006-01-02 15:04:05"))
-
- //templates[1].Week = week + 2
- UpdateTemplateItemWeek(templates[1].OrgID, templates[1].ID, int8(templates[1].Week))
-
- }
-
- mode.Week = 0
- mode.ExecuteTimes += 1
- mode.ModifyTime = now.Unix()
- updateModeErr := tx.Save(mode).Error
- if updateModeErr != nil {
- utils.ErrorLog("自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- tx.Rollback()
- continue
- }
- tx.Commit()
- utils.SuccessLog("自动排班-插入排班成功:org_id: %v", mode.OrgID)
- time.Sleep(time.Second * 30)
-
- }
- } else if mode.Mode == 3 { // mode.ExecuteTimes % 2 的结果,如果为0,用第一周模板生成下第二周的排班,否则用第二周的模板生成排班
- if mode.ExecuteTimes%3 == 0 {
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
- }
-
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入下第三周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入下第三周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
- } else {
- templates[0].Week = GetWeeks(nextSecWeek.Format("2006-01-02 15:04:05"))
-
- UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, int8(templates[0].Week))
- }
- mode.Week = 0
- mode.ExecuteTimes += 1
- mode.ModifyTime = now.Unix()
- updateModeErr := tx.Save(mode).Error
- if updateModeErr != nil {
- utils.ErrorLog("自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- tx.Rollback()
- continue
- }
- tx.Commit()
- utils.SuccessLog("自动排班-插入排班成功:org_id: %v", mode.OrgID)
- time.Sleep(time.Second * 30)
-
- } else if mode.ExecuteTimes%3 == 1 {
- if len(templates) < 2 {
- utils.WarningLog("自动排班-org_id: %v :没有第二周的模板")
- continue
- }
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[1].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
- }
-
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
- } else {
- templates[1].Week = GetWeeks(nextSecWeek.Format("2006-01-02 15:04:05"))
-
- UpdateTemplateItemWeek(templates[1].OrgID, templates[1].ID, int8(templates[1].Week))
-
- }
- mode.Week = 0
- mode.ExecuteTimes += 1
- mode.ModifyTime = now.Unix()
- updateModeErr := tx.Save(mode).Error
- if updateModeErr != nil {
- utils.ErrorLog("自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- tx.Rollback()
- continue
- }
- tx.Commit()
- utils.SuccessLog("自动排班-插入排班成功:org_id: %v", mode.OrgID)
- time.Sleep(time.Second * 30)
-
- } else {
- if len(templates) < 3 {
- utils.WarningLog("自动排班-org_id: %v :没有第三周的模板")
- continue
- }
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[2].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第三周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第三周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
- }
-
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第三周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第三周模板插入下第三周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
- } else {
- templates[2].Week = GetWeeks(nextSecWeek.Format("2006-01-02 15:04:05"))
-
- UpdateTemplateItemWeek(templates[2].OrgID, templates[2].ID, int8(templates[2].Week))
-
- }
- mode.Week = 0
- mode.ExecuteTimes += 1
- mode.ModifyTime = now.Unix()
- updateModeErr := tx.Save(mode).Error
- if updateModeErr != nil {
- utils.ErrorLog("自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- tx.Rollback()
- continue
- }
- tx.Commit()
- utils.SuccessLog("自动排班-插入排班成功:org_id: %v", mode.OrgID)
- time.Sleep(time.Second * 30)
-
- }
- } else if mode.Mode == 4 { // mode.ExecuteTimes % 2 的结果,如果为0,用第一周模板生成下第二周的排班,否则用第二周的模板生成排班
- if mode.ExecuteTimes%4 == 0 {
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
- }
-
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
- } else {
- templates[0].Week = GetWeeks(nextSecWeek.Format("2006-01-02 15:04:05"))
-
- UpdateTemplateItemWeek(templates[0].OrgID, templates[0].ID, int8(templates[0].Week))
-
- }
- mode.Week = 0
- mode.ExecuteTimes += 1
- mode.ModifyTime = now.Unix()
- updateModeErr := tx.Save(mode).Error
- if updateModeErr != nil {
- utils.ErrorLog("自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- tx.Rollback()
- continue
- }
- tx.Commit()
- utils.SuccessLog("自动排班-插入排班成功:org_id: %v", mode.OrgID)
- time.Sleep(time.Second * 30)
-
- } else if mode.ExecuteTimes%4 == 1 {
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[1].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
- }
-
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
- } else {
- templates[1].Week = GetWeeks(nextSecWeek.Format("2006-01-02 15:04:05"))
-
- UpdateTemplateItemWeek(templates[1].OrgID, templates[1].ID, int8(templates[1].Week))
-
- }
- mode.Week = 0
- mode.ExecuteTimes += 1
- mode.ModifyTime = now.Unix()
- updateModeErr := tx.Save(mode).Error
- if updateModeErr != nil {
- utils.ErrorLog("自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- tx.Rollback()
- continue
- }
- tx.Commit()
- utils.SuccessLog("自动排班-插入排班成功:org_id: %v", mode.OrgID)
- time.Sleep(time.Second * 30)
-
- } else if mode.ExecuteTimes%4 == 2 {
- if len(templates) < 2 {
- utils.WarningLog("自动排班-org_id: %v :没有第二周的模板")
- continue
- }
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[2].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
- }
-
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
- } else {
- templates[2].Week = GetWeeks(nextSecWeek.Format("2006-01-02 15:04:05"))
-
- UpdateTemplateItemWeek(templates[2].OrgID, templates[2].ID, int8(templates[2].Week))
-
- }
- mode.Week = 0
- mode.ExecuteTimes += 1
- mode.ModifyTime = now.Unix()
- updateModeErr := tx.Save(mode).Error
- if updateModeErr != nil {
- utils.ErrorLog("自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- tx.Rollback()
- continue
- }
- tx.Commit()
- utils.SuccessLog("自动排班-插入排班成功:org_id: %v", mode.OrgID)
- time.Sleep(time.Second * 30)
-
- } else if mode.ExecuteTimes%4 == 3 {
- if len(templates) < 3 {
- utils.WarningLog("自动排班-org_id: %v :没有第三周的模板")
- continue
- }
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[3].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第三周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第三周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
- }
-
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入下第四周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入下第三周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
- } else {
- templates[3].Week = GetWeeks(nextSecWeek.Format("2006-01-02 15:04:05"))
-
- UpdateTemplateItemWeek(templates[3].OrgID, templates[3].ID, int8(templates[3].Week))
-
- }
-
- mode.Week = 0
- mode.ExecuteTimes += 1
- mode.ModifyTime = now.Unix()
- updateModeErr := tx.Save(mode).Error
- if updateModeErr != nil {
- utils.ErrorLog("自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- tx.Rollback()
- continue
- }
- tx.Commit()
- utils.SuccessLog("自动排班-插入排班成功:org_id: %v", mode.OrgID)
- time.Sleep(time.Second * 30)
-
- }
- }
-
- }
- utils.SuccessLog("自动排班任务完成")
- }
- }
-
- }
-
- func AutoCreateWeekSchedules() {
- utils.TraceLog("自动排班任务开始执行")
- var modes []*models.PatientScheduleTemplateMode
- getModesErr := readDb.Model(&models.PatientScheduleTemplateMode{}).Where("mode <> 0 AND status = 1").Find(&modes).Error
- if getModesErr != nil {
- utils.ErrorLog("自动排班-获取启用模板模式列表失败:%v", getModesErr)
- CreateDBErrorRecord(0, "自动排班-获取启用模板模式列表失败:%v", getModesErr)
- return
- }
-
- for _, mode := range modes {
- templates, getTemplatesErr := GetOrgPatientScheduleTemplateIDs(mode.OrgID)
- if getTemplatesErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取模板 ID 失败:%v", mode.OrgID, getTemplatesErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取模板 ID 失败:%v", mode.OrgID, getTemplatesErr)
- continue
- } else if len(templates) == 0 {
- utils.WarningLog("自动排班-org_id: %v :没有模板", mode.OrgID)
- continue
- }
-
- now := time.Now()
- nextSecWeek := now.AddDate(0, 0, 14)
- tx := writeDb.Begin()
-
- if mode.Mode == 1 { // 用第一周模板生成下第二周的排班
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
- }
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
- }
-
- } else if mode.Mode == 2 { // mode.ExecuteTimes % 2 的结果,如果为0,用第一周模板生成下第二周的排班,否则用第二周的模板生成排班
- if mode.ExecuteTimes%2 == 0 {
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第一周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
- }
-
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
- }
-
- } else {
- if len(templates) < 2 {
- utils.WarningLog("自动排班-org_id: %v :没有第二周的模板")
- continue
- }
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[1].ID)
- if getItemsErr != nil {
- utils.ErrorLog("自动排班-org_id: %v :获取第二周模板Item失败:%v", mode.OrgID, getItemsErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :获取第二周模板Item失败:%v", mode.OrgID, getItemsErr)
- continue
- }
-
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, nextSecWeek, items, nil)
- if insertErr != "" {
- utils.ErrorLog("自动排班-org_id: %v :使用第二周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第二周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
- } else {
-
- }
- }
- }
- mode.ExecuteTimes += 1
- mode.ModifyTime = now.Unix()
- updateModeErr := tx.Save(mode).Error
- if updateModeErr != nil {
- utils.ErrorLog("自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id:%v :更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- tx.Rollback()
- continue
- }
- tx.Commit()
- utils.SuccessLog("自动排班-插入排班成功:org_id: %v", mode.OrgID)
- time.Sleep(time.Second * 30)
- }
-
- utils.SuccessLog("自动排班任务完成")
- }
-
- func _refreshWeekSchedulesWithTemplateItems(tx *gorm.DB, orgID int64, week time.Time, templateItems []*models.PatientScheduleTemplateItem, deviceNumberMap map[int64]*DeviceNumberViewModel) (string, int) {
- if deviceNumberMap == nil {
- deviceNumbers, getDeviceNumbersErr := GetAllValidDeviceNumbers(orgID)
- //fmt.Println("getDeviceNumbersErr3232322332323232323323223232323",getDeviceNumbersErr)
- if getDeviceNumbersErr != nil {
- return getDeviceNumbersErr.Error(), 0
- }
- deviceNumberMap = make(map[int64]*DeviceNumberViewModel, 0)
- for _, deviceNumber := range deviceNumbers {
- deviceNumberMap[deviceNumber.ID] = deviceNumber
- }
- }
-
- now := time.Now()
- monday, sunday := utils.GetMondayAndSundayOfWeekDate(&week)
- disableErr := tx.Model(&models.Schedule{}).Where("user_org_id = ? AND status = 1 AND schedule_date >= ? AND schedule_date <= ?", orgID, monday.Unix(), sunday.Unix()).Updates(map[string]interface{}{"status": 0, "updated_time": now.Unix()}).Error
- //fmt.Println("disableErr323232332323232233222332232332",disableErr)
- if disableErr != nil {
- return disableErr.Error(), 0
- }
-
- var week_count int
- // 批量插入排班
- if len(templateItems) > 0 {
-
- timeLayout := "2006-01-02"
- loc, _ := time.LoadLocation("Local")
- tmp, _ := time.ParseInLocation(timeLayout, time.Unix(monday.Unix(), 0).Format("2006-01-02"), loc)
- _, weeks := tmp.ISOWeek()
- week_count = weeks
-
- //valueStrs := make([]string, 0, len(templateItems))
- //values := make([]interface{}, 0, len(templateItems)*11)
- var errs string
- for _, item := range templateItems {
- deviceNumber := deviceNumberMap[item.DeviceNumberID]
- if deviceNumber == nil {
- continue
- }
-
- so, _ := GetDialysisSolutionTwo(item.OrgID, item.PatientID, item.TreatMode)
- filedRecordOne, _ := FindFiledBy(item.OrgID, "透析器")
- filedRecordTwo, _ := FindFiledBy(item.OrgID, "灌流器")
- filedRecordThree, _ := FindFiledBy(item.OrgID, "透析器/灌流器")
- var DialysisMachineName string
- if filedRecordOne.IsShow == 1 {
- DialysisMachineName = so.DialysisDialyszers
- }
- if filedRecordThree.IsShow == 1 {
- if len(DialysisMachineName) > 0 {
- DialysisMachineName = DialysisMachineName + "," + so.DialyzerPerfusionApparatus
-
- } else {
- DialysisMachineName = so.DialyzerPerfusionApparatus
-
- }
- }
- if filedRecordTwo.IsShow == 1 {
- if len(DialysisMachineName) > 0 {
- DialysisMachineName = DialysisMachineName + "," + so.DialysisIrrigation
-
- } else {
- DialysisMachineName = so.DialysisIrrigation
-
- }
- }
-
- var sch models.Schedule
- sch.UserOrgId = orgID
- sch.PartitionId = deviceNumber.ZoneID
- sch.BedId = item.DeviceNumberID
- sch.PatientId = item.PatientID
- sch.ScheduleDate = monday.AddDate(0, 0, int(item.Weekday-1)).Unix()
- sch.ScheduleWeek = int64(item.Weekday)
- sch.ScheduleType = int64(item.TimeType)
- sch.ModeId = int64(item.TreatMode)
- sch.Status = 1
- sch.CreatedTime = now.Unix()
- sch.UpdatedTime = now.Unix()
- sch.IsExport = 0
- sch.DialysisMachineName = DialysisMachineName
-
- //为了防止数据重复,先查询有无记录
- //var othSch []*models.Schedule
- //readDb.Model(&models.Schedule{}).Where("status = 1 AND schedule_date = ? AND user_org_id = ? AND patient_id = ?", sch.ScheduleDate, sch.UserOrgId, sch.PatientId).Find(&othSch)
- //for _, items := range othSch {
- // items.Status = 0
- // writeDb.Save(&items)
- //}
- err := writeDb.Create(&sch).Error
- if err != nil {
- errs = errs + err.Error()
- }
-
- //valueStrs = append(valueStrs, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
- //values = append(values, orgID)
- //values = append(values, deviceNumber.ZoneID)
- //values = append(values, item.DeviceNumberID)
- //values = append(values, item.PatientID)
- //values = append(values, monday.AddDate(0, 0, int(item.Weekday-1)).Unix())
- //values = append(values, item.Weekday)
- //values = append(values, item.TimeType)
- //values = append(values, item.TreatMode)
- //values = append(values, 1)
- //values = append(values, now.Unix())
- //values = append(values, now.Unix())
- //values = append(values, 0)
- //values = append(values, DialysisMachineName)
- }
- //sql := fmt.Sprintf("INSERT INTO xt_schedule (user_org_id, partition_id, bed_id, patient_id, schedule_date, schedule_week, schedule_type, mode_id, status, created_time, updated_time,is_export,dialysis_machine_name) VALUES %v;", strings.Join(valueStrs, ", "))
- //if insertErr := tx.Exec(sql, values...).Error; insertErr != nil {
- // fmt.Println("insertErr232332233223332232323222222www", insertErr)
- //}
- if len(errs) != 0 {
- return errs, 0
- }
- }
-
- return "", week_count
- }
-
- func FakeAutoCreateNextTwoWeekSchedules(orgID int64) {
- mode, getModeErr := GetOrgPatientScheduleTemplateMode(orgID)
- if getModeErr != nil {
- utils.ErrorLog("%v", getModeErr)
- return
- }
-
- templates, getTemplatesErr := GetOrgPatientScheduleTemplateIDs(mode.OrgID)
- if getTemplatesErr != nil {
- utils.ErrorLog("获取模板 ID 失败:%v", getTemplatesErr)
- return
- } else if len(templates) == 0 {
- utils.WarningLog("没有模板")
- return
- }
-
- deviceNumbers, getDeviceNumbersErr := GetAllValidDeviceNumbers(orgID)
- if getDeviceNumbersErr != nil {
- utils.ErrorLog("获取床位列表失败:%v", getDeviceNumbersErr)
- return
- }
- deviceNumberMap := make(map[int64]*DeviceNumberViewModel, 0)
- for _, deviceNumber := range deviceNumbers {
- deviceNumberMap[deviceNumber.ID] = deviceNumber
- }
-
- now := time.Now()
- tx := writeDb.Begin()
-
- if mode.Mode == 1 { // 用第一周模板生成下两周的排班
- items, getItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
- if getItemsErr != nil {
- utils.ErrorLog("获取第一周模板Item失败:%v", getItemsErr)
- return
- }
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, now.AddDate(0, 0, 7), items, deviceNumberMap)
- if insertErr != "" {
- utils.ErrorLog("使用第一周模板插入下第一周排班失败:%v", insertErr)
- tx.Rollback()
- return
- }
- insertErr, _ = _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, now.AddDate(0, 0, 14), items, deviceNumberMap)
- if insertErr != "" {
- utils.ErrorLog("使用第一周模板插入下第二周排班失败:%v", insertErr)
- tx.Rollback()
- return
- }
-
- } else if mode.Mode == 2 {
- firstItems, getFirstItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[0].ID)
- if getFirstItemsErr != nil {
- utils.ErrorLog("获取第一周模板Item失败:%v", getFirstItemsErr)
- return
- }
- secItems, getSecItemsErr := GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(mode.OrgID, templates[1].ID)
- if getSecItemsErr != nil {
- utils.ErrorLog("获取第一周模板Item失败:%v", getSecItemsErr)
- return
- }
-
- if mode.ExecuteTimes%2 == 0 {
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, now.AddDate(0, 0, 7), firstItems, deviceNumberMap)
- if insertErr != "" {
- utils.ErrorLog("使用第一周模板插入下第一周排班失败:%v", insertErr)
- tx.Rollback()
- return
- }
- insertErr, _ = _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, now.AddDate(0, 0, 14), secItems, deviceNumberMap)
- if insertErr != "" {
- utils.ErrorLog("使用第二周模板插入下第二周排班失败:%v", insertErr)
- tx.Rollback()
- return
- }
-
- } else { // mode.ExecuteTimes%2 == 1
- insertErr, _ := _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, now.AddDate(0, 0, 7), secItems, deviceNumberMap)
- if insertErr != "" {
- utils.ErrorLog("使用第二周模板插入下第一周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- return
- }
- insertErr, _ = _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, now.AddDate(0, 0, 14), firstItems, deviceNumberMap)
- if insertErr != "" {
- utils.ErrorLog("使用第一周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- return
- }
- }
- }
-
- mode.ExecuteTimes += 2
- mode.ModifyTime = now.Unix()
- updateModeErr := tx.Save(mode).Error
- if updateModeErr != nil {
- utils.ErrorLog("更新启用模板模式的执行次数失败:%v", mode.OrgID, updateModeErr)
- tx.Rollback()
- return
- }
-
- tx.Commit()
- utils.SuccessLog("模拟自动更新下两周排班成功")
- }
-
- func GetWeeks(oneTimeStr string) int {
- fmt.Println(oneTimeStr)
-
- //某个时间
- //oneTimeStr := "2018-01-08 00:00:00"
- //oneTimeStr := "2021-04-05 00:00:00"
- //时间转换成time.Time格式
- t, err := time.ParseInLocation("2006-01-02 15:04:05", oneTimeStr, time.Local)
- if err != nil {
- fmt.Println(err)
- }
- //获取这个时间的基于这一年有多少天了
- yearDay := t.YearDay()
- //获取上一年的最后一天
- yesterdayYearEndDay := t.AddDate(0, 0, -yearDay)
- //获取上一年最后一天是星期几
- dayInWeek := int(yesterdayYearEndDay.Weekday())
- //第一周的总天数,默认是7天
- firstWeekDays := 7
- //如果上一年最后一天不是星期天,则第一周总天数是7-dayInWeek
- if dayInWeek != 0 {
- firstWeekDays = 7 - dayInWeek
- }
- week := 0
- //如果这一年的总天数小于第一周总天数,则是第一周,否则按照这一年多少天减去第一周的天数除以7+1 但是要考虑这一天减去第一周天数除以7会取整型,
- //所以需要处理两个数取余之后是否大于0,如果大于0 则多加一天,这样自然周就算出来了。
- if yearDay <= firstWeekDays {
- week = 1
- } else {
- plusDay := 0
- if (yearDay-firstWeekDays)%7 > 0 {
- plusDay = 1
- }
- week = (yearDay-firstWeekDays)/7 + 1 + plusDay
- }
- fmt.Println(week)
-
- return week - 1
-
- }
|