create.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package voice_broadcast
  2. import (
  3. "hongze/hongze_yb/global"
  4. "hongze/hongze_yb/models/tables/voice_broadcast_img"
  5. )
  6. // AddVoiceBroadcast 新增记录
  7. func (voiceBroadcast *VoiceBroadcast) AddVoiceBroadcast() (err error) {
  8. err = global.DEFAULT_MYSQL.Create(voiceBroadcast).Error
  9. return
  10. }
  11. // Update 更新语音播报
  12. func (voiceBroadcast *VoiceBroadcast) Update(updateCols []string) (err error) {
  13. err = global.DEFAULT_MYSQL.Model(voiceBroadcast).Select(updateCols).Updates(*voiceBroadcast).Error
  14. return
  15. }
  16. // CreateVoiceBroadcastAndImgs 新增语音播报及图片
  17. func CreateVoiceBroadcastAndImgs(item *VoiceBroadcast, imgs []*voice_broadcast_img.YbVoiceBroadcastImg) (err error) {
  18. tx := global.DEFAULT_MYSQL.Begin()
  19. defer func() {
  20. if err != nil {
  21. tx.Rollback()
  22. } else {
  23. tx.Commit()
  24. }
  25. }()
  26. if err = tx.Create(item).Error; err != nil {
  27. return
  28. }
  29. imgLen := len(imgs)
  30. if imgLen > 0 {
  31. for i := 0; i < imgLen; i++ {
  32. imgs[i].BroadcastId = item.BroadcastId
  33. }
  34. if err = tx.Model(voice_broadcast_img.YbVoiceBroadcastImg{}).CreateInBatches(imgs, len(imgs)).Error; err != nil {
  35. return
  36. }
  37. }
  38. return
  39. }
  40. // UpdateVoiceBroadcastAndImgs 更新语音播报及图片
  41. func UpdateVoiceBroadcastAndImgs(item *VoiceBroadcast, updateCols []string, imgs []*voice_broadcast_img.YbVoiceBroadcastImg) (err error) {
  42. tx := global.DEFAULT_MYSQL.Begin()
  43. defer func() {
  44. if err != nil {
  45. tx.Rollback()
  46. } else {
  47. tx.Commit()
  48. }
  49. }()
  50. if err = tx.Model(item).Select(updateCols).Where("broadcast_id = ?", item.BroadcastId).Updates(*item).Error; err != nil {
  51. return
  52. }
  53. if err = tx.Where("broadcast_id = ?", item.BroadcastId).Delete(voice_broadcast_img.YbVoiceBroadcastImg{}).Error; err != nil {
  54. return
  55. }
  56. if len(imgs) > 0 {
  57. if err = tx.Model(voice_broadcast_img.YbVoiceBroadcastImg{}).CreateInBatches(imgs, len(imgs)).Error; err != nil {
  58. return
  59. }
  60. }
  61. return
  62. }