line_feature.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package line_feature
  2. import (
  3. "eta/eta_api/models/data_manage"
  4. "eta/eta_api/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  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. o := orm.NewOrmUsingDB("data")
  18. tx, err := o.Begin()
  19. if err != nil {
  20. return
  21. }
  22. defer func() {
  23. if err != nil {
  24. _ = tx.Rollback()
  25. } else {
  26. _ = tx.Commit()
  27. }
  28. }()
  29. // 新增图表信息
  30. newId, err := tx.Insert(chartInfo)
  31. if err != nil {
  32. return
  33. }
  34. // 指标mapping
  35. chartInfo.ChartInfoId = int(newId)
  36. chartInfoId = int(newId)
  37. if len(edbMappingList) > 0 {
  38. for i := range edbMappingList {
  39. edbMappingList[i].ChartInfoId = chartInfoId
  40. }
  41. _, err = tx.InsertMulti(len(edbMappingList), edbMappingList)
  42. if err != nil {
  43. return
  44. }
  45. }
  46. return
  47. }
  48. // EditLineFeatureChartAndMapping 批量新增/编辑拟合方程图表
  49. func EditLineFeatureChartAndMapping(req *data_manage.EditChartInfoReq, edbInfoIdStr string, calendar string, dateType, disabled int, extraConfig string) (err error) {
  50. o := orm.NewOrmUsingDB("data")
  51. to, err := o.Begin()
  52. if err != nil {
  53. return
  54. }
  55. defer func() {
  56. if err != nil {
  57. _ = to.Rollback()
  58. } else {
  59. _ = to.Commit()
  60. }
  61. }()
  62. var pars []interface{}
  63. pars = append(pars, req.ChartName)
  64. pars = append(pars, req.ChartNameEn)
  65. pars = append(pars, edbInfoIdStr)
  66. pars = append(pars, req.ChartClassifyId)
  67. pars = append(pars, disabled)
  68. pars = append(pars, extraConfig)
  69. sql := ` UPDATE chart_info
  70. SET
  71. chart_name =?,
  72. chart_name_en =?,
  73. edb_info_ids=?,
  74. chart_classify_id = ?,
  75. modify_time = NOW(),
  76. disabled = ?,
  77. extra_config = ?
  78. `
  79. if calendar != "" {
  80. sql += `,calendar = ? `
  81. pars = append(pars, calendar)
  82. }
  83. if dateType > 0 {
  84. sql += `,date_type = ? `
  85. pars = append(pars, dateType)
  86. }
  87. sql += `,start_date = ? `
  88. pars = append(pars, req.StartDate)
  89. sql += `,end_date = ? `
  90. pars = append(pars, req.EndDate)
  91. sql += `,season_start_date = ? `
  92. pars = append(pars, req.StartDate)
  93. sql += `,season_end_date = ? `
  94. pars = append(pars, req.EndDate)
  95. sql += `,left_min = ? `
  96. pars = append(pars, req.LeftMin)
  97. sql += `,left_max = ? `
  98. pars = append(pars, req.LeftMax)
  99. sql += `,right_min = ? `
  100. pars = append(pars, req.RightMin)
  101. sql += `,right_max = ? `
  102. pars = append(pars, req.RightMax)
  103. sql += `WHERE chart_info_id = ?`
  104. pars = append(pars, req.ChartInfoId)
  105. _, err = to.Raw(sql, pars).Exec()
  106. if err != nil {
  107. fmt.Println("UPDATE chart_info Err:", err.Error())
  108. return err
  109. }
  110. chartEdbMappingIdList := make([]string, 0)
  111. for _, v := range req.CorrelationChartInfo.EdbInfoIdList {
  112. // 查询该指标是否存在,如果存在的话,那么就去修改,否则新增
  113. var tmpChartEdbMapping *data_manage.ChartEdbMapping
  114. csql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id=? AND edb_info_id=? AND source = ? `
  115. err = to.Raw(csql, req.ChartInfoId, v.EdbInfoId, utils.CHART_SOURCE_CORRELATION).QueryRow(&tmpChartEdbMapping)
  116. if err != nil && err.Error() != utils.ErrNoRow() {
  117. fmt.Println("QueryRow Err:", err.Error())
  118. return err
  119. }
  120. if tmpChartEdbMapping != nil {
  121. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(tmpChartEdbMapping.ChartEdbMappingId))
  122. //tmpChartEdbMapping.ModifyTime = time.Now()
  123. //tmpChartEdbMapping.MaxData = v.MaxData
  124. //tmpChartEdbMapping.MinData = v.MinData
  125. //tmpChartEdbMapping.IsOrder = v.IsOrder
  126. //tmpChartEdbMapping.IsAxis = v.IsAxis
  127. //tmpChartEdbMapping.EdbInfoType = v.EdbInfoType
  128. //tmpChartEdbMapping.LeadValue = v.LeadValue
  129. //tmpChartEdbMapping.LeadUnit = v.LeadUnit
  130. //tmpChartEdbMapping.ChartStyle = v.ChartStyle
  131. //tmpChartEdbMapping.ChartColor = v.ChartColor
  132. //tmpChartEdbMapping.PredictChartColor = v.PredictChartColor
  133. //tmpChartEdbMapping.ChartWidth = v.ChartWidth
  134. //_, err = to.Update(tmpChartEdbMapping, "ModifyTime", "MaxData", "MinData", "IsOrder", "IsAxis", "EdbInfoType", "LeadValue", "LeadUnit", "ChartStyle", "ChartColor", "PredictChartColor", "ChartWidth")
  135. //if err != nil {
  136. // fmt.Println("chart_edb_mapping Err:" + err.Error())
  137. // return err
  138. //}
  139. } else {
  140. mapItem := new(data_manage.ChartEdbMapping)
  141. mapItem.ChartInfoId = req.ChartInfoId
  142. mapItem.EdbInfoId = v.EdbInfoId
  143. mapItem.CreateTime = time.Now()
  144. mapItem.ModifyTime = time.Now()
  145. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  146. mapItem.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp + "_" + strconv.Itoa(v.EdbInfoId))
  147. //mapItem.MaxData = v.MaxData
  148. //mapItem.MinData = v.MinData
  149. mapItem.IsOrder = true
  150. mapItem.IsAxis = 1
  151. mapItem.EdbInfoType = 1
  152. //mapItem.LeadValue = v.LeadValue
  153. //mapItem.LeadUnit = v.LeadUnit
  154. //mapItem.ChartStyle = v.ChartStyle
  155. //mapItem.ChartColor = v.ChartColor
  156. //mapItem.PredictChartColor = v.PredictChartColor
  157. //mapItem.ChartWidth = v.ChartWidth
  158. mapItem.Source = utils.CHART_SOURCE_CORRELATION
  159. tmpId, err := to.Insert(mapItem)
  160. if err != nil {
  161. fmt.Println("AddChartEdbMapping Err:" + err.Error())
  162. return err
  163. }
  164. mapItem.ChartEdbMappingId = int(tmpId)
  165. chartEdbMappingIdList = append(chartEdbMappingIdList, strconv.Itoa(mapItem.ChartEdbMappingId))
  166. }
  167. }
  168. if len(chartEdbMappingIdList) > 0 {
  169. chartEdbMappingIdStr := strings.Join(chartEdbMappingIdList, ",")
  170. dsql := `DELETE FROM chart_edb_mapping WHERE chart_info_id=? AND chart_edb_mapping_id NOT IN(` + chartEdbMappingIdStr + `)`
  171. _, err = to.Raw(dsql, req.ChartInfoId).Exec()
  172. if err != nil {
  173. fmt.Println("delete err:" + err.Error())
  174. return err
  175. }
  176. }
  177. return
  178. }