line_feature.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package line_feature
  2. import (
  3. "eta_gn/eta_api/global"
  4. "eta_gn/eta_api/models/data_manage"
  5. "eta_gn/eta_api/utils"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. type BatchAddChart struct {
  12. ChartInfo *data_manage.ChartInfo
  13. EdbMappingList []*data_manage.ChartEdbMapping
  14. MultipleGraphConfigChartMapping *data_manage.MultipleGraphConfigChartMapping
  15. }
  16. func CreateLineFeatureChartAndEdb(chartInfo *data_manage.ChartInfo, edbMappingList []*data_manage.ChartEdbMapping) (chartInfoId int, err error) {
  17. tx := global.DmSQL["data"].Begin()
  18. defer func() {
  19. if err != nil {
  20. _ = tx.Rollback()
  21. } else {
  22. _ = tx.Commit()
  23. }
  24. }()
  25. // 新增图表信息
  26. err = tx.Create(chartInfo).Error
  27. if err != nil {
  28. return
  29. }
  30. chartInfoId = chartInfo.ChartInfoId
  31. // 指标mapping
  32. if len(edbMappingList) > 0 {
  33. for i := range edbMappingList {
  34. edbMappingList[i].ChartInfoId = chartInfoId
  35. }
  36. err = tx.CreateInBatches(edbMappingList, utils.MultiAddNum).Error
  37. if err != nil {
  38. return
  39. }
  40. }
  41. return
  42. }
  43. // EditLineFeatureChartAndMapping 批量新增/编辑拟合方程图表
  44. func EditLineFeatureChartAndMapping(req *data_manage.EditChartInfoReq, edbInfoIdStr string, calendar string, dateType, disabled int, extraConfig string) (err error) {
  45. to := global.DmSQL["data"].Begin()
  46. defer func() {
  47. if err != nil {
  48. _ = to.Rollback()
  49. } else {
  50. _ = to.Commit()
  51. }
  52. }()
  53. var pars []interface{}
  54. pars = append(pars, req.ChartName)
  55. pars = append(pars, req.ChartNameEn)
  56. pars = append(pars, edbInfoIdStr)
  57. pars = append(pars, req.ChartClassifyId)
  58. pars = append(pars, disabled)
  59. pars = append(pars, extraConfig)
  60. sql := ` UPDATE chart_info
  61. SET
  62. chart_name =?,
  63. chart_name_en =?,
  64. edb_info_ids=?,
  65. chart_classify_id = ?,
  66. modify_time = NOW(),
  67. disabled = ?,
  68. extra_config = ?
  69. `
  70. if calendar != "" {
  71. sql += `,calendar = ? `
  72. pars = append(pars, calendar)
  73. }
  74. if dateType > 0 {
  75. sql += `,date_type = ? `
  76. pars = append(pars, dateType)
  77. }
  78. sql += `,start_date = ? `
  79. pars = append(pars, req.StartDate)
  80. sql += `,end_date = ? `
  81. pars = append(pars, req.EndDate)
  82. sql += `,season_start_date = ? `
  83. pars = append(pars, req.StartDate)
  84. sql += `,season_end_date = ? `
  85. pars = append(pars, req.EndDate)
  86. sql += `,left_min = ? `
  87. pars = append(pars, req.LeftMin)
  88. sql += `,left_max = ? `
  89. pars = append(pars, req.LeftMax)
  90. sql += `,right_min = ? `
  91. pars = append(pars, req.RightMin)
  92. sql += `,right_max = ? `
  93. pars = append(pars, req.RightMax)
  94. sql += `WHERE chart_info_id = ?`
  95. pars = append(pars, req.ChartInfoId)
  96. err = to.Exec(sql, pars).Error
  97. if err != nil {
  98. fmt.Println("UPDATE chart_info Err:", err.Error())
  99. return err
  100. }
  101. chartEdbMappingIdList := make([]string, 0)
  102. for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
  103. // 查询该指标是否存在,如果存在的话,那么就去修改,否则新增
  104. var tmpChartEdbMapping *data_manage.ChartEdbMapping
  105. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = ? `
  106. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId, utils.CHART_SOURCE_CORRELATION).First(&tmpChartEdbMapping).Error
  107. if err != nil && !utils.IsErrNoRow(err) {
  108. fmt.Println("QueryRow Err:", err.Error())
  109. return err
  110. }
  111. if tmpChartEdbMapping != nil {
  112. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(tmpChartEdbMapping.ChartEdbMappingId))
  113. } else {
  114. mapItem := new(data_manage.ChartEdbMapping)
  115. mapItem.ChartInfoId = req.ChartInfoId
  116. mapItem.EdbInfoId = v.EdbInfoId
  117. mapItem.CreateTime = time.Now()
  118. mapItem.ModifyTime = time.Now()
  119. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  120. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp + "_" + strconv.Itoa(v.EdbInfoId))
  121. mapItem.IsOrder = true
  122. mapItem.IsAxis = 1
  123. mapItem.EdbInfoType = 1
  124. mapItem.Source = utils.CHART_SOURCE_CORRELATION
  125. err = to.Create(mapItem).Error
  126. if err != nil {
  127. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  128. return err
  129. }
  130. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(mapItem.ChartEdbMappingId))
  131. }
  132. }
  133. if len(chartEdbMappingIdList) > 0 {
  134. chartEdbMappingIdStr := strings.Join(chartEdbMappingIdList, ",")
  135. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND chart_edb_mapping_id NOT IN(` + chartEdbMappingIdStr + `)`
  136. err = to.Exec(dsql, req.ChartInfoId).Error
  137. if err != nil {
  138. fmt.Println("delete err:" + err.Error())
  139. return err
  140. }
  141. }
  142. return
  143. }