123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- package service
-
- import (
- "fmt"
- "strings"
- "time"
-
- "Xcx_New/models"
- "Xcx_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点生成排班
- createWeekSchedulesCronJob.AddFunc(spec, func() {
- AutoCreateWeekSchedules()
- })
- }
-
- func BeginAutoCreateWeekSchedulesJob() {
- createWeekSchedulesCronJob.Start()
- }
-
- 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 != nil {
- 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 != nil {
- 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 != nil {
- utils.ErrorLog("自动排班-org_id: %v :使用第二周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- CreateDBErrorRecord(mode.OrgID, "自动排班-org_id: %v :使用第二周模板插入下第二周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- continue
- }
- }
- }
-
- 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) error {
- if deviceNumberMap == nil {
- deviceNumbers, getDeviceNumbersErr := GetAllValidDeviceNumbers(orgID)
- if getDeviceNumbersErr != nil {
- return getDeviceNumbersErr
- }
- 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, "mtime": now.Unix()}).Error
- if disableErr != nil {
- return disableErr
- }
-
- // 批量插入排班
- if len(templateItems) > 0 {
- valueStrs := make([]string, 0, len(templateItems))
- values := make([]interface{}, 0, len(templateItems)*11)
- for _, item := range templateItems {
- deviceNumber := deviceNumberMap[item.DeviceNumberID]
- if deviceNumber == nil {
- continue
- }
-
- 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())
- }
-
- 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) VALUES %v;", strings.Join(valueStrs, ", "))
- if insertErr := tx.Exec(sql, values...).Error; insertErr != nil {
- return insertErr
- }
- }
- return nil
- }
-
- 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 != nil {
- utils.ErrorLog("使用第一周模板插入下第一周排班失败:%v", insertErr)
- tx.Rollback()
- return
- }
- insertErr = _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, now.AddDate(0, 0, 14), items, deviceNumberMap)
- if insertErr != nil {
- 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 != nil {
- utils.ErrorLog("使用第一周模板插入下第一周排班失败:%v", insertErr)
- tx.Rollback()
- return
- }
- insertErr = _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, now.AddDate(0, 0, 14), secItems, deviceNumberMap)
- if insertErr != nil {
- 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 != nil {
- utils.ErrorLog("使用第二周模板插入下第一周排班失败:%v", mode.OrgID, insertErr)
- tx.Rollback()
- return
- }
- insertErr = _refreshWeekSchedulesWithTemplateItems(tx, mode.OrgID, now.AddDate(0, 0, 14), firstItems, deviceNumberMap)
- if insertErr != nil {
- 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("模拟自动更新下两周排班成功")
- }
|