line_equation.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package line_equation
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/models/data_manage"
  5. "eta_gn/eta_api/utils"
  6. )
  7. type BatchAddChart struct {
  8. ChartInfo *data_manage.ChartInfo
  9. EdbMappingList []*data_manage.ChartEdbMapping
  10. MultipleGraphConfigChartMapping *data_manage.MultipleGraphConfigChartMapping
  11. }
  12. func CreateLineEquationChartAndEdb(multipleGraphConfig *data_manage.MultipleGraphConfig, batchAddChartList []BatchAddChart) (err error) {
  13. tx := global.DmSQL["data"].Begin()
  14. defer func() {
  15. if err != nil {
  16. _ = tx.Rollback()
  17. } else {
  18. _ = tx.Commit()
  19. }
  20. }()
  21. err = tx.Create(multipleGraphConfig).Error
  22. if err != nil {
  23. return
  24. }
  25. for k, v := range batchAddChartList {
  26. chartInfo := v.ChartInfo
  27. err = tx.Create(chartInfo).Error
  28. if err != nil {
  29. return
  30. }
  31. batchAddChartList[k].ChartInfo = chartInfo
  32. edbMappingList := v.EdbMappingList
  33. if len(edbMappingList) > 0 {
  34. for i := range edbMappingList {
  35. edbMappingList[i].ChartInfoId = chartInfo.ChartInfoId
  36. }
  37. err = tx.CreateInBatches(edbMappingList, utils.MultiAddNum).Error
  38. if err != nil {
  39. return
  40. }
  41. }
  42. batchAddChartList[k].EdbMappingList = edbMappingList
  43. multipleGraphConfigChartMapping := v.MultipleGraphConfigChartMapping
  44. multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
  45. multipleGraphConfigChartMapping.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
  46. err = tx.Create(v.MultipleGraphConfigChartMapping).Error
  47. if err != nil {
  48. return
  49. }
  50. }
  51. return
  52. }
  53. func CreateAndEditLineEquationChartAndEdb(multipleGraphConfig *data_manage.MultipleGraphConfig, batchAddChartList []BatchAddChart, updateMultipleGraphConfigCols, updateChartCols []string) (err error) {
  54. tx := global.DmSQL["data"].Begin()
  55. defer func() {
  56. if err != nil {
  57. _ = tx.Rollback()
  58. } else {
  59. _ = tx.Commit()
  60. }
  61. }()
  62. err = tx.Select(updateMultipleGraphConfigCols).Updates(multipleGraphConfig).Error
  63. if err != nil {
  64. return
  65. }
  66. for k, v := range batchAddChartList {
  67. chartInfo := v.ChartInfo
  68. var isDelMapping bool
  69. {
  70. if chartInfo.ChartInfoId <= 0 {
  71. err = tx.Create(chartInfo).Error
  72. if err != nil {
  73. return
  74. }
  75. batchAddChartList[k].ChartInfo = chartInfo
  76. } else {
  77. err = tx.Select(updateChartCols).Updates(chartInfo).Error
  78. if err != nil {
  79. return
  80. }
  81. isDelMapping = true
  82. }
  83. }
  84. {
  85. if isDelMapping {
  86. sql := ` DELETE FROM chart_edb_mapping WHERE chart_info_id=? `
  87. err = tx.Exec(sql, chartInfo.ChartInfoId).Error
  88. if err != nil {
  89. return
  90. }
  91. }
  92. edbMappingList := v.EdbMappingList
  93. if len(edbMappingList) > 0 {
  94. for i := range edbMappingList {
  95. edbMappingList[i].ChartInfoId = chartInfo.ChartInfoId
  96. }
  97. err = tx.CreateInBatches(edbMappingList, utils.MultiAddNum).Error
  98. if err != nil {
  99. return
  100. }
  101. }
  102. batchAddChartList[k].EdbMappingList = edbMappingList
  103. }
  104. {
  105. multipleGraphConfigChartMapping := v.MultipleGraphConfigChartMapping
  106. if multipleGraphConfigChartMapping.Id <= 0 {
  107. multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
  108. multipleGraphConfigChartMapping.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
  109. err = tx.Create(v.MultipleGraphConfigChartMapping).Error
  110. if err != nil {
  111. return
  112. }
  113. } else {
  114. if multipleGraphConfigChartMapping.ChartInfoId != chartInfo.ChartInfoId {
  115. multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
  116. err = tx.Select("ChartInfoId").Updates(multipleGraphConfigChartMapping).Error
  117. if err != nil {
  118. return
  119. }
  120. }
  121. }
  122. batchAddChartList[k].MultipleGraphConfigChartMapping = multipleGraphConfigChartMapping
  123. }
  124. }
  125. return
  126. }