patient_schedule_template_service.go 38KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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(4).Find(&ids).Error
  26. if err != nil {
  27. return nil, err
  28. }
  29. return ids, nil
  30. }
  31. func GetOrgPatientScheduleTemplateIDByTemplateID(orgID int64, templateID int64) (id models.PatientScheduleTemplateId, err error) {
  32. err = readDb.Model(&models.PatientScheduleTemplateId{}).Where("org_id = ? AND id = ? AND status = 1", orgID, templateID).First(&id).Error
  33. return id, nil
  34. }
  35. type PatientsTwo struct {
  36. ID int64 `gorm:"column:id" json:"id" form:"id"`
  37. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  38. UserId int64 `gorm:"column:user_id" json:"user_id" form:"user_id"`
  39. Name string `gorm:"column:name" json:"name" form:"name"`
  40. }
  41. func (PatientsTwo) TableName() string {
  42. return "xt_patients"
  43. }
  44. type PatientScheduleTemplateItemViewModel struct {
  45. models.PatientScheduleTemplateItem
  46. Patient *PatientsTwo `gorm:"ForeignKey:PatientID" json:"patient"`
  47. DeviceNumberViewModel DeviceNumberViewModel `gorm:"ForeignKey:DeviceNumberID" json:"devices"`
  48. }
  49. type PatientScheduleTemplateViewModel struct {
  50. // ID int64 `gorm:"column:id" json:"id" form:"id"`
  51. // OrgID int64 `gorm:"column:org_id" json:"-" form:"org_id"`
  52. models.PatientScheduleTemplateId
  53. Items []*PatientScheduleTemplateItemViewModel `gorm:"ForeignKey:TemplateID" json:"items"`
  54. }
  55. func (PatientScheduleTemplateViewModel) TableName() string {
  56. return "xt_patient_schedule_template_id"
  57. }
  58. // 获取至多两个模板ID及其对应的模板排班列表
  59. func GetOrgPatientScheduleTemplateItems(orgID int64) ([]*PatientScheduleTemplateViewModel, error) {
  60. var vms []*PatientScheduleTemplateViewModel
  61. err := readDb.
  62. Model(&PatientScheduleTemplateViewModel{}).
  63. Preload("Items", "org_id = ? AND status = 1", orgID).
  64. Preload("Items.Patient", "user_org_id = ? AND status = 1", orgID).
  65. Preload("Items.DeviceNumberViewModel.Zone", "status = 1").
  66. Where("org_id = ? AND status = 1", orgID).
  67. Limit(4).
  68. Find(&vms).
  69. Error
  70. if err != nil {
  71. return nil, err
  72. }
  73. return vms, nil
  74. }
  75. // 获取指定模板ID的模板排班列表
  76. func GetOrgPatientScheduleTemplateItemsByTemplateID(orgID int64) ([]*PatientScheduleTemplateItemViewModel, error) {
  77. var vms []*PatientScheduleTemplateItemViewModel
  78. err := readDb.
  79. Model(&PatientScheduleTemplateItemViewModel{}).
  80. Preload("Patient", "user_org_id = ? AND status = 1", orgID).
  81. Where("org_id = ? AND template_id = ? AND status = 1", orgID, 0).
  82. Find(&vms).
  83. Error
  84. if err != nil {
  85. return nil, err
  86. }
  87. return vms, nil
  88. }
  89. func GetOrgPatientScheduleTemplateItemsWithoutPatientByTemplateID(orgID int64, templateID int64) ([]*models.PatientScheduleTemplateItem, error) {
  90. var items []*models.PatientScheduleTemplateItem
  91. 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
  92. // err := readDb.
  93. // Model(&models.PatientScheduleTemplateItem{}).
  94. // Where("org_id = ? AND template_id = ? AND status = 1", orgID, templateID).
  95. // Find(&items).
  96. // Error
  97. if err != nil {
  98. return nil, err
  99. }
  100. return items, nil
  101. }
  102. type ScheduleDeviceNumberViewModel struct {
  103. models.DeviceNumber
  104. Zone *models.DeviceZone `gorm:"ForeignKey:ZoneID" json:"zone"`
  105. }
  106. func GetScheduleValidDeviceNumbers(orgID int64, ids []string) ([]*ScheduleDeviceNumberViewModel, error) {
  107. if len(ids) == 0 {
  108. var vms []*ScheduleDeviceNumberViewModel
  109. 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
  110. if err != nil {
  111. return nil, err
  112. }
  113. return vms, nil
  114. } else {
  115. var vms []*ScheduleDeviceNumberViewModel
  116. err := readDb.Model(&ScheduleDeviceNumberViewModel{}).Preload("Zone", "status = 1 AND id in (?)", ids).Where("org_id = ? AND status = 1", orgID).Order("zone_id asc").Order("id asc").Find(&vms).Error
  117. if err != nil {
  118. return nil, err
  119. }
  120. return vms, nil
  121. }
  122. }
  123. // 创建两个默认模板
  124. func CreateTwoPatientScheduleTemplates(orgID int64) (*PatientScheduleTemplateViewModel, *PatientScheduleTemplateViewModel, *PatientScheduleTemplateViewModel, *PatientScheduleTemplateViewModel, error) {
  125. firstTemp := models.PatientScheduleTemplateId{}
  126. firstTemp.OrgID = orgID
  127. firstTemp.Status = 1
  128. secondTemp := models.PatientScheduleTemplateId{}
  129. secondTemp.OrgID = orgID
  130. secondTemp.Status = 1
  131. thirdTemp := models.PatientScheduleTemplateId{}
  132. thirdTemp.OrgID = orgID
  133. thirdTemp.Status = 1
  134. fourTemp := models.PatientScheduleTemplateId{}
  135. fourTemp.OrgID = orgID
  136. fourTemp.Status = 1
  137. tx := writeDb.Begin()
  138. if err := tx.Model(&models.PatientScheduleTemplateId{}).Create(&firstTemp).Error; err != nil {
  139. tx.Rollback()
  140. return nil, nil, nil, nil, err
  141. }
  142. if err := tx.Model(&models.PatientScheduleTemplateId{}).Create(&secondTemp).Error; err != nil {
  143. tx.Rollback()
  144. return nil, nil, nil, nil, err
  145. }
  146. if err := tx.Model(&models.PatientScheduleTemplateId{}).Create(&thirdTemp).Error; err != nil {
  147. tx.Rollback()
  148. return nil, nil, nil, nil, err
  149. }
  150. if err := tx.Model(&models.PatientScheduleTemplateId{}).Create(&fourTemp).Error; err != nil {
  151. tx.Rollback()
  152. return nil, nil, nil, nil, err
  153. }
  154. vm1 := PatientScheduleTemplateViewModel{}
  155. vm1.ID = firstTemp.ID
  156. vm1.Items = make([]*PatientScheduleTemplateItemViewModel, 0, 0)
  157. vm2 := PatientScheduleTemplateViewModel{}
  158. vm2.ID = secondTemp.ID
  159. vm2.Items = make([]*PatientScheduleTemplateItemViewModel, 0, 0)
  160. vm3 := PatientScheduleTemplateViewModel{}
  161. vm3.ID = thirdTemp.ID
  162. vm3.Items = make([]*PatientScheduleTemplateItemViewModel, 0, 0)
  163. vm4 := PatientScheduleTemplateViewModel{}
  164. vm4.ID = fourTemp.ID
  165. vm4.Items = make([]*PatientScheduleTemplateItemViewModel, 0, 0)
  166. tx.Commit()
  167. return &vm1, &vm2, &vm3, &vm4, nil
  168. }
  169. func CreatePatientScheduleTemplate(orgID int64) (*PatientScheduleTemplateViewModel, *PatientScheduleTemplateViewModel, *PatientScheduleTemplateViewModel, error) {
  170. temp := models.PatientScheduleTemplateId{}
  171. temp.OrgID = orgID
  172. temp.Status = 1
  173. tx := writeDb.Begin()
  174. if err := tx.Model(&models.PatientScheduleTemplateId{}).Create(&temp).Error; err != nil {
  175. tx.Rollback()
  176. return nil, nil, nil, err
  177. }
  178. thirdTemp := models.PatientScheduleTemplateId{}
  179. thirdTemp.OrgID = orgID
  180. thirdTemp.Status = 1
  181. if err := tx.Model(&models.PatientScheduleTemplateId{}).Create(&thirdTemp).Error; err != nil {
  182. tx.Rollback()
  183. return nil, nil, nil, err
  184. }
  185. fourTemp := models.PatientScheduleTemplateId{}
  186. fourTemp.OrgID = orgID
  187. fourTemp.Status = 1
  188. if err := tx.Model(&models.PatientScheduleTemplateId{}).Create(&fourTemp).Error; err != nil {
  189. tx.Rollback()
  190. return nil, nil, nil, err
  191. }
  192. vm := PatientScheduleTemplateViewModel{}
  193. vm.ID = temp.ID
  194. vm.Items = make([]*PatientScheduleTemplateItemViewModel, 0, 0)
  195. vm3 := PatientScheduleTemplateViewModel{}
  196. vm3.ID = thirdTemp.ID
  197. vm3.Items = make([]*PatientScheduleTemplateItemViewModel, 0, 0)
  198. vm4 := PatientScheduleTemplateViewModel{}
  199. vm4.ID = fourTemp.ID
  200. vm4.Items = make([]*PatientScheduleTemplateItemViewModel, 0, 0)
  201. tx.Commit()
  202. return &vm, &vm3, &vm4, nil
  203. }
  204. func CreatePatientScheduleTemplateTwo(orgID int64) (*PatientScheduleTemplateViewModel, *PatientScheduleTemplateViewModel, error) {
  205. tx := writeDb.Begin()
  206. thirdTemp := models.PatientScheduleTemplateId{}
  207. thirdTemp.OrgID = orgID
  208. thirdTemp.Status = 1
  209. if err := tx.Model(&models.PatientScheduleTemplateId{}).Create(&thirdTemp).Error; err != nil {
  210. tx.Rollback()
  211. return nil, nil, err
  212. }
  213. fourTemp := models.PatientScheduleTemplateId{}
  214. fourTemp.OrgID = orgID
  215. fourTemp.Status = 1
  216. if err := tx.Model(&models.PatientScheduleTemplateId{}).Create(&fourTemp).Error; err != nil {
  217. tx.Rollback()
  218. return nil, nil, err
  219. }
  220. vm3 := PatientScheduleTemplateViewModel{}
  221. vm3.ID = thirdTemp.ID
  222. vm3.Items = make([]*PatientScheduleTemplateItemViewModel, 0, 0)
  223. vm4 := PatientScheduleTemplateViewModel{}
  224. vm4.ID = fourTemp.ID
  225. vm4.Items = make([]*PatientScheduleTemplateItemViewModel, 0, 0)
  226. tx.Commit()
  227. return &vm3, &vm4, nil
  228. }
  229. func CreatePatientScheduleTemplateThree(orgID int64) (*PatientScheduleTemplateViewModel, error) {
  230. tx := writeDb.Begin()
  231. fourTemp := models.PatientScheduleTemplateId{}
  232. fourTemp.OrgID = orgID
  233. fourTemp.Status = 1
  234. if err := tx.Model(&models.PatientScheduleTemplateId{}).Create(&fourTemp).Error; err != nil {
  235. tx.Rollback()
  236. return nil, err
  237. }
  238. vm4 := PatientScheduleTemplateViewModel{}
  239. vm4.ID = fourTemp.ID
  240. vm4.Items = make([]*PatientScheduleTemplateItemViewModel, 0, 0)
  241. tx.Commit()
  242. return &vm4, nil
  243. }
  244. type PatientScheduleTemplatePatientVM struct {
  245. ID int64 `gorm:"column:id" json:"id" form:"id"`
  246. Name string `gorm:"column:name" json:"name" form:"name"`
  247. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  248. FirstLetter string `gorm:"column:first_letter" json:"first_letter" form:"first_letter"`
  249. Contagions []*PatientScheduleContagion `gorm:"ForeignKey:PatientID" json:"contagions"`
  250. }
  251. func (PatientScheduleTemplatePatientVM) TableName() string {
  252. return "xt_patients"
  253. }
  254. type PatientScheduleContagion struct {
  255. ID int64 `gorm:"column:id" json:"id" form:"id"`
  256. PatientID int64 `gorm:"column:patient_id" json:"-"`
  257. DiseaseID int64 `gorm:"column:disease_id" json:"disease_id" form:"disease_id"`
  258. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  259. }
  260. func (PatientScheduleContagion) TableName() string {
  261. return "xt_patients_infectious_diseases"
  262. }
  263. func PatientScheduleTemplateGetAllValidPatient(orgID int64) ([]*PatientScheduleTemplatePatientVM, error) {
  264. var vms []*PatientScheduleTemplatePatientVM
  265. err := readDb.
  266. Model(&PatientScheduleTemplatePatientVM{}).
  267. Preload("Contagions", "status = 1").
  268. Where("user_org_id = ? and lapseto=1 AND status = 1", orgID).
  269. Find(&vms).
  270. Error
  271. if err != nil {
  272. return nil, err
  273. }
  274. return vms, nil
  275. }
  276. func UpdateScheduleTemplate(orgID int64, template_id int64,
  277. addTemplateItems []*models.PatientScheduleTemplateItem,
  278. delTemplateItems []*models.PatientScheduleTemplateItem,
  279. changeTemplateItems []*models.PatientScheduleTemplateItem,
  280. shouldUpdateNextWeekSchedules bool,
  281. shouldUpdateNextSecondWeekSchedules bool) error {
  282. now := time.Now()
  283. tx := writeDb.Begin()
  284. readDb.Model(&models.XtPatientScheduleTemplateItem{}).Where("template_id = ? AND weekday = ? AND patient_id = ?", template_id)
  285. // 批量插入 add template item
  286. if len(addTemplateItems) > 0 {
  287. // utils.TraceLog("addTemplateItems len: %v", len(addTemplateItems))
  288. valueStrs := make([]string, 0, len(addTemplateItems))
  289. values := make([]interface{}, 0, len(addTemplateItems)*10)
  290. for _, item := range addTemplateItems {
  291. var pst []models.XtPatientScheduleTemplateItem
  292. readDb.Model(&models.XtPatientScheduleTemplateItem{}).Where("template_id = ? AND weekday = ? AND patient_id = ? AND status = 1", template_id, item.Weekday, item.PatientID).Find(&pst)
  293. for _, item := range pst {
  294. item.Status = 0
  295. writeDb.Save(item)
  296. }
  297. valueStrs = append(valueStrs, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
  298. values = append(values, orgID)
  299. values = append(values, template_id)
  300. values = append(values, item.DeviceNumberID)
  301. values = append(values, item.PatientID)
  302. values = append(values, item.TreatMode)
  303. values = append(values, item.Weekday)
  304. values = append(values, item.TimeType)
  305. values = append(values, 1)
  306. values = append(values, now.Unix())
  307. values = append(values, now.Unix())
  308. }
  309. 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, ", "))
  310. if insertErr := tx.Exec(sql, values...).Error; insertErr != nil {
  311. tx.Rollback()
  312. return insertErr
  313. }
  314. }
  315. // 批量假删除 del template item
  316. if len(delTemplateItems) > 0 {
  317. // utils.TraceLog("delTemplateItems len: %v", len(delTemplateItems))
  318. ins := make([]string, 0, len(delTemplateItems))
  319. for _, item := range delTemplateItems {
  320. ins = append(ins, fmt.Sprintf("(%v, %v, %v, %v, %v)", orgID, template_id, item.DeviceNumberID, item.Weekday, item.TimeType))
  321. }
  322. 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, ","))
  323. if delErr := tx.Exec(sql).Error; delErr != nil {
  324. tx.Rollback()
  325. return delErr
  326. }
  327. }
  328. // 批量更新 change template item
  329. if len(changeTemplateItems) > 0 {
  330. // utils.TraceLog("changeTemplateItems len: %v", len(changeTemplateItems))
  331. for _, item := range changeTemplateItems {
  332. 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, template_id, item.DeviceNumberID, item.Weekday, item.TimeType).Error; updateErr != nil {
  333. tx.Rollback()
  334. return updateErr
  335. }
  336. }
  337. }
  338. // 更改下四周的排班
  339. deviceNumbers, getDeviceNumbersErr := GetAllValidDeviceNumbers(orgID)
  340. if getDeviceNumbersErr != nil {
  341. tx.Rollback()
  342. return getDeviceNumbersErr
  343. }
  344. devicesMap := make(map[int64]*DeviceNumberViewModel, 0)
  345. for _, deviceNumber := range deviceNumbers {
  346. devicesMap[deviceNumber.ID] = deviceNumber
  347. }
  348. //if shouldUpdateNextWeekSchedules {
  349. // nextWeek := now.AddDate(0, 0, 7)
  350. // if updateErr := _updateWeekSchedules(tx, orgID, nextWeek, addTemplateItems, delTemplateItems, changeTemplateItems, devicesMap); updateErr != nil {
  351. // tx.Rollback()
  352. // return updateErr
  353. // }
  354. //}
  355. //if shouldUpdateNextSecondWeekSchedules {
  356. // nextWeek := now.AddDate(0, 0, 14)
  357. // if updateErr := _updateWeekSchedules(tx, orgID, nextWeek, addTemplateItems, delTemplateItems, changeTemplateItems, devicesMap); updateErr != nil {
  358. // tx.Rollback()
  359. // return updateErr
  360. // }
  361. //}
  362. //
  363. //if shouldUpdateNextThirdWeekSchedules {
  364. // nextWeek := now.AddDate(0, 0, 21)
  365. // if updateErr := _updateWeekSchedules(tx, orgID, nextWeek, addTemplateItems, delTemplateItems, changeTemplateItems, devicesMap); updateErr != nil {
  366. // tx.Rollback()
  367. // return updateErr
  368. // }
  369. //}
  370. //
  371. //if shouldUpdateNextFourWeekSchedules {
  372. // nextWeek := now.AddDate(0, 0, 28)
  373. // if updateErr := _updateWeekSchedules(tx, orgID, nextWeek, addTemplateItems, delTemplateItems, changeTemplateItems, devicesMap); updateErr != nil {
  374. // tx.Rollback()
  375. // return updateErr
  376. // }
  377. //}
  378. //if orgID != 9671 && orgID != 10340 {
  379. // if shouldUpdateNextWeekSchedules {
  380. // nextWeek := now.AddDate(0, 0, 7)
  381. // if updateErr := _updateWeekSchedules(tx, orgID, nextWeek, addTemplateItems, delTemplateItems, changeTemplateItems, devicesMap); updateErr != nil {
  382. // tx.Rollback()
  383. // return updateErr
  384. // }
  385. // }
  386. // if shouldUpdateNextSecondWeekSchedules {
  387. // nextWeek := now.AddDate(0, 0, 14)
  388. // if updateErr := _updateWeekSchedules(tx, orgID, nextWeek, addTemplateItems, delTemplateItems, changeTemplateItems, devicesMap); updateErr != nil {
  389. // tx.Rollback()
  390. // return updateErr
  391. // }
  392. // }
  393. //}
  394. tx.Commit()
  395. return nil
  396. }
  397. func _updateWeekSchedules(tx *gorm.DB, orgID int64, week time.Time, addTemplateItems []*models.PatientScheduleTemplateItem, delTemplateItems []*models.PatientScheduleTemplateItem, changeTemplateItems []*models.PatientScheduleTemplateItem, deviceNumberMap map[int64]*DeviceNumberViewModel) error {
  398. if deviceNumberMap == nil {
  399. deviceNumbers, getDeviceNumbersErr := GetAllValidDeviceNumbers(orgID)
  400. if getDeviceNumbersErr != nil {
  401. return getDeviceNumbersErr
  402. }
  403. deviceNumberMap = make(map[int64]*DeviceNumberViewModel, 0)
  404. for _, deviceNumber := range deviceNumbers {
  405. deviceNumberMap[deviceNumber.ID] = deviceNumber
  406. }
  407. }
  408. now := time.Now()
  409. monday, _ := utils.GetMondayAndSundayOfWeekDate(&week)
  410. // 先取消要取消的排班和新排班可能存在的重复排班
  411. delIns := make([]string, 0, len(addTemplateItems)+len(delTemplateItems))
  412. addValueStrs := make([]string, 0, len(addTemplateItems))
  413. addValues := make([]interface{}, 0, len(addTemplateItems)*11)
  414. for _, item := range addTemplateItems {
  415. scheduleDate := monday.AddDate(0, 0, int(item.Weekday-1))
  416. delIns = append(delIns, fmt.Sprintf("(%v, %v, %v)", item.DeviceNumberID, scheduleDate.Unix(), item.TimeType))
  417. // 构建新排班的条件
  418. deviceNumber := deviceNumberMap[item.DeviceNumberID]
  419. if deviceNumber != nil {
  420. addValueStrs = append(addValueStrs, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
  421. addValues = append(addValues, orgID)
  422. addValues = append(addValues, deviceNumber.ZoneID)
  423. addValues = append(addValues, item.DeviceNumberID)
  424. addValues = append(addValues, item.PatientID)
  425. addValues = append(addValues, scheduleDate.Unix())
  426. addValues = append(addValues, item.Weekday)
  427. addValues = append(addValues, item.TimeType)
  428. addValues = append(addValues, item.TreatMode)
  429. addValues = append(addValues, 1)
  430. addValues = append(addValues, now.Unix())
  431. addValues = append(addValues, now.Unix())
  432. }
  433. }
  434. for _, item := range delTemplateItems {
  435. delIns = append(delIns, fmt.Sprintf("(%v, %v, %v)", item.DeviceNumberID, monday.AddDate(0, 0, int(item.Weekday-1)).Unix(), item.TimeType))
  436. }
  437. if len(delIns) > 0 {
  438. 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, ","))
  439. if delErr := tx.Exec(sql).Error; delErr != nil {
  440. tx.Rollback()
  441. return delErr
  442. }
  443. }
  444. // 批量插入排班
  445. if len(addValueStrs) > 0 {
  446. 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, ", "))
  447. if insertErr := tx.Exec(sql, addValues...).Error; insertErr != nil {
  448. return insertErr
  449. }
  450. }
  451. // 更新已有排班
  452. if len(changeTemplateItems) > 0 {
  453. for _, item := range changeTemplateItems {
  454. 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 {
  455. tx.Rollback()
  456. return updateErr
  457. }
  458. }
  459. }
  460. return nil
  461. }
  462. func SavePatientScheduleTemplateMode(templateMode *models.PatientScheduleTemplateMode) error {
  463. tx := writeDb.Begin()
  464. saveErr := tx.Save(templateMode).Error
  465. if saveErr != nil {
  466. tx.Rollback()
  467. return saveErr
  468. }
  469. tx.Commit()
  470. return nil
  471. }
  472. // 1 -> 0 或 2 -> 0 则不处理排班,但清除所有模板item
  473. func SavePatientScheduleTemplateModeAndClearAllTemplateItems(templateMode *models.PatientScheduleTemplateMode) error {
  474. tx := writeDb.Begin()
  475. saveErr := tx.Save(templateMode).Error
  476. if saveErr != nil {
  477. tx.Rollback()
  478. return saveErr
  479. }
  480. disableErr := tx.Model(&models.PatientScheduleTemplateItem{}).Where("org_id = ? AND status = 1", templateMode.OrgID).Updates(map[string]interface{}{"status": 0, "mtime": time.Now().Unix()}).Error
  481. if disableErr != nil {
  482. tx.Rollback()
  483. return disableErr
  484. }
  485. tx.Commit()
  486. return nil
  487. }
  488. // 0 -> 1 或 0 -> 2 则直接清除下两周的排班
  489. func SavePatientScheduleTemplateModeAndClearNextTwoWeekSchedules(templateMode *models.PatientScheduleTemplateMode) error {
  490. tx := writeDb.Begin()
  491. saveErr := tx.Save(templateMode).Error
  492. if saveErr != nil {
  493. tx.Rollback()
  494. return saveErr
  495. }
  496. now := time.Now()
  497. nextWeek := now.AddDate(0, 0, 7)
  498. nextMonday, _ := utils.GetMondayAndSundayOfWeekDate(&nextWeek)
  499. nextSecWeek := now.AddDate(0, 0, 21)
  500. _, nextSecSunday := utils.GetMondayAndSundayOfWeekDate(&nextSecWeek)
  501. 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
  502. if disableErr != nil {
  503. tx.Rollback()
  504. return disableErr
  505. }
  506. tx.Commit()
  507. return nil
  508. }
  509. // 1 -> 3 清除第二周排班
  510. func SavePatientScheduleTemplateModeAndClearNextThirfWeekSchedules(templateMode *models.PatientScheduleTemplateMode) error {
  511. tx := writeDb.Begin()
  512. saveErr := tx.Save(templateMode).Error
  513. if saveErr != nil {
  514. tx.Rollback()
  515. return saveErr
  516. }
  517. now := time.Now()
  518. nextSecWeek := now.AddDate(0, 0, 21)
  519. nextSecMonday, nextSecSunday := utils.GetMondayAndSundayOfWeekDate(&nextSecWeek)
  520. //tx.Model(&models.PatientScheduleTemplateId{}).Where("user_org_id = ? AND status = 1", templateMode.OrgID, nextSecMonday.Unix(), nextSecSunday.Unix()).Updates(map[string]interface{}{"status": 0, "updated_time": now.Unix()}).Error
  521. 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, "updated_time": now.Unix()}).Error
  522. if disableErr != nil {
  523. tx.Rollback()
  524. return disableErr
  525. }
  526. tx.Commit()
  527. return nil
  528. }
  529. // 1 -> 4 清除第二周排班
  530. func SavePatientScheduleTemplateModeAndClearNextFourWeekSchedules(templateMode *models.PatientScheduleTemplateMode) error {
  531. tx := writeDb.Begin()
  532. saveErr := tx.Save(templateMode).Error
  533. if saveErr != nil {
  534. tx.Rollback()
  535. return saveErr
  536. }
  537. now := time.Now()
  538. nextSecWeek := now.AddDate(0, 0, 14)
  539. nextSecMonday, nextSecSunday := utils.GetMondayAndSundayOfWeekDate(&nextSecWeek)
  540. //tx.Model(&models.PatientScheduleTemplateId{}).Where("user_org_id = ? AND status = 1", templateMode.OrgID, nextSecMonday.Unix(), nextSecSunday.Unix()).Updates(map[string]interface{}{"status": 0, "updated_time": now.Unix()}).Error
  541. 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, "updated_time": now.Unix()}).Error
  542. if disableErr != nil {
  543. tx.Rollback()
  544. return disableErr
  545. }
  546. tx.Commit()
  547. return nil
  548. }
  549. func SaveTemplateMode(templateMode *models.PatientScheduleTemplateMode) error {
  550. err := writeDb.Model(&models.PatientScheduleTemplateMode{}).Where("id = ? AND status = 1", templateMode.ID).Updates(map[string]interface{}{"mode": templateMode.Mode, "mtime": templateMode.ModifyTime}).Error
  551. return err
  552. }
  553. // 2 -> 1 清除第二周模板item,清除第二周排班,将第一周的排班重复到第二周
  554. func SavePatientScheduleTemplateMode2To1(templateMode *models.PatientScheduleTemplateMode, secondWeekTemplateID int64, firstWeekTemplateItems []*models.PatientScheduleTemplateItem) error {
  555. tx := writeDb.Begin()
  556. saveErr := tx.Save(templateMode).Error
  557. if saveErr != nil {
  558. tx.Rollback()
  559. return saveErr
  560. }
  561. now := time.Now()
  562. disableItemsErr := tx.Model(&models.PatientScheduleTemplateItem{}).Where("template_id = ? AND status = 1", secondWeekTemplateID).Updates(map[string]interface{}{"status": 0, "mtime": now.Unix()}).Error
  563. if disableItemsErr != nil {
  564. tx.Rollback()
  565. return disableItemsErr
  566. }
  567. nextSecWeek := now.AddDate(0, 0, 14)
  568. nextSecMonday, nextSecSunday := utils.GetMondayAndSundayOfWeekDate(&nextSecWeek)
  569. 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, "updated_time": now.Unix()}).Error
  570. if disableSchsErr != nil {
  571. tx.Rollback()
  572. return disableSchsErr
  573. }
  574. if len(firstWeekTemplateItems) > 0 {
  575. deviceNumbers, getDeviceNumbersErr := GetAllValidDeviceNumbers(templateMode.OrgID)
  576. if getDeviceNumbersErr != nil {
  577. tx.Rollback()
  578. return getDeviceNumbersErr
  579. }
  580. deviceNumberMap := make(map[int64]*DeviceNumberViewModel, 0)
  581. for _, deviceNumber := range deviceNumbers {
  582. deviceNumberMap[deviceNumber.ID] = deviceNumber
  583. }
  584. valueStrs := make([]string, 0, len(firstWeekTemplateItems))
  585. values := make([]interface{}, 0, len(firstWeekTemplateItems)*11)
  586. for _, item := range firstWeekTemplateItems {
  587. deviceNumber := deviceNumberMap[item.DeviceNumberID]
  588. if deviceNumber == nil {
  589. continue
  590. }
  591. valueStrs = append(valueStrs, "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
  592. values = append(values, templateMode.OrgID)
  593. values = append(values, deviceNumber.ZoneID)
  594. values = append(values, item.DeviceNumberID)
  595. values = append(values, item.PatientID)
  596. values = append(values, nextSecMonday.AddDate(0, 0, int(item.Weekday-1)).Unix())
  597. values = append(values, item.Weekday)
  598. values = append(values, item.TimeType)
  599. values = append(values, item.TreatMode)
  600. values = append(values, 1)
  601. values = append(values, now.Unix())
  602. values = append(values, now.Unix())
  603. }
  604. 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, ", "))
  605. if insertErr := tx.Exec(sql, values...).Error; insertErr != nil {
  606. return insertErr
  607. }
  608. }
  609. tx.Commit()
  610. return nil
  611. }
  612. // 3 -> 1 清除第三,二周模板item,清除第三,二周排班
  613. func SavePatientScheduleTemplateMode3To1(templateMode *models.PatientScheduleTemplateMode, secondWeekTemplateID int64, thirdWeekTemplateID int64) error {
  614. tx := writeDb.Begin()
  615. saveErr := tx.Save(templateMode).Error
  616. if saveErr != nil {
  617. tx.Rollback()
  618. return saveErr
  619. }
  620. now := time.Now()
  621. disableItemsErr := tx.Model(&models.PatientScheduleTemplateItem{}).Where("(template_id = ? AND status = 1) OR (template_id = ? AND status = 1) ", secondWeekTemplateID, thirdWeekTemplateID).Updates(map[string]interface{}{"status": 0, "mtime": now.Unix()}).Error
  622. if disableItemsErr != nil {
  623. tx.Rollback()
  624. return disableItemsErr
  625. }
  626. nextSecWeek := now.AddDate(0, 0, 14)
  627. nextSecMonday, nextSecSunday := utils.GetMondayAndSundayOfWeekDate(&nextSecWeek)
  628. 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, "updated_time": now.Unix()}).Error
  629. if disableSchsErr != nil {
  630. tx.Rollback()
  631. return disableSchsErr
  632. }
  633. nextThirdWeek := now.AddDate(0, 0, 21)
  634. nextThirdMonday, nextThirdSunday := utils.GetMondayAndSundayOfWeekDate(&nextThirdWeek)
  635. disableSchsErr2 := tx.Model(&models.Schedule{}).Where("user_org_id = ? AND status = 1 AND schedule_date >= ? AND schedule_date <= ?", templateMode.OrgID, nextThirdMonday.Unix(), nextThirdSunday.Unix()).Updates(map[string]interface{}{"status": 0, "updated_time": now.Unix()}).Error
  636. if disableSchsErr2 != nil {
  637. tx.Rollback()
  638. return disableSchsErr2
  639. }
  640. tx.Commit()
  641. return nil
  642. }
  643. // 3 -> 2 清除第三周模板item,清除第三周排班
  644. func SavePatientScheduleTemplateMode3To2(templateMode *models.PatientScheduleTemplateMode, thirdWeekTemplateID int64) error {
  645. tx := writeDb.Begin()
  646. saveErr := tx.Save(templateMode).Error
  647. if saveErr != nil {
  648. tx.Rollback()
  649. return saveErr
  650. }
  651. now := time.Now()
  652. disableItemsErr := tx.Model(&models.PatientScheduleTemplateItem{}).Where("template_id = ? AND status = 1", thirdWeekTemplateID).Updates(map[string]interface{}{"status": 0, "mtime": now.Unix()}).Error
  653. if disableItemsErr != nil {
  654. tx.Rollback()
  655. return disableItemsErr
  656. }
  657. nextThirdWeek := now.AddDate(0, 0, 21)
  658. nextThirdMonday, nextThirdSunday := utils.GetMondayAndSundayOfWeekDate(&nextThirdWeek)
  659. disableSchsErr2 := tx.Model(&models.Schedule{}).Where("user_org_id = ? AND status = 1 AND schedule_date >= ? AND schedule_date <= ?", templateMode.OrgID, nextThirdMonday.Unix(), nextThirdSunday.Unix()).Updates(map[string]interface{}{"status": 0, "updated_time": now.Unix()}).Error
  660. if disableSchsErr2 != nil {
  661. tx.Rollback()
  662. return disableSchsErr2
  663. }
  664. tx.Commit()
  665. return nil
  666. }
  667. // 4 -> 1 清除第四,三,二周模板item,清除第四,三,二周排班
  668. func SavePatientScheduleTemplateMode4To1(templateMode *models.PatientScheduleTemplateMode, secondWeekTemplateID int64, thirdWeekTemplateID int64, fourWeekTemplateID int64) error {
  669. tx := writeDb.Begin()
  670. saveErr := tx.Save(templateMode).Error
  671. if saveErr != nil {
  672. tx.Rollback()
  673. return saveErr
  674. }
  675. now := time.Now()
  676. disableItemsErr := tx.Model(&models.PatientScheduleTemplateItem{}).Where("(template_id = ? AND status = 1)OR(template_id = ? AND status = 1)OR(template_id = ? AND status = 1)", secondWeekTemplateID, thirdWeekTemplateID, fourWeekTemplateID).Updates(map[string]interface{}{"status": 0, "mtime": now.Unix()}).Error
  677. if disableItemsErr != nil {
  678. tx.Rollback()
  679. return disableItemsErr
  680. }
  681. nextSecWeek := now.AddDate(0, 0, 14)
  682. nextSecMonday, nextSecSunday := utils.GetMondayAndSundayOfWeekDate(&nextSecWeek)
  683. 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, "updated_time": now.Unix()}).Error
  684. if disableSchsErr != nil {
  685. tx.Rollback()
  686. return disableSchsErr
  687. }
  688. nextThirdWeek := now.AddDate(0, 0, 21)
  689. nextThirdMonday, nextThirdSunday := utils.GetMondayAndSundayOfWeekDate(&nextThirdWeek)
  690. disableSchsErr2 := tx.Model(&models.Schedule{}).Where("user_org_id = ? AND status = 1 AND schedule_date >= ? AND schedule_date <= ?", templateMode.OrgID, nextThirdMonday.Unix(), nextThirdSunday.Unix()).Updates(map[string]interface{}{"status": 0, "updated_time": now.Unix()}).Error
  691. if disableSchsErr2 != nil {
  692. tx.Rollback()
  693. return disableSchsErr2
  694. }
  695. nextFourWeek := now.AddDate(0, 0, 28)
  696. nextFourMonday, nextFourSunday := utils.GetMondayAndSundayOfWeekDate(&nextFourWeek)
  697. disableSchsErr3 := tx.Model(&models.Schedule{}).Where("user_org_id = ? AND status = 1 AND schedule_date >= ? AND schedule_date <= ?", templateMode.OrgID, nextFourMonday.Unix(), nextFourSunday.Unix()).Updates(map[string]interface{}{"status": 0, "updated_time": now.Unix()}).Error
  698. if disableSchsErr3 != nil {
  699. tx.Rollback()
  700. return disableSchsErr3
  701. }
  702. tx.Commit()
  703. return nil
  704. }
  705. // 4 -> 2 清除第四,三模板item,清除第四,三排班
  706. func SavePatientScheduleTemplateMode4To2(templateMode *models.PatientScheduleTemplateMode, thirdWeekTemplateID int64, fourWeekTemplateID int64) error {
  707. tx := writeDb.Begin()
  708. saveErr := tx.Save(templateMode).Error
  709. if saveErr != nil {
  710. tx.Rollback()
  711. return saveErr
  712. }
  713. now := time.Now()
  714. disableItemsErr := tx.Model(&models.PatientScheduleTemplateItem{}).Where("(template_id = ? AND status = 1)OR(template_id = ? AND status = 1)", thirdWeekTemplateID, fourWeekTemplateID).Updates(map[string]interface{}{"status": 0, "mtime": now.Unix()}).Error
  715. if disableItemsErr != nil {
  716. tx.Rollback()
  717. return disableItemsErr
  718. }
  719. nextThirdWeek := now.AddDate(0, 0, 21)
  720. nextThirdMonday, nextThirdSunday := utils.GetMondayAndSundayOfWeekDate(&nextThirdWeek)
  721. disableSchsErr2 := tx.Model(&models.Schedule{}).Where("user_org_id = ? AND status = 1 AND schedule_date >= ? AND schedule_date <= ?", templateMode.OrgID, nextThirdMonday.Unix(), nextThirdSunday.Unix()).Updates(map[string]interface{}{"status": 0, "updated_time": now.Unix()}).Error
  722. if disableSchsErr2 != nil {
  723. tx.Rollback()
  724. return disableSchsErr2
  725. }
  726. nextFourWeek := now.AddDate(0, 0, 28)
  727. nextFourMonday, nextFourSunday := utils.GetMondayAndSundayOfWeekDate(&nextFourWeek)
  728. disableSchsErr3 := tx.Model(&models.Schedule{}).Where("user_org_id = ? AND status = 1 AND schedule_date >= ? AND schedule_date <= ?", templateMode.OrgID, nextFourMonday.Unix(), nextFourSunday.Unix()).Updates(map[string]interface{}{"status": 0, "updated_time": now.Unix()}).Error
  729. if disableSchsErr3 != nil {
  730. tx.Rollback()
  731. return disableSchsErr3
  732. }
  733. tx.Commit()
  734. return nil
  735. }
  736. // 4 -> 3 清除第四模板item,清除第四周排班
  737. func SavePatientScheduleTemplateMode4To3(templateMode *models.PatientScheduleTemplateMode, fourWeekTemplateID int64) error {
  738. tx := writeDb.Begin()
  739. saveErr := tx.Save(templateMode).Error
  740. if saveErr != nil {
  741. tx.Rollback()
  742. return saveErr
  743. }
  744. now := time.Now()
  745. disableItemsErr := tx.Model(&models.PatientScheduleTemplateItem{}).Where("(template_id = ? AND status = 1)", fourWeekTemplateID).Updates(map[string]interface{}{"status": 0, "mtime": now.Unix()}).Error
  746. if disableItemsErr != nil {
  747. tx.Rollback()
  748. return disableItemsErr
  749. }
  750. nextFourWeek := now.AddDate(0, 0, 28)
  751. nextFourMonday, nextFourSunday := utils.GetMondayAndSundayOfWeekDate(&nextFourWeek)
  752. disableSchsErr3 := tx.Model(&models.Schedule{}).Where("user_org_id = ? AND status = 1 AND schedule_date >= ? AND schedule_date <= ?", templateMode.OrgID, nextFourMonday.Unix(), nextFourSunday.Unix()).Updates(map[string]interface{}{"status": 0, "updated_time": now.Unix()}).Error
  753. if disableSchsErr3 != nil {
  754. tx.Rollback()
  755. return disableSchsErr3
  756. }
  757. tx.Commit()
  758. return nil
  759. }
  760. type SchTemplatePatient struct {
  761. ID int64 `gorm:"column:id" json:"id" form:"id"`
  762. UserOrgId int64 `gorm:"column:user_org_id" json:"user_org_id" form:"user_org_id"`
  763. Name string `gorm:"column:name" json:"name" form:"name"`
  764. Lapseto int64 `gorm:"column:lapseto" json:"lapseto" form:"lapseto"`
  765. Status int64 `gorm:"column:status" json:"status" form:"status"`
  766. DialysisNo string `gorm:"column:dialysis_no" json:"dialysis_no" form:"dialysis_no"`
  767. IsInfectious int64 `gorm:"column:is_infectious" json:"is_infectious"`
  768. FirstLetter string `gorm:"column:first_letter" json:"first_letter" form:"first_letter"`
  769. }
  770. func (SchTemplatePatient) TableName() string {
  771. return "xt_patients"
  772. }
  773. func GetSchTemplatePatientList(orgID int64) ([]*SchTemplatePatient, error) {
  774. var patients []*SchTemplatePatient
  775. err := readDb.Model(&SchTemplatePatient{}).Where("user_org_id = ? AND status = 1 AND lapseto = 1", orgID).Find(&patients).Error
  776. return patients, err
  777. }
  778. func UpdateTemplateItemWeek(orgID int64, id int64, week_time int8) {
  779. writeDb.Model(&models.PatientScheduleTemplateId{}).Where("org_id = ? AND status = 1 AND id = ? ", orgID, id).Updates(map[string]interface{}{"week": week_time})
  780. return
  781. }
  782. func GetOrgPatientScheduleTemplateModeTwo(orgID int64) (models.PatientScheduleTemplateMode, error) {
  783. var mode models.PatientScheduleTemplateMode
  784. err := readDb.Model(&models.PatientScheduleTemplateMode{}).Where("org_id = ? AND status = 1", orgID).First(&mode).Error
  785. return mode, err
  786. }
  787. func GetAllPatientScheduleTemplateItemsWithoutPatientByTemplateID(orgID int64) ([]*models.PatientScheduleTemplateItem, error) {
  788. var items []*models.PatientScheduleTemplateItem
  789. 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.status = 1 and patient.lapseto = 1 and patient.status = 1", orgID).Scan(&items).Error
  790. // err := readDb.
  791. // Model(&models.PatientScheduleTemplateItem{}).
  792. // Where("org_id = ? AND template_id = ? AND status = 1", orgID, templateID).
  793. // Find(&items).
  794. // Error
  795. if err != nil {
  796. return nil, err
  797. }
  798. return items, nil
  799. }
  800. func GetScheduleTemplateItem(id int64) (item models.PatientScheduleTemplateItem, err error) {
  801. err = readDb.Model(&models.PatientScheduleTemplateItem{}).Where("id = ?", id).First(&item).Error
  802. return
  803. }
  804. func SaveSchTemplateItem(item models.PatientScheduleTemplateItem) (err error) {
  805. err = writeDb.Save(&item).Error
  806. return
  807. }
  808. func UpdateTemplateItemModeWeek(orgID int64, id int64, week_time int64) {
  809. readDb.Model(&models.PatientScheduleTemplateMode{}).Where("org_id = ? AND status = 1 AND id = ? ", orgID, id).Updates(map[string]interface{}{"original_week": week_time, "week": week_time})
  810. return
  811. }
  812. func ClearAndCreateSyncSch(user_org_id int64, start_time int64, schs []models.Schedule) (err error) {
  813. utx := writeDb.Begin()
  814. err = utx.Table("xt_schedule as s").
  815. Where("s.schedule_date > ? AND s.status = 1 AND "+
  816. "s.user_org_id = ?", start_time, user_org_id).
  817. Updates(map[string]interface{}{"status": 0, "updated_time": time.Now().Unix()}).Error
  818. if err != nil {
  819. utx.Rollback()
  820. return
  821. }
  822. if len(schs) > 0 {
  823. if len(schs) > 0 {
  824. thisSQL := "INSERT INTO xt_schedule (user_org_id, partition_id, bed_id, patient_id, schedule_date,schedule_type,schedule_week,mode_id,status,created_time,updated_time,is_export,dialysis_machine_name,remark) VALUES "
  825. insertParams := make([]string, 0)
  826. insertData := make([]interface{}, 0)
  827. for _, info := range schs {
  828. insertParams = append(insertParams, "(?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
  829. insertData = append(insertData, info.UserOrgId)
  830. insertData = append(insertData, info.PartitionId)
  831. insertData = append(insertData, info.BedId)
  832. insertData = append(insertData, info.PatientId)
  833. insertData = append(insertData, info.ScheduleDate)
  834. insertData = append(insertData, info.ScheduleType)
  835. insertData = append(insertData, info.ScheduleWeek)
  836. insertData = append(insertData, info.ModeId)
  837. insertData = append(insertData, info.Status)
  838. insertData = append(insertData, info.CreatedTime)
  839. insertData = append(insertData, info.UpdatedTime)
  840. insertData = append(insertData, info.IsExport)
  841. insertData = append(insertData, info.DialysisMachineName)
  842. insertData = append(insertData, "")
  843. }
  844. thisSQL += strings.Join(insertParams, ", ")
  845. err = utx.Exec(thisSQL, insertData...).Error
  846. if err != nil {
  847. utx.Rollback()
  848. return
  849. }
  850. }
  851. utx.Commit()
  852. }
  853. return
  854. }