patient_schedule_template_service.go 36KB

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