line_equation.go 4.1 KB

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