patient_schedule_template_service.go 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. package service
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "XT_New/models"
  7. "XT_New/utils"
  8. "github.com/jinzhu/gorm"
  9. )
  10. func GetOrgPatientScheduleTemplateMode(orgID int64) (*models.PatientScheduleTemplateMode, error) {
  11. var mode models.PatientScheduleTemplateMode
  12. err := readDb.Model(&models.PatientScheduleTemplateMode{}).Where("org_id = ? AND status = 1", orgID).First(&mode).Error
  13. if err != nil {
  14. if err == gorm.ErrRecordNotFound {
  15. return nil, nil
  16. } else {
  17. return nil, err
  18. }
  19. }
  20. return &mode, nil
  21. }
  22. // 获取至多两个模板ID
  23. func GetOrgPatientScheduleTemplateIDs(orgID int64) ([]*models.PatientScheduleTemplateId, error) {
  24. var ids []*models.PatientScheduleTemplateId
  25. err := readDb.Model(&models.PatientScheduleTemplateId{}).Where("org_id = ? AND status = 1", orgID).Limit(2).Find(&ids).Error
  26. if err != nil {
  27. return nil, err
  28. }
  29. return ids, nil
  30. }
  31. func GetOrgPatientScheduleTemplateIDByTemplateID(orgID int64, templateID int64) (*models.PatientScheduleTemplateId, error) {
  32. var id models.PatientScheduleTemplateId
  33. err := readDb.Model(&models.PatientScheduleTemplateId{}).Where("org_id = ? AND id = ? AND status = 1", orgID, templateID).First(&id).Error
  34. if err != nil {
  35. if err == gorm.ErrRecordNotFound {
  36. return nil, nil
  37. } else {
  38. return nil, err
  39. }
  40. }
  41. return &id, nil
  42. }
  43. type PatientScheduleTemplateItemViewModel struct {
  44. models.PatientScheduleTemplateItem
  45. Patient *models.Patients `gorm:"ForeignKey:PatientID" json:"patient"`
  46. }
  47. type PatientScheduleTemplateViewModel struct {
  48. // ID int64 `gorm:"column:id" json:"id" form:"id"`
  49. // OrgID int64 `gorm:"column:org_id" json:"-" form:"org_id"`
  50. models.PatientScheduleTemplateId
  51. Items []*PatientScheduleTemplateItemViewModel `gorm:"ForeignKey:TemplateID" json:"items"`
  52. }
  53. func (PatientScheduleTemplateViewModel) TableName() string {
  54. return "xt_patient_schedule_template_id"
  55. }
  56. // 获取至多两个模板ID及其对应的模板排班列表
  57. func GetOrgPatientScheduleTemplateItems(orgID int64) ([]*PatientScheduleTemplateViewModel, error) {
  58. var vms []*PatientScheduleTemplateViewModel
  59. err := readDb.
  60. Model(&PatientScheduleTemplateViewModel{}).
  61. Preload("Items", "org_id = ? AND status = 1", orgID).
  62. Preload("Items.Patient", "user_org_id = ? AND status = 1", orgID).
  63. Where("org_id = ? AND status = 1", orgID).
  64. Limit(2).
  65. Find(&vms).
  66. Error
  67. if err != nil {
  68. return nil, err
  69. }
  70. return vms, nil
  71. }
  72. // 获取指定模板ID的模板排班列表
  73. func GetOrgPatientScheduleTemplateItemsByTemplateID(orgID int64, templateID int64) ([]*PatientScheduleTemplateItemViewModel, error) {
  74. var vms []*PatientScheduleTemplateItemViewModel
  75. err := readDb.
  76. Model(&PatientScheduleTemplateItemViewModel{}).
  77. Preload("Patient", "user_org_id = ? AND status = 1", orgID).
  78. Where("org_id = ? AND template_id = ? AND status = 1", orgID, templateID).
  79. Find(&vms).
  80. Error
  81. if err != nil {
  82. return nil, err
  83. }
  84. return vms, nil
  85. }
  86. func GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(orgID int64, templateID int64) ([]*models.PatientScheduleTemplateItem, error) {
  87. var items []*models.PatientScheduleTemplateItem
  88. err := readDb.Raw("select item.id,item.org_id,item.template_id,item.device_number_id,item.patient_id,item.treat_mode,item.weekday,item.time_type,item.status,item.status,item.ctime,item.mtime from xt_patient_schedule_template_item as item left join xt_patients as patient on item.patient_id = patient.id where item.org_id = ? AND item.template_id = ? AND item.status = 1 and patient.lapseto = 1 and patient.status = 1", orgID, templateID).Scan(&items).Error
  89. // err := readDb.
  90. // Model(&models.PatientScheduleTemplateItem{}).
  91. // Where("org_id = ? AND template_id = ? AND status = 1", orgID, templateID).
  92. // Find(&items).
  93. // Error
  94. if err != nil {
  95. return nil, err
  96. }
  97. return items, nil
  98. }
  99. type ScheduleDeviceNumberViewModel struct {
  100. models.DeviceNumber
  101. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  102. }
  103. func GetScheduleValidDeviceNumbers(orgID int64) ([]*ScheduleDeviceNumberViewModel, error) {
  104. var vms []*ScheduleDeviceNumberViewModel
  105. err := readDb.Model(&ScheduleDeviceNumberViewModel{}).Preload("Zone", "status = 1").Where("org_id = ? AND status = 1", orgID).Order("zone_id asc").Order("id asc").Find(&vms).Error
  106. if err != nil {
  107. return nil, err
  108. }
  109. return vms, nil
  110. }
  111. // 创建两个默认模板
  112. func CreateTwoPatientScheduleTemplates(orgID int64) (*PatientScheduleTemplateViewModel, *PatientScheduleTemplateViewModel, error) {
  113. now := time.Now().Unix()
  114. firstTemp := models.PatientScheduleTemplateId{}
  115. firstTemp.OrgID = orgID
  116. firstTemp.Status = 1
  117. firstTemp.CreateTime = now
  118. firstTemp.ModifyTime = now
  119. secondTemp := models.PatientScheduleTemplateId{}
  120. secondTemp.OrgID = orgID
  121. secondTemp.Status = 1
  122. secondTemp.CreateTime = now
  123. secondTemp.ModifyTime = now
  124. tx := writeDb.Begin()
  125. if err := tx.Model(&models.PatientScheduleTemplateId{}).Create(&firstTemp).Error; err != nil {
  126. tx.Rollback()
  127. return nil, nil, err
  128. }
  129. if err := tx.Model(&models.PatientScheduleTemplateId{}).Create(&secondTemp).Error; err != nil {
  130. tx.Rollback()
  131. return nil, nil, err
  132. }
  133. vm1 := PatientScheduleTemplateViewModel{}
  134. vm1.ID = firstTemp.ID
  135. vm1.Items = make([]*PatientScheduleTemplateItemViewModel, 0, 0)
  136. vm2 := PatientScheduleTemplateViewModel{}
  137. vm2.ID = secondTemp.ID
  138. vm2.Items = make([]*PatientScheduleTemplateItemViewModel, 0, 0)
  139. tx.Commit()
  140. return &vm1, &vm2, nil
  141. }
  142. func CreatePatientScheduleTemplate(orgID int64) (*PatientScheduleTemplateViewModel, error) {
  143. now := time.Now().Unix()
  144. temp := models.PatientScheduleTemplateId{}
  145. temp.OrgID = orgID
  146. temp.Status = 1
  147. temp.CreateTime = now
  148. temp.ModifyTime = now
  149. tx := writeDb.Begin()
  150. if err := tx.Model(&models.PatientScheduleTemplateId{}).Create(&temp).Error; err != nil {
  151. tx.Rollback()
  152. return nil, err
  153. }
  154. vm := PatientScheduleTemplateViewModel{}
  155. vm.ID = temp.ID
  156. vm.Items = make([]*PatientScheduleTemplateItemViewModel, 0, 0)
  157. tx.Commit()
  158. return &vm, nil
  159. }
  160. type PatientScheduleTemplatePatientVM struct {
  161. ID int64 `gorm:"column:id" json:"id" form:"id"`
  162. Name string `gorm:"column:name" json:"name" form:"name"`
  163. Contagions []*PatientScheduleContagion `gorm:"ForeignKey:PatientID" json:"contagions"`
  164. }
  165. func (PatientScheduleTemplatePatientVM) TableName() string {
  166. return "xt_patients"
  167. }
  168. type PatientScheduleContagion struct {
  169. ID int64 `gorm:"column:id" json:"id" form:"id"`
  170. PatientID int64 `gorm:"column:patient_id" json:"-"`
  171. DiseaseID int64 `gorm:"column:disease_id" json:"disease_id" form:"disease_id"`
  172. }
  173. func (PatientScheduleContagion) TableName() string {
  174. return "xt_patients_infectious_diseases"
  175. }
  176. func PatientScheduleTemplateGetAllValidPatient(orgID int64) ([]*PatientScheduleTemplatePatientVM, error) {
  177. var vms []*PatientScheduleTemplatePatientVM
  178. err := readDb.
  179. Model(&PatientScheduleTemplatePatientVM{}).
  180. Preload("Contagions", "status = 1").
  181. Where("user_org_id = ? and lapseto=1 AND status = 1", orgID).
  182. Find(&vms).
  183. Error
  184. if err != nil {
  185. return nil, err
  186. }
  187. return vms, nil
  188. }
  189. func UpdateScheduleTemplate(orgID int64, templateID int64,
  190. addTemplateItems []*models.PatientScheduleTemplateItem,
  191. delTemplateItems []*models.PatientScheduleTemplateItem,
  192. changeTemplateItems []*models.PatientScheduleTemplateItem,
  193. shouldUpdateNextWeekSchedules bool,
  194. shouldUpdateNextSecondWeekSchedules bool) error {
  195. now := time.Now()
  196. tx := writeDb.Begin()
  197. // 批量插入 add template item
  198. if len(addTemplateItems) > 0 {
  199. // utils.TraceLog("addTemplateItems len: %v", len(addTemplateItems))
  200. valueStrs := make([]string, 0, len(addTemplateItems))
  201. values := make([]interface{}, 0, len(addTemplateItems)*10)
  202. for _, item := range addTemplateItems {
  203. valueStrs = append(valueStrs, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
  204. values = append(values, orgID)
  205. values = append(values, templateID)
  206. values = append(values, item.DeviceNumberID)
  207. values = append(values, item.PatientID)
  208. values = append(values, item.TreatMode)
  209. values = append(values, item.Weekday)
  210. values = append(values, item.TimeType)
  211. values = append(values, 1)
  212. values = append(values, now.Unix())
  213. values = append(values, now.Unix())
  214. }
  215. sql := fmt.Sprintf("INSERT INTO xt_patient_schedule_template_item (org_id, template_id, device_number_id, patient_id, treat_mode, weekday, time_type, status, ctime, mtime) VALUES %v;", strings.Join(valueStrs, ", "))
  216. if insertErr := tx.Exec(sql, values...).Error; insertErr != nil {
  217. tx.Rollback()
  218. return insertErr
  219. }
  220. }
  221. // 批量假删除 del template item
  222. if len(delTemplateItems) > 0 {
  223. // utils.TraceLog("delTemplateItems len: %v", len(delTemplateItems))
  224. ins := make([]string, 0, len(delTemplateItems))
  225. for _, item := range delTemplateItems {
  226. ins = append(ins, fmt.Sprintf("(%v, %v, %v, %v, %v)", orgID, templateID, item.DeviceNumberID, item.Weekday, item.TimeType))
  227. }
  228. sql := fmt.Sprintf("UPDATE xt_patient_schedule_template_item SET status = 0, mtime = %v WHERE status = 1 AND (org_id, template_id, device_number_id, weekday, time_type) IN (%v);", now.Unix(), strings.Join(ins, ","))
  229. if delErr := tx.Exec(sql).Error; delErr != nil {
  230. tx.Rollback()
  231. return delErr
  232. }
  233. }
  234. // 批量更新 change template item
  235. if len(changeTemplateItems) > 0 {
  236. // utils.TraceLog("changeTemplateItems len: %v", len(changeTemplateItems))
  237. for _, item := range changeTemplateItems {
  238. if updateErr := tx.Exec("UPDATE xt_patient_schedule_template_item SET patient_id = ?, treat_mode = ?, mtime = ? WHERE status = 1 AND org_id = ? AND template_id = ? AND device_number_id = ? AND weekday = ? AND time_type = ?;", item.PatientID, item.TreatMode, now.Unix(), orgID, templateID, item.DeviceNumberID, item.Weekday, item.TimeType).Error; updateErr != nil {
  239. tx.Rollback()
  240. return updateErr
  241. }
  242. }
  243. }
  244. // 更改下两周的排班
  245. deviceNumbers, getDeviceNumbersErr := GetAllValidDeviceNumbers(orgID)
  246. if getDeviceNumbersErr != nil {
  247. tx.Rollback()
  248. return getDeviceNumbersErr
  249. }
  250. devicesMap := make(map[int64]*DeviceNumberViewModel, 0)
  251. for _, deviceNumber := range deviceNumbers {
  252. devicesMap[deviceNumber.ID] = deviceNumber
  253. }
  254. if shouldUpdateNextWeekSchedules {
  255. nextWeek := now.AddDate(0, 0, 7)
  256. if updateErr := _updateWeekSchedules(tx, orgID, nextWeek, addTemplateItems, delTemplateItems, changeTemplateItems, devicesMap); updateErr != nil {
  257. tx.Rollback()
  258. return updateErr
  259. }
  260. }
  261. if shouldUpdateNextSecondWeekSchedules {
  262. nextWeek := now.AddDate(0, 0, 14)
  263. if updateErr := _updateWeekSchedules(tx, orgID, nextWeek, addTemplateItems, delTemplateItems, changeTemplateItems, devicesMap); updateErr != nil {
  264. tx.Rollback()
  265. return updateErr
  266. }
  267. }
  268. tx.Commit()
  269. return nil
  270. }
  271. func _updateWeekSchedules(tx *gorm.DB, orgID int64, week time.Time, addTemplateItems []*models.PatientScheduleTemplateItem, delTemplateItems []*models.PatientScheduleTemplateItem, changeTemplateItems []*models.PatientScheduleTemplateItem, deviceNumberMap map[int64]*DeviceNumberViewModel) error {
  272. if deviceNumberMap == nil {
  273. deviceNumbers, getDeviceNumbersErr := GetAllValidDeviceNumbers(orgID)
  274. if getDeviceNumbersErr != nil {
  275. return getDeviceNumbersErr
  276. }
  277. deviceNumberMap = make(map[int64]*DeviceNumberViewModel, 0)
  278. for _, deviceNumber := range deviceNumbers {
  279. deviceNumberMap[deviceNumber.ID] = deviceNumber
  280. }
  281. }
  282. now := time.Now()
  283. monday, _ := utils.GetMondayAndSundayOfWeekDate(&week)
  284. // 先取消要取消的排班和新排班可能存在的重复排班
  285. delIns := make([]string, 0, len(addTemplateItems)+len(delTemplateItems))
  286. addValueStrs := make([]string, 0, len(addTemplateItems))
  287. addValues := make([]interface{}, 0, len(addTemplateItems)*11)
  288. for _, item := range addTemplateItems {
  289. scheduleDate := monday.AddDate(0, 0, int(item.Weekday-1))
  290. delIns = append(delIns, fmt.Sprintf("(%v, %v, %v)", item.DeviceNumberID, scheduleDate.Unix(), item.TimeType))
  291. // 构建新排班的条件
  292. deviceNumber := deviceNumberMap[item.DeviceNumberID]
  293. if deviceNumber != nil {
  294. addValueStrs = append(addValueStrs, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
  295. addValues = append(addValues, orgID)
  296. addValues = append(addValues, deviceNumber.ZoneID)
  297. addValues = append(addValues, item.DeviceNumberID)
  298. addValues = append(addValues, item.PatientID)
  299. addValues = append(addValues, scheduleDate.Unix())
  300. addValues = append(addValues, item.Weekday)
  301. addValues = append(addValues, item.TimeType)
  302. addValues = append(addValues, item.TreatMode)
  303. addValues = append(addValues, 1)
  304. addValues = append(addValues, now.Unix())
  305. addValues = append(addValues, now.Unix())
  306. }
  307. }
  308. for _, item := range delTemplateItems {
  309. delIns = append(delIns, fmt.Sprintf("(%v, %v, %v)", item.DeviceNumberID, monday.AddDate(0, 0, int(item.Weekday-1)).Unix(), item.TimeType))
  310. }
  311. if len(delIns) > 0 {
  312. sql := fmt.Sprintf("UPDATE xt_schedule SET status = 0, updated_time = %v WHERE user_org_id = %v AND status = 1 AND (bed_id, schedule_date, schedule_type) IN (%v);", now.Unix(), orgID, strings.Join(delIns, ","))
  313. if delErr := tx.Exec(sql).Error; delErr != nil {
  314. tx.Rollback()
  315. return delErr
  316. }
  317. }
  318. // 批量插入排班
  319. if len(addValueStrs) > 0 {
  320. 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(addValueStrs, ", "))
  321. if insertErr := tx.Exec(sql, addValues...).Error; insertErr != nil {
  322. return insertErr
  323. }
  324. }
  325. // 更新已有排班
  326. if len(changeTemplateItems) > 0 {
  327. for _, item := range changeTemplateItems {
  328. if updateErr := tx.Exec("UPDATE xt_schedule SET patient_id = ?, mode_id = ?, updated_time = ? WHERE status = 1 AND user_org_id = ? AND bed_id = ? AND schedule_date = ? AND schedule_type = ?;", item.PatientID, item.TreatMode, now.Unix(), orgID, item.DeviceNumberID, monday.AddDate(0, 0, int(item.Weekday-1)).Unix(), item.TimeType).Error; updateErr != nil {
  329. tx.Rollback()
  330. return updateErr
  331. }
  332. }
  333. }
  334. return nil
  335. }
  336. func SavePatientScheduleTemplateMode(templateMode *models.PatientScheduleTemplateMode) error {
  337. tx := writeDb.Begin()
  338. saveErr := tx.Save(templateMode).Error
  339. if saveErr != nil {
  340. tx.Rollback()
  341. return saveErr
  342. }
  343. tx.Commit()
  344. return nil
  345. }
  346. // 1 -> 0 或 2 -> 0 则不处理排班,但清除所有模板item
  347. func SavePatientScheduleTemplateModeAndClearAllTemplateItems(templateMode *models.PatientScheduleTemplateMode) error {
  348. tx := writeDb.Begin()
  349. saveErr := tx.Save(templateMode).Error
  350. if saveErr != nil {
  351. tx.Rollback()
  352. return saveErr
  353. }
  354. disableErr := tx.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND status = 1", templateMode.OrgID).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  355. if disableErr != nil {
  356. tx.Rollback()
  357. return disableErr
  358. }
  359. tx.Commit()
  360. return nil
  361. }
  362. // 0 -> 1 或 0 -> 2 则直接清除下两周的排班
  363. func SavePatientScheduleTemplateModeAndClearNextTwoWeekSchedules(templateMode *models.PatientScheduleTemplateMode) error {
  364. tx := writeDb.Begin()
  365. saveErr := tx.Save(templateMode).Error
  366. if saveErr != nil {
  367. tx.Rollback()
  368. return saveErr
  369. }
  370. now := time.Now()
  371. nextWeek := now.AddDate(0, 0, 7)
  372. nextMonday, _ := utils.GetMondayAndSundayOfWeekDate(&nextWeek)
  373. nextSecWeek := now.AddDate(0, 0, 14)
  374. _, nextSecSunday := utils.GetMondayAndSundayOfWeekDate(&nextSecWeek)
  375. disableErr := tx.Model(&models.Schedule{}).Where("user_org_id = ? AND status = 1 AND schedule_date >= ? AND schedule_date <= ?", templateMode.OrgID, nextMonday.Unix(), nextSecSunday.Unix()).Updates(map[string]interface{}{"status": 0, "updated_time": now.Unix()}).Error
  376. if disableErr != nil {
  377. tx.Rollback()
  378. return disableErr
  379. }
  380. tx.Commit()
  381. return nil
  382. }
  383. // 1 -> 2 清除第二周排班
  384. func SavePatientScheduleTemplateModeAndClearNextSecondWeekSchedules(templateMode *models.PatientScheduleTemplateMode) error {
  385. tx := writeDb.Begin()
  386. saveErr := tx.Save(templateMode).Error
  387. if saveErr != nil {
  388. tx.Rollback()
  389. return saveErr
  390. }
  391. now := time.Now()
  392. nextSecWeek := now.AddDate(0, 0, 14)
  393. nextSecMonday, nextSecSunday := utils.GetMondayAndSundayOfWeekDate(&nextSecWeek)
  394. disableErr := tx.Model(&models.Schedule{}).Where("user_org_id = ? AND status = 1 AND schedule_date >= ? AND schedule_date <= ?", templateMode.OrgID, nextSecMonday.Unix(), nextSecSunday.Unix()).Updates(map[string]interface{}{"status": 0, "mtime": now.Unix()}).Error
  395. if disableErr != nil {
  396. tx.Rollback()
  397. return disableErr
  398. }
  399. tx.Commit()
  400. return nil
  401. }
  402. // 2 -> 1 清除第二周模板item,清除第二周排班,将第一周的排班重复到第二周
  403. func SavePatientScheduleTemplateMode2To1(templateMode *models.PatientScheduleTemplateMode, secondWeekTemplateID int64, firstWeekTemplateItems []*models.PatientScheduleTemplateItem) error {
  404. tx := writeDb.Begin()
  405. saveErr := tx.Save(templateMode).Error
  406. if saveErr != nil {
  407. tx.Rollback()
  408. return saveErr
  409. }
  410. now := time.Now()
  411. disableItemsErr := tx.Model(&models.PatientScheduleTemplateItem{}).Where("template_id = ? AND status = 1", secondWeekTemplateID).Updates(map[string]interface{}{"status": 0, "mtime": now.Unix()}).Error
  412. if disableItemsErr != nil {
  413. tx.Rollback()
  414. return disableItemsErr
  415. }
  416. nextSecWeek := now.AddDate(0, 0, 14)
  417. nextSecMonday, nextSecSunday := utils.GetMondayAndSundayOfWeekDate(&nextSecWeek)
  418. disableSchsErr := tx.Model(&models.Schedule{}).Where("user_org_id = ? AND status = 1 AND schedule_date >= ? AND schedule_date <= ?", templateMode.OrgID, nextSecMonday.Unix(), nextSecSunday.Unix()).Updates(map[string]interface{}{"status": 0, "mtime": now.Unix()}).Error
  419. if disableSchsErr != nil {
  420. tx.Rollback()
  421. return disableSchsErr
  422. }
  423. if len(firstWeekTemplateItems) > 0 {
  424. deviceNumbers, getDeviceNumbersErr := GetAllValidDeviceNumbers(templateMode.OrgID)
  425. if getDeviceNumbersErr != nil {
  426. tx.Rollback()
  427. return getDeviceNumbersErr
  428. }
  429. deviceNumberMap := make(map[int64]*DeviceNumberViewModel, 0)
  430. for _, deviceNumber := range deviceNumbers {
  431. deviceNumberMap[deviceNumber.ID] = deviceNumber
  432. }
  433. valueStrs := make([]string, 0, len(firstWeekTemplateItems))
  434. values := make([]interface{}, 0, len(firstWeekTemplateItems)*11)
  435. for _, item := range firstWeekTemplateItems {
  436. deviceNumber := deviceNumberMap[item.DeviceNumberID]
  437. if deviceNumber == nil {
  438. continue
  439. }
  440. valueStrs = append(valueStrs, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
  441. values = append(values, templateMode.OrgID)
  442. values = append(values, deviceNumber.ZoneID)
  443. values = append(values, item.DeviceNumberID)
  444. values = append(values, item.PatientID)
  445. values = append(values, nextSecMonday.AddDate(0, 0, int(item.Weekday-1)).Unix())
  446. values = append(values, item.Weekday)
  447. values = append(values, item.TimeType)
  448. values = append(values, item.TreatMode)
  449. values = append(values, 1)
  450. values = append(values, now.Unix())
  451. values = append(values, now.Unix())
  452. }
  453. 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, ", "))
  454. if insertErr := tx.Exec(sql, values...).Error; insertErr != nil {
  455. return insertErr
  456. }
  457. }
  458. tx.Commit()
  459. return nil
  460. }