line_equation.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. // CreateLineEquationChartAndEdb 批量新增拟合方程图表
  13. func CreateLineEquationChartAndEdb(multipleGraphConfig *data_manage.MultipleGraphConfig, batchAddChartList []BatchAddChart) (err error) {
  14. tx := global.DmSQL["data"].Begin()
  15. if err != nil {
  16. return
  17. }
  18. defer func() {
  19. if err != nil {
  20. _ = tx.Rollback()
  21. } else {
  22. _ = tx.Commit()
  23. }
  24. }()
  25. // 新增批量配置
  26. err = tx.Create(multipleGraphConfig).Error
  27. if err != nil {
  28. return
  29. }
  30. for k, v := range batchAddChartList {
  31. // 新增图表信息
  32. chartInfo := v.ChartInfo
  33. err = tx.Create(chartInfo).Error
  34. if err != nil {
  35. return
  36. }
  37. batchAddChartList[k].ChartInfo = chartInfo
  38. // 指标mapping
  39. edbMappingList := v.EdbMappingList
  40. if len(edbMappingList) > 0 {
  41. for i := range edbMappingList {
  42. edbMappingList[i].ChartInfoId = chartInfo.ChartInfoId
  43. }
  44. err = tx.CreateInBatches(edbMappingList, utils.MultiAddNum).Error
  45. if err != nil {
  46. return
  47. }
  48. }
  49. batchAddChartList[k].EdbMappingList = edbMappingList
  50. // 批量配置的关系表
  51. multipleGraphConfigChartMapping := v.MultipleGraphConfigChartMapping
  52. multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
  53. multipleGraphConfigChartMapping.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
  54. err = tx.Create(v.MultipleGraphConfigChartMapping).Error
  55. if err != nil {
  56. return
  57. }
  58. }
  59. return
  60. }
  61. // CreateAndEditLineEquationChartAndEdb 批量新增/编辑拟合方程图表
  62. func CreateAndEditLineEquationChartAndEdb(multipleGraphConfig *data_manage.MultipleGraphConfig, batchAddChartList []BatchAddChart, updateMultipleGraphConfigCols, updateChartCols []string) (err error) {
  63. tx := global.DmSQL["data"].Begin()
  64. if err != nil {
  65. return
  66. }
  67. defer func() {
  68. if err != nil {
  69. _ = tx.Rollback()
  70. } else {
  71. _ = tx.Commit()
  72. }
  73. }()
  74. // 编辑批量配置
  75. err = tx.Select(updateMultipleGraphConfigCols).Updates(multipleGraphConfig).Error
  76. if err != nil {
  77. return
  78. }
  79. for k, v := range batchAddChartList {
  80. // 新增图表信息
  81. chartInfo := v.ChartInfo
  82. var isDelMapping bool
  83. {
  84. if chartInfo.ChartInfoId <= 0 {
  85. err = tx.Create(chartInfo).Error
  86. if err != nil {
  87. return
  88. }
  89. batchAddChartList[k].ChartInfo = chartInfo
  90. } else {
  91. // 更改图表信息
  92. err = tx.Select(updateChartCols).Updates(chartInfo).Error
  93. if err != nil {
  94. return
  95. }
  96. isDelMapping = true
  97. }
  98. }
  99. // 图表与指标的关系表
  100. {
  101. // 先删除图表关联的指标关系,然后再新增
  102. if isDelMapping {
  103. sql := ` DELETE FROM chart_edb_mapping WHERE chart_info_id=? `
  104. err = tx.Exec(sql, chartInfo.ChartInfoId).Error
  105. if err != nil {
  106. return
  107. }
  108. }
  109. // 指标mapping
  110. edbMappingList := v.EdbMappingList
  111. if len(edbMappingList) > 0 {
  112. for i := range edbMappingList {
  113. edbMappingList[i].ChartInfoId = chartInfo.ChartInfoId
  114. }
  115. err = tx.CreateInBatches(edbMappingList, utils.MultiAddNum).Error
  116. if err != nil {
  117. return
  118. }
  119. }
  120. batchAddChartList[k].EdbMappingList = edbMappingList
  121. }
  122. // 批量配置的关系表
  123. {
  124. multipleGraphConfigChartMapping := v.MultipleGraphConfigChartMapping
  125. if multipleGraphConfigChartMapping.Id <= 0 {
  126. multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
  127. multipleGraphConfigChartMapping.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
  128. err = tx.Create(v.MultipleGraphConfigChartMapping).Error
  129. if err != nil {
  130. return
  131. }
  132. } else {
  133. if multipleGraphConfigChartMapping.ChartInfoId != chartInfo.ChartInfoId {
  134. multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
  135. err = tx.Select("ChartInfoId").Updates(multipleGraphConfigChartMapping).Error
  136. if err != nil {
  137. return
  138. }
  139. }
  140. }
  141. batchAddChartList[k].MultipleGraphConfigChartMapping = multipleGraphConfigChartMapping
  142. }
  143. }
  144. return
  145. }