chart_description.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package models
  2. import (
  3. "eta/eta_forum_hub/utils"
  4. "time"
  5. "github.com/beego/beego/v2/client/orm"
  6. )
  7. type ChartDescription struct {
  8. Id int `gorm:"column:id;primaryKey"`
  9. Description string `gorm:"column:description"`
  10. ChartInfoId int `gorm:"column:chart_info_id"` // 图表id
  11. SysUserId int `gorm:"column:sys_user_id"` // 创建人id
  12. SysUserRealName string `gorm:"column:sys_user_real_name"` // 创建人姓名
  13. ModifyTime time.Time `gorm:"column:modify_time"` // 变更时间
  14. CreateTime time.Time `gorm:"column:create_time"` // 关系建立时间
  15. }
  16. type ChartDescriptionReq struct {
  17. Id int
  18. Description string // 图表id
  19. ChartInfoId int
  20. SysUserId int // 创建人id
  21. SysUserRealName string // 创建人姓名
  22. AdminName string // 创建人姓名
  23. ModifyTime time.Time // 变更时间
  24. CreateTime time.Time // 关系建立时间
  25. }
  26. // GetChartDescriptionByChartInfoId 根据图表ID获取图表简介
  27. func GetChartDescriptionByChartInfoId(chartInfoId int) (item []*ChartDescription, err error) {
  28. o := orm.NewOrm()
  29. sql := `SELECT * FROM chart_description WHERE chart_info_id = ? order by create_time desc, id desc`
  30. _, err = o.Raw(sql, chartInfoId).QueryRows(&item)
  31. return
  32. }
  33. // 批量更新
  34. func BatchUpdateChartDescription(chartInfoId int, descriptionList []*ChartDescription) (err error) {
  35. o := orm.NewOrm()
  36. // 用事务
  37. tx, err := o.Begin()
  38. if err != nil {
  39. return err
  40. }
  41. defer func () {
  42. if err != nil {
  43. tx.Rollback()
  44. }else{
  45. tx.Commit()
  46. }
  47. }()
  48. // 批量更新
  49. if len(descriptionList) == 0 {
  50. // 删除所有记录
  51. sql := `DELETE FROM chart_description WHERE chart_info_id = ?`
  52. _, err = tx.Raw(sql, chartInfoId).Exec()
  53. return
  54. }
  55. // 删除数组
  56. deleteIdMap := make(map[int]bool)
  57. // 查询现有记录
  58. existingRecords := make(map[int]*ChartDescription)
  59. var existing []*ChartDescription
  60. sql := `SELECT * FROM chart_description WHERE chart_info_id = ?`
  61. _, err = o.Raw(sql, chartInfoId).QueryRows(&existing)
  62. if err != nil {
  63. return err
  64. }
  65. for _, record := range existing {
  66. existingRecords[record.Id] = record
  67. deleteIdMap[record.Id] = true
  68. }
  69. // 分离需要更新和插入的记录
  70. var toInsert []*ChartDescription
  71. for _, desc := range descriptionList {
  72. delete(deleteIdMap, desc.Id)
  73. if _, exists := existingRecords[desc.Id]; exists {
  74. // 更新
  75. sql := "UPDATE chart_description SET description = ?, sys_user_id = ?, sys_user_real_name = ?, modify_time = ? WHERE id = ?"
  76. _, err = tx.Raw(sql, desc.Description, desc.SysUserId, desc.SysUserRealName, desc.ModifyTime, desc.Id).Exec()
  77. if err != nil {
  78. return err
  79. }
  80. } else {
  81. toInsert = append(toInsert, desc)
  82. }
  83. }
  84. // 批量插入
  85. if len(toInsert) > 0 {
  86. _, err := tx.InsertMulti(len(toInsert), toInsert)
  87. if err != nil {
  88. return err
  89. }
  90. }
  91. // 删除数组中为true的记录
  92. deleteIds := make([]int, 0)
  93. for id, _ := range deleteIdMap {
  94. deleteIds = append(deleteIds, id)
  95. }
  96. if len(deleteIds) > 0 {
  97. sql := `DELETE FROM chart_description WHERE id IN (`+utils.GetOrmInReplace(len(deleteIds))+`) AND chart_info_id = ?`
  98. _, err = tx.Raw(sql, deleteIds, chartInfoId).Exec()
  99. }
  100. return
  101. }