line_equation.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package line_equation
  2. import (
  3. "eta/eta_api/models/data_manage"
  4. "github.com/beego/beego/v2/client/orm"
  5. )
  6. type BatchAddChart struct {
  7. ChartInfo *data_manage.ChartInfo
  8. EdbMappingList []*data_manage.ChartEdbMapping
  9. MultipleGraphConfigChartMapping *data_manage.MultipleGraphConfigChartMapping
  10. }
  11. // CreateLineEquationChartAndEdb 批量新增拟合方程图表
  12. func CreateLineEquationChartAndEdb(multipleGraphConfig *data_manage.MultipleGraphConfig, batchAddChartList []BatchAddChart) (err error) {
  13. o := orm.NewOrmUsingDB("data")
  14. tx, err := o.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. newMultipleGraphConfigId, err := tx.Insert(multipleGraphConfig)
  27. if err != nil {
  28. return
  29. }
  30. multipleGraphConfig.MultipleGraphConfigId = int(newMultipleGraphConfigId)
  31. for k, v := range batchAddChartList {
  32. // 新增图表信息
  33. chartInfo := v.ChartInfo
  34. newId, tmpErr := tx.Insert(chartInfo)
  35. if tmpErr != nil {
  36. err = tmpErr
  37. return
  38. }
  39. chartInfo.ChartInfoId = int(newId)
  40. batchAddChartList[k].ChartInfo = chartInfo
  41. // 指标mapping
  42. edbMappingList := v.EdbMappingList
  43. if len(edbMappingList) > 0 {
  44. for i := range edbMappingList {
  45. edbMappingList[i].ChartInfoId = chartInfo.ChartInfoId
  46. }
  47. _, err = tx.InsertMulti(len(edbMappingList), edbMappingList)
  48. if err != nil {
  49. return
  50. }
  51. }
  52. batchAddChartList[k].EdbMappingList = edbMappingList
  53. // 批量配置的关系表
  54. multipleGraphConfigChartMapping := v.MultipleGraphConfigChartMapping
  55. multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
  56. multipleGraphConfigChartMapping.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
  57. newMultipleGraphConfigChartMappingId, tmpErr := tx.Insert(v.MultipleGraphConfigChartMapping)
  58. if tmpErr != nil {
  59. err = tmpErr
  60. return
  61. }
  62. multipleGraphConfigChartMapping.Id = int(newMultipleGraphConfigChartMappingId)
  63. }
  64. return
  65. }
  66. // CreateAndEditLineEquationChartAndEdb 批量新增/编辑拟合方程图表
  67. func CreateAndEditLineEquationChartAndEdb(multipleGraphConfig *data_manage.MultipleGraphConfig, batchAddChartList []BatchAddChart, updateMultipleGraphConfigCols, updateChartCols []string) (err error) {
  68. o := orm.NewOrmUsingDB("data")
  69. tx, err := o.Begin()
  70. if err != nil {
  71. return
  72. }
  73. defer func() {
  74. if err != nil {
  75. _ = tx.Rollback()
  76. } else {
  77. _ = tx.Commit()
  78. }
  79. }()
  80. // 编辑批量配置
  81. _, err = tx.Update(multipleGraphConfig, updateMultipleGraphConfigCols...)
  82. if err != nil {
  83. return
  84. }
  85. for k, v := range batchAddChartList {
  86. // 新增图表信息
  87. chartInfo := v.ChartInfo
  88. var isDelMapping bool
  89. {
  90. if chartInfo.ChartInfoId <= 0 {
  91. newId, tmpErr := tx.Insert(chartInfo)
  92. if tmpErr != nil {
  93. err = tmpErr
  94. return
  95. }
  96. chartInfo.ChartInfoId = int(newId)
  97. batchAddChartList[k].ChartInfo = chartInfo
  98. } else {
  99. // 更改图表信息
  100. _, err = tx.Update(chartInfo, updateChartCols...)
  101. if err != nil {
  102. return
  103. }
  104. isDelMapping = true
  105. }
  106. }
  107. // 图表与指标的关系表
  108. {
  109. // 先删除图表关联的指标关系,然后再新增
  110. if isDelMapping {
  111. sql := ` DELETE FROM chart_edb_mapping WHERE chart_info_id=? `
  112. _, err = tx.Raw(sql, chartInfo.ChartInfoId).Exec()
  113. if err != nil {
  114. return
  115. }
  116. }
  117. // 指标mapping
  118. edbMappingList := v.EdbMappingList
  119. if len(edbMappingList) > 0 {
  120. for i := range edbMappingList {
  121. edbMappingList[i].ChartInfoId = chartInfo.ChartInfoId
  122. }
  123. _, err = tx.InsertMulti(len(edbMappingList), edbMappingList)
  124. if err != nil {
  125. return
  126. }
  127. }
  128. batchAddChartList[k].EdbMappingList = edbMappingList
  129. }
  130. // 批量配置的关系表
  131. {
  132. multipleGraphConfigChartMapping := v.MultipleGraphConfigChartMapping
  133. if multipleGraphConfigChartMapping.Id <= 0 {
  134. multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
  135. multipleGraphConfigChartMapping.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
  136. newMultipleGraphConfigChartMappingId, tmpErr := tx.Insert(v.MultipleGraphConfigChartMapping)
  137. if tmpErr != nil {
  138. err = tmpErr
  139. return
  140. }
  141. multipleGraphConfigChartMapping.Id = int(newMultipleGraphConfigChartMappingId)
  142. } else {
  143. if multipleGraphConfigChartMapping.ChartInfoId != chartInfo.ChartInfoId {
  144. multipleGraphConfigChartMapping.ChartInfoId = chartInfo.ChartInfoId
  145. _, err = tx.Update(multipleGraphConfigChartMapping, "ChartInfoId")
  146. if err != nil {
  147. return
  148. }
  149. }
  150. }
  151. batchAddChartList[k].MultipleGraphConfigChartMapping = multipleGraphConfigChartMapping
  152. }
  153. }
  154. return
  155. }